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)
59 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
61 /* bits 2..20 are reserved for classes */
62 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
63 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
64 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
65 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
66 #define HIDPP_QUIRK_UNIFYING BIT(25)
67 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
68 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
69 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
71 /* Convenience constant to check for any high-res support. */
72 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
73 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
74 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
76 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
78 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
79 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
80 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
81 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
84 * There are two hidpp protocols in use, the first version hidpp10 is known
85 * as register access protocol or RAP, the second version hidpp20 is known as
86 * feature access protocol or FAP
88 * Most older devices (including the Unifying usb receiver) use the RAP protocol
89 * where as most newer devices use the FAP protocol. Both protocols are
90 * compatible with the underlying transport, which could be usb, Unifiying, or
91 * bluetooth. The message lengths are defined by the hid vendor specific report
92 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
93 * the HIDPP_LONG report type (total message length 20 bytes)
95 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
96 * messages. The Unifying receiver itself responds to RAP messages (device index
97 * is 0xFF for the receiver), and all messages (short or long) with a device
98 * index between 1 and 6 are passed untouched to the corresponding paired
101 * The paired device can be RAP or FAP, it will receive the message untouched
102 * from the Unifiying receiver.
107 u8 funcindex_clientid;
108 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
114 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
117 struct hidpp_report {
123 u8 rawbytes[sizeof(struct fap)];
127 struct hidpp_battery {
129 u8 solar_feature_index;
130 struct power_supply_desc desc;
131 struct power_supply *ps;
139 struct hidpp_device {
140 struct hid_device *hid_dev;
141 struct mutex send_mutex;
142 void *send_receive_buf;
143 char *name; /* will never be NULL and should not be freed */
144 wait_queue_head_t wait;
145 bool answer_available;
151 struct work_struct work;
152 struct kfifo delayed_work_fifo;
154 struct input_dev *delayed_input;
156 unsigned long quirks;
157 unsigned long capabilities;
159 struct hidpp_battery battery;
160 struct hid_scroll_counter vertical_wheel_counter;
163 /* HID++ 1.0 error codes */
164 #define HIDPP_ERROR 0x8f
165 #define HIDPP_ERROR_SUCCESS 0x00
166 #define HIDPP_ERROR_INVALID_SUBID 0x01
167 #define HIDPP_ERROR_INVALID_ADRESS 0x02
168 #define HIDPP_ERROR_INVALID_VALUE 0x03
169 #define HIDPP_ERROR_CONNECT_FAIL 0x04
170 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
171 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
172 #define HIDPP_ERROR_BUSY 0x07
173 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
174 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
175 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
176 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
177 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
178 /* HID++ 2.0 error codes */
179 #define HIDPP20_ERROR 0xff
181 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
183 static int __hidpp_send_report(struct hid_device *hdev,
184 struct hidpp_report *hidpp_report)
186 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
187 int fields_count, ret;
189 hidpp = hid_get_drvdata(hdev);
191 switch (hidpp_report->report_id) {
192 case REPORT_ID_HIDPP_SHORT:
193 fields_count = HIDPP_REPORT_SHORT_LENGTH;
195 case REPORT_ID_HIDPP_LONG:
196 fields_count = HIDPP_REPORT_LONG_LENGTH;
198 case REPORT_ID_HIDPP_VERY_LONG:
199 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
206 * set the device_index as the receiver, it will be overwritten by
207 * hid_hw_request if needed
209 hidpp_report->device_index = 0xff;
211 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
212 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
214 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
215 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
219 return ret == fields_count ? 0 : -1;
223 * hidpp_send_message_sync() returns 0 in case of success, and something else
224 * in case of a failure.
225 * - If ' something else' is positive, that means that an error has been raised
226 * by the protocol itself.
227 * - If ' something else' is negative, that means that we had a classic error
228 * (-ENOMEM, -EPIPE, etc...)
230 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
231 struct hidpp_report *message,
232 struct hidpp_report *response)
236 mutex_lock(&hidpp->send_mutex);
238 hidpp->send_receive_buf = response;
239 hidpp->answer_available = false;
242 * So that we can later validate the answer when it arrives
245 *response = *message;
247 ret = __hidpp_send_report(hidpp->hid_dev, message);
250 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
251 memset(response, 0, sizeof(struct hidpp_report));
255 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
257 dbg_hid("%s:timeout waiting for response\n", __func__);
258 memset(response, 0, sizeof(struct hidpp_report));
262 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
263 response->rap.sub_id == HIDPP_ERROR) {
264 ret = response->rap.params[1];
265 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
269 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
270 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
271 response->fap.feature_index == HIDPP20_ERROR) {
272 ret = response->fap.params[1];
273 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
278 mutex_unlock(&hidpp->send_mutex);
283 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
284 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
285 struct hidpp_report *response)
287 struct hidpp_report *message;
290 if (param_count > sizeof(message->fap.params))
293 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
297 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
298 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
300 message->report_id = REPORT_ID_HIDPP_LONG;
301 message->fap.feature_index = feat_index;
302 message->fap.funcindex_clientid = funcindex_clientid;
303 memcpy(&message->fap.params, params, param_count);
305 ret = hidpp_send_message_sync(hidpp, message, response);
310 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
311 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
312 struct hidpp_report *response)
314 struct hidpp_report *message;
318 case REPORT_ID_HIDPP_SHORT:
319 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
321 case REPORT_ID_HIDPP_LONG:
322 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
324 case REPORT_ID_HIDPP_VERY_LONG:
325 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
331 if (param_count > max_count)
334 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
337 message->report_id = report_id;
338 message->rap.sub_id = sub_id;
339 message->rap.reg_address = reg_address;
340 memcpy(&message->rap.params, params, param_count);
342 ret = hidpp_send_message_sync(hidpp_dev, message, response);
347 static void delayed_work_cb(struct work_struct *work)
349 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
351 hidpp_connect_event(hidpp);
354 static inline bool hidpp_match_answer(struct hidpp_report *question,
355 struct hidpp_report *answer)
357 return (answer->fap.feature_index == question->fap.feature_index) &&
358 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
361 static inline bool hidpp_match_error(struct hidpp_report *question,
362 struct hidpp_report *answer)
364 return ((answer->rap.sub_id == HIDPP_ERROR) ||
365 (answer->fap.feature_index == HIDPP20_ERROR)) &&
366 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
367 (answer->fap.params[0] == question->fap.funcindex_clientid);
370 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
372 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
373 (report->rap.sub_id == 0x41);
377 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
379 static void hidpp_prefix_name(char **name, int name_length)
381 #define PREFIX_LENGTH 9 /* "Logitech " */
386 if (name_length > PREFIX_LENGTH &&
387 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
388 /* The prefix has is already in the name */
391 new_length = PREFIX_LENGTH + name_length;
392 new_name = kzalloc(new_length, GFP_KERNEL);
396 snprintf(new_name, new_length, "Logitech %s", *name);
403 /* -------------------------------------------------------------------------- */
404 /* HIDP++ 1.0 commands */
405 /* -------------------------------------------------------------------------- */
407 #define HIDPP_SET_REGISTER 0x80
408 #define HIDPP_GET_REGISTER 0x81
409 #define HIDPP_SET_LONG_REGISTER 0x82
410 #define HIDPP_GET_LONG_REGISTER 0x83
413 * hidpp10_set_register_bit() - Sets a single bit in a HID++ 1.0 register.
414 * @hidpp_dev: the device to set the register on.
415 * @register_address: the address of the register to modify.
416 * @byte: the byte of the register to modify. Should be less than 3.
417 * Return: 0 if successful, otherwise a negative error code.
419 static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev,
420 u8 register_address, u8 byte, u8 bit)
422 struct hidpp_report response;
424 u8 params[3] = { 0 };
426 ret = hidpp_send_rap_command_sync(hidpp_dev,
427 REPORT_ID_HIDPP_SHORT,
434 memcpy(params, response.rap.params, 3);
436 params[byte] |= BIT(bit);
438 return hidpp_send_rap_command_sync(hidpp_dev,
439 REPORT_ID_HIDPP_SHORT,
442 params, 3, &response);
446 #define HIDPP_REG_GENERAL 0x00
448 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
450 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_GENERAL, 0, 4);
453 #define HIDPP_REG_FEATURES 0x01
455 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
456 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
458 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_FEATURES, 0, 6);
461 #define HIDPP_REG_BATTERY_STATUS 0x07
463 static int hidpp10_battery_status_map_level(u8 param)
469 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
472 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
475 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
478 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
481 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
487 static int hidpp10_battery_status_map_status(u8 param)
493 /* discharging (in use) */
494 status = POWER_SUPPLY_STATUS_DISCHARGING;
496 case 0x21: /* (standard) charging */
497 case 0x24: /* fast charging */
498 case 0x25: /* slow charging */
499 status = POWER_SUPPLY_STATUS_CHARGING;
501 case 0x26: /* topping charge */
502 case 0x22: /* charge complete */
503 status = POWER_SUPPLY_STATUS_FULL;
505 case 0x20: /* unknown */
506 status = POWER_SUPPLY_STATUS_UNKNOWN;
509 * 0x01...0x1F = reserved (not charging)
510 * 0x23 = charging error
511 * 0x27..0xff = reserved
514 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
521 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
523 struct hidpp_report response;
526 ret = hidpp_send_rap_command_sync(hidpp,
527 REPORT_ID_HIDPP_SHORT,
529 HIDPP_REG_BATTERY_STATUS,
534 hidpp->battery.level =
535 hidpp10_battery_status_map_level(response.rap.params[0]);
536 status = hidpp10_battery_status_map_status(response.rap.params[1]);
537 hidpp->battery.status = status;
538 /* the capacity is only available when discharging or full */
539 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
540 status == POWER_SUPPLY_STATUS_FULL;
545 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
547 static int hidpp10_battery_mileage_map_status(u8 param)
551 switch (param >> 6) {
553 /* discharging (in use) */
554 status = POWER_SUPPLY_STATUS_DISCHARGING;
556 case 0x01: /* charging */
557 status = POWER_SUPPLY_STATUS_CHARGING;
559 case 0x02: /* charge complete */
560 status = POWER_SUPPLY_STATUS_FULL;
563 * 0x03 = charging error
566 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
573 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
575 struct hidpp_report response;
578 ret = hidpp_send_rap_command_sync(hidpp,
579 REPORT_ID_HIDPP_SHORT,
581 HIDPP_REG_BATTERY_MILEAGE,
586 hidpp->battery.capacity = response.rap.params[0];
587 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
588 hidpp->battery.status = status;
589 /* the capacity is only available when discharging or full */
590 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
591 status == POWER_SUPPLY_STATUS_FULL;
596 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
598 struct hidpp_report *report = (struct hidpp_report *)data;
599 int status, capacity, level;
602 if (report->report_id != REPORT_ID_HIDPP_SHORT)
605 switch (report->rap.sub_id) {
606 case HIDPP_REG_BATTERY_STATUS:
607 capacity = hidpp->battery.capacity;
608 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
609 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
611 case HIDPP_REG_BATTERY_MILEAGE:
612 capacity = report->rap.params[0];
613 level = hidpp->battery.level;
614 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
620 changed = capacity != hidpp->battery.capacity ||
621 level != hidpp->battery.level ||
622 status != hidpp->battery.status;
624 /* the capacity is only available when discharging or full */
625 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
626 status == POWER_SUPPLY_STATUS_FULL;
629 hidpp->battery.level = level;
630 hidpp->battery.status = status;
631 if (hidpp->battery.ps)
632 power_supply_changed(hidpp->battery.ps);
638 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
639 #define HIDPP_EXTENDED_PAIRING 0x30
640 #define HIDPP_DEVICE_NAME 0x40
642 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
644 struct hidpp_report response;
646 u8 params[1] = { HIDPP_DEVICE_NAME };
650 ret = hidpp_send_rap_command_sync(hidpp_dev,
651 REPORT_ID_HIDPP_SHORT,
652 HIDPP_GET_LONG_REGISTER,
653 HIDPP_REG_PAIRING_INFORMATION,
654 params, 1, &response);
658 len = response.rap.params[1];
660 if (2 + len > sizeof(response.rap.params))
663 name = kzalloc(len + 1, GFP_KERNEL);
667 memcpy(name, &response.rap.params[2], len);
669 /* include the terminating '\0' */
670 hidpp_prefix_name(&name, len + 1);
675 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
677 struct hidpp_report response;
679 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
681 ret = hidpp_send_rap_command_sync(hidpp,
682 REPORT_ID_HIDPP_SHORT,
683 HIDPP_GET_LONG_REGISTER,
684 HIDPP_REG_PAIRING_INFORMATION,
685 params, 1, &response);
690 * We don't care about LE or BE, we will output it as a string
691 * with %4phD, so we need to keep the order.
693 *serial = *((u32 *)&response.rap.params[1]);
697 static int hidpp_unifying_init(struct hidpp_device *hidpp)
699 struct hid_device *hdev = hidpp->hid_dev;
704 ret = hidpp_unifying_get_serial(hidpp, &serial);
708 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
709 hdev->product, &serial);
710 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
712 name = hidpp_unifying_get_name(hidpp);
716 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
717 dbg_hid("HID++ Unifying: Got name: %s\n", name);
723 /* -------------------------------------------------------------------------- */
725 /* -------------------------------------------------------------------------- */
727 #define HIDPP_PAGE_ROOT 0x0000
728 #define HIDPP_PAGE_ROOT_IDX 0x00
730 #define CMD_ROOT_GET_FEATURE 0x01
731 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
733 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
734 u8 *feature_index, u8 *feature_type)
736 struct hidpp_report response;
738 u8 params[2] = { feature >> 8, feature & 0x00FF };
740 ret = hidpp_send_fap_command_sync(hidpp,
742 CMD_ROOT_GET_FEATURE,
743 params, 2, &response);
747 if (response.fap.params[0] == 0)
750 *feature_index = response.fap.params[0];
751 *feature_type = response.fap.params[1];
756 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
758 struct hidpp_report response;
761 ret = hidpp_send_fap_command_sync(hidpp,
763 CMD_ROOT_GET_PROTOCOL_VERSION,
766 if (ret == HIDPP_ERROR_INVALID_SUBID) {
767 hidpp->protocol_major = 1;
768 hidpp->protocol_minor = 0;
772 /* the device might not be connected */
773 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
777 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
784 hidpp->protocol_major = response.fap.params[0];
785 hidpp->protocol_minor = response.fap.params[1];
790 static bool hidpp_is_connected(struct hidpp_device *hidpp)
794 ret = hidpp_root_get_protocol_version(hidpp);
796 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
797 hidpp->protocol_major, hidpp->protocol_minor);
801 /* -------------------------------------------------------------------------- */
802 /* 0x0005: GetDeviceNameType */
803 /* -------------------------------------------------------------------------- */
805 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
807 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
808 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
809 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
811 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
812 u8 feature_index, u8 *nameLength)
814 struct hidpp_report response;
817 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
818 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
821 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
828 *nameLength = response.fap.params[0];
833 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
834 u8 feature_index, u8 char_index, char *device_name, int len_buf)
836 struct hidpp_report response;
840 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
841 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
845 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
852 switch (response.report_id) {
853 case REPORT_ID_HIDPP_VERY_LONG:
854 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
856 case REPORT_ID_HIDPP_LONG:
857 count = HIDPP_REPORT_LONG_LENGTH - 4;
859 case REPORT_ID_HIDPP_SHORT:
860 count = HIDPP_REPORT_SHORT_LENGTH - 4;
869 for (i = 0; i < count; i++)
870 device_name[i] = response.fap.params[i];
875 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
884 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
885 &feature_index, &feature_type);
889 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
894 name = kzalloc(__name_length + 1, GFP_KERNEL);
898 while (index < __name_length) {
899 ret = hidpp_devicenametype_get_device_name(hidpp,
900 feature_index, index, name + index,
901 __name_length - index);
909 /* include the terminating '\0' */
910 hidpp_prefix_name(&name, __name_length + 1);
915 /* -------------------------------------------------------------------------- */
916 /* 0x1000: Battery level status */
917 /* -------------------------------------------------------------------------- */
919 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
921 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
922 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
924 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
926 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
927 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
928 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
930 static int hidpp_map_battery_level(int capacity)
933 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
934 else if (capacity < 31)
935 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
936 else if (capacity < 81)
937 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
938 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
941 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
948 *next_capacity = data[1];
949 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
951 /* When discharging, we can rely on the device reported capacity.
952 * For all other states the device reports 0 (unknown).
955 case 0: /* discharging (in use) */
956 status = POWER_SUPPLY_STATUS_DISCHARGING;
957 *level = hidpp_map_battery_level(*capacity);
959 case 1: /* recharging */
960 status = POWER_SUPPLY_STATUS_CHARGING;
962 case 2: /* charge in final stage */
963 status = POWER_SUPPLY_STATUS_CHARGING;
965 case 3: /* charge complete */
966 status = POWER_SUPPLY_STATUS_FULL;
967 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
970 case 4: /* recharging below optimal speed */
971 status = POWER_SUPPLY_STATUS_CHARGING;
973 /* 5 = invalid battery type
975 7 = other charging error */
977 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
984 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
991 struct hidpp_report response;
993 u8 *params = (u8 *)response.fap.params;
995 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
996 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
999 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1006 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1013 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1016 struct hidpp_report response;
1018 u8 *params = (u8 *)response.fap.params;
1019 unsigned int level_count, flags;
1021 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1022 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1023 NULL, 0, &response);
1025 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1032 level_count = params[0];
1035 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1036 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1038 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1043 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1047 int status, capacity, next_capacity, level;
1049 if (hidpp->battery.feature_index == 0xff) {
1050 ret = hidpp_root_get_feature(hidpp,
1051 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1052 &hidpp->battery.feature_index,
1058 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1059 hidpp->battery.feature_index,
1061 &next_capacity, &level);
1065 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1066 hidpp->battery.feature_index);
1070 hidpp->battery.status = status;
1071 hidpp->battery.capacity = capacity;
1072 hidpp->battery.level = level;
1073 /* the capacity is only available when discharging or full */
1074 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1075 status == POWER_SUPPLY_STATUS_FULL;
1080 static int hidpp20_battery_event(struct hidpp_device *hidpp,
1083 struct hidpp_report *report = (struct hidpp_report *)data;
1084 int status, capacity, next_capacity, level;
1087 if (report->fap.feature_index != hidpp->battery.feature_index ||
1088 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1091 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1096 /* the capacity is only available when discharging or full */
1097 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1098 status == POWER_SUPPLY_STATUS_FULL;
1100 changed = capacity != hidpp->battery.capacity ||
1101 level != hidpp->battery.level ||
1102 status != hidpp->battery.status;
1105 hidpp->battery.level = level;
1106 hidpp->battery.capacity = capacity;
1107 hidpp->battery.status = status;
1108 if (hidpp->battery.ps)
1109 power_supply_changed(hidpp->battery.ps);
1115 static enum power_supply_property hidpp_battery_props[] = {
1116 POWER_SUPPLY_PROP_ONLINE,
1117 POWER_SUPPLY_PROP_STATUS,
1118 POWER_SUPPLY_PROP_SCOPE,
1119 POWER_SUPPLY_PROP_MODEL_NAME,
1120 POWER_SUPPLY_PROP_MANUFACTURER,
1121 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1122 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1123 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1126 static int hidpp_battery_get_property(struct power_supply *psy,
1127 enum power_supply_property psp,
1128 union power_supply_propval *val)
1130 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1134 case POWER_SUPPLY_PROP_STATUS:
1135 val->intval = hidpp->battery.status;
1137 case POWER_SUPPLY_PROP_CAPACITY:
1138 val->intval = hidpp->battery.capacity;
1140 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1141 val->intval = hidpp->battery.level;
1143 case POWER_SUPPLY_PROP_SCOPE:
1144 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1146 case POWER_SUPPLY_PROP_ONLINE:
1147 val->intval = hidpp->battery.online;
1149 case POWER_SUPPLY_PROP_MODEL_NAME:
1150 if (!strncmp(hidpp->name, "Logitech ", 9))
1151 val->strval = hidpp->name + 9;
1153 val->strval = hidpp->name;
1155 case POWER_SUPPLY_PROP_MANUFACTURER:
1156 val->strval = "Logitech";
1158 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1159 val->strval = hidpp->hid_dev->uniq;
1169 /* -------------------------------------------------------------------------- */
1170 /* 0x2120: Hi-resolution scrolling */
1171 /* -------------------------------------------------------------------------- */
1173 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1175 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1177 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1178 bool enabled, u8 *multiplier)
1184 struct hidpp_report response;
1186 ret = hidpp_root_get_feature(hidpp,
1187 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1193 params[0] = enabled ? BIT(0) : 0;
1194 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1195 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1196 params, sizeof(params), &response);
1199 *multiplier = response.fap.params[1];
1203 /* -------------------------------------------------------------------------- */
1204 /* 0x2121: HiRes Wheel */
1205 /* -------------------------------------------------------------------------- */
1207 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1209 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1210 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1212 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1218 struct hidpp_report response;
1220 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1221 &feature_index, &feature_type);
1223 goto return_default;
1225 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1226 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1227 NULL, 0, &response);
1229 goto return_default;
1231 *multiplier = response.fap.params[0];
1235 hid_warn(hidpp->hid_dev,
1236 "Couldn't get wheel multiplier (error %d), assuming %d.\n",
1241 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1242 bool high_resolution, bool use_hidpp)
1248 struct hidpp_report response;
1250 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1251 &feature_index, &feature_type);
1255 params[0] = (invert ? BIT(2) : 0) |
1256 (high_resolution ? BIT(1) : 0) |
1257 (use_hidpp ? BIT(0) : 0);
1259 return hidpp_send_fap_command_sync(hidpp, feature_index,
1260 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1261 params, sizeof(params), &response);
1264 /* -------------------------------------------------------------------------- */
1265 /* 0x4301: Solar Keyboard */
1266 /* -------------------------------------------------------------------------- */
1268 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1270 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1272 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1273 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1274 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1276 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1278 struct hidpp_report response;
1279 u8 params[2] = { 1, 1 };
1283 if (hidpp->battery.feature_index == 0xff) {
1284 ret = hidpp_root_get_feature(hidpp,
1285 HIDPP_PAGE_SOLAR_KEYBOARD,
1286 &hidpp->battery.solar_feature_index,
1292 ret = hidpp_send_fap_command_sync(hidpp,
1293 hidpp->battery.solar_feature_index,
1294 CMD_SOLAR_SET_LIGHT_MEASURE,
1295 params, 2, &response);
1297 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1304 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1309 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1312 struct hidpp_report *report = (struct hidpp_report *)data;
1313 int capacity, lux, status;
1316 function = report->fap.funcindex_clientid;
1319 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1320 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1321 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1322 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1325 capacity = report->fap.params[0];
1328 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1329 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1331 status = POWER_SUPPLY_STATUS_CHARGING;
1333 status = POWER_SUPPLY_STATUS_DISCHARGING;
1335 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1337 if (capacity < hidpp->battery.capacity)
1338 status = POWER_SUPPLY_STATUS_DISCHARGING;
1340 status = POWER_SUPPLY_STATUS_CHARGING;
1344 if (capacity == 100)
1345 status = POWER_SUPPLY_STATUS_FULL;
1347 hidpp->battery.online = true;
1348 if (capacity != hidpp->battery.capacity ||
1349 status != hidpp->battery.status) {
1350 hidpp->battery.capacity = capacity;
1351 hidpp->battery.status = status;
1352 if (hidpp->battery.ps)
1353 power_supply_changed(hidpp->battery.ps);
1359 /* -------------------------------------------------------------------------- */
1360 /* 0x6010: Touchpad FW items */
1361 /* -------------------------------------------------------------------------- */
1363 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1365 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1367 struct hidpp_touchpad_fw_items {
1369 uint8_t desired_state;
1375 * send a set state command to the device by reading the current items->state
1376 * field. items is then filled with the current state.
1378 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1380 struct hidpp_touchpad_fw_items *items)
1382 struct hidpp_report response;
1384 u8 *params = (u8 *)response.fap.params;
1386 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1387 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1390 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1397 items->presence = params[0];
1398 items->desired_state = params[1];
1399 items->state = params[2];
1400 items->persistent = params[3];
1405 /* -------------------------------------------------------------------------- */
1406 /* 0x6100: TouchPadRawXY */
1407 /* -------------------------------------------------------------------------- */
1409 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1411 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1412 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1414 #define EVENT_TOUCHPAD_RAW_XY 0x00
1416 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1417 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1419 struct hidpp_touchpad_raw_info {
1430 struct hidpp_touchpad_raw_xy_finger {
1440 struct hidpp_touchpad_raw_xy {
1442 struct hidpp_touchpad_raw_xy_finger fingers[2];
1449 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1450 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1452 struct hidpp_report response;
1454 u8 *params = (u8 *)response.fap.params;
1456 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1457 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1460 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1467 raw_info->x_size = get_unaligned_be16(¶ms[0]);
1468 raw_info->y_size = get_unaligned_be16(¶ms[2]);
1469 raw_info->z_range = params[4];
1470 raw_info->area_range = params[5];
1471 raw_info->maxcontacts = params[7];
1472 raw_info->origin = params[8];
1473 /* res is given in unit per inch */
1474 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
1479 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1480 u8 feature_index, bool send_raw_reports,
1481 bool sensor_enhanced_settings)
1483 struct hidpp_report response;
1487 * bit 0 - enable raw
1488 * bit 1 - 16bit Z, no area
1489 * bit 2 - enhanced sensitivity
1490 * bit 3 - width, height (4 bits each) instead of area
1491 * bit 4 - send raw + gestures (degrades smoothness)
1492 * remaining bits - reserved
1494 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1496 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1497 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
1500 static void hidpp_touchpad_touch_event(u8 *data,
1501 struct hidpp_touchpad_raw_xy_finger *finger)
1503 u8 x_m = data[0] << 2;
1504 u8 y_m = data[2] << 2;
1506 finger->x = x_m << 6 | data[1];
1507 finger->y = y_m << 6 | data[3];
1509 finger->contact_type = data[0] >> 6;
1510 finger->contact_status = data[2] >> 6;
1512 finger->z = data[4];
1513 finger->area = data[5];
1514 finger->finger_id = data[6] >> 4;
1517 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1518 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1520 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1521 raw_xy->end_of_frame = data[8] & 0x01;
1522 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1523 raw_xy->finger_count = data[15] & 0x0f;
1524 raw_xy->button = (data[8] >> 2) & 0x01;
1526 if (raw_xy->finger_count) {
1527 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1528 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1532 /* -------------------------------------------------------------------------- */
1533 /* 0x8123: Force feedback support */
1534 /* -------------------------------------------------------------------------- */
1536 #define HIDPP_FF_GET_INFO 0x01
1537 #define HIDPP_FF_RESET_ALL 0x11
1538 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1539 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1540 #define HIDPP_FF_DESTROY_EFFECT 0x41
1541 #define HIDPP_FF_GET_APERTURE 0x51
1542 #define HIDPP_FF_SET_APERTURE 0x61
1543 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1544 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1546 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1547 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1548 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1549 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1551 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1552 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1553 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1554 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1555 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1556 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1557 #define HIDPP_FF_EFFECT_SPRING 0x06
1558 #define HIDPP_FF_EFFECT_DAMPER 0x07
1559 #define HIDPP_FF_EFFECT_FRICTION 0x08
1560 #define HIDPP_FF_EFFECT_INERTIA 0x09
1561 #define HIDPP_FF_EFFECT_RAMP 0x0A
1563 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1565 #define HIDPP_FF_EFFECTID_NONE -1
1566 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1568 #define HIDPP_FF_MAX_PARAMS 20
1569 #define HIDPP_FF_RESERVED_SLOTS 1
1571 struct hidpp_ff_private_data {
1572 struct hidpp_device *hidpp;
1580 struct workqueue_struct *wq;
1581 atomic_t workqueue_size;
1584 struct hidpp_ff_work_data {
1585 struct work_struct work;
1586 struct hidpp_ff_private_data *data;
1589 u8 params[HIDPP_FF_MAX_PARAMS];
1593 static const signed short hiddpp_ff_effects[] = {
1608 static const signed short hiddpp_ff_effects_v2[] = {
1615 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1616 HIDPP_FF_EFFECT_SPRING,
1617 HIDPP_FF_EFFECT_FRICTION,
1618 HIDPP_FF_EFFECT_DAMPER,
1619 HIDPP_FF_EFFECT_INERTIA
1622 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1630 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1634 for (i = 0; i < data->num_effects; i++)
1635 if (data->effect_ids[i] == effect_id)
1641 static void hidpp_ff_work_handler(struct work_struct *w)
1643 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1644 struct hidpp_ff_private_data *data = wd->data;
1645 struct hidpp_report response;
1649 /* add slot number if needed */
1650 switch (wd->effect_id) {
1651 case HIDPP_FF_EFFECTID_AUTOCENTER:
1652 wd->params[0] = data->slot_autocenter;
1654 case HIDPP_FF_EFFECTID_NONE:
1655 /* leave slot as zero */
1658 /* find current slot for effect */
1659 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1663 /* send command and wait for reply */
1664 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1665 wd->command, wd->params, wd->size, &response);
1668 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1672 /* parse return data */
1673 switch (wd->command) {
1674 case HIDPP_FF_DOWNLOAD_EFFECT:
1675 slot = response.fap.params[0];
1676 if (slot > 0 && slot <= data->num_effects) {
1677 if (wd->effect_id >= 0)
1678 /* regular effect uploaded */
1679 data->effect_ids[slot-1] = wd->effect_id;
1680 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1681 /* autocenter spring uploaded */
1682 data->slot_autocenter = slot;
1685 case HIDPP_FF_DESTROY_EFFECT:
1686 if (wd->effect_id >= 0)
1687 /* regular effect destroyed */
1688 data->effect_ids[wd->params[0]-1] = -1;
1689 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1690 /* autocenter spring destoyed */
1691 data->slot_autocenter = 0;
1693 case HIDPP_FF_SET_GLOBAL_GAINS:
1694 data->gain = (wd->params[0] << 8) + wd->params[1];
1696 case HIDPP_FF_SET_APERTURE:
1697 data->range = (wd->params[0] << 8) + wd->params[1];
1700 /* no action needed */
1705 atomic_dec(&data->workqueue_size);
1709 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1711 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1717 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1720 wd->effect_id = effect_id;
1721 wd->command = command;
1723 memcpy(wd->params, params, size);
1725 atomic_inc(&data->workqueue_size);
1726 queue_work(data->wq, &wd->work);
1728 /* warn about excessive queue size */
1729 s = atomic_read(&data->workqueue_size);
1730 if (s >= 20 && s % 20 == 0)
1731 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1736 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1738 struct hidpp_ff_private_data *data = dev->ff->private;
1743 /* set common parameters */
1744 params[2] = effect->replay.length >> 8;
1745 params[3] = effect->replay.length & 255;
1746 params[4] = effect->replay.delay >> 8;
1747 params[5] = effect->replay.delay & 255;
1749 switch (effect->type) {
1751 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1752 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1753 params[6] = force >> 8;
1754 params[7] = force & 255;
1755 params[8] = effect->u.constant.envelope.attack_level >> 7;
1756 params[9] = effect->u.constant.envelope.attack_length >> 8;
1757 params[10] = effect->u.constant.envelope.attack_length & 255;
1758 params[11] = effect->u.constant.envelope.fade_level >> 7;
1759 params[12] = effect->u.constant.envelope.fade_length >> 8;
1760 params[13] = effect->u.constant.envelope.fade_length & 255;
1762 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1763 effect->u.constant.level,
1764 effect->direction, force);
1765 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1766 effect->u.constant.envelope.attack_level,
1767 effect->u.constant.envelope.attack_length,
1768 effect->u.constant.envelope.fade_level,
1769 effect->u.constant.envelope.fade_length);
1773 switch (effect->u.periodic.waveform) {
1775 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1778 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1781 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1784 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1787 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1790 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1793 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1794 params[6] = effect->u.periodic.magnitude >> 8;
1795 params[7] = effect->u.periodic.magnitude & 255;
1796 params[8] = effect->u.periodic.offset >> 8;
1797 params[9] = effect->u.periodic.offset & 255;
1798 params[10] = effect->u.periodic.period >> 8;
1799 params[11] = effect->u.periodic.period & 255;
1800 params[12] = effect->u.periodic.phase >> 8;
1801 params[13] = effect->u.periodic.phase & 255;
1802 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1803 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1804 params[16] = effect->u.periodic.envelope.attack_length & 255;
1805 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1806 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1807 params[19] = effect->u.periodic.envelope.fade_length & 255;
1809 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1810 effect->u.periodic.magnitude, effect->direction,
1811 effect->u.periodic.offset,
1812 effect->u.periodic.period,
1813 effect->u.periodic.phase);
1814 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1815 effect->u.periodic.envelope.attack_level,
1816 effect->u.periodic.envelope.attack_length,
1817 effect->u.periodic.envelope.fade_level,
1818 effect->u.periodic.envelope.fade_length);
1822 params[1] = HIDPP_FF_EFFECT_RAMP;
1823 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1824 params[6] = force >> 8;
1825 params[7] = force & 255;
1826 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1827 params[8] = force >> 8;
1828 params[9] = force & 255;
1829 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1830 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1831 params[12] = effect->u.ramp.envelope.attack_length & 255;
1832 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1833 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1834 params[15] = effect->u.ramp.envelope.fade_length & 255;
1836 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1837 effect->u.ramp.start_level,
1838 effect->u.ramp.end_level,
1839 effect->direction, force);
1840 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1841 effect->u.ramp.envelope.attack_level,
1842 effect->u.ramp.envelope.attack_length,
1843 effect->u.ramp.envelope.fade_level,
1844 effect->u.ramp.envelope.fade_length);
1850 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1851 params[6] = effect->u.condition[0].left_saturation >> 9;
1852 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1853 params[8] = effect->u.condition[0].left_coeff >> 8;
1854 params[9] = effect->u.condition[0].left_coeff & 255;
1855 params[10] = effect->u.condition[0].deadband >> 9;
1856 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1857 params[12] = effect->u.condition[0].center >> 8;
1858 params[13] = effect->u.condition[0].center & 255;
1859 params[14] = effect->u.condition[0].right_coeff >> 8;
1860 params[15] = effect->u.condition[0].right_coeff & 255;
1861 params[16] = effect->u.condition[0].right_saturation >> 9;
1862 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1864 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1865 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1866 effect->u.condition[0].left_coeff,
1867 effect->u.condition[0].left_saturation,
1868 effect->u.condition[0].right_coeff,
1869 effect->u.condition[0].right_saturation);
1870 dbg_hid(" deadband=%d, center=%d\n",
1871 effect->u.condition[0].deadband,
1872 effect->u.condition[0].center);
1875 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1879 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1882 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1884 struct hidpp_ff_private_data *data = dev->ff->private;
1887 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1889 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1891 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1894 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1896 struct hidpp_ff_private_data *data = dev->ff->private;
1899 dbg_hid("Erasing effect %d.\n", effect_id);
1901 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1904 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1906 struct hidpp_ff_private_data *data = dev->ff->private;
1909 dbg_hid("Setting autocenter to %d.\n", magnitude);
1911 /* start a standard spring effect */
1912 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1913 /* zero delay and duration */
1914 params[2] = params[3] = params[4] = params[5] = 0;
1915 /* set coeff to 25% of saturation */
1916 params[8] = params[14] = magnitude >> 11;
1917 params[9] = params[15] = (magnitude >> 3) & 255;
1918 params[6] = params[16] = magnitude >> 9;
1919 params[7] = params[17] = (magnitude >> 1) & 255;
1920 /* zero deadband and center */
1921 params[10] = params[11] = params[12] = params[13] = 0;
1923 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1926 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1928 struct hidpp_ff_private_data *data = dev->ff->private;
1931 dbg_hid("Setting gain to %d.\n", gain);
1933 params[0] = gain >> 8;
1934 params[1] = gain & 255;
1935 params[2] = 0; /* no boost */
1938 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1941 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1943 struct hid_device *hid = to_hid_device(dev);
1944 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1945 struct input_dev *idev = hidinput->input;
1946 struct hidpp_ff_private_data *data = idev->ff->private;
1948 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1951 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1953 struct hid_device *hid = to_hid_device(dev);
1954 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1955 struct input_dev *idev = hidinput->input;
1956 struct hidpp_ff_private_data *data = idev->ff->private;
1958 int range = simple_strtoul(buf, NULL, 10);
1960 range = clamp(range, 180, 900);
1962 params[0] = range >> 8;
1963 params[1] = range & 0x00FF;
1965 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1970 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1972 static void hidpp_ff_destroy(struct ff_device *ff)
1974 struct hidpp_ff_private_data *data = ff->private;
1976 kfree(data->effect_ids);
1979 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
1981 struct hid_device *hid = hidpp->hid_dev;
1982 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1983 struct input_dev *dev = hidinput->input;
1984 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1985 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1986 struct ff_device *ff;
1987 struct hidpp_report response;
1988 struct hidpp_ff_private_data *data;
1989 int error, j, num_slots;
1993 hid_err(hid, "Struct input_dev not set!\n");
1997 /* Get firmware release */
1998 version = bcdDevice & 255;
2000 /* Set supported force feedback capabilities */
2001 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
2002 set_bit(hiddpp_ff_effects[j], dev->ffbit);
2004 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
2005 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
2007 /* Read number of slots available in device */
2008 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2009 HIDPP_FF_GET_INFO, NULL, 0, &response);
2013 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2018 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2020 error = input_ff_create(dev, num_slots);
2023 hid_err(dev, "Failed to create FF device!\n");
2027 data = kzalloc(sizeof(*data), GFP_KERNEL);
2030 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2031 if (!data->effect_ids) {
2035 data->hidpp = hidpp;
2036 data->feature_index = feature_index;
2037 data->version = version;
2038 data->slot_autocenter = 0;
2039 data->num_effects = num_slots;
2040 for (j = 0; j < num_slots; j++)
2041 data->effect_ids[j] = -1;
2046 ff->upload = hidpp_ff_upload_effect;
2047 ff->erase = hidpp_ff_erase_effect;
2048 ff->playback = hidpp_ff_playback;
2049 ff->set_gain = hidpp_ff_set_gain;
2050 ff->set_autocenter = hidpp_ff_set_autocenter;
2051 ff->destroy = hidpp_ff_destroy;
2054 /* reset all forces */
2055 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2056 HIDPP_FF_RESET_ALL, NULL, 0, &response);
2058 /* Read current Range */
2059 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2060 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
2062 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
2063 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
2065 /* Create sysfs interface */
2066 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2068 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2070 /* Read the current gain values */
2071 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2072 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
2074 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
2075 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
2076 /* ignore boost value at response.fap.params[2] */
2078 /* init the hardware command queue */
2079 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2080 atomic_set(&data->workqueue_size, 0);
2082 /* initialize with zero autocenter to get wheel in usable state */
2083 hidpp_ff_set_autocenter(dev, 0);
2085 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2091 static int hidpp_ff_deinit(struct hid_device *hid)
2093 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2094 struct input_dev *dev = hidinput->input;
2095 struct hidpp_ff_private_data *data;
2098 hid_err(hid, "Struct input_dev not found!\n");
2102 hid_info(hid, "Unloading HID++ force feedback.\n");
2103 data = dev->ff->private;
2105 hid_err(hid, "Private data not found!\n");
2109 destroy_workqueue(data->wq);
2110 device_remove_file(&hid->dev, &dev_attr_range);
2116 /* ************************************************************************** */
2118 /* Device Support */
2120 /* ************************************************************************** */
2122 /* -------------------------------------------------------------------------- */
2123 /* Touchpad HID++ devices */
2124 /* -------------------------------------------------------------------------- */
2126 #define WTP_MANUAL_RESOLUTION 39
2129 struct input_dev *input;
2132 u8 mt_feature_index;
2133 u8 button_feature_index;
2136 unsigned int resolution;
2139 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2140 struct hid_field *field, struct hid_usage *usage,
2141 unsigned long **bit, int *max)
2146 static void wtp_populate_input(struct hidpp_device *hidpp,
2147 struct input_dev *input_dev, bool origin_is_hid_core)
2149 struct wtp_data *wd = hidpp->private_data;
2151 __set_bit(EV_ABS, input_dev->evbit);
2152 __set_bit(EV_KEY, input_dev->evbit);
2153 __clear_bit(EV_REL, input_dev->evbit);
2154 __clear_bit(EV_LED, input_dev->evbit);
2156 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2157 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2158 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2159 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2161 /* Max pressure is not given by the devices, pick one */
2162 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2164 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2166 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2167 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2169 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2171 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2172 INPUT_MT_DROP_UNUSED);
2174 wd->input = input_dev;
2177 static void wtp_touch_event(struct wtp_data *wd,
2178 struct hidpp_touchpad_raw_xy_finger *touch_report)
2182 if (!touch_report->finger_id || touch_report->contact_type)
2183 /* no actual data */
2186 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2188 input_mt_slot(wd->input, slot);
2189 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2190 touch_report->contact_status);
2191 if (touch_report->contact_status) {
2192 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2194 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2195 wd->flip_y ? wd->y_size - touch_report->y :
2197 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2198 touch_report->area);
2202 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2203 struct hidpp_touchpad_raw_xy *raw)
2205 struct wtp_data *wd = hidpp->private_data;
2208 for (i = 0; i < 2; i++)
2209 wtp_touch_event(wd, &(raw->fingers[i]));
2211 if (raw->end_of_frame &&
2212 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2213 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2215 if (raw->end_of_frame || raw->finger_count <= 2) {
2216 input_mt_sync_frame(wd->input);
2217 input_sync(wd->input);
2221 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2223 struct wtp_data *wd = hidpp->private_data;
2224 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2225 (data[7] >> 4) * (data[7] >> 4)) / 2;
2226 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2227 (data[13] >> 4) * (data[13] >> 4)) / 2;
2228 struct hidpp_touchpad_raw_xy raw = {
2229 .timestamp = data[1],
2233 .contact_status = !!data[7],
2234 .x = get_unaligned_le16(&data[3]),
2235 .y = get_unaligned_le16(&data[5]),
2238 .finger_id = data[2],
2241 .contact_status = !!data[13],
2242 .x = get_unaligned_le16(&data[9]),
2243 .y = get_unaligned_le16(&data[11]),
2246 .finger_id = data[8],
2249 .finger_count = wd->maxcontacts,
2251 .end_of_frame = (data[0] >> 7) == 0,
2252 .button = data[0] & 0x01,
2255 wtp_send_raw_xy_event(hidpp, &raw);
2260 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2262 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2263 struct wtp_data *wd = hidpp->private_data;
2264 struct hidpp_report *report = (struct hidpp_report *)data;
2265 struct hidpp_touchpad_raw_xy raw;
2267 if (!wd || !wd->input)
2273 hid_err(hdev, "Received HID report of bad size (%d)",
2277 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2278 input_event(wd->input, EV_KEY, BTN_LEFT,
2279 !!(data[1] & 0x01));
2280 input_event(wd->input, EV_KEY, BTN_RIGHT,
2281 !!(data[1] & 0x02));
2282 input_sync(wd->input);
2287 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2289 case REPORT_ID_HIDPP_LONG:
2290 /* size is already checked in hidpp_raw_event. */
2291 if ((report->fap.feature_index != wd->mt_feature_index) ||
2292 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2294 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2296 wtp_send_raw_xy_event(hidpp, &raw);
2303 static int wtp_get_config(struct hidpp_device *hidpp)
2305 struct wtp_data *wd = hidpp->private_data;
2306 struct hidpp_touchpad_raw_info raw_info = {0};
2310 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2311 &wd->mt_feature_index, &feature_type);
2313 /* means that the device is not powered up */
2316 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2321 wd->x_size = raw_info.x_size;
2322 wd->y_size = raw_info.y_size;
2323 wd->maxcontacts = raw_info.maxcontacts;
2324 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2325 wd->resolution = raw_info.res;
2326 if (!wd->resolution)
2327 wd->resolution = WTP_MANUAL_RESOLUTION;
2332 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2334 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2335 struct wtp_data *wd;
2337 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2342 hidpp->private_data = wd;
2347 static int wtp_connect(struct hid_device *hdev, bool connected)
2349 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2350 struct wtp_data *wd = hidpp->private_data;
2354 ret = wtp_get_config(hidpp);
2356 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2361 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2365 /* ------------------------------------------------------------------------- */
2366 /* Logitech M560 devices */
2367 /* ------------------------------------------------------------------------- */
2370 * Logitech M560 protocol overview
2372 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2373 * the sides buttons are pressed, it sends some keyboard keys events
2374 * instead of buttons ones.
2375 * To complicate things further, the middle button keys sequence
2376 * is different from the odd press and the even press.
2378 * forward button -> Super_R
2379 * backward button -> Super_L+'d' (press only)
2380 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2381 * 2nd time: left-click (press only)
2382 * NB: press-only means that when the button is pressed, the
2383 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2384 * together sequentially; instead when the button is released, no event is
2388 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2389 * the mouse reacts differently:
2390 * - it never sends a keyboard key event
2391 * - for the three mouse button it sends:
2392 * middle button press 11<xx>0a 3500af00...
2393 * side 1 button (forward) press 11<xx>0a 3500b000...
2394 * side 2 button (backward) press 11<xx>0a 3500ae00...
2395 * middle/side1/side2 button release 11<xx>0a 35000000...
2398 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2400 struct m560_private_data {
2401 struct input_dev *input;
2404 /* how buttons are mapped in the report */
2405 #define M560_MOUSE_BTN_LEFT 0x01
2406 #define M560_MOUSE_BTN_RIGHT 0x02
2407 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2408 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2410 #define M560_SUB_ID 0x0a
2411 #define M560_BUTTON_MODE_REGISTER 0x35
2413 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2415 struct hidpp_report response;
2416 struct hidpp_device *hidpp_dev;
2418 hidpp_dev = hid_get_drvdata(hdev);
2420 return hidpp_send_rap_command_sync(
2422 REPORT_ID_HIDPP_SHORT,
2424 M560_BUTTON_MODE_REGISTER,
2425 (u8 *)m560_config_parameter,
2426 sizeof(m560_config_parameter),
2431 static int m560_allocate(struct hid_device *hdev)
2433 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2434 struct m560_private_data *d;
2436 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2441 hidpp->private_data = d;
2446 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2448 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2449 struct m560_private_data *mydata = hidpp->private_data;
2452 if (!mydata || !mydata->input) {
2453 hid_err(hdev, "error in parameter\n");
2458 hid_err(hdev, "error in report\n");
2462 if (data[0] == REPORT_ID_HIDPP_LONG &&
2463 data[2] == M560_SUB_ID && data[6] == 0x00) {
2465 * m560 mouse report for middle, forward and backward button
2468 * data[1] = device-id
2470 * data[5] = 0xaf -> middle
2473 * 0x00 -> release all
2479 input_report_key(mydata->input, BTN_MIDDLE, 1);
2482 input_report_key(mydata->input, BTN_FORWARD, 1);
2485 input_report_key(mydata->input, BTN_BACK, 1);
2488 input_report_key(mydata->input, BTN_BACK, 0);
2489 input_report_key(mydata->input, BTN_FORWARD, 0);
2490 input_report_key(mydata->input, BTN_MIDDLE, 0);
2493 hid_err(hdev, "error in report\n");
2496 input_sync(mydata->input);
2498 } else if (data[0] == 0x02) {
2500 * Logitech M560 mouse report
2502 * data[0] = type (0x02)
2503 * data[1..2] = buttons
2510 input_report_key(mydata->input, BTN_LEFT,
2511 !!(data[1] & M560_MOUSE_BTN_LEFT));
2512 input_report_key(mydata->input, BTN_RIGHT,
2513 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2515 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2516 input_report_rel(mydata->input, REL_HWHEEL, -1);
2517 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2518 input_report_rel(mydata->input, REL_HWHEEL, 1);
2520 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2521 input_report_rel(mydata->input, REL_X, v);
2523 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2524 input_report_rel(mydata->input, REL_Y, v);
2526 v = hid_snto32(data[6], 8);
2527 hid_scroll_counter_handle_scroll(
2528 &hidpp->vertical_wheel_counter, v);
2530 input_sync(mydata->input);
2536 static void m560_populate_input(struct hidpp_device *hidpp,
2537 struct input_dev *input_dev, bool origin_is_hid_core)
2539 struct m560_private_data *mydata = hidpp->private_data;
2541 mydata->input = input_dev;
2543 __set_bit(EV_KEY, mydata->input->evbit);
2544 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2545 __set_bit(BTN_RIGHT, mydata->input->keybit);
2546 __set_bit(BTN_LEFT, mydata->input->keybit);
2547 __set_bit(BTN_BACK, mydata->input->keybit);
2548 __set_bit(BTN_FORWARD, mydata->input->keybit);
2550 __set_bit(EV_REL, mydata->input->evbit);
2551 __set_bit(REL_X, mydata->input->relbit);
2552 __set_bit(REL_Y, mydata->input->relbit);
2553 __set_bit(REL_WHEEL, mydata->input->relbit);
2554 __set_bit(REL_HWHEEL, mydata->input->relbit);
2557 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2558 struct hid_field *field, struct hid_usage *usage,
2559 unsigned long **bit, int *max)
2564 /* ------------------------------------------------------------------------- */
2565 /* Logitech K400 devices */
2566 /* ------------------------------------------------------------------------- */
2569 * The Logitech K400 keyboard has an embedded touchpad which is seen
2570 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2571 * tap-to-click but the setting is not remembered accross reset, annoying some
2574 * We can toggle this feature from the host by using the feature 0x6010:
2578 struct k400_private_data {
2582 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2584 struct k400_private_data *k400 = hidpp->private_data;
2585 struct hidpp_touchpad_fw_items items = {};
2589 if (!k400->feature_index) {
2590 ret = hidpp_root_get_feature(hidpp,
2591 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2592 &k400->feature_index, &feature_type);
2594 /* means that the device is not powered up */
2598 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2605 static int k400_allocate(struct hid_device *hdev)
2607 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2608 struct k400_private_data *k400;
2610 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2615 hidpp->private_data = k400;
2620 static int k400_connect(struct hid_device *hdev, bool connected)
2622 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2624 if (!disable_tap_to_click)
2627 return k400_disable_tap_to_click(hidpp);
2630 /* ------------------------------------------------------------------------- */
2631 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2632 /* ------------------------------------------------------------------------- */
2634 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2636 static int g920_get_config(struct hidpp_device *hidpp)
2642 /* Find feature and store for later use */
2643 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2644 &feature_index, &feature_type);
2648 ret = hidpp_ff_init(hidpp, feature_index);
2650 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2656 /* -------------------------------------------------------------------------- */
2657 /* High-resolution scroll wheels */
2658 /* -------------------------------------------------------------------------- */
2661 * struct hi_res_scroll_info - Stores info on a device's high-res scroll wheel.
2662 * @product_id: the HID product ID of the device being described.
2663 * @microns_per_hi_res_unit: the distance moved by the user's finger for each
2664 * high-resolution unit reported by the device, in
2665 * 256ths of a millimetre.
2667 struct hi_res_scroll_info {
2669 int microns_per_hi_res_unit;
2672 static struct hi_res_scroll_info hi_res_scroll_devices[] = {
2674 .product_id = 0x1017, .microns_per_hi_res_unit = 445 },
2675 { /* Performance MX */
2676 .product_id = 0x101a, .microns_per_hi_res_unit = 406 },
2678 .product_id = 0x402d, .microns_per_hi_res_unit = 435 },
2679 { /* MX Master 2S */
2680 .product_id = 0x4069, .microns_per_hi_res_unit = 406 },
2683 static int hi_res_scroll_look_up_microns(__u32 product_id)
2686 int num_devices = sizeof(hi_res_scroll_devices)
2687 / sizeof(hi_res_scroll_devices[0]);
2688 for (i = 0; i < num_devices; i++) {
2689 if (hi_res_scroll_devices[i].product_id == product_id)
2690 return hi_res_scroll_devices[i].microns_per_hi_res_unit;
2692 /* We don't have a value for this device, so use a sensible default. */
2696 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
2701 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
2702 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
2703 hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
2704 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
2705 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
2707 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
2708 ret = hidpp10_enable_scrolling_acceleration(hidpp);
2714 hidpp->vertical_wheel_counter.resolution_multiplier = multiplier;
2715 hidpp->vertical_wheel_counter.microns_per_hi_res_unit =
2716 hi_res_scroll_look_up_microns(hidpp->hid_dev->product);
2717 hid_info(hidpp->hid_dev, "multiplier = %d, microns = %d\n",
2719 hidpp->vertical_wheel_counter.microns_per_hi_res_unit);
2723 /* -------------------------------------------------------------------------- */
2724 /* Generic HID++ devices */
2725 /* -------------------------------------------------------------------------- */
2727 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2728 struct hid_field *field, struct hid_usage *usage,
2729 unsigned long **bit, int *max)
2731 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2733 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2734 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2735 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2736 field->application != HID_GD_MOUSE)
2737 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2742 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2743 struct hid_field *field, struct hid_usage *usage,
2744 unsigned long **bit, int *max)
2746 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2748 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2749 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2750 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2751 usage->code == ABS_Y || usage->code == ABS_Z ||
2752 usage->code == ABS_RZ)) {
2753 field->application = HID_GD_MULTIAXIS;
2761 static void hidpp_populate_input(struct hidpp_device *hidpp,
2762 struct input_dev *input, bool origin_is_hid_core)
2764 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2765 wtp_populate_input(hidpp, input, origin_is_hid_core);
2766 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2767 m560_populate_input(hidpp, input, origin_is_hid_core);
2769 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) {
2770 input_set_capability(input, EV_REL, REL_WHEEL_HI_RES);
2771 hidpp->vertical_wheel_counter.dev = input;
2775 static int hidpp_input_configured(struct hid_device *hdev,
2776 struct hid_input *hidinput)
2778 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2779 struct input_dev *input = hidinput->input;
2781 hidpp_populate_input(hidpp, input, true);
2786 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2789 struct hidpp_report *question = hidpp->send_receive_buf;
2790 struct hidpp_report *answer = hidpp->send_receive_buf;
2791 struct hidpp_report *report = (struct hidpp_report *)data;
2795 * If the mutex is locked then we have a pending answer from a
2796 * previously sent command.
2798 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2800 * Check for a correct hidpp20 answer or the corresponding
2803 if (hidpp_match_answer(question, report) ||
2804 hidpp_match_error(question, report)) {
2806 hidpp->answer_available = true;
2807 wake_up(&hidpp->wait);
2809 * This was an answer to a command that this driver sent
2810 * We return 1 to hid-core to avoid forwarding the
2811 * command upstream as it has been treated by the driver
2818 if (unlikely(hidpp_report_is_connect_event(report))) {
2819 atomic_set(&hidpp->connected,
2820 !(report->rap.params[0] & (1 << 6)));
2821 if (schedule_work(&hidpp->work) == 0)
2822 dbg_hid("%s: connect event already queued\n", __func__);
2826 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2827 ret = hidpp20_battery_event(hidpp, data, size);
2830 ret = hidpp_solar_battery_event(hidpp, data, size);
2835 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2836 ret = hidpp10_battery_event(hidpp, data, size);
2844 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2847 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2850 /* Generic HID++ processing. */
2852 case REPORT_ID_HIDPP_VERY_LONG:
2853 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2854 hid_err(hdev, "received hid++ report of bad size (%d)",
2858 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2860 case REPORT_ID_HIDPP_LONG:
2861 if (size != HIDPP_REPORT_LONG_LENGTH) {
2862 hid_err(hdev, "received hid++ report of bad size (%d)",
2866 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2868 case REPORT_ID_HIDPP_SHORT:
2869 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2870 hid_err(hdev, "received hid++ report of bad size (%d)",
2874 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2878 /* If no report is available for further processing, skip calling
2879 * raw_event of subclasses. */
2883 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2884 return wtp_raw_event(hdev, data, size);
2885 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2886 return m560_raw_event(hdev, data, size);
2891 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
2892 struct hid_usage *usage, __s32 value)
2894 /* This function will only be called for scroll events, due to the
2895 * restriction imposed in hidpp_usages.
2897 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2898 struct hid_scroll_counter *counter = &hidpp->vertical_wheel_counter;
2899 /* A scroll event may occur before the multiplier has been retrieved or
2900 * the input device set, or high-res scroll enabling may fail. In such
2901 * cases we must return early (falling back to default behaviour) to
2902 * avoid a crash in hid_scroll_counter_handle_scroll.
2904 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
2905 || counter->dev == NULL || counter->resolution_multiplier == 0)
2908 hid_scroll_counter_handle_scroll(counter, value);
2912 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2914 static atomic_t battery_no = ATOMIC_INIT(0);
2915 struct power_supply_config cfg = { .drv_data = hidpp };
2916 struct power_supply_desc *desc = &hidpp->battery.desc;
2917 enum power_supply_property *battery_props;
2918 struct hidpp_battery *battery;
2919 unsigned int num_battery_props;
2923 if (hidpp->battery.ps)
2926 hidpp->battery.feature_index = 0xff;
2927 hidpp->battery.solar_feature_index = 0xff;
2929 if (hidpp->protocol_major >= 2) {
2930 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
2931 ret = hidpp_solar_request_battery_event(hidpp);
2933 ret = hidpp20_query_battery_info(hidpp);
2937 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2939 ret = hidpp10_query_battery_status(hidpp);
2941 ret = hidpp10_query_battery_mileage(hidpp);
2944 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2946 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
2948 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
2951 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
2952 hidpp_battery_props,
2953 sizeof(hidpp_battery_props),
2958 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
2960 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2961 battery_props[num_battery_props++] =
2962 POWER_SUPPLY_PROP_CAPACITY;
2964 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
2965 battery_props[num_battery_props++] =
2966 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
2968 battery = &hidpp->battery;
2970 n = atomic_inc_return(&battery_no) - 1;
2971 desc->properties = battery_props;
2972 desc->num_properties = num_battery_props;
2973 desc->get_property = hidpp_battery_get_property;
2974 sprintf(battery->name, "hidpp_battery_%ld", n);
2975 desc->name = battery->name;
2976 desc->type = POWER_SUPPLY_TYPE_BATTERY;
2977 desc->use_for_apm = 0;
2979 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2982 if (IS_ERR(battery->ps))
2983 return PTR_ERR(battery->ps);
2985 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2990 static void hidpp_overwrite_name(struct hid_device *hdev)
2992 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2995 if (hidpp->protocol_major < 2)
2998 name = hidpp_get_device_name(hidpp);
3001 hid_err(hdev, "unable to retrieve the name of the device");
3003 dbg_hid("HID++: Got name: %s\n", name);
3004 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3010 static int hidpp_input_open(struct input_dev *dev)
3012 struct hid_device *hid = input_get_drvdata(dev);
3014 return hid_hw_open(hid);
3017 static void hidpp_input_close(struct input_dev *dev)
3019 struct hid_device *hid = input_get_drvdata(dev);
3024 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3026 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3027 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3032 input_set_drvdata(input_dev, hdev);
3033 input_dev->open = hidpp_input_open;
3034 input_dev->close = hidpp_input_close;
3036 input_dev->name = hidpp->name;
3037 input_dev->phys = hdev->phys;
3038 input_dev->uniq = hdev->uniq;
3039 input_dev->id.bustype = hdev->bus;
3040 input_dev->id.vendor = hdev->vendor;
3041 input_dev->id.product = hdev->product;
3042 input_dev->id.version = hdev->version;
3043 input_dev->dev.parent = &hdev->dev;
3048 static void hidpp_connect_event(struct hidpp_device *hidpp)
3050 struct hid_device *hdev = hidpp->hid_dev;
3052 bool connected = atomic_read(&hidpp->connected);
3053 struct input_dev *input;
3054 char *name, *devm_name;
3057 if (hidpp->battery.ps) {
3058 hidpp->battery.online = false;
3059 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3060 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3061 power_supply_changed(hidpp->battery.ps);
3066 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3067 ret = wtp_connect(hdev, connected);
3070 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3071 ret = m560_send_config_command(hdev, connected);
3074 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3075 ret = k400_connect(hdev, connected);
3080 /* the device is already connected, we can ask for its name and
3082 if (!hidpp->protocol_major) {
3083 ret = !hidpp_is_connected(hidpp);
3085 hid_err(hdev, "Can not get the protocol version.\n");
3088 hid_info(hdev, "HID++ %u.%u device connected.\n",
3089 hidpp->protocol_major, hidpp->protocol_minor);
3092 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3093 name = hidpp_get_device_name(hidpp);
3096 "unable to retrieve the name of the device");
3100 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
3105 hidpp->name = devm_name;
3108 hidpp_initialize_battery(hidpp);
3110 /* forward current battery state */
3111 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3112 hidpp10_enable_battery_reporting(hidpp);
3113 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3114 hidpp10_query_battery_mileage(hidpp);
3116 hidpp10_query_battery_status(hidpp);
3117 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3118 hidpp20_query_battery_info(hidpp);
3120 if (hidpp->battery.ps)
3121 power_supply_changed(hidpp->battery.ps);
3123 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3124 hi_res_scroll_enable(hidpp);
3126 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3127 /* if the input nodes are already created, we can stop now */
3130 input = hidpp_allocate_input(hdev);
3132 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3136 hidpp_populate_input(hidpp, input, false);
3138 ret = input_register_device(input);
3140 input_free_device(input);
3142 hidpp->delayed_input = input;
3145 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3147 static struct attribute *sysfs_attrs[] = {
3148 &dev_attr_builtin_power_supply.attr,
3152 static const struct attribute_group ps_attribute_group = {
3153 .attrs = sysfs_attrs
3156 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3158 struct hidpp_device *hidpp;
3161 unsigned int connect_mask = HID_CONNECT_DEFAULT;
3163 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
3168 hidpp->hid_dev = hdev;
3169 hidpp->name = hdev->name;
3170 hid_set_drvdata(hdev, hidpp);
3172 hidpp->quirks = id->driver_data;
3174 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3175 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3177 if (disable_raw_mode) {
3178 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
3179 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
3182 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3183 ret = wtp_allocate(hdev, id);
3186 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3187 ret = m560_allocate(hdev);
3190 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3191 ret = k400_allocate(hdev);
3196 INIT_WORK(&hidpp->work, delayed_work_cb);
3197 mutex_init(&hidpp->send_mutex);
3198 init_waitqueue_head(&hidpp->wait);
3200 /* indicates we are handling the battery properties in the kernel */
3201 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3203 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3206 ret = hid_parse(hdev);
3208 hid_err(hdev, "%s:parse failed\n", __func__);
3209 goto hid_parse_fail;
3212 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3213 connect_mask &= ~HID_CONNECT_HIDINPUT;
3215 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3216 ret = hid_hw_start(hdev, connect_mask);
3218 hid_err(hdev, "hw start failed\n");
3219 goto hid_hw_start_fail;
3221 ret = hid_hw_open(hdev);
3223 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3226 goto hid_hw_start_fail;
3231 /* Allow incoming packets */
3232 hid_device_io_start(hdev);
3234 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3235 hidpp_unifying_init(hidpp);
3237 connected = hidpp_is_connected(hidpp);
3238 atomic_set(&hidpp->connected, connected);
3239 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
3242 hid_err(hdev, "Device not connected");
3243 goto hid_hw_open_failed;
3246 hid_info(hdev, "HID++ %u.%u device connected.\n",
3247 hidpp->protocol_major, hidpp->protocol_minor);
3249 hidpp_overwrite_name(hdev);
3252 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
3253 ret = wtp_get_config(hidpp);
3255 goto hid_hw_open_failed;
3256 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3257 ret = g920_get_config(hidpp);
3259 goto hid_hw_open_failed;
3262 /* Block incoming packets */
3263 hid_device_io_stop(hdev);
3265 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3266 ret = hid_hw_start(hdev, connect_mask);
3268 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3269 goto hid_hw_start_fail;
3273 /* Allow incoming packets */
3274 hid_device_io_start(hdev);
3276 hidpp_connect_event(hidpp);
3281 hid_device_io_stop(hdev);
3282 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3288 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3289 cancel_work_sync(&hidpp->work);
3290 mutex_destroy(&hidpp->send_mutex);
3292 hid_set_drvdata(hdev, NULL);
3296 static void hidpp_remove(struct hid_device *hdev)
3298 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3300 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3302 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3303 hidpp_ff_deinit(hdev);
3307 cancel_work_sync(&hidpp->work);
3308 mutex_destroy(&hidpp->send_mutex);
3311 #define LDJ_DEVICE(product) \
3312 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3313 USB_VENDOR_ID_LOGITECH, (product))
3315 static const struct hid_device_id hidpp_devices[] = {
3316 { /* wireless touchpad */
3318 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3319 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
3320 { /* wireless touchpad T650 */
3322 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3323 { /* wireless touchpad T651 */
3324 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3325 USB_DEVICE_ID_LOGITECH_T651),
3326 .driver_data = HIDPP_QUIRK_CLASS_WTP },
3327 { /* Mouse Logitech Anywhere MX */
3328 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3329 { /* Mouse Logitech Cube */
3330 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3331 { /* Mouse Logitech M335 */
3332 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3333 { /* Mouse Logitech M515 */
3334 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3335 { /* Mouse logitech M560 */
3337 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
3338 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3339 { /* Mouse Logitech M705 (firmware RQM17) */
3340 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3341 { /* Mouse Logitech M705 (firmware RQM67) */
3342 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3343 { /* Mouse Logitech M720 */
3344 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3345 { /* Mouse Logitech MX Anywhere 2 */
3346 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3347 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3348 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3349 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3350 { /* Mouse Logitech MX Anywhere 2S */
3351 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3352 { /* Mouse Logitech MX Master */
3353 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3354 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3355 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3356 { /* Mouse Logitech MX Master 2S */
3357 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3358 { /* Mouse Logitech Performance MX */
3359 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3360 { /* Keyboard logitech K400 */
3362 .driver_data = HIDPP_QUIRK_CLASS_K400 },
3363 { /* Solar Keyboard Logitech K750 */
3365 .driver_data = HIDPP_QUIRK_CLASS_K750 },
3367 { LDJ_DEVICE(HID_ANY_ID) },
3369 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
3370 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
3374 MODULE_DEVICE_TABLE(hid, hidpp_devices);
3376 static const struct hid_usage_id hidpp_usages[] = {
3377 { HID_GD_WHEEL, EV_REL, REL_WHEEL },
3378 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
3381 static struct hid_driver hidpp_driver = {
3382 .name = "logitech-hidpp-device",
3383 .id_table = hidpp_devices,
3384 .probe = hidpp_probe,
3385 .remove = hidpp_remove,
3386 .raw_event = hidpp_raw_event,
3387 .usage_table = hidpp_usages,
3388 .event = hidpp_event,
3389 .input_configured = hidpp_input_configured,
3390 .input_mapping = hidpp_input_mapping,
3391 .input_mapped = hidpp_input_mapped,
3394 module_hid_driver(hidpp_driver);