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)
44 /* bits 1..20 are reserved for classes */
45 #define HIDPP_QUIRK_DELAYED_INIT BIT(21)
46 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
47 #define HIDPP_QUIRK_MULTI_INPUT BIT(23)
50 * There are two hidpp protocols in use, the first version hidpp10 is known
51 * as register access protocol or RAP, the second version hidpp20 is known as
52 * feature access protocol or FAP
54 * Most older devices (including the Unifying usb receiver) use the RAP protocol
55 * where as most newer devices use the FAP protocol. Both protocols are
56 * compatible with the underlying transport, which could be usb, Unifiying, or
57 * bluetooth. The message lengths are defined by the hid vendor specific report
58 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
59 * the HIDPP_LONG report type (total message length 20 bytes)
61 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
62 * messages. The Unifying receiver itself responds to RAP messages (device index
63 * is 0xFF for the receiver), and all messages (short or long) with a device
64 * index between 1 and 6 are passed untouched to the corresponding paired
67 * The paired device can be RAP or FAP, it will receive the message untouched
68 * from the Unifiying receiver.
73 u8 funcindex_clientid;
74 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
80 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
89 u8 rawbytes[sizeof(struct fap)];
94 struct hid_device *hid_dev;
95 struct mutex send_mutex;
96 void *send_receive_buf;
97 char *name; /* will never be NULL and should not be freed */
98 wait_queue_head_t wait;
99 bool answer_available;
105 struct work_struct work;
106 struct kfifo delayed_work_fifo;
108 struct input_dev *delayed_input;
110 unsigned long quirks;
114 /* HID++ 1.0 error codes */
115 #define HIDPP_ERROR 0x8f
116 #define HIDPP_ERROR_SUCCESS 0x00
117 #define HIDPP_ERROR_INVALID_SUBID 0x01
118 #define HIDPP_ERROR_INVALID_ADRESS 0x02
119 #define HIDPP_ERROR_INVALID_VALUE 0x03
120 #define HIDPP_ERROR_CONNECT_FAIL 0x04
121 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
122 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
123 #define HIDPP_ERROR_BUSY 0x07
124 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
125 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
126 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
127 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
128 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
129 /* HID++ 2.0 error codes */
130 #define HIDPP20_ERROR 0xff
132 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
134 static int __hidpp_send_report(struct hid_device *hdev,
135 struct hidpp_report *hidpp_report)
137 int fields_count, ret;
139 switch (hidpp_report->report_id) {
140 case REPORT_ID_HIDPP_SHORT:
141 fields_count = HIDPP_REPORT_SHORT_LENGTH;
143 case REPORT_ID_HIDPP_LONG:
144 fields_count = HIDPP_REPORT_LONG_LENGTH;
151 * set the device_index as the receiver, it will be overwritten by
152 * hid_hw_request if needed
154 hidpp_report->device_index = 0xff;
156 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
157 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
160 return ret == fields_count ? 0 : -1;
164 * hidpp_send_message_sync() returns 0 in case of success, and something else
165 * in case of a failure.
166 * - If ' something else' is positive, that means that an error has been raised
167 * by the protocol itself.
168 * - If ' something else' is negative, that means that we had a classic error
169 * (-ENOMEM, -EPIPE, etc...)
171 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
172 struct hidpp_report *message,
173 struct hidpp_report *response)
177 mutex_lock(&hidpp->send_mutex);
179 hidpp->send_receive_buf = response;
180 hidpp->answer_available = false;
183 * So that we can later validate the answer when it arrives
186 *response = *message;
188 ret = __hidpp_send_report(hidpp->hid_dev, message);
191 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
192 memset(response, 0, sizeof(struct hidpp_report));
196 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
198 dbg_hid("%s:timeout waiting for response\n", __func__);
199 memset(response, 0, sizeof(struct hidpp_report));
203 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
204 response->rap.sub_id == HIDPP_ERROR) {
205 ret = response->rap.params[1];
206 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
210 if (response->report_id == REPORT_ID_HIDPP_LONG &&
211 response->fap.feature_index == HIDPP20_ERROR) {
212 ret = response->fap.params[1];
213 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
218 mutex_unlock(&hidpp->send_mutex);
223 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
224 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
225 struct hidpp_report *response)
227 struct hidpp_report *message;
230 if (param_count > sizeof(message->fap.params))
233 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
236 message->report_id = REPORT_ID_HIDPP_LONG;
237 message->fap.feature_index = feat_index;
238 message->fap.funcindex_clientid = funcindex_clientid;
239 memcpy(&message->fap.params, params, param_count);
241 ret = hidpp_send_message_sync(hidpp, message, response);
246 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
247 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
248 struct hidpp_report *response)
250 struct hidpp_report *message;
253 if ((report_id != REPORT_ID_HIDPP_SHORT) &&
254 (report_id != REPORT_ID_HIDPP_LONG))
257 if (param_count > sizeof(message->rap.params))
260 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
263 message->report_id = report_id;
264 message->rap.sub_id = sub_id;
265 message->rap.reg_address = reg_address;
266 memcpy(&message->rap.params, params, param_count);
268 ret = hidpp_send_message_sync(hidpp_dev, message, response);
273 static void delayed_work_cb(struct work_struct *work)
275 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
277 hidpp_connect_event(hidpp);
280 static inline bool hidpp_match_answer(struct hidpp_report *question,
281 struct hidpp_report *answer)
283 return (answer->fap.feature_index == question->fap.feature_index) &&
284 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
287 static inline bool hidpp_match_error(struct hidpp_report *question,
288 struct hidpp_report *answer)
290 return ((answer->rap.sub_id == HIDPP_ERROR) ||
291 (answer->fap.feature_index == HIDPP20_ERROR)) &&
292 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
293 (answer->fap.params[0] == question->fap.funcindex_clientid);
296 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
298 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
299 (report->rap.sub_id == 0x41);
303 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
305 static void hidpp_prefix_name(char **name, int name_length)
307 #define PREFIX_LENGTH 9 /* "Logitech " */
312 if (name_length > PREFIX_LENGTH &&
313 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
314 /* The prefix has is already in the name */
317 new_length = PREFIX_LENGTH + name_length;
318 new_name = kzalloc(new_length, GFP_KERNEL);
322 snprintf(new_name, new_length, "Logitech %s", *name);
329 /* -------------------------------------------------------------------------- */
330 /* HIDP++ 1.0 commands */
331 /* -------------------------------------------------------------------------- */
333 #define HIDPP_SET_REGISTER 0x80
334 #define HIDPP_GET_REGISTER 0x81
335 #define HIDPP_SET_LONG_REGISTER 0x82
336 #define HIDPP_GET_LONG_REGISTER 0x83
338 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
339 #define DEVICE_NAME 0x40
341 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
343 struct hidpp_report response;
345 /* hid-logitech-dj is in charge of setting the right device index */
346 u8 params[1] = { DEVICE_NAME };
350 ret = hidpp_send_rap_command_sync(hidpp_dev,
351 REPORT_ID_HIDPP_SHORT,
352 HIDPP_GET_LONG_REGISTER,
353 HIDPP_REG_PAIRING_INFORMATION,
354 params, 1, &response);
358 len = response.rap.params[1];
360 if (2 + len > sizeof(response.rap.params))
363 name = kzalloc(len + 1, GFP_KERNEL);
367 memcpy(name, &response.rap.params[2], len);
369 /* include the terminating '\0' */
370 hidpp_prefix_name(&name, len + 1);
375 /* -------------------------------------------------------------------------- */
377 /* -------------------------------------------------------------------------- */
379 #define HIDPP_PAGE_ROOT 0x0000
380 #define HIDPP_PAGE_ROOT_IDX 0x00
382 #define CMD_ROOT_GET_FEATURE 0x01
383 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
385 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
386 u8 *feature_index, u8 *feature_type)
388 struct hidpp_report response;
390 u8 params[2] = { feature >> 8, feature & 0x00FF };
392 ret = hidpp_send_fap_command_sync(hidpp,
394 CMD_ROOT_GET_FEATURE,
395 params, 2, &response);
399 *feature_index = response.fap.params[0];
400 *feature_type = response.fap.params[1];
405 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
407 struct hidpp_report response;
410 ret = hidpp_send_fap_command_sync(hidpp,
412 CMD_ROOT_GET_PROTOCOL_VERSION,
415 if (ret == HIDPP_ERROR_INVALID_SUBID) {
416 hidpp->protocol_major = 1;
417 hidpp->protocol_minor = 0;
421 /* the device might not be connected */
422 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
426 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
433 hidpp->protocol_major = response.fap.params[0];
434 hidpp->protocol_minor = response.fap.params[1];
439 static bool hidpp_is_connected(struct hidpp_device *hidpp)
443 ret = hidpp_root_get_protocol_version(hidpp);
445 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
446 hidpp->protocol_major, hidpp->protocol_minor);
450 /* -------------------------------------------------------------------------- */
451 /* 0x0005: GetDeviceNameType */
452 /* -------------------------------------------------------------------------- */
454 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
456 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
457 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
458 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
460 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
461 u8 feature_index, u8 *nameLength)
463 struct hidpp_report response;
466 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
467 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
470 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
477 *nameLength = response.fap.params[0];
482 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
483 u8 feature_index, u8 char_index, char *device_name, int len_buf)
485 struct hidpp_report response;
489 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
490 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
494 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
501 if (response.report_id == REPORT_ID_HIDPP_LONG)
502 count = HIDPP_REPORT_LONG_LENGTH - 4;
504 count = HIDPP_REPORT_SHORT_LENGTH - 4;
509 for (i = 0; i < count; i++)
510 device_name[i] = response.fap.params[i];
515 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
524 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
525 &feature_index, &feature_type);
529 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
534 name = kzalloc(__name_length + 1, GFP_KERNEL);
538 while (index < __name_length) {
539 ret = hidpp_devicenametype_get_device_name(hidpp,
540 feature_index, index, name + index,
541 __name_length - index);
549 /* include the terminating '\0' */
550 hidpp_prefix_name(&name, __name_length + 1);
555 /* -------------------------------------------------------------------------- */
556 /* 0x6100: TouchPadRawXY */
557 /* -------------------------------------------------------------------------- */
559 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
561 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
562 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
564 #define EVENT_TOUCHPAD_RAW_XY 0x00
566 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
567 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
569 struct hidpp_touchpad_raw_info {
580 struct hidpp_touchpad_raw_xy_finger {
590 struct hidpp_touchpad_raw_xy {
592 struct hidpp_touchpad_raw_xy_finger fingers[2];
599 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
600 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
602 struct hidpp_report response;
604 u8 *params = (u8 *)response.fap.params;
606 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
607 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
610 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
617 raw_info->x_size = get_unaligned_be16(¶ms[0]);
618 raw_info->y_size = get_unaligned_be16(¶ms[2]);
619 raw_info->z_range = params[4];
620 raw_info->area_range = params[5];
621 raw_info->maxcontacts = params[7];
622 raw_info->origin = params[8];
623 /* res is given in unit per inch */
624 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
629 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
630 u8 feature_index, bool send_raw_reports,
631 bool sensor_enhanced_settings)
633 struct hidpp_report response;
638 * bit 1 - 16bit Z, no area
639 * bit 2 - enhanced sensitivity
640 * bit 3 - width, height (4 bits each) instead of area
641 * bit 4 - send raw + gestures (degrades smoothness)
642 * remaining bits - reserved
644 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
646 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
647 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
650 static void hidpp_touchpad_touch_event(u8 *data,
651 struct hidpp_touchpad_raw_xy_finger *finger)
653 u8 x_m = data[0] << 2;
654 u8 y_m = data[2] << 2;
656 finger->x = x_m << 6 | data[1];
657 finger->y = y_m << 6 | data[3];
659 finger->contact_type = data[0] >> 6;
660 finger->contact_status = data[2] >> 6;
663 finger->area = data[5];
664 finger->finger_id = data[6] >> 4;
667 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
668 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
670 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
671 raw_xy->end_of_frame = data[8] & 0x01;
672 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
673 raw_xy->finger_count = data[15] & 0x0f;
674 raw_xy->button = (data[8] >> 2) & 0x01;
676 if (raw_xy->finger_count) {
677 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
678 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
682 /* ************************************************************************** */
686 /* ************************************************************************** */
688 /* -------------------------------------------------------------------------- */
689 /* Touchpad HID++ devices */
690 /* -------------------------------------------------------------------------- */
692 #define WTP_MANUAL_RESOLUTION 39
695 struct input_dev *input;
699 u8 button_feature_index;
702 unsigned int resolution;
705 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
706 struct hid_field *field, struct hid_usage *usage,
707 unsigned long **bit, int *max)
709 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
711 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) &&
712 (field->application == HID_GD_KEYBOARD))
718 static void wtp_populate_input(struct hidpp_device *hidpp,
719 struct input_dev *input_dev, bool origin_is_hid_core)
721 struct wtp_data *wd = hidpp->private_data;
723 if ((hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT) && origin_is_hid_core)
724 /* this is the generic hid-input call */
727 __set_bit(EV_ABS, input_dev->evbit);
728 __set_bit(EV_KEY, input_dev->evbit);
729 __clear_bit(EV_REL, input_dev->evbit);
730 __clear_bit(EV_LED, input_dev->evbit);
732 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
733 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
734 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
735 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
737 /* Max pressure is not given by the devices, pick one */
738 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
740 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
742 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
743 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
745 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
747 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
748 INPUT_MT_DROP_UNUSED);
750 wd->input = input_dev;
753 static void wtp_touch_event(struct wtp_data *wd,
754 struct hidpp_touchpad_raw_xy_finger *touch_report)
758 if (!touch_report->finger_id || touch_report->contact_type)
762 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
764 input_mt_slot(wd->input, slot);
765 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
766 touch_report->contact_status);
767 if (touch_report->contact_status) {
768 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
770 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
771 wd->flip_y ? wd->y_size - touch_report->y :
773 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
778 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
779 struct hidpp_touchpad_raw_xy *raw)
781 struct wtp_data *wd = hidpp->private_data;
784 for (i = 0; i < 2; i++)
785 wtp_touch_event(wd, &(raw->fingers[i]));
787 if (raw->end_of_frame &&
788 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
789 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
791 if (raw->end_of_frame || raw->finger_count <= 2) {
792 input_mt_sync_frame(wd->input);
793 input_sync(wd->input);
797 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
799 struct wtp_data *wd = hidpp->private_data;
800 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
801 (data[7] >> 4) * (data[7] >> 4)) / 2;
802 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
803 (data[13] >> 4) * (data[13] >> 4)) / 2;
804 struct hidpp_touchpad_raw_xy raw = {
805 .timestamp = data[1],
809 .contact_status = !!data[7],
810 .x = get_unaligned_le16(&data[3]),
811 .y = get_unaligned_le16(&data[5]),
814 .finger_id = data[2],
817 .contact_status = !!data[13],
818 .x = get_unaligned_le16(&data[9]),
819 .y = get_unaligned_le16(&data[11]),
822 .finger_id = data[8],
825 .finger_count = wd->maxcontacts,
827 .end_of_frame = (data[0] >> 7) == 0,
828 .button = data[0] & 0x01,
831 wtp_send_raw_xy_event(hidpp, &raw);
836 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
838 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
839 struct wtp_data *wd = hidpp->private_data;
840 struct hidpp_report *report = (struct hidpp_report *)data;
841 struct hidpp_touchpad_raw_xy raw;
843 if (!wd || !wd->input)
849 hid_err(hdev, "Received HID report of bad size (%d)",
853 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
854 input_event(wd->input, EV_KEY, BTN_LEFT,
856 input_event(wd->input, EV_KEY, BTN_RIGHT,
858 input_sync(wd->input);
863 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
865 case REPORT_ID_HIDPP_LONG:
866 /* size is already checked in hidpp_raw_event. */
867 if ((report->fap.feature_index != wd->mt_feature_index) ||
868 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
870 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
872 wtp_send_raw_xy_event(hidpp, &raw);
879 static int wtp_get_config(struct hidpp_device *hidpp)
881 struct wtp_data *wd = hidpp->private_data;
882 struct hidpp_touchpad_raw_info raw_info = {0};
886 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
887 &wd->mt_feature_index, &feature_type);
889 /* means that the device is not powered up */
892 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
897 wd->x_size = raw_info.x_size;
898 wd->y_size = raw_info.y_size;
899 wd->maxcontacts = raw_info.maxcontacts;
900 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
901 wd->resolution = raw_info.res;
903 wd->resolution = WTP_MANUAL_RESOLUTION;
908 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
910 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
913 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
918 hidpp->private_data = wd;
923 static int wtp_connect(struct hid_device *hdev, bool connected)
925 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
926 struct wtp_data *wd = hidpp->private_data;
933 ret = wtp_get_config(hidpp);
935 hid_err(hdev, "Can not get wtp config: %d\n", ret);
940 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
944 /* -------------------------------------------------------------------------- */
945 /* Generic HID++ devices */
946 /* -------------------------------------------------------------------------- */
948 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
949 struct hid_field *field, struct hid_usage *usage,
950 unsigned long **bit, int *max)
952 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
954 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
955 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
960 static void hidpp_populate_input(struct hidpp_device *hidpp,
961 struct input_dev *input, bool origin_is_hid_core)
963 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
964 wtp_populate_input(hidpp, input, origin_is_hid_core);
967 static void hidpp_input_configured(struct hid_device *hdev,
968 struct hid_input *hidinput)
970 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
971 struct input_dev *input = hidinput->input;
973 hidpp_populate_input(hidpp, input, true);
976 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
979 struct hidpp_report *question = hidpp->send_receive_buf;
980 struct hidpp_report *answer = hidpp->send_receive_buf;
981 struct hidpp_report *report = (struct hidpp_report *)data;
984 * If the mutex is locked then we have a pending answer from a
985 * previously sent command.
987 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
989 * Check for a correct hidpp20 answer or the corresponding
992 if (hidpp_match_answer(question, report) ||
993 hidpp_match_error(question, report)) {
995 hidpp->answer_available = true;
996 wake_up(&hidpp->wait);
998 * This was an answer to a command that this driver sent
999 * We return 1 to hid-core to avoid forwarding the
1000 * command upstream as it has been treated by the driver
1007 if (unlikely(hidpp_report_is_connect_event(report))) {
1008 atomic_set(&hidpp->connected,
1009 !(report->rap.params[0] & (1 << 6)));
1010 if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
1011 (schedule_work(&hidpp->work) == 0))
1012 dbg_hid("%s: connect event already queued\n", __func__);
1019 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1022 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1025 /* Generic HID++ processing. */
1027 case REPORT_ID_HIDPP_LONG:
1028 if (size != HIDPP_REPORT_LONG_LENGTH) {
1029 hid_err(hdev, "received hid++ report of bad size (%d)",
1033 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1035 case REPORT_ID_HIDPP_SHORT:
1036 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1037 hid_err(hdev, "received hid++ report of bad size (%d)",
1041 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1045 /* If no report is available for further processing, skip calling
1046 * raw_event of subclasses. */
1050 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1051 return wtp_raw_event(hdev, data, size);
1056 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
1058 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1063 * the device is connected through an Unifying receiver, and
1064 * might not be already connected.
1065 * Ask the receiver for its name.
1067 name = hidpp_get_unifying_name(hidpp);
1069 name = hidpp_get_device_name(hidpp);
1072 hid_err(hdev, "unable to retrieve the name of the device");
1074 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1079 static int hidpp_input_open(struct input_dev *dev)
1081 struct hid_device *hid = input_get_drvdata(dev);
1083 return hid_hw_open(hid);
1086 static void hidpp_input_close(struct input_dev *dev)
1088 struct hid_device *hid = input_get_drvdata(dev);
1093 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1095 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
1096 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1101 input_set_drvdata(input_dev, hdev);
1102 input_dev->open = hidpp_input_open;
1103 input_dev->close = hidpp_input_close;
1105 input_dev->name = hidpp->name;
1106 input_dev->phys = hdev->phys;
1107 input_dev->uniq = hdev->uniq;
1108 input_dev->id.bustype = hdev->bus;
1109 input_dev->id.vendor = hdev->vendor;
1110 input_dev->id.product = hdev->product;
1111 input_dev->id.version = hdev->version;
1112 input_dev->dev.parent = &hdev->dev;
1117 static void hidpp_connect_event(struct hidpp_device *hidpp)
1119 struct hid_device *hdev = hidpp->hid_dev;
1121 bool connected = atomic_read(&hidpp->connected);
1122 struct input_dev *input;
1123 char *name, *devm_name;
1125 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1126 ret = wtp_connect(hdev, connected);
1131 if (!connected || hidpp->delayed_input)
1134 if (!hidpp->protocol_major) {
1135 ret = !hidpp_is_connected(hidpp);
1137 hid_err(hdev, "Can not get the protocol version.\n");
1142 /* the device is already connected, we can ask for its name and
1144 hid_info(hdev, "HID++ %u.%u device connected.\n",
1145 hidpp->protocol_major, hidpp->protocol_minor);
1147 if (!hidpp->name || hidpp->name == hdev->name) {
1148 name = hidpp_get_device_name(hidpp);
1151 "unable to retrieve the name of the device");
1155 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1160 hidpp->name = devm_name;
1163 input = hidpp_allocate_input(hdev);
1165 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1169 hidpp_populate_input(hidpp, input, false);
1171 ret = input_register_device(input);
1173 input_free_device(input);
1175 hidpp->delayed_input = input;
1178 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1180 struct hidpp_device *hidpp;
1183 unsigned int connect_mask = HID_CONNECT_DEFAULT;
1185 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1190 hidpp->hid_dev = hdev;
1191 hidpp->name = hdev->name;
1192 hid_set_drvdata(hdev, hidpp);
1194 hidpp->quirks = id->driver_data;
1196 if (disable_raw_mode) {
1197 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
1198 hidpp->quirks &= ~HIDPP_QUIRK_DELAYED_INIT;
1201 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1202 ret = wtp_allocate(hdev, id);
1204 goto wtp_allocate_fail;
1207 INIT_WORK(&hidpp->work, delayed_work_cb);
1208 mutex_init(&hidpp->send_mutex);
1209 init_waitqueue_head(&hidpp->wait);
1211 ret = hid_parse(hdev);
1213 hid_err(hdev, "%s:parse failed\n", __func__);
1214 goto hid_parse_fail;
1217 /* Allow incoming packets */
1218 hid_device_io_start(hdev);
1220 connected = hidpp_is_connected(hidpp);
1221 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1224 hid_err(hdev, "Device not connected");
1225 hid_device_io_stop(hdev);
1226 goto hid_parse_fail;
1229 hid_info(hdev, "HID++ %u.%u device connected.\n",
1230 hidpp->protocol_major, hidpp->protocol_minor);
1233 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
1234 atomic_set(&hidpp->connected, connected);
1236 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
1237 ret = wtp_get_config(hidpp);
1239 goto hid_parse_fail;
1242 /* Block incoming packets */
1243 hid_device_io_stop(hdev);
1245 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1246 connect_mask &= ~HID_CONNECT_HIDINPUT;
1248 /* Re-enable hidinput for multi-input devices */
1249 if (hidpp->quirks & HIDPP_QUIRK_MULTI_INPUT)
1250 connect_mask |= HID_CONNECT_HIDINPUT;
1252 ret = hid_hw_start(hdev, connect_mask);
1254 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1255 goto hid_hw_start_fail;
1258 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1259 /* Allow incoming packets */
1260 hid_device_io_start(hdev);
1262 hidpp_connect_event(hidpp);
1269 cancel_work_sync(&hidpp->work);
1270 mutex_destroy(&hidpp->send_mutex);
1272 hid_set_drvdata(hdev, NULL);
1276 static void hidpp_remove(struct hid_device *hdev)
1278 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1280 cancel_work_sync(&hidpp->work);
1281 mutex_destroy(&hidpp->send_mutex);
1285 static const struct hid_device_id hidpp_devices[] = {
1286 { /* wireless touchpad */
1287 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1288 USB_VENDOR_ID_LOGITECH, 0x4011),
1289 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1290 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
1291 { /* wireless touchpad T650 */
1292 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1293 USB_VENDOR_ID_LOGITECH, 0x4101),
1294 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
1295 { /* wireless touchpad T651 */
1296 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1297 USB_DEVICE_ID_LOGITECH_T651),
1298 .driver_data = HIDPP_QUIRK_CLASS_WTP },
1299 { /* Keyboard TK820 */
1300 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1301 USB_VENDOR_ID_LOGITECH, 0x4102),
1302 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_MULTI_INPUT |
1303 HIDPP_QUIRK_CLASS_WTP },
1305 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1306 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1310 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1312 static struct hid_driver hidpp_driver = {
1313 .name = "logitech-hidpp-device",
1314 .id_table = hidpp_devices,
1315 .probe = hidpp_probe,
1316 .remove = hidpp_remove,
1317 .raw_event = hidpp_raw_event,
1318 .input_configured = hidpp_input_configured,
1319 .input_mapping = hidpp_input_mapping,
1322 module_hid_driver(hidpp_driver);