2 * HIDPP protocol for Logitech Unifying receivers
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/device.h>
18 #include <linux/input.h>
19 #include <linux/usb.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/sched/clock.h>
25 #include <linux/kfifo.h>
26 #include <linux/input/mt.h>
27 #include <linux/workqueue.h>
28 #include <linux/atomic.h>
29 #include <linux/fixp-arith.h>
30 #include <asm/unaligned.h>
31 #include "usbhid/usbhid.h"
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
36 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
38 static bool disable_raw_mode
;
39 module_param(disable_raw_mode
, bool, 0644);
40 MODULE_PARM_DESC(disable_raw_mode
,
41 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
43 static bool disable_tap_to_click
;
44 module_param(disable_tap_to_click
, bool, 0644);
45 MODULE_PARM_DESC(disable_tap_to_click
,
46 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
48 #define REPORT_ID_HIDPP_SHORT 0x10
49 #define REPORT_ID_HIDPP_LONG 0x11
50 #define REPORT_ID_HIDPP_VERY_LONG 0x12
52 #define HIDPP_REPORT_SHORT_LENGTH 7
53 #define HIDPP_REPORT_LONG_LENGTH 20
54 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
56 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
57 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
58 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
59 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
60 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
62 /* bits 2..20 are reserved for classes */
63 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
64 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
65 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
66 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
67 #define HIDPP_QUIRK_UNIFYING BIT(25)
68 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
69 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
70 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
72 /* Convenience constant to check for any high-res support. */
73 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
74 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
75 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
77 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
79 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
80 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
81 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
82 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
85 * There are two hidpp protocols in use, the first version hidpp10 is known
86 * as register access protocol or RAP, the second version hidpp20 is known as
87 * feature access protocol or FAP
89 * Most older devices (including the Unifying usb receiver) use the RAP protocol
90 * where as most newer devices use the FAP protocol. Both protocols are
91 * compatible with the underlying transport, which could be usb, Unifiying, or
92 * bluetooth. The message lengths are defined by the hid vendor specific report
93 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
94 * the HIDPP_LONG report type (total message length 20 bytes)
96 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
97 * messages. The Unifying receiver itself responds to RAP messages (device index
98 * is 0xFF for the receiver), and all messages (short or long) with a device
99 * index between 1 and 6 are passed untouched to the corresponding paired
102 * The paired device can be RAP or FAP, it will receive the message untouched
103 * from the Unifiying receiver.
108 u8 funcindex_clientid
;
109 u8 params
[HIDPP_REPORT_VERY_LONG_LENGTH
- 4U];
115 u8 params
[HIDPP_REPORT_VERY_LONG_LENGTH
- 4U];
118 struct hidpp_report
{
124 u8 rawbytes
[sizeof(struct fap
)];
128 struct hidpp_battery
{
130 u8 solar_feature_index
;
131 struct power_supply_desc desc
;
132 struct power_supply
*ps
;
141 * struct hidpp_scroll_counter - Utility class for processing high-resolution
143 * @dev: the input device for which events should be reported.
144 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
145 * @remainder: counts the number of high-resolution units moved since the last
146 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
147 * only be used by class methods.
148 * @direction: direction of last movement (1 or -1)
149 * @last_time: last event time, used to reset remainder after inactivity
151 struct hidpp_scroll_counter
{
152 struct input_dev
*dev
;
153 int wheel_multiplier
;
156 unsigned long long last_time
;
159 struct hidpp_device
{
160 struct hid_device
*hid_dev
;
161 struct mutex send_mutex
;
162 void *send_receive_buf
;
163 char *name
; /* will never be NULL and should not be freed */
164 wait_queue_head_t wait
;
165 bool answer_available
;
171 struct work_struct work
;
172 struct kfifo delayed_work_fifo
;
174 struct input_dev
*delayed_input
;
176 unsigned long quirks
;
177 unsigned long capabilities
;
179 struct hidpp_battery battery
;
180 struct hidpp_scroll_counter vertical_wheel_counter
;
183 /* HID++ 1.0 error codes */
184 #define HIDPP_ERROR 0x8f
185 #define HIDPP_ERROR_SUCCESS 0x00
186 #define HIDPP_ERROR_INVALID_SUBID 0x01
187 #define HIDPP_ERROR_INVALID_ADRESS 0x02
188 #define HIDPP_ERROR_INVALID_VALUE 0x03
189 #define HIDPP_ERROR_CONNECT_FAIL 0x04
190 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
191 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
192 #define HIDPP_ERROR_BUSY 0x07
193 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
194 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
195 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
196 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
197 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
198 /* HID++ 2.0 error codes */
199 #define HIDPP20_ERROR 0xff
201 static void hidpp_connect_event(struct hidpp_device
*hidpp_dev
);
203 static int __hidpp_send_report(struct hid_device
*hdev
,
204 struct hidpp_report
*hidpp_report
)
206 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
207 int fields_count
, ret
;
209 hidpp
= hid_get_drvdata(hdev
);
211 switch (hidpp_report
->report_id
) {
212 case REPORT_ID_HIDPP_SHORT
:
213 fields_count
= HIDPP_REPORT_SHORT_LENGTH
;
215 case REPORT_ID_HIDPP_LONG
:
216 fields_count
= HIDPP_REPORT_LONG_LENGTH
;
218 case REPORT_ID_HIDPP_VERY_LONG
:
219 fields_count
= HIDPP_REPORT_VERY_LONG_LENGTH
;
226 * set the device_index as the receiver, it will be overwritten by
227 * hid_hw_request if needed
229 hidpp_report
->device_index
= 0xff;
231 if (hidpp
->quirks
& HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
) {
232 ret
= hid_hw_output_report(hdev
, (u8
*)hidpp_report
, fields_count
);
234 ret
= hid_hw_raw_request(hdev
, hidpp_report
->report_id
,
235 (u8
*)hidpp_report
, fields_count
, HID_OUTPUT_REPORT
,
239 return ret
== fields_count
? 0 : -1;
243 * hidpp_send_message_sync() returns 0 in case of success, and something else
244 * in case of a failure.
245 * - If ' something else' is positive, that means that an error has been raised
246 * by the protocol itself.
247 * - If ' something else' is negative, that means that we had a classic error
248 * (-ENOMEM, -EPIPE, etc...)
250 static int hidpp_send_message_sync(struct hidpp_device
*hidpp
,
251 struct hidpp_report
*message
,
252 struct hidpp_report
*response
)
256 mutex_lock(&hidpp
->send_mutex
);
258 hidpp
->send_receive_buf
= response
;
259 hidpp
->answer_available
= false;
262 * So that we can later validate the answer when it arrives
265 *response
= *message
;
267 ret
= __hidpp_send_report(hidpp
->hid_dev
, message
);
270 dbg_hid("__hidpp_send_report returned err: %d\n", ret
);
271 memset(response
, 0, sizeof(struct hidpp_report
));
275 if (!wait_event_timeout(hidpp
->wait
, hidpp
->answer_available
,
277 dbg_hid("%s:timeout waiting for response\n", __func__
);
278 memset(response
, 0, sizeof(struct hidpp_report
));
282 if (response
->report_id
== REPORT_ID_HIDPP_SHORT
&&
283 response
->rap
.sub_id
== HIDPP_ERROR
) {
284 ret
= response
->rap
.params
[1];
285 dbg_hid("%s:got hidpp error %02X\n", __func__
, ret
);
289 if ((response
->report_id
== REPORT_ID_HIDPP_LONG
||
290 response
->report_id
== REPORT_ID_HIDPP_VERY_LONG
) &&
291 response
->fap
.feature_index
== HIDPP20_ERROR
) {
292 ret
= response
->fap
.params
[1];
293 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__
, ret
);
298 mutex_unlock(&hidpp
->send_mutex
);
303 static int hidpp_send_fap_command_sync(struct hidpp_device
*hidpp
,
304 u8 feat_index
, u8 funcindex_clientid
, u8
*params
, int param_count
,
305 struct hidpp_report
*response
)
307 struct hidpp_report
*message
;
310 if (param_count
> sizeof(message
->fap
.params
))
313 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
317 if (param_count
> (HIDPP_REPORT_LONG_LENGTH
- 4))
318 message
->report_id
= REPORT_ID_HIDPP_VERY_LONG
;
320 message
->report_id
= REPORT_ID_HIDPP_LONG
;
321 message
->fap
.feature_index
= feat_index
;
322 message
->fap
.funcindex_clientid
= funcindex_clientid
;
323 memcpy(&message
->fap
.params
, params
, param_count
);
325 ret
= hidpp_send_message_sync(hidpp
, message
, response
);
330 static int hidpp_send_rap_command_sync(struct hidpp_device
*hidpp_dev
,
331 u8 report_id
, u8 sub_id
, u8 reg_address
, u8
*params
, int param_count
,
332 struct hidpp_report
*response
)
334 struct hidpp_report
*message
;
338 case REPORT_ID_HIDPP_SHORT
:
339 max_count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
341 case REPORT_ID_HIDPP_LONG
:
342 max_count
= HIDPP_REPORT_LONG_LENGTH
- 4;
344 case REPORT_ID_HIDPP_VERY_LONG
:
345 max_count
= HIDPP_REPORT_VERY_LONG_LENGTH
- 4;
351 if (param_count
> max_count
)
354 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
357 message
->report_id
= report_id
;
358 message
->rap
.sub_id
= sub_id
;
359 message
->rap
.reg_address
= reg_address
;
360 memcpy(&message
->rap
.params
, params
, param_count
);
362 ret
= hidpp_send_message_sync(hidpp_dev
, message
, response
);
367 static void delayed_work_cb(struct work_struct
*work
)
369 struct hidpp_device
*hidpp
= container_of(work
, struct hidpp_device
,
371 hidpp_connect_event(hidpp
);
374 static inline bool hidpp_match_answer(struct hidpp_report
*question
,
375 struct hidpp_report
*answer
)
377 return (answer
->fap
.feature_index
== question
->fap
.feature_index
) &&
378 (answer
->fap
.funcindex_clientid
== question
->fap
.funcindex_clientid
);
381 static inline bool hidpp_match_error(struct hidpp_report
*question
,
382 struct hidpp_report
*answer
)
384 return ((answer
->rap
.sub_id
== HIDPP_ERROR
) ||
385 (answer
->fap
.feature_index
== HIDPP20_ERROR
)) &&
386 (answer
->fap
.funcindex_clientid
== question
->fap
.feature_index
) &&
387 (answer
->fap
.params
[0] == question
->fap
.funcindex_clientid
);
390 static inline bool hidpp_report_is_connect_event(struct hidpp_report
*report
)
392 return (report
->report_id
== REPORT_ID_HIDPP_SHORT
) &&
393 (report
->rap
.sub_id
== 0x41);
397 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
399 static void hidpp_prefix_name(char **name
, int name_length
)
401 #define PREFIX_LENGTH 9 /* "Logitech " */
406 if (name_length
> PREFIX_LENGTH
&&
407 strncmp(*name
, "Logitech ", PREFIX_LENGTH
) == 0)
408 /* The prefix has is already in the name */
411 new_length
= PREFIX_LENGTH
+ name_length
;
412 new_name
= kzalloc(new_length
, GFP_KERNEL
);
416 snprintf(new_name
, new_length
, "Logitech %s", *name
);
424 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
425 * events given a high-resolution wheel
427 * @counter: a hid_scroll_counter struct describing the wheel.
428 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
431 * Given a high-resolution movement, this function converts the movement into
432 * fractions of 120 and emits high-resolution scroll events for the input
433 * device. It also uses the multiplier from &struct hid_scroll_counter to
434 * emit low-resolution scroll events when appropriate for
435 * backwards-compatibility with userspace input libraries.
437 static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter
*counter
,
440 int low_res_value
, remainder
, direction
;
441 unsigned long long now
, previous
;
443 hi_res_value
= hi_res_value
* 120/counter
->wheel_multiplier
;
444 input_report_rel(counter
->dev
, REL_WHEEL_HI_RES
, hi_res_value
);
446 remainder
= counter
->remainder
;
447 direction
= hi_res_value
> 0 ? 1 : -1;
450 previous
= counter
->last_time
;
451 counter
->last_time
= now
;
453 * Reset the remainder after a period of inactivity or when the
454 * direction changes. This prevents the REL_WHEEL emulation point
455 * from sliding for devices that don't always provide the same
456 * number of movements per detent.
458 if (now
- previous
> 1000000000 || direction
!= counter
->direction
)
461 counter
->direction
= direction
;
462 remainder
+= hi_res_value
;
464 /* Some wheels will rest 7/8ths of a detent from the previous detent
465 * after slow movement, so we want the threshold for low-res events to
466 * be in the middle between two detents (e.g. after 4/8ths) as
467 * opposed to on the detents themselves (8/8ths).
469 if (abs(remainder
) >= 60) {
470 /* Add (or subtract) 1 because we want to trigger when the wheel
471 * is half-way to the next detent (i.e. scroll 1 detent after a
472 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
475 low_res_value
= remainder
/ 120;
476 if (low_res_value
== 0)
477 low_res_value
= (hi_res_value
> 0 ? 1 : -1);
478 input_report_rel(counter
->dev
, REL_WHEEL
, low_res_value
);
479 remainder
-= low_res_value
* 120;
481 counter
->remainder
= remainder
;
484 /* -------------------------------------------------------------------------- */
485 /* HIDP++ 1.0 commands */
486 /* -------------------------------------------------------------------------- */
488 #define HIDPP_SET_REGISTER 0x80
489 #define HIDPP_GET_REGISTER 0x81
490 #define HIDPP_SET_LONG_REGISTER 0x82
491 #define HIDPP_GET_LONG_REGISTER 0x83
494 * hidpp10_set_register_bit() - Sets a single bit in a HID++ 1.0 register.
495 * @hidpp_dev: the device to set the register on.
496 * @register_address: the address of the register to modify.
497 * @byte: the byte of the register to modify. Should be less than 3.
498 * Return: 0 if successful, otherwise a negative error code.
500 static int hidpp10_set_register_bit(struct hidpp_device
*hidpp_dev
,
501 u8 register_address
, u8 byte
, u8 bit
)
503 struct hidpp_report response
;
505 u8 params
[3] = { 0 };
507 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
508 REPORT_ID_HIDPP_SHORT
,
515 memcpy(params
, response
.rap
.params
, 3);
517 params
[byte
] |= BIT(bit
);
519 return hidpp_send_rap_command_sync(hidpp_dev
,
520 REPORT_ID_HIDPP_SHORT
,
523 params
, 3, &response
);
527 #define HIDPP_REG_GENERAL 0x00
529 static int hidpp10_enable_battery_reporting(struct hidpp_device
*hidpp_dev
)
531 return hidpp10_set_register_bit(hidpp_dev
, HIDPP_REG_GENERAL
, 0, 4);
534 #define HIDPP_REG_FEATURES 0x01
536 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
537 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device
*hidpp_dev
)
539 return hidpp10_set_register_bit(hidpp_dev
, HIDPP_REG_FEATURES
, 0, 6);
542 #define HIDPP_REG_BATTERY_STATUS 0x07
544 static int hidpp10_battery_status_map_level(u8 param
)
550 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
553 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
556 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
559 level
= POWER_SUPPLY_CAPACITY_LEVEL_HIGH
;
562 level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
568 static int hidpp10_battery_status_map_status(u8 param
)
574 /* discharging (in use) */
575 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
577 case 0x21: /* (standard) charging */
578 case 0x24: /* fast charging */
579 case 0x25: /* slow charging */
580 status
= POWER_SUPPLY_STATUS_CHARGING
;
582 case 0x26: /* topping charge */
583 case 0x22: /* charge complete */
584 status
= POWER_SUPPLY_STATUS_FULL
;
586 case 0x20: /* unknown */
587 status
= POWER_SUPPLY_STATUS_UNKNOWN
;
590 * 0x01...0x1F = reserved (not charging)
591 * 0x23 = charging error
592 * 0x27..0xff = reserved
595 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
602 static int hidpp10_query_battery_status(struct hidpp_device
*hidpp
)
604 struct hidpp_report response
;
607 ret
= hidpp_send_rap_command_sync(hidpp
,
608 REPORT_ID_HIDPP_SHORT
,
610 HIDPP_REG_BATTERY_STATUS
,
615 hidpp
->battery
.level
=
616 hidpp10_battery_status_map_level(response
.rap
.params
[0]);
617 status
= hidpp10_battery_status_map_status(response
.rap
.params
[1]);
618 hidpp
->battery
.status
= status
;
619 /* the capacity is only available when discharging or full */
620 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
621 status
== POWER_SUPPLY_STATUS_FULL
;
626 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
628 static int hidpp10_battery_mileage_map_status(u8 param
)
632 switch (param
>> 6) {
634 /* discharging (in use) */
635 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
637 case 0x01: /* charging */
638 status
= POWER_SUPPLY_STATUS_CHARGING
;
640 case 0x02: /* charge complete */
641 status
= POWER_SUPPLY_STATUS_FULL
;
644 * 0x03 = charging error
647 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
654 static int hidpp10_query_battery_mileage(struct hidpp_device
*hidpp
)
656 struct hidpp_report response
;
659 ret
= hidpp_send_rap_command_sync(hidpp
,
660 REPORT_ID_HIDPP_SHORT
,
662 HIDPP_REG_BATTERY_MILEAGE
,
667 hidpp
->battery
.capacity
= response
.rap
.params
[0];
668 status
= hidpp10_battery_mileage_map_status(response
.rap
.params
[2]);
669 hidpp
->battery
.status
= status
;
670 /* the capacity is only available when discharging or full */
671 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
672 status
== POWER_SUPPLY_STATUS_FULL
;
677 static int hidpp10_battery_event(struct hidpp_device
*hidpp
, u8
*data
, int size
)
679 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
680 int status
, capacity
, level
;
683 if (report
->report_id
!= REPORT_ID_HIDPP_SHORT
)
686 switch (report
->rap
.sub_id
) {
687 case HIDPP_REG_BATTERY_STATUS
:
688 capacity
= hidpp
->battery
.capacity
;
689 level
= hidpp10_battery_status_map_level(report
->rawbytes
[1]);
690 status
= hidpp10_battery_status_map_status(report
->rawbytes
[2]);
692 case HIDPP_REG_BATTERY_MILEAGE
:
693 capacity
= report
->rap
.params
[0];
694 level
= hidpp
->battery
.level
;
695 status
= hidpp10_battery_mileage_map_status(report
->rawbytes
[3]);
701 changed
= capacity
!= hidpp
->battery
.capacity
||
702 level
!= hidpp
->battery
.level
||
703 status
!= hidpp
->battery
.status
;
705 /* the capacity is only available when discharging or full */
706 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
707 status
== POWER_SUPPLY_STATUS_FULL
;
710 hidpp
->battery
.level
= level
;
711 hidpp
->battery
.status
= status
;
712 if (hidpp
->battery
.ps
)
713 power_supply_changed(hidpp
->battery
.ps
);
719 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
720 #define HIDPP_EXTENDED_PAIRING 0x30
721 #define HIDPP_DEVICE_NAME 0x40
723 static char *hidpp_unifying_get_name(struct hidpp_device
*hidpp_dev
)
725 struct hidpp_report response
;
727 u8 params
[1] = { HIDPP_DEVICE_NAME
};
731 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
732 REPORT_ID_HIDPP_SHORT
,
733 HIDPP_GET_LONG_REGISTER
,
734 HIDPP_REG_PAIRING_INFORMATION
,
735 params
, 1, &response
);
739 len
= response
.rap
.params
[1];
741 if (2 + len
> sizeof(response
.rap
.params
))
744 name
= kzalloc(len
+ 1, GFP_KERNEL
);
748 memcpy(name
, &response
.rap
.params
[2], len
);
750 /* include the terminating '\0' */
751 hidpp_prefix_name(&name
, len
+ 1);
756 static int hidpp_unifying_get_serial(struct hidpp_device
*hidpp
, u32
*serial
)
758 struct hidpp_report response
;
760 u8 params
[1] = { HIDPP_EXTENDED_PAIRING
};
762 ret
= hidpp_send_rap_command_sync(hidpp
,
763 REPORT_ID_HIDPP_SHORT
,
764 HIDPP_GET_LONG_REGISTER
,
765 HIDPP_REG_PAIRING_INFORMATION
,
766 params
, 1, &response
);
771 * We don't care about LE or BE, we will output it as a string
772 * with %4phD, so we need to keep the order.
774 *serial
= *((u32
*)&response
.rap
.params
[1]);
778 static int hidpp_unifying_init(struct hidpp_device
*hidpp
)
780 struct hid_device
*hdev
= hidpp
->hid_dev
;
785 ret
= hidpp_unifying_get_serial(hidpp
, &serial
);
789 snprintf(hdev
->uniq
, sizeof(hdev
->uniq
), "%04x-%4phD",
790 hdev
->product
, &serial
);
791 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev
->uniq
);
793 name
= hidpp_unifying_get_name(hidpp
);
797 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
798 dbg_hid("HID++ Unifying: Got name: %s\n", name
);
804 /* -------------------------------------------------------------------------- */
806 /* -------------------------------------------------------------------------- */
808 #define HIDPP_PAGE_ROOT 0x0000
809 #define HIDPP_PAGE_ROOT_IDX 0x00
811 #define CMD_ROOT_GET_FEATURE 0x01
812 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
814 static int hidpp_root_get_feature(struct hidpp_device
*hidpp
, u16 feature
,
815 u8
*feature_index
, u8
*feature_type
)
817 struct hidpp_report response
;
819 u8 params
[2] = { feature
>> 8, feature
& 0x00FF };
821 ret
= hidpp_send_fap_command_sync(hidpp
,
823 CMD_ROOT_GET_FEATURE
,
824 params
, 2, &response
);
828 if (response
.fap
.params
[0] == 0)
831 *feature_index
= response
.fap
.params
[0];
832 *feature_type
= response
.fap
.params
[1];
837 static int hidpp_root_get_protocol_version(struct hidpp_device
*hidpp
)
839 const u8 ping_byte
= 0x5a;
840 u8 ping_data
[3] = { 0, 0, ping_byte
};
841 struct hidpp_report response
;
844 ret
= hidpp_send_rap_command_sync(hidpp
,
845 REPORT_ID_HIDPP_SHORT
,
847 CMD_ROOT_GET_PROTOCOL_VERSION
,
848 ping_data
, sizeof(ping_data
), &response
);
850 if (ret
== HIDPP_ERROR_INVALID_SUBID
) {
851 hidpp
->protocol_major
= 1;
852 hidpp
->protocol_minor
= 0;
856 /* the device might not be connected */
857 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
861 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
868 if (response
.rap
.params
[2] != ping_byte
) {
869 hid_err(hidpp
->hid_dev
, "%s: ping mismatch 0x%02x != 0x%02x\n",
870 __func__
, response
.rap
.params
[2], ping_byte
);
874 hidpp
->protocol_major
= response
.rap
.params
[0];
875 hidpp
->protocol_minor
= response
.rap
.params
[1];
880 static bool hidpp_is_connected(struct hidpp_device
*hidpp
)
884 ret
= hidpp_root_get_protocol_version(hidpp
);
886 hid_dbg(hidpp
->hid_dev
, "HID++ %u.%u device connected.\n",
887 hidpp
->protocol_major
, hidpp
->protocol_minor
);
891 /* -------------------------------------------------------------------------- */
892 /* 0x0005: GetDeviceNameType */
893 /* -------------------------------------------------------------------------- */
895 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
897 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
898 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
899 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
901 static int hidpp_devicenametype_get_count(struct hidpp_device
*hidpp
,
902 u8 feature_index
, u8
*nameLength
)
904 struct hidpp_report response
;
907 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
908 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT
, NULL
, 0, &response
);
911 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
918 *nameLength
= response
.fap
.params
[0];
923 static int hidpp_devicenametype_get_device_name(struct hidpp_device
*hidpp
,
924 u8 feature_index
, u8 char_index
, char *device_name
, int len_buf
)
926 struct hidpp_report response
;
930 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
931 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME
, &char_index
, 1,
935 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
942 switch (response
.report_id
) {
943 case REPORT_ID_HIDPP_VERY_LONG
:
944 count
= HIDPP_REPORT_VERY_LONG_LENGTH
- 4;
946 case REPORT_ID_HIDPP_LONG
:
947 count
= HIDPP_REPORT_LONG_LENGTH
- 4;
949 case REPORT_ID_HIDPP_SHORT
:
950 count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
959 for (i
= 0; i
< count
; i
++)
960 device_name
[i
] = response
.fap
.params
[i
];
965 static char *hidpp_get_device_name(struct hidpp_device
*hidpp
)
974 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_GET_DEVICE_NAME_TYPE
,
975 &feature_index
, &feature_type
);
979 ret
= hidpp_devicenametype_get_count(hidpp
, feature_index
,
984 name
= kzalloc(__name_length
+ 1, GFP_KERNEL
);
988 while (index
< __name_length
) {
989 ret
= hidpp_devicenametype_get_device_name(hidpp
,
990 feature_index
, index
, name
+ index
,
991 __name_length
- index
);
999 /* include the terminating '\0' */
1000 hidpp_prefix_name(&name
, __name_length
+ 1);
1005 /* -------------------------------------------------------------------------- */
1006 /* 0x1000: Battery level status */
1007 /* -------------------------------------------------------------------------- */
1009 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1011 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1012 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1014 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1016 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1017 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1018 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1020 static int hidpp_map_battery_level(int capacity
)
1023 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
1025 * The spec says this should be < 31 but some devices report 30
1026 * with brand new batteries and Windows reports 30 as "Good".
1028 else if (capacity
< 30)
1029 return POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
1030 else if (capacity
< 81)
1031 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
1032 return POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1035 static int hidpp20_batterylevel_map_status_capacity(u8 data
[3], int *capacity
,
1041 *capacity
= data
[0];
1042 *next_capacity
= data
[1];
1043 *level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
1045 /* When discharging, we can rely on the device reported capacity.
1046 * For all other states the device reports 0 (unknown).
1049 case 0: /* discharging (in use) */
1050 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1051 *level
= hidpp_map_battery_level(*capacity
);
1053 case 1: /* recharging */
1054 status
= POWER_SUPPLY_STATUS_CHARGING
;
1056 case 2: /* charge in final stage */
1057 status
= POWER_SUPPLY_STATUS_CHARGING
;
1059 case 3: /* charge complete */
1060 status
= POWER_SUPPLY_STATUS_FULL
;
1061 *level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1064 case 4: /* recharging below optimal speed */
1065 status
= POWER_SUPPLY_STATUS_CHARGING
;
1067 /* 5 = invalid battery type
1069 7 = other charging error */
1071 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1078 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device
*hidpp
,
1085 struct hidpp_report response
;
1087 u8
*params
= (u8
*)response
.fap
.params
;
1089 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1090 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS
,
1091 NULL
, 0, &response
);
1093 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1100 *status
= hidpp20_batterylevel_map_status_capacity(params
, capacity
,
1107 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device
*hidpp
,
1110 struct hidpp_report response
;
1112 u8
*params
= (u8
*)response
.fap
.params
;
1113 unsigned int level_count
, flags
;
1115 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1116 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY
,
1117 NULL
, 0, &response
);
1119 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1126 level_count
= params
[0];
1129 if (level_count
< 10 || !(flags
& FLAG_BATTERY_LEVEL_MILEAGE
))
1130 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
;
1132 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
1137 static int hidpp20_query_battery_info(struct hidpp_device
*hidpp
)
1141 int status
, capacity
, next_capacity
, level
;
1143 if (hidpp
->battery
.feature_index
== 0xff) {
1144 ret
= hidpp_root_get_feature(hidpp
,
1145 HIDPP_PAGE_BATTERY_LEVEL_STATUS
,
1146 &hidpp
->battery
.feature_index
,
1152 ret
= hidpp20_batterylevel_get_battery_capacity(hidpp
,
1153 hidpp
->battery
.feature_index
,
1155 &next_capacity
, &level
);
1159 ret
= hidpp20_batterylevel_get_battery_info(hidpp
,
1160 hidpp
->battery
.feature_index
);
1164 hidpp
->battery
.status
= status
;
1165 hidpp
->battery
.capacity
= capacity
;
1166 hidpp
->battery
.level
= level
;
1167 /* the capacity is only available when discharging or full */
1168 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
1169 status
== POWER_SUPPLY_STATUS_FULL
;
1174 static int hidpp20_battery_event(struct hidpp_device
*hidpp
,
1177 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1178 int status
, capacity
, next_capacity
, level
;
1181 if (report
->fap
.feature_index
!= hidpp
->battery
.feature_index
||
1182 report
->fap
.funcindex_clientid
!= EVENT_BATTERY_LEVEL_STATUS_BROADCAST
)
1185 status
= hidpp20_batterylevel_map_status_capacity(report
->fap
.params
,
1190 /* the capacity is only available when discharging or full */
1191 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
1192 status
== POWER_SUPPLY_STATUS_FULL
;
1194 changed
= capacity
!= hidpp
->battery
.capacity
||
1195 level
!= hidpp
->battery
.level
||
1196 status
!= hidpp
->battery
.status
;
1199 hidpp
->battery
.level
= level
;
1200 hidpp
->battery
.capacity
= capacity
;
1201 hidpp
->battery
.status
= status
;
1202 if (hidpp
->battery
.ps
)
1203 power_supply_changed(hidpp
->battery
.ps
);
1209 static enum power_supply_property hidpp_battery_props
[] = {
1210 POWER_SUPPLY_PROP_ONLINE
,
1211 POWER_SUPPLY_PROP_STATUS
,
1212 POWER_SUPPLY_PROP_SCOPE
,
1213 POWER_SUPPLY_PROP_MODEL_NAME
,
1214 POWER_SUPPLY_PROP_MANUFACTURER
,
1215 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
1216 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1217 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1220 static int hidpp_battery_get_property(struct power_supply
*psy
,
1221 enum power_supply_property psp
,
1222 union power_supply_propval
*val
)
1224 struct hidpp_device
*hidpp
= power_supply_get_drvdata(psy
);
1228 case POWER_SUPPLY_PROP_STATUS
:
1229 val
->intval
= hidpp
->battery
.status
;
1231 case POWER_SUPPLY_PROP_CAPACITY
:
1232 val
->intval
= hidpp
->battery
.capacity
;
1234 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
1235 val
->intval
= hidpp
->battery
.level
;
1237 case POWER_SUPPLY_PROP_SCOPE
:
1238 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1240 case POWER_SUPPLY_PROP_ONLINE
:
1241 val
->intval
= hidpp
->battery
.online
;
1243 case POWER_SUPPLY_PROP_MODEL_NAME
:
1244 if (!strncmp(hidpp
->name
, "Logitech ", 9))
1245 val
->strval
= hidpp
->name
+ 9;
1247 val
->strval
= hidpp
->name
;
1249 case POWER_SUPPLY_PROP_MANUFACTURER
:
1250 val
->strval
= "Logitech";
1252 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
1253 val
->strval
= hidpp
->hid_dev
->uniq
;
1263 /* -------------------------------------------------------------------------- */
1264 /* 0x2120: Hi-resolution scrolling */
1265 /* -------------------------------------------------------------------------- */
1267 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1269 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1271 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device
*hidpp
,
1272 bool enabled
, u8
*multiplier
)
1278 struct hidpp_report response
;
1280 ret
= hidpp_root_get_feature(hidpp
,
1281 HIDPP_PAGE_HI_RESOLUTION_SCROLLING
,
1287 params
[0] = enabled
? BIT(0) : 0;
1288 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1289 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE
,
1290 params
, sizeof(params
), &response
);
1293 *multiplier
= response
.fap
.params
[1];
1297 /* -------------------------------------------------------------------------- */
1298 /* 0x2121: HiRes Wheel */
1299 /* -------------------------------------------------------------------------- */
1301 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1303 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1304 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1306 static int hidpp_hrw_get_wheel_capability(struct hidpp_device
*hidpp
,
1312 struct hidpp_report response
;
1314 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_HIRES_WHEEL
,
1315 &feature_index
, &feature_type
);
1317 goto return_default
;
1319 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1320 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY
,
1321 NULL
, 0, &response
);
1323 goto return_default
;
1325 *multiplier
= response
.fap
.params
[0];
1328 hid_warn(hidpp
->hid_dev
,
1329 "Couldn't get wheel multiplier (error %d)\n", ret
);
1333 static int hidpp_hrw_set_wheel_mode(struct hidpp_device
*hidpp
, bool invert
,
1334 bool high_resolution
, bool use_hidpp
)
1340 struct hidpp_report response
;
1342 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_HIRES_WHEEL
,
1343 &feature_index
, &feature_type
);
1347 params
[0] = (invert
? BIT(2) : 0) |
1348 (high_resolution
? BIT(1) : 0) |
1349 (use_hidpp
? BIT(0) : 0);
1351 return hidpp_send_fap_command_sync(hidpp
, feature_index
,
1352 CMD_HIRES_WHEEL_SET_WHEEL_MODE
,
1353 params
, sizeof(params
), &response
);
1356 /* -------------------------------------------------------------------------- */
1357 /* 0x4301: Solar Keyboard */
1358 /* -------------------------------------------------------------------------- */
1360 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1362 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1364 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1365 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1366 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1368 static int hidpp_solar_request_battery_event(struct hidpp_device
*hidpp
)
1370 struct hidpp_report response
;
1371 u8 params
[2] = { 1, 1 };
1375 if (hidpp
->battery
.feature_index
== 0xff) {
1376 ret
= hidpp_root_get_feature(hidpp
,
1377 HIDPP_PAGE_SOLAR_KEYBOARD
,
1378 &hidpp
->battery
.solar_feature_index
,
1384 ret
= hidpp_send_fap_command_sync(hidpp
,
1385 hidpp
->battery
.solar_feature_index
,
1386 CMD_SOLAR_SET_LIGHT_MEASURE
,
1387 params
, 2, &response
);
1389 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1396 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
1401 static int hidpp_solar_battery_event(struct hidpp_device
*hidpp
,
1404 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1405 int capacity
, lux
, status
;
1408 function
= report
->fap
.funcindex_clientid
;
1411 if (report
->fap
.feature_index
!= hidpp
->battery
.solar_feature_index
||
1412 !(function
== EVENT_SOLAR_BATTERY_BROADCAST
||
1413 function
== EVENT_SOLAR_BATTERY_LIGHT_MEASURE
||
1414 function
== EVENT_SOLAR_CHECK_LIGHT_BUTTON
))
1417 capacity
= report
->fap
.params
[0];
1420 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE
:
1421 lux
= (report
->fap
.params
[1] << 8) | report
->fap
.params
[2];
1423 status
= POWER_SUPPLY_STATUS_CHARGING
;
1425 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1427 case EVENT_SOLAR_CHECK_LIGHT_BUTTON
:
1429 if (capacity
< hidpp
->battery
.capacity
)
1430 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1432 status
= POWER_SUPPLY_STATUS_CHARGING
;
1436 if (capacity
== 100)
1437 status
= POWER_SUPPLY_STATUS_FULL
;
1439 hidpp
->battery
.online
= true;
1440 if (capacity
!= hidpp
->battery
.capacity
||
1441 status
!= hidpp
->battery
.status
) {
1442 hidpp
->battery
.capacity
= capacity
;
1443 hidpp
->battery
.status
= status
;
1444 if (hidpp
->battery
.ps
)
1445 power_supply_changed(hidpp
->battery
.ps
);
1451 /* -------------------------------------------------------------------------- */
1452 /* 0x6010: Touchpad FW items */
1453 /* -------------------------------------------------------------------------- */
1455 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1457 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1459 struct hidpp_touchpad_fw_items
{
1461 uint8_t desired_state
;
1467 * send a set state command to the device by reading the current items->state
1468 * field. items is then filled with the current state.
1470 static int hidpp_touchpad_fw_items_set(struct hidpp_device
*hidpp
,
1472 struct hidpp_touchpad_fw_items
*items
)
1474 struct hidpp_report response
;
1476 u8
*params
= (u8
*)response
.fap
.params
;
1478 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1479 CMD_TOUCHPAD_FW_ITEMS_SET
, &items
->state
, 1, &response
);
1482 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1489 items
->presence
= params
[0];
1490 items
->desired_state
= params
[1];
1491 items
->state
= params
[2];
1492 items
->persistent
= params
[3];
1497 /* -------------------------------------------------------------------------- */
1498 /* 0x6100: TouchPadRawXY */
1499 /* -------------------------------------------------------------------------- */
1501 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1503 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1504 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1506 #define EVENT_TOUCHPAD_RAW_XY 0x00
1508 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1509 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1511 struct hidpp_touchpad_raw_info
{
1522 struct hidpp_touchpad_raw_xy_finger
{
1532 struct hidpp_touchpad_raw_xy
{
1534 struct hidpp_touchpad_raw_xy_finger fingers
[2];
1541 static int hidpp_touchpad_get_raw_info(struct hidpp_device
*hidpp
,
1542 u8 feature_index
, struct hidpp_touchpad_raw_info
*raw_info
)
1544 struct hidpp_report response
;
1546 u8
*params
= (u8
*)response
.fap
.params
;
1548 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1549 CMD_TOUCHPAD_GET_RAW_INFO
, NULL
, 0, &response
);
1552 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1559 raw_info
->x_size
= get_unaligned_be16(¶ms
[0]);
1560 raw_info
->y_size
= get_unaligned_be16(¶ms
[2]);
1561 raw_info
->z_range
= params
[4];
1562 raw_info
->area_range
= params
[5];
1563 raw_info
->maxcontacts
= params
[7];
1564 raw_info
->origin
= params
[8];
1565 /* res is given in unit per inch */
1566 raw_info
->res
= get_unaligned_be16(¶ms
[13]) * 2 / 51;
1571 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device
*hidpp_dev
,
1572 u8 feature_index
, bool send_raw_reports
,
1573 bool sensor_enhanced_settings
)
1575 struct hidpp_report response
;
1579 * bit 0 - enable raw
1580 * bit 1 - 16bit Z, no area
1581 * bit 2 - enhanced sensitivity
1582 * bit 3 - width, height (4 bits each) instead of area
1583 * bit 4 - send raw + gestures (degrades smoothness)
1584 * remaining bits - reserved
1586 u8 params
= send_raw_reports
| (sensor_enhanced_settings
<< 2);
1588 return hidpp_send_fap_command_sync(hidpp_dev
, feature_index
,
1589 CMD_TOUCHPAD_SET_RAW_REPORT_STATE
, ¶ms
, 1, &response
);
1592 static void hidpp_touchpad_touch_event(u8
*data
,
1593 struct hidpp_touchpad_raw_xy_finger
*finger
)
1595 u8 x_m
= data
[0] << 2;
1596 u8 y_m
= data
[2] << 2;
1598 finger
->x
= x_m
<< 6 | data
[1];
1599 finger
->y
= y_m
<< 6 | data
[3];
1601 finger
->contact_type
= data
[0] >> 6;
1602 finger
->contact_status
= data
[2] >> 6;
1604 finger
->z
= data
[4];
1605 finger
->area
= data
[5];
1606 finger
->finger_id
= data
[6] >> 4;
1609 static void hidpp_touchpad_raw_xy_event(struct hidpp_device
*hidpp_dev
,
1610 u8
*data
, struct hidpp_touchpad_raw_xy
*raw_xy
)
1612 memset(raw_xy
, 0, sizeof(struct hidpp_touchpad_raw_xy
));
1613 raw_xy
->end_of_frame
= data
[8] & 0x01;
1614 raw_xy
->spurious_flag
= (data
[8] >> 1) & 0x01;
1615 raw_xy
->finger_count
= data
[15] & 0x0f;
1616 raw_xy
->button
= (data
[8] >> 2) & 0x01;
1618 if (raw_xy
->finger_count
) {
1619 hidpp_touchpad_touch_event(&data
[2], &raw_xy
->fingers
[0]);
1620 hidpp_touchpad_touch_event(&data
[9], &raw_xy
->fingers
[1]);
1624 /* -------------------------------------------------------------------------- */
1625 /* 0x8123: Force feedback support */
1626 /* -------------------------------------------------------------------------- */
1628 #define HIDPP_FF_GET_INFO 0x01
1629 #define HIDPP_FF_RESET_ALL 0x11
1630 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1631 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1632 #define HIDPP_FF_DESTROY_EFFECT 0x41
1633 #define HIDPP_FF_GET_APERTURE 0x51
1634 #define HIDPP_FF_SET_APERTURE 0x61
1635 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1636 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1638 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1639 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1640 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1641 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1643 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1644 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1645 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1646 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1647 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1648 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1649 #define HIDPP_FF_EFFECT_SPRING 0x06
1650 #define HIDPP_FF_EFFECT_DAMPER 0x07
1651 #define HIDPP_FF_EFFECT_FRICTION 0x08
1652 #define HIDPP_FF_EFFECT_INERTIA 0x09
1653 #define HIDPP_FF_EFFECT_RAMP 0x0A
1655 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1657 #define HIDPP_FF_EFFECTID_NONE -1
1658 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1660 #define HIDPP_FF_MAX_PARAMS 20
1661 #define HIDPP_FF_RESERVED_SLOTS 1
1663 struct hidpp_ff_private_data
{
1664 struct hidpp_device
*hidpp
;
1672 struct workqueue_struct
*wq
;
1673 atomic_t workqueue_size
;
1676 struct hidpp_ff_work_data
{
1677 struct work_struct work
;
1678 struct hidpp_ff_private_data
*data
;
1681 u8 params
[HIDPP_FF_MAX_PARAMS
];
1685 static const signed short hidpp_ff_effects
[] = {
1700 static const signed short hidpp_ff_effects_v2
[] = {
1707 static const u8 HIDPP_FF_CONDITION_CMDS
[] = {
1708 HIDPP_FF_EFFECT_SPRING
,
1709 HIDPP_FF_EFFECT_FRICTION
,
1710 HIDPP_FF_EFFECT_DAMPER
,
1711 HIDPP_FF_EFFECT_INERTIA
1714 static const char *HIDPP_FF_CONDITION_NAMES
[] = {
1722 static u8
hidpp_ff_find_effect(struct hidpp_ff_private_data
*data
, int effect_id
)
1726 for (i
= 0; i
< data
->num_effects
; i
++)
1727 if (data
->effect_ids
[i
] == effect_id
)
1733 static void hidpp_ff_work_handler(struct work_struct
*w
)
1735 struct hidpp_ff_work_data
*wd
= container_of(w
, struct hidpp_ff_work_data
, work
);
1736 struct hidpp_ff_private_data
*data
= wd
->data
;
1737 struct hidpp_report response
;
1741 /* add slot number if needed */
1742 switch (wd
->effect_id
) {
1743 case HIDPP_FF_EFFECTID_AUTOCENTER
:
1744 wd
->params
[0] = data
->slot_autocenter
;
1746 case HIDPP_FF_EFFECTID_NONE
:
1747 /* leave slot as zero */
1750 /* find current slot for effect */
1751 wd
->params
[0] = hidpp_ff_find_effect(data
, wd
->effect_id
);
1755 /* send command and wait for reply */
1756 ret
= hidpp_send_fap_command_sync(data
->hidpp
, data
->feature_index
,
1757 wd
->command
, wd
->params
, wd
->size
, &response
);
1760 hid_err(data
->hidpp
->hid_dev
, "Failed to send command to device!\n");
1764 /* parse return data */
1765 switch (wd
->command
) {
1766 case HIDPP_FF_DOWNLOAD_EFFECT
:
1767 slot
= response
.fap
.params
[0];
1768 if (slot
> 0 && slot
<= data
->num_effects
) {
1769 if (wd
->effect_id
>= 0)
1770 /* regular effect uploaded */
1771 data
->effect_ids
[slot
-1] = wd
->effect_id
;
1772 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
1773 /* autocenter spring uploaded */
1774 data
->slot_autocenter
= slot
;
1777 case HIDPP_FF_DESTROY_EFFECT
:
1778 if (wd
->effect_id
>= 0)
1779 /* regular effect destroyed */
1780 data
->effect_ids
[wd
->params
[0]-1] = -1;
1781 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
1782 /* autocenter spring destoyed */
1783 data
->slot_autocenter
= 0;
1785 case HIDPP_FF_SET_GLOBAL_GAINS
:
1786 data
->gain
= (wd
->params
[0] << 8) + wd
->params
[1];
1788 case HIDPP_FF_SET_APERTURE
:
1789 data
->range
= (wd
->params
[0] << 8) + wd
->params
[1];
1792 /* no action needed */
1797 atomic_dec(&data
->workqueue_size
);
1801 static int hidpp_ff_queue_work(struct hidpp_ff_private_data
*data
, int effect_id
, u8 command
, u8
*params
, u8 size
)
1803 struct hidpp_ff_work_data
*wd
= kzalloc(sizeof(*wd
), GFP_KERNEL
);
1809 INIT_WORK(&wd
->work
, hidpp_ff_work_handler
);
1812 wd
->effect_id
= effect_id
;
1813 wd
->command
= command
;
1815 memcpy(wd
->params
, params
, size
);
1817 atomic_inc(&data
->workqueue_size
);
1818 queue_work(data
->wq
, &wd
->work
);
1820 /* warn about excessive queue size */
1821 s
= atomic_read(&data
->workqueue_size
);
1822 if (s
>= 20 && s
% 20 == 0)
1823 hid_warn(data
->hidpp
->hid_dev
, "Force feedback command queue contains %d commands, causing substantial delays!", s
);
1828 static int hidpp_ff_upload_effect(struct input_dev
*dev
, struct ff_effect
*effect
, struct ff_effect
*old
)
1830 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1835 /* set common parameters */
1836 params
[2] = effect
->replay
.length
>> 8;
1837 params
[3] = effect
->replay
.length
& 255;
1838 params
[4] = effect
->replay
.delay
>> 8;
1839 params
[5] = effect
->replay
.delay
& 255;
1841 switch (effect
->type
) {
1843 force
= (effect
->u
.constant
.level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1844 params
[1] = HIDPP_FF_EFFECT_CONSTANT
;
1845 params
[6] = force
>> 8;
1846 params
[7] = force
& 255;
1847 params
[8] = effect
->u
.constant
.envelope
.attack_level
>> 7;
1848 params
[9] = effect
->u
.constant
.envelope
.attack_length
>> 8;
1849 params
[10] = effect
->u
.constant
.envelope
.attack_length
& 255;
1850 params
[11] = effect
->u
.constant
.envelope
.fade_level
>> 7;
1851 params
[12] = effect
->u
.constant
.envelope
.fade_length
>> 8;
1852 params
[13] = effect
->u
.constant
.envelope
.fade_length
& 255;
1854 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1855 effect
->u
.constant
.level
,
1856 effect
->direction
, force
);
1857 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1858 effect
->u
.constant
.envelope
.attack_level
,
1859 effect
->u
.constant
.envelope
.attack_length
,
1860 effect
->u
.constant
.envelope
.fade_level
,
1861 effect
->u
.constant
.envelope
.fade_length
);
1865 switch (effect
->u
.periodic
.waveform
) {
1867 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SINE
;
1870 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE
;
1873 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP
;
1876 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN
;
1879 params
[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE
;
1882 hid_err(data
->hidpp
->hid_dev
, "Unexpected periodic waveform type %i!\n", effect
->u
.periodic
.waveform
);
1885 force
= (effect
->u
.periodic
.magnitude
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1886 params
[6] = effect
->u
.periodic
.magnitude
>> 8;
1887 params
[7] = effect
->u
.periodic
.magnitude
& 255;
1888 params
[8] = effect
->u
.periodic
.offset
>> 8;
1889 params
[9] = effect
->u
.periodic
.offset
& 255;
1890 params
[10] = effect
->u
.periodic
.period
>> 8;
1891 params
[11] = effect
->u
.periodic
.period
& 255;
1892 params
[12] = effect
->u
.periodic
.phase
>> 8;
1893 params
[13] = effect
->u
.periodic
.phase
& 255;
1894 params
[14] = effect
->u
.periodic
.envelope
.attack_level
>> 7;
1895 params
[15] = effect
->u
.periodic
.envelope
.attack_length
>> 8;
1896 params
[16] = effect
->u
.periodic
.envelope
.attack_length
& 255;
1897 params
[17] = effect
->u
.periodic
.envelope
.fade_level
>> 7;
1898 params
[18] = effect
->u
.periodic
.envelope
.fade_length
>> 8;
1899 params
[19] = effect
->u
.periodic
.envelope
.fade_length
& 255;
1901 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1902 effect
->u
.periodic
.magnitude
, effect
->direction
,
1903 effect
->u
.periodic
.offset
,
1904 effect
->u
.periodic
.period
,
1905 effect
->u
.periodic
.phase
);
1906 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1907 effect
->u
.periodic
.envelope
.attack_level
,
1908 effect
->u
.periodic
.envelope
.attack_length
,
1909 effect
->u
.periodic
.envelope
.fade_level
,
1910 effect
->u
.periodic
.envelope
.fade_length
);
1914 params
[1] = HIDPP_FF_EFFECT_RAMP
;
1915 force
= (effect
->u
.ramp
.start_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1916 params
[6] = force
>> 8;
1917 params
[7] = force
& 255;
1918 force
= (effect
->u
.ramp
.end_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1919 params
[8] = force
>> 8;
1920 params
[9] = force
& 255;
1921 params
[10] = effect
->u
.ramp
.envelope
.attack_level
>> 7;
1922 params
[11] = effect
->u
.ramp
.envelope
.attack_length
>> 8;
1923 params
[12] = effect
->u
.ramp
.envelope
.attack_length
& 255;
1924 params
[13] = effect
->u
.ramp
.envelope
.fade_level
>> 7;
1925 params
[14] = effect
->u
.ramp
.envelope
.fade_length
>> 8;
1926 params
[15] = effect
->u
.ramp
.envelope
.fade_length
& 255;
1928 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1929 effect
->u
.ramp
.start_level
,
1930 effect
->u
.ramp
.end_level
,
1931 effect
->direction
, force
);
1932 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1933 effect
->u
.ramp
.envelope
.attack_level
,
1934 effect
->u
.ramp
.envelope
.attack_length
,
1935 effect
->u
.ramp
.envelope
.fade_level
,
1936 effect
->u
.ramp
.envelope
.fade_length
);
1942 params
[1] = HIDPP_FF_CONDITION_CMDS
[effect
->type
- FF_SPRING
];
1943 params
[6] = effect
->u
.condition
[0].left_saturation
>> 9;
1944 params
[7] = (effect
->u
.condition
[0].left_saturation
>> 1) & 255;
1945 params
[8] = effect
->u
.condition
[0].left_coeff
>> 8;
1946 params
[9] = effect
->u
.condition
[0].left_coeff
& 255;
1947 params
[10] = effect
->u
.condition
[0].deadband
>> 9;
1948 params
[11] = (effect
->u
.condition
[0].deadband
>> 1) & 255;
1949 params
[12] = effect
->u
.condition
[0].center
>> 8;
1950 params
[13] = effect
->u
.condition
[0].center
& 255;
1951 params
[14] = effect
->u
.condition
[0].right_coeff
>> 8;
1952 params
[15] = effect
->u
.condition
[0].right_coeff
& 255;
1953 params
[16] = effect
->u
.condition
[0].right_saturation
>> 9;
1954 params
[17] = (effect
->u
.condition
[0].right_saturation
>> 1) & 255;
1956 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1957 HIDPP_FF_CONDITION_NAMES
[effect
->type
- FF_SPRING
],
1958 effect
->u
.condition
[0].left_coeff
,
1959 effect
->u
.condition
[0].left_saturation
,
1960 effect
->u
.condition
[0].right_coeff
,
1961 effect
->u
.condition
[0].right_saturation
);
1962 dbg_hid(" deadband=%d, center=%d\n",
1963 effect
->u
.condition
[0].deadband
,
1964 effect
->u
.condition
[0].center
);
1967 hid_err(data
->hidpp
->hid_dev
, "Unexpected force type %i!\n", effect
->type
);
1971 return hidpp_ff_queue_work(data
, effect
->id
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, size
);
1974 static int hidpp_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
1976 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1979 params
[1] = value
? HIDPP_FF_EFFECT_STATE_PLAY
: HIDPP_FF_EFFECT_STATE_STOP
;
1981 dbg_hid("St%sing playback of effect %d.\n", value
?"art":"opp", effect_id
);
1983 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_SET_EFFECT_STATE
, params
, ARRAY_SIZE(params
));
1986 static int hidpp_ff_erase_effect(struct input_dev
*dev
, int effect_id
)
1988 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1991 dbg_hid("Erasing effect %d.\n", effect_id
);
1993 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_DESTROY_EFFECT
, &slot
, 1);
1996 static void hidpp_ff_set_autocenter(struct input_dev
*dev
, u16 magnitude
)
1998 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
2001 dbg_hid("Setting autocenter to %d.\n", magnitude
);
2003 /* start a standard spring effect */
2004 params
[1] = HIDPP_FF_EFFECT_SPRING
| HIDPP_FF_EFFECT_AUTOSTART
;
2005 /* zero delay and duration */
2006 params
[2] = params
[3] = params
[4] = params
[5] = 0;
2007 /* set coeff to 25% of saturation */
2008 params
[8] = params
[14] = magnitude
>> 11;
2009 params
[9] = params
[15] = (magnitude
>> 3) & 255;
2010 params
[6] = params
[16] = magnitude
>> 9;
2011 params
[7] = params
[17] = (magnitude
>> 1) & 255;
2012 /* zero deadband and center */
2013 params
[10] = params
[11] = params
[12] = params
[13] = 0;
2015 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_AUTOCENTER
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, ARRAY_SIZE(params
));
2018 static void hidpp_ff_set_gain(struct input_dev
*dev
, u16 gain
)
2020 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
2023 dbg_hid("Setting gain to %d.\n", gain
);
2025 params
[0] = gain
>> 8;
2026 params
[1] = gain
& 255;
2027 params
[2] = 0; /* no boost */
2030 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_NONE
, HIDPP_FF_SET_GLOBAL_GAINS
, params
, ARRAY_SIZE(params
));
2033 static ssize_t
hidpp_ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2035 struct hid_device
*hid
= to_hid_device(dev
);
2036 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
2037 struct input_dev
*idev
= hidinput
->input
;
2038 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
2040 return scnprintf(buf
, PAGE_SIZE
, "%u\n", data
->range
);
2043 static ssize_t
hidpp_ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
2045 struct hid_device
*hid
= to_hid_device(dev
);
2046 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
2047 struct input_dev
*idev
= hidinput
->input
;
2048 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
2050 int range
= simple_strtoul(buf
, NULL
, 10);
2052 range
= clamp(range
, 180, 900);
2054 params
[0] = range
>> 8;
2055 params
[1] = range
& 0x00FF;
2057 hidpp_ff_queue_work(data
, -1, HIDPP_FF_SET_APERTURE
, params
, ARRAY_SIZE(params
));
2062 static DEVICE_ATTR(range
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
, hidpp_ff_range_show
, hidpp_ff_range_store
);
2064 static void hidpp_ff_destroy(struct ff_device
*ff
)
2066 struct hidpp_ff_private_data
*data
= ff
->private;
2068 kfree(data
->effect_ids
);
2071 static int hidpp_ff_init(struct hidpp_device
*hidpp
, u8 feature_index
)
2073 struct hid_device
*hid
= hidpp
->hid_dev
;
2074 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
2075 struct input_dev
*dev
= hidinput
->input
;
2076 const struct usb_device_descriptor
*udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
2077 const u16 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
2078 struct ff_device
*ff
;
2079 struct hidpp_report response
;
2080 struct hidpp_ff_private_data
*data
;
2081 int error
, j
, num_slots
;
2085 hid_err(hid
, "Struct input_dev not set!\n");
2089 /* Get firmware release */
2090 version
= bcdDevice
& 255;
2092 /* Set supported force feedback capabilities */
2093 for (j
= 0; hidpp_ff_effects
[j
] >= 0; j
++)
2094 set_bit(hidpp_ff_effects
[j
], dev
->ffbit
);
2096 for (j
= 0; hidpp_ff_effects_v2
[j
] >= 0; j
++)
2097 set_bit(hidpp_ff_effects_v2
[j
], dev
->ffbit
);
2099 /* Read number of slots available in device */
2100 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
2101 HIDPP_FF_GET_INFO
, NULL
, 0, &response
);
2105 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
2110 num_slots
= response
.fap
.params
[0] - HIDPP_FF_RESERVED_SLOTS
;
2112 error
= input_ff_create(dev
, num_slots
);
2115 hid_err(dev
, "Failed to create FF device!\n");
2119 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2122 data
->effect_ids
= kcalloc(num_slots
, sizeof(int), GFP_KERNEL
);
2123 if (!data
->effect_ids
) {
2127 data
->wq
= create_singlethread_workqueue("hidpp-ff-sendqueue");
2129 kfree(data
->effect_ids
);
2134 data
->hidpp
= hidpp
;
2135 data
->feature_index
= feature_index
;
2136 data
->version
= version
;
2137 data
->slot_autocenter
= 0;
2138 data
->num_effects
= num_slots
;
2139 for (j
= 0; j
< num_slots
; j
++)
2140 data
->effect_ids
[j
] = -1;
2145 ff
->upload
= hidpp_ff_upload_effect
;
2146 ff
->erase
= hidpp_ff_erase_effect
;
2147 ff
->playback
= hidpp_ff_playback
;
2148 ff
->set_gain
= hidpp_ff_set_gain
;
2149 ff
->set_autocenter
= hidpp_ff_set_autocenter
;
2150 ff
->destroy
= hidpp_ff_destroy
;
2153 /* reset all forces */
2154 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
2155 HIDPP_FF_RESET_ALL
, NULL
, 0, &response
);
2157 /* Read current Range */
2158 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
2159 HIDPP_FF_GET_APERTURE
, NULL
, 0, &response
);
2161 hid_warn(hidpp
->hid_dev
, "Failed to read range from device!\n");
2162 data
->range
= error
? 900 : get_unaligned_be16(&response
.fap
.params
[0]);
2164 /* Create sysfs interface */
2165 error
= device_create_file(&(hidpp
->hid_dev
->dev
), &dev_attr_range
);
2167 hid_warn(hidpp
->hid_dev
, "Unable to create sysfs interface for \"range\", errno %d!\n", error
);
2169 /* Read the current gain values */
2170 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
2171 HIDPP_FF_GET_GLOBAL_GAINS
, NULL
, 0, &response
);
2173 hid_warn(hidpp
->hid_dev
, "Failed to read gain values from device!\n");
2174 data
->gain
= error
? 0xffff : get_unaligned_be16(&response
.fap
.params
[0]);
2175 /* ignore boost value at response.fap.params[2] */
2177 /* init the hardware command queue */
2178 atomic_set(&data
->workqueue_size
, 0);
2180 /* initialize with zero autocenter to get wheel in usable state */
2181 hidpp_ff_set_autocenter(dev
, 0);
2183 hid_info(hid
, "Force feedback support loaded (firmware release %d).\n",
2189 static int hidpp_ff_deinit(struct hid_device
*hid
)
2191 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
2192 struct input_dev
*dev
= hidinput
->input
;
2193 struct hidpp_ff_private_data
*data
;
2196 hid_err(hid
, "Struct input_dev not found!\n");
2200 hid_info(hid
, "Unloading HID++ force feedback.\n");
2201 data
= dev
->ff
->private;
2203 hid_err(hid
, "Private data not found!\n");
2207 destroy_workqueue(data
->wq
);
2208 device_remove_file(&hid
->dev
, &dev_attr_range
);
2214 /* ************************************************************************** */
2216 /* Device Support */
2218 /* ************************************************************************** */
2220 /* -------------------------------------------------------------------------- */
2221 /* Touchpad HID++ devices */
2222 /* -------------------------------------------------------------------------- */
2224 #define WTP_MANUAL_RESOLUTION 39
2227 struct input_dev
*input
;
2230 u8 mt_feature_index
;
2231 u8 button_feature_index
;
2234 unsigned int resolution
;
2237 static int wtp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2238 struct hid_field
*field
, struct hid_usage
*usage
,
2239 unsigned long **bit
, int *max
)
2244 static void wtp_populate_input(struct hidpp_device
*hidpp
,
2245 struct input_dev
*input_dev
, bool origin_is_hid_core
)
2247 struct wtp_data
*wd
= hidpp
->private_data
;
2249 __set_bit(EV_ABS
, input_dev
->evbit
);
2250 __set_bit(EV_KEY
, input_dev
->evbit
);
2251 __clear_bit(EV_REL
, input_dev
->evbit
);
2252 __clear_bit(EV_LED
, input_dev
->evbit
);
2254 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, wd
->x_size
, 0, 0);
2255 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
, wd
->resolution
);
2256 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, wd
->y_size
, 0, 0);
2257 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
, wd
->resolution
);
2259 /* Max pressure is not given by the devices, pick one */
2260 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 50, 0, 0);
2262 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
2264 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
)
2265 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
2267 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
2269 input_mt_init_slots(input_dev
, wd
->maxcontacts
, INPUT_MT_POINTER
|
2270 INPUT_MT_DROP_UNUSED
);
2272 wd
->input
= input_dev
;
2275 static void wtp_touch_event(struct wtp_data
*wd
,
2276 struct hidpp_touchpad_raw_xy_finger
*touch_report
)
2280 if (!touch_report
->finger_id
|| touch_report
->contact_type
)
2281 /* no actual data */
2284 slot
= input_mt_get_slot_by_key(wd
->input
, touch_report
->finger_id
);
2286 input_mt_slot(wd
->input
, slot
);
2287 input_mt_report_slot_state(wd
->input
, MT_TOOL_FINGER
,
2288 touch_report
->contact_status
);
2289 if (touch_report
->contact_status
) {
2290 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_X
,
2292 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_Y
,
2293 wd
->flip_y
? wd
->y_size
- touch_report
->y
:
2295 input_event(wd
->input
, EV_ABS
, ABS_MT_PRESSURE
,
2296 touch_report
->area
);
2300 static void wtp_send_raw_xy_event(struct hidpp_device
*hidpp
,
2301 struct hidpp_touchpad_raw_xy
*raw
)
2303 struct wtp_data
*wd
= hidpp
->private_data
;
2306 for (i
= 0; i
< 2; i
++)
2307 wtp_touch_event(wd
, &(raw
->fingers
[i
]));
2309 if (raw
->end_of_frame
&&
2310 !(hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
))
2311 input_event(wd
->input
, EV_KEY
, BTN_LEFT
, raw
->button
);
2313 if (raw
->end_of_frame
|| raw
->finger_count
<= 2) {
2314 input_mt_sync_frame(wd
->input
);
2315 input_sync(wd
->input
);
2319 static int wtp_mouse_raw_xy_event(struct hidpp_device
*hidpp
, u8
*data
)
2321 struct wtp_data
*wd
= hidpp
->private_data
;
2322 u8 c1_area
= ((data
[7] & 0xf) * (data
[7] & 0xf) +
2323 (data
[7] >> 4) * (data
[7] >> 4)) / 2;
2324 u8 c2_area
= ((data
[13] & 0xf) * (data
[13] & 0xf) +
2325 (data
[13] >> 4) * (data
[13] >> 4)) / 2;
2326 struct hidpp_touchpad_raw_xy raw
= {
2327 .timestamp
= data
[1],
2331 .contact_status
= !!data
[7],
2332 .x
= get_unaligned_le16(&data
[3]),
2333 .y
= get_unaligned_le16(&data
[5]),
2336 .finger_id
= data
[2],
2339 .contact_status
= !!data
[13],
2340 .x
= get_unaligned_le16(&data
[9]),
2341 .y
= get_unaligned_le16(&data
[11]),
2344 .finger_id
= data
[8],
2347 .finger_count
= wd
->maxcontacts
,
2349 .end_of_frame
= (data
[0] >> 7) == 0,
2350 .button
= data
[0] & 0x01,
2353 wtp_send_raw_xy_event(hidpp
, &raw
);
2358 static int wtp_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
2360 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2361 struct wtp_data
*wd
= hidpp
->private_data
;
2362 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
2363 struct hidpp_touchpad_raw_xy raw
;
2365 if (!wd
|| !wd
->input
)
2371 hid_err(hdev
, "Received HID report of bad size (%d)",
2375 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
) {
2376 input_event(wd
->input
, EV_KEY
, BTN_LEFT
,
2377 !!(data
[1] & 0x01));
2378 input_event(wd
->input
, EV_KEY
, BTN_RIGHT
,
2379 !!(data
[1] & 0x02));
2380 input_sync(wd
->input
);
2385 return wtp_mouse_raw_xy_event(hidpp
, &data
[7]);
2387 case REPORT_ID_HIDPP_LONG
:
2388 /* size is already checked in hidpp_raw_event. */
2389 if ((report
->fap
.feature_index
!= wd
->mt_feature_index
) ||
2390 (report
->fap
.funcindex_clientid
!= EVENT_TOUCHPAD_RAW_XY
))
2392 hidpp_touchpad_raw_xy_event(hidpp
, data
+ 4, &raw
);
2394 wtp_send_raw_xy_event(hidpp
, &raw
);
2401 static int wtp_get_config(struct hidpp_device
*hidpp
)
2403 struct wtp_data
*wd
= hidpp
->private_data
;
2404 struct hidpp_touchpad_raw_info raw_info
= {0};
2408 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_TOUCHPAD_RAW_XY
,
2409 &wd
->mt_feature_index
, &feature_type
);
2411 /* means that the device is not powered up */
2414 ret
= hidpp_touchpad_get_raw_info(hidpp
, wd
->mt_feature_index
,
2419 wd
->x_size
= raw_info
.x_size
;
2420 wd
->y_size
= raw_info
.y_size
;
2421 wd
->maxcontacts
= raw_info
.maxcontacts
;
2422 wd
->flip_y
= raw_info
.origin
== TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT
;
2423 wd
->resolution
= raw_info
.res
;
2424 if (!wd
->resolution
)
2425 wd
->resolution
= WTP_MANUAL_RESOLUTION
;
2430 static int wtp_allocate(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2432 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2433 struct wtp_data
*wd
;
2435 wd
= devm_kzalloc(&hdev
->dev
, sizeof(struct wtp_data
),
2440 hidpp
->private_data
= wd
;
2445 static int wtp_connect(struct hid_device
*hdev
, bool connected
)
2447 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2448 struct wtp_data
*wd
= hidpp
->private_data
;
2452 ret
= wtp_get_config(hidpp
);
2454 hid_err(hdev
, "Can not get wtp config: %d\n", ret
);
2459 return hidpp_touchpad_set_raw_report_state(hidpp
, wd
->mt_feature_index
,
2463 /* ------------------------------------------------------------------------- */
2464 /* Logitech M560 devices */
2465 /* ------------------------------------------------------------------------- */
2468 * Logitech M560 protocol overview
2470 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2471 * the sides buttons are pressed, it sends some keyboard keys events
2472 * instead of buttons ones.
2473 * To complicate things further, the middle button keys sequence
2474 * is different from the odd press and the even press.
2476 * forward button -> Super_R
2477 * backward button -> Super_L+'d' (press only)
2478 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2479 * 2nd time: left-click (press only)
2480 * NB: press-only means that when the button is pressed, the
2481 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2482 * together sequentially; instead when the button is released, no event is
2486 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2487 * the mouse reacts differently:
2488 * - it never sends a keyboard key event
2489 * - for the three mouse button it sends:
2490 * middle button press 11<xx>0a 3500af00...
2491 * side 1 button (forward) press 11<xx>0a 3500b000...
2492 * side 2 button (backward) press 11<xx>0a 3500ae00...
2493 * middle/side1/side2 button release 11<xx>0a 35000000...
2496 static const u8 m560_config_parameter
[] = {0x00, 0xaf, 0x03};
2498 struct m560_private_data
{
2499 struct input_dev
*input
;
2502 /* how buttons are mapped in the report */
2503 #define M560_MOUSE_BTN_LEFT 0x01
2504 #define M560_MOUSE_BTN_RIGHT 0x02
2505 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2506 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2508 #define M560_SUB_ID 0x0a
2509 #define M560_BUTTON_MODE_REGISTER 0x35
2511 static int m560_send_config_command(struct hid_device
*hdev
, bool connected
)
2513 struct hidpp_report response
;
2514 struct hidpp_device
*hidpp_dev
;
2516 hidpp_dev
= hid_get_drvdata(hdev
);
2518 return hidpp_send_rap_command_sync(
2520 REPORT_ID_HIDPP_SHORT
,
2522 M560_BUTTON_MODE_REGISTER
,
2523 (u8
*)m560_config_parameter
,
2524 sizeof(m560_config_parameter
),
2529 static int m560_allocate(struct hid_device
*hdev
)
2531 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2532 struct m560_private_data
*d
;
2534 d
= devm_kzalloc(&hdev
->dev
, sizeof(struct m560_private_data
),
2539 hidpp
->private_data
= d
;
2544 static int m560_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
2546 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2547 struct m560_private_data
*mydata
= hidpp
->private_data
;
2550 if (!mydata
|| !mydata
->input
) {
2551 hid_err(hdev
, "error in parameter\n");
2556 hid_err(hdev
, "error in report\n");
2560 if (data
[0] == REPORT_ID_HIDPP_LONG
&&
2561 data
[2] == M560_SUB_ID
&& data
[6] == 0x00) {
2563 * m560 mouse report for middle, forward and backward button
2566 * data[1] = device-id
2568 * data[5] = 0xaf -> middle
2571 * 0x00 -> release all
2577 input_report_key(mydata
->input
, BTN_MIDDLE
, 1);
2580 input_report_key(mydata
->input
, BTN_FORWARD
, 1);
2583 input_report_key(mydata
->input
, BTN_BACK
, 1);
2586 input_report_key(mydata
->input
, BTN_BACK
, 0);
2587 input_report_key(mydata
->input
, BTN_FORWARD
, 0);
2588 input_report_key(mydata
->input
, BTN_MIDDLE
, 0);
2591 hid_err(hdev
, "error in report\n");
2594 input_sync(mydata
->input
);
2596 } else if (data
[0] == 0x02) {
2598 * Logitech M560 mouse report
2600 * data[0] = type (0x02)
2601 * data[1..2] = buttons
2608 input_report_key(mydata
->input
, BTN_LEFT
,
2609 !!(data
[1] & M560_MOUSE_BTN_LEFT
));
2610 input_report_key(mydata
->input
, BTN_RIGHT
,
2611 !!(data
[1] & M560_MOUSE_BTN_RIGHT
));
2613 if (data
[1] & M560_MOUSE_BTN_WHEEL_LEFT
) {
2614 input_report_rel(mydata
->input
, REL_HWHEEL
, -1);
2615 input_report_rel(mydata
->input
, REL_HWHEEL_HI_RES
,
2617 } else if (data
[1] & M560_MOUSE_BTN_WHEEL_RIGHT
) {
2618 input_report_rel(mydata
->input
, REL_HWHEEL
, 1);
2619 input_report_rel(mydata
->input
, REL_HWHEEL_HI_RES
,
2623 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 0, 12), 12);
2624 input_report_rel(mydata
->input
, REL_X
, v
);
2626 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 12, 12), 12);
2627 input_report_rel(mydata
->input
, REL_Y
, v
);
2629 v
= hid_snto32(data
[6], 8);
2631 hidpp_scroll_counter_handle_scroll(
2632 &hidpp
->vertical_wheel_counter
, v
);
2634 input_sync(mydata
->input
);
2640 static void m560_populate_input(struct hidpp_device
*hidpp
,
2641 struct input_dev
*input_dev
, bool origin_is_hid_core
)
2643 struct m560_private_data
*mydata
= hidpp
->private_data
;
2645 mydata
->input
= input_dev
;
2647 __set_bit(EV_KEY
, mydata
->input
->evbit
);
2648 __set_bit(BTN_MIDDLE
, mydata
->input
->keybit
);
2649 __set_bit(BTN_RIGHT
, mydata
->input
->keybit
);
2650 __set_bit(BTN_LEFT
, mydata
->input
->keybit
);
2651 __set_bit(BTN_BACK
, mydata
->input
->keybit
);
2652 __set_bit(BTN_FORWARD
, mydata
->input
->keybit
);
2654 __set_bit(EV_REL
, mydata
->input
->evbit
);
2655 __set_bit(REL_X
, mydata
->input
->relbit
);
2656 __set_bit(REL_Y
, mydata
->input
->relbit
);
2657 __set_bit(REL_WHEEL
, mydata
->input
->relbit
);
2658 __set_bit(REL_HWHEEL
, mydata
->input
->relbit
);
2659 __set_bit(REL_WHEEL_HI_RES
, mydata
->input
->relbit
);
2660 __set_bit(REL_HWHEEL_HI_RES
, mydata
->input
->relbit
);
2663 static int m560_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2664 struct hid_field
*field
, struct hid_usage
*usage
,
2665 unsigned long **bit
, int *max
)
2670 /* ------------------------------------------------------------------------- */
2671 /* Logitech K400 devices */
2672 /* ------------------------------------------------------------------------- */
2675 * The Logitech K400 keyboard has an embedded touchpad which is seen
2676 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2677 * tap-to-click but the setting is not remembered accross reset, annoying some
2680 * We can toggle this feature from the host by using the feature 0x6010:
2684 struct k400_private_data
{
2688 static int k400_disable_tap_to_click(struct hidpp_device
*hidpp
)
2690 struct k400_private_data
*k400
= hidpp
->private_data
;
2691 struct hidpp_touchpad_fw_items items
= {};
2695 if (!k400
->feature_index
) {
2696 ret
= hidpp_root_get_feature(hidpp
,
2697 HIDPP_PAGE_TOUCHPAD_FW_ITEMS
,
2698 &k400
->feature_index
, &feature_type
);
2700 /* means that the device is not powered up */
2704 ret
= hidpp_touchpad_fw_items_set(hidpp
, k400
->feature_index
, &items
);
2711 static int k400_allocate(struct hid_device
*hdev
)
2713 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2714 struct k400_private_data
*k400
;
2716 k400
= devm_kzalloc(&hdev
->dev
, sizeof(struct k400_private_data
),
2721 hidpp
->private_data
= k400
;
2726 static int k400_connect(struct hid_device
*hdev
, bool connected
)
2728 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2730 if (!disable_tap_to_click
)
2733 return k400_disable_tap_to_click(hidpp
);
2736 /* ------------------------------------------------------------------------- */
2737 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2738 /* ------------------------------------------------------------------------- */
2740 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2742 static int g920_get_config(struct hidpp_device
*hidpp
)
2748 /* Find feature and store for later use */
2749 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_G920_FORCE_FEEDBACK
,
2750 &feature_index
, &feature_type
);
2754 ret
= hidpp_ff_init(hidpp
, feature_index
);
2756 hid_warn(hidpp
->hid_dev
, "Unable to initialize force feedback support, errno %d\n",
2762 /* -------------------------------------------------------------------------- */
2763 /* High-resolution scroll wheels */
2764 /* -------------------------------------------------------------------------- */
2766 static int hi_res_scroll_enable(struct hidpp_device
*hidpp
)
2771 if (hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL_X2121
) {
2772 ret
= hidpp_hrw_set_wheel_mode(hidpp
, false, true, false);
2774 ret
= hidpp_hrw_get_wheel_capability(hidpp
, &multiplier
);
2775 } else if (hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL_X2120
) {
2776 ret
= hidpp_hrs_set_highres_scrolling_mode(hidpp
, true,
2778 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
2779 ret
= hidpp10_enable_scrolling_acceleration(hidpp
);
2785 if (multiplier
== 0)
2788 hidpp
->vertical_wheel_counter
.wheel_multiplier
= multiplier
;
2789 hid_info(hidpp
->hid_dev
, "multiplier = %d\n", multiplier
);
2793 /* -------------------------------------------------------------------------- */
2794 /* Generic HID++ devices */
2795 /* -------------------------------------------------------------------------- */
2797 static int hidpp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2798 struct hid_field
*field
, struct hid_usage
*usage
,
2799 unsigned long **bit
, int *max
)
2801 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2803 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2804 return wtp_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
2805 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
&&
2806 field
->application
!= HID_GD_MOUSE
)
2807 return m560_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
2812 static int hidpp_input_mapped(struct hid_device
*hdev
, struct hid_input
*hi
,
2813 struct hid_field
*field
, struct hid_usage
*usage
,
2814 unsigned long **bit
, int *max
)
2816 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2818 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2819 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
2820 if (usage
->type
== EV_ABS
&& (usage
->code
== ABS_X
||
2821 usage
->code
== ABS_Y
|| usage
->code
== ABS_Z
||
2822 usage
->code
== ABS_RZ
)) {
2823 field
->application
= HID_GD_MULTIAXIS
;
2831 static void hidpp_populate_input(struct hidpp_device
*hidpp
,
2832 struct input_dev
*input
, bool origin_is_hid_core
)
2834 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2835 wtp_populate_input(hidpp
, input
, origin_is_hid_core
);
2836 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
2837 m560_populate_input(hidpp
, input
, origin_is_hid_core
);
2839 if (hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL
)
2840 hidpp
->vertical_wheel_counter
.dev
= input
;
2843 static int hidpp_input_configured(struct hid_device
*hdev
,
2844 struct hid_input
*hidinput
)
2846 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2847 struct input_dev
*input
= hidinput
->input
;
2849 hidpp_populate_input(hidpp
, input
, true);
2854 static int hidpp_raw_hidpp_event(struct hidpp_device
*hidpp
, u8
*data
,
2857 struct hidpp_report
*question
= hidpp
->send_receive_buf
;
2858 struct hidpp_report
*answer
= hidpp
->send_receive_buf
;
2859 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
2863 * If the mutex is locked then we have a pending answer from a
2864 * previously sent command.
2866 if (unlikely(mutex_is_locked(&hidpp
->send_mutex
))) {
2868 * Check for a correct hidpp20 answer or the corresponding
2871 if (hidpp_match_answer(question
, report
) ||
2872 hidpp_match_error(question
, report
)) {
2874 hidpp
->answer_available
= true;
2875 wake_up(&hidpp
->wait
);
2877 * This was an answer to a command that this driver sent
2878 * We return 1 to hid-core to avoid forwarding the
2879 * command upstream as it has been treated by the driver
2886 if (unlikely(hidpp_report_is_connect_event(report
))) {
2887 atomic_set(&hidpp
->connected
,
2888 !(report
->rap
.params
[0] & (1 << 6)));
2889 if (schedule_work(&hidpp
->work
) == 0)
2890 dbg_hid("%s: connect event already queued\n", __func__
);
2894 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP20_BATTERY
) {
2895 ret
= hidpp20_battery_event(hidpp
, data
, size
);
2898 ret
= hidpp_solar_battery_event(hidpp
, data
, size
);
2903 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP10_BATTERY
) {
2904 ret
= hidpp10_battery_event(hidpp
, data
, size
);
2912 static int hidpp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
2915 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2918 /* Generic HID++ processing. */
2920 case REPORT_ID_HIDPP_VERY_LONG
:
2921 if (size
!= HIDPP_REPORT_VERY_LONG_LENGTH
) {
2922 hid_err(hdev
, "received hid++ report of bad size (%d)",
2926 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2928 case REPORT_ID_HIDPP_LONG
:
2929 if (size
!= HIDPP_REPORT_LONG_LENGTH
) {
2930 hid_err(hdev
, "received hid++ report of bad size (%d)",
2934 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2936 case REPORT_ID_HIDPP_SHORT
:
2937 if (size
!= HIDPP_REPORT_SHORT_LENGTH
) {
2938 hid_err(hdev
, "received hid++ report of bad size (%d)",
2942 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2946 /* If no report is available for further processing, skip calling
2947 * raw_event of subclasses. */
2951 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2952 return wtp_raw_event(hdev
, data
, size
);
2953 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
2954 return m560_raw_event(hdev
, data
, size
);
2959 static int hidpp_event(struct hid_device
*hdev
, struct hid_field
*field
,
2960 struct hid_usage
*usage
, __s32 value
)
2962 /* This function will only be called for scroll events, due to the
2963 * restriction imposed in hidpp_usages.
2965 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2966 struct hidpp_scroll_counter
*counter
= &hidpp
->vertical_wheel_counter
;
2967 /* A scroll event may occur before the multiplier has been retrieved or
2968 * the input device set, or high-res scroll enabling may fail. In such
2969 * cases we must return early (falling back to default behaviour) to
2970 * avoid a crash in hidpp_scroll_counter_handle_scroll.
2972 if (!(hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL
) || value
== 0
2973 || counter
->dev
== NULL
|| counter
->wheel_multiplier
== 0)
2976 hidpp_scroll_counter_handle_scroll(counter
, value
);
2980 static int hidpp_initialize_battery(struct hidpp_device
*hidpp
)
2982 static atomic_t battery_no
= ATOMIC_INIT(0);
2983 struct power_supply_config cfg
= { .drv_data
= hidpp
};
2984 struct power_supply_desc
*desc
= &hidpp
->battery
.desc
;
2985 enum power_supply_property
*battery_props
;
2986 struct hidpp_battery
*battery
;
2987 unsigned int num_battery_props
;
2991 if (hidpp
->battery
.ps
)
2994 hidpp
->battery
.feature_index
= 0xff;
2995 hidpp
->battery
.solar_feature_index
= 0xff;
2997 if (hidpp
->protocol_major
>= 2) {
2998 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K750
)
2999 ret
= hidpp_solar_request_battery_event(hidpp
);
3001 ret
= hidpp20_query_battery_info(hidpp
);
3005 hidpp
->capabilities
|= HIDPP_CAPABILITY_HIDPP20_BATTERY
;
3007 ret
= hidpp10_query_battery_status(hidpp
);
3009 ret
= hidpp10_query_battery_mileage(hidpp
);
3012 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
3014 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
;
3016 hidpp
->capabilities
|= HIDPP_CAPABILITY_HIDPP10_BATTERY
;
3019 battery_props
= devm_kmemdup(&hidpp
->hid_dev
->dev
,
3020 hidpp_battery_props
,
3021 sizeof(hidpp_battery_props
),
3026 num_battery_props
= ARRAY_SIZE(hidpp_battery_props
) - 2;
3028 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_MILEAGE
)
3029 battery_props
[num_battery_props
++] =
3030 POWER_SUPPLY_PROP_CAPACITY
;
3032 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
)
3033 battery_props
[num_battery_props
++] =
3034 POWER_SUPPLY_PROP_CAPACITY_LEVEL
;
3036 battery
= &hidpp
->battery
;
3038 n
= atomic_inc_return(&battery_no
) - 1;
3039 desc
->properties
= battery_props
;
3040 desc
->num_properties
= num_battery_props
;
3041 desc
->get_property
= hidpp_battery_get_property
;
3042 sprintf(battery
->name
, "hidpp_battery_%ld", n
);
3043 desc
->name
= battery
->name
;
3044 desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
3045 desc
->use_for_apm
= 0;
3047 battery
->ps
= devm_power_supply_register(&hidpp
->hid_dev
->dev
,
3050 if (IS_ERR(battery
->ps
))
3051 return PTR_ERR(battery
->ps
);
3053 power_supply_powers(battery
->ps
, &hidpp
->hid_dev
->dev
);
3058 static void hidpp_overwrite_name(struct hid_device
*hdev
)
3060 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3063 if (hidpp
->protocol_major
< 2)
3066 name
= hidpp_get_device_name(hidpp
);
3069 hid_err(hdev
, "unable to retrieve the name of the device");
3071 dbg_hid("HID++: Got name: %s\n", name
);
3072 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
3078 static int hidpp_input_open(struct input_dev
*dev
)
3080 struct hid_device
*hid
= input_get_drvdata(dev
);
3082 return hid_hw_open(hid
);
3085 static void hidpp_input_close(struct input_dev
*dev
)
3087 struct hid_device
*hid
= input_get_drvdata(dev
);
3092 static struct input_dev
*hidpp_allocate_input(struct hid_device
*hdev
)
3094 struct input_dev
*input_dev
= devm_input_allocate_device(&hdev
->dev
);
3095 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3100 input_set_drvdata(input_dev
, hdev
);
3101 input_dev
->open
= hidpp_input_open
;
3102 input_dev
->close
= hidpp_input_close
;
3104 input_dev
->name
= hidpp
->name
;
3105 input_dev
->phys
= hdev
->phys
;
3106 input_dev
->uniq
= hdev
->uniq
;
3107 input_dev
->id
.bustype
= hdev
->bus
;
3108 input_dev
->id
.vendor
= hdev
->vendor
;
3109 input_dev
->id
.product
= hdev
->product
;
3110 input_dev
->id
.version
= hdev
->version
;
3111 input_dev
->dev
.parent
= &hdev
->dev
;
3116 static void hidpp_connect_event(struct hidpp_device
*hidpp
)
3118 struct hid_device
*hdev
= hidpp
->hid_dev
;
3120 bool connected
= atomic_read(&hidpp
->connected
);
3121 struct input_dev
*input
;
3122 char *name
, *devm_name
;
3125 if (hidpp
->battery
.ps
) {
3126 hidpp
->battery
.online
= false;
3127 hidpp
->battery
.status
= POWER_SUPPLY_STATUS_UNKNOWN
;
3128 hidpp
->battery
.level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
3129 power_supply_changed(hidpp
->battery
.ps
);
3134 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
3135 ret
= wtp_connect(hdev
, connected
);
3138 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
3139 ret
= m560_send_config_command(hdev
, connected
);
3142 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
3143 ret
= k400_connect(hdev
, connected
);
3148 /* the device is already connected, we can ask for its name and
3150 if (!hidpp
->protocol_major
) {
3151 ret
= !hidpp_is_connected(hidpp
);
3153 hid_err(hdev
, "Can not get the protocol version.\n");
3156 hid_info(hdev
, "HID++ %u.%u device connected.\n",
3157 hidpp
->protocol_major
, hidpp
->protocol_minor
);
3160 if (hidpp
->name
== hdev
->name
&& hidpp
->protocol_major
>= 2) {
3161 name
= hidpp_get_device_name(hidpp
);
3164 "unable to retrieve the name of the device");
3168 devm_name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s", name
);
3173 hidpp
->name
= devm_name
;
3176 hidpp_initialize_battery(hidpp
);
3178 /* forward current battery state */
3179 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP10_BATTERY
) {
3180 hidpp10_enable_battery_reporting(hidpp
);
3181 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_MILEAGE
)
3182 hidpp10_query_battery_mileage(hidpp
);
3184 hidpp10_query_battery_status(hidpp
);
3185 } else if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP20_BATTERY
) {
3186 hidpp20_query_battery_info(hidpp
);
3188 if (hidpp
->battery
.ps
)
3189 power_supply_changed(hidpp
->battery
.ps
);
3191 if (hidpp
->quirks
& HIDPP_QUIRK_HI_RES_SCROLL
)
3192 hi_res_scroll_enable(hidpp
);
3194 if (!(hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
) || hidpp
->delayed_input
)
3195 /* if the input nodes are already created, we can stop now */
3198 input
= hidpp_allocate_input(hdev
);
3200 hid_err(hdev
, "cannot allocate new input device: %d\n", ret
);
3204 hidpp_populate_input(hidpp
, input
, false);
3206 ret
= input_register_device(input
);
3208 input_free_device(input
);
3210 hidpp
->delayed_input
= input
;
3213 static DEVICE_ATTR(builtin_power_supply
, 0000, NULL
, NULL
);
3215 static struct attribute
*sysfs_attrs
[] = {
3216 &dev_attr_builtin_power_supply
.attr
,
3220 static const struct attribute_group ps_attribute_group
= {
3221 .attrs
= sysfs_attrs
3224 static int hidpp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
3226 struct hidpp_device
*hidpp
;
3229 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
3231 hidpp
= devm_kzalloc(&hdev
->dev
, sizeof(struct hidpp_device
),
3236 hidpp
->hid_dev
= hdev
;
3237 hidpp
->name
= hdev
->name
;
3238 hid_set_drvdata(hdev
, hidpp
);
3240 hidpp
->quirks
= id
->driver_data
;
3242 if (id
->group
== HID_GROUP_LOGITECH_DJ_DEVICE
)
3243 hidpp
->quirks
|= HIDPP_QUIRK_UNIFYING
;
3245 if (disable_raw_mode
) {
3246 hidpp
->quirks
&= ~HIDPP_QUIRK_CLASS_WTP
;
3247 hidpp
->quirks
&= ~HIDPP_QUIRK_NO_HIDINPUT
;
3250 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
3251 ret
= wtp_allocate(hdev
, id
);
3254 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
3255 ret
= m560_allocate(hdev
);
3258 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
3259 ret
= k400_allocate(hdev
);
3264 INIT_WORK(&hidpp
->work
, delayed_work_cb
);
3265 mutex_init(&hidpp
->send_mutex
);
3266 init_waitqueue_head(&hidpp
->wait
);
3268 /* indicates we are handling the battery properties in the kernel */
3269 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
3271 hid_warn(hdev
, "Cannot allocate sysfs group for %s\n",
3274 ret
= hid_parse(hdev
);
3276 hid_err(hdev
, "%s:parse failed\n", __func__
);
3277 goto hid_parse_fail
;
3280 if (hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
)
3281 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
3283 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
3284 ret
= hid_hw_start(hdev
, connect_mask
);
3286 hid_err(hdev
, "hw start failed\n");
3287 goto hid_hw_start_fail
;
3289 ret
= hid_hw_open(hdev
);
3291 dev_err(&hdev
->dev
, "%s:hid_hw_open returned error:%d\n",
3294 goto hid_hw_start_fail
;
3299 /* Allow incoming packets */
3300 hid_device_io_start(hdev
);
3302 if (hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)
3303 hidpp_unifying_init(hidpp
);
3305 connected
= hidpp_is_connected(hidpp
);
3306 atomic_set(&hidpp
->connected
, connected
);
3307 if (!(hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)) {
3310 hid_err(hdev
, "Device not connected");
3311 goto hid_hw_open_failed
;
3314 hid_info(hdev
, "HID++ %u.%u device connected.\n",
3315 hidpp
->protocol_major
, hidpp
->protocol_minor
);
3317 hidpp_overwrite_name(hdev
);
3320 if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)) {
3321 ret
= wtp_get_config(hidpp
);
3323 goto hid_hw_open_failed
;
3324 } else if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
)) {
3325 ret
= g920_get_config(hidpp
);
3327 goto hid_hw_open_failed
;
3330 /* Block incoming packets */
3331 hid_device_io_stop(hdev
);
3333 if (!(hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
)) {
3334 ret
= hid_hw_start(hdev
, connect_mask
);
3336 hid_err(hdev
, "%s:hid_hw_start returned error\n", __func__
);
3337 goto hid_hw_start_fail
;
3341 /* Allow incoming packets */
3342 hid_device_io_start(hdev
);
3344 hidpp_connect_event(hidpp
);
3349 hid_device_io_stop(hdev
);
3350 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
3356 sysfs_remove_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
3357 cancel_work_sync(&hidpp
->work
);
3358 mutex_destroy(&hidpp
->send_mutex
);
3360 hid_set_drvdata(hdev
, NULL
);
3364 static void hidpp_remove(struct hid_device
*hdev
)
3366 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3368 sysfs_remove_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
3370 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
3371 hidpp_ff_deinit(hdev
);
3375 cancel_work_sync(&hidpp
->work
);
3376 mutex_destroy(&hidpp
->send_mutex
);
3379 #define LDJ_DEVICE(product) \
3380 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3381 USB_VENDOR_ID_LOGITECH, (product))
3383 static const struct hid_device_id hidpp_devices
[] = {
3384 { /* wireless touchpad */
3386 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
|
3387 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
},
3388 { /* wireless touchpad T650 */
3390 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
},
3391 { /* wireless touchpad T651 */
3392 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
,
3393 USB_DEVICE_ID_LOGITECH_T651
),
3394 .driver_data
= HIDPP_QUIRK_CLASS_WTP
},
3395 { /* Mouse Logitech Anywhere MX */
3396 LDJ_DEVICE(0x1017), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_1P0
},
3397 { /* Mouse Logitech Cube */
3398 LDJ_DEVICE(0x4010), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2120
},
3399 { /* Mouse Logitech M335 */
3400 LDJ_DEVICE(0x4050), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3401 { /* Mouse Logitech M515 */
3402 LDJ_DEVICE(0x4007), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2120
},
3403 { /* Mouse logitech M560 */
3405 .driver_data
= HIDPP_QUIRK_DELAYED_INIT
| HIDPP_QUIRK_CLASS_M560
3406 | HIDPP_QUIRK_HI_RES_SCROLL_X2120
},
3407 { /* Mouse Logitech M705 (firmware RQM17) */
3408 LDJ_DEVICE(0x101b), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_1P0
},
3409 { /* Mouse Logitech M705 (firmware RQM67) */
3410 LDJ_DEVICE(0x406d), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3411 { /* Mouse Logitech M720 */
3412 LDJ_DEVICE(0x405e), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3413 { /* Mouse Logitech MX Anywhere 2 */
3414 LDJ_DEVICE(0x404a), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3415 { LDJ_DEVICE(0xb013), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3416 { LDJ_DEVICE(0xb018), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3417 { LDJ_DEVICE(0xb01f), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3418 { /* Mouse Logitech MX Anywhere 2S */
3419 LDJ_DEVICE(0x406a), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3420 { /* Mouse Logitech MX Master */
3421 LDJ_DEVICE(0x4041), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3422 { LDJ_DEVICE(0x4060), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3423 { LDJ_DEVICE(0x4071), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3424 { /* Mouse Logitech MX Master 2S */
3425 LDJ_DEVICE(0x4069), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_X2121
},
3426 { /* Mouse Logitech Performance MX */
3427 LDJ_DEVICE(0x101a), .driver_data
= HIDPP_QUIRK_HI_RES_SCROLL_1P0
},
3428 { /* Keyboard logitech K400 */
3430 .driver_data
= HIDPP_QUIRK_CLASS_K400
},
3431 { /* Solar Keyboard Logitech K750 */
3433 .driver_data
= HIDPP_QUIRK_CLASS_K750
},
3435 { LDJ_DEVICE(HID_ANY_ID
) },
3437 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, USB_DEVICE_ID_LOGITECH_G920_WHEEL
),
3438 .driver_data
= HIDPP_QUIRK_CLASS_G920
| HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
},
3442 MODULE_DEVICE_TABLE(hid
, hidpp_devices
);
3444 static const struct hid_usage_id hidpp_usages
[] = {
3445 { HID_GD_WHEEL
, EV_REL
, REL_WHEEL_HI_RES
},
3446 { HID_ANY_ID
- 1, HID_ANY_ID
- 1, HID_ANY_ID
- 1}
3449 static struct hid_driver hidpp_driver
= {
3450 .name
= "logitech-hidpp-device",
3451 .id_table
= hidpp_devices
,
3452 .probe
= hidpp_probe
,
3453 .remove
= hidpp_remove
,
3454 .raw_event
= hidpp_raw_event
,
3455 .usage_table
= hidpp_usages
,
3456 .event
= hidpp_event
,
3457 .input_configured
= hidpp_input_configured
,
3458 .input_mapping
= hidpp_input_mapping
,
3459 .input_mapped
= hidpp_input_mapped
,
3462 module_hid_driver(hidpp_driver
);