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_CONNECT_EVENTS BIT(21)
47 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
48 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
50 #define HIDPP_QUIRK_DELAYED_INIT (HIDPP_QUIRK_NO_HIDINPUT | \
51 HIDPP_QUIRK_CONNECT_EVENTS)
54 * There are two hidpp protocols in use, the first version hidpp10 is known
55 * as register access protocol or RAP, the second version hidpp20 is known as
56 * feature access protocol or FAP
58 * Most older devices (including the Unifying usb receiver) use the RAP protocol
59 * where as most newer devices use the FAP protocol. Both protocols are
60 * compatible with the underlying transport, which could be usb, Unifiying, or
61 * bluetooth. The message lengths are defined by the hid vendor specific report
62 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
63 * the HIDPP_LONG report type (total message length 20 bytes)
65 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
66 * messages. The Unifying receiver itself responds to RAP messages (device index
67 * is 0xFF for the receiver), and all messages (short or long) with a device
68 * index between 1 and 6 are passed untouched to the corresponding paired
71 * The paired device can be RAP or FAP, it will receive the message untouched
72 * from the Unifiying receiver.
77 u8 funcindex_clientid;
78 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
84 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
93 u8 rawbytes[sizeof(struct fap)];
98 struct hid_device *hid_dev;
99 struct mutex send_mutex;
100 void *send_receive_buf;
101 char *name; /* will never be NULL and should not be freed */
102 wait_queue_head_t wait;
103 bool answer_available;
109 struct work_struct work;
110 struct kfifo delayed_work_fifo;
112 struct input_dev *delayed_input;
114 unsigned long quirks;
118 /* HID++ 1.0 error codes */
119 #define HIDPP_ERROR 0x8f
120 #define HIDPP_ERROR_SUCCESS 0x00
121 #define HIDPP_ERROR_INVALID_SUBID 0x01
122 #define HIDPP_ERROR_INVALID_ADRESS 0x02
123 #define HIDPP_ERROR_INVALID_VALUE 0x03
124 #define HIDPP_ERROR_CONNECT_FAIL 0x04
125 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
126 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
127 #define HIDPP_ERROR_BUSY 0x07
128 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
129 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
130 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
131 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
132 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
133 /* HID++ 2.0 error codes */
134 #define HIDPP20_ERROR 0xff
136 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
138 static int __hidpp_send_report(struct hid_device *hdev,
139 struct hidpp_report *hidpp_report)
141 int fields_count, ret;
143 switch (hidpp_report->report_id) {
144 case REPORT_ID_HIDPP_SHORT:
145 fields_count = HIDPP_REPORT_SHORT_LENGTH;
147 case REPORT_ID_HIDPP_LONG:
148 fields_count = HIDPP_REPORT_LONG_LENGTH;
155 * set the device_index as the receiver, it will be overwritten by
156 * hid_hw_request if needed
158 hidpp_report->device_index = 0xff;
160 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
161 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
164 return ret == fields_count ? 0 : -1;
168 * hidpp_send_message_sync() returns 0 in case of success, and something else
169 * in case of a failure.
170 * - If ' something else' is positive, that means that an error has been raised
171 * by the protocol itself.
172 * - If ' something else' is negative, that means that we had a classic error
173 * (-ENOMEM, -EPIPE, etc...)
175 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
176 struct hidpp_report *message,
177 struct hidpp_report *response)
181 mutex_lock(&hidpp->send_mutex);
183 hidpp->send_receive_buf = response;
184 hidpp->answer_available = false;
187 * So that we can later validate the answer when it arrives
190 *response = *message;
192 ret = __hidpp_send_report(hidpp->hid_dev, message);
195 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
196 memset(response, 0, sizeof(struct hidpp_report));
200 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
202 dbg_hid("%s:timeout waiting for response\n", __func__);
203 memset(response, 0, sizeof(struct hidpp_report));
207 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
208 response->rap.sub_id == HIDPP_ERROR) {
209 ret = response->rap.params[1];
210 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
214 if (response->report_id == REPORT_ID_HIDPP_LONG &&
215 response->fap.feature_index == HIDPP20_ERROR) {
216 ret = response->fap.params[1];
217 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
222 mutex_unlock(&hidpp->send_mutex);
227 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
228 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
229 struct hidpp_report *response)
231 struct hidpp_report *message;
234 if (param_count > sizeof(message->fap.params))
237 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
240 message->report_id = REPORT_ID_HIDPP_LONG;
241 message->fap.feature_index = feat_index;
242 message->fap.funcindex_clientid = funcindex_clientid;
243 memcpy(&message->fap.params, params, param_count);
245 ret = hidpp_send_message_sync(hidpp, message, response);
250 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
251 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
252 struct hidpp_report *response)
254 struct hidpp_report *message;
257 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
258 (report_id != REPORT_ID_HIDPP_LONG))
261 if (param_count > sizeof(message->rap.params))
264 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
267 message->report_id = report_id;
268 message->rap.sub_id = sub_id;
269 message->rap.reg_address = reg_address;
270 memcpy(&message->rap.params, params, param_count);
272 ret = hidpp_send_message_sync(hidpp_dev, message, response);
277 static void delayed_work_cb(struct work_struct *work)
279 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
281 hidpp_connect_event(hidpp);
284 static inline bool hidpp_match_answer(struct hidpp_report *question,
285 struct hidpp_report *answer)
287 return (answer->fap.feature_index == question->fap.feature_index) &&
288 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
291 static inline bool hidpp_match_error(struct hidpp_report *question,
292 struct hidpp_report *answer)
294 return ((answer->rap.sub_id == HIDPP_ERROR) ||
295 (answer->fap.feature_index == HIDPP20_ERROR)) &&
296 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
297 (answer->fap.params[0] == question->fap.funcindex_clientid);
300 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
302 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
303 (report->rap.sub_id == 0x41);
307 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
309 static void hidpp_prefix_name(char **name, int name_length)
311 #define PREFIX_LENGTH 9 /* "Logitech " */
316 if (name_length > PREFIX_LENGTH &&
317 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
318 /* The prefix has is already in the name */
321 new_length = PREFIX_LENGTH + name_length;
322 new_name = kzalloc(new_length, GFP_KERNEL);
326 snprintf(new_name, new_length, "Logitech %s", *name);
333 /* -------------------------------------------------------------------------- */
334 /* HIDP++ 1.0 commands */
335 /* -------------------------------------------------------------------------- */
337 #define HIDPP_SET_REGISTER 0x80
338 #define HIDPP_GET_REGISTER 0x81
339 #define HIDPP_SET_LONG_REGISTER 0x82
340 #define HIDPP_GET_LONG_REGISTER 0x83
342 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
343 #define DEVICE_NAME 0x40
345 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
347 struct hidpp_report response;
349 /* hid-logitech-dj is in charge of setting the right device index */
350 u8 params[1] = { DEVICE_NAME };
354 ret = hidpp_send_rap_command_sync(hidpp_dev,
355 REPORT_ID_HIDPP_SHORT,
356 HIDPP_GET_LONG_REGISTER,
357 HIDPP_REG_PAIRING_INFORMATION,
358 params, 1, &response);
362 len = response.rap.params[1];
364 if (2 + len > sizeof(response.rap.params))
367 name = kzalloc(len + 1, GFP_KERNEL);
371 memcpy(name, &response.rap.params[2], len);
373 /* include the terminating '\0' */
374 hidpp_prefix_name(&name, len + 1);
379 /* -------------------------------------------------------------------------- */
381 /* -------------------------------------------------------------------------- */
383 #define HIDPP_PAGE_ROOT 0x0000
384 #define HIDPP_PAGE_ROOT_IDX 0x00
386 #define CMD_ROOT_GET_FEATURE 0x01
387 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
389 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
390 u8 *feature_index, u8 *feature_type)
392 struct hidpp_report response;
394 u8 params[2] = { feature >> 8, feature & 0x00FF };
396 ret = hidpp_send_fap_command_sync(hidpp,
398 CMD_ROOT_GET_FEATURE,
399 params, 2, &response);
403 *feature_index = response.fap.params[0];
404 *feature_type = response.fap.params[1];
409 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
411 struct hidpp_report response;
414 ret = hidpp_send_fap_command_sync(hidpp,
416 CMD_ROOT_GET_PROTOCOL_VERSION,
419 if (ret == HIDPP_ERROR_INVALID_SUBID) {
420 hidpp->protocol_major = 1;
421 hidpp->protocol_minor = 0;
425 /* the device might not be connected */
426 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
430 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
437 hidpp->protocol_major = response.fap.params[0];
438 hidpp->protocol_minor = response.fap.params[1];
443 static bool hidpp_is_connected(struct hidpp_device *hidpp)
447 ret = hidpp_root_get_protocol_version(hidpp);
449 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
450 hidpp->protocol_major, hidpp->protocol_minor);
454 /* -------------------------------------------------------------------------- */
455 /* 0x0005: GetDeviceNameType */
456 /* -------------------------------------------------------------------------- */
458 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
460 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
461 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
462 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
464 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
465 u8 feature_index, u8 *nameLength)
467 struct hidpp_report response;
470 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
471 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
474 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
481 *nameLength = response.fap.params[0];
486 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
487 u8 feature_index, u8 char_index, char *device_name, int len_buf)
489 struct hidpp_report response;
493 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
494 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
498 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
505 if (response.report_id == REPORT_ID_HIDPP_LONG)
506 count = HIDPP_REPORT_LONG_LENGTH - 4;
508 count = HIDPP_REPORT_SHORT_LENGTH - 4;
513 for (i = 0; i < count; i++)
514 device_name[i] = response.fap.params[i];
519 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
528 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
529 &feature_index, &feature_type);
533 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
538 name = kzalloc(__name_length + 1, GFP_KERNEL);
542 while (index < __name_length) {
543 ret = hidpp_devicenametype_get_device_name(hidpp,
544 feature_index, index, name + index,
545 __name_length - index);
553 /* include the terminating '\0' */
554 hidpp_prefix_name(&name, __name_length + 1);
559 /* -------------------------------------------------------------------------- */
560 /* 0x6100: TouchPadRawXY */
561 /* -------------------------------------------------------------------------- */
563 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
565 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
566 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
568 #define EVENT_TOUCHPAD_RAW_XY 0x00
570 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
571 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
573 struct hidpp_touchpad_raw_info {
584 struct hidpp_touchpad_raw_xy_finger {
594 struct hidpp_touchpad_raw_xy {
596 struct hidpp_touchpad_raw_xy_finger fingers[2];
603 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
604 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
606 struct hidpp_report response;
608 u8 *params = (u8 *)response.fap.params;
610 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
611 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
614 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
621 raw_info->x_size = get_unaligned_be16(¶ms[0]);
622 raw_info->y_size = get_unaligned_be16(¶ms[2]);
623 raw_info->z_range = params[4];
624 raw_info->area_range = params[5];
625 raw_info->maxcontacts = params[7];
626 raw_info->origin = params[8];
627 /* res is given in unit per inch */
628 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
633 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
634 u8 feature_index, bool send_raw_reports,
635 bool sensor_enhanced_settings)
637 struct hidpp_report response;
642 * bit 1 - 16bit Z, no area
643 * bit 2 - enhanced sensitivity
644 * bit 3 - width, height (4 bits each) instead of area
645 * bit 4 - send raw + gestures (degrades smoothness)
646 * remaining bits - reserved
648 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
650 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
651 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
654 static void hidpp_touchpad_touch_event(u8 *data,
655 struct hidpp_touchpad_raw_xy_finger *finger)
657 u8 x_m = data[0] << 2;
658 u8 y_m = data[2] << 2;
660 finger->x = x_m << 6 | data[1];
661 finger->y = y_m << 6 | data[3];
663 finger->contact_type = data[0] >> 6;
664 finger->contact_status = data[2] >> 6;
667 finger->area = data[5];
668 finger->finger_id = data[6] >> 4;
671 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
672 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
674 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
675 raw_xy->end_of_frame = data[8] & 0x01;
676 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
677 raw_xy->finger_count = data[15] & 0x0f;
678 raw_xy->button = (data[8] >> 2) & 0x01;
680 if (raw_xy->finger_count) {
681 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
682 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
686 /* ************************************************************************** */
690 /* ************************************************************************** */
692 /* -------------------------------------------------------------------------- */
693 /* Touchpad HID++ devices */
694 /* -------------------------------------------------------------------------- */
696 #define WTP_MANUAL_RESOLUTION 39
699 struct input_dev *input;
703 u8 button_feature_index;
706 unsigned int resolution;
709 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
710 struct hid_field *field, struct hid_usage *usage,
711 unsigned long **bit, int *max)
716 static void wtp_populate_input(struct hidpp_device *hidpp,
717 struct input_dev *input_dev, bool origin_is_hid_core)
719 struct wtp_data *wd = hidpp->private_data;
721 __set_bit(EV_ABS, input_dev->evbit);
722 __set_bit(EV_KEY, input_dev->evbit);
723 __clear_bit(EV_REL, input_dev->evbit);
724 __clear_bit(EV_LED, input_dev->evbit);
726 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
727 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
728 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
729 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
731 /* Max pressure is not given by the devices, pick one */
732 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
734 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
736 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
737 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
739 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
741 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
742 INPUT_MT_DROP_UNUSED);
744 wd->input = input_dev;
747 static void wtp_touch_event(struct wtp_data *wd,
748 struct hidpp_touchpad_raw_xy_finger *touch_report)
752 if (!touch_report->finger_id || touch_report->contact_type)
756 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
758 input_mt_slot(wd->input, slot);
759 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
760 touch_report->contact_status);
761 if (touch_report->contact_status) {
762 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
764 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
765 wd->flip_y ? wd->y_size - touch_report->y :
767 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
772 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
773 struct hidpp_touchpad_raw_xy *raw)
775 struct wtp_data *wd = hidpp->private_data;
778 for (i = 0; i < 2; i++)
779 wtp_touch_event(wd, &(raw->fingers[i]));
781 if (raw->end_of_frame &&
782 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
783 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
785 if (raw->end_of_frame || raw->finger_count <= 2) {
786 input_mt_sync_frame(wd->input);
787 input_sync(wd->input);
791 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
793 struct wtp_data *wd = hidpp->private_data;
794 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
795 (data[7] >> 4) * (data[7] >> 4)) / 2;
796 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
797 (data[13] >> 4) * (data[13] >> 4)) / 2;
798 struct hidpp_touchpad_raw_xy raw = {
799 .timestamp = data[1],
803 .contact_status = !!data[7],
804 .x = get_unaligned_le16(&data[3]),
805 .y = get_unaligned_le16(&data[5]),
808 .finger_id = data[2],
811 .contact_status = !!data[13],
812 .x = get_unaligned_le16(&data[9]),
813 .y = get_unaligned_le16(&data[11]),
816 .finger_id = data[8],
819 .finger_count = wd->maxcontacts,
821 .end_of_frame = (data[0] >> 7) == 0,
822 .button = data[0] & 0x01,
825 wtp_send_raw_xy_event(hidpp, &raw);
830 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
832 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
833 struct wtp_data *wd = hidpp->private_data;
834 struct hidpp_report *report = (struct hidpp_report *)data;
835 struct hidpp_touchpad_raw_xy raw;
837 if (!wd || !wd->input)
843 hid_err(hdev, "Received HID report of bad size (%d)",
847 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
848 input_event(wd->input, EV_KEY, BTN_LEFT,
850 input_event(wd->input, EV_KEY, BTN_RIGHT,
852 input_sync(wd->input);
857 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
859 case REPORT_ID_HIDPP_LONG:
860 /* size is already checked in hidpp_raw_event. */
861 if ((report->fap.feature_index != wd->mt_feature_index) ||
862 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
864 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
866 wtp_send_raw_xy_event(hidpp, &raw);
873 static int wtp_get_config(struct hidpp_device *hidpp)
875 struct wtp_data *wd = hidpp->private_data;
876 struct hidpp_touchpad_raw_info raw_info = {0};
880 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
881 &wd->mt_feature_index, &feature_type);
883 /* means that the device is not powered up */
886 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
891 wd->x_size = raw_info.x_size;
892 wd->y_size = raw_info.y_size;
893 wd->maxcontacts = raw_info.maxcontacts;
894 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
895 wd->resolution = raw_info.res;
897 wd->resolution = WTP_MANUAL_RESOLUTION;
902 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
904 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
907 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
912 hidpp->private_data = wd;
917 static int wtp_connect(struct hid_device *hdev, bool connected)
919 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
920 struct wtp_data *wd = hidpp->private_data;
927 ret = wtp_get_config(hidpp);
929 hid_err(hdev, "Can not get wtp config: %d\n", ret);
934 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
938 /* ------------------------------------------------------------------------- */
939 /* Logitech M560 devices */
940 /* ------------------------------------------------------------------------- */
943 * Logitech M560 protocol overview
945 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
946 * the sides buttons are pressed, it sends some keyboard keys events
947 * instead of buttons ones.
948 * To complicate things further, the middle button keys sequence
949 * is different from the odd press and the even press.
951 * forward button -> Super_R
952 * backward button -> Super_L+'d' (press only)
953 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
954 * 2nd time: left-click (press only)
955 * NB: press-only means that when the button is pressed, the
956 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
957 * together sequentially; instead when the button is released, no event is
961 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
962 * the mouse reacts differently:
963 * - it never sends a keyboard key event
964 * - for the three mouse button it sends:
965 * middle button press 11<xx>0a 3500af00...
966 * side 1 button (forward) press 11<xx>0a 3500b000...
967 * side 2 button (backward) press 11<xx>0a 3500ae00...
968 * middle/side1/side2 button release 11<xx>0a 35000000...
971 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
973 struct m560_private_data {
974 struct input_dev *input;
977 /* how buttons are mapped in the report */
978 #define M560_MOUSE_BTN_LEFT 0x01
979 #define M560_MOUSE_BTN_RIGHT 0x02
980 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
981 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
983 #define M560_SUB_ID 0x0a
984 #define M560_BUTTON_MODE_REGISTER 0x35
986 static int m560_send_config_command(struct hid_device *hdev, bool connected)
988 struct hidpp_report response;
989 struct hidpp_device *hidpp_dev;
991 hidpp_dev = hid_get_drvdata(hdev);
996 return hidpp_send_rap_command_sync(
998 REPORT_ID_HIDPP_SHORT,
1000 M560_BUTTON_MODE_REGISTER,
1001 (u8 *)m560_config_parameter,
1002 sizeof(m560_config_parameter),
1007 static int m560_allocate(struct hid_device *hdev)
1009 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1010 struct m560_private_data *d;
1012 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1017 hidpp->private_data = d;
1022 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1024 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1025 struct m560_private_data *mydata = hidpp->private_data;
1028 if (!mydata || !mydata->input) {
1029 hid_err(hdev, "error in parameter\n");
1034 hid_err(hdev, "error in report\n");
1038 if (data[0] == REPORT_ID_HIDPP_LONG &&
1039 data[2] == M560_SUB_ID && data[6] == 0x00) {
1041 * m560 mouse report for middle, forward and backward button
1044 * data[1] = device-id
1046 * data[5] = 0xaf -> middle
1049 * 0x00 -> release all
1055 input_report_key(mydata->input, BTN_MIDDLE, 1);
1058 input_report_key(mydata->input, BTN_FORWARD, 1);
1061 input_report_key(mydata->input, BTN_BACK, 1);
1064 input_report_key(mydata->input, BTN_BACK, 0);
1065 input_report_key(mydata->input, BTN_FORWARD, 0);
1066 input_report_key(mydata->input, BTN_MIDDLE, 0);
1069 hid_err(hdev, "error in report\n");
1072 input_sync(mydata->input);
1074 } else if (data[0] == 0x02) {
1076 * Logitech M560 mouse report
1078 * data[0] = type (0x02)
1079 * data[1..2] = buttons
1086 input_report_key(mydata->input, BTN_LEFT,
1087 !!(data[1] & M560_MOUSE_BTN_LEFT));
1088 input_report_key(mydata->input, BTN_RIGHT,
1089 !!(data[1] & M560_MOUSE_BTN_RIGHT));
1091 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
1092 input_report_rel(mydata->input, REL_HWHEEL, -1);
1093 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
1094 input_report_rel(mydata->input, REL_HWHEEL, 1);
1096 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
1097 input_report_rel(mydata->input, REL_X, v);
1099 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
1100 input_report_rel(mydata->input, REL_Y, v);
1102 v = hid_snto32(data[6], 8);
1103 input_report_rel(mydata->input, REL_WHEEL, v);
1105 input_sync(mydata->input);
1111 static void m560_populate_input(struct hidpp_device *hidpp,
1112 struct input_dev *input_dev, bool origin_is_hid_core)
1114 struct m560_private_data *mydata = hidpp->private_data;
1116 mydata->input = input_dev;
1118 __set_bit(EV_KEY, mydata->input->evbit);
1119 __set_bit(BTN_MIDDLE, mydata->input->keybit);
1120 __set_bit(BTN_RIGHT, mydata->input->keybit);
1121 __set_bit(BTN_LEFT, mydata->input->keybit);
1122 __set_bit(BTN_BACK, mydata->input->keybit);
1123 __set_bit(BTN_FORWARD, mydata->input->keybit);
1125 __set_bit(EV_REL, mydata->input->evbit);
1126 __set_bit(REL_X, mydata->input->relbit);
1127 __set_bit(REL_Y, mydata->input->relbit);
1128 __set_bit(REL_WHEEL, mydata->input->relbit);
1129 __set_bit(REL_HWHEEL, mydata->input->relbit);
1132 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1133 struct hid_field *field, struct hid_usage *usage,
1134 unsigned long **bit, int *max)
1139 /* -------------------------------------------------------------------------- */
1140 /* Generic HID++ devices */
1141 /* -------------------------------------------------------------------------- */
1143 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1144 struct hid_field *field, struct hid_usage *usage,
1145 unsigned long **bit, int *max)
1147 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1149 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1150 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
1151 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
1152 field->application != HID_GD_MOUSE)
1153 return m560_input_mapping(hdev, hi, field, usage, bit, max);
1158 static void hidpp_populate_input(struct hidpp_device *hidpp,
1159 struct input_dev *input, bool origin_is_hid_core)
1161 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1162 wtp_populate_input(hidpp, input, origin_is_hid_core);
1163 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1164 m560_populate_input(hidpp, input, origin_is_hid_core);
1167 static void hidpp_input_configured(struct hid_device *hdev,
1168 struct hid_input *hidinput)
1170 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1171 struct input_dev *input = hidinput->input;
1173 hidpp_populate_input(hidpp, input, true);
1176 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
1179 struct hidpp_report *question = hidpp->send_receive_buf;
1180 struct hidpp_report *answer = hidpp->send_receive_buf;
1181 struct hidpp_report *report = (struct hidpp_report *)data;
1184 * If the mutex is locked then we have a pending answer from a
1185 * previously sent command.
1187 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
1189 * Check for a correct hidpp20 answer or the corresponding
1192 if (hidpp_match_answer(question, report) ||
1193 hidpp_match_error(question, report)) {
1195 hidpp->answer_available = true;
1196 wake_up(&hidpp->wait);
1198 * This was an answer to a command that this driver sent
1199 * We return 1 to hid-core to avoid forwarding the
1200 * command upstream as it has been treated by the driver
1207 if (unlikely(hidpp_report_is_connect_event(report))) {
1208 atomic_set(&hidpp->connected,
1209 !(report->rap.params[0] & (1 << 6)));
1210 if ((hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) &&
1211 (schedule_work(&hidpp->work) == 0))
1212 dbg_hid("%s: connect event already queued\n", __func__);
1219 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1222 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1225 /* Generic HID++ processing. */
1227 case REPORT_ID_HIDPP_LONG:
1228 if (size != HIDPP_REPORT_LONG_LENGTH) {
1229 hid_err(hdev, "received hid++ report of bad size (%d)",
1233 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1235 case REPORT_ID_HIDPP_SHORT:
1236 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1237 hid_err(hdev, "received hid++ report of bad size (%d)",
1241 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1245 /* If no report is available for further processing, skip calling
1246 * raw_event of subclasses. */
1250 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1251 return wtp_raw_event(hdev, data, size);
1252 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1253 return m560_raw_event(hdev, data, size);
1258 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
1260 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1265 * the device is connected through an Unifying receiver, and
1266 * might not be already connected.
1267 * Ask the receiver for its name.
1269 name = hidpp_get_unifying_name(hidpp);
1271 name = hidpp_get_device_name(hidpp);
1274 hid_err(hdev, "unable to retrieve the name of the device");
1276 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1281 static int hidpp_input_open(struct input_dev *dev)
1283 struct hid_device *hid = input_get_drvdata(dev);
1285 return hid_hw_open(hid);
1288 static void hidpp_input_close(struct input_dev *dev)
1290 struct hid_device *hid = input_get_drvdata(dev);
1295 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1297 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
1298 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1303 input_set_drvdata(input_dev, hdev);
1304 input_dev->open = hidpp_input_open;
1305 input_dev->close = hidpp_input_close;
1307 input_dev->name = hidpp->name;
1308 input_dev->phys = hdev->phys;
1309 input_dev->uniq = hdev->uniq;
1310 input_dev->id.bustype = hdev->bus;
1311 input_dev->id.vendor = hdev->vendor;
1312 input_dev->id.product = hdev->product;
1313 input_dev->id.version = hdev->version;
1314 input_dev->dev.parent = &hdev->dev;
1319 static void hidpp_connect_event(struct hidpp_device *hidpp)
1321 struct hid_device *hdev = hidpp->hid_dev;
1323 bool connected = atomic_read(&hidpp->connected);
1324 struct input_dev *input;
1325 char *name, *devm_name;
1327 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1328 ret = wtp_connect(hdev, connected);
1331 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1332 ret = m560_send_config_command(hdev, connected);
1337 if (!connected || hidpp->delayed_input)
1340 /* the device is already connected, we can ask for its name and
1342 if (!hidpp->protocol_major) {
1343 ret = !hidpp_is_connected(hidpp);
1345 hid_err(hdev, "Can not get the protocol version.\n");
1348 hid_info(hdev, "HID++ %u.%u device connected.\n",
1349 hidpp->protocol_major, hidpp->protocol_minor);
1352 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
1353 /* if HID created the input nodes for us, we can stop now */
1356 if (!hidpp->name || hidpp->name == hdev->name) {
1357 name = hidpp_get_device_name(hidpp);
1360 "unable to retrieve the name of the device");
1364 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1369 hidpp->name = devm_name;
1372 input = hidpp_allocate_input(hdev);
1374 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1378 hidpp_populate_input(hidpp, input, false);
1380 ret = input_register_device(input);
1382 input_free_device(input);
1384 hidpp->delayed_input = input;
1387 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1389 struct hidpp_device *hidpp;
1392 unsigned int connect_mask = HID_CONNECT_DEFAULT;
1394 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1399 hidpp->hid_dev = hdev;
1400 hidpp->name = hdev->name;
1401 hid_set_drvdata(hdev, hidpp);
1403 hidpp->quirks = id->driver_data;
1405 if (disable_raw_mode) {
1406 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
1407 hidpp->quirks &= ~HIDPP_QUIRK_CONNECT_EVENTS;
1408 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
1411 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1412 ret = wtp_allocate(hdev, id);
1415 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1416 ret = m560_allocate(hdev);
1421 INIT_WORK(&hidpp->work, delayed_work_cb);
1422 mutex_init(&hidpp->send_mutex);
1423 init_waitqueue_head(&hidpp->wait);
1425 ret = hid_parse(hdev);
1427 hid_err(hdev, "%s:parse failed\n", __func__);
1428 goto hid_parse_fail;
1431 /* Allow incoming packets */
1432 hid_device_io_start(hdev);
1434 connected = hidpp_is_connected(hidpp);
1435 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1438 hid_err(hdev, "Device not connected");
1439 hid_device_io_stop(hdev);
1440 goto hid_parse_fail;
1443 hid_info(hdev, "HID++ %u.%u device connected.\n",
1444 hidpp->protocol_major, hidpp->protocol_minor);
1447 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
1448 atomic_set(&hidpp->connected, connected);
1450 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
1451 ret = wtp_get_config(hidpp);
1453 goto hid_parse_fail;
1456 /* Block incoming packets */
1457 hid_device_io_stop(hdev);
1459 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
1460 connect_mask &= ~HID_CONNECT_HIDINPUT;
1462 ret = hid_hw_start(hdev, connect_mask);
1464 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1465 goto hid_hw_start_fail;
1468 if (hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) {
1469 /* Allow incoming packets */
1470 hid_device_io_start(hdev);
1472 hidpp_connect_event(hidpp);
1479 cancel_work_sync(&hidpp->work);
1480 mutex_destroy(&hidpp->send_mutex);
1482 hid_set_drvdata(hdev, NULL);
1486 static void hidpp_remove(struct hid_device *hdev)
1488 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1490 cancel_work_sync(&hidpp->work);
1491 mutex_destroy(&hidpp->send_mutex);
1495 static const struct hid_device_id hidpp_devices[] = {
1496 { /* wireless touchpad */
1497 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1498 USB_VENDOR_ID_LOGITECH, 0x4011),
1499 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1500 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
1501 { /* wireless touchpad T650 */
1502 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1503 USB_VENDOR_ID_LOGITECH, 0x4101),
1504 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
1505 { /* wireless touchpad T651 */
1506 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1507 USB_DEVICE_ID_LOGITECH_T651),
1508 .driver_data = HIDPP_QUIRK_CLASS_WTP },
1509 { /* Mouse logitech M560 */
1510 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1511 USB_VENDOR_ID_LOGITECH, 0x402d),
1512 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
1514 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1515 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1519 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1521 static struct hid_driver hidpp_driver = {
1522 .name = "logitech-hidpp-device",
1523 .id_table = hidpp_devices,
1524 .probe = hidpp_probe,
1525 .remove = hidpp_remove,
1526 .raw_event = hidpp_raw_event,
1527 .input_configured = hidpp_input_configured,
1528 .input_mapping = hidpp_input_mapping,
1531 module_hid_driver(hidpp_driver);