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/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/kfifo.h>
23 #include <linux/input/mt.h>
24 #include <asm/unaligned.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
31 static bool disable_raw_mode
;
32 module_param(disable_raw_mode
, bool, 0644);
33 MODULE_PARM_DESC(disable_raw_mode
,
34 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
36 #define REPORT_ID_HIDPP_SHORT 0x10
37 #define REPORT_ID_HIDPP_LONG 0x11
39 #define HIDPP_REPORT_SHORT_LENGTH 7
40 #define HIDPP_REPORT_LONG_LENGTH 20
42 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
43 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
45 /* bits 2..20 are reserved for classes */
46 #define HIDPP_QUIRK_DELAYED_INIT BIT(21)
47 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
50 * There are two hidpp protocols in use, the first version hidpp10 is known
51 * as register access protocol or RAP, the second version hidpp20 is known as
52 * feature access protocol or FAP
54 * Most older devices (including the Unifying usb receiver) use the RAP protocol
55 * where as most newer devices use the FAP protocol. Both protocols are
56 * compatible with the underlying transport, which could be usb, Unifiying, or
57 * bluetooth. The message lengths are defined by the hid vendor specific report
58 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
59 * the HIDPP_LONG report type (total message length 20 bytes)
61 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
62 * messages. The Unifying receiver itself responds to RAP messages (device index
63 * is 0xFF for the receiver), and all messages (short or long) with a device
64 * index between 1 and 6 are passed untouched to the corresponding paired
67 * The paired device can be RAP or FAP, it will receive the message untouched
68 * from the Unifiying receiver.
73 u8 funcindex_clientid
;
74 u8 params
[HIDPP_REPORT_LONG_LENGTH
- 4U];
80 u8 params
[HIDPP_REPORT_LONG_LENGTH
- 4U];
89 u8 rawbytes
[sizeof(struct fap
)];
94 struct hid_device
*hid_dev
;
95 struct mutex send_mutex
;
96 void *send_receive_buf
;
97 char *name
; /* will never be NULL and should not be freed */
98 wait_queue_head_t wait
;
99 bool answer_available
;
105 struct work_struct work
;
106 struct kfifo delayed_work_fifo
;
108 struct input_dev
*delayed_input
;
110 unsigned long quirks
;
114 /* HID++ 1.0 error codes */
115 #define HIDPP_ERROR 0x8f
116 #define HIDPP_ERROR_SUCCESS 0x00
117 #define HIDPP_ERROR_INVALID_SUBID 0x01
118 #define HIDPP_ERROR_INVALID_ADRESS 0x02
119 #define HIDPP_ERROR_INVALID_VALUE 0x03
120 #define HIDPP_ERROR_CONNECT_FAIL 0x04
121 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
122 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
123 #define HIDPP_ERROR_BUSY 0x07
124 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
125 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
126 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
127 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
128 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
129 /* HID++ 2.0 error codes */
130 #define HIDPP20_ERROR 0xff
132 static void hidpp_connect_event(struct hidpp_device
*hidpp_dev
);
134 static int __hidpp_send_report(struct hid_device
*hdev
,
135 struct hidpp_report
*hidpp_report
)
137 int fields_count
, ret
;
139 switch (hidpp_report
->report_id
) {
140 case REPORT_ID_HIDPP_SHORT
:
141 fields_count
= HIDPP_REPORT_SHORT_LENGTH
;
143 case REPORT_ID_HIDPP_LONG
:
144 fields_count
= HIDPP_REPORT_LONG_LENGTH
;
151 * set the device_index as the receiver, it will be overwritten by
152 * hid_hw_request if needed
154 hidpp_report
->device_index
= 0xff;
156 ret
= hid_hw_raw_request(hdev
, hidpp_report
->report_id
,
157 (u8
*)hidpp_report
, fields_count
, HID_OUTPUT_REPORT
,
160 return ret
== fields_count
? 0 : -1;
164 * hidpp_send_message_sync() returns 0 in case of success, and something else
165 * in case of a failure.
166 * - If ' something else' is positive, that means that an error has been raised
167 * by the protocol itself.
168 * - If ' something else' is negative, that means that we had a classic error
169 * (-ENOMEM, -EPIPE, etc...)
171 static int hidpp_send_message_sync(struct hidpp_device
*hidpp
,
172 struct hidpp_report
*message
,
173 struct hidpp_report
*response
)
177 mutex_lock(&hidpp
->send_mutex
);
179 hidpp
->send_receive_buf
= response
;
180 hidpp
->answer_available
= false;
183 * So that we can later validate the answer when it arrives
186 *response
= *message
;
188 ret
= __hidpp_send_report(hidpp
->hid_dev
, message
);
191 dbg_hid("__hidpp_send_report returned err: %d\n", ret
);
192 memset(response
, 0, sizeof(struct hidpp_report
));
196 if (!wait_event_timeout(hidpp
->wait
, hidpp
->answer_available
,
198 dbg_hid("%s:timeout waiting for response\n", __func__
);
199 memset(response
, 0, sizeof(struct hidpp_report
));
203 if (response
->report_id
== REPORT_ID_HIDPP_SHORT
&&
204 response
->rap
.sub_id
== HIDPP_ERROR
) {
205 ret
= response
->rap
.params
[1];
206 dbg_hid("%s:got hidpp error %02X\n", __func__
, ret
);
210 if (response
->report_id
== REPORT_ID_HIDPP_LONG
&&
211 response
->fap
.feature_index
== HIDPP20_ERROR
) {
212 ret
= response
->fap
.params
[1];
213 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__
, ret
);
218 mutex_unlock(&hidpp
->send_mutex
);
223 static int hidpp_send_fap_command_sync(struct hidpp_device
*hidpp
,
224 u8 feat_index
, u8 funcindex_clientid
, u8
*params
, int param_count
,
225 struct hidpp_report
*response
)
227 struct hidpp_report
*message
;
230 if (param_count
> sizeof(message
->fap
.params
))
233 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
236 message
->report_id
= REPORT_ID_HIDPP_LONG
;
237 message
->fap
.feature_index
= feat_index
;
238 message
->fap
.funcindex_clientid
= funcindex_clientid
;
239 memcpy(&message
->fap
.params
, params
, param_count
);
241 ret
= hidpp_send_message_sync(hidpp
, message
, response
);
246 static int hidpp_send_rap_command_sync(struct hidpp_device
*hidpp_dev
,
247 u8 report_id
, u8 sub_id
, u8 reg_address
, u8
*params
, int param_count
,
248 struct hidpp_report
*response
)
250 struct hidpp_report
*message
;
253 if ((report_id
!= REPORT_ID_HIDPP_SHORT
) &&
254 (report_id
!= REPORT_ID_HIDPP_LONG
))
257 if (param_count
> sizeof(message
->rap
.params
))
260 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
263 message
->report_id
= report_id
;
264 message
->rap
.sub_id
= sub_id
;
265 message
->rap
.reg_address
= reg_address
;
266 memcpy(&message
->rap
.params
, params
, param_count
);
268 ret
= hidpp_send_message_sync(hidpp_dev
, message
, response
);
273 static void delayed_work_cb(struct work_struct
*work
)
275 struct hidpp_device
*hidpp
= container_of(work
, struct hidpp_device
,
277 hidpp_connect_event(hidpp
);
280 static inline bool hidpp_match_answer(struct hidpp_report
*question
,
281 struct hidpp_report
*answer
)
283 return (answer
->fap
.feature_index
== question
->fap
.feature_index
) &&
284 (answer
->fap
.funcindex_clientid
== question
->fap
.funcindex_clientid
);
287 static inline bool hidpp_match_error(struct hidpp_report
*question
,
288 struct hidpp_report
*answer
)
290 return ((answer
->rap
.sub_id
== HIDPP_ERROR
) ||
291 (answer
->fap
.feature_index
== HIDPP20_ERROR
)) &&
292 (answer
->fap
.funcindex_clientid
== question
->fap
.feature_index
) &&
293 (answer
->fap
.params
[0] == question
->fap
.funcindex_clientid
);
296 static inline bool hidpp_report_is_connect_event(struct hidpp_report
*report
)
298 return (report
->report_id
== REPORT_ID_HIDPP_SHORT
) &&
299 (report
->rap
.sub_id
== 0x41);
303 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
305 static void hidpp_prefix_name(char **name
, int name_length
)
307 #define PREFIX_LENGTH 9 /* "Logitech " */
312 if (name_length
> PREFIX_LENGTH
&&
313 strncmp(*name
, "Logitech ", PREFIX_LENGTH
) == 0)
314 /* The prefix has is already in the name */
317 new_length
= PREFIX_LENGTH
+ name_length
;
318 new_name
= kzalloc(new_length
, GFP_KERNEL
);
322 snprintf(new_name
, new_length
, "Logitech %s", *name
);
329 /* -------------------------------------------------------------------------- */
330 /* HIDP++ 1.0 commands */
331 /* -------------------------------------------------------------------------- */
333 #define HIDPP_SET_REGISTER 0x80
334 #define HIDPP_GET_REGISTER 0x81
335 #define HIDPP_SET_LONG_REGISTER 0x82
336 #define HIDPP_GET_LONG_REGISTER 0x83
338 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
339 #define DEVICE_NAME 0x40
341 static char *hidpp_get_unifying_name(struct hidpp_device
*hidpp_dev
)
343 struct hidpp_report response
;
345 /* hid-logitech-dj is in charge of setting the right device index */
346 u8 params
[1] = { DEVICE_NAME
};
350 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
351 REPORT_ID_HIDPP_SHORT
,
352 HIDPP_GET_LONG_REGISTER
,
353 HIDPP_REG_PAIRING_INFORMATION
,
354 params
, 1, &response
);
358 len
= response
.rap
.params
[1];
360 if (2 + len
> sizeof(response
.rap
.params
))
363 name
= kzalloc(len
+ 1, GFP_KERNEL
);
367 memcpy(name
, &response
.rap
.params
[2], len
);
369 /* include the terminating '\0' */
370 hidpp_prefix_name(&name
, len
+ 1);
375 /* -------------------------------------------------------------------------- */
377 /* -------------------------------------------------------------------------- */
379 #define HIDPP_PAGE_ROOT 0x0000
380 #define HIDPP_PAGE_ROOT_IDX 0x00
382 #define CMD_ROOT_GET_FEATURE 0x01
383 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
385 static int hidpp_root_get_feature(struct hidpp_device
*hidpp
, u16 feature
,
386 u8
*feature_index
, u8
*feature_type
)
388 struct hidpp_report response
;
390 u8 params
[2] = { feature
>> 8, feature
& 0x00FF };
392 ret
= hidpp_send_fap_command_sync(hidpp
,
394 CMD_ROOT_GET_FEATURE
,
395 params
, 2, &response
);
399 *feature_index
= response
.fap
.params
[0];
400 *feature_type
= response
.fap
.params
[1];
405 static int hidpp_root_get_protocol_version(struct hidpp_device
*hidpp
)
407 struct hidpp_report response
;
410 ret
= hidpp_send_fap_command_sync(hidpp
,
412 CMD_ROOT_GET_PROTOCOL_VERSION
,
415 if (ret
== HIDPP_ERROR_INVALID_SUBID
) {
416 hidpp
->protocol_major
= 1;
417 hidpp
->protocol_minor
= 0;
421 /* the device might not be connected */
422 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
426 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
433 hidpp
->protocol_major
= response
.fap
.params
[0];
434 hidpp
->protocol_minor
= response
.fap
.params
[1];
439 static bool hidpp_is_connected(struct hidpp_device
*hidpp
)
443 ret
= hidpp_root_get_protocol_version(hidpp
);
445 hid_dbg(hidpp
->hid_dev
, "HID++ %u.%u device connected.\n",
446 hidpp
->protocol_major
, hidpp
->protocol_minor
);
450 /* -------------------------------------------------------------------------- */
451 /* 0x0005: GetDeviceNameType */
452 /* -------------------------------------------------------------------------- */
454 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
456 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
457 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
458 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
460 static int hidpp_devicenametype_get_count(struct hidpp_device
*hidpp
,
461 u8 feature_index
, u8
*nameLength
)
463 struct hidpp_report response
;
466 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
467 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT
, NULL
, 0, &response
);
470 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
477 *nameLength
= response
.fap
.params
[0];
482 static int hidpp_devicenametype_get_device_name(struct hidpp_device
*hidpp
,
483 u8 feature_index
, u8 char_index
, char *device_name
, int len_buf
)
485 struct hidpp_report response
;
489 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
490 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME
, &char_index
, 1,
494 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
501 if (response
.report_id
== REPORT_ID_HIDPP_LONG
)
502 count
= HIDPP_REPORT_LONG_LENGTH
- 4;
504 count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
509 for (i
= 0; i
< count
; i
++)
510 device_name
[i
] = response
.fap
.params
[i
];
515 static char *hidpp_get_device_name(struct hidpp_device
*hidpp
)
524 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_GET_DEVICE_NAME_TYPE
,
525 &feature_index
, &feature_type
);
529 ret
= hidpp_devicenametype_get_count(hidpp
, feature_index
,
534 name
= kzalloc(__name_length
+ 1, GFP_KERNEL
);
538 while (index
< __name_length
) {
539 ret
= hidpp_devicenametype_get_device_name(hidpp
,
540 feature_index
, index
, name
+ index
,
541 __name_length
- index
);
549 /* include the terminating '\0' */
550 hidpp_prefix_name(&name
, __name_length
+ 1);
555 /* -------------------------------------------------------------------------- */
556 /* 0x6100: TouchPadRawXY */
557 /* -------------------------------------------------------------------------- */
559 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
561 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
562 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
564 #define EVENT_TOUCHPAD_RAW_XY 0x00
566 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
567 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
569 struct hidpp_touchpad_raw_info
{
580 struct hidpp_touchpad_raw_xy_finger
{
590 struct hidpp_touchpad_raw_xy
{
592 struct hidpp_touchpad_raw_xy_finger fingers
[2];
599 static int hidpp_touchpad_get_raw_info(struct hidpp_device
*hidpp
,
600 u8 feature_index
, struct hidpp_touchpad_raw_info
*raw_info
)
602 struct hidpp_report response
;
604 u8
*params
= (u8
*)response
.fap
.params
;
606 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
607 CMD_TOUCHPAD_GET_RAW_INFO
, NULL
, 0, &response
);
610 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
617 raw_info
->x_size
= get_unaligned_be16(¶ms
[0]);
618 raw_info
->y_size
= get_unaligned_be16(¶ms
[2]);
619 raw_info
->z_range
= params
[4];
620 raw_info
->area_range
= params
[5];
621 raw_info
->maxcontacts
= params
[7];
622 raw_info
->origin
= params
[8];
623 /* res is given in unit per inch */
624 raw_info
->res
= get_unaligned_be16(¶ms
[13]) * 2 / 51;
629 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device
*hidpp_dev
,
630 u8 feature_index
, bool send_raw_reports
,
631 bool sensor_enhanced_settings
)
633 struct hidpp_report response
;
638 * bit 1 - 16bit Z, no area
639 * bit 2 - enhanced sensitivity
640 * bit 3 - width, height (4 bits each) instead of area
641 * bit 4 - send raw + gestures (degrades smoothness)
642 * remaining bits - reserved
644 u8 params
= send_raw_reports
| (sensor_enhanced_settings
<< 2);
646 return hidpp_send_fap_command_sync(hidpp_dev
, feature_index
,
647 CMD_TOUCHPAD_SET_RAW_REPORT_STATE
, ¶ms
, 1, &response
);
650 static void hidpp_touchpad_touch_event(u8
*data
,
651 struct hidpp_touchpad_raw_xy_finger
*finger
)
653 u8 x_m
= data
[0] << 2;
654 u8 y_m
= data
[2] << 2;
656 finger
->x
= x_m
<< 6 | data
[1];
657 finger
->y
= y_m
<< 6 | data
[3];
659 finger
->contact_type
= data
[0] >> 6;
660 finger
->contact_status
= data
[2] >> 6;
663 finger
->area
= data
[5];
664 finger
->finger_id
= data
[6] >> 4;
667 static void hidpp_touchpad_raw_xy_event(struct hidpp_device
*hidpp_dev
,
668 u8
*data
, struct hidpp_touchpad_raw_xy
*raw_xy
)
670 memset(raw_xy
, 0, sizeof(struct hidpp_touchpad_raw_xy
));
671 raw_xy
->end_of_frame
= data
[8] & 0x01;
672 raw_xy
->spurious_flag
= (data
[8] >> 1) & 0x01;
673 raw_xy
->finger_count
= data
[15] & 0x0f;
674 raw_xy
->button
= (data
[8] >> 2) & 0x01;
676 if (raw_xy
->finger_count
) {
677 hidpp_touchpad_touch_event(&data
[2], &raw_xy
->fingers
[0]);
678 hidpp_touchpad_touch_event(&data
[9], &raw_xy
->fingers
[1]);
682 /* ************************************************************************** */
686 /* ************************************************************************** */
688 /* -------------------------------------------------------------------------- */
689 /* Touchpad HID++ devices */
690 /* -------------------------------------------------------------------------- */
692 #define WTP_MANUAL_RESOLUTION 39
695 struct input_dev
*input
;
699 u8 button_feature_index
;
702 unsigned int resolution
;
705 static int wtp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
706 struct hid_field
*field
, struct hid_usage
*usage
,
707 unsigned long **bit
, int *max
)
712 static void wtp_populate_input(struct hidpp_device
*hidpp
,
713 struct input_dev
*input_dev
, bool origin_is_hid_core
)
715 struct wtp_data
*wd
= hidpp
->private_data
;
717 __set_bit(EV_ABS
, input_dev
->evbit
);
718 __set_bit(EV_KEY
, input_dev
->evbit
);
719 __clear_bit(EV_REL
, input_dev
->evbit
);
720 __clear_bit(EV_LED
, input_dev
->evbit
);
722 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, wd
->x_size
, 0, 0);
723 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
, wd
->resolution
);
724 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, wd
->y_size
, 0, 0);
725 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
, wd
->resolution
);
727 /* Max pressure is not given by the devices, pick one */
728 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 50, 0, 0);
730 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
732 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
)
733 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
735 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
737 input_mt_init_slots(input_dev
, wd
->maxcontacts
, INPUT_MT_POINTER
|
738 INPUT_MT_DROP_UNUSED
);
740 wd
->input
= input_dev
;
743 static void wtp_touch_event(struct wtp_data
*wd
,
744 struct hidpp_touchpad_raw_xy_finger
*touch_report
)
748 if (!touch_report
->finger_id
|| touch_report
->contact_type
)
752 slot
= input_mt_get_slot_by_key(wd
->input
, touch_report
->finger_id
);
754 input_mt_slot(wd
->input
, slot
);
755 input_mt_report_slot_state(wd
->input
, MT_TOOL_FINGER
,
756 touch_report
->contact_status
);
757 if (touch_report
->contact_status
) {
758 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_X
,
760 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_Y
,
761 wd
->flip_y
? wd
->y_size
- touch_report
->y
:
763 input_event(wd
->input
, EV_ABS
, ABS_MT_PRESSURE
,
768 static void wtp_send_raw_xy_event(struct hidpp_device
*hidpp
,
769 struct hidpp_touchpad_raw_xy
*raw
)
771 struct wtp_data
*wd
= hidpp
->private_data
;
774 for (i
= 0; i
< 2; i
++)
775 wtp_touch_event(wd
, &(raw
->fingers
[i
]));
777 if (raw
->end_of_frame
&&
778 !(hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
))
779 input_event(wd
->input
, EV_KEY
, BTN_LEFT
, raw
->button
);
781 if (raw
->end_of_frame
|| raw
->finger_count
<= 2) {
782 input_mt_sync_frame(wd
->input
);
783 input_sync(wd
->input
);
787 static int wtp_mouse_raw_xy_event(struct hidpp_device
*hidpp
, u8
*data
)
789 struct wtp_data
*wd
= hidpp
->private_data
;
790 u8 c1_area
= ((data
[7] & 0xf) * (data
[7] & 0xf) +
791 (data
[7] >> 4) * (data
[7] >> 4)) / 2;
792 u8 c2_area
= ((data
[13] & 0xf) * (data
[13] & 0xf) +
793 (data
[13] >> 4) * (data
[13] >> 4)) / 2;
794 struct hidpp_touchpad_raw_xy raw
= {
795 .timestamp
= data
[1],
799 .contact_status
= !!data
[7],
800 .x
= get_unaligned_le16(&data
[3]),
801 .y
= get_unaligned_le16(&data
[5]),
804 .finger_id
= data
[2],
807 .contact_status
= !!data
[13],
808 .x
= get_unaligned_le16(&data
[9]),
809 .y
= get_unaligned_le16(&data
[11]),
812 .finger_id
= data
[8],
815 .finger_count
= wd
->maxcontacts
,
817 .end_of_frame
= (data
[0] >> 7) == 0,
818 .button
= data
[0] & 0x01,
821 wtp_send_raw_xy_event(hidpp
, &raw
);
826 static int wtp_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
828 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
829 struct wtp_data
*wd
= hidpp
->private_data
;
830 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
831 struct hidpp_touchpad_raw_xy raw
;
833 if (!wd
|| !wd
->input
)
839 hid_err(hdev
, "Received HID report of bad size (%d)",
843 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
) {
844 input_event(wd
->input
, EV_KEY
, BTN_LEFT
,
846 input_event(wd
->input
, EV_KEY
, BTN_RIGHT
,
848 input_sync(wd
->input
);
853 return wtp_mouse_raw_xy_event(hidpp
, &data
[7]);
855 case REPORT_ID_HIDPP_LONG
:
856 /* size is already checked in hidpp_raw_event. */
857 if ((report
->fap
.feature_index
!= wd
->mt_feature_index
) ||
858 (report
->fap
.funcindex_clientid
!= EVENT_TOUCHPAD_RAW_XY
))
860 hidpp_touchpad_raw_xy_event(hidpp
, data
+ 4, &raw
);
862 wtp_send_raw_xy_event(hidpp
, &raw
);
869 static int wtp_get_config(struct hidpp_device
*hidpp
)
871 struct wtp_data
*wd
= hidpp
->private_data
;
872 struct hidpp_touchpad_raw_info raw_info
= {0};
876 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_TOUCHPAD_RAW_XY
,
877 &wd
->mt_feature_index
, &feature_type
);
879 /* means that the device is not powered up */
882 ret
= hidpp_touchpad_get_raw_info(hidpp
, wd
->mt_feature_index
,
887 wd
->x_size
= raw_info
.x_size
;
888 wd
->y_size
= raw_info
.y_size
;
889 wd
->maxcontacts
= raw_info
.maxcontacts
;
890 wd
->flip_y
= raw_info
.origin
== TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT
;
891 wd
->resolution
= raw_info
.res
;
893 wd
->resolution
= WTP_MANUAL_RESOLUTION
;
898 static int wtp_allocate(struct hid_device
*hdev
, const struct hid_device_id
*id
)
900 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
903 wd
= devm_kzalloc(&hdev
->dev
, sizeof(struct wtp_data
),
908 hidpp
->private_data
= wd
;
913 static int wtp_connect(struct hid_device
*hdev
, bool connected
)
915 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
916 struct wtp_data
*wd
= hidpp
->private_data
;
923 ret
= wtp_get_config(hidpp
);
925 hid_err(hdev
, "Can not get wtp config: %d\n", ret
);
930 return hidpp_touchpad_set_raw_report_state(hidpp
, wd
->mt_feature_index
,
934 /* ------------------------------------------------------------------------- */
935 /* Logitech M560 devices */
936 /* ------------------------------------------------------------------------- */
939 * Logitech M560 protocol overview
941 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
942 * the sides buttons are pressed, it sends some keyboard keys events
943 * instead of buttons ones.
944 * To complicate things further, the middle button keys sequence
945 * is different from the odd press and the even press.
947 * forward button -> Super_R
948 * backward button -> Super_L+'d' (press only)
949 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
950 * 2nd time: left-click (press only)
951 * NB: press-only means that when the button is pressed, the
952 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
953 * together sequentially; instead when the button is released, no event is
957 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
958 * the mouse reacts differently:
959 * - it never sends a keyboard key event
960 * - for the three mouse button it sends:
961 * middle button press 11<xx>0a 3500af00...
962 * side 1 button (forward) press 11<xx>0a 3500b000...
963 * side 2 button (backward) press 11<xx>0a 3500ae00...
964 * middle/side1/side2 button release 11<xx>0a 35000000...
967 static const u8 m560_config_parameter
[] = {0x00, 0xaf, 0x03};
969 struct m560_private_data
{
970 struct input_dev
*input
;
973 /* how buttons are mapped in the report */
974 #define M560_MOUSE_BTN_LEFT 0x01
975 #define M560_MOUSE_BTN_RIGHT 0x02
976 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
977 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
979 #define M560_SUB_ID 0x0a
980 #define M560_BUTTON_MODE_REGISTER 0x35
982 static int m560_send_config_command(struct hid_device
*hdev
, bool connected
)
984 struct hidpp_report response
;
985 struct hidpp_device
*hidpp_dev
;
987 hidpp_dev
= hid_get_drvdata(hdev
);
992 return hidpp_send_rap_command_sync(
994 REPORT_ID_HIDPP_SHORT
,
996 M560_BUTTON_MODE_REGISTER
,
997 (u8
*)m560_config_parameter
,
998 sizeof(m560_config_parameter
),
1003 static int m560_allocate(struct hid_device
*hdev
)
1005 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1006 struct m560_private_data
*d
;
1008 d
= devm_kzalloc(&hdev
->dev
, sizeof(struct m560_private_data
),
1013 hidpp
->private_data
= d
;
1018 static int m560_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
1020 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1021 struct m560_private_data
*mydata
= hidpp
->private_data
;
1024 if (!mydata
|| !mydata
->input
) {
1025 hid_err(hdev
, "error in parameter\n");
1030 hid_err(hdev
, "error in report\n");
1034 if (data
[0] == REPORT_ID_HIDPP_LONG
&&
1035 data
[2] == M560_SUB_ID
&& data
[6] == 0x00) {
1037 * m560 mouse report for middle, forward and backward button
1040 * data[1] = device-id
1042 * data[5] = 0xaf -> middle
1045 * 0x00 -> release all
1051 input_report_key(mydata
->input
, BTN_MIDDLE
, 1);
1054 input_report_key(mydata
->input
, BTN_FORWARD
, 1);
1057 input_report_key(mydata
->input
, BTN_BACK
, 1);
1060 input_report_key(mydata
->input
, BTN_BACK
, 0);
1061 input_report_key(mydata
->input
, BTN_FORWARD
, 0);
1062 input_report_key(mydata
->input
, BTN_MIDDLE
, 0);
1065 hid_err(hdev
, "error in report\n");
1068 input_sync(mydata
->input
);
1070 } else if (data
[0] == 0x02) {
1072 * Logitech M560 mouse report
1074 * data[0] = type (0x02)
1075 * data[1..2] = buttons
1082 input_report_key(mydata
->input
, BTN_LEFT
,
1083 !!(data
[1] & M560_MOUSE_BTN_LEFT
));
1084 input_report_key(mydata
->input
, BTN_RIGHT
,
1085 !!(data
[1] & M560_MOUSE_BTN_RIGHT
));
1087 if (data
[1] & M560_MOUSE_BTN_WHEEL_LEFT
)
1088 input_report_rel(mydata
->input
, REL_HWHEEL
, -1);
1089 else if (data
[1] & M560_MOUSE_BTN_WHEEL_RIGHT
)
1090 input_report_rel(mydata
->input
, REL_HWHEEL
, 1);
1092 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 0, 12), 12);
1093 input_report_rel(mydata
->input
, REL_X
, v
);
1095 v
= hid_snto32(hid_field_extract(hdev
, data
+3, 12, 12), 12);
1096 input_report_rel(mydata
->input
, REL_Y
, v
);
1098 v
= hid_snto32(data
[6], 8);
1099 input_report_rel(mydata
->input
, REL_WHEEL
, v
);
1101 input_sync(mydata
->input
);
1107 static void m560_populate_input(struct hidpp_device
*hidpp
,
1108 struct input_dev
*input_dev
, bool origin_is_hid_core
)
1110 struct m560_private_data
*mydata
= hidpp
->private_data
;
1112 mydata
->input
= input_dev
;
1114 __set_bit(EV_KEY
, mydata
->input
->evbit
);
1115 __set_bit(BTN_MIDDLE
, mydata
->input
->keybit
);
1116 __set_bit(BTN_RIGHT
, mydata
->input
->keybit
);
1117 __set_bit(BTN_LEFT
, mydata
->input
->keybit
);
1118 __set_bit(BTN_BACK
, mydata
->input
->keybit
);
1119 __set_bit(BTN_FORWARD
, mydata
->input
->keybit
);
1121 __set_bit(EV_REL
, mydata
->input
->evbit
);
1122 __set_bit(REL_X
, mydata
->input
->relbit
);
1123 __set_bit(REL_Y
, mydata
->input
->relbit
);
1124 __set_bit(REL_WHEEL
, mydata
->input
->relbit
);
1125 __set_bit(REL_HWHEEL
, mydata
->input
->relbit
);
1128 static int m560_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
1129 struct hid_field
*field
, struct hid_usage
*usage
,
1130 unsigned long **bit
, int *max
)
1135 /* -------------------------------------------------------------------------- */
1136 /* Generic HID++ devices */
1137 /* -------------------------------------------------------------------------- */
1139 static int hidpp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
1140 struct hid_field
*field
, struct hid_usage
*usage
,
1141 unsigned long **bit
, int *max
)
1143 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1145 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
1146 return wtp_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
1147 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
&&
1148 field
->application
!= HID_GD_MOUSE
)
1149 return m560_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
1154 static void hidpp_populate_input(struct hidpp_device
*hidpp
,
1155 struct input_dev
*input
, bool origin_is_hid_core
)
1157 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
1158 wtp_populate_input(hidpp
, input
, origin_is_hid_core
);
1159 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
1160 m560_populate_input(hidpp
, input
, origin_is_hid_core
);
1163 static void hidpp_input_configured(struct hid_device
*hdev
,
1164 struct hid_input
*hidinput
)
1166 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1167 struct input_dev
*input
= hidinput
->input
;
1169 hidpp_populate_input(hidpp
, input
, true);
1172 static int hidpp_raw_hidpp_event(struct hidpp_device
*hidpp
, u8
*data
,
1175 struct hidpp_report
*question
= hidpp
->send_receive_buf
;
1176 struct hidpp_report
*answer
= hidpp
->send_receive_buf
;
1177 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
1180 * If the mutex is locked then we have a pending answer from a
1181 * previously sent command.
1183 if (unlikely(mutex_is_locked(&hidpp
->send_mutex
))) {
1185 * Check for a correct hidpp20 answer or the corresponding
1188 if (hidpp_match_answer(question
, report
) ||
1189 hidpp_match_error(question
, report
)) {
1191 hidpp
->answer_available
= true;
1192 wake_up(&hidpp
->wait
);
1194 * This was an answer to a command that this driver sent
1195 * We return 1 to hid-core to avoid forwarding the
1196 * command upstream as it has been treated by the driver
1203 if (unlikely(hidpp_report_is_connect_event(report
))) {
1204 atomic_set(&hidpp
->connected
,
1205 !(report
->rap
.params
[0] & (1 << 6)));
1206 if ((hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
) &&
1207 (schedule_work(&hidpp
->work
) == 0))
1208 dbg_hid("%s: connect event already queued\n", __func__
);
1215 static int hidpp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1218 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1221 /* Generic HID++ processing. */
1223 case REPORT_ID_HIDPP_LONG
:
1224 if (size
!= HIDPP_REPORT_LONG_LENGTH
) {
1225 hid_err(hdev
, "received hid++ report of bad size (%d)",
1229 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
1231 case REPORT_ID_HIDPP_SHORT
:
1232 if (size
!= HIDPP_REPORT_SHORT_LENGTH
) {
1233 hid_err(hdev
, "received hid++ report of bad size (%d)",
1237 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
1241 /* If no report is available for further processing, skip calling
1242 * raw_event of subclasses. */
1246 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
1247 return wtp_raw_event(hdev
, data
, size
);
1248 else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
)
1249 return m560_raw_event(hdev
, data
, size
);
1254 static void hidpp_overwrite_name(struct hid_device
*hdev
, bool use_unifying
)
1256 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1261 * the device is connected through an Unifying receiver, and
1262 * might not be already connected.
1263 * Ask the receiver for its name.
1265 name
= hidpp_get_unifying_name(hidpp
);
1267 name
= hidpp_get_device_name(hidpp
);
1270 hid_err(hdev
, "unable to retrieve the name of the device");
1272 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
1277 static int hidpp_input_open(struct input_dev
*dev
)
1279 struct hid_device
*hid
= input_get_drvdata(dev
);
1281 return hid_hw_open(hid
);
1284 static void hidpp_input_close(struct input_dev
*dev
)
1286 struct hid_device
*hid
= input_get_drvdata(dev
);
1291 static struct input_dev
*hidpp_allocate_input(struct hid_device
*hdev
)
1293 struct input_dev
*input_dev
= devm_input_allocate_device(&hdev
->dev
);
1294 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1299 input_set_drvdata(input_dev
, hdev
);
1300 input_dev
->open
= hidpp_input_open
;
1301 input_dev
->close
= hidpp_input_close
;
1303 input_dev
->name
= hidpp
->name
;
1304 input_dev
->phys
= hdev
->phys
;
1305 input_dev
->uniq
= hdev
->uniq
;
1306 input_dev
->id
.bustype
= hdev
->bus
;
1307 input_dev
->id
.vendor
= hdev
->vendor
;
1308 input_dev
->id
.product
= hdev
->product
;
1309 input_dev
->id
.version
= hdev
->version
;
1310 input_dev
->dev
.parent
= &hdev
->dev
;
1315 static void hidpp_connect_event(struct hidpp_device
*hidpp
)
1317 struct hid_device
*hdev
= hidpp
->hid_dev
;
1319 bool connected
= atomic_read(&hidpp
->connected
);
1320 struct input_dev
*input
;
1321 char *name
, *devm_name
;
1323 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
1324 ret
= wtp_connect(hdev
, connected
);
1327 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
1328 ret
= m560_send_config_command(hdev
, connected
);
1333 if (!connected
|| hidpp
->delayed_input
)
1336 if (!hidpp
->protocol_major
) {
1337 ret
= !hidpp_is_connected(hidpp
);
1339 hid_err(hdev
, "Can not get the protocol version.\n");
1344 /* the device is already connected, we can ask for its name and
1346 hid_info(hdev
, "HID++ %u.%u device connected.\n",
1347 hidpp
->protocol_major
, hidpp
->protocol_minor
);
1349 if (!hidpp
->name
|| hidpp
->name
== hdev
->name
) {
1350 name
= hidpp_get_device_name(hidpp
);
1353 "unable to retrieve the name of the device");
1357 devm_name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s", name
);
1362 hidpp
->name
= devm_name
;
1365 input
= hidpp_allocate_input(hdev
);
1367 hid_err(hdev
, "cannot allocate new input device: %d\n", ret
);
1371 hidpp_populate_input(hidpp
, input
, false);
1373 ret
= input_register_device(input
);
1375 input_free_device(input
);
1377 hidpp
->delayed_input
= input
;
1380 static int hidpp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1382 struct hidpp_device
*hidpp
;
1385 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
1387 hidpp
= devm_kzalloc(&hdev
->dev
, sizeof(struct hidpp_device
),
1392 hidpp
->hid_dev
= hdev
;
1393 hidpp
->name
= hdev
->name
;
1394 hid_set_drvdata(hdev
, hidpp
);
1396 hidpp
->quirks
= id
->driver_data
;
1398 if (disable_raw_mode
) {
1399 hidpp
->quirks
&= ~HIDPP_QUIRK_CLASS_WTP
;
1400 hidpp
->quirks
&= ~HIDPP_QUIRK_DELAYED_INIT
;
1403 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
1404 ret
= wtp_allocate(hdev
, id
);
1407 } else if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_M560
) {
1408 ret
= m560_allocate(hdev
);
1413 INIT_WORK(&hidpp
->work
, delayed_work_cb
);
1414 mutex_init(&hidpp
->send_mutex
);
1415 init_waitqueue_head(&hidpp
->wait
);
1417 ret
= hid_parse(hdev
);
1419 hid_err(hdev
, "%s:parse failed\n", __func__
);
1420 goto hid_parse_fail
;
1423 /* Allow incoming packets */
1424 hid_device_io_start(hdev
);
1426 connected
= hidpp_is_connected(hidpp
);
1427 if (id
->group
!= HID_GROUP_LOGITECH_DJ_DEVICE
) {
1430 hid_err(hdev
, "Device not connected");
1431 hid_device_io_stop(hdev
);
1432 goto hid_parse_fail
;
1435 hid_info(hdev
, "HID++ %u.%u device connected.\n",
1436 hidpp
->protocol_major
, hidpp
->protocol_minor
);
1439 hidpp_overwrite_name(hdev
, id
->group
== HID_GROUP_LOGITECH_DJ_DEVICE
);
1440 atomic_set(&hidpp
->connected
, connected
);
1442 if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)) {
1443 ret
= wtp_get_config(hidpp
);
1445 goto hid_parse_fail
;
1448 /* Block incoming packets */
1449 hid_device_io_stop(hdev
);
1451 if (hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
)
1452 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
1454 ret
= hid_hw_start(hdev
, connect_mask
);
1456 hid_err(hdev
, "%s:hid_hw_start returned error\n", __func__
);
1457 goto hid_hw_start_fail
;
1460 if (hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
) {
1461 /* Allow incoming packets */
1462 hid_device_io_start(hdev
);
1464 hidpp_connect_event(hidpp
);
1471 cancel_work_sync(&hidpp
->work
);
1472 mutex_destroy(&hidpp
->send_mutex
);
1474 hid_set_drvdata(hdev
, NULL
);
1478 static void hidpp_remove(struct hid_device
*hdev
)
1480 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1482 cancel_work_sync(&hidpp
->work
);
1483 mutex_destroy(&hidpp
->send_mutex
);
1487 static const struct hid_device_id hidpp_devices
[] = {
1488 { /* wireless touchpad */
1489 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1490 USB_VENDOR_ID_LOGITECH
, 0x4011),
1491 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
|
1492 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
},
1493 { /* wireless touchpad T650 */
1494 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1495 USB_VENDOR_ID_LOGITECH
, 0x4101),
1496 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
},
1497 { /* wireless touchpad T651 */
1498 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
,
1499 USB_DEVICE_ID_LOGITECH_T651
),
1500 .driver_data
= HIDPP_QUIRK_CLASS_WTP
},
1501 { /* Mouse logitech M560 */
1502 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1503 USB_VENDOR_ID_LOGITECH
, 0x402d),
1504 .driver_data
= HIDPP_QUIRK_DELAYED_INIT
| HIDPP_QUIRK_CLASS_M560
},
1506 { HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1507 USB_VENDOR_ID_LOGITECH
, HID_ANY_ID
)},
1511 MODULE_DEVICE_TABLE(hid
, hidpp_devices
);
1513 static struct hid_driver hidpp_driver
= {
1514 .name
= "logitech-hidpp-device",
1515 .id_table
= hidpp_devices
,
1516 .probe
= hidpp_probe
,
1517 .remove
= hidpp_remove
,
1518 .raw_event
= hidpp_raw_event
,
1519 .input_configured
= hidpp_input_configured
,
1520 .input_mapping
= hidpp_input_mapping
,
1523 module_hid_driver(hidpp_driver
);