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>");
33 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
35 static bool disable_tap_to_click;
36 module_param(disable_tap_to_click, bool, 0644);
37 MODULE_PARM_DESC(disable_tap_to_click,
38 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
40 /* Define a non-zero software ID to identify our own requests */
41 #define LINUX_KERNEL_SW_ID 0x01
43 #define REPORT_ID_HIDPP_SHORT 0x10
44 #define REPORT_ID_HIDPP_LONG 0x11
45 #define REPORT_ID_HIDPP_VERY_LONG 0x12
47 #define HIDPP_REPORT_SHORT_LENGTH 7
48 #define HIDPP_REPORT_LONG_LENGTH 20
49 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
51 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
52 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
53 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
55 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
56 #define HIDPP_SUB_ID_ROLLER 0x05
57 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
58 #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
59 #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
61 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
62 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
63 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
64 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
65 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
67 /* bits 2..20 are reserved for classes */
68 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
69 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
70 #define HIDPP_QUIRK_DELAYED_INIT BIT(23)
71 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
72 #define HIDPP_QUIRK_UNIFYING BIT(25)
73 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(26)
74 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(27)
75 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(28)
76 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(29)
78 /* These are just aliases for now */
79 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
80 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
82 /* Convenience constant to check for any high-res support. */
83 #define HIDPP_CAPABILITY_HI_RES_SCROLL (HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \
84 HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \
85 HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL)
87 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
88 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
89 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
90 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
91 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
92 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
93 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
94 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL BIT(7)
95 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL BIT(8)
96 #define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL BIT(9)
98 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
101 * There are two hidpp protocols in use, the first version hidpp10 is known
102 * as register access protocol or RAP, the second version hidpp20 is known as
103 * feature access protocol or FAP
105 * Most older devices (including the Unifying usb receiver) use the RAP protocol
106 * where as most newer devices use the FAP protocol. Both protocols are
107 * compatible with the underlying transport, which could be usb, Unifiying, or
108 * bluetooth. The message lengths are defined by the hid vendor specific report
109 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
110 * the HIDPP_LONG report type (total message length 20 bytes)
112 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
113 * messages. The Unifying receiver itself responds to RAP messages (device index
114 * is 0xFF for the receiver), and all messages (short or long) with a device
115 * index between 1 and 6 are passed untouched to the corresponding paired
118 * The paired device can be RAP or FAP, it will receive the message untouched
119 * from the Unifiying receiver.
124 u8 funcindex_clientid;
125 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
131 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
134 struct hidpp_report {
140 u8 rawbytes[sizeof(struct fap)];
144 struct hidpp_battery {
146 u8 solar_feature_index;
147 u8 voltage_feature_index;
148 struct power_supply_desc desc;
149 struct power_supply *ps;
157 u8 supported_levels_1004;
161 * struct hidpp_scroll_counter - Utility class for processing high-resolution
163 * @dev: the input device for which events should be reported.
164 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
165 * @remainder: counts the number of high-resolution units moved since the last
166 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
167 * only be used by class methods.
168 * @direction: direction of last movement (1 or -1)
169 * @last_time: last event time, used to reset remainder after inactivity
171 struct hidpp_scroll_counter {
172 int wheel_multiplier;
175 unsigned long long last_time;
178 struct hidpp_device {
179 struct hid_device *hid_dev;
180 struct input_dev *input;
181 struct mutex send_mutex;
182 void *send_receive_buf;
183 char *name; /* will never be NULL and should not be freed */
184 wait_queue_head_t wait;
185 int very_long_report_length;
186 bool answer_available;
192 struct work_struct work;
193 struct kfifo delayed_work_fifo;
195 struct input_dev *delayed_input;
197 unsigned long quirks;
198 unsigned long capabilities;
199 u8 supported_reports;
201 struct hidpp_battery battery;
202 struct hidpp_scroll_counter vertical_wheel_counter;
204 u8 wireless_feature_index;
207 /* HID++ 1.0 error codes */
208 #define HIDPP_ERROR 0x8f
209 #define HIDPP_ERROR_SUCCESS 0x00
210 #define HIDPP_ERROR_INVALID_SUBID 0x01
211 #define HIDPP_ERROR_INVALID_ADRESS 0x02
212 #define HIDPP_ERROR_INVALID_VALUE 0x03
213 #define HIDPP_ERROR_CONNECT_FAIL 0x04
214 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
215 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
216 #define HIDPP_ERROR_BUSY 0x07
217 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
218 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
219 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
220 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
221 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
222 /* HID++ 2.0 error codes */
223 #define HIDPP20_ERROR_NO_ERROR 0x00
224 #define HIDPP20_ERROR_UNKNOWN 0x01
225 #define HIDPP20_ERROR_INVALID_ARGS 0x02
226 #define HIDPP20_ERROR_OUT_OF_RANGE 0x03
227 #define HIDPP20_ERROR_HW_ERROR 0x04
228 #define HIDPP20_ERROR_LOGITECH_INTERNAL 0x05
229 #define HIDPP20_ERROR_INVALID_FEATURE_INDEX 0x06
230 #define HIDPP20_ERROR_INVALID_FUNCTION_ID 0x07
231 #define HIDPP20_ERROR_BUSY 0x08
232 #define HIDPP20_ERROR_UNSUPPORTED 0x09
233 #define HIDPP20_ERROR 0xff
235 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
237 static int __hidpp_send_report(struct hid_device *hdev,
238 struct hidpp_report *hidpp_report)
240 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
241 int fields_count, ret;
243 switch (hidpp_report->report_id) {
244 case REPORT_ID_HIDPP_SHORT:
245 fields_count = HIDPP_REPORT_SHORT_LENGTH;
247 case REPORT_ID_HIDPP_LONG:
248 fields_count = HIDPP_REPORT_LONG_LENGTH;
250 case REPORT_ID_HIDPP_VERY_LONG:
251 fields_count = hidpp->very_long_report_length;
258 * set the device_index as the receiver, it will be overwritten by
259 * hid_hw_request if needed
261 hidpp_report->device_index = 0xff;
263 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
264 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
266 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
267 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
271 return ret == fields_count ? 0 : -1;
275 * hidpp_send_message_sync() returns 0 in case of success, and something else
276 * in case of a failure.
277 * - If ' something else' is positive, that means that an error has been raised
278 * by the protocol itself.
279 * - If ' something else' is negative, that means that we had a classic error
280 * (-ENOMEM, -EPIPE, etc...)
282 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
283 struct hidpp_report *message,
284 struct hidpp_report *response)
289 mutex_lock(&hidpp->send_mutex);
291 hidpp->send_receive_buf = response;
292 hidpp->answer_available = false;
295 * So that we can later validate the answer when it arrives
298 *response = *message;
300 for (; max_retries != 0; max_retries--) {
301 ret = __hidpp_send_report(hidpp->hid_dev, message);
304 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
305 memset(response, 0, sizeof(struct hidpp_report));
309 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
311 dbg_hid("%s:timeout waiting for response\n", __func__);
312 memset(response, 0, sizeof(struct hidpp_report));
316 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
317 response->rap.sub_id == HIDPP_ERROR) {
318 ret = response->rap.params[1];
319 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
323 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
324 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
325 response->fap.feature_index == HIDPP20_ERROR) {
326 ret = response->fap.params[1];
327 if (ret != HIDPP20_ERROR_BUSY) {
328 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
331 dbg_hid("%s:got busy hidpp 2.0 error %02X, retrying\n", __func__, ret);
336 mutex_unlock(&hidpp->send_mutex);
341 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
342 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
343 struct hidpp_report *response)
345 struct hidpp_report *message;
348 if (param_count > sizeof(message->fap.params)) {
349 hid_dbg(hidpp->hid_dev,
350 "Invalid number of parameters passed to command (%d != %llu)\n",
352 (unsigned long long) sizeof(message->fap.params));
356 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
360 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
361 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
363 message->report_id = REPORT_ID_HIDPP_LONG;
364 message->fap.feature_index = feat_index;
365 message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID;
366 memcpy(&message->fap.params, params, param_count);
368 ret = hidpp_send_message_sync(hidpp, message, response);
373 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
374 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
375 struct hidpp_report *response)
377 struct hidpp_report *message;
380 /* Send as long report if short reports are not supported. */
381 if (report_id == REPORT_ID_HIDPP_SHORT &&
382 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
383 report_id = REPORT_ID_HIDPP_LONG;
386 case REPORT_ID_HIDPP_SHORT:
387 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
389 case REPORT_ID_HIDPP_LONG:
390 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
392 case REPORT_ID_HIDPP_VERY_LONG:
393 max_count = hidpp_dev->very_long_report_length - 4;
399 if (param_count > max_count)
402 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
405 message->report_id = report_id;
406 message->rap.sub_id = sub_id;
407 message->rap.reg_address = reg_address;
408 memcpy(&message->rap.params, params, param_count);
410 ret = hidpp_send_message_sync(hidpp_dev, message, response);
415 static void delayed_work_cb(struct work_struct *work)
417 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
419 hidpp_connect_event(hidpp);
422 static inline bool hidpp_match_answer(struct hidpp_report *question,
423 struct hidpp_report *answer)
425 return (answer->fap.feature_index == question->fap.feature_index) &&
426 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
429 static inline bool hidpp_match_error(struct hidpp_report *question,
430 struct hidpp_report *answer)
432 return ((answer->rap.sub_id == HIDPP_ERROR) ||
433 (answer->fap.feature_index == HIDPP20_ERROR)) &&
434 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
435 (answer->fap.params[0] == question->fap.funcindex_clientid);
438 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
439 struct hidpp_report *report)
441 return (hidpp->wireless_feature_index &&
442 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
443 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
444 (report->rap.sub_id == 0x41));
448 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
450 static void hidpp_prefix_name(char **name, int name_length)
452 #define PREFIX_LENGTH 9 /* "Logitech " */
457 if (name_length > PREFIX_LENGTH &&
458 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
459 /* The prefix has is already in the name */
462 new_length = PREFIX_LENGTH + name_length;
463 new_name = kzalloc(new_length, GFP_KERNEL);
467 snprintf(new_name, new_length, "Logitech %s", *name);
475 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
476 * events given a high-resolution wheel
478 * @input_dev: Pointer to the input device
479 * @counter: a hid_scroll_counter struct describing the wheel.
480 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
483 * Given a high-resolution movement, this function converts the movement into
484 * fractions of 120 and emits high-resolution scroll events for the input
485 * device. It also uses the multiplier from &struct hid_scroll_counter to
486 * emit low-resolution scroll events when appropriate for
487 * backwards-compatibility with userspace input libraries.
489 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
490 struct hidpp_scroll_counter *counter,
493 int low_res_value, remainder, direction;
494 unsigned long long now, previous;
496 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
497 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
499 remainder = counter->remainder;
500 direction = hi_res_value > 0 ? 1 : -1;
503 previous = counter->last_time;
504 counter->last_time = now;
506 * Reset the remainder after a period of inactivity or when the
507 * direction changes. This prevents the REL_WHEEL emulation point
508 * from sliding for devices that don't always provide the same
509 * number of movements per detent.
511 if (now - previous > 1000000000 || direction != counter->direction)
514 counter->direction = direction;
515 remainder += hi_res_value;
517 /* Some wheels will rest 7/8ths of a detent from the previous detent
518 * after slow movement, so we want the threshold for low-res events to
519 * be in the middle between two detents (e.g. after 4/8ths) as
520 * opposed to on the detents themselves (8/8ths).
522 if (abs(remainder) >= 60) {
523 /* Add (or subtract) 1 because we want to trigger when the wheel
524 * is half-way to the next detent (i.e. scroll 1 detent after a
525 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
528 low_res_value = remainder / 120;
529 if (low_res_value == 0)
530 low_res_value = (hi_res_value > 0 ? 1 : -1);
531 input_report_rel(input_dev, REL_WHEEL, low_res_value);
532 remainder -= low_res_value * 120;
534 counter->remainder = remainder;
537 /* -------------------------------------------------------------------------- */
538 /* HIDP++ 1.0 commands */
539 /* -------------------------------------------------------------------------- */
541 #define HIDPP_SET_REGISTER 0x80
542 #define HIDPP_GET_REGISTER 0x81
543 #define HIDPP_SET_LONG_REGISTER 0x82
544 #define HIDPP_GET_LONG_REGISTER 0x83
547 * hidpp10_set_register - Modify a HID++ 1.0 register.
548 * @hidpp_dev: the device to set the register on.
549 * @register_address: the address of the register to modify.
550 * @byte: the byte of the register to modify. Should be less than 3.
551 * @mask: mask of the bits to modify
552 * @value: new values for the bits in mask
553 * Return: 0 if successful, otherwise a negative error code.
555 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
556 u8 register_address, u8 byte, u8 mask, u8 value)
558 struct hidpp_report response;
560 u8 params[3] = { 0 };
562 ret = hidpp_send_rap_command_sync(hidpp_dev,
563 REPORT_ID_HIDPP_SHORT,
570 memcpy(params, response.rap.params, 3);
572 params[byte] &= ~mask;
573 params[byte] |= value & mask;
575 return hidpp_send_rap_command_sync(hidpp_dev,
576 REPORT_ID_HIDPP_SHORT,
579 params, 3, &response);
582 #define HIDPP_REG_ENABLE_REPORTS 0x00
583 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
584 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
585 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
586 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
587 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
589 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
591 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
592 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
595 #define HIDPP_REG_FEATURES 0x01
596 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
597 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
599 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
600 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
602 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
603 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
606 #define HIDPP_REG_BATTERY_STATUS 0x07
608 static int hidpp10_battery_status_map_level(u8 param)
614 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
617 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
620 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
623 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
626 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
632 static int hidpp10_battery_status_map_status(u8 param)
638 /* discharging (in use) */
639 status = POWER_SUPPLY_STATUS_DISCHARGING;
641 case 0x21: /* (standard) charging */
642 case 0x24: /* fast charging */
643 case 0x25: /* slow charging */
644 status = POWER_SUPPLY_STATUS_CHARGING;
646 case 0x26: /* topping charge */
647 case 0x22: /* charge complete */
648 status = POWER_SUPPLY_STATUS_FULL;
650 case 0x20: /* unknown */
651 status = POWER_SUPPLY_STATUS_UNKNOWN;
654 * 0x01...0x1F = reserved (not charging)
655 * 0x23 = charging error
656 * 0x27..0xff = reserved
659 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
666 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
668 struct hidpp_report response;
671 ret = hidpp_send_rap_command_sync(hidpp,
672 REPORT_ID_HIDPP_SHORT,
674 HIDPP_REG_BATTERY_STATUS,
679 hidpp->battery.level =
680 hidpp10_battery_status_map_level(response.rap.params[0]);
681 status = hidpp10_battery_status_map_status(response.rap.params[1]);
682 hidpp->battery.status = status;
683 /* the capacity is only available when discharging or full */
684 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
685 status == POWER_SUPPLY_STATUS_FULL;
690 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
692 static int hidpp10_battery_mileage_map_status(u8 param)
696 switch (param >> 6) {
698 /* discharging (in use) */
699 status = POWER_SUPPLY_STATUS_DISCHARGING;
701 case 0x01: /* charging */
702 status = POWER_SUPPLY_STATUS_CHARGING;
704 case 0x02: /* charge complete */
705 status = POWER_SUPPLY_STATUS_FULL;
708 * 0x03 = charging error
711 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
718 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
720 struct hidpp_report response;
723 ret = hidpp_send_rap_command_sync(hidpp,
724 REPORT_ID_HIDPP_SHORT,
726 HIDPP_REG_BATTERY_MILEAGE,
731 hidpp->battery.capacity = response.rap.params[0];
732 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
733 hidpp->battery.status = status;
734 /* the capacity is only available when discharging or full */
735 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
736 status == POWER_SUPPLY_STATUS_FULL;
741 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
743 struct hidpp_report *report = (struct hidpp_report *)data;
744 int status, capacity, level;
747 if (report->report_id != REPORT_ID_HIDPP_SHORT)
750 switch (report->rap.sub_id) {
751 case HIDPP_REG_BATTERY_STATUS:
752 capacity = hidpp->battery.capacity;
753 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
754 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
756 case HIDPP_REG_BATTERY_MILEAGE:
757 capacity = report->rap.params[0];
758 level = hidpp->battery.level;
759 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
765 changed = capacity != hidpp->battery.capacity ||
766 level != hidpp->battery.level ||
767 status != hidpp->battery.status;
769 /* the capacity is only available when discharging or full */
770 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
771 status == POWER_SUPPLY_STATUS_FULL;
774 hidpp->battery.level = level;
775 hidpp->battery.status = status;
776 if (hidpp->battery.ps)
777 power_supply_changed(hidpp->battery.ps);
783 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
784 #define HIDPP_EXTENDED_PAIRING 0x30
785 #define HIDPP_DEVICE_NAME 0x40
787 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
789 struct hidpp_report response;
791 u8 params[1] = { HIDPP_DEVICE_NAME };
795 ret = hidpp_send_rap_command_sync(hidpp_dev,
796 REPORT_ID_HIDPP_SHORT,
797 HIDPP_GET_LONG_REGISTER,
798 HIDPP_REG_PAIRING_INFORMATION,
799 params, 1, &response);
803 len = response.rap.params[1];
805 if (2 + len > sizeof(response.rap.params))
808 if (len < 4) /* logitech devices are usually at least Xddd */
811 name = kzalloc(len + 1, GFP_KERNEL);
815 memcpy(name, &response.rap.params[2], len);
817 /* include the terminating '\0' */
818 hidpp_prefix_name(&name, len + 1);
823 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
825 struct hidpp_report response;
827 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
829 ret = hidpp_send_rap_command_sync(hidpp,
830 REPORT_ID_HIDPP_SHORT,
831 HIDPP_GET_LONG_REGISTER,
832 HIDPP_REG_PAIRING_INFORMATION,
833 params, 1, &response);
838 * We don't care about LE or BE, we will output it as a string
839 * with %4phD, so we need to keep the order.
841 *serial = *((u32 *)&response.rap.params[1]);
845 static int hidpp_unifying_init(struct hidpp_device *hidpp)
847 struct hid_device *hdev = hidpp->hid_dev;
852 ret = hidpp_unifying_get_serial(hidpp, &serial);
856 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
857 hdev->product, &serial);
858 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
860 name = hidpp_unifying_get_name(hidpp);
864 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
865 dbg_hid("HID++ Unifying: Got name: %s\n", name);
871 /* -------------------------------------------------------------------------- */
873 /* -------------------------------------------------------------------------- */
875 #define HIDPP_PAGE_ROOT 0x0000
876 #define HIDPP_PAGE_ROOT_IDX 0x00
878 #define CMD_ROOT_GET_FEATURE 0x00
879 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x10
881 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
882 u8 *feature_index, u8 *feature_type)
884 struct hidpp_report response;
886 u8 params[2] = { feature >> 8, feature & 0x00FF };
888 ret = hidpp_send_fap_command_sync(hidpp,
890 CMD_ROOT_GET_FEATURE,
891 params, 2, &response);
895 if (response.fap.params[0] == 0)
898 *feature_index = response.fap.params[0];
899 *feature_type = response.fap.params[1];
904 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
906 const u8 ping_byte = 0x5a;
907 u8 ping_data[3] = { 0, 0, ping_byte };
908 struct hidpp_report response;
911 ret = hidpp_send_rap_command_sync(hidpp,
912 REPORT_ID_HIDPP_SHORT,
914 CMD_ROOT_GET_PROTOCOL_VERSION | LINUX_KERNEL_SW_ID,
915 ping_data, sizeof(ping_data), &response);
917 if (ret == HIDPP_ERROR_INVALID_SUBID) {
918 hidpp->protocol_major = 1;
919 hidpp->protocol_minor = 0;
923 /* the device might not be connected */
924 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
928 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
935 if (response.rap.params[2] != ping_byte) {
936 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
937 __func__, response.rap.params[2], ping_byte);
941 hidpp->protocol_major = response.rap.params[0];
942 hidpp->protocol_minor = response.rap.params[1];
945 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
946 hidpp->protocol_major, hidpp->protocol_minor);
950 /* -------------------------------------------------------------------------- */
951 /* 0x0005: GetDeviceNameType */
952 /* -------------------------------------------------------------------------- */
954 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
956 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x00
957 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x10
958 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x20
960 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
961 u8 feature_index, u8 *nameLength)
963 struct hidpp_report response;
966 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
967 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
970 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
977 *nameLength = response.fap.params[0];
982 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
983 u8 feature_index, u8 char_index, char *device_name, int len_buf)
985 struct hidpp_report response;
989 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
990 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
994 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1001 switch (response.report_id) {
1002 case REPORT_ID_HIDPP_VERY_LONG:
1003 count = hidpp->very_long_report_length - 4;
1005 case REPORT_ID_HIDPP_LONG:
1006 count = HIDPP_REPORT_LONG_LENGTH - 4;
1008 case REPORT_ID_HIDPP_SHORT:
1009 count = HIDPP_REPORT_SHORT_LENGTH - 4;
1015 if (len_buf < count)
1018 for (i = 0; i < count; i++)
1019 device_name[i] = response.fap.params[i];
1024 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1033 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1034 &feature_index, &feature_type);
1038 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1043 name = kzalloc(__name_length + 1, GFP_KERNEL);
1047 while (index < __name_length) {
1048 ret = hidpp_devicenametype_get_device_name(hidpp,
1049 feature_index, index, name + index,
1050 __name_length - index);
1058 /* include the terminating '\0' */
1059 hidpp_prefix_name(&name, __name_length + 1);
1064 /* -------------------------------------------------------------------------- */
1065 /* 0x1000: Battery level status */
1066 /* -------------------------------------------------------------------------- */
1068 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1070 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1071 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1073 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1075 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1076 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1077 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1079 static int hidpp_map_battery_level(int capacity)
1082 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1084 * The spec says this should be < 31 but some devices report 30
1085 * with brand new batteries and Windows reports 30 as "Good".
1087 else if (capacity < 30)
1088 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1089 else if (capacity < 81)
1090 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1091 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1094 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1100 *capacity = data[0];
1101 *next_capacity = data[1];
1102 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1104 /* When discharging, we can rely on the device reported capacity.
1105 * For all other states the device reports 0 (unknown).
1108 case 0: /* discharging (in use) */
1109 status = POWER_SUPPLY_STATUS_DISCHARGING;
1110 *level = hidpp_map_battery_level(*capacity);
1112 case 1: /* recharging */
1113 status = POWER_SUPPLY_STATUS_CHARGING;
1115 case 2: /* charge in final stage */
1116 status = POWER_SUPPLY_STATUS_CHARGING;
1118 case 3: /* charge complete */
1119 status = POWER_SUPPLY_STATUS_FULL;
1120 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1123 case 4: /* recharging below optimal speed */
1124 status = POWER_SUPPLY_STATUS_CHARGING;
1126 /* 5 = invalid battery type
1128 7 = other charging error */
1130 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1137 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1144 struct hidpp_report response;
1146 u8 *params = (u8 *)response.fap.params;
1148 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1149 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1150 NULL, 0, &response);
1151 /* Ignore these intermittent errors */
1152 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1155 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1162 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1169 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1172 struct hidpp_report response;
1174 u8 *params = (u8 *)response.fap.params;
1175 unsigned int level_count, flags;
1177 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1178 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1179 NULL, 0, &response);
1181 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1188 level_count = params[0];
1191 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1192 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1194 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1199 static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1203 int status, capacity, next_capacity, level;
1205 if (hidpp->battery.feature_index == 0xff) {
1206 ret = hidpp_root_get_feature(hidpp,
1207 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1208 &hidpp->battery.feature_index,
1214 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1215 hidpp->battery.feature_index,
1217 &next_capacity, &level);
1221 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1222 hidpp->battery.feature_index);
1226 hidpp->battery.status = status;
1227 hidpp->battery.capacity = capacity;
1228 hidpp->battery.level = level;
1229 /* the capacity is only available when discharging or full */
1230 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1231 status == POWER_SUPPLY_STATUS_FULL;
1236 static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1239 struct hidpp_report *report = (struct hidpp_report *)data;
1240 int status, capacity, next_capacity, level;
1243 if (report->fap.feature_index != hidpp->battery.feature_index ||
1244 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1247 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1252 /* the capacity is only available when discharging or full */
1253 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1254 status == POWER_SUPPLY_STATUS_FULL;
1256 changed = capacity != hidpp->battery.capacity ||
1257 level != hidpp->battery.level ||
1258 status != hidpp->battery.status;
1261 hidpp->battery.level = level;
1262 hidpp->battery.capacity = capacity;
1263 hidpp->battery.status = status;
1264 if (hidpp->battery.ps)
1265 power_supply_changed(hidpp->battery.ps);
1271 /* -------------------------------------------------------------------------- */
1272 /* 0x1001: Battery voltage */
1273 /* -------------------------------------------------------------------------- */
1275 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1277 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1279 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1281 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1282 int *level, int *charge_type)
1286 long flags = (long) data[2];
1287 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1290 switch (flags & 0x07) {
1292 status = POWER_SUPPLY_STATUS_CHARGING;
1295 status = POWER_SUPPLY_STATUS_FULL;
1296 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1299 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1302 status = POWER_SUPPLY_STATUS_UNKNOWN;
1306 status = POWER_SUPPLY_STATUS_DISCHARGING;
1308 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1309 if (test_bit(3, &flags)) {
1310 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1312 if (test_bit(4, &flags)) {
1313 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1315 if (test_bit(5, &flags)) {
1316 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1319 *voltage = get_unaligned_be16(data);
1324 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1326 int *status, int *voltage,
1327 int *level, int *charge_type)
1329 struct hidpp_report response;
1331 u8 *params = (u8 *)response.fap.params;
1333 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1334 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1335 NULL, 0, &response);
1338 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1345 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1347 *status = hidpp20_battery_map_status_voltage(params, voltage,
1348 level, charge_type);
1353 static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1355 /* NB: This voltage curve doesn't necessarily map perfectly to all
1356 * devices that implement the BATTERY_VOLTAGE feature. This is because
1357 * there are a few devices that use different battery technology.
1360 static const int voltages[] = {
1361 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1362 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1363 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1364 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1365 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1366 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1367 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1368 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1369 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1370 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1375 BUILD_BUG_ON(ARRAY_SIZE(voltages) != 100);
1377 if (unlikely(voltage < 3500 || voltage >= 5000))
1378 hid_warn_once(hid_dev,
1379 "%s: possibly using the wrong voltage curve\n",
1382 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1383 if (voltage >= voltages[i])
1384 return ARRAY_SIZE(voltages) - i;
1390 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1394 int status, voltage, level, charge_type;
1396 if (hidpp->battery.voltage_feature_index == 0xff) {
1397 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1398 &hidpp->battery.voltage_feature_index,
1404 ret = hidpp20_battery_get_battery_voltage(hidpp,
1405 hidpp->battery.voltage_feature_index,
1406 &status, &voltage, &level, &charge_type);
1411 hidpp->battery.status = status;
1412 hidpp->battery.voltage = voltage;
1413 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1415 hidpp->battery.level = level;
1416 hidpp->battery.charge_type = charge_type;
1417 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1422 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1425 struct hidpp_report *report = (struct hidpp_report *)data;
1426 int status, voltage, level, charge_type;
1428 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1429 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1432 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1433 &level, &charge_type);
1435 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1437 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1438 hidpp->battery.voltage = voltage;
1439 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev,
1441 hidpp->battery.status = status;
1442 hidpp->battery.level = level;
1443 hidpp->battery.charge_type = charge_type;
1444 if (hidpp->battery.ps)
1445 power_supply_changed(hidpp->battery.ps);
1450 /* -------------------------------------------------------------------------- */
1451 /* 0x1004: Unified battery */
1452 /* -------------------------------------------------------------------------- */
1454 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1456 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1457 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1459 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1461 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1462 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1463 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1464 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1466 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1467 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1469 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1472 struct hidpp_report response;
1474 u8 *params = (u8 *)response.fap.params;
1476 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1477 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1478 /* we have already set the device capabilities, so let's skip */
1482 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1483 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1484 NULL, 0, &response);
1485 /* Ignore these intermittent errors */
1486 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1489 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1497 * If the device supports state of charge (battery percentage) we won't
1498 * export the battery level information. there are 4 possible battery
1499 * levels and they all are optional, this means that the device might
1500 * not support any of them, we are just better off with the battery
1503 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1504 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1505 hidpp->battery.supported_levels_1004 = 0;
1507 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1508 hidpp->battery.supported_levels_1004 = params[0];
1514 static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1516 u8 external_power_status)
1520 switch (charging_status) {
1521 case 0: /* discharging */
1522 status = POWER_SUPPLY_STATUS_DISCHARGING;
1524 case 1: /* charging */
1525 case 2: /* charging slow */
1526 status = POWER_SUPPLY_STATUS_CHARGING;
1528 case 3: /* complete */
1529 status = POWER_SUPPLY_STATUS_FULL;
1532 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1533 hid_info(hidpp->hid_dev, "%s: charging error",
1537 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1544 static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1547 /* cler unsupported level bits */
1548 battery_level &= hidpp->battery.supported_levels_1004;
1550 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1551 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1552 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1553 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1554 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1555 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1556 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1557 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1559 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1562 static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1564 u8 *state_of_charge,
1568 struct hidpp_report response;
1570 u8 *params = (u8 *)response.fap.params;
1572 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1573 CMD_UNIFIED_BATTERY_GET_STATUS,
1574 NULL, 0, &response);
1575 /* Ignore these intermittent errors */
1576 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1579 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1586 *state_of_charge = params[0];
1587 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1588 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1593 static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1600 if (hidpp->battery.feature_index == 0xff) {
1601 ret = hidpp_root_get_feature(hidpp,
1602 HIDPP_PAGE_UNIFIED_BATTERY,
1603 &hidpp->battery.feature_index,
1609 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1610 hidpp->battery.feature_index);
1614 ret = hidpp20_unifiedbattery_get_status(hidpp,
1615 hidpp->battery.feature_index,
1622 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1623 hidpp->battery.capacity = state_of_charge;
1624 hidpp->battery.status = status;
1625 hidpp->battery.level = level;
1626 hidpp->battery.online = true;
1631 static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1634 struct hidpp_report *report = (struct hidpp_report *)data;
1635 u8 *params = (u8 *)report->fap.params;
1636 int state_of_charge, status, level;
1639 if (report->fap.feature_index != hidpp->battery.feature_index ||
1640 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1643 state_of_charge = params[0];
1644 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1645 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1647 changed = status != hidpp->battery.status ||
1648 (state_of_charge != hidpp->battery.capacity &&
1649 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1650 (level != hidpp->battery.level &&
1651 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1654 hidpp->battery.capacity = state_of_charge;
1655 hidpp->battery.status = status;
1656 hidpp->battery.level = level;
1657 if (hidpp->battery.ps)
1658 power_supply_changed(hidpp->battery.ps);
1664 /* -------------------------------------------------------------------------- */
1665 /* Battery feature helpers */
1666 /* -------------------------------------------------------------------------- */
1668 static enum power_supply_property hidpp_battery_props[] = {
1669 POWER_SUPPLY_PROP_ONLINE,
1670 POWER_SUPPLY_PROP_STATUS,
1671 POWER_SUPPLY_PROP_SCOPE,
1672 POWER_SUPPLY_PROP_MODEL_NAME,
1673 POWER_SUPPLY_PROP_MANUFACTURER,
1674 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1675 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1676 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1677 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1680 static int hidpp_battery_get_property(struct power_supply *psy,
1681 enum power_supply_property psp,
1682 union power_supply_propval *val)
1684 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1688 case POWER_SUPPLY_PROP_STATUS:
1689 val->intval = hidpp->battery.status;
1691 case POWER_SUPPLY_PROP_CAPACITY:
1692 val->intval = hidpp->battery.capacity;
1694 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1695 val->intval = hidpp->battery.level;
1697 case POWER_SUPPLY_PROP_SCOPE:
1698 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1700 case POWER_SUPPLY_PROP_ONLINE:
1701 val->intval = hidpp->battery.online;
1703 case POWER_SUPPLY_PROP_MODEL_NAME:
1704 if (!strncmp(hidpp->name, "Logitech ", 9))
1705 val->strval = hidpp->name + 9;
1707 val->strval = hidpp->name;
1709 case POWER_SUPPLY_PROP_MANUFACTURER:
1710 val->strval = "Logitech";
1712 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1713 val->strval = hidpp->hid_dev->uniq;
1715 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1716 /* hardware reports voltage in mV. sysfs expects uV */
1717 val->intval = hidpp->battery.voltage * 1000;
1719 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1720 val->intval = hidpp->battery.charge_type;
1730 /* -------------------------------------------------------------------------- */
1731 /* 0x1d4b: Wireless device status */
1732 /* -------------------------------------------------------------------------- */
1733 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1735 static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1740 ret = hidpp_root_get_feature(hidpp,
1741 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1742 &hidpp->wireless_feature_index,
1748 /* -------------------------------------------------------------------------- */
1749 /* 0x2120: Hi-resolution scrolling */
1750 /* -------------------------------------------------------------------------- */
1752 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1754 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1756 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1757 bool enabled, u8 *multiplier)
1763 struct hidpp_report response;
1765 ret = hidpp_root_get_feature(hidpp,
1766 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1772 params[0] = enabled ? BIT(0) : 0;
1773 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1774 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1775 params, sizeof(params), &response);
1778 *multiplier = response.fap.params[1];
1782 /* -------------------------------------------------------------------------- */
1783 /* 0x2121: HiRes Wheel */
1784 /* -------------------------------------------------------------------------- */
1786 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1788 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1789 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1791 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1797 struct hidpp_report response;
1799 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1800 &feature_index, &feature_type);
1802 goto return_default;
1804 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1805 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1806 NULL, 0, &response);
1808 goto return_default;
1810 *multiplier = response.fap.params[0];
1813 hid_warn(hidpp->hid_dev,
1814 "Couldn't get wheel multiplier (error %d)\n", ret);
1818 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1819 bool high_resolution, bool use_hidpp)
1825 struct hidpp_report response;
1827 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1828 &feature_index, &feature_type);
1832 params[0] = (invert ? BIT(2) : 0) |
1833 (high_resolution ? BIT(1) : 0) |
1834 (use_hidpp ? BIT(0) : 0);
1836 return hidpp_send_fap_command_sync(hidpp, feature_index,
1837 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1838 params, sizeof(params), &response);
1841 /* -------------------------------------------------------------------------- */
1842 /* 0x4301: Solar Keyboard */
1843 /* -------------------------------------------------------------------------- */
1845 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1847 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1849 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1850 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1851 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1853 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1855 struct hidpp_report response;
1856 u8 params[2] = { 1, 1 };
1860 if (hidpp->battery.feature_index == 0xff) {
1861 ret = hidpp_root_get_feature(hidpp,
1862 HIDPP_PAGE_SOLAR_KEYBOARD,
1863 &hidpp->battery.solar_feature_index,
1869 ret = hidpp_send_fap_command_sync(hidpp,
1870 hidpp->battery.solar_feature_index,
1871 CMD_SOLAR_SET_LIGHT_MEASURE,
1872 params, 2, &response);
1874 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1881 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1886 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1889 struct hidpp_report *report = (struct hidpp_report *)data;
1890 int capacity, lux, status;
1893 function = report->fap.funcindex_clientid;
1896 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1897 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1898 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1899 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1902 capacity = report->fap.params[0];
1905 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1906 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1908 status = POWER_SUPPLY_STATUS_CHARGING;
1910 status = POWER_SUPPLY_STATUS_DISCHARGING;
1912 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1914 if (capacity < hidpp->battery.capacity)
1915 status = POWER_SUPPLY_STATUS_DISCHARGING;
1917 status = POWER_SUPPLY_STATUS_CHARGING;
1921 if (capacity == 100)
1922 status = POWER_SUPPLY_STATUS_FULL;
1924 hidpp->battery.online = true;
1925 if (capacity != hidpp->battery.capacity ||
1926 status != hidpp->battery.status) {
1927 hidpp->battery.capacity = capacity;
1928 hidpp->battery.status = status;
1929 if (hidpp->battery.ps)
1930 power_supply_changed(hidpp->battery.ps);
1936 /* -------------------------------------------------------------------------- */
1937 /* 0x6010: Touchpad FW items */
1938 /* -------------------------------------------------------------------------- */
1940 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1942 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1944 struct hidpp_touchpad_fw_items {
1946 uint8_t desired_state;
1952 * send a set state command to the device by reading the current items->state
1953 * field. items is then filled with the current state.
1955 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1957 struct hidpp_touchpad_fw_items *items)
1959 struct hidpp_report response;
1961 u8 *params = (u8 *)response.fap.params;
1963 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1964 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1967 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1974 items->presence = params[0];
1975 items->desired_state = params[1];
1976 items->state = params[2];
1977 items->persistent = params[3];
1982 /* -------------------------------------------------------------------------- */
1983 /* 0x6100: TouchPadRawXY */
1984 /* -------------------------------------------------------------------------- */
1986 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1988 #define CMD_TOUCHPAD_GET_RAW_INFO 0x00
1989 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x20
1991 #define EVENT_TOUCHPAD_RAW_XY 0x00
1993 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1994 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1996 struct hidpp_touchpad_raw_info {
2007 struct hidpp_touchpad_raw_xy_finger {
2017 struct hidpp_touchpad_raw_xy {
2019 struct hidpp_touchpad_raw_xy_finger fingers[2];
2026 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2027 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2029 struct hidpp_report response;
2031 u8 *params = (u8 *)response.fap.params;
2033 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
2034 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
2037 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2044 raw_info->x_size = get_unaligned_be16(¶ms[0]);
2045 raw_info->y_size = get_unaligned_be16(¶ms[2]);
2046 raw_info->z_range = params[4];
2047 raw_info->area_range = params[5];
2048 raw_info->maxcontacts = params[7];
2049 raw_info->origin = params[8];
2050 /* res is given in unit per inch */
2051 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
2056 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2057 u8 feature_index, bool send_raw_reports,
2058 bool sensor_enhanced_settings)
2060 struct hidpp_report response;
2064 * bit 0 - enable raw
2065 * bit 1 - 16bit Z, no area
2066 * bit 2 - enhanced sensitivity
2067 * bit 3 - width, height (4 bits each) instead of area
2068 * bit 4 - send raw + gestures (degrades smoothness)
2069 * remaining bits - reserved
2071 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2073 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2074 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
2077 static void hidpp_touchpad_touch_event(u8 *data,
2078 struct hidpp_touchpad_raw_xy_finger *finger)
2080 u8 x_m = data[0] << 2;
2081 u8 y_m = data[2] << 2;
2083 finger->x = x_m << 6 | data[1];
2084 finger->y = y_m << 6 | data[3];
2086 finger->contact_type = data[0] >> 6;
2087 finger->contact_status = data[2] >> 6;
2089 finger->z = data[4];
2090 finger->area = data[5];
2091 finger->finger_id = data[6] >> 4;
2094 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2095 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2097 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2098 raw_xy->end_of_frame = data[8] & 0x01;
2099 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2100 raw_xy->finger_count = data[15] & 0x0f;
2101 raw_xy->button = (data[8] >> 2) & 0x01;
2103 if (raw_xy->finger_count) {
2104 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2105 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2109 /* -------------------------------------------------------------------------- */
2110 /* 0x8123: Force feedback support */
2111 /* -------------------------------------------------------------------------- */
2113 #define HIDPP_FF_GET_INFO 0x01
2114 #define HIDPP_FF_RESET_ALL 0x11
2115 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2116 #define HIDPP_FF_SET_EFFECT_STATE 0x31
2117 #define HIDPP_FF_DESTROY_EFFECT 0x41
2118 #define HIDPP_FF_GET_APERTURE 0x51
2119 #define HIDPP_FF_SET_APERTURE 0x61
2120 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2121 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2123 #define HIDPP_FF_EFFECT_STATE_GET 0x00
2124 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
2125 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2126 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2128 #define HIDPP_FF_EFFECT_CONSTANT 0x00
2129 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2130 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2131 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2132 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2133 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2134 #define HIDPP_FF_EFFECT_SPRING 0x06
2135 #define HIDPP_FF_EFFECT_DAMPER 0x07
2136 #define HIDPP_FF_EFFECT_FRICTION 0x08
2137 #define HIDPP_FF_EFFECT_INERTIA 0x09
2138 #define HIDPP_FF_EFFECT_RAMP 0x0A
2140 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
2142 #define HIDPP_FF_EFFECTID_NONE -1
2143 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
2144 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2146 #define HIDPP_FF_MAX_PARAMS 20
2147 #define HIDPP_FF_RESERVED_SLOTS 1
2149 struct hidpp_ff_private_data {
2150 struct hidpp_device *hidpp;
2158 struct workqueue_struct *wq;
2159 atomic_t workqueue_size;
2162 struct hidpp_ff_work_data {
2163 struct work_struct work;
2164 struct hidpp_ff_private_data *data;
2167 u8 params[HIDPP_FF_MAX_PARAMS];
2171 static const signed short hidpp_ff_effects[] = {
2186 static const signed short hidpp_ff_effects_v2[] = {
2193 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2194 HIDPP_FF_EFFECT_SPRING,
2195 HIDPP_FF_EFFECT_FRICTION,
2196 HIDPP_FF_EFFECT_DAMPER,
2197 HIDPP_FF_EFFECT_INERTIA
2200 static const char *HIDPP_FF_CONDITION_NAMES[] = {
2208 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2212 for (i = 0; i < data->num_effects; i++)
2213 if (data->effect_ids[i] == effect_id)
2219 static void hidpp_ff_work_handler(struct work_struct *w)
2221 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2222 struct hidpp_ff_private_data *data = wd->data;
2223 struct hidpp_report response;
2227 /* add slot number if needed */
2228 switch (wd->effect_id) {
2229 case HIDPP_FF_EFFECTID_AUTOCENTER:
2230 wd->params[0] = data->slot_autocenter;
2232 case HIDPP_FF_EFFECTID_NONE:
2233 /* leave slot as zero */
2236 /* find current slot for effect */
2237 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2241 /* send command and wait for reply */
2242 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2243 wd->command, wd->params, wd->size, &response);
2246 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2250 /* parse return data */
2251 switch (wd->command) {
2252 case HIDPP_FF_DOWNLOAD_EFFECT:
2253 slot = response.fap.params[0];
2254 if (slot > 0 && slot <= data->num_effects) {
2255 if (wd->effect_id >= 0)
2256 /* regular effect uploaded */
2257 data->effect_ids[slot-1] = wd->effect_id;
2258 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2259 /* autocenter spring uploaded */
2260 data->slot_autocenter = slot;
2263 case HIDPP_FF_DESTROY_EFFECT:
2264 if (wd->effect_id >= 0)
2265 /* regular effect destroyed */
2266 data->effect_ids[wd->params[0]-1] = -1;
2267 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2268 /* autocenter spring destoyed */
2269 data->slot_autocenter = 0;
2271 case HIDPP_FF_SET_GLOBAL_GAINS:
2272 data->gain = (wd->params[0] << 8) + wd->params[1];
2274 case HIDPP_FF_SET_APERTURE:
2275 data->range = (wd->params[0] << 8) + wd->params[1];
2278 /* no action needed */
2283 atomic_dec(&data->workqueue_size);
2287 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2289 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2295 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2298 wd->effect_id = effect_id;
2299 wd->command = command;
2301 memcpy(wd->params, params, size);
2303 s = atomic_inc_return(&data->workqueue_size);
2304 queue_work(data->wq, &wd->work);
2306 /* warn about excessive queue size */
2307 if (s >= 20 && s % 20 == 0)
2308 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2313 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2315 struct hidpp_ff_private_data *data = dev->ff->private;
2320 /* set common parameters */
2321 params[2] = effect->replay.length >> 8;
2322 params[3] = effect->replay.length & 255;
2323 params[4] = effect->replay.delay >> 8;
2324 params[5] = effect->replay.delay & 255;
2326 switch (effect->type) {
2328 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2329 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2330 params[6] = force >> 8;
2331 params[7] = force & 255;
2332 params[8] = effect->u.constant.envelope.attack_level >> 7;
2333 params[9] = effect->u.constant.envelope.attack_length >> 8;
2334 params[10] = effect->u.constant.envelope.attack_length & 255;
2335 params[11] = effect->u.constant.envelope.fade_level >> 7;
2336 params[12] = effect->u.constant.envelope.fade_length >> 8;
2337 params[13] = effect->u.constant.envelope.fade_length & 255;
2339 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2340 effect->u.constant.level,
2341 effect->direction, force);
2342 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2343 effect->u.constant.envelope.attack_level,
2344 effect->u.constant.envelope.attack_length,
2345 effect->u.constant.envelope.fade_level,
2346 effect->u.constant.envelope.fade_length);
2350 switch (effect->u.periodic.waveform) {
2352 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2355 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2358 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2361 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2364 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2367 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2370 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2371 params[6] = effect->u.periodic.magnitude >> 8;
2372 params[7] = effect->u.periodic.magnitude & 255;
2373 params[8] = effect->u.periodic.offset >> 8;
2374 params[9] = effect->u.periodic.offset & 255;
2375 params[10] = effect->u.periodic.period >> 8;
2376 params[11] = effect->u.periodic.period & 255;
2377 params[12] = effect->u.periodic.phase >> 8;
2378 params[13] = effect->u.periodic.phase & 255;
2379 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2380 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2381 params[16] = effect->u.periodic.envelope.attack_length & 255;
2382 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2383 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2384 params[19] = effect->u.periodic.envelope.fade_length & 255;
2386 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2387 effect->u.periodic.magnitude, effect->direction,
2388 effect->u.periodic.offset,
2389 effect->u.periodic.period,
2390 effect->u.periodic.phase);
2391 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2392 effect->u.periodic.envelope.attack_level,
2393 effect->u.periodic.envelope.attack_length,
2394 effect->u.periodic.envelope.fade_level,
2395 effect->u.periodic.envelope.fade_length);
2399 params[1] = HIDPP_FF_EFFECT_RAMP;
2400 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2401 params[6] = force >> 8;
2402 params[7] = force & 255;
2403 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2404 params[8] = force >> 8;
2405 params[9] = force & 255;
2406 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2407 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2408 params[12] = effect->u.ramp.envelope.attack_length & 255;
2409 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2410 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2411 params[15] = effect->u.ramp.envelope.fade_length & 255;
2413 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2414 effect->u.ramp.start_level,
2415 effect->u.ramp.end_level,
2416 effect->direction, force);
2417 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2418 effect->u.ramp.envelope.attack_level,
2419 effect->u.ramp.envelope.attack_length,
2420 effect->u.ramp.envelope.fade_level,
2421 effect->u.ramp.envelope.fade_length);
2427 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2428 params[6] = effect->u.condition[0].left_saturation >> 9;
2429 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2430 params[8] = effect->u.condition[0].left_coeff >> 8;
2431 params[9] = effect->u.condition[0].left_coeff & 255;
2432 params[10] = effect->u.condition[0].deadband >> 9;
2433 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2434 params[12] = effect->u.condition[0].center >> 8;
2435 params[13] = effect->u.condition[0].center & 255;
2436 params[14] = effect->u.condition[0].right_coeff >> 8;
2437 params[15] = effect->u.condition[0].right_coeff & 255;
2438 params[16] = effect->u.condition[0].right_saturation >> 9;
2439 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2441 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2442 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2443 effect->u.condition[0].left_coeff,
2444 effect->u.condition[0].left_saturation,
2445 effect->u.condition[0].right_coeff,
2446 effect->u.condition[0].right_saturation);
2447 dbg_hid(" deadband=%d, center=%d\n",
2448 effect->u.condition[0].deadband,
2449 effect->u.condition[0].center);
2452 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2456 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2459 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2461 struct hidpp_ff_private_data *data = dev->ff->private;
2464 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2466 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2468 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2471 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2473 struct hidpp_ff_private_data *data = dev->ff->private;
2476 dbg_hid("Erasing effect %d.\n", effect_id);
2478 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2481 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2483 struct hidpp_ff_private_data *data = dev->ff->private;
2484 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2486 dbg_hid("Setting autocenter to %d.\n", magnitude);
2488 /* start a standard spring effect */
2489 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2490 /* zero delay and duration */
2491 params[2] = params[3] = params[4] = params[5] = 0;
2492 /* set coeff to 25% of saturation */
2493 params[8] = params[14] = magnitude >> 11;
2494 params[9] = params[15] = (magnitude >> 3) & 255;
2495 params[6] = params[16] = magnitude >> 9;
2496 params[7] = params[17] = (magnitude >> 1) & 255;
2497 /* zero deadband and center */
2498 params[10] = params[11] = params[12] = params[13] = 0;
2500 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2503 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2505 struct hidpp_ff_private_data *data = dev->ff->private;
2508 dbg_hid("Setting gain to %d.\n", gain);
2510 params[0] = gain >> 8;
2511 params[1] = gain & 255;
2512 params[2] = 0; /* no boost */
2515 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2518 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2520 struct hid_device *hid = to_hid_device(dev);
2521 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2522 struct input_dev *idev = hidinput->input;
2523 struct hidpp_ff_private_data *data = idev->ff->private;
2525 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2528 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2530 struct hid_device *hid = to_hid_device(dev);
2531 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2532 struct input_dev *idev = hidinput->input;
2533 struct hidpp_ff_private_data *data = idev->ff->private;
2535 int range = simple_strtoul(buf, NULL, 10);
2537 range = clamp(range, 180, 900);
2539 params[0] = range >> 8;
2540 params[1] = range & 0x00FF;
2542 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2547 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2549 static void hidpp_ff_destroy(struct ff_device *ff)
2551 struct hidpp_ff_private_data *data = ff->private;
2552 struct hid_device *hid = data->hidpp->hid_dev;
2554 hid_info(hid, "Unloading HID++ force feedback.\n");
2556 device_remove_file(&hid->dev, &dev_attr_range);
2557 destroy_workqueue(data->wq);
2558 kfree(data->effect_ids);
2561 static int hidpp_ff_init(struct hidpp_device *hidpp,
2562 struct hidpp_ff_private_data *data)
2564 struct hid_device *hid = hidpp->hid_dev;
2565 struct hid_input *hidinput;
2566 struct input_dev *dev;
2567 struct usb_device_descriptor *udesc;
2569 struct ff_device *ff;
2570 int error, j, num_slots = data->num_effects;
2573 if (!hid_is_usb(hid)) {
2574 hid_err(hid, "device is not USB\n");
2578 if (list_empty(&hid->inputs)) {
2579 hid_err(hid, "no inputs found\n");
2582 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2583 dev = hidinput->input;
2586 hid_err(hid, "Struct input_dev not set!\n");
2590 /* Get firmware release */
2591 udesc = &(hid_to_usb_dev(hid)->descriptor);
2592 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2593 version = bcdDevice & 255;
2595 /* Set supported force feedback capabilities */
2596 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2597 set_bit(hidpp_ff_effects[j], dev->ffbit);
2599 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2600 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2602 error = input_ff_create(dev, num_slots);
2605 hid_err(dev, "Failed to create FF device!\n");
2609 * Create a copy of passed data, so we can transfer memory
2610 * ownership to FF core
2612 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2615 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2616 if (!data->effect_ids) {
2620 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2622 kfree(data->effect_ids);
2627 data->hidpp = hidpp;
2628 data->version = version;
2629 for (j = 0; j < num_slots; j++)
2630 data->effect_ids[j] = -1;
2635 ff->upload = hidpp_ff_upload_effect;
2636 ff->erase = hidpp_ff_erase_effect;
2637 ff->playback = hidpp_ff_playback;
2638 ff->set_gain = hidpp_ff_set_gain;
2639 ff->set_autocenter = hidpp_ff_set_autocenter;
2640 ff->destroy = hidpp_ff_destroy;
2642 /* Create sysfs interface */
2643 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2645 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2647 /* init the hardware command queue */
2648 atomic_set(&data->workqueue_size, 0);
2650 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2656 /* ************************************************************************** */
2658 /* Device Support */
2660 /* ************************************************************************** */
2662 /* -------------------------------------------------------------------------- */
2663 /* Touchpad HID++ devices */
2664 /* -------------------------------------------------------------------------- */
2666 #define WTP_MANUAL_RESOLUTION 39
2671 u8 mt_feature_index;
2672 u8 button_feature_index;
2675 unsigned int resolution;
2678 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2679 struct hid_field *field, struct hid_usage *usage,
2680 unsigned long **bit, int *max)
2685 static void wtp_populate_input(struct hidpp_device *hidpp,
2686 struct input_dev *input_dev)
2688 struct wtp_data *wd = hidpp->private_data;
2690 __set_bit(EV_ABS, input_dev->evbit);
2691 __set_bit(EV_KEY, input_dev->evbit);
2692 __clear_bit(EV_REL, input_dev->evbit);
2693 __clear_bit(EV_LED, input_dev->evbit);
2695 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2696 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2697 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2698 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2700 /* Max pressure is not given by the devices, pick one */
2701 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2703 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2705 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2706 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2708 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2710 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2711 INPUT_MT_DROP_UNUSED);
2714 static void wtp_touch_event(struct hidpp_device *hidpp,
2715 struct hidpp_touchpad_raw_xy_finger *touch_report)
2717 struct wtp_data *wd = hidpp->private_data;
2720 if (!touch_report->finger_id || touch_report->contact_type)
2721 /* no actual data */
2724 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2726 input_mt_slot(hidpp->input, slot);
2727 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2728 touch_report->contact_status);
2729 if (touch_report->contact_status) {
2730 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2732 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2733 wd->flip_y ? wd->y_size - touch_report->y :
2735 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2736 touch_report->area);
2740 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2741 struct hidpp_touchpad_raw_xy *raw)
2745 for (i = 0; i < 2; i++)
2746 wtp_touch_event(hidpp, &(raw->fingers[i]));
2748 if (raw->end_of_frame &&
2749 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2750 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2752 if (raw->end_of_frame || raw->finger_count <= 2) {
2753 input_mt_sync_frame(hidpp->input);
2754 input_sync(hidpp->input);
2758 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2760 struct wtp_data *wd = hidpp->private_data;
2761 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2762 (data[7] >> 4) * (data[7] >> 4)) / 2;
2763 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2764 (data[13] >> 4) * (data[13] >> 4)) / 2;
2765 struct hidpp_touchpad_raw_xy raw = {
2766 .timestamp = data[1],
2770 .contact_status = !!data[7],
2771 .x = get_unaligned_le16(&data[3]),
2772 .y = get_unaligned_le16(&data[5]),
2775 .finger_id = data[2],
2778 .contact_status = !!data[13],
2779 .x = get_unaligned_le16(&data[9]),
2780 .y = get_unaligned_le16(&data[11]),
2783 .finger_id = data[8],
2786 .finger_count = wd->maxcontacts,
2788 .end_of_frame = (data[0] >> 7) == 0,
2789 .button = data[0] & 0x01,
2792 wtp_send_raw_xy_event(hidpp, &raw);
2797 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2799 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2800 struct wtp_data *wd = hidpp->private_data;
2801 struct hidpp_report *report = (struct hidpp_report *)data;
2802 struct hidpp_touchpad_raw_xy raw;
2804 if (!wd || !hidpp->input)
2810 hid_err(hdev, "Received HID report of bad size (%d)",
2814 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2815 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2816 !!(data[1] & 0x01));
2817 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2818 !!(data[1] & 0x02));
2819 input_sync(hidpp->input);
2824 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2826 case REPORT_ID_HIDPP_LONG:
2827 /* size is already checked in hidpp_raw_event. */
2828 if ((report->fap.feature_index != wd->mt_feature_index) ||
2829 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2831 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2833 wtp_send_raw_xy_event(hidpp, &raw);
2840 static int wtp_get_config(struct hidpp_device *hidpp)
2842 struct wtp_data *wd = hidpp->private_data;
2843 struct hidpp_touchpad_raw_info raw_info = {0};
2847 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2848 &wd->mt_feature_index, &feature_type);
2850 /* means that the device is not powered up */
2853 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2858 wd->x_size = raw_info.x_size;
2859 wd->y_size = raw_info.y_size;
2860 wd->maxcontacts = raw_info.maxcontacts;
2861 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2862 wd->resolution = raw_info.res;
2863 if (!wd->resolution)
2864 wd->resolution = WTP_MANUAL_RESOLUTION;
2869 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2871 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2872 struct wtp_data *wd;
2874 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2879 hidpp->private_data = wd;
2884 static int wtp_connect(struct hid_device *hdev, bool connected)
2886 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2887 struct wtp_data *wd = hidpp->private_data;
2891 ret = wtp_get_config(hidpp);
2893 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2898 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2902 /* ------------------------------------------------------------------------- */
2903 /* Logitech M560 devices */
2904 /* ------------------------------------------------------------------------- */
2907 * Logitech M560 protocol overview
2909 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2910 * the sides buttons are pressed, it sends some keyboard keys events
2911 * instead of buttons ones.
2912 * To complicate things further, the middle button keys sequence
2913 * is different from the odd press and the even press.
2915 * forward button -> Super_R
2916 * backward button -> Super_L+'d' (press only)
2917 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2918 * 2nd time: left-click (press only)
2919 * NB: press-only means that when the button is pressed, the
2920 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2921 * together sequentially; instead when the button is released, no event is
2925 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2926 * the mouse reacts differently:
2927 * - it never sends a keyboard key event
2928 * - for the three mouse button it sends:
2929 * middle button press 11<xx>0a 3500af00...
2930 * side 1 button (forward) press 11<xx>0a 3500b000...
2931 * side 2 button (backward) press 11<xx>0a 3500ae00...
2932 * middle/side1/side2 button release 11<xx>0a 35000000...
2935 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2937 /* how buttons are mapped in the report */
2938 #define M560_MOUSE_BTN_LEFT 0x01
2939 #define M560_MOUSE_BTN_RIGHT 0x02
2940 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2941 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2943 #define M560_SUB_ID 0x0a
2944 #define M560_BUTTON_MODE_REGISTER 0x35
2946 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2948 struct hidpp_report response;
2949 struct hidpp_device *hidpp_dev;
2951 hidpp_dev = hid_get_drvdata(hdev);
2953 return hidpp_send_rap_command_sync(
2955 REPORT_ID_HIDPP_SHORT,
2957 M560_BUTTON_MODE_REGISTER,
2958 (u8 *)m560_config_parameter,
2959 sizeof(m560_config_parameter),
2964 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2966 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2969 if (!hidpp->input) {
2970 hid_err(hdev, "error in parameter\n");
2975 hid_err(hdev, "error in report\n");
2979 if (data[0] == REPORT_ID_HIDPP_LONG &&
2980 data[2] == M560_SUB_ID && data[6] == 0x00) {
2982 * m560 mouse report for middle, forward and backward button
2985 * data[1] = device-id
2987 * data[5] = 0xaf -> middle
2990 * 0x00 -> release all
2996 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2999 input_report_key(hidpp->input, BTN_FORWARD, 1);
3002 input_report_key(hidpp->input, BTN_BACK, 1);
3005 input_report_key(hidpp->input, BTN_BACK, 0);
3006 input_report_key(hidpp->input, BTN_FORWARD, 0);
3007 input_report_key(hidpp->input, BTN_MIDDLE, 0);
3010 hid_err(hdev, "error in report\n");
3013 input_sync(hidpp->input);
3015 } else if (data[0] == 0x02) {
3017 * Logitech M560 mouse report
3019 * data[0] = type (0x02)
3020 * data[1..2] = buttons
3027 input_report_key(hidpp->input, BTN_LEFT,
3028 !!(data[1] & M560_MOUSE_BTN_LEFT));
3029 input_report_key(hidpp->input, BTN_RIGHT,
3030 !!(data[1] & M560_MOUSE_BTN_RIGHT));
3032 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3033 input_report_rel(hidpp->input, REL_HWHEEL, -1);
3034 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3036 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3037 input_report_rel(hidpp->input, REL_HWHEEL, 1);
3038 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3042 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
3043 input_report_rel(hidpp->input, REL_X, v);
3045 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
3046 input_report_rel(hidpp->input, REL_Y, v);
3048 v = hid_snto32(data[6], 8);
3050 hidpp_scroll_counter_handle_scroll(hidpp->input,
3051 &hidpp->vertical_wheel_counter, v);
3053 input_sync(hidpp->input);
3059 static void m560_populate_input(struct hidpp_device *hidpp,
3060 struct input_dev *input_dev)
3062 __set_bit(EV_KEY, input_dev->evbit);
3063 __set_bit(BTN_MIDDLE, input_dev->keybit);
3064 __set_bit(BTN_RIGHT, input_dev->keybit);
3065 __set_bit(BTN_LEFT, input_dev->keybit);
3066 __set_bit(BTN_BACK, input_dev->keybit);
3067 __set_bit(BTN_FORWARD, input_dev->keybit);
3069 __set_bit(EV_REL, input_dev->evbit);
3070 __set_bit(REL_X, input_dev->relbit);
3071 __set_bit(REL_Y, input_dev->relbit);
3072 __set_bit(REL_WHEEL, input_dev->relbit);
3073 __set_bit(REL_HWHEEL, input_dev->relbit);
3074 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3075 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3078 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3079 struct hid_field *field, struct hid_usage *usage,
3080 unsigned long **bit, int *max)
3085 /* ------------------------------------------------------------------------- */
3086 /* Logitech K400 devices */
3087 /* ------------------------------------------------------------------------- */
3090 * The Logitech K400 keyboard has an embedded touchpad which is seen
3091 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3092 * tap-to-click but the setting is not remembered accross reset, annoying some
3095 * We can toggle this feature from the host by using the feature 0x6010:
3099 struct k400_private_data {
3103 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3105 struct k400_private_data *k400 = hidpp->private_data;
3106 struct hidpp_touchpad_fw_items items = {};
3110 if (!k400->feature_index) {
3111 ret = hidpp_root_get_feature(hidpp,
3112 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3113 &k400->feature_index, &feature_type);
3115 /* means that the device is not powered up */
3119 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3126 static int k400_allocate(struct hid_device *hdev)
3128 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3129 struct k400_private_data *k400;
3131 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3136 hidpp->private_data = k400;
3141 static int k400_connect(struct hid_device *hdev, bool connected)
3143 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3145 if (!disable_tap_to_click)
3148 return k400_disable_tap_to_click(hidpp);
3151 /* ------------------------------------------------------------------------- */
3152 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
3153 /* ------------------------------------------------------------------------- */
3155 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3157 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3158 struct hidpp_ff_private_data *data)
3160 struct hidpp_report response;
3161 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3162 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3166 /* initialize with zero autocenter to get wheel in usable state */
3168 dbg_hid("Setting autocenter to 0.\n");
3169 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3170 HIDPP_FF_DOWNLOAD_EFFECT,
3171 params, ARRAY_SIZE(params),
3174 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3176 data->slot_autocenter = response.fap.params[0];
3181 static int g920_get_config(struct hidpp_device *hidpp,
3182 struct hidpp_ff_private_data *data)
3184 struct hidpp_report response;
3188 memset(data, 0, sizeof(*data));
3190 /* Find feature and store for later use */
3191 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3192 &data->feature_index, &feature_type);
3196 /* Read number of slots available in device */
3197 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3204 hid_err(hidpp->hid_dev,
3205 "%s: received protocol error 0x%02x\n", __func__, ret);
3209 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3211 /* reset all forces */
3212 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3217 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3219 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3220 HIDPP_FF_GET_APERTURE,
3224 hid_warn(hidpp->hid_dev,
3225 "Failed to read range from device!\n");
3228 900 : get_unaligned_be16(&response.fap.params[0]);
3230 /* Read the current gain values */
3231 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3232 HIDPP_FF_GET_GLOBAL_GAINS,
3236 hid_warn(hidpp->hid_dev,
3237 "Failed to read gain values from device!\n");
3239 0xffff : get_unaligned_be16(&response.fap.params[0]);
3241 /* ignore boost value at response.fap.params[2] */
3243 return g920_ff_set_autocenter(hidpp, data);
3246 /* -------------------------------------------------------------------------- */
3247 /* Logitech Dinovo Mini keyboard with builtin touchpad */
3248 /* -------------------------------------------------------------------------- */
3249 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3251 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3252 struct hid_field *field, struct hid_usage *usage,
3253 unsigned long **bit, int *max)
3255 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3258 switch (usage->hid & HID_USAGE) {
3259 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3266 /* -------------------------------------------------------------------------- */
3267 /* HID++1.0 devices which use HID++ reports for their wheels */
3268 /* -------------------------------------------------------------------------- */
3269 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3271 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3272 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3273 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3276 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3287 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3293 input_report_rel(hidpp->input, REL_WHEEL, value);
3294 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3295 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3296 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3297 input_sync(hidpp->input);
3302 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3303 struct input_dev *input_dev)
3305 __set_bit(EV_REL, input_dev->evbit);
3306 __set_bit(REL_WHEEL, input_dev->relbit);
3307 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3308 __set_bit(REL_HWHEEL, input_dev->relbit);
3309 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3312 /* -------------------------------------------------------------------------- */
3313 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3314 /* -------------------------------------------------------------------------- */
3315 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3317 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3318 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3319 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3322 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3333 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3334 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3338 * Buttons are either delivered through the regular mouse report *or*
3339 * through the extra buttons report. At least for button 6 how it is
3340 * delivered differs per receiver firmware version. Even receivers with
3341 * the same usb-id show different behavior, so we handle both cases.
3343 for (i = 0; i < 8; i++)
3344 input_report_key(hidpp->input, BTN_MOUSE + i,
3345 (data[3] & (1 << i)));
3347 /* Some mice report events on button 9+, use BTN_MISC */
3348 for (i = 0; i < 8; i++)
3349 input_report_key(hidpp->input, BTN_MISC + i,
3350 (data[4] & (1 << i)));
3352 input_sync(hidpp->input);
3356 static void hidpp10_extra_mouse_buttons_populate_input(
3357 struct hidpp_device *hidpp, struct input_dev *input_dev)
3359 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3360 __set_bit(BTN_0, input_dev->keybit);
3361 __set_bit(BTN_1, input_dev->keybit);
3362 __set_bit(BTN_2, input_dev->keybit);
3363 __set_bit(BTN_3, input_dev->keybit);
3364 __set_bit(BTN_4, input_dev->keybit);
3365 __set_bit(BTN_5, input_dev->keybit);
3366 __set_bit(BTN_6, input_dev->keybit);
3367 __set_bit(BTN_7, input_dev->keybit);
3370 /* -------------------------------------------------------------------------- */
3371 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3372 /* -------------------------------------------------------------------------- */
3374 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3375 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3376 u8 *_rdesc, unsigned int *rsize)
3378 /* Note 0 terminated so we can use strnstr to search for this. */
3379 static const char consumer_rdesc_start[] = {
3380 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3381 0x09, 0x01, /* USAGE (Consumer Control) */
3382 0xA1, 0x01, /* COLLECTION (Application) */
3383 0x85, 0x03, /* REPORT_ID = 3 */
3384 0x75, 0x10, /* REPORT_SIZE (16) */
3385 0x95, 0x02, /* REPORT_COUNT (2) */
3386 0x15, 0x01, /* LOGICAL_MIN (1) */
3387 0x26, 0x00 /* LOGICAL_MAX (... */
3389 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3392 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3393 size = *rsize - (consumer_rdesc - rdesc);
3394 if (consumer_rdesc && size >= 25) {
3395 consumer_rdesc[15] = 0x7f;
3396 consumer_rdesc[16] = 0x10;
3397 consumer_rdesc[20] = 0x7f;
3398 consumer_rdesc[21] = 0x10;
3403 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3405 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3406 HIDPP_ENABLE_CONSUMER_REPORT,
3407 HIDPP_ENABLE_CONSUMER_REPORT);
3410 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3413 u8 consumer_report[5];
3418 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3419 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3423 * Build a normal consumer report (3) out of the data, this detour
3424 * is necessary to get some keyboards to report their 0x10xx usages.
3426 consumer_report[0] = 0x03;
3427 memcpy(&consumer_report[1], &data[3], 4);
3428 /* We are called from atomic context */
3429 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3430 consumer_report, 5, 1);
3435 /* -------------------------------------------------------------------------- */
3436 /* High-resolution scroll wheels */
3437 /* -------------------------------------------------------------------------- */
3439 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3444 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
3445 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3447 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3448 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
3449 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3451 } else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
3452 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3456 hid_dbg(hidpp->hid_dev,
3457 "Could not enable hi-res scrolling: %d\n", ret);
3461 if (multiplier == 0) {
3462 hid_dbg(hidpp->hid_dev,
3463 "Invalid multiplier 0 from device, setting it to 1\n");
3467 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3468 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3472 static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
3475 unsigned long capabilities;
3477 capabilities = hidpp->capabilities;
3479 if (hidpp->protocol_major >= 2) {
3483 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
3484 &feature_index, &feature_type);
3486 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
3487 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
3490 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
3491 &feature_index, &feature_type);
3493 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
3494 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
3497 /* We cannot detect fast scrolling support on HID++ 1.0 devices */
3498 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) {
3499 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
3500 hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
3504 if (hidpp->capabilities == capabilities)
3505 hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
3509 /* -------------------------------------------------------------------------- */
3510 /* Generic HID++ devices */
3511 /* -------------------------------------------------------------------------- */
3513 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3514 unsigned int *rsize)
3516 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3521 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3522 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3523 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3524 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3529 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3530 struct hid_field *field, struct hid_usage *usage,
3531 unsigned long **bit, int *max)
3533 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3538 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3539 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3540 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3541 field->application != HID_GD_MOUSE)
3542 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3544 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3545 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3550 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3551 struct hid_field *field, struct hid_usage *usage,
3552 unsigned long **bit, int *max)
3554 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3559 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3560 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3561 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3562 usage->code == ABS_Y || usage->code == ABS_Z ||
3563 usage->code == ABS_RZ)) {
3564 field->application = HID_GD_MULTIAXIS;
3572 static void hidpp_populate_input(struct hidpp_device *hidpp,
3573 struct input_dev *input)
3575 hidpp->input = input;
3577 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3578 wtp_populate_input(hidpp, input);
3579 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3580 m560_populate_input(hidpp, input);
3582 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3583 hidpp10_wheel_populate_input(hidpp, input);
3585 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3586 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3589 static int hidpp_input_configured(struct hid_device *hdev,
3590 struct hid_input *hidinput)
3592 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3593 struct input_dev *input = hidinput->input;
3598 hidpp_populate_input(hidpp, input);
3603 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3606 struct hidpp_report *question = hidpp->send_receive_buf;
3607 struct hidpp_report *answer = hidpp->send_receive_buf;
3608 struct hidpp_report *report = (struct hidpp_report *)data;
3612 * If the mutex is locked then we have a pending answer from a
3613 * previously sent command.
3615 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3617 * Check for a correct hidpp20 answer or the corresponding
3620 if (hidpp_match_answer(question, report) ||
3621 hidpp_match_error(question, report)) {
3623 hidpp->answer_available = true;
3624 wake_up(&hidpp->wait);
3626 * This was an answer to a command that this driver sent
3627 * We return 1 to hid-core to avoid forwarding the
3628 * command upstream as it has been treated by the driver
3635 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3636 atomic_set(&hidpp->connected,
3637 !(report->rap.params[0] & (1 << 6)));
3638 if (schedule_work(&hidpp->work) == 0)
3639 dbg_hid("%s: connect event already queued\n", __func__);
3643 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3644 data[0] == REPORT_ID_HIDPP_SHORT &&
3645 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3646 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3647 dev_err_ratelimited(&hidpp->hid_dev->dev,
3648 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3649 dev_err_ratelimited(&hidpp->hid_dev->dev,
3650 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3653 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3654 ret = hidpp20_battery_event_1000(hidpp, data, size);
3657 ret = hidpp20_battery_event_1004(hidpp, data, size);
3660 ret = hidpp_solar_battery_event(hidpp, data, size);
3663 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3668 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3669 ret = hidpp10_battery_event(hidpp, data, size);
3674 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3675 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3680 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3681 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3686 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3687 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3695 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3698 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3704 /* Generic HID++ processing. */
3706 case REPORT_ID_HIDPP_VERY_LONG:
3707 if (size != hidpp->very_long_report_length) {
3708 hid_err(hdev, "received hid++ report of bad size (%d)",
3712 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3714 case REPORT_ID_HIDPP_LONG:
3715 if (size != HIDPP_REPORT_LONG_LENGTH) {
3716 hid_err(hdev, "received hid++ report of bad size (%d)",
3720 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3722 case REPORT_ID_HIDPP_SHORT:
3723 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3724 hid_err(hdev, "received hid++ report of bad size (%d)",
3728 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3732 /* If no report is available for further processing, skip calling
3733 * raw_event of subclasses. */
3737 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3738 return wtp_raw_event(hdev, data, size);
3739 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3740 return m560_raw_event(hdev, data, size);
3745 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3746 struct hid_usage *usage, __s32 value)
3748 /* This function will only be called for scroll events, due to the
3749 * restriction imposed in hidpp_usages.
3751 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3752 struct hidpp_scroll_counter *counter;
3757 counter = &hidpp->vertical_wheel_counter;
3758 /* A scroll event may occur before the multiplier has been retrieved or
3759 * the input device set, or high-res scroll enabling may fail. In such
3760 * cases we must return early (falling back to default behaviour) to
3761 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3763 if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
3764 || value == 0 || hidpp->input == NULL
3765 || counter->wheel_multiplier == 0)
3768 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3772 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3774 static atomic_t battery_no = ATOMIC_INIT(0);
3775 struct power_supply_config cfg = { .drv_data = hidpp };
3776 struct power_supply_desc *desc = &hidpp->battery.desc;
3777 enum power_supply_property *battery_props;
3778 struct hidpp_battery *battery;
3779 unsigned int num_battery_props;
3783 if (hidpp->battery.ps)
3786 hidpp->battery.feature_index = 0xff;
3787 hidpp->battery.solar_feature_index = 0xff;
3788 hidpp->battery.voltage_feature_index = 0xff;
3790 if (hidpp->protocol_major >= 2) {
3791 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3792 ret = hidpp_solar_request_battery_event(hidpp);
3794 /* we only support one battery feature right now, so let's
3795 first check the ones that support battery level first
3796 and leave voltage for last */
3797 ret = hidpp20_query_battery_info_1000(hidpp);
3799 ret = hidpp20_query_battery_info_1004(hidpp);
3801 ret = hidpp20_query_battery_voltage_info(hidpp);
3806 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3808 ret = hidpp10_query_battery_status(hidpp);
3810 ret = hidpp10_query_battery_mileage(hidpp);
3813 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3815 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3817 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3820 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3821 hidpp_battery_props,
3822 sizeof(hidpp_battery_props),
3827 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3829 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
3830 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
3831 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3832 battery_props[num_battery_props++] =
3833 POWER_SUPPLY_PROP_CAPACITY;
3835 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3836 battery_props[num_battery_props++] =
3837 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3839 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3840 battery_props[num_battery_props++] =
3841 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3843 battery = &hidpp->battery;
3845 n = atomic_inc_return(&battery_no) - 1;
3846 desc->properties = battery_props;
3847 desc->num_properties = num_battery_props;
3848 desc->get_property = hidpp_battery_get_property;
3849 sprintf(battery->name, "hidpp_battery_%ld", n);
3850 desc->name = battery->name;
3851 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3852 desc->use_for_apm = 0;
3854 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3857 if (IS_ERR(battery->ps))
3858 return PTR_ERR(battery->ps);
3860 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3865 static void hidpp_overwrite_name(struct hid_device *hdev)
3867 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3870 if (hidpp->protocol_major < 2)
3873 name = hidpp_get_device_name(hidpp);
3876 hid_err(hdev, "unable to retrieve the name of the device");
3878 dbg_hid("HID++: Got name: %s\n", name);
3879 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3885 static int hidpp_input_open(struct input_dev *dev)
3887 struct hid_device *hid = input_get_drvdata(dev);
3889 return hid_hw_open(hid);
3892 static void hidpp_input_close(struct input_dev *dev)
3894 struct hid_device *hid = input_get_drvdata(dev);
3899 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3901 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3902 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3907 input_set_drvdata(input_dev, hdev);
3908 input_dev->open = hidpp_input_open;
3909 input_dev->close = hidpp_input_close;
3911 input_dev->name = hidpp->name;
3912 input_dev->phys = hdev->phys;
3913 input_dev->uniq = hdev->uniq;
3914 input_dev->id.bustype = hdev->bus;
3915 input_dev->id.vendor = hdev->vendor;
3916 input_dev->id.product = hdev->product;
3917 input_dev->id.version = hdev->version;
3918 input_dev->dev.parent = &hdev->dev;
3923 static void hidpp_connect_event(struct hidpp_device *hidpp)
3925 struct hid_device *hdev = hidpp->hid_dev;
3927 bool connected = atomic_read(&hidpp->connected);
3928 struct input_dev *input;
3929 char *name, *devm_name;
3932 if (hidpp->battery.ps) {
3933 hidpp->battery.online = false;
3934 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3935 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3936 power_supply_changed(hidpp->battery.ps);
3941 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3942 ret = wtp_connect(hdev, connected);
3945 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3946 ret = m560_send_config_command(hdev, connected);
3949 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3950 ret = k400_connect(hdev, connected);
3955 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3956 ret = hidpp10_wheel_connect(hidpp);
3961 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3962 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3967 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3968 ret = hidpp10_consumer_keys_connect(hidpp);
3973 /* the device is already connected, we can ask for its name and
3975 if (!hidpp->protocol_major) {
3976 ret = hidpp_root_get_protocol_version(hidpp);
3978 hid_err(hdev, "Can not get the protocol version.\n");
3983 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3984 name = hidpp_get_device_name(hidpp);
3986 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3992 hidpp->name = devm_name;
3996 hidpp_initialize_battery(hidpp);
3997 if (!hid_is_usb(hidpp->hid_dev))
3998 hidpp_initialize_hires_scroll(hidpp);
4000 /* forward current battery state */
4001 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
4002 hidpp10_enable_battery_reporting(hidpp);
4003 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
4004 hidpp10_query_battery_mileage(hidpp);
4006 hidpp10_query_battery_status(hidpp);
4007 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
4008 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
4009 hidpp20_query_battery_voltage_info(hidpp);
4010 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
4011 hidpp20_query_battery_info_1004(hidpp);
4013 hidpp20_query_battery_info_1000(hidpp);
4015 if (hidpp->battery.ps)
4016 power_supply_changed(hidpp->battery.ps);
4018 if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4019 hi_res_scroll_enable(hidpp);
4021 if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
4022 /* if the input nodes are already created, we can stop now */
4025 input = hidpp_allocate_input(hdev);
4027 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
4031 hidpp_populate_input(hidpp, input);
4033 ret = input_register_device(input);
4035 input_free_device(input);
4039 hidpp->delayed_input = input;
4042 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
4044 static struct attribute *sysfs_attrs[] = {
4045 &dev_attr_builtin_power_supply.attr,
4049 static const struct attribute_group ps_attribute_group = {
4050 .attrs = sysfs_attrs
4053 static int hidpp_get_report_length(struct hid_device *hdev, int id)
4055 struct hid_report_enum *re;
4056 struct hid_report *report;
4058 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
4059 report = re->report_id_hash[id];
4063 return report->field[0]->report_count + 1;
4066 static u8 hidpp_validate_device(struct hid_device *hdev)
4068 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4069 int id, report_length;
4070 u8 supported_reports = 0;
4072 id = REPORT_ID_HIDPP_SHORT;
4073 report_length = hidpp_get_report_length(hdev, id);
4074 if (report_length) {
4075 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4078 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4081 id = REPORT_ID_HIDPP_LONG;
4082 report_length = hidpp_get_report_length(hdev, id);
4083 if (report_length) {
4084 if (report_length < HIDPP_REPORT_LONG_LENGTH)
4087 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4090 id = REPORT_ID_HIDPP_VERY_LONG;
4091 report_length = hidpp_get_report_length(hdev, id);
4092 if (report_length) {
4093 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4094 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4097 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4098 hidpp->very_long_report_length = report_length;
4101 return supported_reports;
4104 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4108 static bool hidpp_application_equals(struct hid_device *hdev,
4109 unsigned int application)
4111 struct list_head *report_list;
4112 struct hid_report *report;
4114 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4115 report = list_first_entry_or_null(report_list, struct hid_report, list);
4116 return report && report->application == application;
4119 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4121 struct hidpp_device *hidpp;
4124 unsigned int connect_mask = HID_CONNECT_DEFAULT;
4125 struct hidpp_ff_private_data data;
4126 bool will_restart = false;
4128 /* report_fixup needs drvdata to be set before we call hid_parse */
4129 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4133 hidpp->hid_dev = hdev;
4134 hidpp->name = hdev->name;
4135 hidpp->quirks = id->driver_data;
4136 hid_set_drvdata(hdev, hidpp);
4138 ret = hid_parse(hdev);
4140 hid_err(hdev, "%s:parse failed\n", __func__);
4145 * Make sure the device is HID++ capable, otherwise treat as generic HID
4147 hidpp->supported_reports = hidpp_validate_device(hdev);
4149 if (!hidpp->supported_reports) {
4150 hid_set_drvdata(hdev, NULL);
4151 devm_kfree(&hdev->dev, hidpp);
4152 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4155 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4156 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
4158 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4159 hidpp_application_equals(hdev, HID_GD_MOUSE))
4160 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4161 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4163 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4164 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4165 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4167 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4168 ret = wtp_allocate(hdev, id);
4171 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4172 ret = k400_allocate(hdev);
4177 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT ||
4178 hidpp->quirks & HIDPP_QUIRK_UNIFYING)
4179 will_restart = true;
4181 INIT_WORK(&hidpp->work, delayed_work_cb);
4182 mutex_init(&hidpp->send_mutex);
4183 init_waitqueue_head(&hidpp->wait);
4185 /* indicates we are handling the battery properties in the kernel */
4186 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4188 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4192 * Plain USB connections need to actually call start and open
4193 * on the transport driver to allow incoming data.
4195 ret = hid_hw_start(hdev, will_restart ? 0 : connect_mask);
4197 hid_err(hdev, "hw start failed\n");
4198 goto hid_hw_start_fail;
4201 ret = hid_hw_open(hdev);
4203 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4205 goto hid_hw_open_fail;
4208 /* Allow incoming packets */
4209 hid_device_io_start(hdev);
4211 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
4212 hidpp_unifying_init(hidpp);
4214 connected = hidpp_root_get_protocol_version(hidpp) == 0;
4215 atomic_set(&hidpp->connected, connected);
4216 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
4219 hid_err(hdev, "Device not connected");
4220 goto hid_hw_init_fail;
4223 hidpp_overwrite_name(hdev);
4226 if (connected && hidpp->protocol_major >= 2) {
4227 ret = hidpp_set_wireless_feature_index(hidpp);
4229 hidpp->wireless_feature_index = 0;
4231 goto hid_hw_init_fail;
4235 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
4236 ret = wtp_get_config(hidpp);
4238 goto hid_hw_init_fail;
4239 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
4240 ret = g920_get_config(hidpp, &data);
4242 goto hid_hw_init_fail;
4245 hidpp_connect_event(hidpp);
4248 /* Reset the HID node state */
4249 hid_device_io_stop(hdev);
4253 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
4254 connect_mask &= ~HID_CONNECT_HIDINPUT;
4256 /* Now export the actual inputs and hidraw nodes to the world */
4257 ret = hid_hw_start(hdev, connect_mask);
4259 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
4260 goto hid_hw_start_fail;
4264 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4265 ret = hidpp_ff_init(hidpp, &data);
4267 hid_warn(hidpp->hid_dev,
4268 "Unable to initialize force feedback support, errno %d\n",
4279 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4280 cancel_work_sync(&hidpp->work);
4281 mutex_destroy(&hidpp->send_mutex);
4285 static void hidpp_remove(struct hid_device *hdev)
4287 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4290 return hid_hw_stop(hdev);
4292 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4295 cancel_work_sync(&hidpp->work);
4296 mutex_destroy(&hidpp->send_mutex);
4299 #define LDJ_DEVICE(product) \
4300 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4301 USB_VENDOR_ID_LOGITECH, (product))
4303 #define L27MHZ_DEVICE(product) \
4304 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4305 USB_VENDOR_ID_LOGITECH, (product))
4307 static const struct hid_device_id hidpp_devices[] = {
4308 { /* wireless touchpad */
4310 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4311 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4312 { /* wireless touchpad T650 */
4314 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4315 { /* wireless touchpad T651 */
4316 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4317 USB_DEVICE_ID_LOGITECH_T651),
4318 .driver_data = HIDPP_QUIRK_CLASS_WTP },
4319 { /* Mouse Logitech Anywhere MX */
4320 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4321 { /* Mouse logitech M560 */
4323 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
4324 { /* Mouse Logitech M705 (firmware RQM17) */
4325 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4326 { /* Mouse Logitech Performance MX */
4327 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4328 { /* Keyboard logitech K400 */
4330 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4331 { /* Solar Keyboard Logitech K750 */
4333 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4334 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4336 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4337 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4339 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4340 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4342 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4344 { LDJ_DEVICE(HID_ANY_ID) },
4346 { /* Keyboard LX501 (Y-RR53) */
4347 L27MHZ_DEVICE(0x0049),
4348 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4349 { /* Keyboard MX3000 (Y-RAM74) */
4350 L27MHZ_DEVICE(0x0057),
4351 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4352 { /* Keyboard MX3200 (Y-RAV80) */
4353 L27MHZ_DEVICE(0x005c),
4354 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4355 { /* S510 Media Remote */
4356 L27MHZ_DEVICE(0x00fe),
4357 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4359 { L27MHZ_DEVICE(HID_ANY_ID) },
4361 { /* Logitech G403 Wireless Gaming Mouse over USB */
4362 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4363 { /* Logitech G703 Gaming Mouse over USB */
4364 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4365 { /* Logitech G703 Hero Gaming Mouse over USB */
4366 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4367 { /* Logitech G900 Gaming Mouse over USB */
4368 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4369 { /* Logitech G903 Gaming Mouse over USB */
4370 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4371 { /* Logitech G903 Hero Gaming Mouse over USB */
4372 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4373 { /* Logitech G920 Wheel over USB */
4374 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4375 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4376 { /* Logitech G923 Wheel (Xbox version) over USB */
4377 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G923_XBOX_WHEEL),
4378 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS },
4379 { /* Logitech G Pro Gaming Mouse over USB */
4380 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4382 { /* MX5000 keyboard over Bluetooth */
4383 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4384 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4385 { /* Dinovo Edge keyboard over Bluetooth */
4386 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4387 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4388 { /* MX5500 keyboard over Bluetooth */
4389 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4390 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4391 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4392 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4393 { /* MX Master mouse over Bluetooth */
4394 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
4395 { /* MX Ergo trackball over Bluetooth */
4396 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4397 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
4398 { /* Signature M650 over Bluetooth */
4399 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb02a) },
4400 { /* MX Master 3 mouse over Bluetooth */
4401 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
4402 { /* MX Master 3S mouse over Bluetooth */
4403 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) },
4407 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4409 static const struct hid_usage_id hidpp_usages[] = {
4410 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4411 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4414 static struct hid_driver hidpp_driver = {
4415 .name = "logitech-hidpp-device",
4416 .id_table = hidpp_devices,
4417 .report_fixup = hidpp_report_fixup,
4418 .probe = hidpp_probe,
4419 .remove = hidpp_remove,
4420 .raw_event = hidpp_raw_event,
4421 .usage_table = hidpp_usages,
4422 .event = hidpp_event,
4423 .input_configured = hidpp_input_configured,
4424 .input_mapping = hidpp_input_mapping,
4425 .input_mapped = hidpp_input_mapped,
4428 module_hid_driver(hidpp_driver);