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 char *name
; /* will never be NULL and should not be freed */
93 wait_queue_head_t wait
;
94 bool answer_available
;
100 struct work_struct work
;
101 struct kfifo delayed_work_fifo
;
103 struct input_dev
*delayed_input
;
105 unsigned long quirks
;
109 /* HID++ 1.0 error codes */
110 #define HIDPP_ERROR 0x8f
111 #define HIDPP_ERROR_SUCCESS 0x00
112 #define HIDPP_ERROR_INVALID_SUBID 0x01
113 #define HIDPP_ERROR_INVALID_ADRESS 0x02
114 #define HIDPP_ERROR_INVALID_VALUE 0x03
115 #define HIDPP_ERROR_CONNECT_FAIL 0x04
116 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
117 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
118 #define HIDPP_ERROR_BUSY 0x07
119 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
120 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
121 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
122 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
123 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
124 /* HID++ 2.0 error codes */
125 #define HIDPP20_ERROR 0xff
127 static void hidpp_connect_event(struct hidpp_device
*hidpp_dev
);
129 static int __hidpp_send_report(struct hid_device
*hdev
,
130 struct hidpp_report
*hidpp_report
)
132 int fields_count
, ret
;
134 switch (hidpp_report
->report_id
) {
135 case REPORT_ID_HIDPP_SHORT
:
136 fields_count
= HIDPP_REPORT_SHORT_LENGTH
;
138 case REPORT_ID_HIDPP_LONG
:
139 fields_count
= HIDPP_REPORT_LONG_LENGTH
;
146 * set the device_index as the receiver, it will be overwritten by
147 * hid_hw_request if needed
149 hidpp_report
->device_index
= 0xff;
151 ret
= hid_hw_raw_request(hdev
, hidpp_report
->report_id
,
152 (u8
*)hidpp_report
, fields_count
, HID_OUTPUT_REPORT
,
155 return ret
== fields_count
? 0 : -1;
159 * hidpp_send_message_sync() returns 0 in case of success, and something else
160 * in case of a failure.
161 * - If ' something else' is positive, that means that an error has been raised
162 * by the protocol itself.
163 * - If ' something else' is negative, that means that we had a classic error
164 * (-ENOMEM, -EPIPE, etc...)
166 static int hidpp_send_message_sync(struct hidpp_device
*hidpp
,
167 struct hidpp_report
*message
,
168 struct hidpp_report
*response
)
172 mutex_lock(&hidpp
->send_mutex
);
174 hidpp
->send_receive_buf
= response
;
175 hidpp
->answer_available
= false;
178 * So that we can later validate the answer when it arrives
181 *response
= *message
;
183 ret
= __hidpp_send_report(hidpp
->hid_dev
, message
);
186 dbg_hid("__hidpp_send_report returned err: %d\n", ret
);
187 memset(response
, 0, sizeof(struct hidpp_report
));
191 if (!wait_event_timeout(hidpp
->wait
, hidpp
->answer_available
,
193 dbg_hid("%s:timeout waiting for response\n", __func__
);
194 memset(response
, 0, sizeof(struct hidpp_report
));
198 if (response
->report_id
== REPORT_ID_HIDPP_SHORT
&&
199 response
->rap
.sub_id
== HIDPP_ERROR
) {
200 ret
= response
->rap
.params
[1];
201 dbg_hid("%s:got hidpp error %02X\n", __func__
, ret
);
205 if (response
->report_id
== REPORT_ID_HIDPP_LONG
&&
206 response
->fap
.feature_index
== HIDPP20_ERROR
) {
207 ret
= response
->fap
.params
[1];
208 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__
, ret
);
213 mutex_unlock(&hidpp
->send_mutex
);
218 static int hidpp_send_fap_command_sync(struct hidpp_device
*hidpp
,
219 u8 feat_index
, u8 funcindex_clientid
, u8
*params
, int param_count
,
220 struct hidpp_report
*response
)
222 struct hidpp_report
*message
;
225 if (param_count
> sizeof(message
->fap
.params
))
228 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
231 message
->report_id
= REPORT_ID_HIDPP_LONG
;
232 message
->fap
.feature_index
= feat_index
;
233 message
->fap
.funcindex_clientid
= funcindex_clientid
;
234 memcpy(&message
->fap
.params
, params
, param_count
);
236 ret
= hidpp_send_message_sync(hidpp
, message
, response
);
241 static int hidpp_send_rap_command_sync(struct hidpp_device
*hidpp_dev
,
242 u8 report_id
, u8 sub_id
, u8 reg_address
, u8
*params
, int param_count
,
243 struct hidpp_report
*response
)
245 struct hidpp_report
*message
;
248 if ((report_id
!= REPORT_ID_HIDPP_SHORT
) &&
249 (report_id
!= REPORT_ID_HIDPP_LONG
))
252 if (param_count
> sizeof(message
->rap
.params
))
255 message
= kzalloc(sizeof(struct hidpp_report
), GFP_KERNEL
);
258 message
->report_id
= report_id
;
259 message
->rap
.sub_id
= sub_id
;
260 message
->rap
.reg_address
= reg_address
;
261 memcpy(&message
->rap
.params
, params
, param_count
);
263 ret
= hidpp_send_message_sync(hidpp_dev
, message
, response
);
268 static void delayed_work_cb(struct work_struct
*work
)
270 struct hidpp_device
*hidpp
= container_of(work
, struct hidpp_device
,
272 hidpp_connect_event(hidpp
);
275 static inline bool hidpp_match_answer(struct hidpp_report
*question
,
276 struct hidpp_report
*answer
)
278 return (answer
->fap
.feature_index
== question
->fap
.feature_index
) &&
279 (answer
->fap
.funcindex_clientid
== question
->fap
.funcindex_clientid
);
282 static inline bool hidpp_match_error(struct hidpp_report
*question
,
283 struct hidpp_report
*answer
)
285 return ((answer
->rap
.sub_id
== HIDPP_ERROR
) ||
286 (answer
->fap
.feature_index
== HIDPP20_ERROR
)) &&
287 (answer
->fap
.funcindex_clientid
== question
->fap
.feature_index
) &&
288 (answer
->fap
.params
[0] == question
->fap
.funcindex_clientid
);
291 static inline bool hidpp_report_is_connect_event(struct hidpp_report
*report
)
293 return (report
->report_id
== REPORT_ID_HIDPP_SHORT
) &&
294 (report
->rap
.sub_id
== 0x41);
298 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
300 static void hidpp_prefix_name(char **name
, int name_length
)
302 #define PREFIX_LENGTH 9 /* "Logitech " */
307 if (name_length
> PREFIX_LENGTH
&&
308 strncmp(*name
, "Logitech ", PREFIX_LENGTH
) == 0)
309 /* The prefix has is already in the name */
312 new_length
= PREFIX_LENGTH
+ name_length
;
313 new_name
= kzalloc(new_length
, GFP_KERNEL
);
317 snprintf(new_name
, new_length
, "Logitech %s", *name
);
324 /* -------------------------------------------------------------------------- */
325 /* HIDP++ 1.0 commands */
326 /* -------------------------------------------------------------------------- */
328 #define HIDPP_SET_REGISTER 0x80
329 #define HIDPP_GET_REGISTER 0x81
330 #define HIDPP_SET_LONG_REGISTER 0x82
331 #define HIDPP_GET_LONG_REGISTER 0x83
333 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
334 #define DEVICE_NAME 0x40
336 static char *hidpp_get_unifying_name(struct hidpp_device
*hidpp_dev
)
338 struct hidpp_report response
;
340 /* hid-logitech-dj is in charge of setting the right device index */
341 u8 params
[1] = { DEVICE_NAME
};
345 ret
= hidpp_send_rap_command_sync(hidpp_dev
,
346 REPORT_ID_HIDPP_SHORT
,
347 HIDPP_GET_LONG_REGISTER
,
348 HIDPP_REG_PAIRING_INFORMATION
,
349 params
, 1, &response
);
353 len
= response
.rap
.params
[1];
355 if (2 + len
> sizeof(response
.rap
.params
))
358 name
= kzalloc(len
+ 1, GFP_KERNEL
);
362 memcpy(name
, &response
.rap
.params
[2], len
);
364 /* include the terminating '\0' */
365 hidpp_prefix_name(&name
, len
+ 1);
370 /* -------------------------------------------------------------------------- */
372 /* -------------------------------------------------------------------------- */
374 #define HIDPP_PAGE_ROOT 0x0000
375 #define HIDPP_PAGE_ROOT_IDX 0x00
377 #define CMD_ROOT_GET_FEATURE 0x01
378 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
380 static int hidpp_root_get_feature(struct hidpp_device
*hidpp
, u16 feature
,
381 u8
*feature_index
, u8
*feature_type
)
383 struct hidpp_report response
;
385 u8 params
[2] = { feature
>> 8, feature
& 0x00FF };
387 ret
= hidpp_send_fap_command_sync(hidpp
,
389 CMD_ROOT_GET_FEATURE
,
390 params
, 2, &response
);
394 *feature_index
= response
.fap
.params
[0];
395 *feature_type
= response
.fap
.params
[1];
400 static int hidpp_root_get_protocol_version(struct hidpp_device
*hidpp
)
402 struct hidpp_report response
;
405 ret
= hidpp_send_fap_command_sync(hidpp
,
407 CMD_ROOT_GET_PROTOCOL_VERSION
,
410 if (ret
== HIDPP_ERROR_INVALID_SUBID
) {
411 hidpp
->protocol_major
= 1;
412 hidpp
->protocol_minor
= 0;
416 /* the device might not be connected */
417 if (ret
== HIDPP_ERROR_RESOURCE_ERROR
)
421 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
428 hidpp
->protocol_major
= response
.fap
.params
[0];
429 hidpp
->protocol_minor
= response
.fap
.params
[1];
434 static bool hidpp_is_connected(struct hidpp_device
*hidpp
)
438 ret
= hidpp_root_get_protocol_version(hidpp
);
440 hid_dbg(hidpp
->hid_dev
, "HID++ %u.%u device connected.\n",
441 hidpp
->protocol_major
, hidpp
->protocol_minor
);
445 /* -------------------------------------------------------------------------- */
446 /* 0x0005: GetDeviceNameType */
447 /* -------------------------------------------------------------------------- */
449 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
451 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
452 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
453 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
455 static int hidpp_devicenametype_get_count(struct hidpp_device
*hidpp
,
456 u8 feature_index
, u8
*nameLength
)
458 struct hidpp_report response
;
461 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
462 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT
, NULL
, 0, &response
);
465 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
472 *nameLength
= response
.fap
.params
[0];
477 static int hidpp_devicenametype_get_device_name(struct hidpp_device
*hidpp
,
478 u8 feature_index
, u8 char_index
, char *device_name
, int len_buf
)
480 struct hidpp_report response
;
484 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
485 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME
, &char_index
, 1,
489 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
496 if (response
.report_id
== REPORT_ID_HIDPP_LONG
)
497 count
= HIDPP_REPORT_LONG_LENGTH
- 4;
499 count
= HIDPP_REPORT_SHORT_LENGTH
- 4;
504 for (i
= 0; i
< count
; i
++)
505 device_name
[i
] = response
.fap
.params
[i
];
510 static char *hidpp_get_device_name(struct hidpp_device
*hidpp
)
519 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_GET_DEVICE_NAME_TYPE
,
520 &feature_index
, &feature_type
);
524 ret
= hidpp_devicenametype_get_count(hidpp
, feature_index
,
529 name
= kzalloc(__name_length
+ 1, GFP_KERNEL
);
533 while (index
< __name_length
) {
534 ret
= hidpp_devicenametype_get_device_name(hidpp
,
535 feature_index
, index
, name
+ index
,
536 __name_length
- index
);
544 /* include the terminating '\0' */
545 hidpp_prefix_name(&name
, __name_length
+ 1);
550 /* -------------------------------------------------------------------------- */
551 /* 0x6100: TouchPadRawXY */
552 /* -------------------------------------------------------------------------- */
554 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
556 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
557 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
559 #define EVENT_TOUCHPAD_RAW_XY 0x00
561 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
562 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
564 struct hidpp_touchpad_raw_info
{
575 struct hidpp_touchpad_raw_xy_finger
{
585 struct hidpp_touchpad_raw_xy
{
587 struct hidpp_touchpad_raw_xy_finger fingers
[2];
594 static int hidpp_touchpad_get_raw_info(struct hidpp_device
*hidpp
,
595 u8 feature_index
, struct hidpp_touchpad_raw_info
*raw_info
)
597 struct hidpp_report response
;
599 u8
*params
= (u8
*)response
.fap
.params
;
601 ret
= hidpp_send_fap_command_sync(hidpp
, feature_index
,
602 CMD_TOUCHPAD_GET_RAW_INFO
, NULL
, 0, &response
);
605 hid_err(hidpp
->hid_dev
, "%s: received protocol error 0x%02x\n",
612 raw_info
->x_size
= get_unaligned_be16(¶ms
[0]);
613 raw_info
->y_size
= get_unaligned_be16(¶ms
[2]);
614 raw_info
->z_range
= params
[4];
615 raw_info
->area_range
= params
[5];
616 raw_info
->maxcontacts
= params
[7];
617 raw_info
->origin
= params
[8];
618 /* res is given in unit per inch */
619 raw_info
->res
= get_unaligned_be16(¶ms
[13]) * 2 / 51;
624 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device
*hidpp_dev
,
625 u8 feature_index
, bool send_raw_reports
,
626 bool sensor_enhanced_settings
)
628 struct hidpp_report response
;
633 * bit 1 - 16bit Z, no area
634 * bit 2 - enhanced sensitivity
635 * bit 3 - width, height (4 bits each) instead of area
636 * bit 4 - send raw + gestures (degrades smoothness)
637 * remaining bits - reserved
639 u8 params
= send_raw_reports
| (sensor_enhanced_settings
<< 2);
641 return hidpp_send_fap_command_sync(hidpp_dev
, feature_index
,
642 CMD_TOUCHPAD_SET_RAW_REPORT_STATE
, ¶ms
, 1, &response
);
645 static void hidpp_touchpad_touch_event(u8
*data
,
646 struct hidpp_touchpad_raw_xy_finger
*finger
)
648 u8 x_m
= data
[0] << 2;
649 u8 y_m
= data
[2] << 2;
651 finger
->x
= x_m
<< 6 | data
[1];
652 finger
->y
= y_m
<< 6 | data
[3];
654 finger
->contact_type
= data
[0] >> 6;
655 finger
->contact_status
= data
[2] >> 6;
658 finger
->area
= data
[5];
659 finger
->finger_id
= data
[6] >> 4;
662 static void hidpp_touchpad_raw_xy_event(struct hidpp_device
*hidpp_dev
,
663 u8
*data
, struct hidpp_touchpad_raw_xy
*raw_xy
)
665 memset(raw_xy
, 0, sizeof(struct hidpp_touchpad_raw_xy
));
666 raw_xy
->end_of_frame
= data
[8] & 0x01;
667 raw_xy
->spurious_flag
= (data
[8] >> 1) & 0x01;
668 raw_xy
->finger_count
= data
[15] & 0x0f;
669 raw_xy
->button
= (data
[8] >> 2) & 0x01;
671 if (raw_xy
->finger_count
) {
672 hidpp_touchpad_touch_event(&data
[2], &raw_xy
->fingers
[0]);
673 hidpp_touchpad_touch_event(&data
[9], &raw_xy
->fingers
[1]);
677 /* ************************************************************************** */
681 /* ************************************************************************** */
683 /* -------------------------------------------------------------------------- */
684 /* Touchpad HID++ devices */
685 /* -------------------------------------------------------------------------- */
687 #define WTP_MANUAL_RESOLUTION 39
690 struct input_dev
*input
;
694 u8 button_feature_index
;
697 unsigned int resolution
;
700 static int wtp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
701 struct hid_field
*field
, struct hid_usage
*usage
,
702 unsigned long **bit
, int *max
)
704 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
706 if ((hidpp
->quirks
& HIDPP_QUIRK_MULTI_INPUT
) &&
707 (field
->application
== HID_GD_KEYBOARD
))
713 static void wtp_populate_input(struct hidpp_device
*hidpp
,
714 struct input_dev
*input_dev
, bool origin_is_hid_core
)
716 struct wtp_data
*wd
= hidpp
->private_data
;
718 if ((hidpp
->quirks
& HIDPP_QUIRK_MULTI_INPUT
) && origin_is_hid_core
)
719 /* this is the generic hid-input call */
722 __set_bit(EV_ABS
, input_dev
->evbit
);
723 __set_bit(EV_KEY
, input_dev
->evbit
);
724 __clear_bit(EV_REL
, input_dev
->evbit
);
725 __clear_bit(EV_LED
, input_dev
->evbit
);
727 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
, 0, wd
->x_size
, 0, 0);
728 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
, wd
->resolution
);
729 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
, 0, wd
->y_size
, 0, 0);
730 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
, wd
->resolution
);
732 /* Max pressure is not given by the devices, pick one */
733 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 50, 0, 0);
735 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
737 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
)
738 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
740 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
742 input_mt_init_slots(input_dev
, wd
->maxcontacts
, INPUT_MT_POINTER
|
743 INPUT_MT_DROP_UNUSED
);
745 wd
->input
= input_dev
;
748 static void wtp_touch_event(struct wtp_data
*wd
,
749 struct hidpp_touchpad_raw_xy_finger
*touch_report
)
753 if (!touch_report
->finger_id
|| touch_report
->contact_type
)
757 slot
= input_mt_get_slot_by_key(wd
->input
, touch_report
->finger_id
);
759 input_mt_slot(wd
->input
, slot
);
760 input_mt_report_slot_state(wd
->input
, MT_TOOL_FINGER
,
761 touch_report
->contact_status
);
762 if (touch_report
->contact_status
) {
763 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_X
,
765 input_event(wd
->input
, EV_ABS
, ABS_MT_POSITION_Y
,
766 wd
->flip_y
? wd
->y_size
- touch_report
->y
:
768 input_event(wd
->input
, EV_ABS
, ABS_MT_PRESSURE
,
773 static void wtp_send_raw_xy_event(struct hidpp_device
*hidpp
,
774 struct hidpp_touchpad_raw_xy
*raw
)
776 struct wtp_data
*wd
= hidpp
->private_data
;
779 for (i
= 0; i
< 2; i
++)
780 wtp_touch_event(wd
, &(raw
->fingers
[i
]));
782 if (raw
->end_of_frame
&&
783 !(hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
))
784 input_event(wd
->input
, EV_KEY
, BTN_LEFT
, raw
->button
);
786 if (raw
->end_of_frame
|| raw
->finger_count
<= 2) {
787 input_mt_sync_frame(wd
->input
);
788 input_sync(wd
->input
);
792 static int wtp_mouse_raw_xy_event(struct hidpp_device
*hidpp
, u8
*data
)
794 struct wtp_data
*wd
= hidpp
->private_data
;
795 u8 c1_area
= ((data
[7] & 0xf) * (data
[7] & 0xf) +
796 (data
[7] >> 4) * (data
[7] >> 4)) / 2;
797 u8 c2_area
= ((data
[13] & 0xf) * (data
[13] & 0xf) +
798 (data
[13] >> 4) * (data
[13] >> 4)) / 2;
799 struct hidpp_touchpad_raw_xy raw
= {
800 .timestamp
= data
[1],
804 .contact_status
= !!data
[7],
805 .x
= get_unaligned_le16(&data
[3]),
806 .y
= get_unaligned_le16(&data
[5]),
809 .finger_id
= data
[2],
812 .contact_status
= !!data
[13],
813 .x
= get_unaligned_le16(&data
[9]),
814 .y
= get_unaligned_le16(&data
[11]),
817 .finger_id
= data
[8],
820 .finger_count
= wd
->maxcontacts
,
822 .end_of_frame
= (data
[0] >> 7) == 0,
823 .button
= data
[0] & 0x01,
826 wtp_send_raw_xy_event(hidpp
, &raw
);
831 static int wtp_raw_event(struct hid_device
*hdev
, u8
*data
, int size
)
833 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
834 struct wtp_data
*wd
= hidpp
->private_data
;
835 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
836 struct hidpp_touchpad_raw_xy raw
;
838 if (!wd
|| !wd
->input
)
844 hid_err(hdev
, "Received HID report of bad size (%d)",
848 if (hidpp
->quirks
& HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
) {
849 input_event(wd
->input
, EV_KEY
, BTN_LEFT
,
851 input_event(wd
->input
, EV_KEY
, BTN_RIGHT
,
853 input_sync(wd
->input
);
858 return wtp_mouse_raw_xy_event(hidpp
, &data
[7]);
860 case REPORT_ID_HIDPP_LONG
:
861 /* size is already checked in hidpp_raw_event. */
862 if ((report
->fap
.feature_index
!= wd
->mt_feature_index
) ||
863 (report
->fap
.funcindex_clientid
!= EVENT_TOUCHPAD_RAW_XY
))
865 hidpp_touchpad_raw_xy_event(hidpp
, data
+ 4, &raw
);
867 wtp_send_raw_xy_event(hidpp
, &raw
);
874 static int wtp_get_config(struct hidpp_device
*hidpp
)
876 struct wtp_data
*wd
= hidpp
->private_data
;
877 struct hidpp_touchpad_raw_info raw_info
= {0};
881 ret
= hidpp_root_get_feature(hidpp
, HIDPP_PAGE_TOUCHPAD_RAW_XY
,
882 &wd
->mt_feature_index
, &feature_type
);
884 /* means that the device is not powered up */
887 ret
= hidpp_touchpad_get_raw_info(hidpp
, wd
->mt_feature_index
,
892 wd
->x_size
= raw_info
.x_size
;
893 wd
->y_size
= raw_info
.y_size
;
894 wd
->maxcontacts
= raw_info
.maxcontacts
;
895 wd
->flip_y
= raw_info
.origin
== TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT
;
896 wd
->resolution
= raw_info
.res
;
898 wd
->resolution
= WTP_MANUAL_RESOLUTION
;
903 static int wtp_allocate(struct hid_device
*hdev
, const struct hid_device_id
*id
)
905 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
908 wd
= devm_kzalloc(&hdev
->dev
, sizeof(struct wtp_data
),
913 hidpp
->private_data
= wd
;
918 static int wtp_connect(struct hid_device
*hdev
, bool connected
)
920 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
921 struct wtp_data
*wd
= hidpp
->private_data
;
928 ret
= wtp_get_config(hidpp
);
930 hid_err(hdev
, "Can not get wtp config: %d\n", ret
);
935 return hidpp_touchpad_set_raw_report_state(hidpp
, wd
->mt_feature_index
,
939 /* -------------------------------------------------------------------------- */
940 /* Generic HID++ devices */
941 /* -------------------------------------------------------------------------- */
943 static int hidpp_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
944 struct hid_field
*field
, struct hid_usage
*usage
,
945 unsigned long **bit
, int *max
)
947 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
949 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
950 return wtp_input_mapping(hdev
, hi
, field
, usage
, bit
, max
);
955 static void hidpp_populate_input(struct hidpp_device
*hidpp
,
956 struct input_dev
*input
, bool origin_is_hid_core
)
958 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
959 wtp_populate_input(hidpp
, input
, origin_is_hid_core
);
962 static void hidpp_input_configured(struct hid_device
*hdev
,
963 struct hid_input
*hidinput
)
965 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
966 struct input_dev
*input
= hidinput
->input
;
968 hidpp_populate_input(hidpp
, input
, true);
971 static int hidpp_raw_hidpp_event(struct hidpp_device
*hidpp
, u8
*data
,
974 struct hidpp_report
*question
= hidpp
->send_receive_buf
;
975 struct hidpp_report
*answer
= hidpp
->send_receive_buf
;
976 struct hidpp_report
*report
= (struct hidpp_report
*)data
;
979 * If the mutex is locked then we have a pending answer from a
980 * previously sent command.
982 if (unlikely(mutex_is_locked(&hidpp
->send_mutex
))) {
984 * Check for a correct hidpp20 answer or the corresponding
987 if (hidpp_match_answer(question
, report
) ||
988 hidpp_match_error(question
, report
)) {
990 hidpp
->answer_available
= true;
991 wake_up(&hidpp
->wait
);
993 * This was an answer to a command that this driver sent
994 * We return 1 to hid-core to avoid forwarding the
995 * command upstream as it has been treated by the driver
1002 if (unlikely(hidpp_report_is_connect_event(report
))) {
1003 atomic_set(&hidpp
->connected
,
1004 !(report
->rap
.params
[0] & (1 << 6)));
1005 if ((hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
) &&
1006 (schedule_work(&hidpp
->work
) == 0))
1007 dbg_hid("%s: connect event already queued\n", __func__
);
1014 static int hidpp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1017 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1020 /* Generic HID++ processing. */
1022 case REPORT_ID_HIDPP_LONG
:
1023 if (size
!= HIDPP_REPORT_LONG_LENGTH
) {
1024 hid_err(hdev
, "received hid++ report of bad size (%d)",
1028 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
1030 case REPORT_ID_HIDPP_SHORT
:
1031 if (size
!= HIDPP_REPORT_SHORT_LENGTH
) {
1032 hid_err(hdev
, "received hid++ report of bad size (%d)",
1036 ret
= hidpp_raw_hidpp_event(hidpp
, data
, size
);
1040 /* If no report is available for further processing, skip calling
1041 * raw_event of subclasses. */
1045 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)
1046 return wtp_raw_event(hdev
, data
, size
);
1051 static void hidpp_overwrite_name(struct hid_device
*hdev
, bool use_unifying
)
1053 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1058 * the device is connected through an Unifying receiver, and
1059 * might not be already connected.
1060 * Ask the receiver for its name.
1062 name
= hidpp_get_unifying_name(hidpp
);
1064 name
= hidpp_get_device_name(hidpp
);
1067 hid_err(hdev
, "unable to retrieve the name of the device");
1069 snprintf(hdev
->name
, sizeof(hdev
->name
), "%s", name
);
1074 static int hidpp_input_open(struct input_dev
*dev
)
1076 struct hid_device
*hid
= input_get_drvdata(dev
);
1078 return hid_hw_open(hid
);
1081 static void hidpp_input_close(struct input_dev
*dev
)
1083 struct hid_device
*hid
= input_get_drvdata(dev
);
1088 static struct input_dev
*hidpp_allocate_input(struct hid_device
*hdev
)
1090 struct input_dev
*input_dev
= devm_input_allocate_device(&hdev
->dev
);
1091 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1096 input_set_drvdata(input_dev
, hdev
);
1097 input_dev
->open
= hidpp_input_open
;
1098 input_dev
->close
= hidpp_input_close
;
1100 input_dev
->name
= hidpp
->name
;
1101 input_dev
->phys
= hdev
->phys
;
1102 input_dev
->uniq
= hdev
->uniq
;
1103 input_dev
->id
.bustype
= hdev
->bus
;
1104 input_dev
->id
.vendor
= hdev
->vendor
;
1105 input_dev
->id
.product
= hdev
->product
;
1106 input_dev
->id
.version
= hdev
->version
;
1107 input_dev
->dev
.parent
= &hdev
->dev
;
1112 static void hidpp_connect_event(struct hidpp_device
*hidpp
)
1114 struct hid_device
*hdev
= hidpp
->hid_dev
;
1116 bool connected
= atomic_read(&hidpp
->connected
);
1117 struct input_dev
*input
;
1118 char *name
, *devm_name
;
1120 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
1121 ret
= wtp_connect(hdev
, connected
);
1126 if (!connected
|| hidpp
->delayed_input
)
1129 if (!hidpp
->protocol_major
) {
1130 ret
= !hidpp_is_connected(hidpp
);
1132 hid_err(hdev
, "Can not get the protocol version.\n");
1137 /* the device is already connected, we can ask for its name and
1139 hid_info(hdev
, "HID++ %u.%u device connected.\n",
1140 hidpp
->protocol_major
, hidpp
->protocol_minor
);
1142 if (!hidpp
->name
|| hidpp
->name
== hdev
->name
) {
1143 name
= hidpp_get_device_name(hidpp
);
1146 "unable to retrieve the name of the device");
1150 devm_name
= devm_kasprintf(&hdev
->dev
, GFP_KERNEL
, "%s", name
);
1155 hidpp
->name
= devm_name
;
1158 input
= hidpp_allocate_input(hdev
);
1160 hid_err(hdev
, "cannot allocate new input device: %d\n", ret
);
1164 hidpp_populate_input(hidpp
, input
, false);
1166 ret
= input_register_device(input
);
1168 input_free_device(input
);
1170 hidpp
->delayed_input
= input
;
1173 static int hidpp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1175 struct hidpp_device
*hidpp
;
1178 unsigned int connect_mask
= HID_CONNECT_DEFAULT
;
1180 hidpp
= devm_kzalloc(&hdev
->dev
, sizeof(struct hidpp_device
),
1185 hidpp
->hid_dev
= hdev
;
1186 hidpp
->name
= hdev
->name
;
1187 hid_set_drvdata(hdev
, hidpp
);
1189 hidpp
->quirks
= id
->driver_data
;
1191 if (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
) {
1192 ret
= wtp_allocate(hdev
, id
);
1194 goto wtp_allocate_fail
;
1197 INIT_WORK(&hidpp
->work
, delayed_work_cb
);
1198 mutex_init(&hidpp
->send_mutex
);
1199 init_waitqueue_head(&hidpp
->wait
);
1201 ret
= hid_parse(hdev
);
1203 hid_err(hdev
, "%s:parse failed\n", __func__
);
1204 goto hid_parse_fail
;
1207 /* Allow incoming packets */
1208 hid_device_io_start(hdev
);
1210 connected
= hidpp_is_connected(hidpp
);
1211 if (id
->group
!= HID_GROUP_LOGITECH_DJ_DEVICE
) {
1213 hid_err(hdev
, "Device not connected");
1214 hid_device_io_stop(hdev
);
1215 goto hid_parse_fail
;
1218 hid_info(hdev
, "HID++ %u.%u device connected.\n",
1219 hidpp
->protocol_major
, hidpp
->protocol_minor
);
1222 hidpp_overwrite_name(hdev
, id
->group
== HID_GROUP_LOGITECH_DJ_DEVICE
);
1223 atomic_set(&hidpp
->connected
, connected
);
1225 if (connected
&& (hidpp
->quirks
& HIDPP_QUIRK_CLASS_WTP
)) {
1226 ret
= wtp_get_config(hidpp
);
1228 goto hid_parse_fail
;
1231 /* Block incoming packets */
1232 hid_device_io_stop(hdev
);
1234 if (hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
)
1235 connect_mask
&= ~HID_CONNECT_HIDINPUT
;
1237 /* Re-enable hidinput for multi-input devices */
1238 if (hidpp
->quirks
& HIDPP_QUIRK_MULTI_INPUT
)
1239 connect_mask
|= HID_CONNECT_HIDINPUT
;
1241 ret
= hid_hw_start(hdev
, connect_mask
);
1243 hid_err(hdev
, "%s:hid_hw_start returned error\n", __func__
);
1244 goto hid_hw_start_fail
;
1247 if (hidpp
->quirks
& HIDPP_QUIRK_DELAYED_INIT
) {
1248 /* Allow incoming packets */
1249 hid_device_io_start(hdev
);
1251 hidpp_connect_event(hidpp
);
1258 cancel_work_sync(&hidpp
->work
);
1259 mutex_destroy(&hidpp
->send_mutex
);
1261 hid_set_drvdata(hdev
, NULL
);
1265 static void hidpp_remove(struct hid_device
*hdev
)
1267 struct hidpp_device
*hidpp
= hid_get_drvdata(hdev
);
1269 cancel_work_sync(&hidpp
->work
);
1270 mutex_destroy(&hidpp
->send_mutex
);
1274 static const struct hid_device_id hidpp_devices
[] = {
1275 { /* wireless touchpad */
1276 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1277 USB_VENDOR_ID_LOGITECH
, 0x4011),
1278 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
|
1279 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS
},
1280 { /* wireless touchpad T650 */
1281 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1282 USB_VENDOR_ID_LOGITECH
, 0x4101),
1283 .driver_data
= HIDPP_QUIRK_CLASS_WTP
| HIDPP_QUIRK_DELAYED_INIT
},
1284 { /* wireless touchpad T651 */
1285 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH
,
1286 USB_DEVICE_ID_LOGITECH_T651
),
1287 .driver_data
= HIDPP_QUIRK_CLASS_WTP
},
1288 { /* Keyboard TK820 */
1289 HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1290 USB_VENDOR_ID_LOGITECH
, 0x4102),
1291 .driver_data
= HIDPP_QUIRK_DELAYED_INIT
| HIDPP_QUIRK_MULTI_INPUT
|
1292 HIDPP_QUIRK_CLASS_WTP
},
1294 { HID_DEVICE(BUS_USB
, HID_GROUP_LOGITECH_DJ_DEVICE
,
1295 USB_VENDOR_ID_LOGITECH
, HID_ANY_ID
)},
1299 MODULE_DEVICE_TABLE(hid
, hidpp_devices
);
1301 static struct hid_driver hidpp_driver
= {
1302 .name
= "logitech-hidpp-device",
1303 .id_table
= hidpp_devices
,
1304 .probe
= hidpp_probe
,
1305 .remove
= hidpp_remove
,
1306 .raw_event
= hidpp_raw_event
,
1307 .input_configured
= hidpp_input_configured
,
1308 .input_mapping
= hidpp_input_mapping
,
1311 module_hid_driver(hidpp_driver
);