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/kfifo.h>
25 #include <linux/input/mt.h>
26 #include <linux/workqueue.h>
27 #include <linux/atomic.h>
28 #include <linux/fixp-arith.h>
29 #include <asm/unaligned.h>
30 #include "usbhid/usbhid.h"
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
35 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
37 static bool disable_raw_mode
;
38 module_param(disable_raw_mode
, bool, 0644);
39 MODULE_PARM_DESC(disable_raw_mode
,
40 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
42 static bool disable_tap_to_click
;
43 module_param(disable_tap_to_click
, bool, 0644);
44 MODULE_PARM_DESC(disable_tap_to_click
,
45 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
47 #define REPORT_ID_HIDPP_SHORT 0x10
48 #define REPORT_ID_HIDPP_LONG 0x11
49 #define REPORT_ID_HIDPP_VERY_LONG 0x12
51 #define HIDPP_REPORT_SHORT_LENGTH 7
52 #define HIDPP_REPORT_LONG_LENGTH 20
53 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
55 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
56 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
57 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
58 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
60 /* bits 2..20 are reserved for classes */
61 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
62 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
63 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
64 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
65 #define HIDPP_QUIRK_UNIFYING BIT(25)
67 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
69 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
70 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
73 * There are two hidpp protocols in use, the first version hidpp10 is known
74 * as register access protocol or RAP, the second version hidpp20 is known as
75 * feature access protocol or FAP
77 * Most older devices (including the Unifying usb receiver) use the RAP protocol
78 * where as most newer devices use the FAP protocol. Both protocols are
79 * compatible with the underlying transport, which could be usb, Unifiying, or
80 * bluetooth. The message lengths are defined by the hid vendor specific report
81 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
82 * the HIDPP_LONG report type (total message length 20 bytes)
84 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
85 * messages. The Unifying receiver itself responds to RAP messages (device index
86 * is 0xFF for the receiver), and all messages (short or long) with a device
87 * index between 1 and 6 are passed untouched to the corresponding paired
90 * The paired device can be RAP or FAP, it will receive the message untouched
91 * from the Unifiying receiver.
96 u8 funcindex_clientid
;
97 u8 params
[HIDPP_REPORT_VERY_LONG_LENGTH
- 4U];
103 u8 params
[HIDPP_REPORT_VERY_LONG_LENGTH
- 4U];
106 struct hidpp_report
{
112 u8 rawbytes
[sizeof(struct fap
)];
116 struct hidpp_battery
{
118 struct power_supply_desc desc
;
119 struct power_supply
*ps
;
125 struct hidpp_device
{
126 struct hid_device
*hid_dev
;
127 struct mutex send_mutex
;
128 void *send_receive_buf
;
129 char *name
; /* will never be NULL and should not be freed */
130 wait_queue_head_t wait
;
131 bool answer_available
;
137 struct work_struct work
;
138 struct kfifo delayed_work_fifo
;
140 struct input_dev
*delayed_input
;
142 unsigned long quirks
;
143 unsigned long capabilities
;
145 struct hidpp_battery battery
;
148 /* HID++ 1.0 error codes */
149 #define HIDPP_ERROR 0x8f
150 #define HIDPP_ERROR_SUCCESS 0x00
151 #define HIDPP_ERROR_INVALID_SUBID 0x01
152 #define HIDPP_ERROR_INVALID_ADRESS 0x02
153 #define HIDPP_ERROR_INVALID_VALUE 0x03
154 #define HIDPP_ERROR_CONNECT_FAIL 0x04
155 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
156 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
157 #define HIDPP_ERROR_BUSY 0x07
158 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
159 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
160 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
161 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
162 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
163 /* HID++ 2.0 error codes */
164 #define HIDPP20_ERROR 0xff
166 static void hidpp_connect_event(struct hidpp_device
*hidpp_dev
);
168 static int __hidpp_send_report(struct hid_device
*hdev
,
169 struct hidpp_report
*hidpp_report
)
171 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
172 int fields_count
, ret
;
174 hidpp
= hid_get_drvdata(hdev
);
176 switch (hidpp_report
->report_id
) {
177 case REPORT_ID_HIDPP_SHORT
:
178 fields_count
= HIDPP_REPORT_SHORT_LENGTH
;
180 case REPORT_ID_HIDPP_LONG
:
181 fields_count
= HIDPP_REPORT_LONG_LENGTH
;
183 case REPORT_ID_HIDPP_VERY_LONG
:
184 fields_count
= HIDPP_REPORT_VERY_LONG_LENGTH
;
191 * set the device_index as the receiver, it will be overwritten by
192 * hid_hw_request if needed
194 hidpp_report
->device_index
= 0xff;
196 if (hidpp
->quirks
& HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
) {
197 ret
= hid_hw_output_report(hdev
, (u8
*)hidpp_report
, fields_count
);
199 ret
= hid_hw_raw_request(hdev
, hidpp_report
->report_id
,
200 (u8
*)hidpp_report
, fields_count
, HID_OUTPUT_REPORT
,
204 return ret
== fields_count
? 0 : -1;
208 * hidpp_send_message_sync() returns 0 in case of success, and something else
209 * in case of a failure.
210 * - If ' something else' is positive, that means that an error has been raised
211 * by the protocol itself.
212 * - If ' something else' is negative, that means that we had a classic error
213 * (-ENOMEM, -EPIPE, etc...)
215 static int hidpp_send_message_sync(struct hidpp_device
*hidpp
,
216 struct hidpp_report
*message
,
217 struct hidpp_report
*response
)
221 mutex_lock(&hidpp
->send_mutex
);
223 hidpp
->send_receive_buf
= response
;
224 hidpp
->answer_available
= false;
227 * So that we can later validate the answer when it arrives
230 *response
= *message
;
232 ret
= __hidpp_send_report(hidpp
->hid_dev
, message
);
235 dbg_hid("__hidpp_send_report returned err: %d\n", ret
);
236 memset(response
, 0, sizeof(struct hidpp_report
));
240 if (!wait_event_timeout(hidpp
->wait
, hidpp
->answer_available
,
242 dbg_hid("%s:timeout waiting for response\n", __func__
);
243 memset(response
, 0, sizeof(struct hidpp_report
));
247 if (response
->report_id
== REPORT_ID_HIDPP_SHORT
&&
248 response
->rap
.sub_id
== HIDPP_ERROR
) {
249 ret
= response
->rap
.params
[1];
250 dbg_hid("%s:got hidpp error %02X\n", __func__
, ret
);
254 if ((response
->report_id
== REPORT_ID_HIDPP_LONG
||
255 response
->report_id
== REPORT_ID_HIDPP_VERY_LONG
) &&
256 response
->fap
.feature_index
== HIDPP20_ERROR
) {
257 ret
= response
->fap
.params
[1];
258 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__
, ret
);
263 mutex_unlock(&hidpp
->send_mutex
);
268 static int hidpp_send_fap_command_sync(struct hidpp_device
*hidpp
,
269 u8 feat_index
, u8 funcindex_clientid
, u8
*params
, int param_count
,
270 struct hidpp_report
*response
)
272 struct hidpp_report
*message
;
275 if (param_count
> sizeof(message
->fap
.params
))
278 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
282 if (param_count
> (HIDPP_REPORT_LONG_LENGTH
- 4))
283 message
->report_id
= REPORT_ID_HIDPP_VERY_LONG
;
285 message
->report_id
= REPORT_ID_HIDPP_LONG
;
286 message
->fap
.feature_index
= feat_index
;
287 message
->fap
.funcindex_clientid
= funcindex_clientid
;
288 memcpy(&message
->fap
.params
, params
, param_count
);
290 ret
= hidpp_send_message_sync(hidpp
, message
, response
);
295 static int hidpp_send_rap_command_sync(struct hidpp_device
*hidpp_dev
,
296 u8 report_id
, u8 sub_id
, u8 reg_address
, u8
*params
, int param_count
,
297 struct hidpp_report
*response
)
299 struct hidpp_report
*message
;
303 case REPORT_ID_HIDPP_SHORT
:
304 max_count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
306 case REPORT_ID_HIDPP_LONG
:
307 max_count
= HIDPP_REPORT_LONG_LENGTH
- 4;
309 case REPORT_ID_HIDPP_VERY_LONG
:
310 max_count
= HIDPP_REPORT_VERY_LONG_LENGTH
- 4;
316 if (param_count
> max_count
)
319 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
322 message
->report_id
= report_id
;
323 message
->rap
.sub_id
= sub_id
;
324 message
->rap
.reg_address
= reg_address
;
325 memcpy(&message
->rap
.params
, params
, param_count
);
327 ret
= hidpp_send_message_sync(hidpp_dev
, message
, response
);
332 static void delayed_work_cb(struct work_struct
*work
)
334 struct hidpp_device
*hidpp
= container_of(work
, struct hidpp_device
,
336 hidpp_connect_event(hidpp
);
339 static inline bool hidpp_match_answer(struct hidpp_report
*question
,
340 struct hidpp_report
*answer
)
342 return (answer
->fap
.feature_index
== question
->fap
.feature_index
) &&
343 (answer
->fap
.funcindex_clientid
== question
->fap
.funcindex_clientid
);
346 static inline bool hidpp_match_error(struct hidpp_report
*question
,
347 struct hidpp_report
*answer
)
349 return ((answer
->rap
.sub_id
== HIDPP_ERROR
) ||
350 (answer
->fap
.feature_index
== HIDPP20_ERROR
)) &&
351 (answer
->fap
.funcindex_clientid
== question
->fap
.feature_index
) &&
352 (answer
->fap
.params
[0] == question
->fap
.funcindex_clientid
);
355 static inline bool hidpp_report_is_connect_event(struct hidpp_report
*report
)
357 return (report
->report_id
== REPORT_ID_HIDPP_SHORT
) &&
358 (report
->rap
.sub_id
== 0x41);
362 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
364 static void hidpp_prefix_name(char **name
, int name_length
)
366 #define PREFIX_LENGTH 9 /* "Logitech " */
371 if (name_length
> PREFIX_LENGTH
&&
372 strncmp(*name
, "Logitech ", PREFIX_LENGTH
) == 0)
373 /* The prefix has is already in the name */
376 new_length
= PREFIX_LENGTH
+ name_length
;
377 new_name
= kzalloc(new_length
, GFP_KERNEL
);
381 snprintf(new_name
, new_length
, "Logitech %s", *name
);
388 /* -------------------------------------------------------------------------- */
389 /* HIDP++ 1.0 commands */
390 /* -------------------------------------------------------------------------- */
392 #define HIDPP_SET_REGISTER 0x80
393 #define HIDPP_GET_REGISTER 0x81
394 #define HIDPP_SET_LONG_REGISTER 0x82
395 #define HIDPP_GET_LONG_REGISTER 0x83
397 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
398 #define HIDPP_EXTENDED_PAIRING 0x30
399 #define HIDPP_DEVICE_NAME 0x40
401 static char *hidpp_unifying_get_name(struct hidpp_device
*hidpp_dev
)
403 struct hidpp_report response
;
405 u8 params
[1] = { HIDPP_DEVICE_NAME
};
409 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
410 REPORT_ID_HIDPP_SHORT
,
411 HIDPP_GET_LONG_REGISTER
,
412 HIDPP_REG_PAIRING_INFORMATION
,
413 params
, 1, &response
);
417 len
= response
.rap
.params
[1];
419 if (2 + len
> sizeof(response
.rap
.params
))
422 name
= kzalloc(len
+ 1, GFP_KERNEL
);
426 memcpy(name
, &response
.rap
.params
[2], len
);
428 /* include the terminating '\0' */
429 hidpp_prefix_name(&name
, len
+ 1);
434 static int hidpp_unifying_get_serial(struct hidpp_device
*hidpp
, u32
*serial
)
436 struct hidpp_report response
;
438 u8 params
[1] = { HIDPP_EXTENDED_PAIRING
};
440 ret
= hidpp_send_rap_command_sync(hidpp
,
441 REPORT_ID_HIDPP_SHORT
,
442 HIDPP_GET_LONG_REGISTER
,
443 HIDPP_REG_PAIRING_INFORMATION
,
444 params
, 1, &response
);
449 * We don't care about LE or BE, we will output it as a string
450 * with %4phD, so we need to keep the order.
452 *serial
= *((u32
*)&response
.rap
.params
[1]);
456 static int hidpp_unifying_init(struct hidpp_device
*hidpp
)
458 struct hid_device
*hdev
= hidpp
->hid_dev
;
463 ret
= hidpp_unifying_get_serial(hidpp
, &serial
);
467 snprintf(hdev
->uniq
, sizeof(hdev
->uniq
), "%04x-%4phD",
468 hdev
->product
, &serial
);
469 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev
->uniq
);
471 name
= hidpp_unifying_get_name(hidpp
);
475 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
476 dbg_hid("HID++ Unifying: Got name: %s\n", name
);
482 /* -------------------------------------------------------------------------- */
484 /* -------------------------------------------------------------------------- */
486 #define HIDPP_PAGE_ROOT 0x0000
487 #define HIDPP_PAGE_ROOT_IDX 0x00
489 #define CMD_ROOT_GET_FEATURE 0x01
490 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
492 static int hidpp_root_get_feature(struct hidpp_device
*hidpp
, u16 feature
,
493 u8
*feature_index
, u8
*feature_type
)
495 struct hidpp_report response
;
497 u8 params
[2] = { feature
>> 8, feature
& 0x00FF };
499 ret
= hidpp_send_fap_command_sync(hidpp
,
501 CMD_ROOT_GET_FEATURE
,
502 params
, 2, &response
);
506 *feature_index
= response
.fap
.params
[0];
507 *feature_type
= response
.fap
.params
[1];
512 static int hidpp_root_get_protocol_version(struct hidpp_device
*hidpp
)
514 struct hidpp_report response
;
517 ret
= hidpp_send_fap_command_sync(hidpp
,
519 CMD_ROOT_GET_PROTOCOL_VERSION
,
522 if (ret
== HIDPP_ERROR_INVALID_SUBID
) {
523 hidpp
->protocol_major
= 1;
524 hidpp
->protocol_minor
= 0;
528 /* the device might not be connected */
529 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
533 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
540 hidpp
->protocol_major
= response
.fap
.params
[0];
541 hidpp
->protocol_minor
= response
.fap
.params
[1];
546 static bool hidpp_is_connected(struct hidpp_device
*hidpp
)
550 ret
= hidpp_root_get_protocol_version(hidpp
);
552 hid_dbg(hidpp
->hid_dev
, "HID++ %u.%u device connected.\n",
553 hidpp
->protocol_major
, hidpp
->protocol_minor
);
557 /* -------------------------------------------------------------------------- */
558 /* 0x0005: GetDeviceNameType */
559 /* -------------------------------------------------------------------------- */
561 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
563 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
564 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
565 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
567 static int hidpp_devicenametype_get_count(struct hidpp_device
*hidpp
,
568 u8 feature_index
, u8
*nameLength
)
570 struct hidpp_report response
;
573 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
574 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT
, NULL
, 0, &response
);
577 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
584 *nameLength
= response
.fap
.params
[0];
589 static int hidpp_devicenametype_get_device_name(struct hidpp_device
*hidpp
,
590 u8 feature_index
, u8 char_index
, char *device_name
, int len_buf
)
592 struct hidpp_report response
;
596 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
597 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME
, &char_index
, 1,
601 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
608 switch (response
.report_id
) {
609 case REPORT_ID_HIDPP_VERY_LONG
:
610 count
= HIDPP_REPORT_VERY_LONG_LENGTH
- 4;
612 case REPORT_ID_HIDPP_LONG
:
613 count
= HIDPP_REPORT_LONG_LENGTH
- 4;
615 case REPORT_ID_HIDPP_SHORT
:
616 count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
625 for (i
= 0; i
< count
; i
++)
626 device_name
[i
] = response
.fap
.params
[i
];
631 static char *hidpp_get_device_name(struct hidpp_device
*hidpp
)
640 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_GET_DEVICE_NAME_TYPE
,
641 &feature_index
, &feature_type
);
645 ret
= hidpp_devicenametype_get_count(hidpp
, feature_index
,
650 name
= kzalloc(__name_length
+ 1, GFP_KERNEL
);
654 while (index
< __name_length
) {
655 ret
= hidpp_devicenametype_get_device_name(hidpp
,
656 feature_index
, index
, name
+ index
,
657 __name_length
- index
);
665 /* include the terminating '\0' */
666 hidpp_prefix_name(&name
, __name_length
+ 1);
671 /* -------------------------------------------------------------------------- */
672 /* 0x1000: Battery level status */
673 /* -------------------------------------------------------------------------- */
675 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
677 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
678 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
680 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
682 static int hidpp20_batterylevel_map_status_level(u8 data
[3], int *level
,
689 *next_level
= data
[1];
691 /* When discharging, we can rely on the device reported level.
692 * For all other states the device reports level 0 (unknown). Make up
696 case 0: /* discharging (in use) */
697 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
700 case 1: /* recharging */
701 status
= POWER_SUPPLY_STATUS_CHARGING
;
704 case 2: /* charge in final stage */
705 status
= POWER_SUPPLY_STATUS_CHARGING
;
708 case 3: /* charge complete */
709 status
= POWER_SUPPLY_STATUS_FULL
;
710 level_override
= 100;
712 case 4: /* recharging below optimal speed */
713 status
= POWER_SUPPLY_STATUS_CHARGING
;
716 /* 5 = invalid battery type
718 7 = other charging error */
720 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
725 if (level_override
!= 0 && *level
== 0)
726 *level
= level_override
;
731 static int hidpp20_batterylevel_get_battery_level(struct hidpp_device
*hidpp
,
737 struct hidpp_report response
;
739 u8
*params
= (u8
*)response
.fap
.params
;
741 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
742 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS
,
745 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
752 *status
= hidpp20_batterylevel_map_status_level(params
, level
,
758 static int hidpp20_query_battery_info(struct hidpp_device
*hidpp
)
762 int status
, level
, next_level
;
764 if (hidpp
->battery
.feature_index
== 0) {
765 ret
= hidpp_root_get_feature(hidpp
,
766 HIDPP_PAGE_BATTERY_LEVEL_STATUS
,
767 &hidpp
->battery
.feature_index
,
773 ret
= hidpp20_batterylevel_get_battery_level(hidpp
,
774 hidpp
->battery
.feature_index
,
775 &status
, &level
, &next_level
);
779 hidpp
->battery
.status
= status
;
780 hidpp
->battery
.level
= level
;
785 static int hidpp20_battery_event(struct hidpp_device
*hidpp
,
788 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
789 int status
, level
, next_level
;
792 if (report
->fap
.feature_index
!= hidpp
->battery
.feature_index
||
793 report
->fap
.funcindex_clientid
!= EVENT_BATTERY_LEVEL_STATUS_BROADCAST
)
796 status
= hidpp20_batterylevel_map_status_level(report
->fap
.params
,
797 &level
, &next_level
);
799 changed
= level
!= hidpp
->battery
.level
||
800 status
!= hidpp
->battery
.status
;
803 hidpp
->battery
.level
= level
;
804 hidpp
->battery
.status
= status
;
805 if (hidpp
->battery
.ps
)
806 power_supply_changed(hidpp
->battery
.ps
);
812 static enum power_supply_property hidpp_battery_props
[] = {
813 POWER_SUPPLY_PROP_STATUS
,
814 POWER_SUPPLY_PROP_CAPACITY
,
815 POWER_SUPPLY_PROP_SCOPE
,
816 POWER_SUPPLY_PROP_MODEL_NAME
,
817 POWER_SUPPLY_PROP_MANUFACTURER
,
818 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
821 static int hidpp_battery_get_property(struct power_supply
*psy
,
822 enum power_supply_property psp
,
823 union power_supply_propval
*val
)
825 struct hidpp_device
*hidpp
= power_supply_get_drvdata(psy
);
829 case POWER_SUPPLY_PROP_STATUS
:
830 val
->intval
= hidpp
->battery
.status
;
832 case POWER_SUPPLY_PROP_CAPACITY
:
833 val
->intval
= hidpp
->battery
.level
;
835 case POWER_SUPPLY_PROP_SCOPE
:
836 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
838 case POWER_SUPPLY_PROP_MODEL_NAME
:
839 if (!strncmp(hidpp
->name
, "Logitech ", 9))
840 val
->strval
= hidpp
->name
+ 9;
842 val
->strval
= hidpp
->name
;
844 case POWER_SUPPLY_PROP_MANUFACTURER
:
845 val
->strval
= "Logitech";
847 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
848 val
->strval
= hidpp
->hid_dev
->uniq
;
858 /* -------------------------------------------------------------------------- */
859 /* 0x6010: Touchpad FW items */
860 /* -------------------------------------------------------------------------- */
862 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
864 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
866 struct hidpp_touchpad_fw_items
{
868 uint8_t desired_state
;
874 * send a set state command to the device by reading the current items->state
875 * field. items is then filled with the current state.
877 static int hidpp_touchpad_fw_items_set(struct hidpp_device
*hidpp
,
879 struct hidpp_touchpad_fw_items
*items
)
881 struct hidpp_report response
;
883 u8
*params
= (u8
*)response
.fap
.params
;
885 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
886 CMD_TOUCHPAD_FW_ITEMS_SET
, &items
->state
, 1, &response
);
889 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
896 items
->presence
= params
[0];
897 items
->desired_state
= params
[1];
898 items
->state
= params
[2];
899 items
->persistent
= params
[3];
904 /* -------------------------------------------------------------------------- */
905 /* 0x6100: TouchPadRawXY */
906 /* -------------------------------------------------------------------------- */
908 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
910 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
911 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
913 #define EVENT_TOUCHPAD_RAW_XY 0x00
915 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
916 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
918 struct hidpp_touchpad_raw_info
{
929 struct hidpp_touchpad_raw_xy_finger
{
939 struct hidpp_touchpad_raw_xy
{
941 struct hidpp_touchpad_raw_xy_finger fingers
[2];
948 static int hidpp_touchpad_get_raw_info(struct hidpp_device
*hidpp
,
949 u8 feature_index
, struct hidpp_touchpad_raw_info
*raw_info
)
951 struct hidpp_report response
;
953 u8
*params
= (u8
*)response
.fap
.params
;
955 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
956 CMD_TOUCHPAD_GET_RAW_INFO
, NULL
, 0, &response
);
959 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
966 raw_info
->x_size
= get_unaligned_be16(¶ms
[0]);
967 raw_info
->y_size
= get_unaligned_be16(¶ms
[2]);
968 raw_info
->z_range
= params
[4];
969 raw_info
->area_range
= params
[5];
970 raw_info
->maxcontacts
= params
[7];
971 raw_info
->origin
= params
[8];
972 /* res is given in unit per inch */
973 raw_info
->res
= get_unaligned_be16(¶ms
[13]) * 2 / 51;
978 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device
*hidpp_dev
,
979 u8 feature_index
, bool send_raw_reports
,
980 bool sensor_enhanced_settings
)
982 struct hidpp_report response
;
987 * bit 1 - 16bit Z, no area
988 * bit 2 - enhanced sensitivity
989 * bit 3 - width, height (4 bits each) instead of area
990 * bit 4 - send raw + gestures (degrades smoothness)
991 * remaining bits - reserved
993 u8 params
= send_raw_reports
| (sensor_enhanced_settings
<< 2);
995 return hidpp_send_fap_command_sync(hidpp_dev
, feature_index
,
996 CMD_TOUCHPAD_SET_RAW_REPORT_STATE
, ¶ms
, 1, &response
);
999 static void hidpp_touchpad_touch_event(u8
*data
,
1000 struct hidpp_touchpad_raw_xy_finger
*finger
)
1002 u8 x_m
= data
[0] << 2;
1003 u8 y_m
= data
[2] << 2;
1005 finger
->x
= x_m
<< 6 | data
[1];
1006 finger
->y
= y_m
<< 6 | data
[3];
1008 finger
->contact_type
= data
[0] >> 6;
1009 finger
->contact_status
= data
[2] >> 6;
1011 finger
->z
= data
[4];
1012 finger
->area
= data
[5];
1013 finger
->finger_id
= data
[6] >> 4;
1016 static void hidpp_touchpad_raw_xy_event(struct hidpp_device
*hidpp_dev
,
1017 u8
*data
, struct hidpp_touchpad_raw_xy
*raw_xy
)
1019 memset(raw_xy
, 0, sizeof(struct hidpp_touchpad_raw_xy
));
1020 raw_xy
->end_of_frame
= data
[8] & 0x01;
1021 raw_xy
->spurious_flag
= (data
[8] >> 1) & 0x01;
1022 raw_xy
->finger_count
= data
[15] & 0x0f;
1023 raw_xy
->button
= (data
[8] >> 2) & 0x01;
1025 if (raw_xy
->finger_count
) {
1026 hidpp_touchpad_touch_event(&data
[2], &raw_xy
->fingers
[0]);
1027 hidpp_touchpad_touch_event(&data
[9], &raw_xy
->fingers
[1]);
1031 /* -------------------------------------------------------------------------- */
1032 /* 0x8123: Force feedback support */
1033 /* -------------------------------------------------------------------------- */
1035 #define HIDPP_FF_GET_INFO 0x01
1036 #define HIDPP_FF_RESET_ALL 0x11
1037 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1038 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1039 #define HIDPP_FF_DESTROY_EFFECT 0x41
1040 #define HIDPP_FF_GET_APERTURE 0x51
1041 #define HIDPP_FF_SET_APERTURE 0x61
1042 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1043 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1045 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1046 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1047 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1048 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1050 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1051 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1052 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1053 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1054 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1055 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1056 #define HIDPP_FF_EFFECT_SPRING 0x06
1057 #define HIDPP_FF_EFFECT_DAMPER 0x07
1058 #define HIDPP_FF_EFFECT_FRICTION 0x08
1059 #define HIDPP_FF_EFFECT_INERTIA 0x09
1060 #define HIDPP_FF_EFFECT_RAMP 0x0A
1062 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1064 #define HIDPP_FF_EFFECTID_NONE -1
1065 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1067 #define HIDPP_FF_MAX_PARAMS 20
1068 #define HIDPP_FF_RESERVED_SLOTS 1
1070 struct hidpp_ff_private_data
{
1071 struct hidpp_device
*hidpp
;
1079 struct workqueue_struct
*wq
;
1080 atomic_t workqueue_size
;
1083 struct hidpp_ff_work_data
{
1084 struct work_struct work
;
1085 struct hidpp_ff_private_data
*data
;
1088 u8 params
[HIDPP_FF_MAX_PARAMS
];
1092 static const signed short hiddpp_ff_effects
[] = {
1107 static const signed short hiddpp_ff_effects_v2
[] = {
1114 static const u8 HIDPP_FF_CONDITION_CMDS
[] = {
1115 HIDPP_FF_EFFECT_SPRING
,
1116 HIDPP_FF_EFFECT_FRICTION
,
1117 HIDPP_FF_EFFECT_DAMPER
,
1118 HIDPP_FF_EFFECT_INERTIA
1121 static const char *HIDPP_FF_CONDITION_NAMES
[] = {
1129 static u8
hidpp_ff_find_effect(struct hidpp_ff_private_data
*data
, int effect_id
)
1133 for (i
= 0; i
< data
->num_effects
; i
++)
1134 if (data
->effect_ids
[i
] == effect_id
)
1140 static void hidpp_ff_work_handler(struct work_struct
*w
)
1142 struct hidpp_ff_work_data
*wd
= container_of(w
, struct hidpp_ff_work_data
, work
);
1143 struct hidpp_ff_private_data
*data
= wd
->data
;
1144 struct hidpp_report response
;
1148 /* add slot number if needed */
1149 switch (wd
->effect_id
) {
1150 case HIDPP_FF_EFFECTID_AUTOCENTER
:
1151 wd
->params
[0] = data
->slot_autocenter
;
1153 case HIDPP_FF_EFFECTID_NONE
:
1154 /* leave slot as zero */
1157 /* find current slot for effect */
1158 wd
->params
[0] = hidpp_ff_find_effect(data
, wd
->effect_id
);
1162 /* send command and wait for reply */
1163 ret
= hidpp_send_fap_command_sync(data
->hidpp
, data
->feature_index
,
1164 wd
->command
, wd
->params
, wd
->size
, &response
);
1167 hid_err(data
->hidpp
->hid_dev
, "Failed to send command to device!\n");
1171 /* parse return data */
1172 switch (wd
->command
) {
1173 case HIDPP_FF_DOWNLOAD_EFFECT
:
1174 slot
= response
.fap
.params
[0];
1175 if (slot
> 0 && slot
<= data
->num_effects
) {
1176 if (wd
->effect_id
>= 0)
1177 /* regular effect uploaded */
1178 data
->effect_ids
[slot
-1] = wd
->effect_id
;
1179 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
1180 /* autocenter spring uploaded */
1181 data
->slot_autocenter
= slot
;
1184 case HIDPP_FF_DESTROY_EFFECT
:
1185 if (wd
->effect_id
>= 0)
1186 /* regular effect destroyed */
1187 data
->effect_ids
[wd
->params
[0]-1] = -1;
1188 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
1189 /* autocenter spring destoyed */
1190 data
->slot_autocenter
= 0;
1192 case HIDPP_FF_SET_GLOBAL_GAINS
:
1193 data
->gain
= (wd
->params
[0] << 8) + wd
->params
[1];
1195 case HIDPP_FF_SET_APERTURE
:
1196 data
->range
= (wd
->params
[0] << 8) + wd
->params
[1];
1199 /* no action needed */
1204 atomic_dec(&data
->workqueue_size
);
1208 static int hidpp_ff_queue_work(struct hidpp_ff_private_data
*data
, int effect_id
, u8 command
, u8
*params
, u8 size
)
1210 struct hidpp_ff_work_data
*wd
= kzalloc(sizeof(*wd
), GFP_KERNEL
);
1216 INIT_WORK(&wd
->work
, hidpp_ff_work_handler
);
1219 wd
->effect_id
= effect_id
;
1220 wd
->command
= command
;
1222 memcpy(wd
->params
, params
, size
);
1224 atomic_inc(&data
->workqueue_size
);
1225 queue_work(data
->wq
, &wd
->work
);
1227 /* warn about excessive queue size */
1228 s
= atomic_read(&data
->workqueue_size
);
1229 if (s
>= 20 && s
% 20 == 0)
1230 hid_warn(data
->hidpp
->hid_dev
, "Force feedback command queue contains %d commands, causing substantial delays!", s
);
1235 static int hidpp_ff_upload_effect(struct input_dev
*dev
, struct ff_effect
*effect
, struct ff_effect
*old
)
1237 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1242 /* set common parameters */
1243 params
[2] = effect
->replay
.length
>> 8;
1244 params
[3] = effect
->replay
.length
& 255;
1245 params
[4] = effect
->replay
.delay
>> 8;
1246 params
[5] = effect
->replay
.delay
& 255;
1248 switch (effect
->type
) {
1250 force
= (effect
->u
.constant
.level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1251 params
[1] = HIDPP_FF_EFFECT_CONSTANT
;
1252 params
[6] = force
>> 8;
1253 params
[7] = force
& 255;
1254 params
[8] = effect
->u
.constant
.envelope
.attack_level
>> 7;
1255 params
[9] = effect
->u
.constant
.envelope
.attack_length
>> 8;
1256 params
[10] = effect
->u
.constant
.envelope
.attack_length
& 255;
1257 params
[11] = effect
->u
.constant
.envelope
.fade_level
>> 7;
1258 params
[12] = effect
->u
.constant
.envelope
.fade_length
>> 8;
1259 params
[13] = effect
->u
.constant
.envelope
.fade_length
& 255;
1261 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1262 effect
->u
.constant
.level
,
1263 effect
->direction
, force
);
1264 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1265 effect
->u
.constant
.envelope
.attack_level
,
1266 effect
->u
.constant
.envelope
.attack_length
,
1267 effect
->u
.constant
.envelope
.fade_level
,
1268 effect
->u
.constant
.envelope
.fade_length
);
1272 switch (effect
->u
.periodic
.waveform
) {
1274 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SINE
;
1277 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE
;
1280 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP
;
1283 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN
;
1286 params
[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE
;
1289 hid_err(data
->hidpp
->hid_dev
, "Unexpected periodic waveform type %i!\n", effect
->u
.periodic
.waveform
);
1292 force
= (effect
->u
.periodic
.magnitude
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1293 params
[6] = effect
->u
.periodic
.magnitude
>> 8;
1294 params
[7] = effect
->u
.periodic
.magnitude
& 255;
1295 params
[8] = effect
->u
.periodic
.offset
>> 8;
1296 params
[9] = effect
->u
.periodic
.offset
& 255;
1297 params
[10] = effect
->u
.periodic
.period
>> 8;
1298 params
[11] = effect
->u
.periodic
.period
& 255;
1299 params
[12] = effect
->u
.periodic
.phase
>> 8;
1300 params
[13] = effect
->u
.periodic
.phase
& 255;
1301 params
[14] = effect
->u
.periodic
.envelope
.attack_level
>> 7;
1302 params
[15] = effect
->u
.periodic
.envelope
.attack_length
>> 8;
1303 params
[16] = effect
->u
.periodic
.envelope
.attack_length
& 255;
1304 params
[17] = effect
->u
.periodic
.envelope
.fade_level
>> 7;
1305 params
[18] = effect
->u
.periodic
.envelope
.fade_length
>> 8;
1306 params
[19] = effect
->u
.periodic
.envelope
.fade_length
& 255;
1308 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1309 effect
->u
.periodic
.magnitude
, effect
->direction
,
1310 effect
->u
.periodic
.offset
,
1311 effect
->u
.periodic
.period
,
1312 effect
->u
.periodic
.phase
);
1313 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1314 effect
->u
.periodic
.envelope
.attack_level
,
1315 effect
->u
.periodic
.envelope
.attack_length
,
1316 effect
->u
.periodic
.envelope
.fade_level
,
1317 effect
->u
.periodic
.envelope
.fade_length
);
1321 params
[1] = HIDPP_FF_EFFECT_RAMP
;
1322 force
= (effect
->u
.ramp
.start_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1323 params
[6] = force
>> 8;
1324 params
[7] = force
& 255;
1325 force
= (effect
->u
.ramp
.end_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1326 params
[8] = force
>> 8;
1327 params
[9] = force
& 255;
1328 params
[10] = effect
->u
.ramp
.envelope
.attack_level
>> 7;
1329 params
[11] = effect
->u
.ramp
.envelope
.attack_length
>> 8;
1330 params
[12] = effect
->u
.ramp
.envelope
.attack_length
& 255;
1331 params
[13] = effect
->u
.ramp
.envelope
.fade_level
>> 7;
1332 params
[14] = effect
->u
.ramp
.envelope
.fade_length
>> 8;
1333 params
[15] = effect
->u
.ramp
.envelope
.fade_length
& 255;
1335 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1336 effect
->u
.ramp
.start_level
,
1337 effect
->u
.ramp
.end_level
,
1338 effect
->direction
, force
);
1339 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1340 effect
->u
.ramp
.envelope
.attack_level
,
1341 effect
->u
.ramp
.envelope
.attack_length
,
1342 effect
->u
.ramp
.envelope
.fade_level
,
1343 effect
->u
.ramp
.envelope
.fade_length
);
1349 params
[1] = HIDPP_FF_CONDITION_CMDS
[effect
->type
- FF_SPRING
];
1350 params
[6] = effect
->u
.condition
[0].left_saturation
>> 9;
1351 params
[7] = (effect
->u
.condition
[0].left_saturation
>> 1) & 255;
1352 params
[8] = effect
->u
.condition
[0].left_coeff
>> 8;
1353 params
[9] = effect
->u
.condition
[0].left_coeff
& 255;
1354 params
[10] = effect
->u
.condition
[0].deadband
>> 9;
1355 params
[11] = (effect
->u
.condition
[0].deadband
>> 1) & 255;
1356 params
[12] = effect
->u
.condition
[0].center
>> 8;
1357 params
[13] = effect
->u
.condition
[0].center
& 255;
1358 params
[14] = effect
->u
.condition
[0].right_coeff
>> 8;
1359 params
[15] = effect
->u
.condition
[0].right_coeff
& 255;
1360 params
[16] = effect
->u
.condition
[0].right_saturation
>> 9;
1361 params
[17] = (effect
->u
.condition
[0].right_saturation
>> 1) & 255;
1363 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1364 HIDPP_FF_CONDITION_NAMES
[effect
->type
- FF_SPRING
],
1365 effect
->u
.condition
[0].left_coeff
,
1366 effect
->u
.condition
[0].left_saturation
,
1367 effect
->u
.condition
[0].right_coeff
,
1368 effect
->u
.condition
[0].right_saturation
);
1369 dbg_hid(" deadband=%d, center=%d\n",
1370 effect
->u
.condition
[0].deadband
,
1371 effect
->u
.condition
[0].center
);
1374 hid_err(data
->hidpp
->hid_dev
, "Unexpected force type %i!\n", effect
->type
);
1378 return hidpp_ff_queue_work(data
, effect
->id
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, size
);
1381 static int hidpp_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
1383 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1386 params
[1] = value
? HIDPP_FF_EFFECT_STATE_PLAY
: HIDPP_FF_EFFECT_STATE_STOP
;
1388 dbg_hid("St%sing playback of effect %d.\n", value
?"art":"opp", effect_id
);
1390 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_SET_EFFECT_STATE
, params
, ARRAY_SIZE(params
));
1393 static int hidpp_ff_erase_effect(struct input_dev
*dev
, int effect_id
)
1395 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1398 dbg_hid("Erasing effect %d.\n", effect_id
);
1400 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_DESTROY_EFFECT
, &slot
, 1);
1403 static void hidpp_ff_set_autocenter(struct input_dev
*dev
, u16 magnitude
)
1405 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1408 dbg_hid("Setting autocenter to %d.\n", magnitude
);
1410 /* start a standard spring effect */
1411 params
[1] = HIDPP_FF_EFFECT_SPRING
| HIDPP_FF_EFFECT_AUTOSTART
;
1412 /* zero delay and duration */
1413 params
[2] = params
[3] = params
[4] = params
[5] = 0;
1414 /* set coeff to 25% of saturation */
1415 params
[8] = params
[14] = magnitude
>> 11;
1416 params
[9] = params
[15] = (magnitude
>> 3) & 255;
1417 params
[6] = params
[16] = magnitude
>> 9;
1418 params
[7] = params
[17] = (magnitude
>> 1) & 255;
1419 /* zero deadband and center */
1420 params
[10] = params
[11] = params
[12] = params
[13] = 0;
1422 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_AUTOCENTER
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, ARRAY_SIZE(params
));
1425 static void hidpp_ff_set_gain(struct input_dev
*dev
, u16 gain
)
1427 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1430 dbg_hid("Setting gain to %d.\n", gain
);
1432 params
[0] = gain
>> 8;
1433 params
[1] = gain
& 255;
1434 params
[2] = 0; /* no boost */
1437 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_NONE
, HIDPP_FF_SET_GLOBAL_GAINS
, params
, ARRAY_SIZE(params
));
1440 static ssize_t
hidpp_ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1442 struct hid_device
*hid
= to_hid_device(dev
);
1443 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1444 struct input_dev
*idev
= hidinput
->input
;
1445 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
1447 return scnprintf(buf
, PAGE_SIZE
, "%u\n", data
->range
);
1450 static ssize_t
hidpp_ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
1452 struct hid_device
*hid
= to_hid_device(dev
);
1453 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1454 struct input_dev
*idev
= hidinput
->input
;
1455 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
1457 int range
= simple_strtoul(buf
, NULL
, 10);
1459 range
= clamp(range
, 180, 900);
1461 params
[0] = range
>> 8;
1462 params
[1] = range
& 0x00FF;
1464 hidpp_ff_queue_work(data
, -1, HIDPP_FF_SET_APERTURE
, params
, ARRAY_SIZE(params
));
1469 static DEVICE_ATTR(range
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
, hidpp_ff_range_show
, hidpp_ff_range_store
);
1471 static void hidpp_ff_destroy(struct ff_device
*ff
)
1473 struct hidpp_ff_private_data
*data
= ff
->private;
1475 kfree(data
->effect_ids
);
1478 static int hidpp_ff_init(struct hidpp_device
*hidpp
, u8 feature_index
)
1480 struct hid_device
*hid
= hidpp
->hid_dev
;
1481 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1482 struct input_dev
*dev
= hidinput
->input
;
1483 const struct usb_device_descriptor
*udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
1484 const u16 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
1485 struct ff_device
*ff
;
1486 struct hidpp_report response
;
1487 struct hidpp_ff_private_data
*data
;
1488 int error
, j
, num_slots
;
1492 hid_err(hid
, "Struct input_dev not set!\n");
1496 /* Get firmware release */
1497 version
= bcdDevice
& 255;
1499 /* Set supported force feedback capabilities */
1500 for (j
= 0; hiddpp_ff_effects
[j
] >= 0; j
++)
1501 set_bit(hiddpp_ff_effects
[j
], dev
->ffbit
);
1503 for (j
= 0; hiddpp_ff_effects_v2
[j
] >= 0; j
++)
1504 set_bit(hiddpp_ff_effects_v2
[j
], dev
->ffbit
);
1506 /* Read number of slots available in device */
1507 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1508 HIDPP_FF_GET_INFO
, NULL
, 0, &response
);
1512 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1517 num_slots
= response
.fap
.params
[0] - HIDPP_FF_RESERVED_SLOTS
;
1519 error
= input_ff_create(dev
, num_slots
);
1522 hid_err(dev
, "Failed to create FF device!\n");
1526 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1529 data
->effect_ids
= kcalloc(num_slots
, sizeof(int), GFP_KERNEL
);
1530 if (!data
->effect_ids
) {
1534 data
->hidpp
= hidpp
;
1535 data
->feature_index
= feature_index
;
1536 data
->version
= version
;
1537 data
->slot_autocenter
= 0;
1538 data
->num_effects
= num_slots
;
1539 for (j
= 0; j
< num_slots
; j
++)
1540 data
->effect_ids
[j
] = -1;
1545 ff
->upload
= hidpp_ff_upload_effect
;
1546 ff
->erase
= hidpp_ff_erase_effect
;
1547 ff
->playback
= hidpp_ff_playback
;
1548 ff
->set_gain
= hidpp_ff_set_gain
;
1549 ff
->set_autocenter
= hidpp_ff_set_autocenter
;
1550 ff
->destroy
= hidpp_ff_destroy
;
1553 /* reset all forces */
1554 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1555 HIDPP_FF_RESET_ALL
, NULL
, 0, &response
);
1557 /* Read current Range */
1558 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1559 HIDPP_FF_GET_APERTURE
, NULL
, 0, &response
);
1561 hid_warn(hidpp
->hid_dev
, "Failed to read range from device!\n");
1562 data
->range
= error
? 900 : get_unaligned_be16(&response
.fap
.params
[0]);
1564 /* Create sysfs interface */
1565 error
= device_create_file(&(hidpp
->hid_dev
->dev
), &dev_attr_range
);
1567 hid_warn(hidpp
->hid_dev
, "Unable to create sysfs interface for \"range\", errno %d!\n", error
);
1569 /* Read the current gain values */
1570 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1571 HIDPP_FF_GET_GLOBAL_GAINS
, NULL
, 0, &response
);
1573 hid_warn(hidpp
->hid_dev
, "Failed to read gain values from device!\n");
1574 data
->gain
= error
? 0xffff : get_unaligned_be16(&response
.fap
.params
[0]);
1575 /* ignore boost value at response.fap.params[2] */
1577 /* init the hardware command queue */
1578 data
->wq
= create_singlethread_workqueue("hidpp-ff-sendqueue");
1579 atomic_set(&data
->workqueue_size
, 0);
1581 /* initialize with zero autocenter to get wheel in usable state */
1582 hidpp_ff_set_autocenter(dev
, 0);
1584 hid_info(hid
, "Force feeback support loaded (firmware release %d).\n", version
);
1589 static int hidpp_ff_deinit(struct hid_device
*hid
)
1591 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1592 struct input_dev
*dev
= hidinput
->input
;
1593 struct hidpp_ff_private_data
*data
;
1596 hid_err(hid
, "Struct input_dev not found!\n");
1600 hid_info(hid
, "Unloading HID++ force feedback.\n");
1601 data
= dev
->ff
->private;
1603 hid_err(hid
, "Private data not found!\n");
1607 destroy_workqueue(data
->wq
);
1608 device_remove_file(&hid
->dev
, &dev_attr_range
);
1614 /* ************************************************************************** */
1616 /* Device Support */
1618 /* ************************************************************************** */
1620 /* -------------------------------------------------------------------------- */
1621 /* Touchpad HID++ devices */
1622 /* -------------------------------------------------------------------------- */
1624 #define WTP_MANUAL_RESOLUTION 39
1627 struct input_dev
*input
;
1630 u8 mt_feature_index
;
1631 u8 button_feature_index
;
1634 unsigned int resolution
;
1637 static int wtp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
1638 struct hid_field
*field
, struct hid_usage
*usage
,
1639 unsigned long **bit
, int *max
)
1644 static void wtp_populate_input(struct hidpp_device
*hidpp
,
1645 struct input_dev
*input_dev
, bool origin_is_hid_core
)
1647 struct wtp_data
*wd
= hidpp
->private_data
;
1649 __set_bit(EV_ABS
, input_dev
->evbit
);
1650 __set_bit(EV_KEY
, input_dev
->evbit
);
1651 __clear_bit(EV_REL
, input_dev
->evbit
);
1652 __clear_bit(EV_LED
, input_dev
->evbit
);
1654 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, wd
->x_size
, 0, 0);
1655 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
, wd
->resolution
);
1656 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, wd
->y_size
, 0, 0);
1657 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
, wd
->resolution
);
1659 /* Max pressure is not given by the devices, pick one */
1660 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 50, 0, 0);
1662 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
1664 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
)
1665 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
1667 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
1669 input_mt_init_slots(input_dev
, wd
->maxcontacts
, INPUT_MT_POINTER
|
1670 INPUT_MT_DROP_UNUSED
);
1672 wd
->input
= input_dev
;
1675 static void wtp_touch_event(struct wtp_data
*wd
,
1676 struct hidpp_touchpad_raw_xy_finger
*touch_report
)
1680 if (!touch_report
->finger_id
|| touch_report
->contact_type
)
1681 /* no actual data */
1684 slot
= input_mt_get_slot_by_key(wd
->input
, touch_report
->finger_id
);
1686 input_mt_slot(wd
->input
, slot
);
1687 input_mt_report_slot_state(wd
->input
, MT_TOOL_FINGER
,
1688 touch_report
->contact_status
);
1689 if (touch_report
->contact_status
) {
1690 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_X
,
1692 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_Y
,
1693 wd
->flip_y
? wd
->y_size
- touch_report
->y
:
1695 input_event(wd
->input
, EV_ABS
, ABS_MT_PRESSURE
,
1696 touch_report
->area
);
1700 static void wtp_send_raw_xy_event(struct hidpp_device
*hidpp
,
1701 struct hidpp_touchpad_raw_xy
*raw
)
1703 struct wtp_data
*wd
= hidpp
->private_data
;
1706 for (i
= 0; i
< 2; i
++)
1707 wtp_touch_event(wd
, &(raw
->fingers
[i
]));
1709 if (raw
->end_of_frame
&&
1710 !(hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
))
1711 input_event(wd
->input
, EV_KEY
, BTN_LEFT
, raw
->button
);
1713 if (raw
->end_of_frame
|| raw
->finger_count
<= 2) {
1714 input_mt_sync_frame(wd
->input
);
1715 input_sync(wd
->input
);
1719 static int wtp_mouse_raw_xy_event(struct hidpp_device
*hidpp
, u8
*data
)
1721 struct wtp_data
*wd
= hidpp
->private_data
;
1722 u8 c1_area
= ((data
[7] & 0xf) * (data
[7] & 0xf) +
1723 (data
[7] >> 4) * (data
[7] >> 4)) / 2;
1724 u8 c2_area
= ((data
[13] & 0xf) * (data
[13] & 0xf) +
1725 (data
[13] >> 4) * (data
[13] >> 4)) / 2;
1726 struct hidpp_touchpad_raw_xy raw
= {
1727 .timestamp
= data
[1],
1731 .contact_status
= !!data
[7],
1732 .x
= get_unaligned_le16(&data
[3]),
1733 .y
= get_unaligned_le16(&data
[5]),
1736 .finger_id
= data
[2],
1739 .contact_status
= !!data
[13],
1740 .x
= get_unaligned_le16(&data
[9]),
1741 .y
= get_unaligned_le16(&data
[11]),
1744 .finger_id
= data
[8],
1747 .finger_count
= wd
->maxcontacts
,
1749 .end_of_frame
= (data
[0] >> 7) == 0,
1750 .button
= data
[0] & 0x01,
1753 wtp_send_raw_xy_event(hidpp
, &raw
);
1758 static int wtp_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
1760 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1761 struct wtp_data
*wd
= hidpp
->private_data
;
1762 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1763 struct hidpp_touchpad_raw_xy raw
;
1765 if (!wd
|| !wd
->input
)
1771 hid_err(hdev
, "Received HID report of bad size (%d)",
1775 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
) {
1776 input_event(wd
->input
, EV_KEY
, BTN_LEFT
,
1777 !!(data
[1] & 0x01));
1778 input_event(wd
->input
, EV_KEY
, BTN_RIGHT
,
1779 !!(data
[1] & 0x02));
1780 input_sync(wd
->input
);
1785 return wtp_mouse_raw_xy_event(hidpp
, &data
[7]);
1787 case REPORT_ID_HIDPP_LONG
:
1788 /* size is already checked in hidpp_raw_event. */
1789 if ((report
->fap
.feature_index
!= wd
->mt_feature_index
) ||
1790 (report
->fap
.funcindex_clientid
!= EVENT_TOUCHPAD_RAW_XY
))
1792 hidpp_touchpad_raw_xy_event(hidpp
, data
+ 4, &raw
);
1794 wtp_send_raw_xy_event(hidpp
, &raw
);
1801 static int wtp_get_config(struct hidpp_device
*hidpp
)
1803 struct wtp_data
*wd
= hidpp
->private_data
;
1804 struct hidpp_touchpad_raw_info raw_info
= {0};
1808 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_TOUCHPAD_RAW_XY
,
1809 &wd
->mt_feature_index
, &feature_type
);
1811 /* means that the device is not powered up */
1814 ret
= hidpp_touchpad_get_raw_info(hidpp
, wd
->mt_feature_index
,
1819 wd
->x_size
= raw_info
.x_size
;
1820 wd
->y_size
= raw_info
.y_size
;
1821 wd
->maxcontacts
= raw_info
.maxcontacts
;
1822 wd
->flip_y
= raw_info
.origin
== TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT
;
1823 wd
->resolution
= raw_info
.res
;
1824 if (!wd
->resolution
)
1825 wd
->resolution
= WTP_MANUAL_RESOLUTION
;
1830 static int wtp_allocate(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1832 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1833 struct wtp_data
*wd
;
1835 wd
= devm_kzalloc(&hdev
->dev
, sizeof(struct wtp_data
),
1840 hidpp
->private_data
= wd
;
1845 static int wtp_connect(struct hid_device
*hdev
, bool connected
)
1847 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1848 struct wtp_data
*wd
= hidpp
->private_data
;
1852 ret
= wtp_get_config(hidpp
);
1854 hid_err(hdev
, "Can not get wtp config: %d\n", ret
);
1859 return hidpp_touchpad_set_raw_report_state(hidpp
, wd
->mt_feature_index
,
1863 /* ------------------------------------------------------------------------- */
1864 /* Logitech M560 devices */
1865 /* ------------------------------------------------------------------------- */
1868 * Logitech M560 protocol overview
1870 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1871 * the sides buttons are pressed, it sends some keyboard keys events
1872 * instead of buttons ones.
1873 * To complicate things further, the middle button keys sequence
1874 * is different from the odd press and the even press.
1876 * forward button -> Super_R
1877 * backward button -> Super_L+'d' (press only)
1878 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1879 * 2nd time: left-click (press only)
1880 * NB: press-only means that when the button is pressed, the
1881 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1882 * together sequentially; instead when the button is released, no event is
1886 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1887 * the mouse reacts differently:
1888 * - it never sends a keyboard key event
1889 * - for the three mouse button it sends:
1890 * middle button press 11<xx>0a 3500af00...
1891 * side 1 button (forward) press 11<xx>0a 3500b000...
1892 * side 2 button (backward) press 11<xx>0a 3500ae00...
1893 * middle/side1/side2 button release 11<xx>0a 35000000...
1896 static const u8 m560_config_parameter
[] = {0x00, 0xaf, 0x03};
1898 struct m560_private_data
{
1899 struct input_dev
*input
;
1902 /* how buttons are mapped in the report */
1903 #define M560_MOUSE_BTN_LEFT 0x01
1904 #define M560_MOUSE_BTN_RIGHT 0x02
1905 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1906 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1908 #define M560_SUB_ID 0x0a
1909 #define M560_BUTTON_MODE_REGISTER 0x35
1911 static int m560_send_config_command(struct hid_device
*hdev
, bool connected
)
1913 struct hidpp_report response
;
1914 struct hidpp_device
*hidpp_dev
;
1916 hidpp_dev
= hid_get_drvdata(hdev
);
1918 return hidpp_send_rap_command_sync(
1920 REPORT_ID_HIDPP_SHORT
,
1922 M560_BUTTON_MODE_REGISTER
,
1923 (u8
*)m560_config_parameter
,
1924 sizeof(m560_config_parameter
),
1929 static int m560_allocate(struct hid_device
*hdev
)
1931 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1932 struct m560_private_data
*d
;
1934 d
= devm_kzalloc(&hdev
->dev
, sizeof(struct m560_private_data
),
1939 hidpp
->private_data
= d
;
1944 static int m560_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
1946 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1947 struct m560_private_data
*mydata
= hidpp
->private_data
;
1950 if (!mydata
|| !mydata
->input
) {
1951 hid_err(hdev
, "error in parameter\n");
1956 hid_err(hdev
, "error in report\n");
1960 if (data
[0] == REPORT_ID_HIDPP_LONG
&&
1961 data
[2] == M560_SUB_ID
&& data
[6] == 0x00) {
1963 * m560 mouse report for middle, forward and backward button
1966 * data[1] = device-id
1968 * data[5] = 0xaf -> middle
1971 * 0x00 -> release all
1977 input_report_key(mydata
->input
, BTN_MIDDLE
, 1);
1980 input_report_key(mydata
->input
, BTN_FORWARD
, 1);
1983 input_report_key(mydata
->input
, BTN_BACK
, 1);
1986 input_report_key(mydata
->input
, BTN_BACK
, 0);
1987 input_report_key(mydata
->input
, BTN_FORWARD
, 0);
1988 input_report_key(mydata
->input
, BTN_MIDDLE
, 0);
1991 hid_err(hdev
, "error in report\n");
1994 input_sync(mydata
->input
);
1996 } else if (data
[0] == 0x02) {
1998 * Logitech M560 mouse report
2000 * data[0] = type (0x02)
2001 * data[1..2] = buttons
2008 input_report_key(mydata
->input
, BTN_LEFT
,
2009 !!(data
[1] & M560_MOUSE_BTN_LEFT
));
2010 input_report_key(mydata
->input
, BTN_RIGHT
,
2011 !!(data
[1] & M560_MOUSE_BTN_RIGHT
));
2013 if (data
[1] & M560_MOUSE_BTN_WHEEL_LEFT
)
2014 input_report_rel(mydata
->input
, REL_HWHEEL
, -1);
2015 else if (data
[1] & M560_MOUSE_BTN_WHEEL_RIGHT
)
2016 input_report_rel(mydata
->input
, REL_HWHEEL
, 1);
2018 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 0, 12), 12);
2019 input_report_rel(mydata
->input
, REL_X
, v
);
2021 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 12, 12), 12);
2022 input_report_rel(mydata
->input
, REL_Y
, v
);
2024 v
= hid_snto32(data
[6], 8);
2025 input_report_rel(mydata
->input
, REL_WHEEL
, v
);
2027 input_sync(mydata
->input
);
2033 static void m560_populate_input(struct hidpp_device
*hidpp
,
2034 struct input_dev
*input_dev
, bool origin_is_hid_core
)
2036 struct m560_private_data
*mydata
= hidpp
->private_data
;
2038 mydata
->input
= input_dev
;
2040 __set_bit(EV_KEY
, mydata
->input
->evbit
);
2041 __set_bit(BTN_MIDDLE
, mydata
->input
->keybit
);
2042 __set_bit(BTN_RIGHT
, mydata
->input
->keybit
);
2043 __set_bit(BTN_LEFT
, mydata
->input
->keybit
);
2044 __set_bit(BTN_BACK
, mydata
->input
->keybit
);
2045 __set_bit(BTN_FORWARD
, mydata
->input
->keybit
);
2047 __set_bit(EV_REL
, mydata
->input
->evbit
);
2048 __set_bit(REL_X
, mydata
->input
->relbit
);
2049 __set_bit(REL_Y
, mydata
->input
->relbit
);
2050 __set_bit(REL_WHEEL
, mydata
->input
->relbit
);
2051 __set_bit(REL_HWHEEL
, mydata
->input
->relbit
);
2054 static int m560_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2055 struct hid_field
*field
, struct hid_usage
*usage
,
2056 unsigned long **bit
, int *max
)
2061 /* ------------------------------------------------------------------------- */
2062 /* Logitech K400 devices */
2063 /* ------------------------------------------------------------------------- */
2066 * The Logitech K400 keyboard has an embedded touchpad which is seen
2067 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2068 * tap-to-click but the setting is not remembered accross reset, annoying some
2071 * We can toggle this feature from the host by using the feature 0x6010:
2075 struct k400_private_data
{
2079 static int k400_disable_tap_to_click(struct hidpp_device
*hidpp
)
2081 struct k400_private_data
*k400
= hidpp
->private_data
;
2082 struct hidpp_touchpad_fw_items items
= {};
2086 if (!k400
->feature_index
) {
2087 ret
= hidpp_root_get_feature(hidpp
,
2088 HIDPP_PAGE_TOUCHPAD_FW_ITEMS
,
2089 &k400
->feature_index
, &feature_type
);
2091 /* means that the device is not powered up */
2095 ret
= hidpp_touchpad_fw_items_set(hidpp
, k400
->feature_index
, &items
);
2102 static int k400_allocate(struct hid_device
*hdev
)
2104 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2105 struct k400_private_data
*k400
;
2107 k400
= devm_kzalloc(&hdev
->dev
, sizeof(struct k400_private_data
),
2112 hidpp
->private_data
= k400
;
2117 static int k400_connect(struct hid_device
*hdev
, bool connected
)
2119 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2121 if (!disable_tap_to_click
)
2124 return k400_disable_tap_to_click(hidpp
);
2127 /* ------------------------------------------------------------------------- */
2128 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2129 /* ------------------------------------------------------------------------- */
2131 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2133 static int g920_get_config(struct hidpp_device
*hidpp
)
2139 /* Find feature and store for later use */
2140 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_G920_FORCE_FEEDBACK
,
2141 &feature_index
, &feature_type
);
2145 ret
= hidpp_ff_init(hidpp
, feature_index
);
2147 hid_warn(hidpp
->hid_dev
, "Unable to initialize force feedback support, errno %d\n",
2153 /* -------------------------------------------------------------------------- */
2154 /* Generic HID++ devices */
2155 /* -------------------------------------------------------------------------- */
2157 static int hidpp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2158 struct hid_field
*field
, struct hid_usage
*usage
,
2159 unsigned long **bit
, int *max
)
2161 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2163 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2164 return wtp_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
2165 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
&&
2166 field
->application
!= HID_GD_MOUSE
)
2167 return m560_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
2172 static int hidpp_input_mapped(struct hid_device
*hdev
, struct hid_input
*hi
,
2173 struct hid_field
*field
, struct hid_usage
*usage
,
2174 unsigned long **bit
, int *max
)
2176 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2178 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2179 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
2180 if (usage
->type
== EV_ABS
&& (usage
->code
== ABS_X
||
2181 usage
->code
== ABS_Y
|| usage
->code
== ABS_Z
||
2182 usage
->code
== ABS_RZ
)) {
2183 field
->application
= HID_GD_MULTIAXIS
;
2191 static void hidpp_populate_input(struct hidpp_device
*hidpp
,
2192 struct input_dev
*input
, bool origin_is_hid_core
)
2194 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2195 wtp_populate_input(hidpp
, input
, origin_is_hid_core
);
2196 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
2197 m560_populate_input(hidpp
, input
, origin_is_hid_core
);
2200 static int hidpp_input_configured(struct hid_device
*hdev
,
2201 struct hid_input
*hidinput
)
2203 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2204 struct input_dev
*input
= hidinput
->input
;
2206 hidpp_populate_input(hidpp
, input
, true);
2211 static int hidpp_raw_hidpp_event(struct hidpp_device
*hidpp
, u8
*data
,
2214 struct hidpp_report
*question
= hidpp
->send_receive_buf
;
2215 struct hidpp_report
*answer
= hidpp
->send_receive_buf
;
2216 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
2220 * If the mutex is locked then we have a pending answer from a
2221 * previously sent command.
2223 if (unlikely(mutex_is_locked(&hidpp
->send_mutex
))) {
2225 * Check for a correct hidpp20 answer or the corresponding
2228 if (hidpp_match_answer(question
, report
) ||
2229 hidpp_match_error(question
, report
)) {
2231 hidpp
->answer_available
= true;
2232 wake_up(&hidpp
->wait
);
2234 * This was an answer to a command that this driver sent
2235 * We return 1 to hid-core to avoid forwarding the
2236 * command upstream as it has been treated by the driver
2243 if (unlikely(hidpp_report_is_connect_event(report
))) {
2244 atomic_set(&hidpp
->connected
,
2245 !(report
->rap
.params
[0] & (1 << 6)));
2246 if (schedule_work(&hidpp
->work
) == 0)
2247 dbg_hid("%s: connect event already queued\n", __func__
);
2251 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP20_BATTERY
) {
2252 ret
= hidpp20_battery_event(hidpp
, data
, size
);
2260 static int hidpp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
2263 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2266 /* Generic HID++ processing. */
2268 case REPORT_ID_HIDPP_VERY_LONG
:
2269 if (size
!= HIDPP_REPORT_VERY_LONG_LENGTH
) {
2270 hid_err(hdev
, "received hid++ report of bad size (%d)",
2274 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2276 case REPORT_ID_HIDPP_LONG
:
2277 if (size
!= HIDPP_REPORT_LONG_LENGTH
) {
2278 hid_err(hdev
, "received hid++ report of bad size (%d)",
2282 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2284 case REPORT_ID_HIDPP_SHORT
:
2285 if (size
!= HIDPP_REPORT_SHORT_LENGTH
) {
2286 hid_err(hdev
, "received hid++ report of bad size (%d)",
2290 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2294 /* If no report is available for further processing, skip calling
2295 * raw_event of subclasses. */
2299 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2300 return wtp_raw_event(hdev
, data
, size
);
2301 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
2302 return m560_raw_event(hdev
, data
, size
);
2307 static int hidpp_initialize_battery(struct hidpp_device
*hidpp
)
2309 static atomic_t battery_no
= ATOMIC_INIT(0);
2310 struct power_supply_config cfg
= { .drv_data
= hidpp
};
2311 struct power_supply_desc
*desc
= &hidpp
->battery
.desc
;
2312 struct hidpp_battery
*battery
;
2316 if (hidpp
->battery
.ps
)
2319 if (hidpp
->protocol_major
>= 2) {
2320 ret
= hidpp20_query_battery_info(hidpp
);
2323 hidpp
->capabilities
|= HIDPP_CAPABILITY_HIDPP20_BATTERY
;
2328 battery
= &hidpp
->battery
;
2330 n
= atomic_inc_return(&battery_no
) - 1;
2331 desc
->properties
= hidpp_battery_props
;
2332 desc
->num_properties
= ARRAY_SIZE(hidpp_battery_props
);
2333 desc
->get_property
= hidpp_battery_get_property
;
2334 sprintf(battery
->name
, "hidpp_battery_%ld", n
);
2335 desc
->name
= battery
->name
;
2336 desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
2337 desc
->use_for_apm
= 0;
2339 battery
->ps
= devm_power_supply_register(&hidpp
->hid_dev
->dev
,
2342 if (IS_ERR(battery
->ps
))
2343 return PTR_ERR(battery
->ps
);
2345 power_supply_powers(battery
->ps
, &hidpp
->hid_dev
->dev
);
2350 static void hidpp_overwrite_name(struct hid_device
*hdev
)
2352 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2355 if (hidpp
->protocol_major
< 2)
2358 name
= hidpp_get_device_name(hidpp
);
2361 hid_err(hdev
, "unable to retrieve the name of the device");
2363 dbg_hid("HID++: Got name: %s\n", name
);
2364 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
2370 static int hidpp_input_open(struct input_dev
*dev
)
2372 struct hid_device
*hid
= input_get_drvdata(dev
);
2374 return hid_hw_open(hid
);
2377 static void hidpp_input_close(struct input_dev
*dev
)
2379 struct hid_device
*hid
= input_get_drvdata(dev
);
2384 static struct input_dev
*hidpp_allocate_input(struct hid_device
*hdev
)
2386 struct input_dev
*input_dev
= devm_input_allocate_device(&hdev
->dev
);
2387 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2392 input_set_drvdata(input_dev
, hdev
);
2393 input_dev
->open
= hidpp_input_open
;
2394 input_dev
->close
= hidpp_input_close
;
2396 input_dev
->name
= hidpp
->name
;
2397 input_dev
->phys
= hdev
->phys
;
2398 input_dev
->uniq
= hdev
->uniq
;
2399 input_dev
->id
.bustype
= hdev
->bus
;
2400 input_dev
->id
.vendor
= hdev
->vendor
;
2401 input_dev
->id
.product
= hdev
->product
;
2402 input_dev
->id
.version
= hdev
->version
;
2403 input_dev
->dev
.parent
= &hdev
->dev
;
2408 static void hidpp_connect_event(struct hidpp_device
*hidpp
)
2410 struct hid_device
*hdev
= hidpp
->hid_dev
;
2412 bool connected
= atomic_read(&hidpp
->connected
);
2413 struct input_dev
*input
;
2414 char *name
, *devm_name
;
2419 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
2420 ret
= wtp_connect(hdev
, connected
);
2423 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
2424 ret
= m560_send_config_command(hdev
, connected
);
2427 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
2428 ret
= k400_connect(hdev
, connected
);
2433 /* the device is already connected, we can ask for its name and
2435 if (!hidpp
->protocol_major
) {
2436 ret
= !hidpp_is_connected(hidpp
);
2438 hid_err(hdev
, "Can not get the protocol version.\n");
2441 hid_info(hdev
, "HID++ %u.%u device connected.\n",
2442 hidpp
->protocol_major
, hidpp
->protocol_minor
);
2445 if (hidpp
->name
== hdev
->name
&& hidpp
->protocol_major
>= 2) {
2446 name
= hidpp_get_device_name(hidpp
);
2449 "unable to retrieve the name of the device");
2453 devm_name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s", name
);
2458 hidpp
->name
= devm_name
;
2461 hidpp_initialize_battery(hidpp
);
2463 if (!(hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
) || hidpp
->delayed_input
)
2464 /* if the input nodes are already created, we can stop now */
2467 input
= hidpp_allocate_input(hdev
);
2469 hid_err(hdev
, "cannot allocate new input device: %d\n", ret
);
2473 hidpp_populate_input(hidpp
, input
, false);
2475 ret
= input_register_device(input
);
2477 input_free_device(input
);
2479 hidpp
->delayed_input
= input
;
2482 static int hidpp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2484 struct hidpp_device
*hidpp
;
2487 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
2489 hidpp
= devm_kzalloc(&hdev
->dev
, sizeof(struct hidpp_device
),
2494 hidpp
->hid_dev
= hdev
;
2495 hidpp
->name
= hdev
->name
;
2496 hid_set_drvdata(hdev
, hidpp
);
2498 hidpp
->quirks
= id
->driver_data
;
2500 if (id
->group
== HID_GROUP_LOGITECH_DJ_DEVICE
)
2501 hidpp
->quirks
|= HIDPP_QUIRK_UNIFYING
;
2503 if (disable_raw_mode
) {
2504 hidpp
->quirks
&= ~HIDPP_QUIRK_CLASS_WTP
;
2505 hidpp
->quirks
&= ~HIDPP_QUIRK_NO_HIDINPUT
;
2508 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
2509 ret
= wtp_allocate(hdev
, id
);
2512 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
2513 ret
= m560_allocate(hdev
);
2516 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
2517 ret
= k400_allocate(hdev
);
2522 INIT_WORK(&hidpp
->work
, delayed_work_cb
);
2523 mutex_init(&hidpp
->send_mutex
);
2524 init_waitqueue_head(&hidpp
->wait
);
2526 ret
= hid_parse(hdev
);
2528 hid_err(hdev
, "%s:parse failed\n", __func__
);
2529 goto hid_parse_fail
;
2532 if (hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
)
2533 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
2535 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
2536 ret
= hid_hw_start(hdev
, connect_mask
);
2538 hid_err(hdev
, "hw start failed\n");
2539 goto hid_hw_start_fail
;
2541 ret
= hid_hw_open(hdev
);
2543 dev_err(&hdev
->dev
, "%s:hid_hw_open returned error:%d\n",
2546 goto hid_hw_start_fail
;
2551 /* Allow incoming packets */
2552 hid_device_io_start(hdev
);
2554 if (hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)
2555 hidpp_unifying_init(hidpp
);
2557 connected
= hidpp_is_connected(hidpp
);
2558 atomic_set(&hidpp
->connected
, connected
);
2559 if (!(hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)) {
2562 hid_err(hdev
, "Device not connected");
2563 goto hid_hw_open_failed
;
2566 hid_info(hdev
, "HID++ %u.%u device connected.\n",
2567 hidpp
->protocol_major
, hidpp
->protocol_minor
);
2569 hidpp_overwrite_name(hdev
);
2572 if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)) {
2573 ret
= wtp_get_config(hidpp
);
2575 goto hid_hw_open_failed
;
2576 } else if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
)) {
2577 ret
= g920_get_config(hidpp
);
2579 goto hid_hw_open_failed
;
2582 /* Block incoming packets */
2583 hid_device_io_stop(hdev
);
2585 if (!(hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
)) {
2586 ret
= hid_hw_start(hdev
, connect_mask
);
2588 hid_err(hdev
, "%s:hid_hw_start returned error\n", __func__
);
2589 goto hid_hw_start_fail
;
2593 /* Allow incoming packets */
2594 hid_device_io_start(hdev
);
2596 hidpp_connect_event(hidpp
);
2601 hid_device_io_stop(hdev
);
2602 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
2608 cancel_work_sync(&hidpp
->work
);
2609 mutex_destroy(&hidpp
->send_mutex
);
2611 hid_set_drvdata(hdev
, NULL
);
2615 static void hidpp_remove(struct hid_device
*hdev
)
2617 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2619 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
2620 hidpp_ff_deinit(hdev
);
2624 cancel_work_sync(&hidpp
->work
);
2625 mutex_destroy(&hidpp
->send_mutex
);
2628 static const struct hid_device_id hidpp_devices
[] = {
2629 { /* wireless touchpad */
2630 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
2631 USB_VENDOR_ID_LOGITECH
, 0x4011),
2632 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
|
2633 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
},
2634 { /* wireless touchpad T650 */
2635 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
2636 USB_VENDOR_ID_LOGITECH
, 0x4101),
2637 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
},
2638 { /* wireless touchpad T651 */
2639 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
,
2640 USB_DEVICE_ID_LOGITECH_T651
),
2641 .driver_data
= HIDPP_QUIRK_CLASS_WTP
},
2642 { /* Mouse logitech M560 */
2643 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
2644 USB_VENDOR_ID_LOGITECH
, 0x402d),
2645 .driver_data
= HIDPP_QUIRK_DELAYED_INIT
| HIDPP_QUIRK_CLASS_M560
},
2646 { /* Keyboard logitech K400 */
2647 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
2648 USB_VENDOR_ID_LOGITECH
, 0x4024),
2649 .driver_data
= HIDPP_QUIRK_CLASS_K400
},
2651 { HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
2652 USB_VENDOR_ID_LOGITECH
, HID_ANY_ID
)},
2654 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, USB_DEVICE_ID_LOGITECH_G920_WHEEL
),
2655 .driver_data
= HIDPP_QUIRK_CLASS_G920
| HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
},
2659 MODULE_DEVICE_TABLE(hid
, hidpp_devices
);
2661 static struct hid_driver hidpp_driver
= {
2662 .name
= "logitech-hidpp-device",
2663 .id_table
= hidpp_devices
,
2664 .probe
= hidpp_probe
,
2665 .remove
= hidpp_remove
,
2666 .raw_event
= hidpp_raw_event
,
2667 .input_configured
= hidpp_input_configured
,
2668 .input_mapping
= hidpp_input_mapping
,
2669 .input_mapped
= hidpp_input_mapped
,
2672 module_hid_driver(hidpp_driver
);