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 #define REPORT_ID_HIDPP_SHORT 0x10
32 #define REPORT_ID_HIDPP_LONG 0x11
34 #define HIDPP_REPORT_SHORT_LENGTH 7
35 #define HIDPP_REPORT_LONG_LENGTH 20
37 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
39 /* bits 1..20 are reserved for classes */
40 #define HIDPP_QUIRK_DELAYED_INIT BIT(21)
41 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
42 #define HIDPP_QUIRK_MULTI_INPUT BIT(23)
45 * There are two hidpp protocols in use, the first version hidpp10 is known
46 * as register access protocol or RAP, the second version hidpp20 is known as
47 * feature access protocol or FAP
49 * Most older devices (including the Unifying usb receiver) use the RAP protocol
50 * where as most newer devices use the FAP protocol. Both protocols are
51 * compatible with the underlying transport, which could be usb, Unifiying, or
52 * bluetooth. The message lengths are defined by the hid vendor specific report
53 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
54 * the HIDPP_LONG report type (total message length 20 bytes)
56 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
57 * messages. The Unifying receiver itself responds to RAP messages (device index
58 * is 0xFF for the receiver), and all messages (short or long) with a device
59 * index between 1 and 6 are passed untouched to the corresponding paired
62 * The paired device can be RAP or FAP, it will receive the message untouched
63 * from the Unifiying receiver.
68 u8 funcindex_clientid
;
69 u8 params
[HIDPP_REPORT_LONG_LENGTH
- 4U];
75 u8 params
[HIDPP_REPORT_LONG_LENGTH
- 4U];
84 u8 rawbytes
[sizeof(struct fap
)];
89 struct hid_device
*hid_dev
;
90 struct mutex send_mutex
;
91 void *send_receive_buf
;
92 wait_queue_head_t wait
;
93 bool answer_available
;
99 struct work_struct work
;
100 struct kfifo delayed_work_fifo
;
102 struct input_dev
*delayed_input
;
104 unsigned long quirks
;
108 #define HIDPP_ERROR 0x8f
109 #define HIDPP_ERROR_SUCCESS 0x00
110 #define HIDPP_ERROR_INVALID_SUBID 0x01
111 #define HIDPP_ERROR_INVALID_ADRESS 0x02
112 #define HIDPP_ERROR_INVALID_VALUE 0x03
113 #define HIDPP_ERROR_CONNECT_FAIL 0x04
114 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
115 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
116 #define HIDPP_ERROR_BUSY 0x07
117 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
118 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
119 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
120 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
121 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
123 static void hidpp_connect_event(struct hidpp_device
*hidpp_dev
);
125 static int __hidpp_send_report(struct hid_device
*hdev
,
126 struct hidpp_report
*hidpp_report
)
128 int fields_count
, ret
;
130 switch (hidpp_report
->report_id
) {
131 case REPORT_ID_HIDPP_SHORT
:
132 fields_count
= HIDPP_REPORT_SHORT_LENGTH
;
134 case REPORT_ID_HIDPP_LONG
:
135 fields_count
= HIDPP_REPORT_LONG_LENGTH
;
142 * set the device_index as the receiver, it will be overwritten by
143 * hid_hw_request if needed
145 hidpp_report
->device_index
= 0xff;
147 ret
= hid_hw_raw_request(hdev
, hidpp_report
->report_id
,
148 (u8
*)hidpp_report
, fields_count
, HID_OUTPUT_REPORT
,
151 return ret
== fields_count
? 0 : -1;
155 * hidpp_send_message_sync() returns 0 in case of success, and something else
156 * in case of a failure.
157 * - If ' something else' is positive, that means that an error has been raised
158 * by the protocol itself.
159 * - If ' something else' is negative, that means that we had a classic error
160 * (-ENOMEM, -EPIPE, etc...)
162 static int hidpp_send_message_sync(struct hidpp_device
*hidpp
,
163 struct hidpp_report
*message
,
164 struct hidpp_report
*response
)
168 mutex_lock(&hidpp
->send_mutex
);
170 hidpp
->send_receive_buf
= response
;
171 hidpp
->answer_available
= false;
174 * So that we can later validate the answer when it arrives
177 *response
= *message
;
179 ret
= __hidpp_send_report(hidpp
->hid_dev
, message
);
182 dbg_hid("__hidpp_send_report returned err: %d\n", ret
);
183 memset(response
, 0, sizeof(struct hidpp_report
));
187 if (!wait_event_timeout(hidpp
->wait
, hidpp
->answer_available
,
189 dbg_hid("%s:timeout waiting for response\n", __func__
);
190 memset(response
, 0, sizeof(struct hidpp_report
));
194 if (response
->report_id
== REPORT_ID_HIDPP_SHORT
&&
195 response
->fap
.feature_index
== HIDPP_ERROR
) {
196 ret
= response
->fap
.params
[1];
197 dbg_hid("__hidpp_send_report got hidpp error %02X\n", ret
);
202 mutex_unlock(&hidpp
->send_mutex
);
207 static int hidpp_send_fap_command_sync(struct hidpp_device
*hidpp
,
208 u8 feat_index
, u8 funcindex_clientid
, u8
*params
, int param_count
,
209 struct hidpp_report
*response
)
211 struct hidpp_report
*message
;
214 if (param_count
> sizeof(message
->fap
.params
))
217 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
220 message
->report_id
= REPORT_ID_HIDPP_LONG
;
221 message
->fap
.feature_index
= feat_index
;
222 message
->fap
.funcindex_clientid
= funcindex_clientid
;
223 memcpy(&message
->fap
.params
, params
, param_count
);
225 ret
= hidpp_send_message_sync(hidpp
, message
, response
);
230 static int hidpp_send_rap_command_sync(struct hidpp_device
*hidpp_dev
,
231 u8 report_id
, u8 sub_id
, u8 reg_address
, u8
*params
, int param_count
,
232 struct hidpp_report
*response
)
234 struct hidpp_report
*message
;
237 if ((report_id
!= REPORT_ID_HIDPP_SHORT
) &&
238 (report_id
!= REPORT_ID_HIDPP_LONG
))
241 if (param_count
> sizeof(message
->rap
.params
))
244 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
247 message
->report_id
= report_id
;
248 message
->rap
.sub_id
= sub_id
;
249 message
->rap
.reg_address
= reg_address
;
250 memcpy(&message
->rap
.params
, params
, param_count
);
252 ret
= hidpp_send_message_sync(hidpp_dev
, message
, response
);
257 static void delayed_work_cb(struct work_struct
*work
)
259 struct hidpp_device
*hidpp
= container_of(work
, struct hidpp_device
,
261 hidpp_connect_event(hidpp
);
264 static inline bool hidpp_match_answer(struct hidpp_report
*question
,
265 struct hidpp_report
*answer
)
267 return (answer
->fap
.feature_index
== question
->fap
.feature_index
) &&
268 (answer
->fap
.funcindex_clientid
== question
->fap
.funcindex_clientid
);
271 static inline bool hidpp_match_error(struct hidpp_report
*question
,
272 struct hidpp_report
*answer
)
274 return (answer
->fap
.feature_index
== HIDPP_ERROR
) &&
275 (answer
->fap
.funcindex_clientid
== question
->fap
.feature_index
) &&
276 (answer
->fap
.params
[0] == question
->fap
.funcindex_clientid
);
279 static inline bool hidpp_report_is_connect_event(struct hidpp_report
*report
)
281 return (report
->report_id
== REPORT_ID_HIDPP_SHORT
) &&
282 (report
->rap
.sub_id
== 0x41);
286 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
288 static void hidpp_prefix_name(char **name
, int name_length
)
290 #define PREFIX_LENGTH 9 /* "Logitech " */
295 if (name_length
> PREFIX_LENGTH
&&
296 strncmp(*name
, "Logitech ", PREFIX_LENGTH
) == 0)
297 /* The prefix has is already in the name */
300 new_length
= PREFIX_LENGTH
+ name_length
;
301 new_name
= kzalloc(new_length
, GFP_KERNEL
);
305 snprintf(new_name
, new_length
, "Logitech %s", *name
);
312 /* -------------------------------------------------------------------------- */
313 /* HIDP++ 1.0 commands */
314 /* -------------------------------------------------------------------------- */
316 #define HIDPP_SET_REGISTER 0x80
317 #define HIDPP_GET_REGISTER 0x81
318 #define HIDPP_SET_LONG_REGISTER 0x82
319 #define HIDPP_GET_LONG_REGISTER 0x83
321 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
322 #define DEVICE_NAME 0x40
324 static char *hidpp_get_unifying_name(struct hidpp_device
*hidpp_dev
)
326 struct hidpp_report response
;
328 /* hid-logitech-dj is in charge of setting the right device index */
329 u8 params
[1] = { DEVICE_NAME
};
333 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
334 REPORT_ID_HIDPP_SHORT
,
335 HIDPP_GET_LONG_REGISTER
,
336 HIDPP_REG_PAIRING_INFORMATION
,
337 params
, 1, &response
);
341 len
= response
.rap
.params
[1];
343 if (2 + len
> sizeof(response
.rap
.params
))
346 name
= kzalloc(len
+ 1, GFP_KERNEL
);
350 memcpy(name
, &response
.rap
.params
[2], len
);
352 /* include the terminating '\0' */
353 hidpp_prefix_name(&name
, len
+ 1);
358 /* -------------------------------------------------------------------------- */
360 /* -------------------------------------------------------------------------- */
362 #define HIDPP_PAGE_ROOT 0x0000
363 #define HIDPP_PAGE_ROOT_IDX 0x00
365 #define CMD_ROOT_GET_FEATURE 0x01
366 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
368 static int hidpp_root_get_feature(struct hidpp_device
*hidpp
, u16 feature
,
369 u8
*feature_index
, u8
*feature_type
)
371 struct hidpp_report response
;
373 u8 params
[2] = { feature
>> 8, feature
& 0x00FF };
375 ret
= hidpp_send_fap_command_sync(hidpp
,
377 CMD_ROOT_GET_FEATURE
,
378 params
, 2, &response
);
382 *feature_index
= response
.fap
.params
[0];
383 *feature_type
= response
.fap
.params
[1];
388 static int hidpp_root_get_protocol_version(struct hidpp_device
*hidpp
)
390 struct hidpp_report response
;
393 ret
= hidpp_send_fap_command_sync(hidpp
,
395 CMD_ROOT_GET_PROTOCOL_VERSION
,
398 if (ret
== HIDPP_ERROR_INVALID_SUBID
) {
399 hidpp
->protocol_major
= 1;
400 hidpp
->protocol_minor
= 0;
404 /* the device might not be connected */
405 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
409 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
416 hidpp
->protocol_major
= response
.fap
.params
[0];
417 hidpp
->protocol_minor
= response
.fap
.params
[1];
422 static bool hidpp_is_connected(struct hidpp_device
*hidpp
)
426 ret
= hidpp_root_get_protocol_version(hidpp
);
428 hid_dbg(hidpp
->hid_dev
, "HID++ %u.%u device connected.\n",
429 hidpp
->protocol_major
, hidpp
->protocol_minor
);
433 /* -------------------------------------------------------------------------- */
434 /* 0x0005: GetDeviceNameType */
435 /* -------------------------------------------------------------------------- */
437 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
439 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
440 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
441 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
443 static int hidpp_devicenametype_get_count(struct hidpp_device
*hidpp
,
444 u8 feature_index
, u8
*nameLength
)
446 struct hidpp_report response
;
449 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
450 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT
, NULL
, 0, &response
);
453 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
460 *nameLength
= response
.fap
.params
[0];
465 static int hidpp_devicenametype_get_device_name(struct hidpp_device
*hidpp
,
466 u8 feature_index
, u8 char_index
, char *device_name
, int len_buf
)
468 struct hidpp_report response
;
472 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
473 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME
, &char_index
, 1,
477 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
484 if (response
.report_id
== REPORT_ID_HIDPP_LONG
)
485 count
= HIDPP_REPORT_LONG_LENGTH
- 4;
487 count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
492 for (i
= 0; i
< count
; i
++)
493 device_name
[i
] = response
.fap
.params
[i
];
498 static char *hidpp_get_device_name(struct hidpp_device
*hidpp
)
507 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_GET_DEVICE_NAME_TYPE
,
508 &feature_index
, &feature_type
);
512 ret
= hidpp_devicenametype_get_count(hidpp
, feature_index
,
517 name
= kzalloc(__name_length
+ 1, GFP_KERNEL
);
521 while (index
< __name_length
) {
522 ret
= hidpp_devicenametype_get_device_name(hidpp
,
523 feature_index
, index
, name
+ index
,
524 __name_length
- index
);
532 /* include the terminating '\0' */
533 hidpp_prefix_name(&name
, __name_length
+ 1);
538 /* -------------------------------------------------------------------------- */
539 /* 0x6100: TouchPadRawXY */
540 /* -------------------------------------------------------------------------- */
542 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
544 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
545 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
547 #define EVENT_TOUCHPAD_RAW_XY 0x00
549 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
550 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
552 struct hidpp_touchpad_raw_info
{
563 struct hidpp_touchpad_raw_xy_finger
{
573 struct hidpp_touchpad_raw_xy
{
575 struct hidpp_touchpad_raw_xy_finger fingers
[2];
582 static int hidpp_touchpad_get_raw_info(struct hidpp_device
*hidpp
,
583 u8 feature_index
, struct hidpp_touchpad_raw_info
*raw_info
)
585 struct hidpp_report response
;
587 u8
*params
= (u8
*)response
.fap
.params
;
589 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
590 CMD_TOUCHPAD_GET_RAW_INFO
, NULL
, 0, &response
);
593 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
600 raw_info
->x_size
= get_unaligned_be16(¶ms
[0]);
601 raw_info
->y_size
= get_unaligned_be16(¶ms
[2]);
602 raw_info
->z_range
= params
[4];
603 raw_info
->area_range
= params
[5];
604 raw_info
->maxcontacts
= params
[7];
605 raw_info
->origin
= params
[8];
606 /* res is given in unit per inch */
607 raw_info
->res
= get_unaligned_be16(¶ms
[13]) * 2 / 51;
612 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device
*hidpp_dev
,
613 u8 feature_index
, bool send_raw_reports
,
614 bool sensor_enhanced_settings
)
616 struct hidpp_report response
;
621 * bit 1 - 16bit Z, no area
622 * bit 2 - enhanced sensitivity
623 * bit 3 - width, height (4 bits each) instead of area
624 * bit 4 - send raw + gestures (degrades smoothness)
625 * remaining bits - reserved
627 u8 params
= send_raw_reports
| (sensor_enhanced_settings
<< 2);
629 return hidpp_send_fap_command_sync(hidpp_dev
, feature_index
,
630 CMD_TOUCHPAD_SET_RAW_REPORT_STATE
, ¶ms
, 1, &response
);
633 static void hidpp_touchpad_touch_event(u8
*data
,
634 struct hidpp_touchpad_raw_xy_finger
*finger
)
636 u8 x_m
= data
[0] << 2;
637 u8 y_m
= data
[2] << 2;
639 finger
->x
= x_m
<< 6 | data
[1];
640 finger
->y
= y_m
<< 6 | data
[3];
642 finger
->contact_type
= data
[0] >> 6;
643 finger
->contact_status
= data
[2] >> 6;
646 finger
->area
= data
[5];
647 finger
->finger_id
= data
[6] >> 4;
650 static void hidpp_touchpad_raw_xy_event(struct hidpp_device
*hidpp_dev
,
651 u8
*data
, struct hidpp_touchpad_raw_xy
*raw_xy
)
653 memset(raw_xy
, 0, sizeof(struct hidpp_touchpad_raw_xy
));
654 raw_xy
->end_of_frame
= data
[8] & 0x01;
655 raw_xy
->spurious_flag
= (data
[8] >> 1) & 0x01;
656 raw_xy
->finger_count
= data
[15] & 0x0f;
657 raw_xy
->button
= (data
[8] >> 2) & 0x01;
659 if (raw_xy
->finger_count
) {
660 hidpp_touchpad_touch_event(&data
[2], &raw_xy
->fingers
[0]);
661 hidpp_touchpad_touch_event(&data
[9], &raw_xy
->fingers
[1]);
665 /* ************************************************************************** */
669 /* ************************************************************************** */
671 /* -------------------------------------------------------------------------- */
672 /* Touchpad HID++ devices */
673 /* -------------------------------------------------------------------------- */
675 #define WTP_MANUAL_RESOLUTION 39
678 struct input_dev
*input
;
682 u8 button_feature_index
;
685 unsigned int resolution
;
688 static int wtp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
689 struct hid_field
*field
, struct hid_usage
*usage
,
690 unsigned long **bit
, int *max
)
692 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
694 if ((hidpp
->quirks
& HIDPP_QUIRK_MULTI_INPUT
) &&
695 (field
->application
== HID_GD_KEYBOARD
))
701 static void wtp_populate_input(struct hidpp_device
*hidpp
,
702 struct input_dev
*input_dev
, bool origin_is_hid_core
)
704 struct wtp_data
*wd
= hidpp
->private_data
;
706 if ((hidpp
->quirks
& HIDPP_QUIRK_MULTI_INPUT
) && origin_is_hid_core
)
707 /* this is the generic hid-input call */
710 __set_bit(EV_ABS
, input_dev
->evbit
);
711 __set_bit(EV_KEY
, input_dev
->evbit
);
712 __clear_bit(EV_REL
, input_dev
->evbit
);
713 __clear_bit(EV_LED
, input_dev
->evbit
);
715 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, wd
->x_size
, 0, 0);
716 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
, wd
->resolution
);
717 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, wd
->y_size
, 0, 0);
718 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
, wd
->resolution
);
720 /* Max pressure is not given by the devices, pick one */
721 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 50, 0, 0);
723 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
725 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
)
726 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
728 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
730 input_mt_init_slots(input_dev
, wd
->maxcontacts
, INPUT_MT_POINTER
|
731 INPUT_MT_DROP_UNUSED
);
733 wd
->input
= input_dev
;
736 static void wtp_touch_event(struct wtp_data
*wd
,
737 struct hidpp_touchpad_raw_xy_finger
*touch_report
)
741 if (!touch_report
->finger_id
|| touch_report
->contact_type
)
745 slot
= input_mt_get_slot_by_key(wd
->input
, touch_report
->finger_id
);
747 input_mt_slot(wd
->input
, slot
);
748 input_mt_report_slot_state(wd
->input
, MT_TOOL_FINGER
,
749 touch_report
->contact_status
);
750 if (touch_report
->contact_status
) {
751 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_X
,
753 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_Y
,
754 wd
->flip_y
? wd
->y_size
- touch_report
->y
:
756 input_event(wd
->input
, EV_ABS
, ABS_MT_PRESSURE
,
761 static void wtp_send_raw_xy_event(struct hidpp_device
*hidpp
,
762 struct hidpp_touchpad_raw_xy
*raw
)
764 struct wtp_data
*wd
= hidpp
->private_data
;
767 for (i
= 0; i
< 2; i
++)
768 wtp_touch_event(wd
, &(raw
->fingers
[i
]));
770 if (raw
->end_of_frame
&&
771 !(hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
))
772 input_event(wd
->input
, EV_KEY
, BTN_LEFT
, raw
->button
);
774 if (raw
->end_of_frame
|| raw
->finger_count
<= 2) {
775 input_mt_sync_frame(wd
->input
);
776 input_sync(wd
->input
);
780 static int wtp_mouse_raw_xy_event(struct hidpp_device
*hidpp
, u8
*data
)
782 struct wtp_data
*wd
= hidpp
->private_data
;
783 u8 c1_area
= ((data
[7] & 0xf) * (data
[7] & 0xf) +
784 (data
[7] >> 4) * (data
[7] >> 4)) / 2;
785 u8 c2_area
= ((data
[13] & 0xf) * (data
[13] & 0xf) +
786 (data
[13] >> 4) * (data
[13] >> 4)) / 2;
787 struct hidpp_touchpad_raw_xy raw
= {
788 .timestamp
= data
[1],
792 .contact_status
= !!data
[7],
793 .x
= get_unaligned_le16(&data
[3]),
794 .y
= get_unaligned_le16(&data
[5]),
797 .finger_id
= data
[2],
800 .contact_status
= !!data
[13],
801 .x
= get_unaligned_le16(&data
[9]),
802 .y
= get_unaligned_le16(&data
[11]),
805 .finger_id
= data
[8],
808 .finger_count
= wd
->maxcontacts
,
810 .end_of_frame
= (data
[0] >> 7) == 0,
811 .button
= data
[0] & 0x01,
814 wtp_send_raw_xy_event(hidpp
, &raw
);
819 static int wtp_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
821 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
822 struct wtp_data
*wd
= hidpp
->private_data
;
823 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
824 struct hidpp_touchpad_raw_xy raw
;
826 if (!wd
|| !wd
->input
)
832 hid_err(hdev
, "Received HID report of bad size (%d)",
836 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
) {
837 input_event(wd
->input
, EV_KEY
, BTN_LEFT
,
839 input_event(wd
->input
, EV_KEY
, BTN_RIGHT
,
841 input_sync(wd
->input
);
846 return wtp_mouse_raw_xy_event(hidpp
, &data
[7]);
848 case REPORT_ID_HIDPP_LONG
:
849 /* size is already checked in hidpp_raw_event. */
850 if ((report
->fap
.feature_index
!= wd
->mt_feature_index
) ||
851 (report
->fap
.funcindex_clientid
!= EVENT_TOUCHPAD_RAW_XY
))
853 hidpp_touchpad_raw_xy_event(hidpp
, data
+ 4, &raw
);
855 wtp_send_raw_xy_event(hidpp
, &raw
);
862 static int wtp_get_config(struct hidpp_device
*hidpp
)
864 struct wtp_data
*wd
= hidpp
->private_data
;
865 struct hidpp_touchpad_raw_info raw_info
= {0};
869 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_TOUCHPAD_RAW_XY
,
870 &wd
->mt_feature_index
, &feature_type
);
872 /* means that the device is not powered up */
875 ret
= hidpp_touchpad_get_raw_info(hidpp
, wd
->mt_feature_index
,
880 wd
->x_size
= raw_info
.x_size
;
881 wd
->y_size
= raw_info
.y_size
;
882 wd
->maxcontacts
= raw_info
.maxcontacts
;
883 wd
->flip_y
= raw_info
.origin
== TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT
;
884 wd
->resolution
= raw_info
.res
;
886 wd
->resolution
= WTP_MANUAL_RESOLUTION
;
891 static int wtp_allocate(struct hid_device
*hdev
, const struct hid_device_id
*id
)
893 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
896 wd
= devm_kzalloc(&hdev
->dev
, sizeof(struct wtp_data
),
901 hidpp
->private_data
= wd
;
906 static void wtp_connect(struct hid_device
*hdev
, bool connected
)
908 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
909 struct wtp_data
*wd
= hidpp
->private_data
;
916 ret
= wtp_get_config(hidpp
);
918 hid_err(hdev
, "Can not get wtp config: %d\n", ret
);
923 hidpp_touchpad_set_raw_report_state(hidpp
, wd
->mt_feature_index
,
927 /* -------------------------------------------------------------------------- */
928 /* Generic HID++ devices */
929 /* -------------------------------------------------------------------------- */
931 static int hidpp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
932 struct hid_field
*field
, struct hid_usage
*usage
,
933 unsigned long **bit
, int *max
)
935 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
937 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
938 return wtp_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
943 static void hidpp_populate_input(struct hidpp_device
*hidpp
,
944 struct input_dev
*input
, bool origin_is_hid_core
)
946 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
947 wtp_populate_input(hidpp
, input
, origin_is_hid_core
);
950 static void hidpp_input_configured(struct hid_device
*hdev
,
951 struct hid_input
*hidinput
)
953 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
954 struct input_dev
*input
= hidinput
->input
;
956 hidpp_populate_input(hidpp
, input
, true);
959 static int hidpp_raw_hidpp_event(struct hidpp_device
*hidpp
, u8
*data
,
962 struct hidpp_report
*question
= hidpp
->send_receive_buf
;
963 struct hidpp_report
*answer
= hidpp
->send_receive_buf
;
964 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
967 * If the mutex is locked then we have a pending answer from a
968 * previoulsly sent command
970 if (unlikely(mutex_is_locked(&hidpp
->send_mutex
))) {
972 * Check for a correct hidpp20 answer or the corresponding
975 if (hidpp_match_answer(question
, report
) ||
976 hidpp_match_error(question
, report
)) {
978 hidpp
->answer_available
= true;
979 wake_up(&hidpp
->wait
);
981 * This was an answer to a command that this driver sent
982 * We return 1 to hid-core to avoid forwarding the
983 * command upstream as it has been treated by the driver
990 if (unlikely(hidpp_report_is_connect_event(report
))) {
991 atomic_set(&hidpp
->connected
,
992 !(report
->rap
.params
[0] & (1 << 6)));
993 if ((hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
) &&
994 (schedule_work(&hidpp
->work
) == 0))
995 dbg_hid("%s: connect event already queued\n", __func__
);
999 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
1000 return wtp_raw_event(hidpp
->hid_dev
, data
, size
);
1005 static int hidpp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1008 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1011 case REPORT_ID_HIDPP_LONG
:
1012 if (size
!= HIDPP_REPORT_LONG_LENGTH
) {
1013 hid_err(hdev
, "received hid++ report of bad size (%d)",
1017 return hidpp_raw_hidpp_event(hidpp
, data
, size
);
1018 case REPORT_ID_HIDPP_SHORT
:
1019 if (size
!= HIDPP_REPORT_SHORT_LENGTH
) {
1020 hid_err(hdev
, "received hid++ report of bad size (%d)",
1024 return hidpp_raw_hidpp_event(hidpp
, data
, size
);
1027 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
1028 return wtp_raw_event(hdev
, data
, size
);
1033 static void hidpp_overwrite_name(struct hid_device
*hdev
, bool use_unifying
)
1035 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1040 * the device is connected through an Unifying receiver, and
1041 * might not be already connected.
1042 * Ask the receiver for its name.
1044 name
= hidpp_get_unifying_name(hidpp
);
1046 name
= hidpp_get_device_name(hidpp
);
1049 hid_err(hdev
, "unable to retrieve the name of the device");
1051 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
1056 static int hidpp_input_open(struct input_dev
*dev
)
1058 struct hid_device
*hid
= input_get_drvdata(dev
);
1060 return hid_hw_open(hid
);
1063 static void hidpp_input_close(struct input_dev
*dev
)
1065 struct hid_device
*hid
= input_get_drvdata(dev
);
1070 static struct input_dev
*hidpp_allocate_input(struct hid_device
*hdev
)
1072 struct input_dev
*input_dev
= devm_input_allocate_device(&hdev
->dev
);
1077 input_set_drvdata(input_dev
, hdev
);
1078 input_dev
->open
= hidpp_input_open
;
1079 input_dev
->close
= hidpp_input_close
;
1081 input_dev
->name
= hdev
->name
;
1082 input_dev
->phys
= hdev
->phys
;
1083 input_dev
->uniq
= hdev
->uniq
;
1084 input_dev
->id
.bustype
= hdev
->bus
;
1085 input_dev
->id
.vendor
= hdev
->vendor
;
1086 input_dev
->id
.product
= hdev
->product
;
1087 input_dev
->id
.version
= hdev
->version
;
1088 input_dev
->dev
.parent
= &hdev
->dev
;
1093 static void hidpp_connect_event(struct hidpp_device
*hidpp
)
1095 struct hid_device
*hdev
= hidpp
->hid_dev
;
1097 bool connected
= atomic_read(&hidpp
->connected
);
1098 struct input_dev
*input
;
1099 char *name
, *devm_name
;
1101 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
1102 wtp_connect(hdev
, connected
);
1104 if (!connected
|| hidpp
->delayed_input
)
1107 if (!hidpp
->protocol_major
) {
1108 ret
= !hidpp_is_connected(hidpp
);
1110 hid_err(hdev
, "Can not get the protocol version.\n");
1115 /* the device is already connected, we can ask for its name and
1117 hid_info(hdev
, "HID++ %u.%u device connected.\n",
1118 hidpp
->protocol_major
, hidpp
->protocol_minor
);
1120 input
= hidpp_allocate_input(hdev
);
1122 hid_err(hdev
, "cannot allocate new input device: %d\n", ret
);
1126 name
= hidpp_get_device_name(hidpp
);
1128 hid_err(hdev
, "unable to retrieve the name of the device");
1130 devm_name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s", name
);
1132 input
->name
= devm_name
;
1136 hidpp_populate_input(hidpp
, input
, false);
1138 ret
= input_register_device(input
);
1140 input_free_device(input
);
1142 hidpp
->delayed_input
= input
;
1145 static int hidpp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1147 struct hidpp_device
*hidpp
;
1150 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
1152 hidpp
= devm_kzalloc(&hdev
->dev
, sizeof(struct hidpp_device
),
1157 hidpp
->hid_dev
= hdev
;
1158 hid_set_drvdata(hdev
, hidpp
);
1160 hidpp
->quirks
= id
->driver_data
;
1162 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
1163 ret
= wtp_allocate(hdev
, id
);
1165 goto wtp_allocate_fail
;
1168 INIT_WORK(&hidpp
->work
, delayed_work_cb
);
1169 mutex_init(&hidpp
->send_mutex
);
1170 init_waitqueue_head(&hidpp
->wait
);
1172 ret
= hid_parse(hdev
);
1174 hid_err(hdev
, "%s:parse failed\n", __func__
);
1175 goto hid_parse_fail
;
1178 /* Allow incoming packets */
1179 hid_device_io_start(hdev
);
1181 connected
= hidpp_is_connected(hidpp
);
1182 if (id
->group
!= HID_GROUP_LOGITECH_DJ_DEVICE
) {
1184 hid_err(hdev
, "Device not connected");
1185 hid_device_io_stop(hdev
);
1186 goto hid_parse_fail
;
1189 hid_info(hdev
, "HID++ %u.%u device connected.\n",
1190 hidpp
->protocol_major
, hidpp
->protocol_minor
);
1193 hidpp_overwrite_name(hdev
, id
->group
== HID_GROUP_LOGITECH_DJ_DEVICE
);
1194 atomic_set(&hidpp
->connected
, connected
);
1196 if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)) {
1197 ret
= wtp_get_config(hidpp
);
1199 goto hid_parse_fail
;
1202 /* Block incoming packets */
1203 hid_device_io_stop(hdev
);
1205 if (hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
)
1206 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
1208 /* Re-enable hidinput for multi-input devices */
1209 if (hidpp
->quirks
& HIDPP_QUIRK_MULTI_INPUT
)
1210 connect_mask
|= HID_CONNECT_HIDINPUT
;
1212 ret
= hid_hw_start(hdev
, connect_mask
);
1214 hid_err(hdev
, "%s:hid_hw_start returned error\n", __func__
);
1215 goto hid_hw_start_fail
;
1218 if (hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
) {
1219 /* Allow incoming packets */
1220 hid_device_io_start(hdev
);
1222 hidpp_connect_event(hidpp
);
1229 cancel_work_sync(&hidpp
->work
);
1230 mutex_destroy(&hidpp
->send_mutex
);
1232 hid_set_drvdata(hdev
, NULL
);
1236 static void hidpp_remove(struct hid_device
*hdev
)
1238 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1240 cancel_work_sync(&hidpp
->work
);
1241 mutex_destroy(&hidpp
->send_mutex
);
1245 static const struct hid_device_id hidpp_devices
[] = {
1246 { /* wireless touchpad */
1247 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1248 USB_VENDOR_ID_LOGITECH
, 0x4011),
1249 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
|
1250 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
},
1251 { /* wireless touchpad T650 */
1252 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1253 USB_VENDOR_ID_LOGITECH
, 0x4101),
1254 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
},
1255 { /* wireless touchpad T651 */
1256 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
,
1257 USB_DEVICE_ID_LOGITECH_T651
),
1258 .driver_data
= HIDPP_QUIRK_CLASS_WTP
},
1259 { /* Keyboard TK820 */
1260 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1261 USB_VENDOR_ID_LOGITECH
, 0x4102),
1262 .driver_data
= HIDPP_QUIRK_DELAYED_INIT
| HIDPP_QUIRK_MULTI_INPUT
|
1263 HIDPP_QUIRK_CLASS_WTP
},
1265 { HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1266 USB_VENDOR_ID_LOGITECH
, HID_ANY_ID
)},
1270 MODULE_DEVICE_TABLE(hid
, hidpp_devices
);
1272 static struct hid_driver hidpp_driver
= {
1273 .name
= "logitech-hidpp-device",
1274 .id_table
= hidpp_devices
,
1275 .probe
= hidpp_probe
,
1276 .remove
= hidpp_remove
,
1277 .raw_event
= hidpp_raw_event
,
1278 .input_configured
= hidpp_input_configured
,
1279 .input_mapping
= hidpp_input_mapping
,
1282 module_hid_driver(hidpp_driver
);