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/input.h>
19 #include <linux/usb.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/kfifo.h>
25 #include <linux/input/mt.h>
26 #include <linux/workqueue.h>
27 #include <linux/atomic.h>
28 #include <linux/fixp-arith.h>
29 #include <asm/unaligned.h>
30 #include "usbhid/usbhid.h"
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
35 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
37 static bool disable_raw_mode;
38 module_param(disable_raw_mode, bool, 0644);
39 MODULE_PARM_DESC(disable_raw_mode,
40 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
42 static bool disable_tap_to_click;
43 module_param(disable_tap_to_click, bool, 0644);
44 MODULE_PARM_DESC(disable_tap_to_click,
45 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
47 #define REPORT_ID_HIDPP_SHORT 0x10
48 #define REPORT_ID_HIDPP_LONG 0x11
49 #define REPORT_ID_HIDPP_VERY_LONG 0x12
51 #define HIDPP_REPORT_SHORT_LENGTH 7
52 #define HIDPP_REPORT_LONG_LENGTH 20
53 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
55 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
56 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
57 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
58 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
60 /* bits 2..20 are reserved for classes */
61 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
62 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
63 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
64 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
65 #define HIDPP_QUIRK_HIDPP20_BATTERY BIT(25)
66 #define HIDPP_QUIRK_HIDPP10_BATTERY BIT(26)
68 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
71 * There are two hidpp protocols in use, the first version hidpp10 is known
72 * as register access protocol or RAP, the second version hidpp20 is known as
73 * feature access protocol or FAP
75 * Most older devices (including the Unifying usb receiver) use the RAP protocol
76 * where as most newer devices use the FAP protocol. Both protocols are
77 * compatible with the underlying transport, which could be usb, Unifiying, or
78 * bluetooth. The message lengths are defined by the hid vendor specific report
79 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
80 * the HIDPP_LONG report type (total message length 20 bytes)
82 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
83 * messages. The Unifying receiver itself responds to RAP messages (device index
84 * is 0xFF for the receiver), and all messages (short or long) with a device
85 * index between 1 and 6 are passed untouched to the corresponding paired
88 * The paired device can be RAP or FAP, it will receive the message untouched
89 * from the Unifiying receiver.
94 u8 funcindex_clientid;
95 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
101 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
104 struct hidpp_report {
110 u8 rawbytes[sizeof(struct fap)];
114 struct hidpp_battery {
116 struct power_supply_desc desc;
117 struct power_supply *ps;
123 struct hidpp_device {
124 struct hid_device *hid_dev;
125 struct mutex send_mutex;
126 void *send_receive_buf;
127 char *name; /* will never be NULL and should not be freed */
128 wait_queue_head_t wait;
129 bool answer_available;
135 struct work_struct work;
136 struct kfifo delayed_work_fifo;
138 struct input_dev *delayed_input;
140 unsigned long quirks;
142 struct hidpp_battery battery;
145 /* HID++ 1.0 error codes */
146 #define HIDPP_ERROR 0x8f
147 #define HIDPP_ERROR_SUCCESS 0x00
148 #define HIDPP_ERROR_INVALID_SUBID 0x01
149 #define HIDPP_ERROR_INVALID_ADRESS 0x02
150 #define HIDPP_ERROR_INVALID_VALUE 0x03
151 #define HIDPP_ERROR_CONNECT_FAIL 0x04
152 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
153 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
154 #define HIDPP_ERROR_BUSY 0x07
155 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
156 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
157 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
158 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
159 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
160 /* HID++ 2.0 error codes */
161 #define HIDPP20_ERROR 0xff
163 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
165 static int __hidpp_send_report(struct hid_device *hdev,
166 struct hidpp_report *hidpp_report)
168 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
169 int fields_count, ret;
171 hidpp = hid_get_drvdata(hdev);
173 switch (hidpp_report->report_id) {
174 case REPORT_ID_HIDPP_SHORT:
175 fields_count = HIDPP_REPORT_SHORT_LENGTH;
177 case REPORT_ID_HIDPP_LONG:
178 fields_count = HIDPP_REPORT_LONG_LENGTH;
180 case REPORT_ID_HIDPP_VERY_LONG:
181 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
188 * set the device_index as the receiver, it will be overwritten by
189 * hid_hw_request if needed
191 hidpp_report->device_index = 0xff;
193 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
194 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
196 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
197 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
201 return ret == fields_count ? 0 : -1;
205 * hidpp_send_message_sync() returns 0 in case of success, and something else
206 * in case of a failure.
207 * - If ' something else' is positive, that means that an error has been raised
208 * by the protocol itself.
209 * - If ' something else' is negative, that means that we had a classic error
210 * (-ENOMEM, -EPIPE, etc...)
212 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
213 struct hidpp_report *message,
214 struct hidpp_report *response)
218 mutex_lock(&hidpp->send_mutex);
220 hidpp->send_receive_buf = response;
221 hidpp->answer_available = false;
224 * So that we can later validate the answer when it arrives
227 *response = *message;
229 ret = __hidpp_send_report(hidpp->hid_dev, message);
232 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
233 memset(response, 0, sizeof(struct hidpp_report));
237 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
239 dbg_hid("%s:timeout waiting for response\n", __func__);
240 memset(response, 0, sizeof(struct hidpp_report));
244 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
245 response->rap.sub_id == HIDPP_ERROR) {
246 ret = response->rap.params[1];
247 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
251 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
252 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
253 response->fap.feature_index == HIDPP20_ERROR) {
254 ret = response->fap.params[1];
255 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
260 mutex_unlock(&hidpp->send_mutex);
265 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
266 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
267 struct hidpp_report *response)
269 struct hidpp_report *message;
272 if (param_count > sizeof(message->fap.params))
275 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
279 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
280 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
282 message->report_id = REPORT_ID_HIDPP_LONG;
283 message->fap.feature_index = feat_index;
284 message->fap.funcindex_clientid = funcindex_clientid;
285 memcpy(&message->fap.params, params, param_count);
287 ret = hidpp_send_message_sync(hidpp, message, response);
292 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
293 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
294 struct hidpp_report *response)
296 struct hidpp_report *message;
300 case REPORT_ID_HIDPP_SHORT:
301 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
303 case REPORT_ID_HIDPP_LONG:
304 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
306 case REPORT_ID_HIDPP_VERY_LONG:
307 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
313 if (param_count > max_count)
316 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
319 message->report_id = report_id;
320 message->rap.sub_id = sub_id;
321 message->rap.reg_address = reg_address;
322 memcpy(&message->rap.params, params, param_count);
324 ret = hidpp_send_message_sync(hidpp_dev, message, response);
329 static void delayed_work_cb(struct work_struct *work)
331 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
333 hidpp_connect_event(hidpp);
336 static inline bool hidpp_match_answer(struct hidpp_report *question,
337 struct hidpp_report *answer)
339 return (answer->fap.feature_index == question->fap.feature_index) &&
340 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
343 static inline bool hidpp_match_error(struct hidpp_report *question,
344 struct hidpp_report *answer)
346 return ((answer->rap.sub_id == HIDPP_ERROR) ||
347 (answer->fap.feature_index == HIDPP20_ERROR)) &&
348 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
349 (answer->fap.params[0] == question->fap.funcindex_clientid);
352 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
354 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
355 (report->rap.sub_id == 0x41);
359 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
361 static void hidpp_prefix_name(char **name, int name_length)
363 #define PREFIX_LENGTH 9 /* "Logitech " */
368 if (name_length > PREFIX_LENGTH &&
369 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
370 /* The prefix has is already in the name */
373 new_length = PREFIX_LENGTH + name_length;
374 new_name = kzalloc(new_length, GFP_KERNEL);
378 snprintf(new_name, new_length, "Logitech %s", *name);
385 /* -------------------------------------------------------------------------- */
386 /* HIDP++ 1.0 commands */
387 /* -------------------------------------------------------------------------- */
389 #define HIDPP_SET_REGISTER 0x80
390 #define HIDPP_GET_REGISTER 0x81
391 #define HIDPP_SET_LONG_REGISTER 0x82
392 #define HIDPP_GET_LONG_REGISTER 0x83
394 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
395 #define DEVICE_NAME 0x40
397 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
399 struct hidpp_report response;
401 /* hid-logitech-dj is in charge of setting the right device index */
402 u8 params[1] = { DEVICE_NAME };
406 ret = hidpp_send_rap_command_sync(hidpp_dev,
407 REPORT_ID_HIDPP_SHORT,
408 HIDPP_GET_LONG_REGISTER,
409 HIDPP_REG_PAIRING_INFORMATION,
410 params, 1, &response);
414 len = response.rap.params[1];
416 if (2 + len > sizeof(response.rap.params))
419 name = kzalloc(len + 1, GFP_KERNEL);
423 memcpy(name, &response.rap.params[2], len);
425 /* include the terminating '\0' */
426 hidpp_prefix_name(&name, len + 1);
431 /* -------------------------------------------------------------------------- */
433 /* -------------------------------------------------------------------------- */
435 #define HIDPP_PAGE_ROOT 0x0000
436 #define HIDPP_PAGE_ROOT_IDX 0x00
438 #define CMD_ROOT_GET_FEATURE 0x01
439 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
441 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
442 u8 *feature_index, u8 *feature_type)
444 struct hidpp_report response;
446 u8 params[2] = { feature >> 8, feature & 0x00FF };
448 ret = hidpp_send_fap_command_sync(hidpp,
450 CMD_ROOT_GET_FEATURE,
451 params, 2, &response);
455 *feature_index = response.fap.params[0];
456 *feature_type = response.fap.params[1];
461 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
463 struct hidpp_report response;
466 ret = hidpp_send_fap_command_sync(hidpp,
468 CMD_ROOT_GET_PROTOCOL_VERSION,
471 if (ret == HIDPP_ERROR_INVALID_SUBID) {
472 hidpp->protocol_major = 1;
473 hidpp->protocol_minor = 0;
477 /* the device might not be connected */
478 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
482 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
489 hidpp->protocol_major = response.fap.params[0];
490 hidpp->protocol_minor = response.fap.params[1];
495 static bool hidpp_is_connected(struct hidpp_device *hidpp)
499 ret = hidpp_root_get_protocol_version(hidpp);
501 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
502 hidpp->protocol_major, hidpp->protocol_minor);
506 /* -------------------------------------------------------------------------- */
507 /* 0x0005: GetDeviceNameType */
508 /* -------------------------------------------------------------------------- */
510 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
512 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
513 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
514 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
516 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
517 u8 feature_index, u8 *nameLength)
519 struct hidpp_report response;
522 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
523 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
526 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
533 *nameLength = response.fap.params[0];
538 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
539 u8 feature_index, u8 char_index, char *device_name, int len_buf)
541 struct hidpp_report response;
545 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
546 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
550 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
557 switch (response.report_id) {
558 case REPORT_ID_HIDPP_VERY_LONG:
559 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
561 case REPORT_ID_HIDPP_LONG:
562 count = HIDPP_REPORT_LONG_LENGTH - 4;
564 case REPORT_ID_HIDPP_SHORT:
565 count = HIDPP_REPORT_SHORT_LENGTH - 4;
574 for (i = 0; i < count; i++)
575 device_name[i] = response.fap.params[i];
580 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
589 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
590 &feature_index, &feature_type);
594 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
599 name = kzalloc(__name_length + 1, GFP_KERNEL);
603 while (index < __name_length) {
604 ret = hidpp_devicenametype_get_device_name(hidpp,
605 feature_index, index, name + index,
606 __name_length - index);
614 /* include the terminating '\0' */
615 hidpp_prefix_name(&name, __name_length + 1);
620 /* -------------------------------------------------------------------------- */
621 /* 0x1000: Battery level status */
622 /* -------------------------------------------------------------------------- */
624 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
626 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
627 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
629 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
631 static int hidpp20_batterylevel_map_status_level(u8 data[3], int *level,
638 *next_level = data[1];
640 /* When discharging, we can rely on the device reported level.
641 * For all other states the device reports level 0 (unknown). Make up
645 case 0: /* discharging (in use) */
646 status = POWER_SUPPLY_STATUS_DISCHARGING;
649 case 1: /* recharging */
650 status = POWER_SUPPLY_STATUS_CHARGING;
653 case 2: /* charge in final stage */
654 status = POWER_SUPPLY_STATUS_CHARGING;
657 case 3: /* charge complete */
658 status = POWER_SUPPLY_STATUS_FULL;
659 level_override = 100;
661 case 4: /* recharging below optimal speed */
662 status = POWER_SUPPLY_STATUS_CHARGING;
665 /* 5 = invalid battery type
667 7 = other charging error */
669 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
674 if (level_override != 0 && *level == 0)
675 *level = level_override;
680 static int hidpp20_batterylevel_get_battery_level(struct hidpp_device *hidpp,
686 struct hidpp_report response;
688 u8 *params = (u8 *)response.fap.params;
690 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
691 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
694 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
701 *status = hidpp20_batterylevel_map_status_level(params, level,
707 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
711 int status, level, next_level;
713 if (hidpp->battery.feature_index == 0) {
714 ret = hidpp_root_get_feature(hidpp,
715 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
716 &hidpp->battery.feature_index,
722 ret = hidpp20_batterylevel_get_battery_level(hidpp,
723 hidpp->battery.feature_index,
724 &status, &level, &next_level);
728 hidpp->battery.status = status;
729 hidpp->battery.level = level;
734 static int hidpp20_battery_event(struct hidpp_device *hidpp,
737 struct hidpp_report *report = (struct hidpp_report *)data;
738 int status, level, next_level;
741 if (report->fap.feature_index != hidpp->battery.feature_index ||
742 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
745 status = hidpp20_batterylevel_map_status_level(report->fap.params,
746 &level, &next_level);
748 changed = level != hidpp->battery.level ||
749 status != hidpp->battery.status;
752 hidpp->battery.level = level;
753 hidpp->battery.status = status;
754 if (hidpp->battery.ps)
755 power_supply_changed(hidpp->battery.ps);
761 static enum power_supply_property hidpp_battery_props[] = {
762 POWER_SUPPLY_PROP_STATUS,
763 POWER_SUPPLY_PROP_CAPACITY,
766 static int hidpp_battery_get_property(struct power_supply *psy,
767 enum power_supply_property psp,
768 union power_supply_propval *val)
770 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
774 case POWER_SUPPLY_PROP_STATUS:
775 val->intval = hidpp->battery.status;
777 case POWER_SUPPLY_PROP_CAPACITY:
778 val->intval = hidpp->battery.level;
788 static int hidpp20_initialize_battery(struct hidpp_device *hidpp)
790 static atomic_t battery_no = ATOMIC_INIT(0);
791 struct power_supply_config cfg = { .drv_data = hidpp };
792 struct power_supply_desc *desc = &hidpp->battery.desc;
793 struct hidpp_battery *battery;
797 ret = hidpp20_query_battery_info(hidpp);
801 battery = &hidpp->battery;
803 n = atomic_inc_return(&battery_no) - 1;
804 desc->properties = hidpp_battery_props;
805 desc->num_properties = ARRAY_SIZE(hidpp_battery_props);
806 desc->get_property = hidpp_battery_get_property;
807 sprintf(battery->name, "hidpp_battery_%ld", n);
808 desc->name = battery->name;
809 desc->type = POWER_SUPPLY_TYPE_BATTERY;
810 desc->use_for_apm = 0;
812 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
815 if (IS_ERR(battery->ps))
816 return PTR_ERR(battery->ps);
818 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
823 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
827 if (hidpp->protocol_major >= 2) {
828 ret = hidpp20_initialize_battery(hidpp);
830 hidpp->quirks |= HIDPP_QUIRK_HIDPP20_BATTERY;
836 /* -------------------------------------------------------------------------- */
837 /* 0x6010: Touchpad FW items */
838 /* -------------------------------------------------------------------------- */
840 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
842 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
844 struct hidpp_touchpad_fw_items {
846 uint8_t desired_state;
852 * send a set state command to the device by reading the current items->state
853 * field. items is then filled with the current state.
855 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
857 struct hidpp_touchpad_fw_items *items)
859 struct hidpp_report response;
861 u8 *params = (u8 *)response.fap.params;
863 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
864 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
867 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
874 items->presence = params[0];
875 items->desired_state = params[1];
876 items->state = params[2];
877 items->persistent = params[3];
882 /* -------------------------------------------------------------------------- */
883 /* 0x6100: TouchPadRawXY */
884 /* -------------------------------------------------------------------------- */
886 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
888 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
889 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
891 #define EVENT_TOUCHPAD_RAW_XY 0x00
893 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
894 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
896 struct hidpp_touchpad_raw_info {
907 struct hidpp_touchpad_raw_xy_finger {
917 struct hidpp_touchpad_raw_xy {
919 struct hidpp_touchpad_raw_xy_finger fingers[2];
926 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
927 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
929 struct hidpp_report response;
931 u8 *params = (u8 *)response.fap.params;
933 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
934 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
937 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
944 raw_info->x_size = get_unaligned_be16(¶ms[0]);
945 raw_info->y_size = get_unaligned_be16(¶ms[2]);
946 raw_info->z_range = params[4];
947 raw_info->area_range = params[5];
948 raw_info->maxcontacts = params[7];
949 raw_info->origin = params[8];
950 /* res is given in unit per inch */
951 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
956 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
957 u8 feature_index, bool send_raw_reports,
958 bool sensor_enhanced_settings)
960 struct hidpp_report response;
965 * bit 1 - 16bit Z, no area
966 * bit 2 - enhanced sensitivity
967 * bit 3 - width, height (4 bits each) instead of area
968 * bit 4 - send raw + gestures (degrades smoothness)
969 * remaining bits - reserved
971 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
973 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
974 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
977 static void hidpp_touchpad_touch_event(u8 *data,
978 struct hidpp_touchpad_raw_xy_finger *finger)
980 u8 x_m = data[0] << 2;
981 u8 y_m = data[2] << 2;
983 finger->x = x_m << 6 | data[1];
984 finger->y = y_m << 6 | data[3];
986 finger->contact_type = data[0] >> 6;
987 finger->contact_status = data[2] >> 6;
990 finger->area = data[5];
991 finger->finger_id = data[6] >> 4;
994 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
995 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
997 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
998 raw_xy->end_of_frame = data[8] & 0x01;
999 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1000 raw_xy->finger_count = data[15] & 0x0f;
1001 raw_xy->button = (data[8] >> 2) & 0x01;
1003 if (raw_xy->finger_count) {
1004 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1005 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1009 /* -------------------------------------------------------------------------- */
1010 /* 0x8123: Force feedback support */
1011 /* -------------------------------------------------------------------------- */
1013 #define HIDPP_FF_GET_INFO 0x01
1014 #define HIDPP_FF_RESET_ALL 0x11
1015 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1016 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1017 #define HIDPP_FF_DESTROY_EFFECT 0x41
1018 #define HIDPP_FF_GET_APERTURE 0x51
1019 #define HIDPP_FF_SET_APERTURE 0x61
1020 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1021 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1023 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1024 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1025 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1026 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1028 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1029 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1030 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1031 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1032 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1033 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1034 #define HIDPP_FF_EFFECT_SPRING 0x06
1035 #define HIDPP_FF_EFFECT_DAMPER 0x07
1036 #define HIDPP_FF_EFFECT_FRICTION 0x08
1037 #define HIDPP_FF_EFFECT_INERTIA 0x09
1038 #define HIDPP_FF_EFFECT_RAMP 0x0A
1040 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1042 #define HIDPP_FF_EFFECTID_NONE -1
1043 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1045 #define HIDPP_FF_MAX_PARAMS 20
1046 #define HIDPP_FF_RESERVED_SLOTS 1
1048 struct hidpp_ff_private_data {
1049 struct hidpp_device *hidpp;
1057 struct workqueue_struct *wq;
1058 atomic_t workqueue_size;
1061 struct hidpp_ff_work_data {
1062 struct work_struct work;
1063 struct hidpp_ff_private_data *data;
1066 u8 params[HIDPP_FF_MAX_PARAMS];
1070 static const signed short hiddpp_ff_effects[] = {
1085 static const signed short hiddpp_ff_effects_v2[] = {
1092 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1093 HIDPP_FF_EFFECT_SPRING,
1094 HIDPP_FF_EFFECT_FRICTION,
1095 HIDPP_FF_EFFECT_DAMPER,
1096 HIDPP_FF_EFFECT_INERTIA
1099 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1107 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1111 for (i = 0; i < data->num_effects; i++)
1112 if (data->effect_ids[i] == effect_id)
1118 static void hidpp_ff_work_handler(struct work_struct *w)
1120 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1121 struct hidpp_ff_private_data *data = wd->data;
1122 struct hidpp_report response;
1126 /* add slot number if needed */
1127 switch (wd->effect_id) {
1128 case HIDPP_FF_EFFECTID_AUTOCENTER:
1129 wd->params[0] = data->slot_autocenter;
1131 case HIDPP_FF_EFFECTID_NONE:
1132 /* leave slot as zero */
1135 /* find current slot for effect */
1136 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1140 /* send command and wait for reply */
1141 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1142 wd->command, wd->params, wd->size, &response);
1145 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1149 /* parse return data */
1150 switch (wd->command) {
1151 case HIDPP_FF_DOWNLOAD_EFFECT:
1152 slot = response.fap.params[0];
1153 if (slot > 0 && slot <= data->num_effects) {
1154 if (wd->effect_id >= 0)
1155 /* regular effect uploaded */
1156 data->effect_ids[slot-1] = wd->effect_id;
1157 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1158 /* autocenter spring uploaded */
1159 data->slot_autocenter = slot;
1162 case HIDPP_FF_DESTROY_EFFECT:
1163 if (wd->effect_id >= 0)
1164 /* regular effect destroyed */
1165 data->effect_ids[wd->params[0]-1] = -1;
1166 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1167 /* autocenter spring destoyed */
1168 data->slot_autocenter = 0;
1170 case HIDPP_FF_SET_GLOBAL_GAINS:
1171 data->gain = (wd->params[0] << 8) + wd->params[1];
1173 case HIDPP_FF_SET_APERTURE:
1174 data->range = (wd->params[0] << 8) + wd->params[1];
1177 /* no action needed */
1182 atomic_dec(&data->workqueue_size);
1186 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1188 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1194 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1197 wd->effect_id = effect_id;
1198 wd->command = command;
1200 memcpy(wd->params, params, size);
1202 atomic_inc(&data->workqueue_size);
1203 queue_work(data->wq, &wd->work);
1205 /* warn about excessive queue size */
1206 s = atomic_read(&data->workqueue_size);
1207 if (s >= 20 && s % 20 == 0)
1208 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1213 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1215 struct hidpp_ff_private_data *data = dev->ff->private;
1220 /* set common parameters */
1221 params[2] = effect->replay.length >> 8;
1222 params[3] = effect->replay.length & 255;
1223 params[4] = effect->replay.delay >> 8;
1224 params[5] = effect->replay.delay & 255;
1226 switch (effect->type) {
1228 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1229 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1230 params[6] = force >> 8;
1231 params[7] = force & 255;
1232 params[8] = effect->u.constant.envelope.attack_level >> 7;
1233 params[9] = effect->u.constant.envelope.attack_length >> 8;
1234 params[10] = effect->u.constant.envelope.attack_length & 255;
1235 params[11] = effect->u.constant.envelope.fade_level >> 7;
1236 params[12] = effect->u.constant.envelope.fade_length >> 8;
1237 params[13] = effect->u.constant.envelope.fade_length & 255;
1239 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1240 effect->u.constant.level,
1241 effect->direction, force);
1242 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1243 effect->u.constant.envelope.attack_level,
1244 effect->u.constant.envelope.attack_length,
1245 effect->u.constant.envelope.fade_level,
1246 effect->u.constant.envelope.fade_length);
1250 switch (effect->u.periodic.waveform) {
1252 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1255 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1258 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1261 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1264 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1267 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1270 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1271 params[6] = effect->u.periodic.magnitude >> 8;
1272 params[7] = effect->u.periodic.magnitude & 255;
1273 params[8] = effect->u.periodic.offset >> 8;
1274 params[9] = effect->u.periodic.offset & 255;
1275 params[10] = effect->u.periodic.period >> 8;
1276 params[11] = effect->u.periodic.period & 255;
1277 params[12] = effect->u.periodic.phase >> 8;
1278 params[13] = effect->u.periodic.phase & 255;
1279 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1280 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1281 params[16] = effect->u.periodic.envelope.attack_length & 255;
1282 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1283 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1284 params[19] = effect->u.periodic.envelope.fade_length & 255;
1286 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1287 effect->u.periodic.magnitude, effect->direction,
1288 effect->u.periodic.offset,
1289 effect->u.periodic.period,
1290 effect->u.periodic.phase);
1291 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1292 effect->u.periodic.envelope.attack_level,
1293 effect->u.periodic.envelope.attack_length,
1294 effect->u.periodic.envelope.fade_level,
1295 effect->u.periodic.envelope.fade_length);
1299 params[1] = HIDPP_FF_EFFECT_RAMP;
1300 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1301 params[6] = force >> 8;
1302 params[7] = force & 255;
1303 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1304 params[8] = force >> 8;
1305 params[9] = force & 255;
1306 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1307 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1308 params[12] = effect->u.ramp.envelope.attack_length & 255;
1309 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1310 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1311 params[15] = effect->u.ramp.envelope.fade_length & 255;
1313 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1314 effect->u.ramp.start_level,
1315 effect->u.ramp.end_level,
1316 effect->direction, force);
1317 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1318 effect->u.ramp.envelope.attack_level,
1319 effect->u.ramp.envelope.attack_length,
1320 effect->u.ramp.envelope.fade_level,
1321 effect->u.ramp.envelope.fade_length);
1327 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1328 params[6] = effect->u.condition[0].left_saturation >> 9;
1329 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1330 params[8] = effect->u.condition[0].left_coeff >> 8;
1331 params[9] = effect->u.condition[0].left_coeff & 255;
1332 params[10] = effect->u.condition[0].deadband >> 9;
1333 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1334 params[12] = effect->u.condition[0].center >> 8;
1335 params[13] = effect->u.condition[0].center & 255;
1336 params[14] = effect->u.condition[0].right_coeff >> 8;
1337 params[15] = effect->u.condition[0].right_coeff & 255;
1338 params[16] = effect->u.condition[0].right_saturation >> 9;
1339 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1341 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1342 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1343 effect->u.condition[0].left_coeff,
1344 effect->u.condition[0].left_saturation,
1345 effect->u.condition[0].right_coeff,
1346 effect->u.condition[0].right_saturation);
1347 dbg_hid(" deadband=%d, center=%d\n",
1348 effect->u.condition[0].deadband,
1349 effect->u.condition[0].center);
1352 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1356 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1359 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1361 struct hidpp_ff_private_data *data = dev->ff->private;
1364 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1366 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1368 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1371 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1373 struct hidpp_ff_private_data *data = dev->ff->private;
1376 dbg_hid("Erasing effect %d.\n", effect_id);
1378 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1381 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1383 struct hidpp_ff_private_data *data = dev->ff->private;
1386 dbg_hid("Setting autocenter to %d.\n", magnitude);
1388 /* start a standard spring effect */
1389 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1390 /* zero delay and duration */
1391 params[2] = params[3] = params[4] = params[5] = 0;
1392 /* set coeff to 25% of saturation */
1393 params[8] = params[14] = magnitude >> 11;
1394 params[9] = params[15] = (magnitude >> 3) & 255;
1395 params[6] = params[16] = magnitude >> 9;
1396 params[7] = params[17] = (magnitude >> 1) & 255;
1397 /* zero deadband and center */
1398 params[10] = params[11] = params[12] = params[13] = 0;
1400 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1403 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1405 struct hidpp_ff_private_data *data = dev->ff->private;
1408 dbg_hid("Setting gain to %d.\n", gain);
1410 params[0] = gain >> 8;
1411 params[1] = gain & 255;
1412 params[2] = 0; /* no boost */
1415 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1418 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1420 struct hid_device *hid = to_hid_device(dev);
1421 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1422 struct input_dev *idev = hidinput->input;
1423 struct hidpp_ff_private_data *data = idev->ff->private;
1425 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1428 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1430 struct hid_device *hid = to_hid_device(dev);
1431 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1432 struct input_dev *idev = hidinput->input;
1433 struct hidpp_ff_private_data *data = idev->ff->private;
1435 int range = simple_strtoul(buf, NULL, 10);
1437 range = clamp(range, 180, 900);
1439 params[0] = range >> 8;
1440 params[1] = range & 0x00FF;
1442 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1447 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1449 static void hidpp_ff_destroy(struct ff_device *ff)
1451 struct hidpp_ff_private_data *data = ff->private;
1453 kfree(data->effect_ids);
1456 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
1458 struct hid_device *hid = hidpp->hid_dev;
1459 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1460 struct input_dev *dev = hidinput->input;
1461 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1462 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1463 struct ff_device *ff;
1464 struct hidpp_report response;
1465 struct hidpp_ff_private_data *data;
1466 int error, j, num_slots;
1470 hid_err(hid, "Struct input_dev not set!\n");
1474 /* Get firmware release */
1475 version = bcdDevice & 255;
1477 /* Set supported force feedback capabilities */
1478 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1479 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1481 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1482 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1484 /* Read number of slots available in device */
1485 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1486 HIDPP_FF_GET_INFO, NULL, 0, &response);
1490 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1495 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1497 error = input_ff_create(dev, num_slots);
1500 hid_err(dev, "Failed to create FF device!\n");
1504 data = kzalloc(sizeof(*data), GFP_KERNEL);
1507 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1508 if (!data->effect_ids) {
1512 data->hidpp = hidpp;
1513 data->feature_index = feature_index;
1514 data->version = version;
1515 data->slot_autocenter = 0;
1516 data->num_effects = num_slots;
1517 for (j = 0; j < num_slots; j++)
1518 data->effect_ids[j] = -1;
1523 ff->upload = hidpp_ff_upload_effect;
1524 ff->erase = hidpp_ff_erase_effect;
1525 ff->playback = hidpp_ff_playback;
1526 ff->set_gain = hidpp_ff_set_gain;
1527 ff->set_autocenter = hidpp_ff_set_autocenter;
1528 ff->destroy = hidpp_ff_destroy;
1531 /* reset all forces */
1532 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1533 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1535 /* Read current Range */
1536 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1537 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1539 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1540 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1542 /* Create sysfs interface */
1543 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1545 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1547 /* Read the current gain values */
1548 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1549 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1551 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1552 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1553 /* ignore boost value at response.fap.params[2] */
1555 /* init the hardware command queue */
1556 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1557 atomic_set(&data->workqueue_size, 0);
1559 /* initialize with zero autocenter to get wheel in usable state */
1560 hidpp_ff_set_autocenter(dev, 0);
1562 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1567 static int hidpp_ff_deinit(struct hid_device *hid)
1569 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1570 struct input_dev *dev = hidinput->input;
1571 struct hidpp_ff_private_data *data;
1574 hid_err(hid, "Struct input_dev not found!\n");
1578 hid_info(hid, "Unloading HID++ force feedback.\n");
1579 data = dev->ff->private;
1581 hid_err(hid, "Private data not found!\n");
1585 destroy_workqueue(data->wq);
1586 device_remove_file(&hid->dev, &dev_attr_range);
1592 /* ************************************************************************** */
1594 /* Device Support */
1596 /* ************************************************************************** */
1598 /* -------------------------------------------------------------------------- */
1599 /* Touchpad HID++ devices */
1600 /* -------------------------------------------------------------------------- */
1602 #define WTP_MANUAL_RESOLUTION 39
1605 struct input_dev *input;
1608 u8 mt_feature_index;
1609 u8 button_feature_index;
1612 unsigned int resolution;
1615 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1616 struct hid_field *field, struct hid_usage *usage,
1617 unsigned long **bit, int *max)
1622 static void wtp_populate_input(struct hidpp_device *hidpp,
1623 struct input_dev *input_dev, bool origin_is_hid_core)
1625 struct wtp_data *wd = hidpp->private_data;
1627 __set_bit(EV_ABS, input_dev->evbit);
1628 __set_bit(EV_KEY, input_dev->evbit);
1629 __clear_bit(EV_REL, input_dev->evbit);
1630 __clear_bit(EV_LED, input_dev->evbit);
1632 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
1633 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
1634 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
1635 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
1637 /* Max pressure is not given by the devices, pick one */
1638 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
1640 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
1642 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
1643 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
1645 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1647 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
1648 INPUT_MT_DROP_UNUSED);
1650 wd->input = input_dev;
1653 static void wtp_touch_event(struct wtp_data *wd,
1654 struct hidpp_touchpad_raw_xy_finger *touch_report)
1658 if (!touch_report->finger_id || touch_report->contact_type)
1659 /* no actual data */
1662 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
1664 input_mt_slot(wd->input, slot);
1665 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
1666 touch_report->contact_status);
1667 if (touch_report->contact_status) {
1668 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
1670 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
1671 wd->flip_y ? wd->y_size - touch_report->y :
1673 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
1674 touch_report->area);
1678 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
1679 struct hidpp_touchpad_raw_xy *raw)
1681 struct wtp_data *wd = hidpp->private_data;
1684 for (i = 0; i < 2; i++)
1685 wtp_touch_event(wd, &(raw->fingers[i]));
1687 if (raw->end_of_frame &&
1688 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
1689 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
1691 if (raw->end_of_frame || raw->finger_count <= 2) {
1692 input_mt_sync_frame(wd->input);
1693 input_sync(wd->input);
1697 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
1699 struct wtp_data *wd = hidpp->private_data;
1700 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
1701 (data[7] >> 4) * (data[7] >> 4)) / 2;
1702 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
1703 (data[13] >> 4) * (data[13] >> 4)) / 2;
1704 struct hidpp_touchpad_raw_xy raw = {
1705 .timestamp = data[1],
1709 .contact_status = !!data[7],
1710 .x = get_unaligned_le16(&data[3]),
1711 .y = get_unaligned_le16(&data[5]),
1714 .finger_id = data[2],
1717 .contact_status = !!data[13],
1718 .x = get_unaligned_le16(&data[9]),
1719 .y = get_unaligned_le16(&data[11]),
1722 .finger_id = data[8],
1725 .finger_count = wd->maxcontacts,
1727 .end_of_frame = (data[0] >> 7) == 0,
1728 .button = data[0] & 0x01,
1731 wtp_send_raw_xy_event(hidpp, &raw);
1736 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
1738 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1739 struct wtp_data *wd = hidpp->private_data;
1740 struct hidpp_report *report = (struct hidpp_report *)data;
1741 struct hidpp_touchpad_raw_xy raw;
1743 if (!wd || !wd->input)
1749 hid_err(hdev, "Received HID report of bad size (%d)",
1753 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
1754 input_event(wd->input, EV_KEY, BTN_LEFT,
1755 !!(data[1] & 0x01));
1756 input_event(wd->input, EV_KEY, BTN_RIGHT,
1757 !!(data[1] & 0x02));
1758 input_sync(wd->input);
1763 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
1765 case REPORT_ID_HIDPP_LONG:
1766 /* size is already checked in hidpp_raw_event. */
1767 if ((report->fap.feature_index != wd->mt_feature_index) ||
1768 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
1770 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
1772 wtp_send_raw_xy_event(hidpp, &raw);
1779 static int wtp_get_config(struct hidpp_device *hidpp)
1781 struct wtp_data *wd = hidpp->private_data;
1782 struct hidpp_touchpad_raw_info raw_info = {0};
1786 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
1787 &wd->mt_feature_index, &feature_type);
1789 /* means that the device is not powered up */
1792 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
1797 wd->x_size = raw_info.x_size;
1798 wd->y_size = raw_info.y_size;
1799 wd->maxcontacts = raw_info.maxcontacts;
1800 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
1801 wd->resolution = raw_info.res;
1802 if (!wd->resolution)
1803 wd->resolution = WTP_MANUAL_RESOLUTION;
1808 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
1810 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1811 struct wtp_data *wd;
1813 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
1818 hidpp->private_data = wd;
1823 static int wtp_connect(struct hid_device *hdev, bool connected)
1825 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1826 struct wtp_data *wd = hidpp->private_data;
1833 ret = wtp_get_config(hidpp);
1835 hid_err(hdev, "Can not get wtp config: %d\n", ret);
1840 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
1844 /* ------------------------------------------------------------------------- */
1845 /* Logitech M560 devices */
1846 /* ------------------------------------------------------------------------- */
1849 * Logitech M560 protocol overview
1851 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1852 * the sides buttons are pressed, it sends some keyboard keys events
1853 * instead of buttons ones.
1854 * To complicate things further, the middle button keys sequence
1855 * is different from the odd press and the even press.
1857 * forward button -> Super_R
1858 * backward button -> Super_L+'d' (press only)
1859 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1860 * 2nd time: left-click (press only)
1861 * NB: press-only means that when the button is pressed, the
1862 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1863 * together sequentially; instead when the button is released, no event is
1867 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1868 * the mouse reacts differently:
1869 * - it never sends a keyboard key event
1870 * - for the three mouse button it sends:
1871 * middle button press 11<xx>0a 3500af00...
1872 * side 1 button (forward) press 11<xx>0a 3500b000...
1873 * side 2 button (backward) press 11<xx>0a 3500ae00...
1874 * middle/side1/side2 button release 11<xx>0a 35000000...
1877 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1879 struct m560_private_data {
1880 struct input_dev *input;
1883 /* how buttons are mapped in the report */
1884 #define M560_MOUSE_BTN_LEFT 0x01
1885 #define M560_MOUSE_BTN_RIGHT 0x02
1886 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1887 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1889 #define M560_SUB_ID 0x0a
1890 #define M560_BUTTON_MODE_REGISTER 0x35
1892 static int m560_send_config_command(struct hid_device *hdev, bool connected)
1894 struct hidpp_report response;
1895 struct hidpp_device *hidpp_dev;
1897 hidpp_dev = hid_get_drvdata(hdev);
1902 return hidpp_send_rap_command_sync(
1904 REPORT_ID_HIDPP_SHORT,
1906 M560_BUTTON_MODE_REGISTER,
1907 (u8 *)m560_config_parameter,
1908 sizeof(m560_config_parameter),
1913 static int m560_allocate(struct hid_device *hdev)
1915 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1916 struct m560_private_data *d;
1918 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1923 hidpp->private_data = d;
1928 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1930 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1931 struct m560_private_data *mydata = hidpp->private_data;
1934 if (!mydata || !mydata->input) {
1935 hid_err(hdev, "error in parameter\n");
1940 hid_err(hdev, "error in report\n");
1944 if (data[0] == REPORT_ID_HIDPP_LONG &&
1945 data[2] == M560_SUB_ID && data[6] == 0x00) {
1947 * m560 mouse report for middle, forward and backward button
1950 * data[1] = device-id
1952 * data[5] = 0xaf -> middle
1955 * 0x00 -> release all
1961 input_report_key(mydata->input, BTN_MIDDLE, 1);
1964 input_report_key(mydata->input, BTN_FORWARD, 1);
1967 input_report_key(mydata->input, BTN_BACK, 1);
1970 input_report_key(mydata->input, BTN_BACK, 0);
1971 input_report_key(mydata->input, BTN_FORWARD, 0);
1972 input_report_key(mydata->input, BTN_MIDDLE, 0);
1975 hid_err(hdev, "error in report\n");
1978 input_sync(mydata->input);
1980 } else if (data[0] == 0x02) {
1982 * Logitech M560 mouse report
1984 * data[0] = type (0x02)
1985 * data[1..2] = buttons
1992 input_report_key(mydata->input, BTN_LEFT,
1993 !!(data[1] & M560_MOUSE_BTN_LEFT));
1994 input_report_key(mydata->input, BTN_RIGHT,
1995 !!(data[1] & M560_MOUSE_BTN_RIGHT));
1997 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
1998 input_report_rel(mydata->input, REL_HWHEEL, -1);
1999 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2000 input_report_rel(mydata->input, REL_HWHEEL, 1);
2002 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2003 input_report_rel(mydata->input, REL_X, v);
2005 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2006 input_report_rel(mydata->input, REL_Y, v);
2008 v = hid_snto32(data[6], 8);
2009 input_report_rel(mydata->input, REL_WHEEL, v);
2011 input_sync(mydata->input);
2017 static void m560_populate_input(struct hidpp_device *hidpp,
2018 struct input_dev *input_dev, bool origin_is_hid_core)
2020 struct m560_private_data *mydata = hidpp->private_data;
2022 mydata->input = input_dev;
2024 __set_bit(EV_KEY, mydata->input->evbit);
2025 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2026 __set_bit(BTN_RIGHT, mydata->input->keybit);
2027 __set_bit(BTN_LEFT, mydata->input->keybit);
2028 __set_bit(BTN_BACK, mydata->input->keybit);
2029 __set_bit(BTN_FORWARD, mydata->input->keybit);
2031 __set_bit(EV_REL, mydata->input->evbit);
2032 __set_bit(REL_X, mydata->input->relbit);
2033 __set_bit(REL_Y, mydata->input->relbit);
2034 __set_bit(REL_WHEEL, mydata->input->relbit);
2035 __set_bit(REL_HWHEEL, mydata->input->relbit);
2038 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2039 struct hid_field *field, struct hid_usage *usage,
2040 unsigned long **bit, int *max)
2045 /* ------------------------------------------------------------------------- */
2046 /* Logitech K400 devices */
2047 /* ------------------------------------------------------------------------- */
2050 * The Logitech K400 keyboard has an embedded touchpad which is seen
2051 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2052 * tap-to-click but the setting is not remembered accross reset, annoying some
2055 * We can toggle this feature from the host by using the feature 0x6010:
2059 struct k400_private_data {
2063 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2065 struct k400_private_data *k400 = hidpp->private_data;
2066 struct hidpp_touchpad_fw_items items = {};
2070 if (!k400->feature_index) {
2071 ret = hidpp_root_get_feature(hidpp,
2072 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2073 &k400->feature_index, &feature_type);
2075 /* means that the device is not powered up */
2079 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2086 static int k400_allocate(struct hid_device *hdev)
2088 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2089 struct k400_private_data *k400;
2091 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2096 hidpp->private_data = k400;
2101 static int k400_connect(struct hid_device *hdev, bool connected)
2103 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2108 if (!disable_tap_to_click)
2111 return k400_disable_tap_to_click(hidpp);
2114 /* ------------------------------------------------------------------------- */
2115 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2116 /* ------------------------------------------------------------------------- */
2118 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2120 static int g920_get_config(struct hidpp_device *hidpp)
2126 /* Find feature and store for later use */
2127 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2128 &feature_index, &feature_type);
2132 ret = hidpp_ff_init(hidpp, feature_index);
2134 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2140 /* -------------------------------------------------------------------------- */
2141 /* Generic HID++ devices */
2142 /* -------------------------------------------------------------------------- */
2144 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2145 struct hid_field *field, struct hid_usage *usage,
2146 unsigned long **bit, int *max)
2148 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2150 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2151 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2152 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2153 field->application != HID_GD_MOUSE)
2154 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2159 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2160 struct hid_field *field, struct hid_usage *usage,
2161 unsigned long **bit, int *max)
2163 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2165 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2166 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2167 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2168 usage->code == ABS_Y || usage->code == ABS_Z ||
2169 usage->code == ABS_RZ)) {
2170 field->application = HID_GD_MULTIAXIS;
2178 static void hidpp_populate_input(struct hidpp_device *hidpp,
2179 struct input_dev *input, bool origin_is_hid_core)
2181 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2182 wtp_populate_input(hidpp, input, origin_is_hid_core);
2183 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2184 m560_populate_input(hidpp, input, origin_is_hid_core);
2187 static int hidpp_input_configured(struct hid_device *hdev,
2188 struct hid_input *hidinput)
2190 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2191 struct input_dev *input = hidinput->input;
2193 hidpp_populate_input(hidpp, input, true);
2198 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2201 struct hidpp_report *question = hidpp->send_receive_buf;
2202 struct hidpp_report *answer = hidpp->send_receive_buf;
2203 struct hidpp_report *report = (struct hidpp_report *)data;
2206 * If the mutex is locked then we have a pending answer from a
2207 * previously sent command.
2209 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2211 * Check for a correct hidpp20 answer or the corresponding
2214 if (hidpp_match_answer(question, report) ||
2215 hidpp_match_error(question, report)) {
2217 hidpp->answer_available = true;
2218 wake_up(&hidpp->wait);
2220 * This was an answer to a command that this driver sent
2221 * We return 1 to hid-core to avoid forwarding the
2222 * command upstream as it has been treated by the driver
2229 if (unlikely(hidpp_report_is_connect_event(report))) {
2230 atomic_set(&hidpp->connected,
2231 !(report->rap.params[0] & (1 << 6)));
2232 if (schedule_work(&hidpp->work) == 0)
2233 dbg_hid("%s: connect event already queued\n", __func__);
2240 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2243 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2246 /* Generic HID++ processing. */
2248 case REPORT_ID_HIDPP_VERY_LONG:
2249 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2250 hid_err(hdev, "received hid++ report of bad size (%d)",
2254 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2256 case REPORT_ID_HIDPP_LONG:
2257 if (size != HIDPP_REPORT_LONG_LENGTH) {
2258 hid_err(hdev, "received hid++ report of bad size (%d)",
2262 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2264 case REPORT_ID_HIDPP_SHORT:
2265 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2266 hid_err(hdev, "received hid++ report of bad size (%d)",
2270 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2274 /* If no report is available for further processing, skip calling
2275 * raw_event of subclasses. */
2279 if (hidpp->quirks & HIDPP_QUIRK_HIDPP20_BATTERY) {
2280 ret = hidpp20_battery_event(hidpp, data, size);
2285 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2286 return wtp_raw_event(hdev, data, size);
2287 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2288 return m560_raw_event(hdev, data, size);
2293 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
2295 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2300 * the device is connected through an Unifying receiver, and
2301 * might not be already connected.
2302 * Ask the receiver for its name.
2304 name = hidpp_get_unifying_name(hidpp);
2306 name = hidpp_get_device_name(hidpp);
2309 hid_err(hdev, "unable to retrieve the name of the device");
2311 dbg_hid("HID++: Got name: %s\n", name);
2312 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
2318 static int hidpp_input_open(struct input_dev *dev)
2320 struct hid_device *hid = input_get_drvdata(dev);
2322 return hid_hw_open(hid);
2325 static void hidpp_input_close(struct input_dev *dev)
2327 struct hid_device *hid = input_get_drvdata(dev);
2332 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2334 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
2335 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2340 input_set_drvdata(input_dev, hdev);
2341 input_dev->open = hidpp_input_open;
2342 input_dev->close = hidpp_input_close;
2344 input_dev->name = hidpp->name;
2345 input_dev->phys = hdev->phys;
2346 input_dev->uniq = hdev->uniq;
2347 input_dev->id.bustype = hdev->bus;
2348 input_dev->id.vendor = hdev->vendor;
2349 input_dev->id.product = hdev->product;
2350 input_dev->id.version = hdev->version;
2351 input_dev->dev.parent = &hdev->dev;
2356 static void hidpp_connect_event(struct hidpp_device *hidpp)
2358 struct hid_device *hdev = hidpp->hid_dev;
2360 bool connected = atomic_read(&hidpp->connected);
2361 struct input_dev *input;
2362 char *name, *devm_name;
2364 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2365 ret = wtp_connect(hdev, connected);
2368 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2369 ret = m560_send_config_command(hdev, connected);
2372 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2373 ret = k400_connect(hdev, connected);
2378 if (!connected || hidpp->delayed_input)
2381 /* the device is already connected, we can ask for its name and
2383 if (!hidpp->protocol_major) {
2384 ret = !hidpp_is_connected(hidpp);
2386 hid_err(hdev, "Can not get the protocol version.\n");
2389 hid_info(hdev, "HID++ %u.%u device connected.\n",
2390 hidpp->protocol_major, hidpp->protocol_minor);
2393 hidpp_initialize_battery(hidpp);
2395 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
2396 /* if HID created the input nodes for us, we can stop now */
2399 if (!hidpp->name || hidpp->name == hdev->name) {
2400 name = hidpp_get_device_name(hidpp);
2403 "unable to retrieve the name of the device");
2407 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2412 hidpp->name = devm_name;
2415 input = hidpp_allocate_input(hdev);
2417 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2421 hidpp_populate_input(hidpp, input, false);
2423 ret = input_register_device(input);
2425 input_free_device(input);
2427 hidpp->delayed_input = input;
2430 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2432 struct hidpp_device *hidpp;
2435 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2437 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2442 hidpp->hid_dev = hdev;
2443 hidpp->name = hdev->name;
2444 hid_set_drvdata(hdev, hidpp);
2446 hidpp->quirks = id->driver_data;
2448 if (disable_raw_mode) {
2449 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
2450 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
2453 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2454 ret = wtp_allocate(hdev, id);
2457 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2458 ret = m560_allocate(hdev);
2461 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2462 ret = k400_allocate(hdev);
2467 INIT_WORK(&hidpp->work, delayed_work_cb);
2468 mutex_init(&hidpp->send_mutex);
2469 init_waitqueue_head(&hidpp->wait);
2471 ret = hid_parse(hdev);
2473 hid_err(hdev, "%s:parse failed\n", __func__);
2474 goto hid_parse_fail;
2477 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2478 connect_mask &= ~HID_CONNECT_HIDINPUT;
2480 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2481 ret = hid_hw_start(hdev, connect_mask);
2483 hid_err(hdev, "hw start failed\n");
2484 goto hid_hw_start_fail;
2486 ret = hid_hw_open(hdev);
2488 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
2491 goto hid_hw_start_fail;
2496 /* Allow incoming packets */
2497 hid_device_io_start(hdev);
2499 connected = hidpp_is_connected(hidpp);
2500 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
2503 hid_err(hdev, "Device not connected");
2504 goto hid_hw_open_failed;
2507 hid_info(hdev, "HID++ %u.%u device connected.\n",
2508 hidpp->protocol_major, hidpp->protocol_minor);
2511 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
2512 atomic_set(&hidpp->connected, connected);
2514 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
2515 ret = wtp_get_config(hidpp);
2517 goto hid_hw_open_failed;
2518 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2519 ret = g920_get_config(hidpp);
2521 goto hid_hw_open_failed;
2524 /* Block incoming packets */
2525 hid_device_io_stop(hdev);
2527 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2528 ret = hid_hw_start(hdev, connect_mask);
2530 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
2531 goto hid_hw_start_fail;
2535 /* Allow incoming packets */
2536 hid_device_io_start(hdev);
2538 hidpp_connect_event(hidpp);
2543 hid_device_io_stop(hdev);
2544 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2550 cancel_work_sync(&hidpp->work);
2551 mutex_destroy(&hidpp->send_mutex);
2553 hid_set_drvdata(hdev, NULL);
2557 static void hidpp_remove(struct hid_device *hdev)
2559 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2561 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2562 hidpp_ff_deinit(hdev);
2566 cancel_work_sync(&hidpp->work);
2567 mutex_destroy(&hidpp->send_mutex);
2570 static const struct hid_device_id hidpp_devices[] = {
2571 { /* wireless touchpad */
2572 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2573 USB_VENDOR_ID_LOGITECH, 0x4011),
2574 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
2575 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
2576 { /* wireless touchpad T650 */
2577 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2578 USB_VENDOR_ID_LOGITECH, 0x4101),
2579 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
2580 { /* wireless touchpad T651 */
2581 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
2582 USB_DEVICE_ID_LOGITECH_T651),
2583 .driver_data = HIDPP_QUIRK_CLASS_WTP },
2584 { /* Mouse logitech M560 */
2585 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2586 USB_VENDOR_ID_LOGITECH, 0x402d),
2587 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
2588 { /* Keyboard logitech K400 */
2589 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2590 USB_VENDOR_ID_LOGITECH, 0x4024),
2591 .driver_data = HIDPP_QUIRK_CLASS_K400 },
2593 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2594 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
2596 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
2597 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
2601 MODULE_DEVICE_TABLE(hid, hidpp_devices);
2603 static struct hid_driver hidpp_driver = {
2604 .name = "logitech-hidpp-device",
2605 .id_table = hidpp_devices,
2606 .probe = hidpp_probe,
2607 .remove = hidpp_remove,
2608 .raw_event = hidpp_raw_event,
2609 .input_configured = hidpp_input_configured,
2610 .input_mapping = hidpp_input_mapping,
2611 .input_mapped = hidpp_input_mapped,
2614 module_hid_driver(hidpp_driver);