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)
59 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
61 /* bits 2..20 are reserved for classes */
62 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
63 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
64 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
65 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
66 #define HIDPP_QUIRK_UNIFYING BIT(25)
68 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
70 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
71 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
72 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
73 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
76 * There are two hidpp protocols in use, the first version hidpp10 is known
77 * as register access protocol or RAP, the second version hidpp20 is known as
78 * feature access protocol or FAP
80 * Most older devices (including the Unifying usb receiver) use the RAP protocol
81 * where as most newer devices use the FAP protocol. Both protocols are
82 * compatible with the underlying transport, which could be usb, Unifiying, or
83 * bluetooth. The message lengths are defined by the hid vendor specific report
84 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
85 * the HIDPP_LONG report type (total message length 20 bytes)
87 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
88 * messages. The Unifying receiver itself responds to RAP messages (device index
89 * is 0xFF for the receiver), and all messages (short or long) with a device
90 * index between 1 and 6 are passed untouched to the corresponding paired
93 * The paired device can be RAP or FAP, it will receive the message untouched
94 * from the Unifiying receiver.
99 u8 funcindex_clientid
;
100 u8 params
[HIDPP_REPORT_VERY_LONG_LENGTH
- 4U];
106 u8 params
[HIDPP_REPORT_VERY_LONG_LENGTH
- 4U];
109 struct hidpp_report
{
115 u8 rawbytes
[sizeof(struct fap
)];
119 struct hidpp_battery
{
121 u8 solar_feature_index
;
122 struct power_supply_desc desc
;
123 struct power_supply
*ps
;
131 struct hidpp_device
{
132 struct hid_device
*hid_dev
;
133 struct mutex send_mutex
;
134 void *send_receive_buf
;
135 char *name
; /* will never be NULL and should not be freed */
136 wait_queue_head_t wait
;
137 bool answer_available
;
143 struct work_struct work
;
144 struct kfifo delayed_work_fifo
;
146 struct input_dev
*delayed_input
;
148 unsigned long quirks
;
149 unsigned long capabilities
;
151 struct hidpp_battery battery
;
154 /* HID++ 1.0 error codes */
155 #define HIDPP_ERROR 0x8f
156 #define HIDPP_ERROR_SUCCESS 0x00
157 #define HIDPP_ERROR_INVALID_SUBID 0x01
158 #define HIDPP_ERROR_INVALID_ADRESS 0x02
159 #define HIDPP_ERROR_INVALID_VALUE 0x03
160 #define HIDPP_ERROR_CONNECT_FAIL 0x04
161 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
162 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
163 #define HIDPP_ERROR_BUSY 0x07
164 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
165 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
166 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
167 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
168 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
169 /* HID++ 2.0 error codes */
170 #define HIDPP20_ERROR 0xff
172 static void hidpp_connect_event(struct hidpp_device
*hidpp_dev
);
174 static int __hidpp_send_report(struct hid_device
*hdev
,
175 struct hidpp_report
*hidpp_report
)
177 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
178 int fields_count
, ret
;
180 hidpp
= hid_get_drvdata(hdev
);
182 switch (hidpp_report
->report_id
) {
183 case REPORT_ID_HIDPP_SHORT
:
184 fields_count
= HIDPP_REPORT_SHORT_LENGTH
;
186 case REPORT_ID_HIDPP_LONG
:
187 fields_count
= HIDPP_REPORT_LONG_LENGTH
;
189 case REPORT_ID_HIDPP_VERY_LONG
:
190 fields_count
= HIDPP_REPORT_VERY_LONG_LENGTH
;
197 * set the device_index as the receiver, it will be overwritten by
198 * hid_hw_request if needed
200 hidpp_report
->device_index
= 0xff;
202 if (hidpp
->quirks
& HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
) {
203 ret
= hid_hw_output_report(hdev
, (u8
*)hidpp_report
, fields_count
);
205 ret
= hid_hw_raw_request(hdev
, hidpp_report
->report_id
,
206 (u8
*)hidpp_report
, fields_count
, HID_OUTPUT_REPORT
,
210 return ret
== fields_count
? 0 : -1;
214 * hidpp_send_message_sync() returns 0 in case of success, and something else
215 * in case of a failure.
216 * - If ' something else' is positive, that means that an error has been raised
217 * by the protocol itself.
218 * - If ' something else' is negative, that means that we had a classic error
219 * (-ENOMEM, -EPIPE, etc...)
221 static int hidpp_send_message_sync(struct hidpp_device
*hidpp
,
222 struct hidpp_report
*message
,
223 struct hidpp_report
*response
)
227 mutex_lock(&hidpp
->send_mutex
);
229 hidpp
->send_receive_buf
= response
;
230 hidpp
->answer_available
= false;
233 * So that we can later validate the answer when it arrives
236 *response
= *message
;
238 ret
= __hidpp_send_report(hidpp
->hid_dev
, message
);
241 dbg_hid("__hidpp_send_report returned err: %d\n", ret
);
242 memset(response
, 0, sizeof(struct hidpp_report
));
246 if (!wait_event_timeout(hidpp
->wait
, hidpp
->answer_available
,
248 dbg_hid("%s:timeout waiting for response\n", __func__
);
249 memset(response
, 0, sizeof(struct hidpp_report
));
253 if (response
->report_id
== REPORT_ID_HIDPP_SHORT
&&
254 response
->rap
.sub_id
== HIDPP_ERROR
) {
255 ret
= response
->rap
.params
[1];
256 dbg_hid("%s:got hidpp error %02X\n", __func__
, ret
);
260 if ((response
->report_id
== REPORT_ID_HIDPP_LONG
||
261 response
->report_id
== REPORT_ID_HIDPP_VERY_LONG
) &&
262 response
->fap
.feature_index
== HIDPP20_ERROR
) {
263 ret
= response
->fap
.params
[1];
264 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__
, ret
);
269 mutex_unlock(&hidpp
->send_mutex
);
274 static int hidpp_send_fap_command_sync(struct hidpp_device
*hidpp
,
275 u8 feat_index
, u8 funcindex_clientid
, u8
*params
, int param_count
,
276 struct hidpp_report
*response
)
278 struct hidpp_report
*message
;
281 if (param_count
> sizeof(message
->fap
.params
))
284 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
288 if (param_count
> (HIDPP_REPORT_LONG_LENGTH
- 4))
289 message
->report_id
= REPORT_ID_HIDPP_VERY_LONG
;
291 message
->report_id
= REPORT_ID_HIDPP_LONG
;
292 message
->fap
.feature_index
= feat_index
;
293 message
->fap
.funcindex_clientid
= funcindex_clientid
;
294 memcpy(&message
->fap
.params
, params
, param_count
);
296 ret
= hidpp_send_message_sync(hidpp
, message
, response
);
301 static int hidpp_send_rap_command_sync(struct hidpp_device
*hidpp_dev
,
302 u8 report_id
, u8 sub_id
, u8 reg_address
, u8
*params
, int param_count
,
303 struct hidpp_report
*response
)
305 struct hidpp_report
*message
;
309 case REPORT_ID_HIDPP_SHORT
:
310 max_count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
312 case REPORT_ID_HIDPP_LONG
:
313 max_count
= HIDPP_REPORT_LONG_LENGTH
- 4;
315 case REPORT_ID_HIDPP_VERY_LONG
:
316 max_count
= HIDPP_REPORT_VERY_LONG_LENGTH
- 4;
322 if (param_count
> max_count
)
325 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
328 message
->report_id
= report_id
;
329 message
->rap
.sub_id
= sub_id
;
330 message
->rap
.reg_address
= reg_address
;
331 memcpy(&message
->rap
.params
, params
, param_count
);
333 ret
= hidpp_send_message_sync(hidpp_dev
, message
, response
);
338 static void delayed_work_cb(struct work_struct
*work
)
340 struct hidpp_device
*hidpp
= container_of(work
, struct hidpp_device
,
342 hidpp_connect_event(hidpp
);
345 static inline bool hidpp_match_answer(struct hidpp_report
*question
,
346 struct hidpp_report
*answer
)
348 return (answer
->fap
.feature_index
== question
->fap
.feature_index
) &&
349 (answer
->fap
.funcindex_clientid
== question
->fap
.funcindex_clientid
);
352 static inline bool hidpp_match_error(struct hidpp_report
*question
,
353 struct hidpp_report
*answer
)
355 return ((answer
->rap
.sub_id
== HIDPP_ERROR
) ||
356 (answer
->fap
.feature_index
== HIDPP20_ERROR
)) &&
357 (answer
->fap
.funcindex_clientid
== question
->fap
.feature_index
) &&
358 (answer
->fap
.params
[0] == question
->fap
.funcindex_clientid
);
361 static inline bool hidpp_report_is_connect_event(struct hidpp_report
*report
)
363 return (report
->report_id
== REPORT_ID_HIDPP_SHORT
) &&
364 (report
->rap
.sub_id
== 0x41);
368 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
370 static void hidpp_prefix_name(char **name
, int name_length
)
372 #define PREFIX_LENGTH 9 /* "Logitech " */
377 if (name_length
> PREFIX_LENGTH
&&
378 strncmp(*name
, "Logitech ", PREFIX_LENGTH
) == 0)
379 /* The prefix has is already in the name */
382 new_length
= PREFIX_LENGTH
+ name_length
;
383 new_name
= kzalloc(new_length
, GFP_KERNEL
);
387 snprintf(new_name
, new_length
, "Logitech %s", *name
);
394 /* -------------------------------------------------------------------------- */
395 /* HIDP++ 1.0 commands */
396 /* -------------------------------------------------------------------------- */
398 #define HIDPP_SET_REGISTER 0x80
399 #define HIDPP_GET_REGISTER 0x81
400 #define HIDPP_SET_LONG_REGISTER 0x82
401 #define HIDPP_GET_LONG_REGISTER 0x83
403 #define HIDPP_REG_GENERAL 0x00
405 static int hidpp10_enable_battery_reporting(struct hidpp_device
*hidpp_dev
)
407 struct hidpp_report response
;
409 u8 params
[3] = { 0 };
411 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
412 REPORT_ID_HIDPP_SHORT
,
419 memcpy(params
, response
.rap
.params
, 3);
421 /* Set the battery bit */
424 return hidpp_send_rap_command_sync(hidpp_dev
,
425 REPORT_ID_HIDPP_SHORT
,
428 params
, 3, &response
);
431 #define HIDPP_REG_BATTERY_STATUS 0x07
433 static int hidpp10_battery_status_map_level(u8 param
)
439 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
442 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
445 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
448 level
= POWER_SUPPLY_CAPACITY_LEVEL_HIGH
;
451 level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
457 static int hidpp10_battery_status_map_status(u8 param
)
463 /* discharging (in use) */
464 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
466 case 0x21: /* (standard) charging */
467 case 0x24: /* fast charging */
468 case 0x25: /* slow charging */
469 status
= POWER_SUPPLY_STATUS_CHARGING
;
471 case 0x26: /* topping charge */
472 case 0x22: /* charge complete */
473 status
= POWER_SUPPLY_STATUS_FULL
;
475 case 0x20: /* unknown */
476 status
= POWER_SUPPLY_STATUS_UNKNOWN
;
479 * 0x01...0x1F = reserved (not charging)
480 * 0x23 = charging error
481 * 0x27..0xff = reserved
484 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
491 static int hidpp10_query_battery_status(struct hidpp_device
*hidpp
)
493 struct hidpp_report response
;
496 ret
= hidpp_send_rap_command_sync(hidpp
,
497 REPORT_ID_HIDPP_SHORT
,
499 HIDPP_REG_BATTERY_STATUS
,
504 hidpp
->battery
.level
=
505 hidpp10_battery_status_map_level(response
.rap
.params
[0]);
506 status
= hidpp10_battery_status_map_status(response
.rap
.params
[1]);
507 hidpp
->battery
.status
= status
;
508 /* the capacity is only available when discharging or full */
509 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
510 status
== POWER_SUPPLY_STATUS_FULL
;
515 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
517 static int hidpp10_battery_mileage_map_status(u8 param
)
521 switch (param
>> 6) {
523 /* discharging (in use) */
524 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
526 case 0x01: /* charging */
527 status
= POWER_SUPPLY_STATUS_CHARGING
;
529 case 0x02: /* charge complete */
530 status
= POWER_SUPPLY_STATUS_FULL
;
533 * 0x03 = charging error
536 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
543 static int hidpp10_query_battery_mileage(struct hidpp_device
*hidpp
)
545 struct hidpp_report response
;
548 ret
= hidpp_send_rap_command_sync(hidpp
,
549 REPORT_ID_HIDPP_SHORT
,
551 HIDPP_REG_BATTERY_MILEAGE
,
556 hidpp
->battery
.capacity
= response
.rap
.params
[0];
557 status
= hidpp10_battery_mileage_map_status(response
.rap
.params
[2]);
558 hidpp
->battery
.status
= status
;
559 /* the capacity is only available when discharging or full */
560 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
561 status
== POWER_SUPPLY_STATUS_FULL
;
566 static int hidpp10_battery_event(struct hidpp_device
*hidpp
, u8
*data
, int size
)
568 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
569 int status
, capacity
, level
;
572 if (report
->report_id
!= REPORT_ID_HIDPP_SHORT
)
575 switch (report
->rap
.sub_id
) {
576 case HIDPP_REG_BATTERY_STATUS
:
577 capacity
= hidpp
->battery
.capacity
;
578 level
= hidpp10_battery_status_map_level(report
->rawbytes
[1]);
579 status
= hidpp10_battery_status_map_status(report
->rawbytes
[2]);
581 case HIDPP_REG_BATTERY_MILEAGE
:
582 capacity
= report
->rap
.params
[0];
583 level
= hidpp
->battery
.level
;
584 status
= hidpp10_battery_mileage_map_status(report
->rawbytes
[3]);
590 changed
= capacity
!= hidpp
->battery
.capacity
||
591 level
!= hidpp
->battery
.level
||
592 status
!= hidpp
->battery
.status
;
594 /* the capacity is only available when discharging or full */
595 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
596 status
== POWER_SUPPLY_STATUS_FULL
;
599 hidpp
->battery
.level
= level
;
600 hidpp
->battery
.status
= status
;
601 if (hidpp
->battery
.ps
)
602 power_supply_changed(hidpp
->battery
.ps
);
608 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
609 #define HIDPP_EXTENDED_PAIRING 0x30
610 #define HIDPP_DEVICE_NAME 0x40
612 static char *hidpp_unifying_get_name(struct hidpp_device
*hidpp_dev
)
614 struct hidpp_report response
;
616 u8 params
[1] = { HIDPP_DEVICE_NAME
};
620 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
621 REPORT_ID_HIDPP_SHORT
,
622 HIDPP_GET_LONG_REGISTER
,
623 HIDPP_REG_PAIRING_INFORMATION
,
624 params
, 1, &response
);
628 len
= response
.rap
.params
[1];
630 if (2 + len
> sizeof(response
.rap
.params
))
633 name
= kzalloc(len
+ 1, GFP_KERNEL
);
637 memcpy(name
, &response
.rap
.params
[2], len
);
639 /* include the terminating '\0' */
640 hidpp_prefix_name(&name
, len
+ 1);
645 static int hidpp_unifying_get_serial(struct hidpp_device
*hidpp
, u32
*serial
)
647 struct hidpp_report response
;
649 u8 params
[1] = { HIDPP_EXTENDED_PAIRING
};
651 ret
= hidpp_send_rap_command_sync(hidpp
,
652 REPORT_ID_HIDPP_SHORT
,
653 HIDPP_GET_LONG_REGISTER
,
654 HIDPP_REG_PAIRING_INFORMATION
,
655 params
, 1, &response
);
660 * We don't care about LE or BE, we will output it as a string
661 * with %4phD, so we need to keep the order.
663 *serial
= *((u32
*)&response
.rap
.params
[1]);
667 static int hidpp_unifying_init(struct hidpp_device
*hidpp
)
669 struct hid_device
*hdev
= hidpp
->hid_dev
;
674 ret
= hidpp_unifying_get_serial(hidpp
, &serial
);
678 snprintf(hdev
->uniq
, sizeof(hdev
->uniq
), "%04x-%4phD",
679 hdev
->product
, &serial
);
680 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev
->uniq
);
682 name
= hidpp_unifying_get_name(hidpp
);
686 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
687 dbg_hid("HID++ Unifying: Got name: %s\n", name
);
693 /* -------------------------------------------------------------------------- */
695 /* -------------------------------------------------------------------------- */
697 #define HIDPP_PAGE_ROOT 0x0000
698 #define HIDPP_PAGE_ROOT_IDX 0x00
700 #define CMD_ROOT_GET_FEATURE 0x01
701 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
703 static int hidpp_root_get_feature(struct hidpp_device
*hidpp
, u16 feature
,
704 u8
*feature_index
, u8
*feature_type
)
706 struct hidpp_report response
;
708 u8 params
[2] = { feature
>> 8, feature
& 0x00FF };
710 ret
= hidpp_send_fap_command_sync(hidpp
,
712 CMD_ROOT_GET_FEATURE
,
713 params
, 2, &response
);
717 if (response
.fap
.params
[0] == 0)
720 *feature_index
= response
.fap
.params
[0];
721 *feature_type
= response
.fap
.params
[1];
726 static int hidpp_root_get_protocol_version(struct hidpp_device
*hidpp
)
728 struct hidpp_report response
;
731 ret
= hidpp_send_fap_command_sync(hidpp
,
733 CMD_ROOT_GET_PROTOCOL_VERSION
,
736 if (ret
== HIDPP_ERROR_INVALID_SUBID
) {
737 hidpp
->protocol_major
= 1;
738 hidpp
->protocol_minor
= 0;
742 /* the device might not be connected */
743 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
747 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
754 hidpp
->protocol_major
= response
.fap
.params
[0];
755 hidpp
->protocol_minor
= response
.fap
.params
[1];
760 static bool hidpp_is_connected(struct hidpp_device
*hidpp
)
764 ret
= hidpp_root_get_protocol_version(hidpp
);
766 hid_dbg(hidpp
->hid_dev
, "HID++ %u.%u device connected.\n",
767 hidpp
->protocol_major
, hidpp
->protocol_minor
);
771 /* -------------------------------------------------------------------------- */
772 /* 0x0005: GetDeviceNameType */
773 /* -------------------------------------------------------------------------- */
775 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
777 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
778 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
779 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
781 static int hidpp_devicenametype_get_count(struct hidpp_device
*hidpp
,
782 u8 feature_index
, u8
*nameLength
)
784 struct hidpp_report response
;
787 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
788 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT
, NULL
, 0, &response
);
791 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
798 *nameLength
= response
.fap
.params
[0];
803 static int hidpp_devicenametype_get_device_name(struct hidpp_device
*hidpp
,
804 u8 feature_index
, u8 char_index
, char *device_name
, int len_buf
)
806 struct hidpp_report response
;
810 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
811 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME
, &char_index
, 1,
815 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
822 switch (response
.report_id
) {
823 case REPORT_ID_HIDPP_VERY_LONG
:
824 count
= HIDPP_REPORT_VERY_LONG_LENGTH
- 4;
826 case REPORT_ID_HIDPP_LONG
:
827 count
= HIDPP_REPORT_LONG_LENGTH
- 4;
829 case REPORT_ID_HIDPP_SHORT
:
830 count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
839 for (i
= 0; i
< count
; i
++)
840 device_name
[i
] = response
.fap
.params
[i
];
845 static char *hidpp_get_device_name(struct hidpp_device
*hidpp
)
854 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_GET_DEVICE_NAME_TYPE
,
855 &feature_index
, &feature_type
);
859 ret
= hidpp_devicenametype_get_count(hidpp
, feature_index
,
864 name
= kzalloc(__name_length
+ 1, GFP_KERNEL
);
868 while (index
< __name_length
) {
869 ret
= hidpp_devicenametype_get_device_name(hidpp
,
870 feature_index
, index
, name
+ index
,
871 __name_length
- index
);
879 /* include the terminating '\0' */
880 hidpp_prefix_name(&name
, __name_length
+ 1);
885 /* -------------------------------------------------------------------------- */
886 /* 0x1000: Battery level status */
887 /* -------------------------------------------------------------------------- */
889 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
891 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
892 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
894 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
896 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
897 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
898 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
900 static int hidpp_map_battery_level(int capacity
)
903 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
904 else if (capacity
< 31)
905 return POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
906 else if (capacity
< 81)
907 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
908 return POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
911 static int hidpp20_batterylevel_map_status_capacity(u8 data
[3], int *capacity
,
918 *next_capacity
= data
[1];
919 *level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
921 /* When discharging, we can rely on the device reported capacity.
922 * For all other states the device reports 0 (unknown).
925 case 0: /* discharging (in use) */
926 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
927 *level
= hidpp_map_battery_level(*capacity
);
929 case 1: /* recharging */
930 status
= POWER_SUPPLY_STATUS_CHARGING
;
932 case 2: /* charge in final stage */
933 status
= POWER_SUPPLY_STATUS_CHARGING
;
935 case 3: /* charge complete */
936 status
= POWER_SUPPLY_STATUS_FULL
;
937 *level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
940 case 4: /* recharging below optimal speed */
941 status
= POWER_SUPPLY_STATUS_CHARGING
;
943 /* 5 = invalid battery type
945 7 = other charging error */
947 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
954 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device
*hidpp
,
961 struct hidpp_report response
;
963 u8
*params
= (u8
*)response
.fap
.params
;
965 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
966 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS
,
969 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
976 *status
= hidpp20_batterylevel_map_status_capacity(params
, capacity
,
983 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device
*hidpp
,
986 struct hidpp_report response
;
988 u8
*params
= (u8
*)response
.fap
.params
;
989 unsigned int level_count
, flags
;
991 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
992 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY
,
995 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1002 level_count
= params
[0];
1005 if (level_count
< 10 || !(flags
& FLAG_BATTERY_LEVEL_MILEAGE
))
1006 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
;
1008 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
1013 static int hidpp20_query_battery_info(struct hidpp_device
*hidpp
)
1017 int status
, capacity
, next_capacity
, level
;
1019 if (hidpp
->battery
.feature_index
== 0xff) {
1020 ret
= hidpp_root_get_feature(hidpp
,
1021 HIDPP_PAGE_BATTERY_LEVEL_STATUS
,
1022 &hidpp
->battery
.feature_index
,
1028 ret
= hidpp20_batterylevel_get_battery_capacity(hidpp
,
1029 hidpp
->battery
.feature_index
,
1031 &next_capacity
, &level
);
1035 ret
= hidpp20_batterylevel_get_battery_info(hidpp
,
1036 hidpp
->battery
.feature_index
);
1040 hidpp
->battery
.status
= status
;
1041 hidpp
->battery
.capacity
= capacity
;
1042 hidpp
->battery
.level
= level
;
1043 /* the capacity is only available when discharging or full */
1044 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
1045 status
== POWER_SUPPLY_STATUS_FULL
;
1050 static int hidpp20_battery_event(struct hidpp_device
*hidpp
,
1053 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1054 int status
, capacity
, next_capacity
, level
;
1057 if (report
->fap
.feature_index
!= hidpp
->battery
.feature_index
||
1058 report
->fap
.funcindex_clientid
!= EVENT_BATTERY_LEVEL_STATUS_BROADCAST
)
1061 status
= hidpp20_batterylevel_map_status_capacity(report
->fap
.params
,
1066 /* the capacity is only available when discharging or full */
1067 hidpp
->battery
.online
= status
== POWER_SUPPLY_STATUS_DISCHARGING
||
1068 status
== POWER_SUPPLY_STATUS_FULL
;
1070 changed
= capacity
!= hidpp
->battery
.capacity
||
1071 level
!= hidpp
->battery
.level
||
1072 status
!= hidpp
->battery
.status
;
1075 hidpp
->battery
.level
= level
;
1076 hidpp
->battery
.capacity
= capacity
;
1077 hidpp
->battery
.status
= status
;
1078 if (hidpp
->battery
.ps
)
1079 power_supply_changed(hidpp
->battery
.ps
);
1085 static enum power_supply_property hidpp_battery_props
[] = {
1086 POWER_SUPPLY_PROP_ONLINE
,
1087 POWER_SUPPLY_PROP_STATUS
,
1088 POWER_SUPPLY_PROP_SCOPE
,
1089 POWER_SUPPLY_PROP_MODEL_NAME
,
1090 POWER_SUPPLY_PROP_MANUFACTURER
,
1091 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
1092 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1093 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1096 static int hidpp_battery_get_property(struct power_supply
*psy
,
1097 enum power_supply_property psp
,
1098 union power_supply_propval
*val
)
1100 struct hidpp_device
*hidpp
= power_supply_get_drvdata(psy
);
1104 case POWER_SUPPLY_PROP_STATUS
:
1105 val
->intval
= hidpp
->battery
.status
;
1107 case POWER_SUPPLY_PROP_CAPACITY
:
1108 val
->intval
= hidpp
->battery
.capacity
;
1110 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
1111 val
->intval
= hidpp
->battery
.level
;
1113 case POWER_SUPPLY_PROP_SCOPE
:
1114 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1116 case POWER_SUPPLY_PROP_ONLINE
:
1117 val
->intval
= hidpp
->battery
.online
;
1119 case POWER_SUPPLY_PROP_MODEL_NAME
:
1120 if (!strncmp(hidpp
->name
, "Logitech ", 9))
1121 val
->strval
= hidpp
->name
+ 9;
1123 val
->strval
= hidpp
->name
;
1125 case POWER_SUPPLY_PROP_MANUFACTURER
:
1126 val
->strval
= "Logitech";
1128 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
1129 val
->strval
= hidpp
->hid_dev
->uniq
;
1139 /* -------------------------------------------------------------------------- */
1140 /* 0x4301: Solar Keyboard */
1141 /* -------------------------------------------------------------------------- */
1143 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1145 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1147 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1148 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1149 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1151 static int hidpp_solar_request_battery_event(struct hidpp_device
*hidpp
)
1153 struct hidpp_report response
;
1154 u8 params
[2] = { 1, 1 };
1158 if (hidpp
->battery
.feature_index
== 0xff) {
1159 ret
= hidpp_root_get_feature(hidpp
,
1160 HIDPP_PAGE_SOLAR_KEYBOARD
,
1161 &hidpp
->battery
.solar_feature_index
,
1167 ret
= hidpp_send_fap_command_sync(hidpp
,
1168 hidpp
->battery
.solar_feature_index
,
1169 CMD_SOLAR_SET_LIGHT_MEASURE
,
1170 params
, 2, &response
);
1172 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1179 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
1184 static int hidpp_solar_battery_event(struct hidpp_device
*hidpp
,
1187 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1188 int capacity
, lux
, status
;
1191 function
= report
->fap
.funcindex_clientid
;
1194 if (report
->fap
.feature_index
!= hidpp
->battery
.solar_feature_index
||
1195 !(function
== EVENT_SOLAR_BATTERY_BROADCAST
||
1196 function
== EVENT_SOLAR_BATTERY_LIGHT_MEASURE
||
1197 function
== EVENT_SOLAR_CHECK_LIGHT_BUTTON
))
1200 capacity
= report
->fap
.params
[0];
1203 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE
:
1204 lux
= (report
->fap
.params
[1] << 8) | report
->fap
.params
[2];
1206 status
= POWER_SUPPLY_STATUS_CHARGING
;
1208 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1210 case EVENT_SOLAR_CHECK_LIGHT_BUTTON
:
1212 if (capacity
< hidpp
->battery
.capacity
)
1213 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
1215 status
= POWER_SUPPLY_STATUS_CHARGING
;
1219 if (capacity
== 100)
1220 status
= POWER_SUPPLY_STATUS_FULL
;
1222 hidpp
->battery
.online
= true;
1223 if (capacity
!= hidpp
->battery
.capacity
||
1224 status
!= hidpp
->battery
.status
) {
1225 hidpp
->battery
.capacity
= capacity
;
1226 hidpp
->battery
.status
= status
;
1227 if (hidpp
->battery
.ps
)
1228 power_supply_changed(hidpp
->battery
.ps
);
1234 /* -------------------------------------------------------------------------- */
1235 /* 0x6010: Touchpad FW items */
1236 /* -------------------------------------------------------------------------- */
1238 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1240 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1242 struct hidpp_touchpad_fw_items
{
1244 uint8_t desired_state
;
1250 * send a set state command to the device by reading the current items->state
1251 * field. items is then filled with the current state.
1253 static int hidpp_touchpad_fw_items_set(struct hidpp_device
*hidpp
,
1255 struct hidpp_touchpad_fw_items
*items
)
1257 struct hidpp_report response
;
1259 u8
*params
= (u8
*)response
.fap
.params
;
1261 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1262 CMD_TOUCHPAD_FW_ITEMS_SET
, &items
->state
, 1, &response
);
1265 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1272 items
->presence
= params
[0];
1273 items
->desired_state
= params
[1];
1274 items
->state
= params
[2];
1275 items
->persistent
= params
[3];
1280 /* -------------------------------------------------------------------------- */
1281 /* 0x6100: TouchPadRawXY */
1282 /* -------------------------------------------------------------------------- */
1284 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1286 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1287 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1289 #define EVENT_TOUCHPAD_RAW_XY 0x00
1291 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1292 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1294 struct hidpp_touchpad_raw_info
{
1305 struct hidpp_touchpad_raw_xy_finger
{
1315 struct hidpp_touchpad_raw_xy
{
1317 struct hidpp_touchpad_raw_xy_finger fingers
[2];
1324 static int hidpp_touchpad_get_raw_info(struct hidpp_device
*hidpp
,
1325 u8 feature_index
, struct hidpp_touchpad_raw_info
*raw_info
)
1327 struct hidpp_report response
;
1329 u8
*params
= (u8
*)response
.fap
.params
;
1331 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1332 CMD_TOUCHPAD_GET_RAW_INFO
, NULL
, 0, &response
);
1335 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1342 raw_info
->x_size
= get_unaligned_be16(¶ms
[0]);
1343 raw_info
->y_size
= get_unaligned_be16(¶ms
[2]);
1344 raw_info
->z_range
= params
[4];
1345 raw_info
->area_range
= params
[5];
1346 raw_info
->maxcontacts
= params
[7];
1347 raw_info
->origin
= params
[8];
1348 /* res is given in unit per inch */
1349 raw_info
->res
= get_unaligned_be16(¶ms
[13]) * 2 / 51;
1354 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device
*hidpp_dev
,
1355 u8 feature_index
, bool send_raw_reports
,
1356 bool sensor_enhanced_settings
)
1358 struct hidpp_report response
;
1362 * bit 0 - enable raw
1363 * bit 1 - 16bit Z, no area
1364 * bit 2 - enhanced sensitivity
1365 * bit 3 - width, height (4 bits each) instead of area
1366 * bit 4 - send raw + gestures (degrades smoothness)
1367 * remaining bits - reserved
1369 u8 params
= send_raw_reports
| (sensor_enhanced_settings
<< 2);
1371 return hidpp_send_fap_command_sync(hidpp_dev
, feature_index
,
1372 CMD_TOUCHPAD_SET_RAW_REPORT_STATE
, ¶ms
, 1, &response
);
1375 static void hidpp_touchpad_touch_event(u8
*data
,
1376 struct hidpp_touchpad_raw_xy_finger
*finger
)
1378 u8 x_m
= data
[0] << 2;
1379 u8 y_m
= data
[2] << 2;
1381 finger
->x
= x_m
<< 6 | data
[1];
1382 finger
->y
= y_m
<< 6 | data
[3];
1384 finger
->contact_type
= data
[0] >> 6;
1385 finger
->contact_status
= data
[2] >> 6;
1387 finger
->z
= data
[4];
1388 finger
->area
= data
[5];
1389 finger
->finger_id
= data
[6] >> 4;
1392 static void hidpp_touchpad_raw_xy_event(struct hidpp_device
*hidpp_dev
,
1393 u8
*data
, struct hidpp_touchpad_raw_xy
*raw_xy
)
1395 memset(raw_xy
, 0, sizeof(struct hidpp_touchpad_raw_xy
));
1396 raw_xy
->end_of_frame
= data
[8] & 0x01;
1397 raw_xy
->spurious_flag
= (data
[8] >> 1) & 0x01;
1398 raw_xy
->finger_count
= data
[15] & 0x0f;
1399 raw_xy
->button
= (data
[8] >> 2) & 0x01;
1401 if (raw_xy
->finger_count
) {
1402 hidpp_touchpad_touch_event(&data
[2], &raw_xy
->fingers
[0]);
1403 hidpp_touchpad_touch_event(&data
[9], &raw_xy
->fingers
[1]);
1407 /* -------------------------------------------------------------------------- */
1408 /* 0x8123: Force feedback support */
1409 /* -------------------------------------------------------------------------- */
1411 #define HIDPP_FF_GET_INFO 0x01
1412 #define HIDPP_FF_RESET_ALL 0x11
1413 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1414 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1415 #define HIDPP_FF_DESTROY_EFFECT 0x41
1416 #define HIDPP_FF_GET_APERTURE 0x51
1417 #define HIDPP_FF_SET_APERTURE 0x61
1418 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1419 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1421 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1422 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1423 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1424 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1426 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1427 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1428 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1429 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1430 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1431 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1432 #define HIDPP_FF_EFFECT_SPRING 0x06
1433 #define HIDPP_FF_EFFECT_DAMPER 0x07
1434 #define HIDPP_FF_EFFECT_FRICTION 0x08
1435 #define HIDPP_FF_EFFECT_INERTIA 0x09
1436 #define HIDPP_FF_EFFECT_RAMP 0x0A
1438 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1440 #define HIDPP_FF_EFFECTID_NONE -1
1441 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1443 #define HIDPP_FF_MAX_PARAMS 20
1444 #define HIDPP_FF_RESERVED_SLOTS 1
1446 struct hidpp_ff_private_data
{
1447 struct hidpp_device
*hidpp
;
1455 struct workqueue_struct
*wq
;
1456 atomic_t workqueue_size
;
1459 struct hidpp_ff_work_data
{
1460 struct work_struct work
;
1461 struct hidpp_ff_private_data
*data
;
1464 u8 params
[HIDPP_FF_MAX_PARAMS
];
1468 static const signed short hiddpp_ff_effects
[] = {
1483 static const signed short hiddpp_ff_effects_v2
[] = {
1490 static const u8 HIDPP_FF_CONDITION_CMDS
[] = {
1491 HIDPP_FF_EFFECT_SPRING
,
1492 HIDPP_FF_EFFECT_FRICTION
,
1493 HIDPP_FF_EFFECT_DAMPER
,
1494 HIDPP_FF_EFFECT_INERTIA
1497 static const char *HIDPP_FF_CONDITION_NAMES
[] = {
1505 static u8
hidpp_ff_find_effect(struct hidpp_ff_private_data
*data
, int effect_id
)
1509 for (i
= 0; i
< data
->num_effects
; i
++)
1510 if (data
->effect_ids
[i
] == effect_id
)
1516 static void hidpp_ff_work_handler(struct work_struct
*w
)
1518 struct hidpp_ff_work_data
*wd
= container_of(w
, struct hidpp_ff_work_data
, work
);
1519 struct hidpp_ff_private_data
*data
= wd
->data
;
1520 struct hidpp_report response
;
1524 /* add slot number if needed */
1525 switch (wd
->effect_id
) {
1526 case HIDPP_FF_EFFECTID_AUTOCENTER
:
1527 wd
->params
[0] = data
->slot_autocenter
;
1529 case HIDPP_FF_EFFECTID_NONE
:
1530 /* leave slot as zero */
1533 /* find current slot for effect */
1534 wd
->params
[0] = hidpp_ff_find_effect(data
, wd
->effect_id
);
1538 /* send command and wait for reply */
1539 ret
= hidpp_send_fap_command_sync(data
->hidpp
, data
->feature_index
,
1540 wd
->command
, wd
->params
, wd
->size
, &response
);
1543 hid_err(data
->hidpp
->hid_dev
, "Failed to send command to device!\n");
1547 /* parse return data */
1548 switch (wd
->command
) {
1549 case HIDPP_FF_DOWNLOAD_EFFECT
:
1550 slot
= response
.fap
.params
[0];
1551 if (slot
> 0 && slot
<= data
->num_effects
) {
1552 if (wd
->effect_id
>= 0)
1553 /* regular effect uploaded */
1554 data
->effect_ids
[slot
-1] = wd
->effect_id
;
1555 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
1556 /* autocenter spring uploaded */
1557 data
->slot_autocenter
= slot
;
1560 case HIDPP_FF_DESTROY_EFFECT
:
1561 if (wd
->effect_id
>= 0)
1562 /* regular effect destroyed */
1563 data
->effect_ids
[wd
->params
[0]-1] = -1;
1564 else if (wd
->effect_id
>= HIDPP_FF_EFFECTID_AUTOCENTER
)
1565 /* autocenter spring destoyed */
1566 data
->slot_autocenter
= 0;
1568 case HIDPP_FF_SET_GLOBAL_GAINS
:
1569 data
->gain
= (wd
->params
[0] << 8) + wd
->params
[1];
1571 case HIDPP_FF_SET_APERTURE
:
1572 data
->range
= (wd
->params
[0] << 8) + wd
->params
[1];
1575 /* no action needed */
1580 atomic_dec(&data
->workqueue_size
);
1584 static int hidpp_ff_queue_work(struct hidpp_ff_private_data
*data
, int effect_id
, u8 command
, u8
*params
, u8 size
)
1586 struct hidpp_ff_work_data
*wd
= kzalloc(sizeof(*wd
), GFP_KERNEL
);
1592 INIT_WORK(&wd
->work
, hidpp_ff_work_handler
);
1595 wd
->effect_id
= effect_id
;
1596 wd
->command
= command
;
1598 memcpy(wd
->params
, params
, size
);
1600 atomic_inc(&data
->workqueue_size
);
1601 queue_work(data
->wq
, &wd
->work
);
1603 /* warn about excessive queue size */
1604 s
= atomic_read(&data
->workqueue_size
);
1605 if (s
>= 20 && s
% 20 == 0)
1606 hid_warn(data
->hidpp
->hid_dev
, "Force feedback command queue contains %d commands, causing substantial delays!", s
);
1611 static int hidpp_ff_upload_effect(struct input_dev
*dev
, struct ff_effect
*effect
, struct ff_effect
*old
)
1613 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1618 /* set common parameters */
1619 params
[2] = effect
->replay
.length
>> 8;
1620 params
[3] = effect
->replay
.length
& 255;
1621 params
[4] = effect
->replay
.delay
>> 8;
1622 params
[5] = effect
->replay
.delay
& 255;
1624 switch (effect
->type
) {
1626 force
= (effect
->u
.constant
.level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1627 params
[1] = HIDPP_FF_EFFECT_CONSTANT
;
1628 params
[6] = force
>> 8;
1629 params
[7] = force
& 255;
1630 params
[8] = effect
->u
.constant
.envelope
.attack_level
>> 7;
1631 params
[9] = effect
->u
.constant
.envelope
.attack_length
>> 8;
1632 params
[10] = effect
->u
.constant
.envelope
.attack_length
& 255;
1633 params
[11] = effect
->u
.constant
.envelope
.fade_level
>> 7;
1634 params
[12] = effect
->u
.constant
.envelope
.fade_length
>> 8;
1635 params
[13] = effect
->u
.constant
.envelope
.fade_length
& 255;
1637 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1638 effect
->u
.constant
.level
,
1639 effect
->direction
, force
);
1640 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1641 effect
->u
.constant
.envelope
.attack_level
,
1642 effect
->u
.constant
.envelope
.attack_length
,
1643 effect
->u
.constant
.envelope
.fade_level
,
1644 effect
->u
.constant
.envelope
.fade_length
);
1648 switch (effect
->u
.periodic
.waveform
) {
1650 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SINE
;
1653 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE
;
1656 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP
;
1659 params
[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN
;
1662 params
[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE
;
1665 hid_err(data
->hidpp
->hid_dev
, "Unexpected periodic waveform type %i!\n", effect
->u
.periodic
.waveform
);
1668 force
= (effect
->u
.periodic
.magnitude
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1669 params
[6] = effect
->u
.periodic
.magnitude
>> 8;
1670 params
[7] = effect
->u
.periodic
.magnitude
& 255;
1671 params
[8] = effect
->u
.periodic
.offset
>> 8;
1672 params
[9] = effect
->u
.periodic
.offset
& 255;
1673 params
[10] = effect
->u
.periodic
.period
>> 8;
1674 params
[11] = effect
->u
.periodic
.period
& 255;
1675 params
[12] = effect
->u
.periodic
.phase
>> 8;
1676 params
[13] = effect
->u
.periodic
.phase
& 255;
1677 params
[14] = effect
->u
.periodic
.envelope
.attack_level
>> 7;
1678 params
[15] = effect
->u
.periodic
.envelope
.attack_length
>> 8;
1679 params
[16] = effect
->u
.periodic
.envelope
.attack_length
& 255;
1680 params
[17] = effect
->u
.periodic
.envelope
.fade_level
>> 7;
1681 params
[18] = effect
->u
.periodic
.envelope
.fade_length
>> 8;
1682 params
[19] = effect
->u
.periodic
.envelope
.fade_length
& 255;
1684 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1685 effect
->u
.periodic
.magnitude
, effect
->direction
,
1686 effect
->u
.periodic
.offset
,
1687 effect
->u
.periodic
.period
,
1688 effect
->u
.periodic
.phase
);
1689 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1690 effect
->u
.periodic
.envelope
.attack_level
,
1691 effect
->u
.periodic
.envelope
.attack_length
,
1692 effect
->u
.periodic
.envelope
.fade_level
,
1693 effect
->u
.periodic
.envelope
.fade_length
);
1697 params
[1] = HIDPP_FF_EFFECT_RAMP
;
1698 force
= (effect
->u
.ramp
.start_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1699 params
[6] = force
>> 8;
1700 params
[7] = force
& 255;
1701 force
= (effect
->u
.ramp
.end_level
* fixp_sin16((effect
->direction
* 360) >> 16)) >> 15;
1702 params
[8] = force
>> 8;
1703 params
[9] = force
& 255;
1704 params
[10] = effect
->u
.ramp
.envelope
.attack_level
>> 7;
1705 params
[11] = effect
->u
.ramp
.envelope
.attack_length
>> 8;
1706 params
[12] = effect
->u
.ramp
.envelope
.attack_length
& 255;
1707 params
[13] = effect
->u
.ramp
.envelope
.fade_level
>> 7;
1708 params
[14] = effect
->u
.ramp
.envelope
.fade_length
>> 8;
1709 params
[15] = effect
->u
.ramp
.envelope
.fade_length
& 255;
1711 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1712 effect
->u
.ramp
.start_level
,
1713 effect
->u
.ramp
.end_level
,
1714 effect
->direction
, force
);
1715 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1716 effect
->u
.ramp
.envelope
.attack_level
,
1717 effect
->u
.ramp
.envelope
.attack_length
,
1718 effect
->u
.ramp
.envelope
.fade_level
,
1719 effect
->u
.ramp
.envelope
.fade_length
);
1725 params
[1] = HIDPP_FF_CONDITION_CMDS
[effect
->type
- FF_SPRING
];
1726 params
[6] = effect
->u
.condition
[0].left_saturation
>> 9;
1727 params
[7] = (effect
->u
.condition
[0].left_saturation
>> 1) & 255;
1728 params
[8] = effect
->u
.condition
[0].left_coeff
>> 8;
1729 params
[9] = effect
->u
.condition
[0].left_coeff
& 255;
1730 params
[10] = effect
->u
.condition
[0].deadband
>> 9;
1731 params
[11] = (effect
->u
.condition
[0].deadband
>> 1) & 255;
1732 params
[12] = effect
->u
.condition
[0].center
>> 8;
1733 params
[13] = effect
->u
.condition
[0].center
& 255;
1734 params
[14] = effect
->u
.condition
[0].right_coeff
>> 8;
1735 params
[15] = effect
->u
.condition
[0].right_coeff
& 255;
1736 params
[16] = effect
->u
.condition
[0].right_saturation
>> 9;
1737 params
[17] = (effect
->u
.condition
[0].right_saturation
>> 1) & 255;
1739 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1740 HIDPP_FF_CONDITION_NAMES
[effect
->type
- FF_SPRING
],
1741 effect
->u
.condition
[0].left_coeff
,
1742 effect
->u
.condition
[0].left_saturation
,
1743 effect
->u
.condition
[0].right_coeff
,
1744 effect
->u
.condition
[0].right_saturation
);
1745 dbg_hid(" deadband=%d, center=%d\n",
1746 effect
->u
.condition
[0].deadband
,
1747 effect
->u
.condition
[0].center
);
1750 hid_err(data
->hidpp
->hid_dev
, "Unexpected force type %i!\n", effect
->type
);
1754 return hidpp_ff_queue_work(data
, effect
->id
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, size
);
1757 static int hidpp_ff_playback(struct input_dev
*dev
, int effect_id
, int value
)
1759 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1762 params
[1] = value
? HIDPP_FF_EFFECT_STATE_PLAY
: HIDPP_FF_EFFECT_STATE_STOP
;
1764 dbg_hid("St%sing playback of effect %d.\n", value
?"art":"opp", effect_id
);
1766 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_SET_EFFECT_STATE
, params
, ARRAY_SIZE(params
));
1769 static int hidpp_ff_erase_effect(struct input_dev
*dev
, int effect_id
)
1771 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1774 dbg_hid("Erasing effect %d.\n", effect_id
);
1776 return hidpp_ff_queue_work(data
, effect_id
, HIDPP_FF_DESTROY_EFFECT
, &slot
, 1);
1779 static void hidpp_ff_set_autocenter(struct input_dev
*dev
, u16 magnitude
)
1781 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1784 dbg_hid("Setting autocenter to %d.\n", magnitude
);
1786 /* start a standard spring effect */
1787 params
[1] = HIDPP_FF_EFFECT_SPRING
| HIDPP_FF_EFFECT_AUTOSTART
;
1788 /* zero delay and duration */
1789 params
[2] = params
[3] = params
[4] = params
[5] = 0;
1790 /* set coeff to 25% of saturation */
1791 params
[8] = params
[14] = magnitude
>> 11;
1792 params
[9] = params
[15] = (magnitude
>> 3) & 255;
1793 params
[6] = params
[16] = magnitude
>> 9;
1794 params
[7] = params
[17] = (magnitude
>> 1) & 255;
1795 /* zero deadband and center */
1796 params
[10] = params
[11] = params
[12] = params
[13] = 0;
1798 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_AUTOCENTER
, HIDPP_FF_DOWNLOAD_EFFECT
, params
, ARRAY_SIZE(params
));
1801 static void hidpp_ff_set_gain(struct input_dev
*dev
, u16 gain
)
1803 struct hidpp_ff_private_data
*data
= dev
->ff
->private;
1806 dbg_hid("Setting gain to %d.\n", gain
);
1808 params
[0] = gain
>> 8;
1809 params
[1] = gain
& 255;
1810 params
[2] = 0; /* no boost */
1813 hidpp_ff_queue_work(data
, HIDPP_FF_EFFECTID_NONE
, HIDPP_FF_SET_GLOBAL_GAINS
, params
, ARRAY_SIZE(params
));
1816 static ssize_t
hidpp_ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1818 struct hid_device
*hid
= to_hid_device(dev
);
1819 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1820 struct input_dev
*idev
= hidinput
->input
;
1821 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
1823 return scnprintf(buf
, PAGE_SIZE
, "%u\n", data
->range
);
1826 static ssize_t
hidpp_ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
1828 struct hid_device
*hid
= to_hid_device(dev
);
1829 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1830 struct input_dev
*idev
= hidinput
->input
;
1831 struct hidpp_ff_private_data
*data
= idev
->ff
->private;
1833 int range
= simple_strtoul(buf
, NULL
, 10);
1835 range
= clamp(range
, 180, 900);
1837 params
[0] = range
>> 8;
1838 params
[1] = range
& 0x00FF;
1840 hidpp_ff_queue_work(data
, -1, HIDPP_FF_SET_APERTURE
, params
, ARRAY_SIZE(params
));
1845 static DEVICE_ATTR(range
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
, hidpp_ff_range_show
, hidpp_ff_range_store
);
1847 static void hidpp_ff_destroy(struct ff_device
*ff
)
1849 struct hidpp_ff_private_data
*data
= ff
->private;
1851 kfree(data
->effect_ids
);
1854 static int hidpp_ff_init(struct hidpp_device
*hidpp
, u8 feature_index
)
1856 struct hid_device
*hid
= hidpp
->hid_dev
;
1857 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1858 struct input_dev
*dev
= hidinput
->input
;
1859 const struct usb_device_descriptor
*udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
1860 const u16 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
1861 struct ff_device
*ff
;
1862 struct hidpp_report response
;
1863 struct hidpp_ff_private_data
*data
;
1864 int error
, j
, num_slots
;
1868 hid_err(hid
, "Struct input_dev not set!\n");
1872 /* Get firmware release */
1873 version
= bcdDevice
& 255;
1875 /* Set supported force feedback capabilities */
1876 for (j
= 0; hiddpp_ff_effects
[j
] >= 0; j
++)
1877 set_bit(hiddpp_ff_effects
[j
], dev
->ffbit
);
1879 for (j
= 0; hiddpp_ff_effects_v2
[j
] >= 0; j
++)
1880 set_bit(hiddpp_ff_effects_v2
[j
], dev
->ffbit
);
1882 /* Read number of slots available in device */
1883 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1884 HIDPP_FF_GET_INFO
, NULL
, 0, &response
);
1888 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
1893 num_slots
= response
.fap
.params
[0] - HIDPP_FF_RESERVED_SLOTS
;
1895 error
= input_ff_create(dev
, num_slots
);
1898 hid_err(dev
, "Failed to create FF device!\n");
1902 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1905 data
->effect_ids
= kcalloc(num_slots
, sizeof(int), GFP_KERNEL
);
1906 if (!data
->effect_ids
) {
1910 data
->hidpp
= hidpp
;
1911 data
->feature_index
= feature_index
;
1912 data
->version
= version
;
1913 data
->slot_autocenter
= 0;
1914 data
->num_effects
= num_slots
;
1915 for (j
= 0; j
< num_slots
; j
++)
1916 data
->effect_ids
[j
] = -1;
1921 ff
->upload
= hidpp_ff_upload_effect
;
1922 ff
->erase
= hidpp_ff_erase_effect
;
1923 ff
->playback
= hidpp_ff_playback
;
1924 ff
->set_gain
= hidpp_ff_set_gain
;
1925 ff
->set_autocenter
= hidpp_ff_set_autocenter
;
1926 ff
->destroy
= hidpp_ff_destroy
;
1929 /* reset all forces */
1930 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1931 HIDPP_FF_RESET_ALL
, NULL
, 0, &response
);
1933 /* Read current Range */
1934 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1935 HIDPP_FF_GET_APERTURE
, NULL
, 0, &response
);
1937 hid_warn(hidpp
->hid_dev
, "Failed to read range from device!\n");
1938 data
->range
= error
? 900 : get_unaligned_be16(&response
.fap
.params
[0]);
1940 /* Create sysfs interface */
1941 error
= device_create_file(&(hidpp
->hid_dev
->dev
), &dev_attr_range
);
1943 hid_warn(hidpp
->hid_dev
, "Unable to create sysfs interface for \"range\", errno %d!\n", error
);
1945 /* Read the current gain values */
1946 error
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
1947 HIDPP_FF_GET_GLOBAL_GAINS
, NULL
, 0, &response
);
1949 hid_warn(hidpp
->hid_dev
, "Failed to read gain values from device!\n");
1950 data
->gain
= error
? 0xffff : get_unaligned_be16(&response
.fap
.params
[0]);
1951 /* ignore boost value at response.fap.params[2] */
1953 /* init the hardware command queue */
1954 data
->wq
= create_singlethread_workqueue("hidpp-ff-sendqueue");
1955 atomic_set(&data
->workqueue_size
, 0);
1957 /* initialize with zero autocenter to get wheel in usable state */
1958 hidpp_ff_set_autocenter(dev
, 0);
1960 hid_info(hid
, "Force feedback support loaded (firmware release %d).\n",
1966 static int hidpp_ff_deinit(struct hid_device
*hid
)
1968 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
1969 struct input_dev
*dev
= hidinput
->input
;
1970 struct hidpp_ff_private_data
*data
;
1973 hid_err(hid
, "Struct input_dev not found!\n");
1977 hid_info(hid
, "Unloading HID++ force feedback.\n");
1978 data
= dev
->ff
->private;
1980 hid_err(hid
, "Private data not found!\n");
1984 destroy_workqueue(data
->wq
);
1985 device_remove_file(&hid
->dev
, &dev_attr_range
);
1991 /* ************************************************************************** */
1993 /* Device Support */
1995 /* ************************************************************************** */
1997 /* -------------------------------------------------------------------------- */
1998 /* Touchpad HID++ devices */
1999 /* -------------------------------------------------------------------------- */
2001 #define WTP_MANUAL_RESOLUTION 39
2004 struct input_dev
*input
;
2007 u8 mt_feature_index
;
2008 u8 button_feature_index
;
2011 unsigned int resolution
;
2014 static int wtp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2015 struct hid_field
*field
, struct hid_usage
*usage
,
2016 unsigned long **bit
, int *max
)
2021 static void wtp_populate_input(struct hidpp_device
*hidpp
,
2022 struct input_dev
*input_dev
, bool origin_is_hid_core
)
2024 struct wtp_data
*wd
= hidpp
->private_data
;
2026 __set_bit(EV_ABS
, input_dev
->evbit
);
2027 __set_bit(EV_KEY
, input_dev
->evbit
);
2028 __clear_bit(EV_REL
, input_dev
->evbit
);
2029 __clear_bit(EV_LED
, input_dev
->evbit
);
2031 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, wd
->x_size
, 0, 0);
2032 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
, wd
->resolution
);
2033 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, wd
->y_size
, 0, 0);
2034 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
, wd
->resolution
);
2036 /* Max pressure is not given by the devices, pick one */
2037 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 50, 0, 0);
2039 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
2041 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
)
2042 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
2044 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
2046 input_mt_init_slots(input_dev
, wd
->maxcontacts
, INPUT_MT_POINTER
|
2047 INPUT_MT_DROP_UNUSED
);
2049 wd
->input
= input_dev
;
2052 static void wtp_touch_event(struct wtp_data
*wd
,
2053 struct hidpp_touchpad_raw_xy_finger
*touch_report
)
2057 if (!touch_report
->finger_id
|| touch_report
->contact_type
)
2058 /* no actual data */
2061 slot
= input_mt_get_slot_by_key(wd
->input
, touch_report
->finger_id
);
2063 input_mt_slot(wd
->input
, slot
);
2064 input_mt_report_slot_state(wd
->input
, MT_TOOL_FINGER
,
2065 touch_report
->contact_status
);
2066 if (touch_report
->contact_status
) {
2067 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_X
,
2069 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_Y
,
2070 wd
->flip_y
? wd
->y_size
- touch_report
->y
:
2072 input_event(wd
->input
, EV_ABS
, ABS_MT_PRESSURE
,
2073 touch_report
->area
);
2077 static void wtp_send_raw_xy_event(struct hidpp_device
*hidpp
,
2078 struct hidpp_touchpad_raw_xy
*raw
)
2080 struct wtp_data
*wd
= hidpp
->private_data
;
2083 for (i
= 0; i
< 2; i
++)
2084 wtp_touch_event(wd
, &(raw
->fingers
[i
]));
2086 if (raw
->end_of_frame
&&
2087 !(hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
))
2088 input_event(wd
->input
, EV_KEY
, BTN_LEFT
, raw
->button
);
2090 if (raw
->end_of_frame
|| raw
->finger_count
<= 2) {
2091 input_mt_sync_frame(wd
->input
);
2092 input_sync(wd
->input
);
2096 static int wtp_mouse_raw_xy_event(struct hidpp_device
*hidpp
, u8
*data
)
2098 struct wtp_data
*wd
= hidpp
->private_data
;
2099 u8 c1_area
= ((data
[7] & 0xf) * (data
[7] & 0xf) +
2100 (data
[7] >> 4) * (data
[7] >> 4)) / 2;
2101 u8 c2_area
= ((data
[13] & 0xf) * (data
[13] & 0xf) +
2102 (data
[13] >> 4) * (data
[13] >> 4)) / 2;
2103 struct hidpp_touchpad_raw_xy raw
= {
2104 .timestamp
= data
[1],
2108 .contact_status
= !!data
[7],
2109 .x
= get_unaligned_le16(&data
[3]),
2110 .y
= get_unaligned_le16(&data
[5]),
2113 .finger_id
= data
[2],
2116 .contact_status
= !!data
[13],
2117 .x
= get_unaligned_le16(&data
[9]),
2118 .y
= get_unaligned_le16(&data
[11]),
2121 .finger_id
= data
[8],
2124 .finger_count
= wd
->maxcontacts
,
2126 .end_of_frame
= (data
[0] >> 7) == 0,
2127 .button
= data
[0] & 0x01,
2130 wtp_send_raw_xy_event(hidpp
, &raw
);
2135 static int wtp_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
2137 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2138 struct wtp_data
*wd
= hidpp
->private_data
;
2139 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
2140 struct hidpp_touchpad_raw_xy raw
;
2142 if (!wd
|| !wd
->input
)
2148 hid_err(hdev
, "Received HID report of bad size (%d)",
2152 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
) {
2153 input_event(wd
->input
, EV_KEY
, BTN_LEFT
,
2154 !!(data
[1] & 0x01));
2155 input_event(wd
->input
, EV_KEY
, BTN_RIGHT
,
2156 !!(data
[1] & 0x02));
2157 input_sync(wd
->input
);
2162 return wtp_mouse_raw_xy_event(hidpp
, &data
[7]);
2164 case REPORT_ID_HIDPP_LONG
:
2165 /* size is already checked in hidpp_raw_event. */
2166 if ((report
->fap
.feature_index
!= wd
->mt_feature_index
) ||
2167 (report
->fap
.funcindex_clientid
!= EVENT_TOUCHPAD_RAW_XY
))
2169 hidpp_touchpad_raw_xy_event(hidpp
, data
+ 4, &raw
);
2171 wtp_send_raw_xy_event(hidpp
, &raw
);
2178 static int wtp_get_config(struct hidpp_device
*hidpp
)
2180 struct wtp_data
*wd
= hidpp
->private_data
;
2181 struct hidpp_touchpad_raw_info raw_info
= {0};
2185 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_TOUCHPAD_RAW_XY
,
2186 &wd
->mt_feature_index
, &feature_type
);
2188 /* means that the device is not powered up */
2191 ret
= hidpp_touchpad_get_raw_info(hidpp
, wd
->mt_feature_index
,
2196 wd
->x_size
= raw_info
.x_size
;
2197 wd
->y_size
= raw_info
.y_size
;
2198 wd
->maxcontacts
= raw_info
.maxcontacts
;
2199 wd
->flip_y
= raw_info
.origin
== TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT
;
2200 wd
->resolution
= raw_info
.res
;
2201 if (!wd
->resolution
)
2202 wd
->resolution
= WTP_MANUAL_RESOLUTION
;
2207 static int wtp_allocate(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2209 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2210 struct wtp_data
*wd
;
2212 wd
= devm_kzalloc(&hdev
->dev
, sizeof(struct wtp_data
),
2217 hidpp
->private_data
= wd
;
2222 static int wtp_connect(struct hid_device
*hdev
, bool connected
)
2224 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2225 struct wtp_data
*wd
= hidpp
->private_data
;
2229 ret
= wtp_get_config(hidpp
);
2231 hid_err(hdev
, "Can not get wtp config: %d\n", ret
);
2236 return hidpp_touchpad_set_raw_report_state(hidpp
, wd
->mt_feature_index
,
2240 /* ------------------------------------------------------------------------- */
2241 /* Logitech M560 devices */
2242 /* ------------------------------------------------------------------------- */
2245 * Logitech M560 protocol overview
2247 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2248 * the sides buttons are pressed, it sends some keyboard keys events
2249 * instead of buttons ones.
2250 * To complicate things further, the middle button keys sequence
2251 * is different from the odd press and the even press.
2253 * forward button -> Super_R
2254 * backward button -> Super_L+'d' (press only)
2255 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2256 * 2nd time: left-click (press only)
2257 * NB: press-only means that when the button is pressed, the
2258 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2259 * together sequentially; instead when the button is released, no event is
2263 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2264 * the mouse reacts differently:
2265 * - it never sends a keyboard key event
2266 * - for the three mouse button it sends:
2267 * middle button press 11<xx>0a 3500af00...
2268 * side 1 button (forward) press 11<xx>0a 3500b000...
2269 * side 2 button (backward) press 11<xx>0a 3500ae00...
2270 * middle/side1/side2 button release 11<xx>0a 35000000...
2273 static const u8 m560_config_parameter
[] = {0x00, 0xaf, 0x03};
2275 struct m560_private_data
{
2276 struct input_dev
*input
;
2279 /* how buttons are mapped in the report */
2280 #define M560_MOUSE_BTN_LEFT 0x01
2281 #define M560_MOUSE_BTN_RIGHT 0x02
2282 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2283 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2285 #define M560_SUB_ID 0x0a
2286 #define M560_BUTTON_MODE_REGISTER 0x35
2288 static int m560_send_config_command(struct hid_device
*hdev
, bool connected
)
2290 struct hidpp_report response
;
2291 struct hidpp_device
*hidpp_dev
;
2293 hidpp_dev
= hid_get_drvdata(hdev
);
2295 return hidpp_send_rap_command_sync(
2297 REPORT_ID_HIDPP_SHORT
,
2299 M560_BUTTON_MODE_REGISTER
,
2300 (u8
*)m560_config_parameter
,
2301 sizeof(m560_config_parameter
),
2306 static int m560_allocate(struct hid_device
*hdev
)
2308 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2309 struct m560_private_data
*d
;
2311 d
= devm_kzalloc(&hdev
->dev
, sizeof(struct m560_private_data
),
2316 hidpp
->private_data
= d
;
2321 static int m560_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
2323 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2324 struct m560_private_data
*mydata
= hidpp
->private_data
;
2327 if (!mydata
|| !mydata
->input
) {
2328 hid_err(hdev
, "error in parameter\n");
2333 hid_err(hdev
, "error in report\n");
2337 if (data
[0] == REPORT_ID_HIDPP_LONG
&&
2338 data
[2] == M560_SUB_ID
&& data
[6] == 0x00) {
2340 * m560 mouse report for middle, forward and backward button
2343 * data[1] = device-id
2345 * data[5] = 0xaf -> middle
2348 * 0x00 -> release all
2354 input_report_key(mydata
->input
, BTN_MIDDLE
, 1);
2357 input_report_key(mydata
->input
, BTN_FORWARD
, 1);
2360 input_report_key(mydata
->input
, BTN_BACK
, 1);
2363 input_report_key(mydata
->input
, BTN_BACK
, 0);
2364 input_report_key(mydata
->input
, BTN_FORWARD
, 0);
2365 input_report_key(mydata
->input
, BTN_MIDDLE
, 0);
2368 hid_err(hdev
, "error in report\n");
2371 input_sync(mydata
->input
);
2373 } else if (data
[0] == 0x02) {
2375 * Logitech M560 mouse report
2377 * data[0] = type (0x02)
2378 * data[1..2] = buttons
2385 input_report_key(mydata
->input
, BTN_LEFT
,
2386 !!(data
[1] & M560_MOUSE_BTN_LEFT
));
2387 input_report_key(mydata
->input
, BTN_RIGHT
,
2388 !!(data
[1] & M560_MOUSE_BTN_RIGHT
));
2390 if (data
[1] & M560_MOUSE_BTN_WHEEL_LEFT
)
2391 input_report_rel(mydata
->input
, REL_HWHEEL
, -1);
2392 else if (data
[1] & M560_MOUSE_BTN_WHEEL_RIGHT
)
2393 input_report_rel(mydata
->input
, REL_HWHEEL
, 1);
2395 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 0, 12), 12);
2396 input_report_rel(mydata
->input
, REL_X
, v
);
2398 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 12, 12), 12);
2399 input_report_rel(mydata
->input
, REL_Y
, v
);
2401 v
= hid_snto32(data
[6], 8);
2402 input_report_rel(mydata
->input
, REL_WHEEL
, v
);
2404 input_sync(mydata
->input
);
2410 static void m560_populate_input(struct hidpp_device
*hidpp
,
2411 struct input_dev
*input_dev
, bool origin_is_hid_core
)
2413 struct m560_private_data
*mydata
= hidpp
->private_data
;
2415 mydata
->input
= input_dev
;
2417 __set_bit(EV_KEY
, mydata
->input
->evbit
);
2418 __set_bit(BTN_MIDDLE
, mydata
->input
->keybit
);
2419 __set_bit(BTN_RIGHT
, mydata
->input
->keybit
);
2420 __set_bit(BTN_LEFT
, mydata
->input
->keybit
);
2421 __set_bit(BTN_BACK
, mydata
->input
->keybit
);
2422 __set_bit(BTN_FORWARD
, mydata
->input
->keybit
);
2424 __set_bit(EV_REL
, mydata
->input
->evbit
);
2425 __set_bit(REL_X
, mydata
->input
->relbit
);
2426 __set_bit(REL_Y
, mydata
->input
->relbit
);
2427 __set_bit(REL_WHEEL
, mydata
->input
->relbit
);
2428 __set_bit(REL_HWHEEL
, mydata
->input
->relbit
);
2431 static int m560_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2432 struct hid_field
*field
, struct hid_usage
*usage
,
2433 unsigned long **bit
, int *max
)
2438 /* ------------------------------------------------------------------------- */
2439 /* Logitech K400 devices */
2440 /* ------------------------------------------------------------------------- */
2443 * The Logitech K400 keyboard has an embedded touchpad which is seen
2444 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2445 * tap-to-click but the setting is not remembered accross reset, annoying some
2448 * We can toggle this feature from the host by using the feature 0x6010:
2452 struct k400_private_data
{
2456 static int k400_disable_tap_to_click(struct hidpp_device
*hidpp
)
2458 struct k400_private_data
*k400
= hidpp
->private_data
;
2459 struct hidpp_touchpad_fw_items items
= {};
2463 if (!k400
->feature_index
) {
2464 ret
= hidpp_root_get_feature(hidpp
,
2465 HIDPP_PAGE_TOUCHPAD_FW_ITEMS
,
2466 &k400
->feature_index
, &feature_type
);
2468 /* means that the device is not powered up */
2472 ret
= hidpp_touchpad_fw_items_set(hidpp
, k400
->feature_index
, &items
);
2479 static int k400_allocate(struct hid_device
*hdev
)
2481 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2482 struct k400_private_data
*k400
;
2484 k400
= devm_kzalloc(&hdev
->dev
, sizeof(struct k400_private_data
),
2489 hidpp
->private_data
= k400
;
2494 static int k400_connect(struct hid_device
*hdev
, bool connected
)
2496 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2498 if (!disable_tap_to_click
)
2501 return k400_disable_tap_to_click(hidpp
);
2504 /* ------------------------------------------------------------------------- */
2505 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2506 /* ------------------------------------------------------------------------- */
2508 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2510 static int g920_get_config(struct hidpp_device
*hidpp
)
2516 /* Find feature and store for later use */
2517 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_G920_FORCE_FEEDBACK
,
2518 &feature_index
, &feature_type
);
2522 ret
= hidpp_ff_init(hidpp
, feature_index
);
2524 hid_warn(hidpp
->hid_dev
, "Unable to initialize force feedback support, errno %d\n",
2530 /* -------------------------------------------------------------------------- */
2531 /* Generic HID++ devices */
2532 /* -------------------------------------------------------------------------- */
2534 static int hidpp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
2535 struct hid_field
*field
, struct hid_usage
*usage
,
2536 unsigned long **bit
, int *max
)
2538 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2540 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2541 return wtp_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
2542 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
&&
2543 field
->application
!= HID_GD_MOUSE
)
2544 return m560_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
2549 static int hidpp_input_mapped(struct hid_device
*hdev
, struct hid_input
*hi
,
2550 struct hid_field
*field
, struct hid_usage
*usage
,
2551 unsigned long **bit
, int *max
)
2553 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2555 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2556 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
2557 if (usage
->type
== EV_ABS
&& (usage
->code
== ABS_X
||
2558 usage
->code
== ABS_Y
|| usage
->code
== ABS_Z
||
2559 usage
->code
== ABS_RZ
)) {
2560 field
->application
= HID_GD_MULTIAXIS
;
2568 static void hidpp_populate_input(struct hidpp_device
*hidpp
,
2569 struct input_dev
*input
, bool origin_is_hid_core
)
2571 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2572 wtp_populate_input(hidpp
, input
, origin_is_hid_core
);
2573 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
2574 m560_populate_input(hidpp
, input
, origin_is_hid_core
);
2577 static int hidpp_input_configured(struct hid_device
*hdev
,
2578 struct hid_input
*hidinput
)
2580 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2581 struct input_dev
*input
= hidinput
->input
;
2583 hidpp_populate_input(hidpp
, input
, true);
2588 static int hidpp_raw_hidpp_event(struct hidpp_device
*hidpp
, u8
*data
,
2591 struct hidpp_report
*question
= hidpp
->send_receive_buf
;
2592 struct hidpp_report
*answer
= hidpp
->send_receive_buf
;
2593 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
2597 * If the mutex is locked then we have a pending answer from a
2598 * previously sent command.
2600 if (unlikely(mutex_is_locked(&hidpp
->send_mutex
))) {
2602 * Check for a correct hidpp20 answer or the corresponding
2605 if (hidpp_match_answer(question
, report
) ||
2606 hidpp_match_error(question
, report
)) {
2608 hidpp
->answer_available
= true;
2609 wake_up(&hidpp
->wait
);
2611 * This was an answer to a command that this driver sent
2612 * We return 1 to hid-core to avoid forwarding the
2613 * command upstream as it has been treated by the driver
2620 if (unlikely(hidpp_report_is_connect_event(report
))) {
2621 atomic_set(&hidpp
->connected
,
2622 !(report
->rap
.params
[0] & (1 << 6)));
2623 if (schedule_work(&hidpp
->work
) == 0)
2624 dbg_hid("%s: connect event already queued\n", __func__
);
2628 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP20_BATTERY
) {
2629 ret
= hidpp20_battery_event(hidpp
, data
, size
);
2632 ret
= hidpp_solar_battery_event(hidpp
, data
, size
);
2637 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP10_BATTERY
) {
2638 ret
= hidpp10_battery_event(hidpp
, data
, size
);
2646 static int hidpp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
2649 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2652 /* Generic HID++ processing. */
2654 case REPORT_ID_HIDPP_VERY_LONG
:
2655 if (size
!= HIDPP_REPORT_VERY_LONG_LENGTH
) {
2656 hid_err(hdev
, "received hid++ report of bad size (%d)",
2660 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2662 case REPORT_ID_HIDPP_LONG
:
2663 if (size
!= HIDPP_REPORT_LONG_LENGTH
) {
2664 hid_err(hdev
, "received hid++ report of bad size (%d)",
2668 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2670 case REPORT_ID_HIDPP_SHORT
:
2671 if (size
!= HIDPP_REPORT_SHORT_LENGTH
) {
2672 hid_err(hdev
, "received hid++ report of bad size (%d)",
2676 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
2680 /* If no report is available for further processing, skip calling
2681 * raw_event of subclasses. */
2685 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
2686 return wtp_raw_event(hdev
, data
, size
);
2687 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
2688 return m560_raw_event(hdev
, data
, size
);
2693 static int hidpp_initialize_battery(struct hidpp_device
*hidpp
)
2695 static atomic_t battery_no
= ATOMIC_INIT(0);
2696 struct power_supply_config cfg
= { .drv_data
= hidpp
};
2697 struct power_supply_desc
*desc
= &hidpp
->battery
.desc
;
2698 enum power_supply_property
*battery_props
;
2699 struct hidpp_battery
*battery
;
2700 unsigned int num_battery_props
;
2704 if (hidpp
->battery
.ps
)
2707 hidpp
->battery
.feature_index
= 0xff;
2708 hidpp
->battery
.solar_feature_index
= 0xff;
2710 if (hidpp
->protocol_major
>= 2) {
2711 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K750
)
2712 ret
= hidpp_solar_request_battery_event(hidpp
);
2714 ret
= hidpp20_query_battery_info(hidpp
);
2718 hidpp
->capabilities
|= HIDPP_CAPABILITY_HIDPP20_BATTERY
;
2720 ret
= hidpp10_query_battery_status(hidpp
);
2722 ret
= hidpp10_query_battery_mileage(hidpp
);
2725 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_MILEAGE
;
2727 hidpp
->capabilities
|= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
;
2729 hidpp
->capabilities
|= HIDPP_CAPABILITY_HIDPP10_BATTERY
;
2732 battery_props
= devm_kmemdup(&hidpp
->hid_dev
->dev
,
2733 hidpp_battery_props
,
2734 sizeof(hidpp_battery_props
),
2739 num_battery_props
= ARRAY_SIZE(hidpp_battery_props
) - 2;
2741 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_MILEAGE
)
2742 battery_props
[num_battery_props
++] =
2743 POWER_SUPPLY_PROP_CAPACITY
;
2745 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS
)
2746 battery_props
[num_battery_props
++] =
2747 POWER_SUPPLY_PROP_CAPACITY_LEVEL
;
2749 battery
= &hidpp
->battery
;
2751 n
= atomic_inc_return(&battery_no
) - 1;
2752 desc
->properties
= battery_props
;
2753 desc
->num_properties
= num_battery_props
;
2754 desc
->get_property
= hidpp_battery_get_property
;
2755 sprintf(battery
->name
, "hidpp_battery_%ld", n
);
2756 desc
->name
= battery
->name
;
2757 desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
2758 desc
->use_for_apm
= 0;
2760 battery
->ps
= devm_power_supply_register(&hidpp
->hid_dev
->dev
,
2763 if (IS_ERR(battery
->ps
))
2764 return PTR_ERR(battery
->ps
);
2766 power_supply_powers(battery
->ps
, &hidpp
->hid_dev
->dev
);
2771 static void hidpp_overwrite_name(struct hid_device
*hdev
)
2773 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2776 if (hidpp
->protocol_major
< 2)
2779 name
= hidpp_get_device_name(hidpp
);
2782 hid_err(hdev
, "unable to retrieve the name of the device");
2784 dbg_hid("HID++: Got name: %s\n", name
);
2785 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
2791 static int hidpp_input_open(struct input_dev
*dev
)
2793 struct hid_device
*hid
= input_get_drvdata(dev
);
2795 return hid_hw_open(hid
);
2798 static void hidpp_input_close(struct input_dev
*dev
)
2800 struct hid_device
*hid
= input_get_drvdata(dev
);
2805 static struct input_dev
*hidpp_allocate_input(struct hid_device
*hdev
)
2807 struct input_dev
*input_dev
= devm_input_allocate_device(&hdev
->dev
);
2808 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
2813 input_set_drvdata(input_dev
, hdev
);
2814 input_dev
->open
= hidpp_input_open
;
2815 input_dev
->close
= hidpp_input_close
;
2817 input_dev
->name
= hidpp
->name
;
2818 input_dev
->phys
= hdev
->phys
;
2819 input_dev
->uniq
= hdev
->uniq
;
2820 input_dev
->id
.bustype
= hdev
->bus
;
2821 input_dev
->id
.vendor
= hdev
->vendor
;
2822 input_dev
->id
.product
= hdev
->product
;
2823 input_dev
->id
.version
= hdev
->version
;
2824 input_dev
->dev
.parent
= &hdev
->dev
;
2829 static void hidpp_connect_event(struct hidpp_device
*hidpp
)
2831 struct hid_device
*hdev
= hidpp
->hid_dev
;
2833 bool connected
= atomic_read(&hidpp
->connected
);
2834 struct input_dev
*input
;
2835 char *name
, *devm_name
;
2838 if (hidpp
->battery
.ps
) {
2839 hidpp
->battery
.online
= false;
2840 hidpp
->battery
.status
= POWER_SUPPLY_STATUS_UNKNOWN
;
2841 hidpp
->battery
.level
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
2842 power_supply_changed(hidpp
->battery
.ps
);
2847 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
2848 ret
= wtp_connect(hdev
, connected
);
2851 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
2852 ret
= m560_send_config_command(hdev
, connected
);
2855 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
2856 ret
= k400_connect(hdev
, connected
);
2861 /* the device is already connected, we can ask for its name and
2863 if (!hidpp
->protocol_major
) {
2864 ret
= !hidpp_is_connected(hidpp
);
2866 hid_err(hdev
, "Can not get the protocol version.\n");
2869 hid_info(hdev
, "HID++ %u.%u device connected.\n",
2870 hidpp
->protocol_major
, hidpp
->protocol_minor
);
2873 if (hidpp
->name
== hdev
->name
&& hidpp
->protocol_major
>= 2) {
2874 name
= hidpp_get_device_name(hidpp
);
2877 "unable to retrieve the name of the device");
2881 devm_name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s", name
);
2886 hidpp
->name
= devm_name
;
2889 hidpp_initialize_battery(hidpp
);
2891 /* forward current battery state */
2892 if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP10_BATTERY
) {
2893 hidpp10_enable_battery_reporting(hidpp
);
2894 if (hidpp
->capabilities
& HIDPP_CAPABILITY_BATTERY_MILEAGE
)
2895 hidpp10_query_battery_mileage(hidpp
);
2897 hidpp10_query_battery_status(hidpp
);
2898 } else if (hidpp
->capabilities
& HIDPP_CAPABILITY_HIDPP20_BATTERY
) {
2899 hidpp20_query_battery_info(hidpp
);
2901 if (hidpp
->battery
.ps
)
2902 power_supply_changed(hidpp
->battery
.ps
);
2904 if (!(hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
) || hidpp
->delayed_input
)
2905 /* if the input nodes are already created, we can stop now */
2908 input
= hidpp_allocate_input(hdev
);
2910 hid_err(hdev
, "cannot allocate new input device: %d\n", ret
);
2914 hidpp_populate_input(hidpp
, input
, false);
2916 ret
= input_register_device(input
);
2918 input_free_device(input
);
2920 hidpp
->delayed_input
= input
;
2923 static DEVICE_ATTR(builtin_power_supply
, 0000, NULL
, NULL
);
2925 static struct attribute
*sysfs_attrs
[] = {
2926 &dev_attr_builtin_power_supply
.attr
,
2930 static const struct attribute_group ps_attribute_group
= {
2931 .attrs
= sysfs_attrs
2934 static int hidpp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
2936 struct hidpp_device
*hidpp
;
2939 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
2941 hidpp
= devm_kzalloc(&hdev
->dev
, sizeof(struct hidpp_device
),
2946 hidpp
->hid_dev
= hdev
;
2947 hidpp
->name
= hdev
->name
;
2948 hid_set_drvdata(hdev
, hidpp
);
2950 hidpp
->quirks
= id
->driver_data
;
2952 if (id
->group
== HID_GROUP_LOGITECH_DJ_DEVICE
)
2953 hidpp
->quirks
|= HIDPP_QUIRK_UNIFYING
;
2955 if (disable_raw_mode
) {
2956 hidpp
->quirks
&= ~HIDPP_QUIRK_CLASS_WTP
;
2957 hidpp
->quirks
&= ~HIDPP_QUIRK_NO_HIDINPUT
;
2960 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
2961 ret
= wtp_allocate(hdev
, id
);
2964 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
2965 ret
= m560_allocate(hdev
);
2968 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_K400
) {
2969 ret
= k400_allocate(hdev
);
2974 INIT_WORK(&hidpp
->work
, delayed_work_cb
);
2975 mutex_init(&hidpp
->send_mutex
);
2976 init_waitqueue_head(&hidpp
->wait
);
2978 /* indicates we are handling the battery properties in the kernel */
2979 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
2981 hid_warn(hdev
, "Cannot allocate sysfs group for %s\n",
2984 ret
= hid_parse(hdev
);
2986 hid_err(hdev
, "%s:parse failed\n", __func__
);
2987 goto hid_parse_fail
;
2990 if (hidpp
->quirks
& HIDPP_QUIRK_NO_HIDINPUT
)
2991 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
2993 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
2994 ret
= hid_hw_start(hdev
, connect_mask
);
2996 hid_err(hdev
, "hw start failed\n");
2997 goto hid_hw_start_fail
;
2999 ret
= hid_hw_open(hdev
);
3001 dev_err(&hdev
->dev
, "%s:hid_hw_open returned error:%d\n",
3004 goto hid_hw_start_fail
;
3009 /* Allow incoming packets */
3010 hid_device_io_start(hdev
);
3012 if (hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)
3013 hidpp_unifying_init(hidpp
);
3015 connected
= hidpp_is_connected(hidpp
);
3016 atomic_set(&hidpp
->connected
, connected
);
3017 if (!(hidpp
->quirks
& HIDPP_QUIRK_UNIFYING
)) {
3020 hid_err(hdev
, "Device not connected");
3021 goto hid_hw_open_failed
;
3024 hid_info(hdev
, "HID++ %u.%u device connected.\n",
3025 hidpp
->protocol_major
, hidpp
->protocol_minor
);
3027 hidpp_overwrite_name(hdev
);
3030 if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)) {
3031 ret
= wtp_get_config(hidpp
);
3033 goto hid_hw_open_failed
;
3034 } else if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
)) {
3035 ret
= g920_get_config(hidpp
);
3037 goto hid_hw_open_failed
;
3040 /* Block incoming packets */
3041 hid_device_io_stop(hdev
);
3043 if (!(hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
)) {
3044 ret
= hid_hw_start(hdev
, connect_mask
);
3046 hid_err(hdev
, "%s:hid_hw_start returned error\n", __func__
);
3047 goto hid_hw_start_fail
;
3051 /* Allow incoming packets */
3052 hid_device_io_start(hdev
);
3054 hidpp_connect_event(hidpp
);
3059 hid_device_io_stop(hdev
);
3060 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
3066 sysfs_remove_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
3067 cancel_work_sync(&hidpp
->work
);
3068 mutex_destroy(&hidpp
->send_mutex
);
3070 hid_set_drvdata(hdev
, NULL
);
3074 static void hidpp_remove(struct hid_device
*hdev
)
3076 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
3078 sysfs_remove_group(&hdev
->dev
.kobj
, &ps_attribute_group
);
3080 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_G920
) {
3081 hidpp_ff_deinit(hdev
);
3085 cancel_work_sync(&hidpp
->work
);
3086 mutex_destroy(&hidpp
->send_mutex
);
3089 static const struct hid_device_id hidpp_devices
[] = {
3090 { /* wireless touchpad */
3091 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
3092 USB_VENDOR_ID_LOGITECH
, 0x4011),
3093 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
|
3094 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
},
3095 { /* wireless touchpad T650 */
3096 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
3097 USB_VENDOR_ID_LOGITECH
, 0x4101),
3098 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
},
3099 { /* wireless touchpad T651 */
3100 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
,
3101 USB_DEVICE_ID_LOGITECH_T651
),
3102 .driver_data
= HIDPP_QUIRK_CLASS_WTP
},
3103 { /* Mouse logitech M560 */
3104 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
3105 USB_VENDOR_ID_LOGITECH
, 0x402d),
3106 .driver_data
= HIDPP_QUIRK_DELAYED_INIT
| HIDPP_QUIRK_CLASS_M560
},
3107 { /* Keyboard logitech K400 */
3108 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
3109 USB_VENDOR_ID_LOGITECH
, 0x4024),
3110 .driver_data
= HIDPP_QUIRK_CLASS_K400
},
3111 { /* Solar Keyboard Logitech K750 */
3112 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
3113 USB_VENDOR_ID_LOGITECH
, 0x4002),
3114 .driver_data
= HIDPP_QUIRK_CLASS_K750
},
3116 { HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
3117 USB_VENDOR_ID_LOGITECH
, HID_ANY_ID
)},
3119 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH
, USB_DEVICE_ID_LOGITECH_G920_WHEEL
),
3120 .driver_data
= HIDPP_QUIRK_CLASS_G920
| HIDPP_QUIRK_FORCE_OUTPUT_REPORTS
},
3124 MODULE_DEVICE_TABLE(hid
, hidpp_devices
);
3126 static struct hid_driver hidpp_driver
= {
3127 .name
= "logitech-hidpp-device",
3128 .id_table
= hidpp_devices
,
3129 .probe
= hidpp_probe
,
3130 .remove
= hidpp_remove
,
3131 .raw_event
= hidpp_raw_event
,
3132 .input_configured
= hidpp_input_configured
,
3133 .input_mapping
= hidpp_input_mapping
,
3134 .input_mapped
= hidpp_input_mapped
,
3137 module_hid_driver(hidpp_driver
);