1 // SPDX-License-Identifier: GPL-2.0-only
3 * HIDPP protocol for Logitech receivers
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/device.h>
14 #include <linux/input.h>
15 #include <linux/usb.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/sched/clock.h>
21 #include <linux/kfifo.h>
22 #include <linux/input/mt.h>
23 #include <linux/workqueue.h>
24 #include <linux/atomic.h>
25 #include <linux/fixp-arith.h>
26 #include <asm/unaligned.h>
27 #include "usbhid/usbhid.h"
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
34 static bool disable_raw_mode
;
35 module_param(disable_raw_mode
, bool, 0644);
36 MODULE_PARM_DESC(disable_raw_mode
,
37 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
39 static bool disable_tap_to_click
;
40 module_param(disable_tap_to_click
, bool, 0644);
41 MODULE_PARM_DESC(disable_tap_to_click
,
42 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
44 #define REPORT_ID_HIDPP_SHORT 0x10
45 #define REPORT_ID_HIDPP_LONG 0x11
46 #define REPORT_ID_HIDPP_VERY_LONG 0x12
48 #define HIDPP_REPORT_SHORT_LENGTH 7
49 #define HIDPP_REPORT_LONG_LENGTH 20
50 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
52 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
53 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
54 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
56 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
57 #define HIDPP_SUB_ID_ROLLER 0x05
58 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
59 #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
60 #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
62 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
63 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
64 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
65 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
66 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
68 /* bits 2..20 are reserved for classes */
69 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
70 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
71 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
72 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
73 #define HIDPP_QUIRK_UNIFYING BIT(25)
74 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
75 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
76 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
77 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
78 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
79 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
81 /* These are just aliases for now */
82 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
83 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
85 /* Convenience constant to check for any high-res support. */
86 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
87 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
88 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
90 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
92 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
93 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
94 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
95 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
96 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
97 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
98 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
100 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
103 * There are two hidpp protocols in use, the first version hidpp10 is known
104 * as register access protocol or RAP, the second version hidpp20 is known as
105 * feature access protocol or FAP
107 * Most older devices (including the Unifying usb receiver) use the RAP protocol
108 * where as most newer devices use the FAP protocol. Both protocols are
109 * compatible with the underlying transport, which could be usb, Unifiying, or
110 * bluetooth. The message lengths are defined by the hid vendor specific report
111 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
112 * the HIDPP_LONG report type (total message length 20 bytes)
114 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
115 * messages. The Unifying receiver itself responds to RAP messages (device index
116 * is 0xFF for the receiver), and all messages (short or long) with a device
117 * index between 1 and 6 are passed untouched to the corresponding paired
120 * The paired device can be RAP or FAP, it will receive the message untouched
121 * from the Unifiying receiver.
126 u8 funcindex_clientid
;
127 u8 params
[HIDPP_REPORT_VERY_LONG_MAX_LENGTH
- 4U];
133 u8 params
[HIDPP_REPORT_VERY_LONG_MAX_LENGTH
- 4U];
136 struct hidpp_report
{
142 u8 rawbytes
[sizeof(struct fap
)];
146 struct hidpp_battery
{
148 u8 solar_feature_index
;
149 u8 voltage_feature_index
;
150 struct power_supply_desc desc
;
151 struct power_supply
*ps
;
159 u8 supported_levels_1004
;
163 * struct hidpp_scroll_counter - Utility class for processing high-resolution
165 * @dev: the input device for which events should be reported.
166 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
167 * @remainder: counts the number of high-resolution units moved since the last
168 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
169 * only be used by class methods.
170 * @direction: direction of last movement (1 or -1)
171 * @last_time: last event time, used to reset remainder after inactivity
173 struct hidpp_scroll_counter
{
174 int wheel_multiplier
;
177 unsigned long long last_time
;
180 struct hidpp_device
{
181 struct hid_device
*hid_dev
;
182 struct input_dev
*input
;
183 struct mutex send_mutex
;
184 void *send_receive_buf
;
185 char *name
; /* will never be NULL and should not be freed */
186 wait_queue_head_t wait
;
187 int very_long_report_length
;
188 bool answer_available
;
194 struct work_struct work
;
195 struct kfifo delayed_work_fifo
;
197 struct input_dev
*delayed_input
;
199 unsigned long quirks
;
200 unsigned long capabilities
;
201 u8 supported_reports
;
203 struct hidpp_battery battery
;
204 struct hidpp_scroll_counter vertical_wheel_counter
;
206 u8 wireless_feature_index
;
209 /* HID++ 1.0 error codes */
210 #define HIDPP_ERROR 0x8f
211 #define HIDPP_ERROR_SUCCESS 0x00
212 #define HIDPP_ERROR_INVALID_SUBID 0x01
213 #define HIDPP_ERROR_INVALID_ADRESS 0x02
214 #define HIDPP_ERROR_INVALID_VALUE 0x03
215 #define HIDPP_ERROR_CONNECT_FAIL 0x04
216 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
217 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
218 #define HIDPP_ERROR_BUSY 0x07
219 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
220 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
221 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
222 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
223 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
224 /* HID++ 2.0 error codes */
225 #define HIDPP20_ERROR 0xff
227 static void hidpp_connect_event(struct hidpp_device
*hidpp_dev
);
229 static int __hidpp_send_report(struct hid_device
*hdev
,
230 struct hidpp_report
*hidpp_report
)
232 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
233 int fields_count
, ret
;
235 switch (hidpp_report
->report_id
) {
236 case REPORT_ID_HIDPP_SHORT
:
237 fields_count
= HIDPP_REPORT_SHORT_LENGTH
;
239 case REPORT_ID_HIDPP_LONG
:
240 fields_count
= HIDPP_REPORT_LONG_LENGTH
;
242 case REPORT_ID_HIDPP_VERY_LONG
:
243 fields_count
= hidpp
->very_long_report_length
;
250 * set the device_index as the receiver, it will be overwritten by
251 * hid_hw_request if needed
253 hidpp_report
->device_index
= 0xff;
255 if (hidpp
->quirks
& HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
) {
256 ret
= hid_hw_output_report(hdev
, (u8
*)hidpp_report
, fields_count
);
258 ret
= hid_hw_raw_request(hdev
, hidpp_report
->report_id
,
259 (u8
*)hidpp_report
, fields_count
, HID_OUTPUT_REPORT
,
263 return ret
== fields_count
? 0 : -1;
267 * hidpp_send_message_sync() returns 0 in case of success, and something else
268 * in case of a failure.
269 * - If ' something else' is positive, that means that an error has been raised
270 * by the protocol itself.
271 * - If ' something else' is negative, that means that we had a classic error
272 * (-ENOMEM, -EPIPE, etc...)
274 static int hidpp_send_message_sync(struct hidpp_device
*hidpp
,
275 struct hidpp_report
*message
,
276 struct hidpp_report
*response
)
280 mutex_lock(&hidpp
->send_mutex
);
282 hidpp
->send_receive_buf
= response
;
283 hidpp
->answer_available
= false;
286 * So that we can later validate the answer when it arrives
289 *response
= *message
;
291 ret
= __hidpp_send_report(hidpp
->hid_dev
, message
);
294 dbg_hid("__hidpp_send_report returned err: %d\n", ret
);
295 memset(response
, 0, sizeof(struct hidpp_report
));
299 if (!wait_event_timeout(hidpp
->wait
, hidpp
->answer_available
,
301 dbg_hid("%s:timeout waiting for response\n", __func__
);
302 memset(response
, 0, sizeof(struct hidpp_report
));
306 if (response
->report_id
== REPORT_ID_HIDPP_SHORT
&&
307 response
->rap
.sub_id
== HIDPP_ERROR
) {
308 ret
= response
->rap
.params
[1];
309 dbg_hid("%s:got hidpp error %02X\n", __func__
, ret
);
313 if ((response
->report_id
== REPORT_ID_HIDPP_LONG
||
314 response
->report_id
== REPORT_ID_HIDPP_VERY_LONG
) &&
315 response
->fap
.feature_index
== HIDPP20_ERROR
) {
316 ret
= response
->fap
.params
[1];
317 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__
, ret
);
322 mutex_unlock(&hidpp
->send_mutex
);
327 static int hidpp_send_fap_command_sync(struct hidpp_device
*hidpp
,
328 u8 feat_index
, u8 funcindex_clientid
, u8
*params
, int param_count
,
329 struct hidpp_report
*response
)
331 struct hidpp_report
*message
;
334 if (param_count
> sizeof(message
->fap
.params
))
337 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
341 if (param_count
> (HIDPP_REPORT_LONG_LENGTH
- 4))
342 message
->report_id
= REPORT_ID_HIDPP_VERY_LONG
;
344 message
->report_id
= REPORT_ID_HIDPP_LONG
;
345 message
->fap
.feature_index
= feat_index
;
346 message
->fap
.funcindex_clientid
= funcindex_clientid
;
347 memcpy(&message
->fap
.params
, params
, param_count
);
349 ret
= hidpp_send_message_sync(hidpp
, message
, response
);
354 static int hidpp_send_rap_command_sync(struct hidpp_device
*hidpp_dev
,
355 u8 report_id
, u8 sub_id
, u8 reg_address
, u8
*params
, int param_count
,
356 struct hidpp_report
*response
)
358 struct hidpp_report
*message
;
361 /* Send as long report if short reports are not supported. */
362 if (report_id
== REPORT_ID_HIDPP_SHORT
&&
363 !(hidpp_dev
->supported_reports
& HIDPP_REPORT_SHORT_SUPPORTED
))
364 report_id
= REPORT_ID_HIDPP_LONG
;
367 case REPORT_ID_HIDPP_SHORT
:
368 max_count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
370 case REPORT_ID_HIDPP_LONG
:
371 max_count
= HIDPP_REPORT_LONG_LENGTH
- 4;
373 case REPORT_ID_HIDPP_VERY_LONG
:
374 max_count
= hidpp_dev
->very_long_report_length
- 4;
380 if (param_count
> max_count
)
383 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
386 message
->report_id
= report_id
;
387 message
->rap
.sub_id
= sub_id
;
388 message
->rap
.reg_address
= reg_address
;
389 memcpy(&message
->rap
.params
, params
, param_count
);
391 ret
= hidpp_send_message_sync(hidpp_dev
, message
, response
);
396 static void delayed_work_cb(struct work_struct
*work
)
398 struct hidpp_device
*hidpp
= container_of(work
, struct hidpp_device
,
400 hidpp_connect_event(hidpp
);
403 static inline bool hidpp_match_answer(struct hidpp_report
*question
,
404 struct hidpp_report
*answer
)
406 return (answer
->fap
.feature_index
== question
->fap
.feature_index
) &&
407 (answer
->fap
.funcindex_clientid
== question
->fap
.funcindex_clientid
);
410 static inline bool hidpp_match_error(struct hidpp_report
*question
,
411 struct hidpp_report
*answer
)
413 return ((answer
->rap
.sub_id
== HIDPP_ERROR
) ||
414 (answer
->fap
.feature_index
== HIDPP20_ERROR
)) &&
415 (answer
->fap
.funcindex_clientid
== question
->fap
.feature_index
) &&
416 (answer
->fap
.params
[0] == question
->fap
.funcindex_clientid
);
419 static inline bool hidpp_report_is_connect_event(struct hidpp_device
*hidpp
,
420 struct hidpp_report
*report
)
422 return (hidpp
->wireless_feature_index
&&
423 (report
->fap
.feature_index
== hidpp
->wireless_feature_index
)) ||
424 ((report
->report_id
== REPORT_ID_HIDPP_SHORT
) &&
425 (report
->rap
.sub_id
== 0x41));
429 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
431 static void hidpp_prefix_name(char **name
, int name_length
)
433 #define PREFIX_LENGTH 9 /* "Logitech " */
438 if (name_length
> PREFIX_LENGTH
&&
439 strncmp(*name
, "Logitech ", PREFIX_LENGTH
) == 0)
440 /* The prefix has is already in the name */
443 new_length
= PREFIX_LENGTH
+ name_length
;
444 new_name
= kzalloc(new_length
, GFP_KERNEL
);
448 snprintf(new_name
, new_length
, "Logitech %s", *name
);
456 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
457 * events given a high-resolution wheel
459 * @input_dev: Pointer to the input device
460 * @counter: a hid_scroll_counter struct describing the wheel.
461 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
464 * Given a high-resolution movement, this function converts the movement into
465 * fractions of 120 and emits high-resolution scroll events for the input
466 * device. It also uses the multiplier from &struct hid_scroll_counter to
467 * emit low-resolution scroll events when appropriate for
468 * backwards-compatibility with userspace input libraries.
470 static void hidpp_scroll_counter_handle_scroll(struct input_dev
*input_dev
,
471 struct hidpp_scroll_counter
*counter
,
474 int low_res_value
, remainder
, direction
;
475 unsigned long long now
, previous
;
477 hi_res_value
= hi_res_value
* 120/counter
->wheel_multiplier
;
478 input_report_rel(input_dev
, REL_WHEEL_HI_RES
, hi_res_value
);
480 remainder
= counter
->remainder
;
481 direction
= hi_res_value
> 0 ? 1 : -1;
484 previous
= counter
->last_time
;
485 counter
->last_time
= now
;
487 * Reset the remainder after a period of inactivity or when the
488 * direction changes. This prevents the REL_WHEEL emulation point
489 * from sliding for devices that don't always provide the same
490 * number of movements per detent.
492 if (now
- previous
> 1000000000 || direction
!= counter
->direction
)
495 counter
->direction
= direction
;
496 remainder
+= hi_res_value
;
498 /* Some wheels will rest 7/8ths of a detent from the previous detent
499 * after slow movement, so we want the threshold for low-res events to
500 * be in the middle between two detents (e.g. after 4/8ths) as
501 * opposed to on the detents themselves (8/8ths).
503 if (abs(remainder
) >= 60) {
504 /* Add (or subtract) 1 because we want to trigger when the wheel
505 * is half-way to the next detent (i.e. scroll 1 detent after a
506 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
509 low_res_value
= remainder
/ 120;
510 if (low_res_value
== 0)
511 low_res_value
= (hi_res_value
> 0 ? 1 : -1);
512 input_report_rel(input_dev
, REL_WHEEL
, low_res_value
);
513 remainder
-= low_res_value
* 120;
515 counter
->remainder
= remainder
;
518 /* -------------------------------------------------------------------------- */
519 /* HIDP++ 1.0 commands */
520 /* -------------------------------------------------------------------------- */
522 #define HIDPP_SET_REGISTER 0x80
523 #define HIDPP_GET_REGISTER 0x81
524 #define HIDPP_SET_LONG_REGISTER 0x82
525 #define HIDPP_GET_LONG_REGISTER 0x83
528 * hidpp10_set_register - Modify a HID++ 1.0 register.
529 * @hidpp_dev: the device to set the register on.
530 * @register_address: the address of the register to modify.
531 * @byte: the byte of the register to modify. Should be less than 3.
532 * @mask: mask of the bits to modify
533 * @value: new values for the bits in mask
534 * Return: 0 if successful, otherwise a negative error code.
536 static int hidpp10_set_register(struct hidpp_device
*hidpp_dev
,
537 u8 register_address
, u8 byte
, u8 mask
, u8 value
)
539 struct hidpp_report response
;
541 u8 params
[3] = { 0 };
543 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
544 REPORT_ID_HIDPP_SHORT
,
551 memcpy(params
, response
.rap
.params
, 3);
553 params
[byte
] &= ~mask
;
554 params
[byte
] |= value
& mask
;
556 return hidpp_send_rap_command_sync(hidpp_dev
,
557 REPORT_ID_HIDPP_SHORT
,
560 params
, 3, &response
);
563 #define HIDPP_REG_ENABLE_REPORTS 0x00
564 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
565 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
566 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
567 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
568 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
570 static int hidpp10_enable_battery_reporting(struct hidpp_device
*hidpp_dev
)
572 return hidpp10_set_register(hidpp_dev
, HIDPP_REG_ENABLE_REPORTS
, 0,
573 HIDPP_ENABLE_BAT_REPORT
, HIDPP_ENABLE_BAT_REPORT
);
576 #define HIDPP_REG_FEATURES 0x01
577 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
578 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
580 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
581 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device
*hidpp_dev
)
583 return hidpp10_set_register(hidpp_dev
, HIDPP_REG_FEATURES
, 0,
584 HIDPP_ENABLE_FAST_SCROLL
, HIDPP_ENABLE_FAST_SCROLL
);
587 #define HIDPP_REG_BATTERY_STATUS 0x07
589 static int hidpp10_battery_status_map_level(u8 param
)
595 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
598 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
601 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
604 level
= POWER_SUPPLY_CAPACITY_LEVEL_HIGH
;
607 level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
613 static int hidpp10_battery_status_map_status(u8 param
)
619 /* discharging (in use) */
620 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
622 case 0x21: /* (standard) charging */
623 case 0x24: /* fast charging */
624 case 0x25: /* slow charging */
625 status
= POWER_SUPPLY_STATUS_CHARGING
;
627 case 0x26: /* topping charge */
628 case 0x22: /* charge complete */
629 status
= POWER_SUPPLY_STATUS_FULL
;
631 case 0x20: /* unknown */
632 status
= POWER_SUPPLY_STATUS_UNKNOWN
;
635 * 0x01...0x1F = reserved (not charging)
636 * 0x23 = charging error
637 * 0x27..0xff = reserved
640 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
647 static int hidpp10_query_battery_status(struct hidpp_device
*hidpp
)
649 struct hidpp_report response
;
652 ret
= hidpp_send_rap_command_sync(hidpp
,
653 REPORT_ID_HIDPP_SHORT
,
655 HIDPP_REG_BATTERY_STATUS
,
660 hidpp
->battery
.level
=
661 hidpp10_battery_status_map_level(response
.rap
.params
[0]);
662 status
= hidpp10_battery_status_map_status(response
.rap
.params
[1]);
663 hidpp
->battery
.status
= status
;
664 /* the capacity is only available when discharging or full */
665 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
666 status
== POWER_SUPPLY_STATUS_FULL
;
671 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
673 static int hidpp10_battery_mileage_map_status(u8 param
)
677 switch (param
>> 6) {
679 /* discharging (in use) */
680 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
682 case 0x01: /* charging */
683 status
= POWER_SUPPLY_STATUS_CHARGING
;
685 case 0x02: /* charge complete */
686 status
= POWER_SUPPLY_STATUS_FULL
;
689 * 0x03 = charging error
692 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
699 static int hidpp10_query_battery_mileage(struct hidpp_device
*hidpp
)
701 struct hidpp_report response
;
704 ret
= hidpp_send_rap_command_sync(hidpp
,
705 REPORT_ID_HIDPP_SHORT
,
707 HIDPP_REG_BATTERY_MILEAGE
,
712 hidpp
->battery
.capacity
= response
.rap
.params
[0];
713 status
= hidpp10_battery_mileage_map_status(response
.rap
.params
[2]);
714 hidpp
->battery
.status
= status
;
715 /* the capacity is only available when discharging or full */
716 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
717 status
== POWER_SUPPLY_STATUS_FULL
;
722 static int hidpp10_battery_event(struct hidpp_device
*hidpp
, u8
*data
, int size
)
724 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
725 int status
, capacity
, level
;
728 if (report
->report_id
!= REPORT_ID_HIDPP_SHORT
)
731 switch (report
->rap
.sub_id
) {
732 case HIDPP_REG_BATTERY_STATUS
:
733 capacity
= hidpp
->battery
.capacity
;
734 level
= hidpp10_battery_status_map_level(report
->rawbytes
[1]);
735 status
= hidpp10_battery_status_map_status(report
->rawbytes
[2]);
737 case HIDPP_REG_BATTERY_MILEAGE
:
738 capacity
= report
->rap
.params
[0];
739 level
= hidpp
->battery
.level
;
740 status
= hidpp10_battery_mileage_map_status(report
->rawbytes
[3]);
746 changed
= capacity
!= hidpp
->battery
.capacity
||
747 level
!= hidpp
->battery
.level
||
748 status
!= hidpp
->battery
.status
;
750 /* the capacity is only available when discharging or full */
751 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
752 status
== POWER_SUPPLY_STATUS_FULL
;
755 hidpp
->battery
.level
= level
;
756 hidpp
->battery
.status
= status
;
757 if (hidpp
->battery
.ps
)
758 power_supply_changed(hidpp
->battery
.ps
);
764 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
765 #define HIDPP_EXTENDED_PAIRING 0x30
766 #define HIDPP_DEVICE_NAME 0x40
768 static char *hidpp_unifying_get_name(struct hidpp_device
*hidpp_dev
)
770 struct hidpp_report response
;
772 u8 params
[1] = { HIDPP_DEVICE_NAME
};
776 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
777 REPORT_ID_HIDPP_SHORT
,
778 HIDPP_GET_LONG_REGISTER
,
779 HIDPP_REG_PAIRING_INFORMATION
,
780 params
, 1, &response
);
784 len
= response
.rap
.params
[1];
786 if (2 + len
> sizeof(response
.rap
.params
))
789 if (len
< 4) /* logitech devices are usually at least Xddd */
792 name
= kzalloc(len
+ 1, GFP_KERNEL
);
796 memcpy(name
, &response
.rap
.params
[2], len
);
798 /* include the terminating '\0' */
799 hidpp_prefix_name(&name
, len
+ 1);
804 static int hidpp_unifying_get_serial(struct hidpp_device
*hidpp
, u32
*serial
)
806 struct hidpp_report response
;
808 u8 params
[1] = { HIDPP_EXTENDED_PAIRING
};
810 ret
= hidpp_send_rap_command_sync(hidpp
,
811 REPORT_ID_HIDPP_SHORT
,
812 HIDPP_GET_LONG_REGISTER
,
813 HIDPP_REG_PAIRING_INFORMATION
,
814 params
, 1, &response
);
819 * We don't care about LE or BE, we will output it as a string
820 * with %4phD, so we need to keep the order.
822 *serial
= *((u32
*)&response
.rap
.params
[1]);
826 static int hidpp_unifying_init(struct hidpp_device
*hidpp
)
828 struct hid_device
*hdev
= hidpp
->hid_dev
;
833 ret
= hidpp_unifying_get_serial(hidpp
, &serial
);
837 snprintf(hdev
->uniq
, sizeof(hdev
->uniq
), "%04x-%4phD",
838 hdev
->product
, &serial
);
839 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev
->uniq
);
841 name
= hidpp_unifying_get_name(hidpp
);
845 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
846 dbg_hid("HID++ Unifying: Got name: %s\n", name
);
852 /* -------------------------------------------------------------------------- */
854 /* -------------------------------------------------------------------------- */
856 #define HIDPP_PAGE_ROOT 0x0000
857 #define HIDPP_PAGE_ROOT_IDX 0x00
859 #define CMD_ROOT_GET_FEATURE 0x01
860 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
862 static int hidpp_root_get_feature(struct hidpp_device
*hidpp
, u16 feature
,
863 u8
*feature_index
, u8
*feature_type
)
865 struct hidpp_report response
;
867 u8 params
[2] = { feature
>> 8, feature
& 0x00FF };
869 ret
= hidpp_send_fap_command_sync(hidpp
,
871 CMD_ROOT_GET_FEATURE
,
872 params
, 2, &response
);
876 if (response
.fap
.params
[0] == 0)
879 *feature_index
= response
.fap
.params
[0];
880 *feature_type
= response
.fap
.params
[1];
885 static int hidpp_root_get_protocol_version(struct hidpp_device
*hidpp
)
887 const u8 ping_byte
= 0x5a;
888 u8 ping_data
[3] = { 0, 0, ping_byte
};
889 struct hidpp_report response
;
892 ret
= hidpp_send_rap_command_sync(hidpp
,
893 REPORT_ID_HIDPP_SHORT
,
895 CMD_ROOT_GET_PROTOCOL_VERSION
,
896 ping_data
, sizeof(ping_data
), &response
);
898 if (ret
== HIDPP_ERROR_INVALID_SUBID
) {
899 hidpp
->protocol_major
= 1;
900 hidpp
->protocol_minor
= 0;
904 /* the device might not be connected */
905 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
909 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
916 if (response
.rap
.params
[2] != ping_byte
) {
917 hid_err(hidpp
->hid_dev
, "%s: ping mismatch 0x%02x != 0x%02x\n",
918 __func__
, response
.rap
.params
[2], ping_byte
);
922 hidpp
->protocol_major
= response
.rap
.params
[0];
923 hidpp
->protocol_minor
= response
.rap
.params
[1];
926 hid_info(hidpp
->hid_dev
, "HID++ %u.%u device connected.\n",
927 hidpp
->protocol_major
, hidpp
->protocol_minor
);
931 /* -------------------------------------------------------------------------- */
932 /* 0x0005: GetDeviceNameType */
933 /* -------------------------------------------------------------------------- */
935 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
937 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
938 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
939 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
941 static int hidpp_devicenametype_get_count(struct hidpp_device
*hidpp
,
942 u8 feature_index
, u8
*nameLength
)
944 struct hidpp_report response
;
947 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
948 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT
, NULL
, 0, &response
);
951 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
958 *nameLength
= response
.fap
.params
[0];
963 static int hidpp_devicenametype_get_device_name(struct hidpp_device
*hidpp
,
964 u8 feature_index
, u8 char_index
, char *device_name
, int len_buf
)
966 struct hidpp_report response
;
970 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
971 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME
, &char_index
, 1,
975 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
982 switch (response
.report_id
) {
983 case REPORT_ID_HIDPP_VERY_LONG
:
984 count
= hidpp
->very_long_report_length
- 4;
986 case REPORT_ID_HIDPP_LONG
:
987 count
= HIDPP_REPORT_LONG_LENGTH
- 4;
989 case REPORT_ID_HIDPP_SHORT
:
990 count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
999 for (i
= 0; i
< count
; i
++)
1000 device_name
[i
] = response
.fap
.params
[i
];
1005 static char *hidpp_get_device_name(struct hidpp_device
*hidpp
)
1014 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_GET_DEVICE_NAME_TYPE
,
1015 &feature_index
, &feature_type
);
1019 ret
= hidpp_devicenametype_get_count(hidpp
, feature_index
,
1024 name
= kzalloc(__name_length
+ 1, GFP_KERNEL
);
1028 while (index
< __name_length
) {
1029 ret
= hidpp_devicenametype_get_device_name(hidpp
,
1030 feature_index
, index
, name
+ index
,
1031 __name_length
- index
);
1039 /* include the terminating '\0' */
1040 hidpp_prefix_name(&name
, __name_length
+ 1);
1045 /* -------------------------------------------------------------------------- */
1046 /* 0x1000: Battery level status */
1047 /* -------------------------------------------------------------------------- */
1049 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1051 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1052 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1054 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1056 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1057 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1058 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1060 static int hidpp_map_battery_level(int capacity
)
1063 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
1065 * The spec says this should be < 31 but some devices report 30
1066 * with brand new batteries and Windows reports 30 as "Good".
1068 else if (capacity
< 30)
1069 return POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
1070 else if (capacity
< 81)
1071 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
1072 return POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1075 static int hidpp20_batterylevel_map_status_capacity(u8 data
[3], int *capacity
,
1081 *capacity
= data
[0];
1082 *next_capacity
= data
[1];
1083 *level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
1085 /* When discharging, we can rely on the device reported capacity.
1086 * For all other states the device reports 0 (unknown).
1089 case 0: /* discharging (in use) */
1090 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1091 *level
= hidpp_map_battery_level(*capacity
);
1093 case 1: /* recharging */
1094 status
= POWER_SUPPLY_STATUS_CHARGING
;
1096 case 2: /* charge in final stage */
1097 status
= POWER_SUPPLY_STATUS_CHARGING
;
1099 case 3: /* charge complete */
1100 status
= POWER_SUPPLY_STATUS_FULL
;
1101 *level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1104 case 4: /* recharging below optimal speed */
1105 status
= POWER_SUPPLY_STATUS_CHARGING
;
1107 /* 5 = invalid battery type
1109 7 = other charging error */
1111 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1118 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device
*hidpp
,
1125 struct hidpp_report response
;
1127 u8
*params
= (u8
*)response
.fap
.params
;
1129 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1130 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS
,
1131 NULL
, 0, &response
);
1132 /* Ignore these intermittent errors */
1133 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
1136 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1143 *status
= hidpp20_batterylevel_map_status_capacity(params
, capacity
,
1150 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device
*hidpp
,
1153 struct hidpp_report response
;
1155 u8
*params
= (u8
*)response
.fap
.params
;
1156 unsigned int level_count
, flags
;
1158 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1159 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY
,
1160 NULL
, 0, &response
);
1162 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1169 level_count
= params
[0];
1172 if (level_count
< 10 || !(flags
& FLAG_BATTERY_LEVEL_MILEAGE
))
1173 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
;
1175 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
1180 static int hidpp20_query_battery_info_1000(struct hidpp_device
*hidpp
)
1184 int status
, capacity
, next_capacity
, level
;
1186 if (hidpp
->battery
.feature_index
== 0xff) {
1187 ret
= hidpp_root_get_feature(hidpp
,
1188 HIDPP_PAGE_BATTERY_LEVEL_STATUS
,
1189 &hidpp
->battery
.feature_index
,
1195 ret
= hidpp20_batterylevel_get_battery_capacity(hidpp
,
1196 hidpp
->battery
.feature_index
,
1198 &next_capacity
, &level
);
1202 ret
= hidpp20_batterylevel_get_battery_info(hidpp
,
1203 hidpp
->battery
.feature_index
);
1207 hidpp
->battery
.status
= status
;
1208 hidpp
->battery
.capacity
= capacity
;
1209 hidpp
->battery
.level
= level
;
1210 /* the capacity is only available when discharging or full */
1211 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
1212 status
== POWER_SUPPLY_STATUS_FULL
;
1217 static int hidpp20_battery_event_1000(struct hidpp_device
*hidpp
,
1220 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1221 int status
, capacity
, next_capacity
, level
;
1224 if (report
->fap
.feature_index
!= hidpp
->battery
.feature_index
||
1225 report
->fap
.funcindex_clientid
!= EVENT_BATTERY_LEVEL_STATUS_BROADCAST
)
1228 status
= hidpp20_batterylevel_map_status_capacity(report
->fap
.params
,
1233 /* the capacity is only available when discharging or full */
1234 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
1235 status
== POWER_SUPPLY_STATUS_FULL
;
1237 changed
= capacity
!= hidpp
->battery
.capacity
||
1238 level
!= hidpp
->battery
.level
||
1239 status
!= hidpp
->battery
.status
;
1242 hidpp
->battery
.level
= level
;
1243 hidpp
->battery
.capacity
= capacity
;
1244 hidpp
->battery
.status
= status
;
1245 if (hidpp
->battery
.ps
)
1246 power_supply_changed(hidpp
->battery
.ps
);
1252 /* -------------------------------------------------------------------------- */
1253 /* 0x1001: Battery voltage */
1254 /* -------------------------------------------------------------------------- */
1256 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1258 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1260 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1262 static int hidpp20_battery_map_status_voltage(u8 data
[3], int *voltage
,
1263 int *level
, int *charge_type
)
1267 long flags
= (long) data
[2];
1268 *level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
1271 switch (flags
& 0x07) {
1273 status
= POWER_SUPPLY_STATUS_CHARGING
;
1276 status
= POWER_SUPPLY_STATUS_FULL
;
1277 *level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1280 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1283 status
= POWER_SUPPLY_STATUS_UNKNOWN
;
1287 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1289 *charge_type
= POWER_SUPPLY_CHARGE_TYPE_STANDARD
;
1290 if (test_bit(3, &flags
)) {
1291 *charge_type
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
1293 if (test_bit(4, &flags
)) {
1294 *charge_type
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
1296 if (test_bit(5, &flags
)) {
1297 *level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
1300 *voltage
= get_unaligned_be16(data
);
1305 static int hidpp20_battery_get_battery_voltage(struct hidpp_device
*hidpp
,
1307 int *status
, int *voltage
,
1308 int *level
, int *charge_type
)
1310 struct hidpp_report response
;
1312 u8
*params
= (u8
*)response
.fap
.params
;
1314 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1315 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE
,
1316 NULL
, 0, &response
);
1319 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1326 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_VOLTAGE
;
1328 *status
= hidpp20_battery_map_status_voltage(params
, voltage
,
1329 level
, charge_type
);
1334 static int hidpp20_query_battery_voltage_info(struct hidpp_device
*hidpp
)
1338 int status
, voltage
, level
, charge_type
;
1340 if (hidpp
->battery
.voltage_feature_index
== 0xff) {
1341 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_BATTERY_VOLTAGE
,
1342 &hidpp
->battery
.voltage_feature_index
,
1348 ret
= hidpp20_battery_get_battery_voltage(hidpp
,
1349 hidpp
->battery
.voltage_feature_index
,
1350 &status
, &voltage
, &level
, &charge_type
);
1355 hidpp
->battery
.status
= status
;
1356 hidpp
->battery
.voltage
= voltage
;
1357 hidpp
->battery
.level
= level
;
1358 hidpp
->battery
.charge_type
= charge_type
;
1359 hidpp
->battery
.online
= status
!= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1364 static int hidpp20_battery_voltage_event(struct hidpp_device
*hidpp
,
1367 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1368 int status
, voltage
, level
, charge_type
;
1370 if (report
->fap
.feature_index
!= hidpp
->battery
.voltage_feature_index
||
1371 report
->fap
.funcindex_clientid
!= EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST
)
1374 status
= hidpp20_battery_map_status_voltage(report
->fap
.params
, &voltage
,
1375 &level
, &charge_type
);
1377 hidpp
->battery
.online
= status
!= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1379 if (voltage
!= hidpp
->battery
.voltage
|| status
!= hidpp
->battery
.status
) {
1380 hidpp
->battery
.voltage
= voltage
;
1381 hidpp
->battery
.status
= status
;
1382 hidpp
->battery
.level
= level
;
1383 hidpp
->battery
.charge_type
= charge_type
;
1384 if (hidpp
->battery
.ps
)
1385 power_supply_changed(hidpp
->battery
.ps
);
1390 /* -------------------------------------------------------------------------- */
1391 /* 0x1004: Unified battery */
1392 /* -------------------------------------------------------------------------- */
1394 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1396 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1397 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1399 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1401 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1402 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1403 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1404 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1406 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1407 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1409 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device
*hidpp
,
1412 struct hidpp_report response
;
1414 u8
*params
= (u8
*)response
.fap
.params
;
1416 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
||
1417 hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_PERCENTAGE
) {
1418 /* we have already set the device capabilities, so let's skip */
1422 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1423 CMD_UNIFIED_BATTERY_GET_CAPABILITIES
,
1424 NULL
, 0, &response
);
1425 /* Ignore these intermittent errors */
1426 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
1429 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1437 * If the device supports state of charge (battery percentage) we won't
1438 * export the battery level information. there are 4 possible battery
1439 * levels and they all are optional, this means that the device might
1440 * not support any of them, we are just better off with the battery
1443 if (params
[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE
) {
1444 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_PERCENTAGE
;
1445 hidpp
->battery
.supported_levels_1004
= 0;
1447 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
;
1448 hidpp
->battery
.supported_levels_1004
= params
[0];
1454 static int hidpp20_unifiedbattery_map_status(struct hidpp_device
*hidpp
,
1456 u8 external_power_status
)
1460 switch (charging_status
) {
1461 case 0: /* discharging */
1462 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1464 case 1: /* charging */
1465 case 2: /* charging slow */
1466 status
= POWER_SUPPLY_STATUS_CHARGING
;
1468 case 3: /* complete */
1469 status
= POWER_SUPPLY_STATUS_FULL
;
1472 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1473 hid_info(hidpp
->hid_dev
, "%s: charging error",
1477 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1484 static int hidpp20_unifiedbattery_map_level(struct hidpp_device
*hidpp
,
1487 /* cler unsupported level bits */
1488 battery_level
&= hidpp
->battery
.supported_levels_1004
;
1490 if (battery_level
& FLAG_UNIFIED_BATTERY_LEVEL_FULL
)
1491 return POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1492 else if (battery_level
& FLAG_UNIFIED_BATTERY_LEVEL_GOOD
)
1493 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
1494 else if (battery_level
& FLAG_UNIFIED_BATTERY_LEVEL_LOW
)
1495 return POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
1496 else if (battery_level
& FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL
)
1497 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
1499 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
1502 static int hidpp20_unifiedbattery_get_status(struct hidpp_device
*hidpp
,
1504 u8
*state_of_charge
,
1508 struct hidpp_report response
;
1510 u8
*params
= (u8
*)response
.fap
.params
;
1512 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1513 CMD_UNIFIED_BATTERY_GET_STATUS
,
1514 NULL
, 0, &response
);
1515 /* Ignore these intermittent errors */
1516 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
1519 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1526 *state_of_charge
= params
[0];
1527 *status
= hidpp20_unifiedbattery_map_status(hidpp
, params
[2], params
[3]);
1528 *level
= hidpp20_unifiedbattery_map_level(hidpp
, params
[1]);
1533 static int hidpp20_query_battery_info_1004(struct hidpp_device
*hidpp
)
1540 if (hidpp
->battery
.feature_index
== 0xff) {
1541 ret
= hidpp_root_get_feature(hidpp
,
1542 HIDPP_PAGE_UNIFIED_BATTERY
,
1543 &hidpp
->battery
.feature_index
,
1549 ret
= hidpp20_unifiedbattery_get_capabilities(hidpp
,
1550 hidpp
->battery
.feature_index
);
1554 ret
= hidpp20_unifiedbattery_get_status(hidpp
,
1555 hidpp
->battery
.feature_index
,
1562 hidpp
->capabilities
|= HIDPP_CAPABILITY_UNIFIED_BATTERY
;
1563 hidpp
->battery
.capacity
= state_of_charge
;
1564 hidpp
->battery
.status
= status
;
1565 hidpp
->battery
.level
= level
;
1566 hidpp
->battery
.online
= true;
1571 static int hidpp20_battery_event_1004(struct hidpp_device
*hidpp
,
1574 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1575 u8
*params
= (u8
*)report
->fap
.params
;
1576 int state_of_charge
, status
, level
;
1579 if (report
->fap
.feature_index
!= hidpp
->battery
.feature_index
||
1580 report
->fap
.funcindex_clientid
!= EVENT_UNIFIED_BATTERY_STATUS_EVENT
)
1583 state_of_charge
= params
[0];
1584 status
= hidpp20_unifiedbattery_map_status(hidpp
, params
[2], params
[3]);
1585 level
= hidpp20_unifiedbattery_map_level(hidpp
, params
[1]);
1587 changed
= status
!= hidpp
->battery
.status
||
1588 (state_of_charge
!= hidpp
->battery
.capacity
&&
1589 hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_PERCENTAGE
) ||
1590 (level
!= hidpp
->battery
.level
&&
1591 hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
);
1594 hidpp
->battery
.capacity
= state_of_charge
;
1595 hidpp
->battery
.status
= status
;
1596 hidpp
->battery
.level
= level
;
1597 if (hidpp
->battery
.ps
)
1598 power_supply_changed(hidpp
->battery
.ps
);
1604 /* -------------------------------------------------------------------------- */
1605 /* Battery feature helpers */
1606 /* -------------------------------------------------------------------------- */
1608 static enum power_supply_property hidpp_battery_props
[] = {
1609 POWER_SUPPLY_PROP_ONLINE
,
1610 POWER_SUPPLY_PROP_STATUS
,
1611 POWER_SUPPLY_PROP_SCOPE
,
1612 POWER_SUPPLY_PROP_MODEL_NAME
,
1613 POWER_SUPPLY_PROP_MANUFACTURER
,
1614 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
1615 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1616 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1617 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1620 static int hidpp_battery_get_property(struct power_supply
*psy
,
1621 enum power_supply_property psp
,
1622 union power_supply_propval
*val
)
1624 struct hidpp_device
*hidpp
= power_supply_get_drvdata(psy
);
1628 case POWER_SUPPLY_PROP_STATUS
:
1629 val
->intval
= hidpp
->battery
.status
;
1631 case POWER_SUPPLY_PROP_CAPACITY
:
1632 val
->intval
= hidpp
->battery
.capacity
;
1634 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
1635 val
->intval
= hidpp
->battery
.level
;
1637 case POWER_SUPPLY_PROP_SCOPE
:
1638 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1640 case POWER_SUPPLY_PROP_ONLINE
:
1641 val
->intval
= hidpp
->battery
.online
;
1643 case POWER_SUPPLY_PROP_MODEL_NAME
:
1644 if (!strncmp(hidpp
->name
, "Logitech ", 9))
1645 val
->strval
= hidpp
->name
+ 9;
1647 val
->strval
= hidpp
->name
;
1649 case POWER_SUPPLY_PROP_MANUFACTURER
:
1650 val
->strval
= "Logitech";
1652 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
1653 val
->strval
= hidpp
->hid_dev
->uniq
;
1655 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
1656 /* hardware reports voltage in in mV. sysfs expects uV */
1657 val
->intval
= hidpp
->battery
.voltage
* 1000;
1659 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
1660 val
->intval
= hidpp
->battery
.charge_type
;
1670 /* -------------------------------------------------------------------------- */
1671 /* 0x1d4b: Wireless device status */
1672 /* -------------------------------------------------------------------------- */
1673 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1675 static int hidpp_set_wireless_feature_index(struct hidpp_device
*hidpp
)
1680 ret
= hidpp_root_get_feature(hidpp
,
1681 HIDPP_PAGE_WIRELESS_DEVICE_STATUS
,
1682 &hidpp
->wireless_feature_index
,
1688 /* -------------------------------------------------------------------------- */
1689 /* 0x2120: Hi-resolution scrolling */
1690 /* -------------------------------------------------------------------------- */
1692 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1694 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1696 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device
*hidpp
,
1697 bool enabled
, u8
*multiplier
)
1703 struct hidpp_report response
;
1705 ret
= hidpp_root_get_feature(hidpp
,
1706 HIDPP_PAGE_HI_RESOLUTION_SCROLLING
,
1712 params
[0] = enabled
? BIT(0) : 0;
1713 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1714 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE
,
1715 params
, sizeof(params
), &response
);
1718 *multiplier
= response
.fap
.params
[1];
1722 /* -------------------------------------------------------------------------- */
1723 /* 0x2121: HiRes Wheel */
1724 /* -------------------------------------------------------------------------- */
1726 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1728 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1729 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1731 static int hidpp_hrw_get_wheel_capability(struct hidpp_device
*hidpp
,
1737 struct hidpp_report response
;
1739 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_HIRES_WHEEL
,
1740 &feature_index
, &feature_type
);
1742 goto return_default
;
1744 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1745 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY
,
1746 NULL
, 0, &response
);
1748 goto return_default
;
1750 *multiplier
= response
.fap
.params
[0];
1753 hid_warn(hidpp
->hid_dev
,
1754 "Couldn't get wheel multiplier (error %d)\n", ret
);
1758 static int hidpp_hrw_set_wheel_mode(struct hidpp_device
*hidpp
, bool invert
,
1759 bool high_resolution
, bool use_hidpp
)
1765 struct hidpp_report response
;
1767 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_HIRES_WHEEL
,
1768 &feature_index
, &feature_type
);
1772 params
[0] = (invert
? BIT(2) : 0) |
1773 (high_resolution
? BIT(1) : 0) |
1774 (use_hidpp
? BIT(0) : 0);
1776 return hidpp_send_fap_command_sync(hidpp
, feature_index
,
1777 CMD_HIRES_WHEEL_SET_WHEEL_MODE
,
1778 params
, sizeof(params
), &response
);
1781 /* -------------------------------------------------------------------------- */
1782 /* 0x4301: Solar Keyboard */
1783 /* -------------------------------------------------------------------------- */
1785 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1787 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1789 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1790 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1791 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1793 static int hidpp_solar_request_battery_event(struct hidpp_device
*hidpp
)
1795 struct hidpp_report response
;
1796 u8 params
[2] = { 1, 1 };
1800 if (hidpp
->battery
.feature_index
== 0xff) {
1801 ret
= hidpp_root_get_feature(hidpp
,
1802 HIDPP_PAGE_SOLAR_KEYBOARD
,
1803 &hidpp
->battery
.solar_feature_index
,
1809 ret
= hidpp_send_fap_command_sync(hidpp
,
1810 hidpp
->battery
.solar_feature_index
,
1811 CMD_SOLAR_SET_LIGHT_MEASURE
,
1812 params
, 2, &response
);
1814 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1821 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
1826 static int hidpp_solar_battery_event(struct hidpp_device
*hidpp
,
1829 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1830 int capacity
, lux
, status
;
1833 function
= report
->fap
.funcindex_clientid
;
1836 if (report
->fap
.feature_index
!= hidpp
->battery
.solar_feature_index
||
1837 !(function
== EVENT_SOLAR_BATTERY_BROADCAST
||
1838 function
== EVENT_SOLAR_BATTERY_LIGHT_MEASURE
||
1839 function
== EVENT_SOLAR_CHECK_LIGHT_BUTTON
))
1842 capacity
= report
->fap
.params
[0];
1845 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE
:
1846 lux
= (report
->fap
.params
[1] << 8) | report
->fap
.params
[2];
1848 status
= POWER_SUPPLY_STATUS_CHARGING
;
1850 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1852 case EVENT_SOLAR_CHECK_LIGHT_BUTTON
:
1854 if (capacity
< hidpp
->battery
.capacity
)
1855 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1857 status
= POWER_SUPPLY_STATUS_CHARGING
;
1861 if (capacity
== 100)
1862 status
= POWER_SUPPLY_STATUS_FULL
;
1864 hidpp
->battery
.online
= true;
1865 if (capacity
!= hidpp
->battery
.capacity
||
1866 status
!= hidpp
->battery
.status
) {
1867 hidpp
->battery
.capacity
= capacity
;
1868 hidpp
->battery
.status
= status
;
1869 if (hidpp
->battery
.ps
)
1870 power_supply_changed(hidpp
->battery
.ps
);
1876 /* -------------------------------------------------------------------------- */
1877 /* 0x6010: Touchpad FW items */
1878 /* -------------------------------------------------------------------------- */
1880 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1882 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1884 struct hidpp_touchpad_fw_items
{
1886 uint8_t desired_state
;
1892 * send a set state command to the device by reading the current items->state
1893 * field. items is then filled with the current state.
1895 static int hidpp_touchpad_fw_items_set(struct hidpp_device
*hidpp
,
1897 struct hidpp_touchpad_fw_items
*items
)
1899 struct hidpp_report response
;
1901 u8
*params
= (u8
*)response
.fap
.params
;
1903 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1904 CMD_TOUCHPAD_FW_ITEMS_SET
, &items
->state
, 1, &response
);
1907 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1914 items
->presence
= params
[0];
1915 items
->desired_state
= params
[1];
1916 items
->state
= params
[2];
1917 items
->persistent
= params
[3];
1922 /* -------------------------------------------------------------------------- */
1923 /* 0x6100: TouchPadRawXY */
1924 /* -------------------------------------------------------------------------- */
1926 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1928 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1929 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1931 #define EVENT_TOUCHPAD_RAW_XY 0x00
1933 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1934 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1936 struct hidpp_touchpad_raw_info
{
1947 struct hidpp_touchpad_raw_xy_finger
{
1957 struct hidpp_touchpad_raw_xy
{
1959 struct hidpp_touchpad_raw_xy_finger fingers
[2];
1966 static int hidpp_touchpad_get_raw_info(struct hidpp_device
*hidpp
,
1967 u8 feature_index
, struct hidpp_touchpad_raw_info
*raw_info
)
1969 struct hidpp_report response
;
1971 u8
*params
= (u8
*)response
.fap
.params
;
1973 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1974 CMD_TOUCHPAD_GET_RAW_INFO
, NULL
, 0, &response
);
1977 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1984 raw_info
->x_size
= get_unaligned_be16(¶ms
[0]);
1985 raw_info
->y_size
= get_unaligned_be16(¶ms
[2]);
1986 raw_info
->z_range
= params
[4];
1987 raw_info
->area_range
= params
[5];
1988 raw_info
->maxcontacts
= params
[7];
1989 raw_info
->origin
= params
[8];
1990 /* res is given in unit per inch */
1991 raw_info
->res
= get_unaligned_be16(¶ms
[13]) * 2 / 51;
1996 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device
*hidpp_dev
,
1997 u8 feature_index
, bool send_raw_reports
,
1998 bool sensor_enhanced_settings
)
2000 struct hidpp_report response
;
2004 * bit 0 - enable raw
2005 * bit 1 - 16bit Z, no area
2006 * bit 2 - enhanced sensitivity
2007 * bit 3 - width, height (4 bits each) instead of area
2008 * bit 4 - send raw + gestures (degrades smoothness)
2009 * remaining bits - reserved
2011 u8 params
= send_raw_reports
| (sensor_enhanced_settings
<< 2);
2013 return hidpp_send_fap_command_sync(hidpp_dev
, feature_index
,
2014 CMD_TOUCHPAD_SET_RAW_REPORT_STATE
, ¶ms
, 1, &response
);
2017 static void hidpp_touchpad_touch_event(u8
*data
,
2018 struct hidpp_touchpad_raw_xy_finger
*finger
)
2020 u8 x_m
= data
[0] << 2;
2021 u8 y_m
= data
[2] << 2;
2023 finger
->x
= x_m
<< 6 | data
[1];
2024 finger
->y
= y_m
<< 6 | data
[3];
2026 finger
->contact_type
= data
[0] >> 6;
2027 finger
->contact_status
= data
[2] >> 6;
2029 finger
->z
= data
[4];
2030 finger
->area
= data
[5];
2031 finger
->finger_id
= data
[6] >> 4;
2034 static void hidpp_touchpad_raw_xy_event(struct hidpp_device
*hidpp_dev
,
2035 u8
*data
, struct hidpp_touchpad_raw_xy
*raw_xy
)
2037 memset(raw_xy
, 0, sizeof(struct hidpp_touchpad_raw_xy
));
2038 raw_xy
->end_of_frame
= data
[8] & 0x01;
2039 raw_xy
->spurious_flag
= (data
[8] >> 1) & 0x01;
2040 raw_xy
->finger_count
= data
[15] & 0x0f;
2041 raw_xy
->button
= (data
[8] >> 2) & 0x01;
2043 if (raw_xy
->finger_count
) {
2044 hidpp_touchpad_touch_event(&data
[2], &raw_xy
->fingers
[0]);
2045 hidpp_touchpad_touch_event(&data
[9], &raw_xy
->fingers
[1]);
2049 /* -------------------------------------------------------------------------- */
2050 /* 0x8123: Force feedback support */
2051 /* -------------------------------------------------------------------------- */
2053 #define HIDPP_FF_GET_INFO 0x01
2054 #define HIDPP_FF_RESET_ALL 0x11
2055 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2056 #define HIDPP_FF_SET_EFFECT_STATE 0x31
2057 #define HIDPP_FF_DESTROY_EFFECT 0x41
2058 #define HIDPP_FF_GET_APERTURE 0x51
2059 #define HIDPP_FF_SET_APERTURE 0x61
2060 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2061 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2063 #define HIDPP_FF_EFFECT_STATE_GET 0x00
2064 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
2065 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2066 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2068 #define HIDPP_FF_EFFECT_CONSTANT 0x00
2069 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2070 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2071 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2072 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2073 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2074 #define HIDPP_FF_EFFECT_SPRING 0x06
2075 #define HIDPP_FF_EFFECT_DAMPER 0x07
2076 #define HIDPP_FF_EFFECT_FRICTION 0x08
2077 #define HIDPP_FF_EFFECT_INERTIA 0x09
2078 #define HIDPP_FF_EFFECT_RAMP 0x0A
2080 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
2082 #define HIDPP_FF_EFFECTID_NONE -1
2083 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
2084 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2086 #define HIDPP_FF_MAX_PARAMS 20
2087 #define HIDPP_FF_RESERVED_SLOTS 1
2089 struct hidpp_ff_private_data
{
2090 struct hidpp_device
*hidpp
;
2098 struct workqueue_struct
*wq
;
2099 atomic_t workqueue_size
;
2102 struct hidpp_ff_work_data
{
2103 struct work_struct work
;
2104 struct hidpp_ff_private_data
*data
;
2107 u8 params
[HIDPP_FF_MAX_PARAMS
];
2111 static const signed short hidpp_ff_effects
[] = {
2126 static const signed short hidpp_ff_effects_v2
[] = {
2133 static const u8 HIDPP_FF_CONDITION_CMDS
[] = {
2134 HIDPP_FF_EFFECT_SPRING
,
2135 HIDPP_FF_EFFECT_FRICTION
,
2136 HIDPP_FF_EFFECT_DAMPER
,
2137 HIDPP_FF_EFFECT_INERTIA
2140 static const char *HIDPP_FF_CONDITION_NAMES
[] = {
2148 static u8
hidpp_ff_find_effect(struct hidpp_ff_private_data
*data
, int effect_id
)
2152 for (i
= 0; i
< data
->num_effects
; i
++)
2153 if (data
->effect_ids
[i
] == effect_id
)
2159 static void hidpp_ff_work_handler(struct work_struct
*w
)
2161 struct hidpp_ff_work_data
*wd
= container_of(w
, struct hidpp_ff_work_data
, work
);
2162 struct hidpp_ff_private_data
*data
= wd
->data
;
2163 struct hidpp_report response
;
2167 /* add slot number if needed */
2168 switch (wd
->effect_id
) {
2169 case HIDPP_FF_EFFECTID_AUTOCENTER
:
2170 wd
->params
[0] = data
->slot_autocenter
;
2172 case HIDPP_FF_EFFECTID_NONE
:
2173 /* leave slot as zero */
2176 /* find current slot for effect */
2177 wd
->params
[0] = hidpp_ff_find_effect(data
, wd
->effect_id
);
2181 /* send command and wait for reply */
2182 ret
= hidpp_send_fap_command_sync(data
->hidpp
, data
->feature_index
,
2183 wd
->command
, wd
->params
, wd
->size
, &response
);
2186 hid_err(data
->hidpp
->hid_dev
, "Failed to send command to device!\n");
2190 /* parse return data */
2191 switch (wd
->command
) {
2192 case HIDPP_FF_DOWNLOAD_EFFECT
:
2193 slot
= response
.fap
.params
[0];
2194 if (slot
> 0 && slot
<= data
->num_effects
) {
2195 if (wd
->effect_id
>= 0)
2196 /* regular effect uploaded */
2197 data
->effect_ids
[slot
-1] = wd
->effect_id
;
2198 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
2199 /* autocenter spring uploaded */
2200 data
->slot_autocenter
= slot
;
2203 case HIDPP_FF_DESTROY_EFFECT
:
2204 if (wd
->effect_id
>= 0)
2205 /* regular effect destroyed */
2206 data
->effect_ids
[wd
->params
[0]-1] = -1;
2207 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
2208 /* autocenter spring destoyed */
2209 data
->slot_autocenter
= 0;
2211 case HIDPP_FF_SET_GLOBAL_GAINS
:
2212 data
->gain
= (wd
->params
[0] << 8) + wd
->params
[1];
2214 case HIDPP_FF_SET_APERTURE
:
2215 data
->range
= (wd
->params
[0] << 8) + wd
->params
[1];
2218 /* no action needed */
2223 atomic_dec(&data
->workqueue_size
);
2227 static int hidpp_ff_queue_work(struct hidpp_ff_private_data
*data
, int effect_id
, u8 command
, u8
*params
, u8 size
)
2229 struct hidpp_ff_work_data
*wd
= kzalloc(sizeof(*wd
), GFP_KERNEL
);
2235 INIT_WORK(&wd
->work
, hidpp_ff_work_handler
);
2238 wd
->effect_id
= effect_id
;
2239 wd
->command
= command
;
2241 memcpy(wd
->params
, params
, size
);
2243 atomic_inc(&data
->workqueue_size
);
2244 queue_work(data
->wq
, &wd
->work
);
2246 /* warn about excessive queue size */
2247 s
= atomic_read(&data
->workqueue_size
);
2248 if (s
>= 20 && s
% 20 == 0)
2249 hid_warn(data
->hidpp
->hid_dev
, "Force feedback command queue contains %d commands, causing substantial delays!", s
);
2254 static int hidpp_ff_upload_effect(struct input_dev
*dev
, struct ff_effect
*effect
, struct ff_effect
*old
)
2256 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
2261 /* set common parameters */
2262 params
[2] = effect
->replay
.length
>> 8;
2263 params
[3] = effect
->replay
.length
& 255;
2264 params
[4] = effect
->replay
.delay
>> 8;
2265 params
[5] = effect
->replay
.delay
& 255;
2267 switch (effect
->type
) {
2269 force
= (effect
->u
.constant
.level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
2270 params
[1] = HIDPP_FF_EFFECT_CONSTANT
;
2271 params
[6] = force
>> 8;
2272 params
[7] = force
& 255;
2273 params
[8] = effect
->u
.constant
.envelope
.attack_level
>> 7;
2274 params
[9] = effect
->u
.constant
.envelope
.attack_length
>> 8;
2275 params
[10] = effect
->u
.constant
.envelope
.attack_length
& 255;
2276 params
[11] = effect
->u
.constant
.envelope
.fade_level
>> 7;
2277 params
[12] = effect
->u
.constant
.envelope
.fade_length
>> 8;
2278 params
[13] = effect
->u
.constant
.envelope
.fade_length
& 255;
2280 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2281 effect
->u
.constant
.level
,
2282 effect
->direction
, force
);
2283 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2284 effect
->u
.constant
.envelope
.attack_level
,
2285 effect
->u
.constant
.envelope
.attack_length
,
2286 effect
->u
.constant
.envelope
.fade_level
,
2287 effect
->u
.constant
.envelope
.fade_length
);
2291 switch (effect
->u
.periodic
.waveform
) {
2293 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SINE
;
2296 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE
;
2299 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP
;
2302 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN
;
2305 params
[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE
;
2308 hid_err(data
->hidpp
->hid_dev
, "Unexpected periodic waveform type %i!\n", effect
->u
.periodic
.waveform
);
2311 force
= (effect
->u
.periodic
.magnitude
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
2312 params
[6] = effect
->u
.periodic
.magnitude
>> 8;
2313 params
[7] = effect
->u
.periodic
.magnitude
& 255;
2314 params
[8] = effect
->u
.periodic
.offset
>> 8;
2315 params
[9] = effect
->u
.periodic
.offset
& 255;
2316 params
[10] = effect
->u
.periodic
.period
>> 8;
2317 params
[11] = effect
->u
.periodic
.period
& 255;
2318 params
[12] = effect
->u
.periodic
.phase
>> 8;
2319 params
[13] = effect
->u
.periodic
.phase
& 255;
2320 params
[14] = effect
->u
.periodic
.envelope
.attack_level
>> 7;
2321 params
[15] = effect
->u
.periodic
.envelope
.attack_length
>> 8;
2322 params
[16] = effect
->u
.periodic
.envelope
.attack_length
& 255;
2323 params
[17] = effect
->u
.periodic
.envelope
.fade_level
>> 7;
2324 params
[18] = effect
->u
.periodic
.envelope
.fade_length
>> 8;
2325 params
[19] = effect
->u
.periodic
.envelope
.fade_length
& 255;
2327 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2328 effect
->u
.periodic
.magnitude
, effect
->direction
,
2329 effect
->u
.periodic
.offset
,
2330 effect
->u
.periodic
.period
,
2331 effect
->u
.periodic
.phase
);
2332 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2333 effect
->u
.periodic
.envelope
.attack_level
,
2334 effect
->u
.periodic
.envelope
.attack_length
,
2335 effect
->u
.periodic
.envelope
.fade_level
,
2336 effect
->u
.periodic
.envelope
.fade_length
);
2340 params
[1] = HIDPP_FF_EFFECT_RAMP
;
2341 force
= (effect
->u
.ramp
.start_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
2342 params
[6] = force
>> 8;
2343 params
[7] = force
& 255;
2344 force
= (effect
->u
.ramp
.end_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
2345 params
[8] = force
>> 8;
2346 params
[9] = force
& 255;
2347 params
[10] = effect
->u
.ramp
.envelope
.attack_level
>> 7;
2348 params
[11] = effect
->u
.ramp
.envelope
.attack_length
>> 8;
2349 params
[12] = effect
->u
.ramp
.envelope
.attack_length
& 255;
2350 params
[13] = effect
->u
.ramp
.envelope
.fade_level
>> 7;
2351 params
[14] = effect
->u
.ramp
.envelope
.fade_length
>> 8;
2352 params
[15] = effect
->u
.ramp
.envelope
.fade_length
& 255;
2354 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2355 effect
->u
.ramp
.start_level
,
2356 effect
->u
.ramp
.end_level
,
2357 effect
->direction
, force
);
2358 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2359 effect
->u
.ramp
.envelope
.attack_level
,
2360 effect
->u
.ramp
.envelope
.attack_length
,
2361 effect
->u
.ramp
.envelope
.fade_level
,
2362 effect
->u
.ramp
.envelope
.fade_length
);
2368 params
[1] = HIDPP_FF_CONDITION_CMDS
[effect
->type
- FF_SPRING
];
2369 params
[6] = effect
->u
.condition
[0].left_saturation
>> 9;
2370 params
[7] = (effect
->u
.condition
[0].left_saturation
>> 1) & 255;
2371 params
[8] = effect
->u
.condition
[0].left_coeff
>> 8;
2372 params
[9] = effect
->u
.condition
[0].left_coeff
& 255;
2373 params
[10] = effect
->u
.condition
[0].deadband
>> 9;
2374 params
[11] = (effect
->u
.condition
[0].deadband
>> 1) & 255;
2375 params
[12] = effect
->u
.condition
[0].center
>> 8;
2376 params
[13] = effect
->u
.condition
[0].center
& 255;
2377 params
[14] = effect
->u
.condition
[0].right_coeff
>> 8;
2378 params
[15] = effect
->u
.condition
[0].right_coeff
& 255;
2379 params
[16] = effect
->u
.condition
[0].right_saturation
>> 9;
2380 params
[17] = (effect
->u
.condition
[0].right_saturation
>> 1) & 255;
2382 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2383 HIDPP_FF_CONDITION_NAMES
[effect
->type
- FF_SPRING
],
2384 effect
->u
.condition
[0].left_coeff
,
2385 effect
->u
.condition
[0].left_saturation
,
2386 effect
->u
.condition
[0].right_coeff
,
2387 effect
->u
.condition
[0].right_saturation
);
2388 dbg_hid(" deadband=%d, center=%d\n",
2389 effect
->u
.condition
[0].deadband
,
2390 effect
->u
.condition
[0].center
);
2393 hid_err(data
->hidpp
->hid_dev
, "Unexpected force type %i!\n", effect
->type
);
2397 return hidpp_ff_queue_work(data
, effect
->id
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, size
);
2400 static int hidpp_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
2402 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
2405 params
[1] = value
? HIDPP_FF_EFFECT_STATE_PLAY
: HIDPP_FF_EFFECT_STATE_STOP
;
2407 dbg_hid("St%sing playback of effect %d.\n", value
?"art":"opp", effect_id
);
2409 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_SET_EFFECT_STATE
, params
, ARRAY_SIZE(params
));
2412 static int hidpp_ff_erase_effect(struct input_dev
*dev
, int effect_id
)
2414 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
2417 dbg_hid("Erasing effect %d.\n", effect_id
);
2419 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_DESTROY_EFFECT
, &slot
, 1);
2422 static void hidpp_ff_set_autocenter(struct input_dev
*dev
, u16 magnitude
)
2424 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
2425 u8 params
[HIDPP_AUTOCENTER_PARAMS_LENGTH
];
2427 dbg_hid("Setting autocenter to %d.\n", magnitude
);
2429 /* start a standard spring effect */
2430 params
[1] = HIDPP_FF_EFFECT_SPRING
| HIDPP_FF_EFFECT_AUTOSTART
;
2431 /* zero delay and duration */
2432 params
[2] = params
[3] = params
[4] = params
[5] = 0;
2433 /* set coeff to 25% of saturation */
2434 params
[8] = params
[14] = magnitude
>> 11;
2435 params
[9] = params
[15] = (magnitude
>> 3) & 255;
2436 params
[6] = params
[16] = magnitude
>> 9;
2437 params
[7] = params
[17] = (magnitude
>> 1) & 255;
2438 /* zero deadband and center */
2439 params
[10] = params
[11] = params
[12] = params
[13] = 0;
2441 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_AUTOCENTER
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, ARRAY_SIZE(params
));
2444 static void hidpp_ff_set_gain(struct input_dev
*dev
, u16 gain
)
2446 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
2449 dbg_hid("Setting gain to %d.\n", gain
);
2451 params
[0] = gain
>> 8;
2452 params
[1] = gain
& 255;
2453 params
[2] = 0; /* no boost */
2456 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_NONE
, HIDPP_FF_SET_GLOBAL_GAINS
, params
, ARRAY_SIZE(params
));
2459 static ssize_t
hidpp_ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2461 struct hid_device
*hid
= to_hid_device(dev
);
2462 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
2463 struct input_dev
*idev
= hidinput
->input
;
2464 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
2466 return scnprintf(buf
, PAGE_SIZE
, "%u\n", data
->range
);
2469 static ssize_t
hidpp_ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
2471 struct hid_device
*hid
= to_hid_device(dev
);
2472 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
2473 struct input_dev
*idev
= hidinput
->input
;
2474 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
2476 int range
= simple_strtoul(buf
, NULL
, 10);
2478 range
= clamp(range
, 180, 900);
2480 params
[0] = range
>> 8;
2481 params
[1] = range
& 0x00FF;
2483 hidpp_ff_queue_work(data
, -1, HIDPP_FF_SET_APERTURE
, params
, ARRAY_SIZE(params
));
2488 static DEVICE_ATTR(range
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
, hidpp_ff_range_show
, hidpp_ff_range_store
);
2490 static void hidpp_ff_destroy(struct ff_device
*ff
)
2492 struct hidpp_ff_private_data
*data
= ff
->private;
2493 struct hid_device
*hid
= data
->hidpp
->hid_dev
;
2495 hid_info(hid
, "Unloading HID++ force feedback.\n");
2497 device_remove_file(&hid
->dev
, &dev_attr_range
);
2498 destroy_workqueue(data
->wq
);
2499 kfree(data
->effect_ids
);
2502 static int hidpp_ff_init(struct hidpp_device
*hidpp
,
2503 struct hidpp_ff_private_data
*data
)
2505 struct hid_device
*hid
= hidpp
->hid_dev
;
2506 struct hid_input
*hidinput
;
2507 struct input_dev
*dev
;
2508 const struct usb_device_descriptor
*udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
2509 const u16 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
2510 struct ff_device
*ff
;
2511 int error
, j
, num_slots
= data
->num_effects
;
2514 if (list_empty(&hid
->inputs
)) {
2515 hid_err(hid
, "no inputs found\n");
2518 hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
2519 dev
= hidinput
->input
;
2522 hid_err(hid
, "Struct input_dev not set!\n");
2526 /* Get firmware release */
2527 version
= bcdDevice
& 255;
2529 /* Set supported force feedback capabilities */
2530 for (j
= 0; hidpp_ff_effects
[j
] >= 0; j
++)
2531 set_bit(hidpp_ff_effects
[j
], dev
->ffbit
);
2533 for (j
= 0; hidpp_ff_effects_v2
[j
] >= 0; j
++)
2534 set_bit(hidpp_ff_effects_v2
[j
], dev
->ffbit
);
2536 error
= input_ff_create(dev
, num_slots
);
2539 hid_err(dev
, "Failed to create FF device!\n");
2543 * Create a copy of passed data, so we can transfer memory
2544 * ownership to FF core
2546 data
= kmemdup(data
, sizeof(*data
), GFP_KERNEL
);
2549 data
->effect_ids
= kcalloc(num_slots
, sizeof(int), GFP_KERNEL
);
2550 if (!data
->effect_ids
) {
2554 data
->wq
= create_singlethread_workqueue("hidpp-ff-sendqueue");
2556 kfree(data
->effect_ids
);
2561 data
->hidpp
= hidpp
;
2562 data
->version
= version
;
2563 for (j
= 0; j
< num_slots
; j
++)
2564 data
->effect_ids
[j
] = -1;
2569 ff
->upload
= hidpp_ff_upload_effect
;
2570 ff
->erase
= hidpp_ff_erase_effect
;
2571 ff
->playback
= hidpp_ff_playback
;
2572 ff
->set_gain
= hidpp_ff_set_gain
;
2573 ff
->set_autocenter
= hidpp_ff_set_autocenter
;
2574 ff
->destroy
= hidpp_ff_destroy
;
2576 /* Create sysfs interface */
2577 error
= device_create_file(&(hidpp
->hid_dev
->dev
), &dev_attr_range
);
2579 hid_warn(hidpp
->hid_dev
, "Unable to create sysfs interface for \"range\", errno %d!\n", error
);
2581 /* init the hardware command queue */
2582 atomic_set(&data
->workqueue_size
, 0);
2584 hid_info(hid
, "Force feedback support loaded (firmware release %d).\n",
2590 /* ************************************************************************** */
2592 /* Device Support */
2594 /* ************************************************************************** */
2596 /* -------------------------------------------------------------------------- */
2597 /* Touchpad HID++ devices */
2598 /* -------------------------------------------------------------------------- */
2600 #define WTP_MANUAL_RESOLUTION 39
2605 u8 mt_feature_index
;
2606 u8 button_feature_index
;
2609 unsigned int resolution
;
2612 static int wtp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2613 struct hid_field
*field
, struct hid_usage
*usage
,
2614 unsigned long **bit
, int *max
)
2619 static void wtp_populate_input(struct hidpp_device
*hidpp
,
2620 struct input_dev
*input_dev
)
2622 struct wtp_data
*wd
= hidpp
->private_data
;
2624 __set_bit(EV_ABS
, input_dev
->evbit
);
2625 __set_bit(EV_KEY
, input_dev
->evbit
);
2626 __clear_bit(EV_REL
, input_dev
->evbit
);
2627 __clear_bit(EV_LED
, input_dev
->evbit
);
2629 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, wd
->x_size
, 0, 0);
2630 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
, wd
->resolution
);
2631 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, wd
->y_size
, 0, 0);
2632 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
, wd
->resolution
);
2634 /* Max pressure is not given by the devices, pick one */
2635 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 50, 0, 0);
2637 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
2639 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
)
2640 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
2642 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
2644 input_mt_init_slots(input_dev
, wd
->maxcontacts
, INPUT_MT_POINTER
|
2645 INPUT_MT_DROP_UNUSED
);
2648 static void wtp_touch_event(struct hidpp_device
*hidpp
,
2649 struct hidpp_touchpad_raw_xy_finger
*touch_report
)
2651 struct wtp_data
*wd
= hidpp
->private_data
;
2654 if (!touch_report
->finger_id
|| touch_report
->contact_type
)
2655 /* no actual data */
2658 slot
= input_mt_get_slot_by_key(hidpp
->input
, touch_report
->finger_id
);
2660 input_mt_slot(hidpp
->input
, slot
);
2661 input_mt_report_slot_state(hidpp
->input
, MT_TOOL_FINGER
,
2662 touch_report
->contact_status
);
2663 if (touch_report
->contact_status
) {
2664 input_event(hidpp
->input
, EV_ABS
, ABS_MT_POSITION_X
,
2666 input_event(hidpp
->input
, EV_ABS
, ABS_MT_POSITION_Y
,
2667 wd
->flip_y
? wd
->y_size
- touch_report
->y
:
2669 input_event(hidpp
->input
, EV_ABS
, ABS_MT_PRESSURE
,
2670 touch_report
->area
);
2674 static void wtp_send_raw_xy_event(struct hidpp_device
*hidpp
,
2675 struct hidpp_touchpad_raw_xy
*raw
)
2679 for (i
= 0; i
< 2; i
++)
2680 wtp_touch_event(hidpp
, &(raw
->fingers
[i
]));
2682 if (raw
->end_of_frame
&&
2683 !(hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
))
2684 input_event(hidpp
->input
, EV_KEY
, BTN_LEFT
, raw
->button
);
2686 if (raw
->end_of_frame
|| raw
->finger_count
<= 2) {
2687 input_mt_sync_frame(hidpp
->input
);
2688 input_sync(hidpp
->input
);
2692 static int wtp_mouse_raw_xy_event(struct hidpp_device
*hidpp
, u8
*data
)
2694 struct wtp_data
*wd
= hidpp
->private_data
;
2695 u8 c1_area
= ((data
[7] & 0xf) * (data
[7] & 0xf) +
2696 (data
[7] >> 4) * (data
[7] >> 4)) / 2;
2697 u8 c2_area
= ((data
[13] & 0xf) * (data
[13] & 0xf) +
2698 (data
[13] >> 4) * (data
[13] >> 4)) / 2;
2699 struct hidpp_touchpad_raw_xy raw
= {
2700 .timestamp
= data
[1],
2704 .contact_status
= !!data
[7],
2705 .x
= get_unaligned_le16(&data
[3]),
2706 .y
= get_unaligned_le16(&data
[5]),
2709 .finger_id
= data
[2],
2712 .contact_status
= !!data
[13],
2713 .x
= get_unaligned_le16(&data
[9]),
2714 .y
= get_unaligned_le16(&data
[11]),
2717 .finger_id
= data
[8],
2720 .finger_count
= wd
->maxcontacts
,
2722 .end_of_frame
= (data
[0] >> 7) == 0,
2723 .button
= data
[0] & 0x01,
2726 wtp_send_raw_xy_event(hidpp
, &raw
);
2731 static int wtp_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
2733 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2734 struct wtp_data
*wd
= hidpp
->private_data
;
2735 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
2736 struct hidpp_touchpad_raw_xy raw
;
2738 if (!wd
|| !hidpp
->input
)
2744 hid_err(hdev
, "Received HID report of bad size (%d)",
2748 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
) {
2749 input_event(hidpp
->input
, EV_KEY
, BTN_LEFT
,
2750 !!(data
[1] & 0x01));
2751 input_event(hidpp
->input
, EV_KEY
, BTN_RIGHT
,
2752 !!(data
[1] & 0x02));
2753 input_sync(hidpp
->input
);
2758 return wtp_mouse_raw_xy_event(hidpp
, &data
[7]);
2760 case REPORT_ID_HIDPP_LONG
:
2761 /* size is already checked in hidpp_raw_event. */
2762 if ((report
->fap
.feature_index
!= wd
->mt_feature_index
) ||
2763 (report
->fap
.funcindex_clientid
!= EVENT_TOUCHPAD_RAW_XY
))
2765 hidpp_touchpad_raw_xy_event(hidpp
, data
+ 4, &raw
);
2767 wtp_send_raw_xy_event(hidpp
, &raw
);
2774 static int wtp_get_config(struct hidpp_device
*hidpp
)
2776 struct wtp_data
*wd
= hidpp
->private_data
;
2777 struct hidpp_touchpad_raw_info raw_info
= {0};
2781 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_TOUCHPAD_RAW_XY
,
2782 &wd
->mt_feature_index
, &feature_type
);
2784 /* means that the device is not powered up */
2787 ret
= hidpp_touchpad_get_raw_info(hidpp
, wd
->mt_feature_index
,
2792 wd
->x_size
= raw_info
.x_size
;
2793 wd
->y_size
= raw_info
.y_size
;
2794 wd
->maxcontacts
= raw_info
.maxcontacts
;
2795 wd
->flip_y
= raw_info
.origin
== TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT
;
2796 wd
->resolution
= raw_info
.res
;
2797 if (!wd
->resolution
)
2798 wd
->resolution
= WTP_MANUAL_RESOLUTION
;
2803 static int wtp_allocate(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2805 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2806 struct wtp_data
*wd
;
2808 wd
= devm_kzalloc(&hdev
->dev
, sizeof(struct wtp_data
),
2813 hidpp
->private_data
= wd
;
2818 static int wtp_connect(struct hid_device
*hdev
, bool connected
)
2820 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2821 struct wtp_data
*wd
= hidpp
->private_data
;
2825 ret
= wtp_get_config(hidpp
);
2827 hid_err(hdev
, "Can not get wtp config: %d\n", ret
);
2832 return hidpp_touchpad_set_raw_report_state(hidpp
, wd
->mt_feature_index
,
2836 /* ------------------------------------------------------------------------- */
2837 /* Logitech M560 devices */
2838 /* ------------------------------------------------------------------------- */
2841 * Logitech M560 protocol overview
2843 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2844 * the sides buttons are pressed, it sends some keyboard keys events
2845 * instead of buttons ones.
2846 * To complicate things further, the middle button keys sequence
2847 * is different from the odd press and the even press.
2849 * forward button -> Super_R
2850 * backward button -> Super_L+'d' (press only)
2851 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2852 * 2nd time: left-click (press only)
2853 * NB: press-only means that when the button is pressed, the
2854 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2855 * together sequentially; instead when the button is released, no event is
2859 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2860 * the mouse reacts differently:
2861 * - it never sends a keyboard key event
2862 * - for the three mouse button it sends:
2863 * middle button press 11<xx>0a 3500af00...
2864 * side 1 button (forward) press 11<xx>0a 3500b000...
2865 * side 2 button (backward) press 11<xx>0a 3500ae00...
2866 * middle/side1/side2 button release 11<xx>0a 35000000...
2869 static const u8 m560_config_parameter
[] = {0x00, 0xaf, 0x03};
2871 /* how buttons are mapped in the report */
2872 #define M560_MOUSE_BTN_LEFT 0x01
2873 #define M560_MOUSE_BTN_RIGHT 0x02
2874 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2875 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2877 #define M560_SUB_ID 0x0a
2878 #define M560_BUTTON_MODE_REGISTER 0x35
2880 static int m560_send_config_command(struct hid_device
*hdev
, bool connected
)
2882 struct hidpp_report response
;
2883 struct hidpp_device
*hidpp_dev
;
2885 hidpp_dev
= hid_get_drvdata(hdev
);
2887 return hidpp_send_rap_command_sync(
2889 REPORT_ID_HIDPP_SHORT
,
2891 M560_BUTTON_MODE_REGISTER
,
2892 (u8
*)m560_config_parameter
,
2893 sizeof(m560_config_parameter
),
2898 static int m560_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
2900 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2903 if (!hidpp
->input
) {
2904 hid_err(hdev
, "error in parameter\n");
2909 hid_err(hdev
, "error in report\n");
2913 if (data
[0] == REPORT_ID_HIDPP_LONG
&&
2914 data
[2] == M560_SUB_ID
&& data
[6] == 0x00) {
2916 * m560 mouse report for middle, forward and backward button
2919 * data[1] = device-id
2921 * data[5] = 0xaf -> middle
2924 * 0x00 -> release all
2930 input_report_key(hidpp
->input
, BTN_MIDDLE
, 1);
2933 input_report_key(hidpp
->input
, BTN_FORWARD
, 1);
2936 input_report_key(hidpp
->input
, BTN_BACK
, 1);
2939 input_report_key(hidpp
->input
, BTN_BACK
, 0);
2940 input_report_key(hidpp
->input
, BTN_FORWARD
, 0);
2941 input_report_key(hidpp
->input
, BTN_MIDDLE
, 0);
2944 hid_err(hdev
, "error in report\n");
2947 input_sync(hidpp
->input
);
2949 } else if (data
[0] == 0x02) {
2951 * Logitech M560 mouse report
2953 * data[0] = type (0x02)
2954 * data[1..2] = buttons
2961 input_report_key(hidpp
->input
, BTN_LEFT
,
2962 !!(data
[1] & M560_MOUSE_BTN_LEFT
));
2963 input_report_key(hidpp
->input
, BTN_RIGHT
,
2964 !!(data
[1] & M560_MOUSE_BTN_RIGHT
));
2966 if (data
[1] & M560_MOUSE_BTN_WHEEL_LEFT
) {
2967 input_report_rel(hidpp
->input
, REL_HWHEEL
, -1);
2968 input_report_rel(hidpp
->input
, REL_HWHEEL_HI_RES
,
2970 } else if (data
[1] & M560_MOUSE_BTN_WHEEL_RIGHT
) {
2971 input_report_rel(hidpp
->input
, REL_HWHEEL
, 1);
2972 input_report_rel(hidpp
->input
, REL_HWHEEL_HI_RES
,
2976 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 0, 12), 12);
2977 input_report_rel(hidpp
->input
, REL_X
, v
);
2979 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 12, 12), 12);
2980 input_report_rel(hidpp
->input
, REL_Y
, v
);
2982 v
= hid_snto32(data
[6], 8);
2984 hidpp_scroll_counter_handle_scroll(hidpp
->input
,
2985 &hidpp
->vertical_wheel_counter
, v
);
2987 input_sync(hidpp
->input
);
2993 static void m560_populate_input(struct hidpp_device
*hidpp
,
2994 struct input_dev
*input_dev
)
2996 __set_bit(EV_KEY
, input_dev
->evbit
);
2997 __set_bit(BTN_MIDDLE
, input_dev
->keybit
);
2998 __set_bit(BTN_RIGHT
, input_dev
->keybit
);
2999 __set_bit(BTN_LEFT
, input_dev
->keybit
);
3000 __set_bit(BTN_BACK
, input_dev
->keybit
);
3001 __set_bit(BTN_FORWARD
, input_dev
->keybit
);
3003 __set_bit(EV_REL
, input_dev
->evbit
);
3004 __set_bit(REL_X
, input_dev
->relbit
);
3005 __set_bit(REL_Y
, input_dev
->relbit
);
3006 __set_bit(REL_WHEEL
, input_dev
->relbit
);
3007 __set_bit(REL_HWHEEL
, input_dev
->relbit
);
3008 __set_bit(REL_WHEEL_HI_RES
, input_dev
->relbit
);
3009 __set_bit(REL_HWHEEL_HI_RES
, input_dev
->relbit
);
3012 static int m560_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
3013 struct hid_field
*field
, struct hid_usage
*usage
,
3014 unsigned long **bit
, int *max
)
3019 /* ------------------------------------------------------------------------- */
3020 /* Logitech K400 devices */
3021 /* ------------------------------------------------------------------------- */
3024 * The Logitech K400 keyboard has an embedded touchpad which is seen
3025 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3026 * tap-to-click but the setting is not remembered accross reset, annoying some
3029 * We can toggle this feature from the host by using the feature 0x6010:
3033 struct k400_private_data
{
3037 static int k400_disable_tap_to_click(struct hidpp_device
*hidpp
)
3039 struct k400_private_data
*k400
= hidpp
->private_data
;
3040 struct hidpp_touchpad_fw_items items
= {};
3044 if (!k400
->feature_index
) {
3045 ret
= hidpp_root_get_feature(hidpp
,
3046 HIDPP_PAGE_TOUCHPAD_FW_ITEMS
,
3047 &k400
->feature_index
, &feature_type
);
3049 /* means that the device is not powered up */
3053 ret
= hidpp_touchpad_fw_items_set(hidpp
, k400
->feature_index
, &items
);
3060 static int k400_allocate(struct hid_device
*hdev
)
3062 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3063 struct k400_private_data
*k400
;
3065 k400
= devm_kzalloc(&hdev
->dev
, sizeof(struct k400_private_data
),
3070 hidpp
->private_data
= k400
;
3075 static int k400_connect(struct hid_device
*hdev
, bool connected
)
3077 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3079 if (!disable_tap_to_click
)
3082 return k400_disable_tap_to_click(hidpp
);
3085 /* ------------------------------------------------------------------------- */
3086 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
3087 /* ------------------------------------------------------------------------- */
3089 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3091 static int g920_ff_set_autocenter(struct hidpp_device
*hidpp
,
3092 struct hidpp_ff_private_data
*data
)
3094 struct hidpp_report response
;
3095 u8 params
[HIDPP_AUTOCENTER_PARAMS_LENGTH
] = {
3096 [1] = HIDPP_FF_EFFECT_SPRING
| HIDPP_FF_EFFECT_AUTOSTART
,
3100 /* initialize with zero autocenter to get wheel in usable state */
3102 dbg_hid("Setting autocenter to 0.\n");
3103 ret
= hidpp_send_fap_command_sync(hidpp
, data
->feature_index
,
3104 HIDPP_FF_DOWNLOAD_EFFECT
,
3105 params
, ARRAY_SIZE(params
),
3108 hid_warn(hidpp
->hid_dev
, "Failed to autocenter device!\n");
3110 data
->slot_autocenter
= response
.fap
.params
[0];
3115 static int g920_get_config(struct hidpp_device
*hidpp
,
3116 struct hidpp_ff_private_data
*data
)
3118 struct hidpp_report response
;
3122 memset(data
, 0, sizeof(*data
));
3124 /* Find feature and store for later use */
3125 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_G920_FORCE_FEEDBACK
,
3126 &data
->feature_index
, &feature_type
);
3130 /* Read number of slots available in device */
3131 ret
= hidpp_send_fap_command_sync(hidpp
, data
->feature_index
,
3138 hid_err(hidpp
->hid_dev
,
3139 "%s: received protocol error 0x%02x\n", __func__
, ret
);
3143 data
->num_effects
= response
.fap
.params
[0] - HIDPP_FF_RESERVED_SLOTS
;
3145 /* reset all forces */
3146 ret
= hidpp_send_fap_command_sync(hidpp
, data
->feature_index
,
3151 hid_warn(hidpp
->hid_dev
, "Failed to reset all forces!\n");
3153 ret
= hidpp_send_fap_command_sync(hidpp
, data
->feature_index
,
3154 HIDPP_FF_GET_APERTURE
,
3158 hid_warn(hidpp
->hid_dev
,
3159 "Failed to read range from device!\n");
3162 900 : get_unaligned_be16(&response
.fap
.params
[0]);
3164 /* Read the current gain values */
3165 ret
= hidpp_send_fap_command_sync(hidpp
, data
->feature_index
,
3166 HIDPP_FF_GET_GLOBAL_GAINS
,
3170 hid_warn(hidpp
->hid_dev
,
3171 "Failed to read gain values from device!\n");
3173 0xffff : get_unaligned_be16(&response
.fap
.params
[0]);
3175 /* ignore boost value at response.fap.params[2] */
3177 return g920_ff_set_autocenter(hidpp
, data
);
3180 /* -------------------------------------------------------------------------- */
3181 /* Logitech Dinovo Mini keyboard with builtin touchpad */
3182 /* -------------------------------------------------------------------------- */
3183 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3185 static int lg_dinovo_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
3186 struct hid_field
*field
, struct hid_usage
*usage
,
3187 unsigned long **bit
, int *max
)
3189 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_LOGIVENDOR
)
3192 switch (usage
->hid
& HID_USAGE
) {
3193 case 0x00d: lg_map_key_clear(KEY_MEDIA
); break;
3200 /* -------------------------------------------------------------------------- */
3201 /* HID++1.0 devices which use HID++ reports for their wheels */
3202 /* -------------------------------------------------------------------------- */
3203 static int hidpp10_wheel_connect(struct hidpp_device
*hidpp
)
3205 return hidpp10_set_register(hidpp
, HIDPP_REG_ENABLE_REPORTS
, 0,
3206 HIDPP_ENABLE_WHEEL_REPORT
| HIDPP_ENABLE_HWHEEL_REPORT
,
3207 HIDPP_ENABLE_WHEEL_REPORT
| HIDPP_ENABLE_HWHEEL_REPORT
);
3210 static int hidpp10_wheel_raw_event(struct hidpp_device
*hidpp
,
3221 if (data
[0] != REPORT_ID_HIDPP_SHORT
|| data
[2] != HIDPP_SUB_ID_ROLLER
)
3227 input_report_rel(hidpp
->input
, REL_WHEEL
, value
);
3228 input_report_rel(hidpp
->input
, REL_WHEEL_HI_RES
, value
* 120);
3229 input_report_rel(hidpp
->input
, REL_HWHEEL
, hvalue
);
3230 input_report_rel(hidpp
->input
, REL_HWHEEL_HI_RES
, hvalue
* 120);
3231 input_sync(hidpp
->input
);
3236 static void hidpp10_wheel_populate_input(struct hidpp_device
*hidpp
,
3237 struct input_dev
*input_dev
)
3239 __set_bit(EV_REL
, input_dev
->evbit
);
3240 __set_bit(REL_WHEEL
, input_dev
->relbit
);
3241 __set_bit(REL_WHEEL_HI_RES
, input_dev
->relbit
);
3242 __set_bit(REL_HWHEEL
, input_dev
->relbit
);
3243 __set_bit(REL_HWHEEL_HI_RES
, input_dev
->relbit
);
3246 /* -------------------------------------------------------------------------- */
3247 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3248 /* -------------------------------------------------------------------------- */
3249 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device
*hidpp
)
3251 return hidpp10_set_register(hidpp
, HIDPP_REG_ENABLE_REPORTS
, 0,
3252 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT
,
3253 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT
);
3256 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device
*hidpp
,
3267 if (data
[0] != REPORT_ID_HIDPP_SHORT
||
3268 data
[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS
)
3272 * Buttons are either delivered through the regular mouse report *or*
3273 * through the extra buttons report. At least for button 6 how it is
3274 * delivered differs per receiver firmware version. Even receivers with
3275 * the same usb-id show different behavior, so we handle both cases.
3277 for (i
= 0; i
< 8; i
++)
3278 input_report_key(hidpp
->input
, BTN_MOUSE
+ i
,
3279 (data
[3] & (1 << i
)));
3281 /* Some mice report events on button 9+, use BTN_MISC */
3282 for (i
= 0; i
< 8; i
++)
3283 input_report_key(hidpp
->input
, BTN_MISC
+ i
,
3284 (data
[4] & (1 << i
)));
3286 input_sync(hidpp
->input
);
3290 static void hidpp10_extra_mouse_buttons_populate_input(
3291 struct hidpp_device
*hidpp
, struct input_dev
*input_dev
)
3293 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3294 __set_bit(BTN_0
, input_dev
->keybit
);
3295 __set_bit(BTN_1
, input_dev
->keybit
);
3296 __set_bit(BTN_2
, input_dev
->keybit
);
3297 __set_bit(BTN_3
, input_dev
->keybit
);
3298 __set_bit(BTN_4
, input_dev
->keybit
);
3299 __set_bit(BTN_5
, input_dev
->keybit
);
3300 __set_bit(BTN_6
, input_dev
->keybit
);
3301 __set_bit(BTN_7
, input_dev
->keybit
);
3304 /* -------------------------------------------------------------------------- */
3305 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3306 /* -------------------------------------------------------------------------- */
3308 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3309 static u8
*hidpp10_consumer_keys_report_fixup(struct hidpp_device
*hidpp
,
3310 u8
*_rdesc
, unsigned int *rsize
)
3312 /* Note 0 terminated so we can use strnstr to search for this. */
3313 static const char consumer_rdesc_start
[] = {
3314 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3315 0x09, 0x01, /* USAGE (Consumer Control) */
3316 0xA1, 0x01, /* COLLECTION (Application) */
3317 0x85, 0x03, /* REPORT_ID = 3 */
3318 0x75, 0x10, /* REPORT_SIZE (16) */
3319 0x95, 0x02, /* REPORT_COUNT (2) */
3320 0x15, 0x01, /* LOGICAL_MIN (1) */
3321 0x26, 0x00 /* LOGICAL_MAX (... */
3323 char *consumer_rdesc
, *rdesc
= (char *)_rdesc
;
3326 consumer_rdesc
= strnstr(rdesc
, consumer_rdesc_start
, *rsize
);
3327 size
= *rsize
- (consumer_rdesc
- rdesc
);
3328 if (consumer_rdesc
&& size
>= 25) {
3329 consumer_rdesc
[15] = 0x7f;
3330 consumer_rdesc
[16] = 0x10;
3331 consumer_rdesc
[20] = 0x7f;
3332 consumer_rdesc
[21] = 0x10;
3337 static int hidpp10_consumer_keys_connect(struct hidpp_device
*hidpp
)
3339 return hidpp10_set_register(hidpp
, HIDPP_REG_ENABLE_REPORTS
, 0,
3340 HIDPP_ENABLE_CONSUMER_REPORT
,
3341 HIDPP_ENABLE_CONSUMER_REPORT
);
3344 static int hidpp10_consumer_keys_raw_event(struct hidpp_device
*hidpp
,
3347 u8 consumer_report
[5];
3352 if (data
[0] != REPORT_ID_HIDPP_SHORT
||
3353 data
[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS
)
3357 * Build a normal consumer report (3) out of the data, this detour
3358 * is necessary to get some keyboards to report their 0x10xx usages.
3360 consumer_report
[0] = 0x03;
3361 memcpy(&consumer_report
[1], &data
[3], 4);
3362 /* We are called from atomic context */
3363 hid_report_raw_event(hidpp
->hid_dev
, HID_INPUT_REPORT
,
3364 consumer_report
, 5, 1);
3369 /* -------------------------------------------------------------------------- */
3370 /* High-resolution scroll wheels */
3371 /* -------------------------------------------------------------------------- */
3373 static int hi_res_scroll_enable(struct hidpp_device
*hidpp
)
3378 if (hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL_X2121
) {
3379 ret
= hidpp_hrw_set_wheel_mode(hidpp
, false, true, false);
3381 ret
= hidpp_hrw_get_wheel_capability(hidpp
, &multiplier
);
3382 } else if (hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL_X2120
) {
3383 ret
= hidpp_hrs_set_highres_scrolling_mode(hidpp
, true,
3385 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
3386 ret
= hidpp10_enable_scrolling_acceleration(hidpp
);
3392 if (multiplier
== 0)
3395 hidpp
->vertical_wheel_counter
.wheel_multiplier
= multiplier
;
3396 hid_dbg(hidpp
->hid_dev
, "wheel multiplier = %d\n", multiplier
);
3400 /* -------------------------------------------------------------------------- */
3401 /* Generic HID++ devices */
3402 /* -------------------------------------------------------------------------- */
3404 static u8
*hidpp_report_fixup(struct hid_device
*hdev
, u8
*rdesc
,
3405 unsigned int *rsize
)
3407 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3412 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3413 if (hdev
->group
== HID_GROUP_LOGITECH_27MHZ_DEVICE
||
3414 (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
))
3415 rdesc
= hidpp10_consumer_keys_report_fixup(hidpp
, rdesc
, rsize
);
3420 static int hidpp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
3421 struct hid_field
*field
, struct hid_usage
*usage
,
3422 unsigned long **bit
, int *max
)
3424 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3429 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
3430 return wtp_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
3431 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
&&
3432 field
->application
!= HID_GD_MOUSE
)
3433 return m560_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
3435 if (hdev
->product
== DINOVO_MINI_PRODUCT_ID
)
3436 return lg_dinovo_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
3441 static int hidpp_input_mapped(struct hid_device
*hdev
, struct hid_input
*hi
,
3442 struct hid_field
*field
, struct hid_usage
*usage
,
3443 unsigned long **bit
, int *max
)
3445 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3450 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3451 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
3452 if (usage
->type
== EV_ABS
&& (usage
->code
== ABS_X
||
3453 usage
->code
== ABS_Y
|| usage
->code
== ABS_Z
||
3454 usage
->code
== ABS_RZ
)) {
3455 field
->application
= HID_GD_MULTIAXIS
;
3463 static void hidpp_populate_input(struct hidpp_device
*hidpp
,
3464 struct input_dev
*input
)
3466 hidpp
->input
= input
;
3468 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
3469 wtp_populate_input(hidpp
, input
);
3470 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
3471 m560_populate_input(hidpp
, input
);
3473 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_WHEELS
)
3474 hidpp10_wheel_populate_input(hidpp
, input
);
3476 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS
)
3477 hidpp10_extra_mouse_buttons_populate_input(hidpp
, input
);
3480 static int hidpp_input_configured(struct hid_device
*hdev
,
3481 struct hid_input
*hidinput
)
3483 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3484 struct input_dev
*input
= hidinput
->input
;
3489 hidpp_populate_input(hidpp
, input
);
3494 static int hidpp_raw_hidpp_event(struct hidpp_device
*hidpp
, u8
*data
,
3497 struct hidpp_report
*question
= hidpp
->send_receive_buf
;
3498 struct hidpp_report
*answer
= hidpp
->send_receive_buf
;
3499 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
3503 * If the mutex is locked then we have a pending answer from a
3504 * previously sent command.
3506 if (unlikely(mutex_is_locked(&hidpp
->send_mutex
))) {
3508 * Check for a correct hidpp20 answer or the corresponding
3511 if (hidpp_match_answer(question
, report
) ||
3512 hidpp_match_error(question
, report
)) {
3514 hidpp
->answer_available
= true;
3515 wake_up(&hidpp
->wait
);
3517 * This was an answer to a command that this driver sent
3518 * We return 1 to hid-core to avoid forwarding the
3519 * command upstream as it has been treated by the driver
3526 if (unlikely(hidpp_report_is_connect_event(hidpp
, report
))) {
3527 atomic_set(&hidpp
->connected
,
3528 !(report
->rap
.params
[0] & (1 << 6)));
3529 if (schedule_work(&hidpp
->work
) == 0)
3530 dbg_hid("%s: connect event already queued\n", __func__
);
3534 if (hidpp
->hid_dev
->group
== HID_GROUP_LOGITECH_27MHZ_DEVICE
&&
3535 data
[0] == REPORT_ID_HIDPP_SHORT
&&
3536 data
[2] == HIDPP_SUB_ID_USER_IFACE_EVENT
&&
3537 (data
[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST
)) {
3538 dev_err_ratelimited(&hidpp
->hid_dev
->dev
,
3539 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3540 dev_err_ratelimited(&hidpp
->hid_dev
->dev
,
3541 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3544 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP20_BATTERY
) {
3545 ret
= hidpp20_battery_event_1000(hidpp
, data
, size
);
3548 ret
= hidpp20_battery_event_1004(hidpp
, data
, size
);
3551 ret
= hidpp_solar_battery_event(hidpp
, data
, size
);
3554 ret
= hidpp20_battery_voltage_event(hidpp
, data
, size
);
3559 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP10_BATTERY
) {
3560 ret
= hidpp10_battery_event(hidpp
, data
, size
);
3565 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_WHEELS
) {
3566 ret
= hidpp10_wheel_raw_event(hidpp
, data
, size
);
3571 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS
) {
3572 ret
= hidpp10_extra_mouse_buttons_raw_event(hidpp
, data
, size
);
3577 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
) {
3578 ret
= hidpp10_consumer_keys_raw_event(hidpp
, data
, size
);
3586 static int hidpp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
3589 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3595 /* Generic HID++ processing. */
3597 case REPORT_ID_HIDPP_VERY_LONG
:
3598 if (size
!= hidpp
->very_long_report_length
) {
3599 hid_err(hdev
, "received hid++ report of bad size (%d)",
3603 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
3605 case REPORT_ID_HIDPP_LONG
:
3606 if (size
!= HIDPP_REPORT_LONG_LENGTH
) {
3607 hid_err(hdev
, "received hid++ report of bad size (%d)",
3611 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
3613 case REPORT_ID_HIDPP_SHORT
:
3614 if (size
!= HIDPP_REPORT_SHORT_LENGTH
) {
3615 hid_err(hdev
, "received hid++ report of bad size (%d)",
3619 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
3623 /* If no report is available for further processing, skip calling
3624 * raw_event of subclasses. */
3628 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
3629 return wtp_raw_event(hdev
, data
, size
);
3630 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
3631 return m560_raw_event(hdev
, data
, size
);
3636 static int hidpp_event(struct hid_device
*hdev
, struct hid_field
*field
,
3637 struct hid_usage
*usage
, __s32 value
)
3639 /* This function will only be called for scroll events, due to the
3640 * restriction imposed in hidpp_usages.
3642 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3643 struct hidpp_scroll_counter
*counter
;
3648 counter
= &hidpp
->vertical_wheel_counter
;
3649 /* A scroll event may occur before the multiplier has been retrieved or
3650 * the input device set, or high-res scroll enabling may fail. In such
3651 * cases we must return early (falling back to default behaviour) to
3652 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3654 if (!(hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL
) || value
== 0
3655 || hidpp
->input
== NULL
|| counter
->wheel_multiplier
== 0)
3658 hidpp_scroll_counter_handle_scroll(hidpp
->input
, counter
, value
);
3662 static int hidpp_initialize_battery(struct hidpp_device
*hidpp
)
3664 static atomic_t battery_no
= ATOMIC_INIT(0);
3665 struct power_supply_config cfg
= { .drv_data
= hidpp
};
3666 struct power_supply_desc
*desc
= &hidpp
->battery
.desc
;
3667 enum power_supply_property
*battery_props
;
3668 struct hidpp_battery
*battery
;
3669 unsigned int num_battery_props
;
3673 if (hidpp
->battery
.ps
)
3676 hidpp
->battery
.feature_index
= 0xff;
3677 hidpp
->battery
.solar_feature_index
= 0xff;
3678 hidpp
->battery
.voltage_feature_index
= 0xff;
3680 if (hidpp
->protocol_major
>= 2) {
3681 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K750
)
3682 ret
= hidpp_solar_request_battery_event(hidpp
);
3684 /* we only support one battery feature right now, so let's
3685 first check the ones that support battery level first
3686 and leave voltage for last */
3687 ret
= hidpp20_query_battery_info_1000(hidpp
);
3689 ret
= hidpp20_query_battery_info_1004(hidpp
);
3691 ret
= hidpp20_query_battery_voltage_info(hidpp
);
3696 hidpp
->capabilities
|= HIDPP_CAPABILITY_HIDPP20_BATTERY
;
3698 ret
= hidpp10_query_battery_status(hidpp
);
3700 ret
= hidpp10_query_battery_mileage(hidpp
);
3703 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
3705 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
;
3707 hidpp
->capabilities
|= HIDPP_CAPABILITY_HIDPP10_BATTERY
;
3710 battery_props
= devm_kmemdup(&hidpp
->hid_dev
->dev
,
3711 hidpp_battery_props
,
3712 sizeof(hidpp_battery_props
),
3717 num_battery_props
= ARRAY_SIZE(hidpp_battery_props
) - 3;
3719 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_MILEAGE
||
3720 hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_PERCENTAGE
)
3721 battery_props
[num_battery_props
++] =
3722 POWER_SUPPLY_PROP_CAPACITY
;
3724 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
)
3725 battery_props
[num_battery_props
++] =
3726 POWER_SUPPLY_PROP_CAPACITY_LEVEL
;
3728 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_VOLTAGE
)
3729 battery_props
[num_battery_props
++] =
3730 POWER_SUPPLY_PROP_VOLTAGE_NOW
;
3732 battery
= &hidpp
->battery
;
3734 n
= atomic_inc_return(&battery_no
) - 1;
3735 desc
->properties
= battery_props
;
3736 desc
->num_properties
= num_battery_props
;
3737 desc
->get_property
= hidpp_battery_get_property
;
3738 sprintf(battery
->name
, "hidpp_battery_%ld", n
);
3739 desc
->name
= battery
->name
;
3740 desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
3741 desc
->use_for_apm
= 0;
3743 battery
->ps
= devm_power_supply_register(&hidpp
->hid_dev
->dev
,
3746 if (IS_ERR(battery
->ps
))
3747 return PTR_ERR(battery
->ps
);
3749 power_supply_powers(battery
->ps
, &hidpp
->hid_dev
->dev
);
3754 static void hidpp_overwrite_name(struct hid_device
*hdev
)
3756 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3759 if (hidpp
->protocol_major
< 2)
3762 name
= hidpp_get_device_name(hidpp
);
3765 hid_err(hdev
, "unable to retrieve the name of the device");
3767 dbg_hid("HID++: Got name: %s\n", name
);
3768 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
3774 static int hidpp_input_open(struct input_dev
*dev
)
3776 struct hid_device
*hid
= input_get_drvdata(dev
);
3778 return hid_hw_open(hid
);
3781 static void hidpp_input_close(struct input_dev
*dev
)
3783 struct hid_device
*hid
= input_get_drvdata(dev
);
3788 static struct input_dev
*hidpp_allocate_input(struct hid_device
*hdev
)
3790 struct input_dev
*input_dev
= devm_input_allocate_device(&hdev
->dev
);
3791 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3796 input_set_drvdata(input_dev
, hdev
);
3797 input_dev
->open
= hidpp_input_open
;
3798 input_dev
->close
= hidpp_input_close
;
3800 input_dev
->name
= hidpp
->name
;
3801 input_dev
->phys
= hdev
->phys
;
3802 input_dev
->uniq
= hdev
->uniq
;
3803 input_dev
->id
.bustype
= hdev
->bus
;
3804 input_dev
->id
.vendor
= hdev
->vendor
;
3805 input_dev
->id
.product
= hdev
->product
;
3806 input_dev
->id
.version
= hdev
->version
;
3807 input_dev
->dev
.parent
= &hdev
->dev
;
3812 static void hidpp_connect_event(struct hidpp_device
*hidpp
)
3814 struct hid_device
*hdev
= hidpp
->hid_dev
;
3816 bool connected
= atomic_read(&hidpp
->connected
);
3817 struct input_dev
*input
;
3818 char *name
, *devm_name
;
3821 if (hidpp
->battery
.ps
) {
3822 hidpp
->battery
.online
= false;
3823 hidpp
->battery
.status
= POWER_SUPPLY_STATUS_UNKNOWN
;
3824 hidpp
->battery
.level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
3825 power_supply_changed(hidpp
->battery
.ps
);
3830 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
3831 ret
= wtp_connect(hdev
, connected
);
3834 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
3835 ret
= m560_send_config_command(hdev
, connected
);
3838 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
3839 ret
= k400_connect(hdev
, connected
);
3844 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_WHEELS
) {
3845 ret
= hidpp10_wheel_connect(hidpp
);
3850 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS
) {
3851 ret
= hidpp10_extra_mouse_buttons_connect(hidpp
);
3856 if (hidpp
->quirks
& HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
) {
3857 ret
= hidpp10_consumer_keys_connect(hidpp
);
3862 /* the device is already connected, we can ask for its name and
3864 if (!hidpp
->protocol_major
) {
3865 ret
= hidpp_root_get_protocol_version(hidpp
);
3867 hid_err(hdev
, "Can not get the protocol version.\n");
3872 if (hidpp
->name
== hdev
->name
&& hidpp
->protocol_major
>= 2) {
3873 name
= hidpp_get_device_name(hidpp
);
3875 devm_name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
,
3881 hidpp
->name
= devm_name
;
3885 hidpp_initialize_battery(hidpp
);
3887 /* forward current battery state */
3888 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP10_BATTERY
) {
3889 hidpp10_enable_battery_reporting(hidpp
);
3890 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_MILEAGE
)
3891 hidpp10_query_battery_mileage(hidpp
);
3893 hidpp10_query_battery_status(hidpp
);
3894 } else if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP20_BATTERY
) {
3895 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_VOLTAGE
)
3896 hidpp20_query_battery_voltage_info(hidpp
);
3897 else if (hidpp
->capabilities
& HIDPP_CAPABILITY_UNIFIED_BATTERY
)
3898 hidpp20_query_battery_info_1004(hidpp
);
3900 hidpp20_query_battery_info_1000(hidpp
);
3902 if (hidpp
->battery
.ps
)
3903 power_supply_changed(hidpp
->battery
.ps
);
3905 if (hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL
)
3906 hi_res_scroll_enable(hidpp
);
3908 if (!(hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
) || hidpp
->delayed_input
)
3909 /* if the input nodes are already created, we can stop now */
3912 input
= hidpp_allocate_input(hdev
);
3914 hid_err(hdev
, "cannot allocate new input device: %d\n", ret
);
3918 hidpp_populate_input(hidpp
, input
);
3920 ret
= input_register_device(input
);
3922 input_free_device(input
);
3924 hidpp
->delayed_input
= input
;
3927 static DEVICE_ATTR(builtin_power_supply
, 0000, NULL
, NULL
);
3929 static struct attribute
*sysfs_attrs
[] = {
3930 &dev_attr_builtin_power_supply
.attr
,
3934 static const struct attribute_group ps_attribute_group
= {
3935 .attrs
= sysfs_attrs
3938 static int hidpp_get_report_length(struct hid_device
*hdev
, int id
)
3940 struct hid_report_enum
*re
;
3941 struct hid_report
*report
;
3943 re
= &(hdev
->report_enum
[HID_OUTPUT_REPORT
]);
3944 report
= re
->report_id_hash
[id
];
3948 return report
->field
[0]->report_count
+ 1;
3951 static u8
hidpp_validate_device(struct hid_device
*hdev
)
3953 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3954 int id
, report_length
;
3955 u8 supported_reports
= 0;
3957 id
= REPORT_ID_HIDPP_SHORT
;
3958 report_length
= hidpp_get_report_length(hdev
, id
);
3959 if (report_length
) {
3960 if (report_length
< HIDPP_REPORT_SHORT_LENGTH
)
3963 supported_reports
|= HIDPP_REPORT_SHORT_SUPPORTED
;
3966 id
= REPORT_ID_HIDPP_LONG
;
3967 report_length
= hidpp_get_report_length(hdev
, id
);
3968 if (report_length
) {
3969 if (report_length
< HIDPP_REPORT_LONG_LENGTH
)
3972 supported_reports
|= HIDPP_REPORT_LONG_SUPPORTED
;
3975 id
= REPORT_ID_HIDPP_VERY_LONG
;
3976 report_length
= hidpp_get_report_length(hdev
, id
);
3977 if (report_length
) {
3978 if (report_length
< HIDPP_REPORT_LONG_LENGTH
||
3979 report_length
> HIDPP_REPORT_VERY_LONG_MAX_LENGTH
)
3982 supported_reports
|= HIDPP_REPORT_VERY_LONG_SUPPORTED
;
3983 hidpp
->very_long_report_length
= report_length
;
3986 return supported_reports
;
3989 hid_warn(hdev
, "not enough values in hidpp report %d\n", id
);
3993 static bool hidpp_application_equals(struct hid_device
*hdev
,
3994 unsigned int application
)
3996 struct list_head
*report_list
;
3997 struct hid_report
*report
;
3999 report_list
= &hdev
->report_enum
[HID_INPUT_REPORT
].report_list
;
4000 report
= list_first_entry_or_null(report_list
, struct hid_report
, list
);
4001 return report
&& report
->application
== application
;
4004 static int hidpp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
4006 struct hidpp_device
*hidpp
;
4009 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
4010 struct hidpp_ff_private_data data
;
4012 /* report_fixup needs drvdata to be set before we call hid_parse */
4013 hidpp
= devm_kzalloc(&hdev
->dev
, sizeof(*hidpp
), GFP_KERNEL
);
4017 hidpp
->hid_dev
= hdev
;
4018 hidpp
->name
= hdev
->name
;
4019 hidpp
->quirks
= id
->driver_data
;
4020 hid_set_drvdata(hdev
, hidpp
);
4022 ret
= hid_parse(hdev
);
4024 hid_err(hdev
, "%s:parse failed\n", __func__
);
4029 * Make sure the device is HID++ capable, otherwise treat as generic HID
4031 hidpp
->supported_reports
= hidpp_validate_device(hdev
);
4033 if (!hidpp
->supported_reports
) {
4034 hid_set_drvdata(hdev
, NULL
);
4035 devm_kfree(&hdev
->dev
, hidpp
);
4036 return hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
4039 if (id
->group
== HID_GROUP_LOGITECH_DJ_DEVICE
)
4040 hidpp
->quirks
|= HIDPP_QUIRK_UNIFYING
;
4042 if (id
->group
== HID_GROUP_LOGITECH_27MHZ_DEVICE
&&
4043 hidpp_application_equals(hdev
, HID_GD_MOUSE
))
4044 hidpp
->quirks
|= HIDPP_QUIRK_HIDPP_WHEELS
|
4045 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS
;
4047 if (id
->group
== HID_GROUP_LOGITECH_27MHZ_DEVICE
&&
4048 hidpp_application_equals(hdev
, HID_GD_KEYBOARD
))
4049 hidpp
->quirks
|= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
;
4051 if (disable_raw_mode
) {
4052 hidpp
->quirks
&= ~HIDPP_QUIRK_CLASS_WTP
;
4053 hidpp
->quirks
&= ~HIDPP_QUIRK_NO_HIDINPUT
;
4056 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
4057 ret
= wtp_allocate(hdev
, id
);
4060 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
4061 ret
= k400_allocate(hdev
);
4066 INIT_WORK(&hidpp
->work
, delayed_work_cb
);
4067 mutex_init(&hidpp
->send_mutex
);
4068 init_waitqueue_head(&hidpp
->wait
);
4070 /* indicates we are handling the battery properties in the kernel */
4071 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
4073 hid_warn(hdev
, "Cannot allocate sysfs group for %s\n",
4077 * Plain USB connections need to actually call start and open
4078 * on the transport driver to allow incoming data.
4080 ret
= hid_hw_start(hdev
, 0);
4082 hid_err(hdev
, "hw start failed\n");
4083 goto hid_hw_start_fail
;
4086 ret
= hid_hw_open(hdev
);
4088 dev_err(&hdev
->dev
, "%s:hid_hw_open returned error:%d\n",
4090 goto hid_hw_open_fail
;
4093 /* Allow incoming packets */
4094 hid_device_io_start(hdev
);
4096 if (hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)
4097 hidpp_unifying_init(hidpp
);
4099 connected
= hidpp_root_get_protocol_version(hidpp
) == 0;
4100 atomic_set(&hidpp
->connected
, connected
);
4101 if (!(hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)) {
4104 hid_err(hdev
, "Device not connected");
4105 goto hid_hw_init_fail
;
4108 hidpp_overwrite_name(hdev
);
4111 if (connected
&& hidpp
->protocol_major
>= 2) {
4112 ret
= hidpp_set_wireless_feature_index(hidpp
);
4114 hidpp
->wireless_feature_index
= 0;
4116 goto hid_hw_init_fail
;
4119 if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)) {
4120 ret
= wtp_get_config(hidpp
);
4122 goto hid_hw_init_fail
;
4123 } else if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
)) {
4124 ret
= g920_get_config(hidpp
, &data
);
4126 goto hid_hw_init_fail
;
4129 hidpp_connect_event(hidpp
);
4131 /* Reset the HID node state */
4132 hid_device_io_stop(hdev
);
4136 if (hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
)
4137 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
4139 /* Now export the actual inputs and hidraw nodes to the world */
4140 ret
= hid_hw_start(hdev
, connect_mask
);
4142 hid_err(hdev
, "%s:hid_hw_start returned error\n", __func__
);
4143 goto hid_hw_start_fail
;
4146 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
4147 ret
= hidpp_ff_init(hidpp
, &data
);
4149 hid_warn(hidpp
->hid_dev
,
4150 "Unable to initialize force feedback support, errno %d\n",
4161 sysfs_remove_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
4162 cancel_work_sync(&hidpp
->work
);
4163 mutex_destroy(&hidpp
->send_mutex
);
4167 static void hidpp_remove(struct hid_device
*hdev
)
4169 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
4172 return hid_hw_stop(hdev
);
4174 sysfs_remove_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
4177 cancel_work_sync(&hidpp
->work
);
4178 mutex_destroy(&hidpp
->send_mutex
);
4181 #define LDJ_DEVICE(product) \
4182 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4183 USB_VENDOR_ID_LOGITECH, (product))
4185 #define L27MHZ_DEVICE(product) \
4186 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4187 USB_VENDOR_ID_LOGITECH, (product))
4189 static const struct hid_device_id hidpp_devices
[] = {
4190 { /* wireless touchpad */
4192 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
|
4193 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
},
4194 { /* wireless touchpad T650 */
4196 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
},
4197 { /* wireless touchpad T651 */
4198 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
,
4199 USB_DEVICE_ID_LOGITECH_T651
),
4200 .driver_data
= HIDPP_QUIRK_CLASS_WTP
},
4201 { /* Mouse Logitech Anywhere MX */
4202 LDJ_DEVICE(0x1017), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_1P0
},
4203 { /* Mouse Logitech Cube */
4204 LDJ_DEVICE(0x4010), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2120
},
4205 { /* Mouse Logitech M335 */
4206 LDJ_DEVICE(0x4050), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4207 { /* Mouse Logitech M515 */
4208 LDJ_DEVICE(0x4007), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2120
},
4209 { /* Mouse logitech M560 */
4211 .driver_data
= HIDPP_QUIRK_DELAYED_INIT
| HIDPP_QUIRK_CLASS_M560
4212 | HIDPP_QUIRK_HI_RES_SCROLL_X2120
},
4213 { /* Mouse Logitech M705 (firmware RQM17) */
4214 LDJ_DEVICE(0x101b), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_1P0
},
4215 { /* Mouse Logitech M705 (firmware RQM67) */
4216 LDJ_DEVICE(0x406d), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4217 { /* Mouse Logitech M720 */
4218 LDJ_DEVICE(0x405e), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4219 { /* Mouse Logitech MX Anywhere 2 */
4220 LDJ_DEVICE(0x404a), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4221 { LDJ_DEVICE(0x4072), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4222 { LDJ_DEVICE(0xb013), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4223 { LDJ_DEVICE(0xb018), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4224 { LDJ_DEVICE(0xb01f), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4225 { /* Mouse Logitech MX Anywhere 2S */
4226 LDJ_DEVICE(0x406a), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4227 { /* Mouse Logitech MX Master */
4228 LDJ_DEVICE(0x4041), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4229 { LDJ_DEVICE(0x4060), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4230 { LDJ_DEVICE(0x4071), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4231 { /* Mouse Logitech MX Master 2S */
4232 LDJ_DEVICE(0x4069), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4233 { /* Mouse Logitech MX Master 3 */
4234 LDJ_DEVICE(0x4082), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4235 { /* Mouse Logitech Performance MX */
4236 LDJ_DEVICE(0x101a), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_1P0
},
4237 { /* Keyboard logitech K400 */
4239 .driver_data
= HIDPP_QUIRK_CLASS_K400
},
4240 { /* Solar Keyboard Logitech K750 */
4242 .driver_data
= HIDPP_QUIRK_CLASS_K750
},
4243 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4245 .driver_data
= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
},
4246 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4248 .driver_data
= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
},
4249 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4251 .driver_data
= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
},
4253 { LDJ_DEVICE(HID_ANY_ID
) },
4255 { /* Keyboard LX501 (Y-RR53) */
4256 L27MHZ_DEVICE(0x0049),
4257 .driver_data
= HIDPP_QUIRK_KBD_ZOOM_WHEEL
},
4258 { /* Keyboard MX3000 (Y-RAM74) */
4259 L27MHZ_DEVICE(0x0057),
4260 .driver_data
= HIDPP_QUIRK_KBD_SCROLL_WHEEL
},
4261 { /* Keyboard MX3200 (Y-RAV80) */
4262 L27MHZ_DEVICE(0x005c),
4263 .driver_data
= HIDPP_QUIRK_KBD_ZOOM_WHEEL
},
4264 { /* S510 Media Remote */
4265 L27MHZ_DEVICE(0x00fe),
4266 .driver_data
= HIDPP_QUIRK_KBD_SCROLL_WHEEL
},
4268 { L27MHZ_DEVICE(HID_ANY_ID
) },
4270 { /* Logitech G403 Wireless Gaming Mouse over USB */
4271 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xC082) },
4272 { /* Logitech G703 Gaming Mouse over USB */
4273 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xC087) },
4274 { /* Logitech G703 Hero Gaming Mouse over USB */
4275 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xC090) },
4276 { /* Logitech G900 Gaming Mouse over USB */
4277 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xC081) },
4278 { /* Logitech G903 Gaming Mouse over USB */
4279 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xC086) },
4280 { /* Logitech G903 Hero Gaming Mouse over USB */
4281 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xC091) },
4282 { /* Logitech G920 Wheel over USB */
4283 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, USB_DEVICE_ID_LOGITECH_G920_WHEEL
),
4284 .driver_data
= HIDPP_QUIRK_CLASS_G920
| HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
},
4285 { /* Logitech G Pro Gaming Mouse over USB */
4286 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xC088) },
4288 { /* MX5000 keyboard over Bluetooth */
4289 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb305),
4290 .driver_data
= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
},
4291 { /* Dinovo Edge keyboard over Bluetooth */
4292 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb309),
4293 .driver_data
= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
},
4294 { /* MX5500 keyboard over Bluetooth */
4295 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb30b),
4296 .driver_data
= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS
},
4297 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4298 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb008) },
4299 { /* MX Master mouse over Bluetooth */
4300 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb012),
4301 .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4302 { /* MX Ergo trackball over Bluetooth */
4303 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb01d) },
4304 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb01e),
4305 .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4306 { /* MX Master 3 mouse over Bluetooth */
4307 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
, 0xb023),
4308 .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
4312 MODULE_DEVICE_TABLE(hid
, hidpp_devices
);
4314 static const struct hid_usage_id hidpp_usages
[] = {
4315 { HID_GD_WHEEL
, EV_REL
, REL_WHEEL_HI_RES
},
4316 { HID_ANY_ID
- 1, HID_ANY_ID
- 1, HID_ANY_ID
- 1}
4319 static struct hid_driver hidpp_driver
= {
4320 .name
= "logitech-hidpp-device",
4321 .id_table
= hidpp_devices
,
4322 .report_fixup
= hidpp_report_fixup
,
4323 .probe
= hidpp_probe
,
4324 .remove
= hidpp_remove
,
4325 .raw_event
= hidpp_raw_event
,
4326 .usage_table
= hidpp_usages
,
4327 .event
= hidpp_event
,
4328 .input_configured
= hidpp_input_configured
,
4329 .input_mapping
= hidpp_input_mapping
,
4330 .input_mapped
= hidpp_input_mapped
,
4333 module_hid_driver(hidpp_driver
);