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/sched/clock.h>
25 #include <linux/kfifo.h>
26 #include <linux/input/mt.h>
27 #include <linux/workqueue.h>
28 #include <linux/atomic.h>
29 #include <linux/fixp-arith.h>
30 #include <asm/unaligned.h>
31 #include "usbhid/usbhid.h"
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
36 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
38 static bool disable_raw_mode;
39 module_param(disable_raw_mode, bool, 0644);
40 MODULE_PARM_DESC(disable_raw_mode,
41 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
43 static bool disable_tap_to_click;
44 module_param(disable_tap_to_click, bool, 0644);
45 MODULE_PARM_DESC(disable_tap_to_click,
46 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
48 #define REPORT_ID_HIDPP_SHORT 0x10
49 #define REPORT_ID_HIDPP_LONG 0x11
50 #define REPORT_ID_HIDPP_VERY_LONG 0x12
52 #define HIDPP_REPORT_SHORT_LENGTH 7
53 #define HIDPP_REPORT_LONG_LENGTH 20
54 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
56 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
57 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
58 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
59 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
60 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
62 /* bits 2..20 are reserved for classes */
63 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
64 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
65 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
66 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
67 #define HIDPP_QUIRK_UNIFYING BIT(25)
68 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
69 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
70 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
72 /* Convenience constant to check for any high-res support. */
73 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
74 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
75 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
77 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
79 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
80 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
81 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
82 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
85 * There are two hidpp protocols in use, the first version hidpp10 is known
86 * as register access protocol or RAP, the second version hidpp20 is known as
87 * feature access protocol or FAP
89 * Most older devices (including the Unifying usb receiver) use the RAP protocol
90 * where as most newer devices use the FAP protocol. Both protocols are
91 * compatible with the underlying transport, which could be usb, Unifiying, or
92 * bluetooth. The message lengths are defined by the hid vendor specific report
93 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
94 * the HIDPP_LONG report type (total message length 20 bytes)
96 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
97 * messages. The Unifying receiver itself responds to RAP messages (device index
98 * is 0xFF for the receiver), and all messages (short or long) with a device
99 * index between 1 and 6 are passed untouched to the corresponding paired
102 * The paired device can be RAP or FAP, it will receive the message untouched
103 * from the Unifiying receiver.
108 u8 funcindex_clientid;
109 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
115 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
118 struct hidpp_report {
124 u8 rawbytes[sizeof(struct fap)];
128 struct hidpp_battery {
130 u8 solar_feature_index;
131 struct power_supply_desc desc;
132 struct power_supply *ps;
141 * struct hidpp_scroll_counter - Utility class for processing high-resolution
143 * @dev: the input device for which events should be reported.
144 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
145 * @remainder: counts the number of high-resolution units moved since the last
146 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
147 * only be used by class methods.
148 * @direction: direction of last movement (1 or -1)
149 * @last_time: last event time, used to reset remainder after inactivity
151 struct hidpp_scroll_counter {
152 struct input_dev *dev;
153 int wheel_multiplier;
156 unsigned long long last_time;
159 struct hidpp_device {
160 struct hid_device *hid_dev;
161 struct mutex send_mutex;
162 void *send_receive_buf;
163 char *name; /* will never be NULL and should not be freed */
164 wait_queue_head_t wait;
165 bool answer_available;
171 struct work_struct work;
172 struct kfifo delayed_work_fifo;
174 struct input_dev *delayed_input;
176 unsigned long quirks;
177 unsigned long capabilities;
179 struct hidpp_battery battery;
180 struct hidpp_scroll_counter vertical_wheel_counter;
183 /* HID++ 1.0 error codes */
184 #define HIDPP_ERROR 0x8f
185 #define HIDPP_ERROR_SUCCESS 0x00
186 #define HIDPP_ERROR_INVALID_SUBID 0x01
187 #define HIDPP_ERROR_INVALID_ADRESS 0x02
188 #define HIDPP_ERROR_INVALID_VALUE 0x03
189 #define HIDPP_ERROR_CONNECT_FAIL 0x04
190 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
191 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
192 #define HIDPP_ERROR_BUSY 0x07
193 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
194 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
195 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
196 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
197 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
198 /* HID++ 2.0 error codes */
199 #define HIDPP20_ERROR 0xff
201 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
203 static int __hidpp_send_report(struct hid_device *hdev,
204 struct hidpp_report *hidpp_report)
206 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
207 int fields_count, ret;
209 hidpp = hid_get_drvdata(hdev);
211 switch (hidpp_report->report_id) {
212 case REPORT_ID_HIDPP_SHORT:
213 fields_count = HIDPP_REPORT_SHORT_LENGTH;
215 case REPORT_ID_HIDPP_LONG:
216 fields_count = HIDPP_REPORT_LONG_LENGTH;
218 case REPORT_ID_HIDPP_VERY_LONG:
219 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
226 * set the device_index as the receiver, it will be overwritten by
227 * hid_hw_request if needed
229 hidpp_report->device_index = 0xff;
231 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
232 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
234 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
235 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
239 return ret == fields_count ? 0 : -1;
243 * hidpp_send_message_sync() returns 0 in case of success, and something else
244 * in case of a failure.
245 * - If ' something else' is positive, that means that an error has been raised
246 * by the protocol itself.
247 * - If ' something else' is negative, that means that we had a classic error
248 * (-ENOMEM, -EPIPE, etc...)
250 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
251 struct hidpp_report *message,
252 struct hidpp_report *response)
256 mutex_lock(&hidpp->send_mutex);
258 hidpp->send_receive_buf = response;
259 hidpp->answer_available = false;
262 * So that we can later validate the answer when it arrives
265 *response = *message;
267 ret = __hidpp_send_report(hidpp->hid_dev, message);
270 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
271 memset(response, 0, sizeof(struct hidpp_report));
275 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
277 dbg_hid("%s:timeout waiting for response\n", __func__);
278 memset(response, 0, sizeof(struct hidpp_report));
282 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
283 response->rap.sub_id == HIDPP_ERROR) {
284 ret = response->rap.params[1];
285 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
289 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
290 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
291 response->fap.feature_index == HIDPP20_ERROR) {
292 ret = response->fap.params[1];
293 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
298 mutex_unlock(&hidpp->send_mutex);
303 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
304 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
305 struct hidpp_report *response)
307 struct hidpp_report *message;
310 if (param_count > sizeof(message->fap.params))
313 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
317 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
318 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
320 message->report_id = REPORT_ID_HIDPP_LONG;
321 message->fap.feature_index = feat_index;
322 message->fap.funcindex_clientid = funcindex_clientid;
323 memcpy(&message->fap.params, params, param_count);
325 ret = hidpp_send_message_sync(hidpp, message, response);
330 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
331 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
332 struct hidpp_report *response)
334 struct hidpp_report *message;
338 case REPORT_ID_HIDPP_SHORT:
339 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
341 case REPORT_ID_HIDPP_LONG:
342 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
344 case REPORT_ID_HIDPP_VERY_LONG:
345 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
351 if (param_count > max_count)
354 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
357 message->report_id = report_id;
358 message->rap.sub_id = sub_id;
359 message->rap.reg_address = reg_address;
360 memcpy(&message->rap.params, params, param_count);
362 ret = hidpp_send_message_sync(hidpp_dev, message, response);
367 static void delayed_work_cb(struct work_struct *work)
369 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
371 hidpp_connect_event(hidpp);
374 static inline bool hidpp_match_answer(struct hidpp_report *question,
375 struct hidpp_report *answer)
377 return (answer->fap.feature_index == question->fap.feature_index) &&
378 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
381 static inline bool hidpp_match_error(struct hidpp_report *question,
382 struct hidpp_report *answer)
384 return ((answer->rap.sub_id == HIDPP_ERROR) ||
385 (answer->fap.feature_index == HIDPP20_ERROR)) &&
386 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
387 (answer->fap.params[0] == question->fap.funcindex_clientid);
390 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
392 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
393 (report->rap.sub_id == 0x41);
397 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
399 static void hidpp_prefix_name(char **name, int name_length)
401 #define PREFIX_LENGTH 9 /* "Logitech " */
406 if (name_length > PREFIX_LENGTH &&
407 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
408 /* The prefix has is already in the name */
411 new_length = PREFIX_LENGTH + name_length;
412 new_name = kzalloc(new_length, GFP_KERNEL);
416 snprintf(new_name, new_length, "Logitech %s", *name);
424 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
425 * events given a high-resolution wheel
427 * @counter: a hid_scroll_counter struct describing the wheel.
428 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
431 * Given a high-resolution movement, this function converts the movement into
432 * fractions of 120 and emits high-resolution scroll events for the input
433 * device. It also uses the multiplier from &struct hid_scroll_counter to
434 * emit low-resolution scroll events when appropriate for
435 * backwards-compatibility with userspace input libraries.
437 static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter *counter,
440 int low_res_value, remainder, direction;
441 unsigned long long now, previous;
443 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
444 input_report_rel(counter->dev, REL_WHEEL_HI_RES, hi_res_value);
446 remainder = counter->remainder;
447 direction = hi_res_value > 0 ? 1 : -1;
450 previous = counter->last_time;
451 counter->last_time = now;
453 * Reset the remainder after a period of inactivity or when the
454 * direction changes. This prevents the REL_WHEEL emulation point
455 * from sliding for devices that don't always provide the same
456 * number of movements per detent.
458 if (now - previous > 1000000000 || direction != counter->direction)
461 counter->direction = direction;
462 remainder += hi_res_value;
464 /* Some wheels will rest 7/8ths of a detent from the previous detent
465 * after slow movement, so we want the threshold for low-res events to
466 * be in the middle between two detents (e.g. after 4/8ths) as
467 * opposed to on the detents themselves (8/8ths).
469 if (abs(remainder) >= 60) {
470 /* Add (or subtract) 1 because we want to trigger when the wheel
471 * is half-way to the next detent (i.e. scroll 1 detent after a
472 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
475 low_res_value = remainder / 120;
476 if (low_res_value == 0)
477 low_res_value = (hi_res_value > 0 ? 1 : -1);
478 input_report_rel(counter->dev, REL_WHEEL, low_res_value);
479 remainder -= low_res_value * 120;
481 counter->remainder = remainder;
484 /* -------------------------------------------------------------------------- */
485 /* HIDP++ 1.0 commands */
486 /* -------------------------------------------------------------------------- */
488 #define HIDPP_SET_REGISTER 0x80
489 #define HIDPP_GET_REGISTER 0x81
490 #define HIDPP_SET_LONG_REGISTER 0x82
491 #define HIDPP_GET_LONG_REGISTER 0x83
494 * hidpp10_set_register_bit() - Sets a single bit in a HID++ 1.0 register.
495 * @hidpp_dev: the device to set the register on.
496 * @register_address: the address of the register to modify.
497 * @byte: the byte of the register to modify. Should be less than 3.
498 * Return: 0 if successful, otherwise a negative error code.
500 static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev,
501 u8 register_address, u8 byte, u8 bit)
503 struct hidpp_report response;
505 u8 params[3] = { 0 };
507 ret = hidpp_send_rap_command_sync(hidpp_dev,
508 REPORT_ID_HIDPP_SHORT,
515 memcpy(params, response.rap.params, 3);
517 params[byte] |= BIT(bit);
519 return hidpp_send_rap_command_sync(hidpp_dev,
520 REPORT_ID_HIDPP_SHORT,
523 params, 3, &response);
527 #define HIDPP_REG_GENERAL 0x00
529 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
531 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_GENERAL, 0, 4);
534 #define HIDPP_REG_FEATURES 0x01
536 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
537 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
539 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_FEATURES, 0, 6);
542 #define HIDPP_REG_BATTERY_STATUS 0x07
544 static int hidpp10_battery_status_map_level(u8 param)
550 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
553 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
556 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
559 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
562 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
568 static int hidpp10_battery_status_map_status(u8 param)
574 /* discharging (in use) */
575 status = POWER_SUPPLY_STATUS_DISCHARGING;
577 case 0x21: /* (standard) charging */
578 case 0x24: /* fast charging */
579 case 0x25: /* slow charging */
580 status = POWER_SUPPLY_STATUS_CHARGING;
582 case 0x26: /* topping charge */
583 case 0x22: /* charge complete */
584 status = POWER_SUPPLY_STATUS_FULL;
586 case 0x20: /* unknown */
587 status = POWER_SUPPLY_STATUS_UNKNOWN;
590 * 0x01...0x1F = reserved (not charging)
591 * 0x23 = charging error
592 * 0x27..0xff = reserved
595 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
602 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
604 struct hidpp_report response;
607 ret = hidpp_send_rap_command_sync(hidpp,
608 REPORT_ID_HIDPP_SHORT,
610 HIDPP_REG_BATTERY_STATUS,
615 hidpp->battery.level =
616 hidpp10_battery_status_map_level(response.rap.params[0]);
617 status = hidpp10_battery_status_map_status(response.rap.params[1]);
618 hidpp->battery.status = status;
619 /* the capacity is only available when discharging or full */
620 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
621 status == POWER_SUPPLY_STATUS_FULL;
626 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
628 static int hidpp10_battery_mileage_map_status(u8 param)
632 switch (param >> 6) {
634 /* discharging (in use) */
635 status = POWER_SUPPLY_STATUS_DISCHARGING;
637 case 0x01: /* charging */
638 status = POWER_SUPPLY_STATUS_CHARGING;
640 case 0x02: /* charge complete */
641 status = POWER_SUPPLY_STATUS_FULL;
644 * 0x03 = charging error
647 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
654 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
656 struct hidpp_report response;
659 ret = hidpp_send_rap_command_sync(hidpp,
660 REPORT_ID_HIDPP_SHORT,
662 HIDPP_REG_BATTERY_MILEAGE,
667 hidpp->battery.capacity = response.rap.params[0];
668 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
669 hidpp->battery.status = status;
670 /* the capacity is only available when discharging or full */
671 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
672 status == POWER_SUPPLY_STATUS_FULL;
677 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
679 struct hidpp_report *report = (struct hidpp_report *)data;
680 int status, capacity, level;
683 if (report->report_id != REPORT_ID_HIDPP_SHORT)
686 switch (report->rap.sub_id) {
687 case HIDPP_REG_BATTERY_STATUS:
688 capacity = hidpp->battery.capacity;
689 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
690 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
692 case HIDPP_REG_BATTERY_MILEAGE:
693 capacity = report->rap.params[0];
694 level = hidpp->battery.level;
695 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
701 changed = capacity != hidpp->battery.capacity ||
702 level != hidpp->battery.level ||
703 status != hidpp->battery.status;
705 /* the capacity is only available when discharging or full */
706 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
707 status == POWER_SUPPLY_STATUS_FULL;
710 hidpp->battery.level = level;
711 hidpp->battery.status = status;
712 if (hidpp->battery.ps)
713 power_supply_changed(hidpp->battery.ps);
719 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
720 #define HIDPP_EXTENDED_PAIRING 0x30
721 #define HIDPP_DEVICE_NAME 0x40
723 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
725 struct hidpp_report response;
727 u8 params[1] = { HIDPP_DEVICE_NAME };
731 ret = hidpp_send_rap_command_sync(hidpp_dev,
732 REPORT_ID_HIDPP_SHORT,
733 HIDPP_GET_LONG_REGISTER,
734 HIDPP_REG_PAIRING_INFORMATION,
735 params, 1, &response);
739 len = response.rap.params[1];
741 if (2 + len > sizeof(response.rap.params))
744 name = kzalloc(len + 1, GFP_KERNEL);
748 memcpy(name, &response.rap.params[2], len);
750 /* include the terminating '\0' */
751 hidpp_prefix_name(&name, len + 1);
756 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
758 struct hidpp_report response;
760 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
762 ret = hidpp_send_rap_command_sync(hidpp,
763 REPORT_ID_HIDPP_SHORT,
764 HIDPP_GET_LONG_REGISTER,
765 HIDPP_REG_PAIRING_INFORMATION,
766 params, 1, &response);
771 * We don't care about LE or BE, we will output it as a string
772 * with %4phD, so we need to keep the order.
774 *serial = *((u32 *)&response.rap.params[1]);
778 static int hidpp_unifying_init(struct hidpp_device *hidpp)
780 struct hid_device *hdev = hidpp->hid_dev;
785 ret = hidpp_unifying_get_serial(hidpp, &serial);
789 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
790 hdev->product, &serial);
791 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
793 name = hidpp_unifying_get_name(hidpp);
797 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
798 dbg_hid("HID++ Unifying: Got name: %s\n", name);
804 /* -------------------------------------------------------------------------- */
806 /* -------------------------------------------------------------------------- */
808 #define HIDPP_PAGE_ROOT 0x0000
809 #define HIDPP_PAGE_ROOT_IDX 0x00
811 #define CMD_ROOT_GET_FEATURE 0x01
812 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
814 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
815 u8 *feature_index, u8 *feature_type)
817 struct hidpp_report response;
819 u8 params[2] = { feature >> 8, feature & 0x00FF };
821 ret = hidpp_send_fap_command_sync(hidpp,
823 CMD_ROOT_GET_FEATURE,
824 params, 2, &response);
828 if (response.fap.params[0] == 0)
831 *feature_index = response.fap.params[0];
832 *feature_type = response.fap.params[1];
837 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
839 struct hidpp_report response;
842 ret = hidpp_send_fap_command_sync(hidpp,
844 CMD_ROOT_GET_PROTOCOL_VERSION,
847 if (ret == HIDPP_ERROR_INVALID_SUBID) {
848 hidpp->protocol_major = 1;
849 hidpp->protocol_minor = 0;
853 /* the device might not be connected */
854 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
858 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
865 hidpp->protocol_major = response.fap.params[0];
866 hidpp->protocol_minor = response.fap.params[1];
871 static bool hidpp_is_connected(struct hidpp_device *hidpp)
875 ret = hidpp_root_get_protocol_version(hidpp);
877 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
878 hidpp->protocol_major, hidpp->protocol_minor);
882 /* -------------------------------------------------------------------------- */
883 /* 0x0005: GetDeviceNameType */
884 /* -------------------------------------------------------------------------- */
886 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
888 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
889 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
890 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
892 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
893 u8 feature_index, u8 *nameLength)
895 struct hidpp_report response;
898 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
899 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
902 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
909 *nameLength = response.fap.params[0];
914 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
915 u8 feature_index, u8 char_index, char *device_name, int len_buf)
917 struct hidpp_report response;
921 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
922 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
926 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
933 switch (response.report_id) {
934 case REPORT_ID_HIDPP_VERY_LONG:
935 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
937 case REPORT_ID_HIDPP_LONG:
938 count = HIDPP_REPORT_LONG_LENGTH - 4;
940 case REPORT_ID_HIDPP_SHORT:
941 count = HIDPP_REPORT_SHORT_LENGTH - 4;
950 for (i = 0; i < count; i++)
951 device_name[i] = response.fap.params[i];
956 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
965 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
966 &feature_index, &feature_type);
970 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
975 name = kzalloc(__name_length + 1, GFP_KERNEL);
979 while (index < __name_length) {
980 ret = hidpp_devicenametype_get_device_name(hidpp,
981 feature_index, index, name + index,
982 __name_length - index);
990 /* include the terminating '\0' */
991 hidpp_prefix_name(&name, __name_length + 1);
996 /* -------------------------------------------------------------------------- */
997 /* 0x1000: Battery level status */
998 /* -------------------------------------------------------------------------- */
1000 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1002 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1003 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1005 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1007 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1008 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1009 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1011 static int hidpp_map_battery_level(int capacity)
1014 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1015 else if (capacity < 31)
1016 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1017 else if (capacity < 81)
1018 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1019 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1022 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1028 *capacity = data[0];
1029 *next_capacity = data[1];
1030 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1032 /* When discharging, we can rely on the device reported capacity.
1033 * For all other states the device reports 0 (unknown).
1036 case 0: /* discharging (in use) */
1037 status = POWER_SUPPLY_STATUS_DISCHARGING;
1038 *level = hidpp_map_battery_level(*capacity);
1040 case 1: /* recharging */
1041 status = POWER_SUPPLY_STATUS_CHARGING;
1043 case 2: /* charge in final stage */
1044 status = POWER_SUPPLY_STATUS_CHARGING;
1046 case 3: /* charge complete */
1047 status = POWER_SUPPLY_STATUS_FULL;
1048 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1051 case 4: /* recharging below optimal speed */
1052 status = POWER_SUPPLY_STATUS_CHARGING;
1054 /* 5 = invalid battery type
1056 7 = other charging error */
1058 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1065 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1072 struct hidpp_report response;
1074 u8 *params = (u8 *)response.fap.params;
1076 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1077 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1078 NULL, 0, &response);
1080 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1087 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1094 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1097 struct hidpp_report response;
1099 u8 *params = (u8 *)response.fap.params;
1100 unsigned int level_count, flags;
1102 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1103 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1104 NULL, 0, &response);
1106 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1113 level_count = params[0];
1116 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1117 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1119 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1124 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1128 int status, capacity, next_capacity, level;
1130 if (hidpp->battery.feature_index == 0xff) {
1131 ret = hidpp_root_get_feature(hidpp,
1132 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1133 &hidpp->battery.feature_index,
1139 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1140 hidpp->battery.feature_index,
1142 &next_capacity, &level);
1146 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1147 hidpp->battery.feature_index);
1151 hidpp->battery.status = status;
1152 hidpp->battery.capacity = capacity;
1153 hidpp->battery.level = level;
1154 /* the capacity is only available when discharging or full */
1155 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1156 status == POWER_SUPPLY_STATUS_FULL;
1161 static int hidpp20_battery_event(struct hidpp_device *hidpp,
1164 struct hidpp_report *report = (struct hidpp_report *)data;
1165 int status, capacity, next_capacity, level;
1168 if (report->fap.feature_index != hidpp->battery.feature_index ||
1169 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1172 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1177 /* the capacity is only available when discharging or full */
1178 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1179 status == POWER_SUPPLY_STATUS_FULL;
1181 changed = capacity != hidpp->battery.capacity ||
1182 level != hidpp->battery.level ||
1183 status != hidpp->battery.status;
1186 hidpp->battery.level = level;
1187 hidpp->battery.capacity = capacity;
1188 hidpp->battery.status = status;
1189 if (hidpp->battery.ps)
1190 power_supply_changed(hidpp->battery.ps);
1196 static enum power_supply_property hidpp_battery_props[] = {
1197 POWER_SUPPLY_PROP_ONLINE,
1198 POWER_SUPPLY_PROP_STATUS,
1199 POWER_SUPPLY_PROP_SCOPE,
1200 POWER_SUPPLY_PROP_MODEL_NAME,
1201 POWER_SUPPLY_PROP_MANUFACTURER,
1202 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1203 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1204 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1207 static int hidpp_battery_get_property(struct power_supply *psy,
1208 enum power_supply_property psp,
1209 union power_supply_propval *val)
1211 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1215 case POWER_SUPPLY_PROP_STATUS:
1216 val->intval = hidpp->battery.status;
1218 case POWER_SUPPLY_PROP_CAPACITY:
1219 val->intval = hidpp->battery.capacity;
1221 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1222 val->intval = hidpp->battery.level;
1224 case POWER_SUPPLY_PROP_SCOPE:
1225 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1227 case POWER_SUPPLY_PROP_ONLINE:
1228 val->intval = hidpp->battery.online;
1230 case POWER_SUPPLY_PROP_MODEL_NAME:
1231 if (!strncmp(hidpp->name, "Logitech ", 9))
1232 val->strval = hidpp->name + 9;
1234 val->strval = hidpp->name;
1236 case POWER_SUPPLY_PROP_MANUFACTURER:
1237 val->strval = "Logitech";
1239 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1240 val->strval = hidpp->hid_dev->uniq;
1250 /* -------------------------------------------------------------------------- */
1251 /* 0x2120: Hi-resolution scrolling */
1252 /* -------------------------------------------------------------------------- */
1254 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1256 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1258 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1259 bool enabled, u8 *multiplier)
1265 struct hidpp_report response;
1267 ret = hidpp_root_get_feature(hidpp,
1268 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1274 params[0] = enabled ? BIT(0) : 0;
1275 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1276 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1277 params, sizeof(params), &response);
1280 *multiplier = response.fap.params[1];
1284 /* -------------------------------------------------------------------------- */
1285 /* 0x2121: HiRes Wheel */
1286 /* -------------------------------------------------------------------------- */
1288 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1290 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1291 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1293 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1299 struct hidpp_report response;
1301 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1302 &feature_index, &feature_type);
1304 goto return_default;
1306 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1307 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1308 NULL, 0, &response);
1310 goto return_default;
1312 *multiplier = response.fap.params[0];
1315 hid_warn(hidpp->hid_dev,
1316 "Couldn't get wheel multiplier (error %d)\n", ret);
1320 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1321 bool high_resolution, bool use_hidpp)
1327 struct hidpp_report response;
1329 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1330 &feature_index, &feature_type);
1334 params[0] = (invert ? BIT(2) : 0) |
1335 (high_resolution ? BIT(1) : 0) |
1336 (use_hidpp ? BIT(0) : 0);
1338 return hidpp_send_fap_command_sync(hidpp, feature_index,
1339 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1340 params, sizeof(params), &response);
1343 /* -------------------------------------------------------------------------- */
1344 /* 0x4301: Solar Keyboard */
1345 /* -------------------------------------------------------------------------- */
1347 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1349 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1351 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1352 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1353 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1355 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1357 struct hidpp_report response;
1358 u8 params[2] = { 1, 1 };
1362 if (hidpp->battery.feature_index == 0xff) {
1363 ret = hidpp_root_get_feature(hidpp,
1364 HIDPP_PAGE_SOLAR_KEYBOARD,
1365 &hidpp->battery.solar_feature_index,
1371 ret = hidpp_send_fap_command_sync(hidpp,
1372 hidpp->battery.solar_feature_index,
1373 CMD_SOLAR_SET_LIGHT_MEASURE,
1374 params, 2, &response);
1376 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1383 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1388 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1391 struct hidpp_report *report = (struct hidpp_report *)data;
1392 int capacity, lux, status;
1395 function = report->fap.funcindex_clientid;
1398 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1399 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1400 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1401 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1404 capacity = report->fap.params[0];
1407 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1408 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1410 status = POWER_SUPPLY_STATUS_CHARGING;
1412 status = POWER_SUPPLY_STATUS_DISCHARGING;
1414 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1416 if (capacity < hidpp->battery.capacity)
1417 status = POWER_SUPPLY_STATUS_DISCHARGING;
1419 status = POWER_SUPPLY_STATUS_CHARGING;
1423 if (capacity == 100)
1424 status = POWER_SUPPLY_STATUS_FULL;
1426 hidpp->battery.online = true;
1427 if (capacity != hidpp->battery.capacity ||
1428 status != hidpp->battery.status) {
1429 hidpp->battery.capacity = capacity;
1430 hidpp->battery.status = status;
1431 if (hidpp->battery.ps)
1432 power_supply_changed(hidpp->battery.ps);
1438 /* -------------------------------------------------------------------------- */
1439 /* 0x6010: Touchpad FW items */
1440 /* -------------------------------------------------------------------------- */
1442 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1444 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1446 struct hidpp_touchpad_fw_items {
1448 uint8_t desired_state;
1454 * send a set state command to the device by reading the current items->state
1455 * field. items is then filled with the current state.
1457 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1459 struct hidpp_touchpad_fw_items *items)
1461 struct hidpp_report response;
1463 u8 *params = (u8 *)response.fap.params;
1465 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1466 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1469 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1476 items->presence = params[0];
1477 items->desired_state = params[1];
1478 items->state = params[2];
1479 items->persistent = params[3];
1484 /* -------------------------------------------------------------------------- */
1485 /* 0x6100: TouchPadRawXY */
1486 /* -------------------------------------------------------------------------- */
1488 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1490 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1491 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1493 #define EVENT_TOUCHPAD_RAW_XY 0x00
1495 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1496 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1498 struct hidpp_touchpad_raw_info {
1509 struct hidpp_touchpad_raw_xy_finger {
1519 struct hidpp_touchpad_raw_xy {
1521 struct hidpp_touchpad_raw_xy_finger fingers[2];
1528 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1529 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1531 struct hidpp_report response;
1533 u8 *params = (u8 *)response.fap.params;
1535 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1536 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1539 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1546 raw_info->x_size = get_unaligned_be16(¶ms[0]);
1547 raw_info->y_size = get_unaligned_be16(¶ms[2]);
1548 raw_info->z_range = params[4];
1549 raw_info->area_range = params[5];
1550 raw_info->maxcontacts = params[7];
1551 raw_info->origin = params[8];
1552 /* res is given in unit per inch */
1553 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
1558 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1559 u8 feature_index, bool send_raw_reports,
1560 bool sensor_enhanced_settings)
1562 struct hidpp_report response;
1566 * bit 0 - enable raw
1567 * bit 1 - 16bit Z, no area
1568 * bit 2 - enhanced sensitivity
1569 * bit 3 - width, height (4 bits each) instead of area
1570 * bit 4 - send raw + gestures (degrades smoothness)
1571 * remaining bits - reserved
1573 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1575 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1576 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
1579 static void hidpp_touchpad_touch_event(u8 *data,
1580 struct hidpp_touchpad_raw_xy_finger *finger)
1582 u8 x_m = data[0] << 2;
1583 u8 y_m = data[2] << 2;
1585 finger->x = x_m << 6 | data[1];
1586 finger->y = y_m << 6 | data[3];
1588 finger->contact_type = data[0] >> 6;
1589 finger->contact_status = data[2] >> 6;
1591 finger->z = data[4];
1592 finger->area = data[5];
1593 finger->finger_id = data[6] >> 4;
1596 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1597 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1599 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1600 raw_xy->end_of_frame = data[8] & 0x01;
1601 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1602 raw_xy->finger_count = data[15] & 0x0f;
1603 raw_xy->button = (data[8] >> 2) & 0x01;
1605 if (raw_xy->finger_count) {
1606 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1607 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1611 /* -------------------------------------------------------------------------- */
1612 /* 0x8123: Force feedback support */
1613 /* -------------------------------------------------------------------------- */
1615 #define HIDPP_FF_GET_INFO 0x01
1616 #define HIDPP_FF_RESET_ALL 0x11
1617 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1618 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1619 #define HIDPP_FF_DESTROY_EFFECT 0x41
1620 #define HIDPP_FF_GET_APERTURE 0x51
1621 #define HIDPP_FF_SET_APERTURE 0x61
1622 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1623 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1625 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1626 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1627 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1628 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1630 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1631 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1632 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1633 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1634 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1635 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1636 #define HIDPP_FF_EFFECT_SPRING 0x06
1637 #define HIDPP_FF_EFFECT_DAMPER 0x07
1638 #define HIDPP_FF_EFFECT_FRICTION 0x08
1639 #define HIDPP_FF_EFFECT_INERTIA 0x09
1640 #define HIDPP_FF_EFFECT_RAMP 0x0A
1642 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1644 #define HIDPP_FF_EFFECTID_NONE -1
1645 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1647 #define HIDPP_FF_MAX_PARAMS 20
1648 #define HIDPP_FF_RESERVED_SLOTS 1
1650 struct hidpp_ff_private_data {
1651 struct hidpp_device *hidpp;
1659 struct workqueue_struct *wq;
1660 atomic_t workqueue_size;
1663 struct hidpp_ff_work_data {
1664 struct work_struct work;
1665 struct hidpp_ff_private_data *data;
1668 u8 params[HIDPP_FF_MAX_PARAMS];
1672 static const signed short hidpp_ff_effects[] = {
1687 static const signed short hidpp_ff_effects_v2[] = {
1694 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1695 HIDPP_FF_EFFECT_SPRING,
1696 HIDPP_FF_EFFECT_FRICTION,
1697 HIDPP_FF_EFFECT_DAMPER,
1698 HIDPP_FF_EFFECT_INERTIA
1701 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1709 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1713 for (i = 0; i < data->num_effects; i++)
1714 if (data->effect_ids[i] == effect_id)
1720 static void hidpp_ff_work_handler(struct work_struct *w)
1722 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1723 struct hidpp_ff_private_data *data = wd->data;
1724 struct hidpp_report response;
1728 /* add slot number if needed */
1729 switch (wd->effect_id) {
1730 case HIDPP_FF_EFFECTID_AUTOCENTER:
1731 wd->params[0] = data->slot_autocenter;
1733 case HIDPP_FF_EFFECTID_NONE:
1734 /* leave slot as zero */
1737 /* find current slot for effect */
1738 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1742 /* send command and wait for reply */
1743 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1744 wd->command, wd->params, wd->size, &response);
1747 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1751 /* parse return data */
1752 switch (wd->command) {
1753 case HIDPP_FF_DOWNLOAD_EFFECT:
1754 slot = response.fap.params[0];
1755 if (slot > 0 && slot <= data->num_effects) {
1756 if (wd->effect_id >= 0)
1757 /* regular effect uploaded */
1758 data->effect_ids[slot-1] = wd->effect_id;
1759 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1760 /* autocenter spring uploaded */
1761 data->slot_autocenter = slot;
1764 case HIDPP_FF_DESTROY_EFFECT:
1765 if (wd->effect_id >= 0)
1766 /* regular effect destroyed */
1767 data->effect_ids[wd->params[0]-1] = -1;
1768 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1769 /* autocenter spring destoyed */
1770 data->slot_autocenter = 0;
1772 case HIDPP_FF_SET_GLOBAL_GAINS:
1773 data->gain = (wd->params[0] << 8) + wd->params[1];
1775 case HIDPP_FF_SET_APERTURE:
1776 data->range = (wd->params[0] << 8) + wd->params[1];
1779 /* no action needed */
1784 atomic_dec(&data->workqueue_size);
1788 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1790 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1796 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1799 wd->effect_id = effect_id;
1800 wd->command = command;
1802 memcpy(wd->params, params, size);
1804 atomic_inc(&data->workqueue_size);
1805 queue_work(data->wq, &wd->work);
1807 /* warn about excessive queue size */
1808 s = atomic_read(&data->workqueue_size);
1809 if (s >= 20 && s % 20 == 0)
1810 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1815 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1817 struct hidpp_ff_private_data *data = dev->ff->private;
1822 /* set common parameters */
1823 params[2] = effect->replay.length >> 8;
1824 params[3] = effect->replay.length & 255;
1825 params[4] = effect->replay.delay >> 8;
1826 params[5] = effect->replay.delay & 255;
1828 switch (effect->type) {
1830 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1831 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1832 params[6] = force >> 8;
1833 params[7] = force & 255;
1834 params[8] = effect->u.constant.envelope.attack_level >> 7;
1835 params[9] = effect->u.constant.envelope.attack_length >> 8;
1836 params[10] = effect->u.constant.envelope.attack_length & 255;
1837 params[11] = effect->u.constant.envelope.fade_level >> 7;
1838 params[12] = effect->u.constant.envelope.fade_length >> 8;
1839 params[13] = effect->u.constant.envelope.fade_length & 255;
1841 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1842 effect->u.constant.level,
1843 effect->direction, force);
1844 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1845 effect->u.constant.envelope.attack_level,
1846 effect->u.constant.envelope.attack_length,
1847 effect->u.constant.envelope.fade_level,
1848 effect->u.constant.envelope.fade_length);
1852 switch (effect->u.periodic.waveform) {
1854 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1857 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1860 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1863 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1866 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1869 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1872 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1873 params[6] = effect->u.periodic.magnitude >> 8;
1874 params[7] = effect->u.periodic.magnitude & 255;
1875 params[8] = effect->u.periodic.offset >> 8;
1876 params[9] = effect->u.periodic.offset & 255;
1877 params[10] = effect->u.periodic.period >> 8;
1878 params[11] = effect->u.periodic.period & 255;
1879 params[12] = effect->u.periodic.phase >> 8;
1880 params[13] = effect->u.periodic.phase & 255;
1881 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1882 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1883 params[16] = effect->u.periodic.envelope.attack_length & 255;
1884 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1885 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1886 params[19] = effect->u.periodic.envelope.fade_length & 255;
1888 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1889 effect->u.periodic.magnitude, effect->direction,
1890 effect->u.periodic.offset,
1891 effect->u.periodic.period,
1892 effect->u.periodic.phase);
1893 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1894 effect->u.periodic.envelope.attack_level,
1895 effect->u.periodic.envelope.attack_length,
1896 effect->u.periodic.envelope.fade_level,
1897 effect->u.periodic.envelope.fade_length);
1901 params[1] = HIDPP_FF_EFFECT_RAMP;
1902 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1903 params[6] = force >> 8;
1904 params[7] = force & 255;
1905 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1906 params[8] = force >> 8;
1907 params[9] = force & 255;
1908 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1909 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1910 params[12] = effect->u.ramp.envelope.attack_length & 255;
1911 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1912 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1913 params[15] = effect->u.ramp.envelope.fade_length & 255;
1915 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1916 effect->u.ramp.start_level,
1917 effect->u.ramp.end_level,
1918 effect->direction, force);
1919 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1920 effect->u.ramp.envelope.attack_level,
1921 effect->u.ramp.envelope.attack_length,
1922 effect->u.ramp.envelope.fade_level,
1923 effect->u.ramp.envelope.fade_length);
1929 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1930 params[6] = effect->u.condition[0].left_saturation >> 9;
1931 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1932 params[8] = effect->u.condition[0].left_coeff >> 8;
1933 params[9] = effect->u.condition[0].left_coeff & 255;
1934 params[10] = effect->u.condition[0].deadband >> 9;
1935 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1936 params[12] = effect->u.condition[0].center >> 8;
1937 params[13] = effect->u.condition[0].center & 255;
1938 params[14] = effect->u.condition[0].right_coeff >> 8;
1939 params[15] = effect->u.condition[0].right_coeff & 255;
1940 params[16] = effect->u.condition[0].right_saturation >> 9;
1941 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1943 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1944 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1945 effect->u.condition[0].left_coeff,
1946 effect->u.condition[0].left_saturation,
1947 effect->u.condition[0].right_coeff,
1948 effect->u.condition[0].right_saturation);
1949 dbg_hid(" deadband=%d, center=%d\n",
1950 effect->u.condition[0].deadband,
1951 effect->u.condition[0].center);
1954 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1958 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1961 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1963 struct hidpp_ff_private_data *data = dev->ff->private;
1966 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1968 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1970 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1973 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1975 struct hidpp_ff_private_data *data = dev->ff->private;
1978 dbg_hid("Erasing effect %d.\n", effect_id);
1980 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1983 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1985 struct hidpp_ff_private_data *data = dev->ff->private;
1988 dbg_hid("Setting autocenter to %d.\n", magnitude);
1990 /* start a standard spring effect */
1991 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1992 /* zero delay and duration */
1993 params[2] = params[3] = params[4] = params[5] = 0;
1994 /* set coeff to 25% of saturation */
1995 params[8] = params[14] = magnitude >> 11;
1996 params[9] = params[15] = (magnitude >> 3) & 255;
1997 params[6] = params[16] = magnitude >> 9;
1998 params[7] = params[17] = (magnitude >> 1) & 255;
1999 /* zero deadband and center */
2000 params[10] = params[11] = params[12] = params[13] = 0;
2002 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2005 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2007 struct hidpp_ff_private_data *data = dev->ff->private;
2010 dbg_hid("Setting gain to %d.\n", gain);
2012 params[0] = gain >> 8;
2013 params[1] = gain & 255;
2014 params[2] = 0; /* no boost */
2017 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2020 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2022 struct hid_device *hid = to_hid_device(dev);
2023 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2024 struct input_dev *idev = hidinput->input;
2025 struct hidpp_ff_private_data *data = idev->ff->private;
2027 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2030 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2032 struct hid_device *hid = to_hid_device(dev);
2033 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2034 struct input_dev *idev = hidinput->input;
2035 struct hidpp_ff_private_data *data = idev->ff->private;
2037 int range = simple_strtoul(buf, NULL, 10);
2039 range = clamp(range, 180, 900);
2041 params[0] = range >> 8;
2042 params[1] = range & 0x00FF;
2044 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2049 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2051 static void hidpp_ff_destroy(struct ff_device *ff)
2053 struct hidpp_ff_private_data *data = ff->private;
2055 kfree(data->effect_ids);
2058 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
2060 struct hid_device *hid = hidpp->hid_dev;
2061 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2062 struct input_dev *dev = hidinput->input;
2063 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2064 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2065 struct ff_device *ff;
2066 struct hidpp_report response;
2067 struct hidpp_ff_private_data *data;
2068 int error, j, num_slots;
2072 hid_err(hid, "Struct input_dev not set!\n");
2076 /* Get firmware release */
2077 version = bcdDevice & 255;
2079 /* Set supported force feedback capabilities */
2080 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2081 set_bit(hidpp_ff_effects[j], dev->ffbit);
2083 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2084 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2086 /* Read number of slots available in device */
2087 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2088 HIDPP_FF_GET_INFO, NULL, 0, &response);
2092 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2097 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2099 error = input_ff_create(dev, num_slots);
2102 hid_err(dev, "Failed to create FF device!\n");
2106 data = kzalloc(sizeof(*data), GFP_KERNEL);
2109 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2110 if (!data->effect_ids) {
2114 data->hidpp = hidpp;
2115 data->feature_index = feature_index;
2116 data->version = version;
2117 data->slot_autocenter = 0;
2118 data->num_effects = num_slots;
2119 for (j = 0; j < num_slots; j++)
2120 data->effect_ids[j] = -1;
2125 ff->upload = hidpp_ff_upload_effect;
2126 ff->erase = hidpp_ff_erase_effect;
2127 ff->playback = hidpp_ff_playback;
2128 ff->set_gain = hidpp_ff_set_gain;
2129 ff->set_autocenter = hidpp_ff_set_autocenter;
2130 ff->destroy = hidpp_ff_destroy;
2133 /* reset all forces */
2134 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2135 HIDPP_FF_RESET_ALL, NULL, 0, &response);
2137 /* Read current Range */
2138 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2139 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
2141 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
2142 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
2144 /* Create sysfs interface */
2145 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2147 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2149 /* Read the current gain values */
2150 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2151 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
2153 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
2154 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
2155 /* ignore boost value at response.fap.params[2] */
2157 /* init the hardware command queue */
2158 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2159 atomic_set(&data->workqueue_size, 0);
2161 /* initialize with zero autocenter to get wheel in usable state */
2162 hidpp_ff_set_autocenter(dev, 0);
2164 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2170 static int hidpp_ff_deinit(struct hid_device *hid)
2172 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2173 struct input_dev *dev = hidinput->input;
2174 struct hidpp_ff_private_data *data;
2177 hid_err(hid, "Struct input_dev not found!\n");
2181 hid_info(hid, "Unloading HID++ force feedback.\n");
2182 data = dev->ff->private;
2184 hid_err(hid, "Private data not found!\n");
2188 destroy_workqueue(data->wq);
2189 device_remove_file(&hid->dev, &dev_attr_range);
2195 /* ************************************************************************** */
2197 /* Device Support */
2199 /* ************************************************************************** */
2201 /* -------------------------------------------------------------------------- */
2202 /* Touchpad HID++ devices */
2203 /* -------------------------------------------------------------------------- */
2205 #define WTP_MANUAL_RESOLUTION 39
2208 struct input_dev *input;
2211 u8 mt_feature_index;
2212 u8 button_feature_index;
2215 unsigned int resolution;
2218 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2219 struct hid_field *field, struct hid_usage *usage,
2220 unsigned long **bit, int *max)
2225 static void wtp_populate_input(struct hidpp_device *hidpp,
2226 struct input_dev *input_dev, bool origin_is_hid_core)
2228 struct wtp_data *wd = hidpp->private_data;
2230 __set_bit(EV_ABS, input_dev->evbit);
2231 __set_bit(EV_KEY, input_dev->evbit);
2232 __clear_bit(EV_REL, input_dev->evbit);
2233 __clear_bit(EV_LED, input_dev->evbit);
2235 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2236 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2237 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2238 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2240 /* Max pressure is not given by the devices, pick one */
2241 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2243 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2245 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2246 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2248 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2250 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2251 INPUT_MT_DROP_UNUSED);
2253 wd->input = input_dev;
2256 static void wtp_touch_event(struct wtp_data *wd,
2257 struct hidpp_touchpad_raw_xy_finger *touch_report)
2261 if (!touch_report->finger_id || touch_report->contact_type)
2262 /* no actual data */
2265 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2267 input_mt_slot(wd->input, slot);
2268 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2269 touch_report->contact_status);
2270 if (touch_report->contact_status) {
2271 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2273 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2274 wd->flip_y ? wd->y_size - touch_report->y :
2276 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2277 touch_report->area);
2281 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2282 struct hidpp_touchpad_raw_xy *raw)
2284 struct wtp_data *wd = hidpp->private_data;
2287 for (i = 0; i < 2; i++)
2288 wtp_touch_event(wd, &(raw->fingers[i]));
2290 if (raw->end_of_frame &&
2291 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2292 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2294 if (raw->end_of_frame || raw->finger_count <= 2) {
2295 input_mt_sync_frame(wd->input);
2296 input_sync(wd->input);
2300 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2302 struct wtp_data *wd = hidpp->private_data;
2303 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2304 (data[7] >> 4) * (data[7] >> 4)) / 2;
2305 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2306 (data[13] >> 4) * (data[13] >> 4)) / 2;
2307 struct hidpp_touchpad_raw_xy raw = {
2308 .timestamp = data[1],
2312 .contact_status = !!data[7],
2313 .x = get_unaligned_le16(&data[3]),
2314 .y = get_unaligned_le16(&data[5]),
2317 .finger_id = data[2],
2320 .contact_status = !!data[13],
2321 .x = get_unaligned_le16(&data[9]),
2322 .y = get_unaligned_le16(&data[11]),
2325 .finger_id = data[8],
2328 .finger_count = wd->maxcontacts,
2330 .end_of_frame = (data[0] >> 7) == 0,
2331 .button = data[0] & 0x01,
2334 wtp_send_raw_xy_event(hidpp, &raw);
2339 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2341 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2342 struct wtp_data *wd = hidpp->private_data;
2343 struct hidpp_report *report = (struct hidpp_report *)data;
2344 struct hidpp_touchpad_raw_xy raw;
2346 if (!wd || !wd->input)
2352 hid_err(hdev, "Received HID report of bad size (%d)",
2356 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2357 input_event(wd->input, EV_KEY, BTN_LEFT,
2358 !!(data[1] & 0x01));
2359 input_event(wd->input, EV_KEY, BTN_RIGHT,
2360 !!(data[1] & 0x02));
2361 input_sync(wd->input);
2366 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2368 case REPORT_ID_HIDPP_LONG:
2369 /* size is already checked in hidpp_raw_event. */
2370 if ((report->fap.feature_index != wd->mt_feature_index) ||
2371 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2373 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2375 wtp_send_raw_xy_event(hidpp, &raw);
2382 static int wtp_get_config(struct hidpp_device *hidpp)
2384 struct wtp_data *wd = hidpp->private_data;
2385 struct hidpp_touchpad_raw_info raw_info = {0};
2389 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2390 &wd->mt_feature_index, &feature_type);
2392 /* means that the device is not powered up */
2395 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2400 wd->x_size = raw_info.x_size;
2401 wd->y_size = raw_info.y_size;
2402 wd->maxcontacts = raw_info.maxcontacts;
2403 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2404 wd->resolution = raw_info.res;
2405 if (!wd->resolution)
2406 wd->resolution = WTP_MANUAL_RESOLUTION;
2411 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2413 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2414 struct wtp_data *wd;
2416 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2421 hidpp->private_data = wd;
2426 static int wtp_connect(struct hid_device *hdev, bool connected)
2428 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2429 struct wtp_data *wd = hidpp->private_data;
2433 ret = wtp_get_config(hidpp);
2435 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2440 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2444 /* ------------------------------------------------------------------------- */
2445 /* Logitech M560 devices */
2446 /* ------------------------------------------------------------------------- */
2449 * Logitech M560 protocol overview
2451 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2452 * the sides buttons are pressed, it sends some keyboard keys events
2453 * instead of buttons ones.
2454 * To complicate things further, the middle button keys sequence
2455 * is different from the odd press and the even press.
2457 * forward button -> Super_R
2458 * backward button -> Super_L+'d' (press only)
2459 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2460 * 2nd time: left-click (press only)
2461 * NB: press-only means that when the button is pressed, the
2462 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2463 * together sequentially; instead when the button is released, no event is
2467 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2468 * the mouse reacts differently:
2469 * - it never sends a keyboard key event
2470 * - for the three mouse button it sends:
2471 * middle button press 11<xx>0a 3500af00...
2472 * side 1 button (forward) press 11<xx>0a 3500b000...
2473 * side 2 button (backward) press 11<xx>0a 3500ae00...
2474 * middle/side1/side2 button release 11<xx>0a 35000000...
2477 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2479 struct m560_private_data {
2480 struct input_dev *input;
2483 /* how buttons are mapped in the report */
2484 #define M560_MOUSE_BTN_LEFT 0x01
2485 #define M560_MOUSE_BTN_RIGHT 0x02
2486 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2487 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2489 #define M560_SUB_ID 0x0a
2490 #define M560_BUTTON_MODE_REGISTER 0x35
2492 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2494 struct hidpp_report response;
2495 struct hidpp_device *hidpp_dev;
2497 hidpp_dev = hid_get_drvdata(hdev);
2499 return hidpp_send_rap_command_sync(
2501 REPORT_ID_HIDPP_SHORT,
2503 M560_BUTTON_MODE_REGISTER,
2504 (u8 *)m560_config_parameter,
2505 sizeof(m560_config_parameter),
2510 static int m560_allocate(struct hid_device *hdev)
2512 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2513 struct m560_private_data *d;
2515 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2520 hidpp->private_data = d;
2525 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2527 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2528 struct m560_private_data *mydata = hidpp->private_data;
2531 if (!mydata || !mydata->input) {
2532 hid_err(hdev, "error in parameter\n");
2537 hid_err(hdev, "error in report\n");
2541 if (data[0] == REPORT_ID_HIDPP_LONG &&
2542 data[2] == M560_SUB_ID && data[6] == 0x00) {
2544 * m560 mouse report for middle, forward and backward button
2547 * data[1] = device-id
2549 * data[5] = 0xaf -> middle
2552 * 0x00 -> release all
2558 input_report_key(mydata->input, BTN_MIDDLE, 1);
2561 input_report_key(mydata->input, BTN_FORWARD, 1);
2564 input_report_key(mydata->input, BTN_BACK, 1);
2567 input_report_key(mydata->input, BTN_BACK, 0);
2568 input_report_key(mydata->input, BTN_FORWARD, 0);
2569 input_report_key(mydata->input, BTN_MIDDLE, 0);
2572 hid_err(hdev, "error in report\n");
2575 input_sync(mydata->input);
2577 } else if (data[0] == 0x02) {
2579 * Logitech M560 mouse report
2581 * data[0] = type (0x02)
2582 * data[1..2] = buttons
2589 input_report_key(mydata->input, BTN_LEFT,
2590 !!(data[1] & M560_MOUSE_BTN_LEFT));
2591 input_report_key(mydata->input, BTN_RIGHT,
2592 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2594 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
2595 input_report_rel(mydata->input, REL_HWHEEL, -1);
2596 input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2598 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
2599 input_report_rel(mydata->input, REL_HWHEEL, 1);
2600 input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2604 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2605 input_report_rel(mydata->input, REL_X, v);
2607 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2608 input_report_rel(mydata->input, REL_Y, v);
2610 v = hid_snto32(data[6], 8);
2611 hidpp_scroll_counter_handle_scroll(
2612 &hidpp->vertical_wheel_counter, v);
2614 input_sync(mydata->input);
2620 static void m560_populate_input(struct hidpp_device *hidpp,
2621 struct input_dev *input_dev, bool origin_is_hid_core)
2623 struct m560_private_data *mydata = hidpp->private_data;
2625 mydata->input = input_dev;
2627 __set_bit(EV_KEY, mydata->input->evbit);
2628 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2629 __set_bit(BTN_RIGHT, mydata->input->keybit);
2630 __set_bit(BTN_LEFT, mydata->input->keybit);
2631 __set_bit(BTN_BACK, mydata->input->keybit);
2632 __set_bit(BTN_FORWARD, mydata->input->keybit);
2634 __set_bit(EV_REL, mydata->input->evbit);
2635 __set_bit(REL_X, mydata->input->relbit);
2636 __set_bit(REL_Y, mydata->input->relbit);
2637 __set_bit(REL_WHEEL, mydata->input->relbit);
2638 __set_bit(REL_HWHEEL, mydata->input->relbit);
2639 __set_bit(REL_WHEEL_HI_RES, mydata->input->relbit);
2640 __set_bit(REL_HWHEEL_HI_RES, mydata->input->relbit);
2643 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2644 struct hid_field *field, struct hid_usage *usage,
2645 unsigned long **bit, int *max)
2650 /* ------------------------------------------------------------------------- */
2651 /* Logitech K400 devices */
2652 /* ------------------------------------------------------------------------- */
2655 * The Logitech K400 keyboard has an embedded touchpad which is seen
2656 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2657 * tap-to-click but the setting is not remembered accross reset, annoying some
2660 * We can toggle this feature from the host by using the feature 0x6010:
2664 struct k400_private_data {
2668 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2670 struct k400_private_data *k400 = hidpp->private_data;
2671 struct hidpp_touchpad_fw_items items = {};
2675 if (!k400->feature_index) {
2676 ret = hidpp_root_get_feature(hidpp,
2677 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2678 &k400->feature_index, &feature_type);
2680 /* means that the device is not powered up */
2684 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2691 static int k400_allocate(struct hid_device *hdev)
2693 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2694 struct k400_private_data *k400;
2696 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2701 hidpp->private_data = k400;
2706 static int k400_connect(struct hid_device *hdev, bool connected)
2708 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2710 if (!disable_tap_to_click)
2713 return k400_disable_tap_to_click(hidpp);
2716 /* ------------------------------------------------------------------------- */
2717 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2718 /* ------------------------------------------------------------------------- */
2720 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2722 static int g920_get_config(struct hidpp_device *hidpp)
2728 /* Find feature and store for later use */
2729 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2730 &feature_index, &feature_type);
2734 ret = hidpp_ff_init(hidpp, feature_index);
2736 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2742 /* -------------------------------------------------------------------------- */
2743 /* High-resolution scroll wheels */
2744 /* -------------------------------------------------------------------------- */
2746 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
2751 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
2752 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
2754 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
2755 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
2756 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
2758 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
2759 ret = hidpp10_enable_scrolling_acceleration(hidpp);
2765 if (multiplier == 0)
2768 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
2769 hid_info(hidpp->hid_dev, "multiplier = %d\n", multiplier);
2773 /* -------------------------------------------------------------------------- */
2774 /* Generic HID++ devices */
2775 /* -------------------------------------------------------------------------- */
2777 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2778 struct hid_field *field, struct hid_usage *usage,
2779 unsigned long **bit, int *max)
2781 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2783 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2784 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2785 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2786 field->application != HID_GD_MOUSE)
2787 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2792 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2793 struct hid_field *field, struct hid_usage *usage,
2794 unsigned long **bit, int *max)
2796 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2798 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2799 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2800 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2801 usage->code == ABS_Y || usage->code == ABS_Z ||
2802 usage->code == ABS_RZ)) {
2803 field->application = HID_GD_MULTIAXIS;
2811 static void hidpp_populate_input(struct hidpp_device *hidpp,
2812 struct input_dev *input, bool origin_is_hid_core)
2814 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2815 wtp_populate_input(hidpp, input, origin_is_hid_core);
2816 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2817 m560_populate_input(hidpp, input, origin_is_hid_core);
2819 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
2820 hidpp->vertical_wheel_counter.dev = input;
2823 static int hidpp_input_configured(struct hid_device *hdev,
2824 struct hid_input *hidinput)
2826 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2827 struct input_dev *input = hidinput->input;
2829 hidpp_populate_input(hidpp, input, true);
2834 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2837 struct hidpp_report *question = hidpp->send_receive_buf;
2838 struct hidpp_report *answer = hidpp->send_receive_buf;
2839 struct hidpp_report *report = (struct hidpp_report *)data;
2843 * If the mutex is locked then we have a pending answer from a
2844 * previously sent command.
2846 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2848 * Check for a correct hidpp20 answer or the corresponding
2851 if (hidpp_match_answer(question, report) ||
2852 hidpp_match_error(question, report)) {
2854 hidpp->answer_available = true;
2855 wake_up(&hidpp->wait);
2857 * This was an answer to a command that this driver sent
2858 * We return 1 to hid-core to avoid forwarding the
2859 * command upstream as it has been treated by the driver
2866 if (unlikely(hidpp_report_is_connect_event(report))) {
2867 atomic_set(&hidpp->connected,
2868 !(report->rap.params[0] & (1 << 6)));
2869 if (schedule_work(&hidpp->work) == 0)
2870 dbg_hid("%s: connect event already queued\n", __func__);
2874 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2875 ret = hidpp20_battery_event(hidpp, data, size);
2878 ret = hidpp_solar_battery_event(hidpp, data, size);
2883 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2884 ret = hidpp10_battery_event(hidpp, data, size);
2892 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2895 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2898 /* Generic HID++ processing. */
2900 case REPORT_ID_HIDPP_VERY_LONG:
2901 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2902 hid_err(hdev, "received hid++ report of bad size (%d)",
2906 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2908 case REPORT_ID_HIDPP_LONG:
2909 if (size != HIDPP_REPORT_LONG_LENGTH) {
2910 hid_err(hdev, "received hid++ report of bad size (%d)",
2914 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2916 case REPORT_ID_HIDPP_SHORT:
2917 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2918 hid_err(hdev, "received hid++ report of bad size (%d)",
2922 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2926 /* If no report is available for further processing, skip calling
2927 * raw_event of subclasses. */
2931 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2932 return wtp_raw_event(hdev, data, size);
2933 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2934 return m560_raw_event(hdev, data, size);
2939 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
2940 struct hid_usage *usage, __s32 value)
2942 /* This function will only be called for scroll events, due to the
2943 * restriction imposed in hidpp_usages.
2945 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2946 struct hidpp_scroll_counter *counter = &hidpp->vertical_wheel_counter;
2947 /* A scroll event may occur before the multiplier has been retrieved or
2948 * the input device set, or high-res scroll enabling may fail. In such
2949 * cases we must return early (falling back to default behaviour) to
2950 * avoid a crash in hidpp_scroll_counter_handle_scroll.
2952 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
2953 || counter->dev == NULL || counter->wheel_multiplier == 0)
2956 hidpp_scroll_counter_handle_scroll(counter, value);
2960 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2962 static atomic_t battery_no = ATOMIC_INIT(0);
2963 struct power_supply_config cfg = { .drv_data = hidpp };
2964 struct power_supply_desc *desc = &hidpp->battery.desc;
2965 enum power_supply_property *battery_props;
2966 struct hidpp_battery *battery;
2967 unsigned int num_battery_props;
2971 if (hidpp->battery.ps)
2974 hidpp->battery.feature_index = 0xff;
2975 hidpp->battery.solar_feature_index = 0xff;
2977 if (hidpp->protocol_major >= 2) {
2978 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
2979 ret = hidpp_solar_request_battery_event(hidpp);
2981 ret = hidpp20_query_battery_info(hidpp);
2985 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2987 ret = hidpp10_query_battery_status(hidpp);
2989 ret = hidpp10_query_battery_mileage(hidpp);
2992 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2994 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
2996 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
2999 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3000 hidpp_battery_props,
3001 sizeof(hidpp_battery_props),
3006 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
3008 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3009 battery_props[num_battery_props++] =
3010 POWER_SUPPLY_PROP_CAPACITY;
3012 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3013 battery_props[num_battery_props++] =
3014 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3016 battery = &hidpp->battery;
3018 n = atomic_inc_return(&battery_no) - 1;
3019 desc->properties = battery_props;
3020 desc->num_properties = num_battery_props;
3021 desc->get_property = hidpp_battery_get_property;
3022 sprintf(battery->name, "hidpp_battery_%ld", n);
3023 desc->name = battery->name;
3024 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3025 desc->use_for_apm = 0;
3027 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3030 if (IS_ERR(battery->ps))
3031 return PTR_ERR(battery->ps);
3033 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3038 static void hidpp_overwrite_name(struct hid_device *hdev)
3040 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3043 if (hidpp->protocol_major < 2)
3046 name = hidpp_get_device_name(hidpp);
3049 hid_err(hdev, "unable to retrieve the name of the device");
3051 dbg_hid("HID++: Got name: %s\n", name);
3052 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3058 static int hidpp_input_open(struct input_dev *dev)
3060 struct hid_device *hid = input_get_drvdata(dev);
3062 return hid_hw_open(hid);
3065 static void hidpp_input_close(struct input_dev *dev)
3067 struct hid_device *hid = input_get_drvdata(dev);
3072 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3074 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3075 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3080 input_set_drvdata(input_dev, hdev);
3081 input_dev->open = hidpp_input_open;
3082 input_dev->close = hidpp_input_close;
3084 input_dev->name = hidpp->name;
3085 input_dev->phys = hdev->phys;
3086 input_dev->uniq = hdev->uniq;
3087 input_dev->id.bustype = hdev->bus;
3088 input_dev->id.vendor = hdev->vendor;
3089 input_dev->id.product = hdev->product;
3090 input_dev->id.version = hdev->version;
3091 input_dev->dev.parent = &hdev->dev;
3096 static void hidpp_connect_event(struct hidpp_device *hidpp)
3098 struct hid_device *hdev = hidpp->hid_dev;
3100 bool connected = atomic_read(&hidpp->connected);
3101 struct input_dev *input;
3102 char *name, *devm_name;
3105 if (hidpp->battery.ps) {
3106 hidpp->battery.online = false;
3107 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3108 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3109 power_supply_changed(hidpp->battery.ps);
3114 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3115 ret = wtp_connect(hdev, connected);
3118 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3119 ret = m560_send_config_command(hdev, connected);
3122 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3123 ret = k400_connect(hdev, connected);
3128 /* the device is already connected, we can ask for its name and
3130 if (!hidpp->protocol_major) {
3131 ret = !hidpp_is_connected(hidpp);
3133 hid_err(hdev, "Can not get the protocol version.\n");
3136 hid_info(hdev, "HID++ %u.%u device connected.\n",
3137 hidpp->protocol_major, hidpp->protocol_minor);
3140 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3141 name = hidpp_get_device_name(hidpp);
3144 "unable to retrieve the name of the device");
3148 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
3153 hidpp->name = devm_name;
3156 hidpp_initialize_battery(hidpp);
3158 /* forward current battery state */
3159 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3160 hidpp10_enable_battery_reporting(hidpp);
3161 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3162 hidpp10_query_battery_mileage(hidpp);
3164 hidpp10_query_battery_status(hidpp);
3165 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3166 hidpp20_query_battery_info(hidpp);
3168 if (hidpp->battery.ps)
3169 power_supply_changed(hidpp->battery.ps);
3171 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3172 hi_res_scroll_enable(hidpp);
3174 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3175 /* if the input nodes are already created, we can stop now */
3178 input = hidpp_allocate_input(hdev);
3180 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3184 hidpp_populate_input(hidpp, input, false);
3186 ret = input_register_device(input);
3188 input_free_device(input);
3190 hidpp->delayed_input = input;
3193 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3195 static struct attribute *sysfs_attrs[] = {
3196 &dev_attr_builtin_power_supply.attr,
3200 static const struct attribute_group ps_attribute_group = {
3201 .attrs = sysfs_attrs
3204 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3206 struct hidpp_device *hidpp;
3209 unsigned int connect_mask = HID_CONNECT_DEFAULT;
3211 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
3216 hidpp->hid_dev = hdev;
3217 hidpp->name = hdev->name;
3218 hid_set_drvdata(hdev, hidpp);
3220 hidpp->quirks = id->driver_data;
3222 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3223 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3225 if (disable_raw_mode) {
3226 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
3227 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
3230 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3231 ret = wtp_allocate(hdev, id);
3234 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3235 ret = m560_allocate(hdev);
3238 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3239 ret = k400_allocate(hdev);
3244 INIT_WORK(&hidpp->work, delayed_work_cb);
3245 mutex_init(&hidpp->send_mutex);
3246 init_waitqueue_head(&hidpp->wait);
3248 /* indicates we are handling the battery properties in the kernel */
3249 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3251 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3254 ret = hid_parse(hdev);
3256 hid_err(hdev, "%s:parse failed\n", __func__);
3257 goto hid_parse_fail;
3260 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3261 connect_mask &= ~HID_CONNECT_HIDINPUT;
3263 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3264 ret = hid_hw_start(hdev, connect_mask);
3266 hid_err(hdev, "hw start failed\n");
3267 goto hid_hw_start_fail;
3269 ret = hid_hw_open(hdev);
3271 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3274 goto hid_hw_start_fail;
3279 /* Allow incoming packets */
3280 hid_device_io_start(hdev);
3282 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3283 hidpp_unifying_init(hidpp);
3285 connected = hidpp_is_connected(hidpp);
3286 atomic_set(&hidpp->connected, connected);
3287 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
3290 hid_err(hdev, "Device not connected");
3291 goto hid_hw_open_failed;
3294 hid_info(hdev, "HID++ %u.%u device connected.\n",
3295 hidpp->protocol_major, hidpp->protocol_minor);
3297 hidpp_overwrite_name(hdev);
3300 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
3301 ret = wtp_get_config(hidpp);
3303 goto hid_hw_open_failed;
3304 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3305 ret = g920_get_config(hidpp);
3307 goto hid_hw_open_failed;
3310 /* Block incoming packets */
3311 hid_device_io_stop(hdev);
3313 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3314 ret = hid_hw_start(hdev, connect_mask);
3316 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3317 goto hid_hw_start_fail;
3321 /* Allow incoming packets */
3322 hid_device_io_start(hdev);
3324 hidpp_connect_event(hidpp);
3329 hid_device_io_stop(hdev);
3330 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3336 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3337 cancel_work_sync(&hidpp->work);
3338 mutex_destroy(&hidpp->send_mutex);
3340 hid_set_drvdata(hdev, NULL);
3344 static void hidpp_remove(struct hid_device *hdev)
3346 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3348 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3350 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3351 hidpp_ff_deinit(hdev);
3355 cancel_work_sync(&hidpp->work);
3356 mutex_destroy(&hidpp->send_mutex);
3359 #define LDJ_DEVICE(product) \
3360 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3361 USB_VENDOR_ID_LOGITECH, (product))
3363 static const struct hid_device_id hidpp_devices[] = {
3364 { /* wireless touchpad */
3366 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3367 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
3368 { /* wireless touchpad T650 */
3370 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3371 { /* wireless touchpad T651 */
3372 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3373 USB_DEVICE_ID_LOGITECH_T651),
3374 .driver_data = HIDPP_QUIRK_CLASS_WTP },
3375 { /* Mouse Logitech Anywhere MX */
3376 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3377 { /* Mouse Logitech Cube */
3378 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3379 { /* Mouse Logitech M335 */
3380 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3381 { /* Mouse Logitech M515 */
3382 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3383 { /* Mouse logitech M560 */
3385 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
3386 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3387 { /* Mouse Logitech M705 (firmware RQM17) */
3388 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3389 { /* Mouse Logitech M705 (firmware RQM67) */
3390 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3391 { /* Mouse Logitech M720 */
3392 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3393 { /* Mouse Logitech MX Anywhere 2 */
3394 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3395 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3396 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3397 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3398 { /* Mouse Logitech MX Anywhere 2S */
3399 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3400 { /* Mouse Logitech MX Master */
3401 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3402 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3403 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3404 { /* Mouse Logitech MX Master 2S */
3405 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3406 { /* Mouse Logitech Performance MX */
3407 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3408 { /* Keyboard logitech K400 */
3410 .driver_data = HIDPP_QUIRK_CLASS_K400 },
3411 { /* Solar Keyboard Logitech K750 */
3413 .driver_data = HIDPP_QUIRK_CLASS_K750 },
3415 { LDJ_DEVICE(HID_ANY_ID) },
3417 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
3418 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
3422 MODULE_DEVICE_TABLE(hid, hidpp_devices);
3424 static const struct hid_usage_id hidpp_usages[] = {
3425 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
3426 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
3429 static struct hid_driver hidpp_driver = {
3430 .name = "logitech-hidpp-device",
3431 .id_table = hidpp_devices,
3432 .probe = hidpp_probe,
3433 .remove = hidpp_remove,
3434 .raw_event = hidpp_raw_event,
3435 .usage_table = hidpp_usages,
3436 .event = hidpp_event,
3437 .input_configured = hidpp_input_configured,
3438 .input_mapping = hidpp_input_mapping,
3439 .input_mapped = hidpp_input_mapped,
3442 module_hid_driver(hidpp_driver);