1 // SPDX-License-Identifier: GPL-2.0-only
3 * HIDPP protocol for Logitech receivers
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/device.h>
14 #include <linux/input.h>
15 #include <linux/usb.h>
16 #include <linux/hid.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/sched/clock.h>
21 #include <linux/kfifo.h>
22 #include <linux/input/mt.h>
23 #include <linux/workqueue.h>
24 #include <linux/atomic.h>
25 #include <linux/fixp-arith.h>
26 #include <asm/unaligned.h>
27 #include "usbhid/usbhid.h"
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
34 static bool disable_tap_to_click;
35 module_param(disable_tap_to_click, bool, 0644);
36 MODULE_PARM_DESC(disable_tap_to_click,
37 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
39 #define REPORT_ID_HIDPP_SHORT 0x10
40 #define REPORT_ID_HIDPP_LONG 0x11
41 #define REPORT_ID_HIDPP_VERY_LONG 0x12
43 #define HIDPP_REPORT_SHORT_LENGTH 7
44 #define HIDPP_REPORT_LONG_LENGTH 20
45 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
47 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
48 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
49 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
51 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
52 #define HIDPP_SUB_ID_ROLLER 0x05
53 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
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_DELAYED_INIT 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)
70 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
71 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
72 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
74 /* These are just aliases for now */
75 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
76 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
78 /* Convenience constant to check for any high-res support. */
79 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
80 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
81 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
83 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
84 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
85 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
86 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
87 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
89 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
92 * There are two hidpp protocols in use, the first version hidpp10 is known
93 * as register access protocol or RAP, the second version hidpp20 is known as
94 * feature access protocol or FAP
96 * Most older devices (including the Unifying usb receiver) use the RAP protocol
97 * where as most newer devices use the FAP protocol. Both protocols are
98 * compatible with the underlying transport, which could be usb, Unifiying, or
99 * bluetooth. The message lengths are defined by the hid vendor specific report
100 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
101 * the HIDPP_LONG report type (total message length 20 bytes)
103 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
104 * messages. The Unifying receiver itself responds to RAP messages (device index
105 * is 0xFF for the receiver), and all messages (short or long) with a device
106 * index between 1 and 6 are passed untouched to the corresponding paired
109 * The paired device can be RAP or FAP, it will receive the message untouched
110 * from the Unifiying receiver.
115 u8 funcindex_clientid;
116 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
122 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
125 struct hidpp_report {
131 u8 rawbytes[sizeof(struct fap)];
135 struct hidpp_battery {
137 u8 solar_feature_index;
138 u8 voltage_feature_index;
139 struct power_supply_desc desc;
140 struct power_supply *ps;
151 * struct hidpp_scroll_counter - Utility class for processing high-resolution
153 * @dev: the input device for which events should be reported.
154 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
155 * @remainder: counts the number of high-resolution units moved since the last
156 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
157 * only be used by class methods.
158 * @direction: direction of last movement (1 or -1)
159 * @last_time: last event time, used to reset remainder after inactivity
161 struct hidpp_scroll_counter {
162 int wheel_multiplier;
165 unsigned long long last_time;
168 struct hidpp_device {
169 struct hid_device *hid_dev;
170 struct input_dev *input;
171 struct mutex send_mutex;
172 void *send_receive_buf;
173 char *name; /* will never be NULL and should not be freed */
174 wait_queue_head_t wait;
175 int very_long_report_length;
176 bool answer_available;
182 struct work_struct work;
183 struct kfifo delayed_work_fifo;
185 struct input_dev *delayed_input;
187 unsigned long quirks;
188 unsigned long capabilities;
189 u8 supported_reports;
191 struct hidpp_battery battery;
192 struct hidpp_scroll_counter vertical_wheel_counter;
194 u8 wireless_feature_index;
197 /* HID++ 1.0 error codes */
198 #define HIDPP_ERROR 0x8f
199 #define HIDPP_ERROR_SUCCESS 0x00
200 #define HIDPP_ERROR_INVALID_SUBID 0x01
201 #define HIDPP_ERROR_INVALID_ADRESS 0x02
202 #define HIDPP_ERROR_INVALID_VALUE 0x03
203 #define HIDPP_ERROR_CONNECT_FAIL 0x04
204 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
205 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
206 #define HIDPP_ERROR_BUSY 0x07
207 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
208 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
209 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
210 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
211 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
212 /* HID++ 2.0 error codes */
213 #define HIDPP20_ERROR 0xff
215 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
217 static int __hidpp_send_report(struct hid_device *hdev,
218 struct hidpp_report *hidpp_report)
220 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
221 int fields_count, ret;
223 switch (hidpp_report->report_id) {
224 case REPORT_ID_HIDPP_SHORT:
225 fields_count = HIDPP_REPORT_SHORT_LENGTH;
227 case REPORT_ID_HIDPP_LONG:
228 fields_count = HIDPP_REPORT_LONG_LENGTH;
230 case REPORT_ID_HIDPP_VERY_LONG:
231 fields_count = hidpp->very_long_report_length;
238 * set the device_index as the receiver, it will be overwritten by
239 * hid_hw_request if needed
241 hidpp_report->device_index = 0xff;
243 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
244 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
246 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
247 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
251 return ret == fields_count ? 0 : -1;
255 * hidpp_send_message_sync() returns 0 in case of success, and something else
256 * in case of a failure.
257 * - If ' something else' is positive, that means that an error has been raised
258 * by the protocol itself.
259 * - If ' something else' is negative, that means that we had a classic error
260 * (-ENOMEM, -EPIPE, etc...)
262 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
263 struct hidpp_report *message,
264 struct hidpp_report *response)
268 mutex_lock(&hidpp->send_mutex);
270 hidpp->send_receive_buf = response;
271 hidpp->answer_available = false;
274 * So that we can later validate the answer when it arrives
277 *response = *message;
279 ret = __hidpp_send_report(hidpp->hid_dev, message);
282 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
283 memset(response, 0, sizeof(struct hidpp_report));
287 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
289 dbg_hid("%s:timeout waiting for response\n", __func__);
290 memset(response, 0, sizeof(struct hidpp_report));
294 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
295 response->rap.sub_id == HIDPP_ERROR) {
296 ret = response->rap.params[1];
297 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
301 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
302 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
303 response->fap.feature_index == HIDPP20_ERROR) {
304 ret = response->fap.params[1];
305 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
310 mutex_unlock(&hidpp->send_mutex);
315 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
316 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
317 struct hidpp_report *response)
319 struct hidpp_report *message;
322 if (param_count > sizeof(message->fap.params))
325 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
329 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
330 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
332 message->report_id = REPORT_ID_HIDPP_LONG;
333 message->fap.feature_index = feat_index;
334 message->fap.funcindex_clientid = funcindex_clientid;
335 memcpy(&message->fap.params, params, param_count);
337 ret = hidpp_send_message_sync(hidpp, message, response);
342 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
343 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
344 struct hidpp_report *response)
346 struct hidpp_report *message;
349 /* Send as long report if short reports are not supported. */
350 if (report_id == REPORT_ID_HIDPP_SHORT &&
351 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
352 report_id = REPORT_ID_HIDPP_LONG;
355 case REPORT_ID_HIDPP_SHORT:
356 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
358 case REPORT_ID_HIDPP_LONG:
359 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
361 case REPORT_ID_HIDPP_VERY_LONG:
362 max_count = hidpp_dev->very_long_report_length - 4;
368 if (param_count > max_count)
371 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
374 message->report_id = report_id;
375 message->rap.sub_id = sub_id;
376 message->rap.reg_address = reg_address;
377 memcpy(&message->rap.params, params, param_count);
379 ret = hidpp_send_message_sync(hidpp_dev, message, response);
384 static void delayed_work_cb(struct work_struct *work)
386 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
388 hidpp_connect_event(hidpp);
391 static inline bool hidpp_match_answer(struct hidpp_report *question,
392 struct hidpp_report *answer)
394 return (answer->fap.feature_index == question->fap.feature_index) &&
395 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
398 static inline bool hidpp_match_error(struct hidpp_report *question,
399 struct hidpp_report *answer)
401 return ((answer->rap.sub_id == HIDPP_ERROR) ||
402 (answer->fap.feature_index == HIDPP20_ERROR)) &&
403 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
404 (answer->fap.params[0] == question->fap.funcindex_clientid);
407 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
408 struct hidpp_report *report)
410 return (hidpp->wireless_feature_index &&
411 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
412 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
413 (report->rap.sub_id == 0x41));
417 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
419 static void hidpp_prefix_name(char **name, int name_length)
421 #define PREFIX_LENGTH 9 /* "Logitech " */
426 if (name_length > PREFIX_LENGTH &&
427 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
428 /* The prefix has is already in the name */
431 new_length = PREFIX_LENGTH + name_length;
432 new_name = kzalloc(new_length, GFP_KERNEL);
436 snprintf(new_name, new_length, "Logitech %s", *name);
444 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
445 * events given a high-resolution wheel
447 * @counter: a hid_scroll_counter struct describing the wheel.
448 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
451 * Given a high-resolution movement, this function converts the movement into
452 * fractions of 120 and emits high-resolution scroll events for the input
453 * device. It also uses the multiplier from &struct hid_scroll_counter to
454 * emit low-resolution scroll events when appropriate for
455 * backwards-compatibility with userspace input libraries.
457 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
458 struct hidpp_scroll_counter *counter,
461 int low_res_value, remainder, direction;
462 unsigned long long now, previous;
464 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
465 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
467 remainder = counter->remainder;
468 direction = hi_res_value > 0 ? 1 : -1;
471 previous = counter->last_time;
472 counter->last_time = now;
474 * Reset the remainder after a period of inactivity or when the
475 * direction changes. This prevents the REL_WHEEL emulation point
476 * from sliding for devices that don't always provide the same
477 * number of movements per detent.
479 if (now - previous > 1000000000 || direction != counter->direction)
482 counter->direction = direction;
483 remainder += hi_res_value;
485 /* Some wheels will rest 7/8ths of a detent from the previous detent
486 * after slow movement, so we want the threshold for low-res events to
487 * be in the middle between two detents (e.g. after 4/8ths) as
488 * opposed to on the detents themselves (8/8ths).
490 if (abs(remainder) >= 60) {
491 /* Add (or subtract) 1 because we want to trigger when the wheel
492 * is half-way to the next detent (i.e. scroll 1 detent after a
493 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
496 low_res_value = remainder / 120;
497 if (low_res_value == 0)
498 low_res_value = (hi_res_value > 0 ? 1 : -1);
499 input_report_rel(input_dev, REL_WHEEL, low_res_value);
500 remainder -= low_res_value * 120;
502 counter->remainder = remainder;
505 /* -------------------------------------------------------------------------- */
506 /* HIDP++ 1.0 commands */
507 /* -------------------------------------------------------------------------- */
509 #define HIDPP_SET_REGISTER 0x80
510 #define HIDPP_GET_REGISTER 0x81
511 #define HIDPP_SET_LONG_REGISTER 0x82
512 #define HIDPP_GET_LONG_REGISTER 0x83
515 * hidpp10_set_register - Modify a HID++ 1.0 register.
516 * @hidpp_dev: the device to set the register on.
517 * @register_address: the address of the register to modify.
518 * @byte: the byte of the register to modify. Should be less than 3.
519 * @mask: mask of the bits to modify
520 * @value: new values for the bits in mask
521 * Return: 0 if successful, otherwise a negative error code.
523 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
524 u8 register_address, u8 byte, u8 mask, u8 value)
526 struct hidpp_report response;
528 u8 params[3] = { 0 };
530 ret = hidpp_send_rap_command_sync(hidpp_dev,
531 REPORT_ID_HIDPP_SHORT,
538 memcpy(params, response.rap.params, 3);
540 params[byte] &= ~mask;
541 params[byte] |= value & mask;
543 return hidpp_send_rap_command_sync(hidpp_dev,
544 REPORT_ID_HIDPP_SHORT,
547 params, 3, &response);
550 #define HIDPP_REG_ENABLE_REPORTS 0x00
551 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
552 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
553 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
554 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
555 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
557 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
559 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
560 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
563 #define HIDPP_REG_FEATURES 0x01
564 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
565 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
567 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
568 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
570 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
571 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
574 #define HIDPP_REG_BATTERY_STATUS 0x07
576 static int hidpp10_battery_status_map_level(u8 param)
582 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
585 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
588 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
591 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
594 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
600 static int hidpp10_battery_status_map_status(u8 param)
606 /* discharging (in use) */
607 status = POWER_SUPPLY_STATUS_DISCHARGING;
609 case 0x21: /* (standard) charging */
610 case 0x24: /* fast charging */
611 case 0x25: /* slow charging */
612 status = POWER_SUPPLY_STATUS_CHARGING;
614 case 0x26: /* topping charge */
615 case 0x22: /* charge complete */
616 status = POWER_SUPPLY_STATUS_FULL;
618 case 0x20: /* unknown */
619 status = POWER_SUPPLY_STATUS_UNKNOWN;
622 * 0x01...0x1F = reserved (not charging)
623 * 0x23 = charging error
624 * 0x27..0xff = reserved
627 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
634 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
636 struct hidpp_report response;
639 ret = hidpp_send_rap_command_sync(hidpp,
640 REPORT_ID_HIDPP_SHORT,
642 HIDPP_REG_BATTERY_STATUS,
647 hidpp->battery.level =
648 hidpp10_battery_status_map_level(response.rap.params[0]);
649 status = hidpp10_battery_status_map_status(response.rap.params[1]);
650 hidpp->battery.status = status;
651 /* the capacity is only available when discharging or full */
652 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
653 status == POWER_SUPPLY_STATUS_FULL;
658 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
660 static int hidpp10_battery_mileage_map_status(u8 param)
664 switch (param >> 6) {
666 /* discharging (in use) */
667 status = POWER_SUPPLY_STATUS_DISCHARGING;
669 case 0x01: /* charging */
670 status = POWER_SUPPLY_STATUS_CHARGING;
672 case 0x02: /* charge complete */
673 status = POWER_SUPPLY_STATUS_FULL;
676 * 0x03 = charging error
679 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
686 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
688 struct hidpp_report response;
691 ret = hidpp_send_rap_command_sync(hidpp,
692 REPORT_ID_HIDPP_SHORT,
694 HIDPP_REG_BATTERY_MILEAGE,
699 hidpp->battery.capacity = response.rap.params[0];
700 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
701 hidpp->battery.status = status;
702 /* the capacity is only available when discharging or full */
703 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
704 status == POWER_SUPPLY_STATUS_FULL;
709 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
711 struct hidpp_report *report = (struct hidpp_report *)data;
712 int status, capacity, level;
715 if (report->report_id != REPORT_ID_HIDPP_SHORT)
718 switch (report->rap.sub_id) {
719 case HIDPP_REG_BATTERY_STATUS:
720 capacity = hidpp->battery.capacity;
721 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
722 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
724 case HIDPP_REG_BATTERY_MILEAGE:
725 capacity = report->rap.params[0];
726 level = hidpp->battery.level;
727 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
733 changed = capacity != hidpp->battery.capacity ||
734 level != hidpp->battery.level ||
735 status != hidpp->battery.status;
737 /* the capacity is only available when discharging or full */
738 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
739 status == POWER_SUPPLY_STATUS_FULL;
742 hidpp->battery.level = level;
743 hidpp->battery.status = status;
744 if (hidpp->battery.ps)
745 power_supply_changed(hidpp->battery.ps);
751 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
752 #define HIDPP_EXTENDED_PAIRING 0x30
753 #define HIDPP_DEVICE_NAME 0x40
755 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
757 struct hidpp_report response;
759 u8 params[1] = { HIDPP_DEVICE_NAME };
763 ret = hidpp_send_rap_command_sync(hidpp_dev,
764 REPORT_ID_HIDPP_SHORT,
765 HIDPP_GET_LONG_REGISTER,
766 HIDPP_REG_PAIRING_INFORMATION,
767 params, 1, &response);
771 len = response.rap.params[1];
773 if (2 + len > sizeof(response.rap.params))
776 if (len < 4) /* logitech devices are usually at least Xddd */
779 name = kzalloc(len + 1, GFP_KERNEL);
783 memcpy(name, &response.rap.params[2], len);
785 /* include the terminating '\0' */
786 hidpp_prefix_name(&name, len + 1);
791 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
793 struct hidpp_report response;
795 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
797 ret = hidpp_send_rap_command_sync(hidpp,
798 REPORT_ID_HIDPP_SHORT,
799 HIDPP_GET_LONG_REGISTER,
800 HIDPP_REG_PAIRING_INFORMATION,
801 params, 1, &response);
806 * We don't care about LE or BE, we will output it as a string
807 * with %4phD, so we need to keep the order.
809 *serial = *((u32 *)&response.rap.params[1]);
813 static int hidpp_unifying_init(struct hidpp_device *hidpp)
815 struct hid_device *hdev = hidpp->hid_dev;
820 ret = hidpp_unifying_get_serial(hidpp, &serial);
824 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
825 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
827 name = hidpp_unifying_get_name(hidpp);
831 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
832 dbg_hid("HID++ Unifying: Got name: %s\n", name);
838 /* -------------------------------------------------------------------------- */
840 /* -------------------------------------------------------------------------- */
842 #define HIDPP_PAGE_ROOT 0x0000
843 #define HIDPP_PAGE_ROOT_IDX 0x00
845 #define CMD_ROOT_GET_FEATURE 0x01
846 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
848 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
849 u8 *feature_index, u8 *feature_type)
851 struct hidpp_report response;
853 u8 params[2] = { feature >> 8, feature & 0x00FF };
855 ret = hidpp_send_fap_command_sync(hidpp,
857 CMD_ROOT_GET_FEATURE,
858 params, 2, &response);
862 if (response.fap.params[0] == 0)
865 *feature_index = response.fap.params[0];
866 *feature_type = response.fap.params[1];
871 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
873 const u8 ping_byte = 0x5a;
874 u8 ping_data[3] = { 0, 0, ping_byte };
875 struct hidpp_report response;
878 ret = hidpp_send_rap_command_sync(hidpp,
879 REPORT_ID_HIDPP_SHORT,
881 CMD_ROOT_GET_PROTOCOL_VERSION,
882 ping_data, sizeof(ping_data), &response);
884 if (ret == HIDPP_ERROR_INVALID_SUBID) {
885 hidpp->protocol_major = 1;
886 hidpp->protocol_minor = 0;
890 /* the device might not be connected */
891 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
895 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
902 if (response.rap.params[2] != ping_byte) {
903 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
904 __func__, response.rap.params[2], ping_byte);
908 hidpp->protocol_major = response.rap.params[0];
909 hidpp->protocol_minor = response.rap.params[1];
912 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
913 hidpp->protocol_major, hidpp->protocol_minor);
917 /* -------------------------------------------------------------------------- */
918 /* 0x0003: Device Information */
919 /* -------------------------------------------------------------------------- */
921 #define HIDPP_PAGE_DEVICE_INFORMATION 0x0003
923 #define CMD_GET_DEVICE_INFO 0x00
925 static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
927 struct hidpp_report response;
932 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
938 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
944 /* See hidpp_unifying_get_serial() */
945 *serial = *((u32 *)&response.rap.params[1]);
949 static int hidpp_serial_init(struct hidpp_device *hidpp)
951 struct hid_device *hdev = hidpp->hid_dev;
955 ret = hidpp_get_serial(hidpp, &serial);
959 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
960 dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
965 /* -------------------------------------------------------------------------- */
966 /* 0x0005: GetDeviceNameType */
967 /* -------------------------------------------------------------------------- */
969 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
971 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
972 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
973 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
975 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
976 u8 feature_index, u8 *nameLength)
978 struct hidpp_report response;
981 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
982 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
985 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
992 *nameLength = response.fap.params[0];
997 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
998 u8 feature_index, u8 char_index, char *device_name, int len_buf)
1000 struct hidpp_report response;
1004 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1005 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
1009 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1016 switch (response.report_id) {
1017 case REPORT_ID_HIDPP_VERY_LONG:
1018 count = hidpp->very_long_report_length - 4;
1020 case REPORT_ID_HIDPP_LONG:
1021 count = HIDPP_REPORT_LONG_LENGTH - 4;
1023 case REPORT_ID_HIDPP_SHORT:
1024 count = HIDPP_REPORT_SHORT_LENGTH - 4;
1030 if (len_buf < count)
1033 for (i = 0; i < count; i++)
1034 device_name[i] = response.fap.params[i];
1039 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1048 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1049 &feature_index, &feature_type);
1053 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1058 name = kzalloc(__name_length + 1, GFP_KERNEL);
1062 while (index < __name_length) {
1063 ret = hidpp_devicenametype_get_device_name(hidpp,
1064 feature_index, index, name + index,
1065 __name_length - index);
1073 /* include the terminating '\0' */
1074 hidpp_prefix_name(&name, __name_length + 1);
1079 /* -------------------------------------------------------------------------- */
1080 /* 0x1000: Battery level status */
1081 /* -------------------------------------------------------------------------- */
1083 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1085 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1086 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1088 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1090 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1091 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1092 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1094 static int hidpp_map_battery_level(int capacity)
1097 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1099 * The spec says this should be < 31 but some devices report 30
1100 * with brand new batteries and Windows reports 30 as "Good".
1102 else if (capacity < 30)
1103 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1104 else if (capacity < 81)
1105 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1106 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1109 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1115 *capacity = data[0];
1116 *next_capacity = data[1];
1117 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1119 /* When discharging, we can rely on the device reported capacity.
1120 * For all other states the device reports 0 (unknown).
1123 case 0: /* discharging (in use) */
1124 status = POWER_SUPPLY_STATUS_DISCHARGING;
1125 *level = hidpp_map_battery_level(*capacity);
1127 case 1: /* recharging */
1128 status = POWER_SUPPLY_STATUS_CHARGING;
1130 case 2: /* charge in final stage */
1131 status = POWER_SUPPLY_STATUS_CHARGING;
1133 case 3: /* charge complete */
1134 status = POWER_SUPPLY_STATUS_FULL;
1135 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1138 case 4: /* recharging below optimal speed */
1139 status = POWER_SUPPLY_STATUS_CHARGING;
1141 /* 5 = invalid battery type
1143 7 = other charging error */
1145 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1152 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1159 struct hidpp_report response;
1161 u8 *params = (u8 *)response.fap.params;
1163 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1164 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1165 NULL, 0, &response);
1166 /* Ignore these intermittent errors */
1167 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1170 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1177 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1184 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1187 struct hidpp_report response;
1189 u8 *params = (u8 *)response.fap.params;
1190 unsigned int level_count, flags;
1192 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1193 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1194 NULL, 0, &response);
1196 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1203 level_count = params[0];
1206 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1207 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1209 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1214 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1218 int status, capacity, next_capacity, level;
1220 if (hidpp->battery.feature_index == 0xff) {
1221 ret = hidpp_root_get_feature(hidpp,
1222 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1223 &hidpp->battery.feature_index,
1229 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1230 hidpp->battery.feature_index,
1232 &next_capacity, &level);
1236 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1237 hidpp->battery.feature_index);
1241 hidpp->battery.status = status;
1242 hidpp->battery.capacity = capacity;
1243 hidpp->battery.level = level;
1244 /* the capacity is only available when discharging or full */
1245 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1246 status == POWER_SUPPLY_STATUS_FULL;
1251 static int hidpp20_battery_event(struct hidpp_device *hidpp,
1254 struct hidpp_report *report = (struct hidpp_report *)data;
1255 int status, capacity, next_capacity, level;
1258 if (report->fap.feature_index != hidpp->battery.feature_index ||
1259 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1262 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1267 /* the capacity is only available when discharging or full */
1268 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1269 status == POWER_SUPPLY_STATUS_FULL;
1271 changed = capacity != hidpp->battery.capacity ||
1272 level != hidpp->battery.level ||
1273 status != hidpp->battery.status;
1276 hidpp->battery.level = level;
1277 hidpp->battery.capacity = capacity;
1278 hidpp->battery.status = status;
1279 if (hidpp->battery.ps)
1280 power_supply_changed(hidpp->battery.ps);
1286 /* -------------------------------------------------------------------------- */
1287 /* 0x1001: Battery voltage */
1288 /* -------------------------------------------------------------------------- */
1290 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1292 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1294 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1296 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1297 int *level, int *charge_type)
1301 long flags = (long) data[2];
1302 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1305 switch (flags & 0x07) {
1307 status = POWER_SUPPLY_STATUS_CHARGING;
1310 status = POWER_SUPPLY_STATUS_FULL;
1311 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1314 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1317 status = POWER_SUPPLY_STATUS_UNKNOWN;
1321 status = POWER_SUPPLY_STATUS_DISCHARGING;
1323 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1324 if (test_bit(3, &flags)) {
1325 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1327 if (test_bit(4, &flags)) {
1328 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1330 if (test_bit(5, &flags)) {
1331 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1334 *voltage = get_unaligned_be16(data);
1339 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1341 int *status, int *voltage,
1342 int *level, int *charge_type)
1344 struct hidpp_report response;
1346 u8 *params = (u8 *)response.fap.params;
1348 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1349 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1350 NULL, 0, &response);
1353 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1360 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1362 *status = hidpp20_battery_map_status_voltage(params, voltage,
1363 level, charge_type);
1368 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1372 int status, voltage, level, charge_type;
1374 if (hidpp->battery.voltage_feature_index == 0xff) {
1375 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1376 &hidpp->battery.voltage_feature_index,
1382 ret = hidpp20_battery_get_battery_voltage(hidpp,
1383 hidpp->battery.voltage_feature_index,
1384 &status, &voltage, &level, &charge_type);
1389 hidpp->battery.status = status;
1390 hidpp->battery.voltage = voltage;
1391 hidpp->battery.level = level;
1392 hidpp->battery.charge_type = charge_type;
1393 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1398 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1401 struct hidpp_report *report = (struct hidpp_report *)data;
1402 int status, voltage, level, charge_type;
1404 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1405 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1408 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1409 &level, &charge_type);
1411 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1413 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1414 hidpp->battery.voltage = voltage;
1415 hidpp->battery.status = status;
1416 hidpp->battery.level = level;
1417 hidpp->battery.charge_type = charge_type;
1418 if (hidpp->battery.ps)
1419 power_supply_changed(hidpp->battery.ps);
1424 static enum power_supply_property hidpp_battery_props[] = {
1425 POWER_SUPPLY_PROP_ONLINE,
1426 POWER_SUPPLY_PROP_STATUS,
1427 POWER_SUPPLY_PROP_SCOPE,
1428 POWER_SUPPLY_PROP_MODEL_NAME,
1429 POWER_SUPPLY_PROP_MANUFACTURER,
1430 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1431 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1432 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1433 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1436 static int hidpp_battery_get_property(struct power_supply *psy,
1437 enum power_supply_property psp,
1438 union power_supply_propval *val)
1440 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1444 case POWER_SUPPLY_PROP_STATUS:
1445 val->intval = hidpp->battery.status;
1447 case POWER_SUPPLY_PROP_CAPACITY:
1448 val->intval = hidpp->battery.capacity;
1450 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1451 val->intval = hidpp->battery.level;
1453 case POWER_SUPPLY_PROP_SCOPE:
1454 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1456 case POWER_SUPPLY_PROP_ONLINE:
1457 val->intval = hidpp->battery.online;
1459 case POWER_SUPPLY_PROP_MODEL_NAME:
1460 if (!strncmp(hidpp->name, "Logitech ", 9))
1461 val->strval = hidpp->name + 9;
1463 val->strval = hidpp->name;
1465 case POWER_SUPPLY_PROP_MANUFACTURER:
1466 val->strval = "Logitech";
1468 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1469 val->strval = hidpp->hid_dev->uniq;
1471 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1472 /* hardware reports voltage in in mV. sysfs expects uV */
1473 val->intval = hidpp->battery.voltage * 1000;
1475 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1476 val->intval = hidpp->battery.charge_type;
1486 /* -------------------------------------------------------------------------- */
1487 /* 0x1d4b: Wireless device status */
1488 /* -------------------------------------------------------------------------- */
1489 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1491 static int hidpp_get_wireless_feature_index(struct hidpp_device *hidpp, u8 *feature_index)
1496 ret = hidpp_root_get_feature(hidpp,
1497 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1498 feature_index, &feature_type);
1503 /* -------------------------------------------------------------------------- */
1504 /* 0x2120: Hi-resolution scrolling */
1505 /* -------------------------------------------------------------------------- */
1507 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1509 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1511 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1512 bool enabled, u8 *multiplier)
1518 struct hidpp_report response;
1520 ret = hidpp_root_get_feature(hidpp,
1521 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1527 params[0] = enabled ? BIT(0) : 0;
1528 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1529 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1530 params, sizeof(params), &response);
1533 *multiplier = response.fap.params[1];
1537 /* -------------------------------------------------------------------------- */
1538 /* 0x2121: HiRes Wheel */
1539 /* -------------------------------------------------------------------------- */
1541 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1543 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1544 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1546 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1552 struct hidpp_report response;
1554 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1555 &feature_index, &feature_type);
1557 goto return_default;
1559 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1560 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1561 NULL, 0, &response);
1563 goto return_default;
1565 *multiplier = response.fap.params[0];
1568 hid_warn(hidpp->hid_dev,
1569 "Couldn't get wheel multiplier (error %d)\n", ret);
1573 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1574 bool high_resolution, bool use_hidpp)
1580 struct hidpp_report response;
1582 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1583 &feature_index, &feature_type);
1587 params[0] = (invert ? BIT(2) : 0) |
1588 (high_resolution ? BIT(1) : 0) |
1589 (use_hidpp ? BIT(0) : 0);
1591 return hidpp_send_fap_command_sync(hidpp, feature_index,
1592 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1593 params, sizeof(params), &response);
1596 /* -------------------------------------------------------------------------- */
1597 /* 0x4301: Solar Keyboard */
1598 /* -------------------------------------------------------------------------- */
1600 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1602 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1604 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1605 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1606 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1608 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1610 struct hidpp_report response;
1611 u8 params[2] = { 1, 1 };
1615 if (hidpp->battery.feature_index == 0xff) {
1616 ret = hidpp_root_get_feature(hidpp,
1617 HIDPP_PAGE_SOLAR_KEYBOARD,
1618 &hidpp->battery.solar_feature_index,
1624 ret = hidpp_send_fap_command_sync(hidpp,
1625 hidpp->battery.solar_feature_index,
1626 CMD_SOLAR_SET_LIGHT_MEASURE,
1627 params, 2, &response);
1629 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1636 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1641 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1644 struct hidpp_report *report = (struct hidpp_report *)data;
1645 int capacity, lux, status;
1648 function = report->fap.funcindex_clientid;
1651 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1652 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1653 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1654 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1657 capacity = report->fap.params[0];
1660 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1661 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1663 status = POWER_SUPPLY_STATUS_CHARGING;
1665 status = POWER_SUPPLY_STATUS_DISCHARGING;
1667 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1669 if (capacity < hidpp->battery.capacity)
1670 status = POWER_SUPPLY_STATUS_DISCHARGING;
1672 status = POWER_SUPPLY_STATUS_CHARGING;
1676 if (capacity == 100)
1677 status = POWER_SUPPLY_STATUS_FULL;
1679 hidpp->battery.online = true;
1680 if (capacity != hidpp->battery.capacity ||
1681 status != hidpp->battery.status) {
1682 hidpp->battery.capacity = capacity;
1683 hidpp->battery.status = status;
1684 if (hidpp->battery.ps)
1685 power_supply_changed(hidpp->battery.ps);
1691 /* -------------------------------------------------------------------------- */
1692 /* 0x6010: Touchpad FW items */
1693 /* -------------------------------------------------------------------------- */
1695 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1697 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1699 struct hidpp_touchpad_fw_items {
1701 uint8_t desired_state;
1707 * send a set state command to the device by reading the current items->state
1708 * field. items is then filled with the current state.
1710 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1712 struct hidpp_touchpad_fw_items *items)
1714 struct hidpp_report response;
1716 u8 *params = (u8 *)response.fap.params;
1718 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1719 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1722 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1729 items->presence = params[0];
1730 items->desired_state = params[1];
1731 items->state = params[2];
1732 items->persistent = params[3];
1737 /* -------------------------------------------------------------------------- */
1738 /* 0x6100: TouchPadRawXY */
1739 /* -------------------------------------------------------------------------- */
1741 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1743 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1744 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1746 #define EVENT_TOUCHPAD_RAW_XY 0x00
1748 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1749 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1751 struct hidpp_touchpad_raw_info {
1762 struct hidpp_touchpad_raw_xy_finger {
1772 struct hidpp_touchpad_raw_xy {
1774 struct hidpp_touchpad_raw_xy_finger fingers[2];
1781 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1782 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1784 struct hidpp_report response;
1786 u8 *params = (u8 *)response.fap.params;
1788 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1789 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1792 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1799 raw_info->x_size = get_unaligned_be16(¶ms[0]);
1800 raw_info->y_size = get_unaligned_be16(¶ms[2]);
1801 raw_info->z_range = params[4];
1802 raw_info->area_range = params[5];
1803 raw_info->maxcontacts = params[7];
1804 raw_info->origin = params[8];
1805 /* res is given in unit per inch */
1806 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
1811 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1812 u8 feature_index, bool send_raw_reports,
1813 bool sensor_enhanced_settings)
1815 struct hidpp_report response;
1819 * bit 0 - enable raw
1820 * bit 1 - 16bit Z, no area
1821 * bit 2 - enhanced sensitivity
1822 * bit 3 - width, height (4 bits each) instead of area
1823 * bit 4 - send raw + gestures (degrades smoothness)
1824 * remaining bits - reserved
1826 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1828 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1829 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
1832 static void hidpp_touchpad_touch_event(u8 *data,
1833 struct hidpp_touchpad_raw_xy_finger *finger)
1835 u8 x_m = data[0] << 2;
1836 u8 y_m = data[2] << 2;
1838 finger->x = x_m << 6 | data[1];
1839 finger->y = y_m << 6 | data[3];
1841 finger->contact_type = data[0] >> 6;
1842 finger->contact_status = data[2] >> 6;
1844 finger->z = data[4];
1845 finger->area = data[5];
1846 finger->finger_id = data[6] >> 4;
1849 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1850 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1852 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1853 raw_xy->end_of_frame = data[8] & 0x01;
1854 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1855 raw_xy->finger_count = data[15] & 0x0f;
1856 raw_xy->button = (data[8] >> 2) & 0x01;
1858 if (raw_xy->finger_count) {
1859 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1860 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1864 /* -------------------------------------------------------------------------- */
1865 /* 0x8123: Force feedback support */
1866 /* -------------------------------------------------------------------------- */
1868 #define HIDPP_FF_GET_INFO 0x01
1869 #define HIDPP_FF_RESET_ALL 0x11
1870 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1871 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1872 #define HIDPP_FF_DESTROY_EFFECT 0x41
1873 #define HIDPP_FF_GET_APERTURE 0x51
1874 #define HIDPP_FF_SET_APERTURE 0x61
1875 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1876 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1878 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1879 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1880 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1881 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1883 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1884 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1885 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1886 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1887 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1888 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1889 #define HIDPP_FF_EFFECT_SPRING 0x06
1890 #define HIDPP_FF_EFFECT_DAMPER 0x07
1891 #define HIDPP_FF_EFFECT_FRICTION 0x08
1892 #define HIDPP_FF_EFFECT_INERTIA 0x09
1893 #define HIDPP_FF_EFFECT_RAMP 0x0A
1895 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1897 #define HIDPP_FF_EFFECTID_NONE -1
1898 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1899 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
1901 #define HIDPP_FF_MAX_PARAMS 20
1902 #define HIDPP_FF_RESERVED_SLOTS 1
1904 struct hidpp_ff_private_data {
1905 struct hidpp_device *hidpp;
1913 struct workqueue_struct *wq;
1914 atomic_t workqueue_size;
1917 struct hidpp_ff_work_data {
1918 struct work_struct work;
1919 struct hidpp_ff_private_data *data;
1922 u8 params[HIDPP_FF_MAX_PARAMS];
1926 static const signed short hidpp_ff_effects[] = {
1941 static const signed short hidpp_ff_effects_v2[] = {
1948 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1949 HIDPP_FF_EFFECT_SPRING,
1950 HIDPP_FF_EFFECT_FRICTION,
1951 HIDPP_FF_EFFECT_DAMPER,
1952 HIDPP_FF_EFFECT_INERTIA
1955 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1963 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1967 for (i = 0; i < data->num_effects; i++)
1968 if (data->effect_ids[i] == effect_id)
1974 static void hidpp_ff_work_handler(struct work_struct *w)
1976 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1977 struct hidpp_ff_private_data *data = wd->data;
1978 struct hidpp_report response;
1982 /* add slot number if needed */
1983 switch (wd->effect_id) {
1984 case HIDPP_FF_EFFECTID_AUTOCENTER:
1985 wd->params[0] = data->slot_autocenter;
1987 case HIDPP_FF_EFFECTID_NONE:
1988 /* leave slot as zero */
1991 /* find current slot for effect */
1992 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1996 /* send command and wait for reply */
1997 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1998 wd->command, wd->params, wd->size, &response);
2001 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2005 /* parse return data */
2006 switch (wd->command) {
2007 case HIDPP_FF_DOWNLOAD_EFFECT:
2008 slot = response.fap.params[0];
2009 if (slot > 0 && slot <= data->num_effects) {
2010 if (wd->effect_id >= 0)
2011 /* regular effect uploaded */
2012 data->effect_ids[slot-1] = wd->effect_id;
2013 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2014 /* autocenter spring uploaded */
2015 data->slot_autocenter = slot;
2018 case HIDPP_FF_DESTROY_EFFECT:
2019 if (wd->effect_id >= 0)
2020 /* regular effect destroyed */
2021 data->effect_ids[wd->params[0]-1] = -1;
2022 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2023 /* autocenter spring destoyed */
2024 data->slot_autocenter = 0;
2026 case HIDPP_FF_SET_GLOBAL_GAINS:
2027 data->gain = (wd->params[0] << 8) + wd->params[1];
2029 case HIDPP_FF_SET_APERTURE:
2030 data->range = (wd->params[0] << 8) + wd->params[1];
2033 /* no action needed */
2038 atomic_dec(&data->workqueue_size);
2042 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2044 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2050 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2053 wd->effect_id = effect_id;
2054 wd->command = command;
2056 memcpy(wd->params, params, size);
2058 atomic_inc(&data->workqueue_size);
2059 queue_work(data->wq, &wd->work);
2061 /* warn about excessive queue size */
2062 s = atomic_read(&data->workqueue_size);
2063 if (s >= 20 && s % 20 == 0)
2064 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2069 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2071 struct hidpp_ff_private_data *data = dev->ff->private;
2076 /* set common parameters */
2077 params[2] = effect->replay.length >> 8;
2078 params[3] = effect->replay.length & 255;
2079 params[4] = effect->replay.delay >> 8;
2080 params[5] = effect->replay.delay & 255;
2082 switch (effect->type) {
2084 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2085 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2086 params[6] = force >> 8;
2087 params[7] = force & 255;
2088 params[8] = effect->u.constant.envelope.attack_level >> 7;
2089 params[9] = effect->u.constant.envelope.attack_length >> 8;
2090 params[10] = effect->u.constant.envelope.attack_length & 255;
2091 params[11] = effect->u.constant.envelope.fade_level >> 7;
2092 params[12] = effect->u.constant.envelope.fade_length >> 8;
2093 params[13] = effect->u.constant.envelope.fade_length & 255;
2095 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2096 effect->u.constant.level,
2097 effect->direction, force);
2098 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2099 effect->u.constant.envelope.attack_level,
2100 effect->u.constant.envelope.attack_length,
2101 effect->u.constant.envelope.fade_level,
2102 effect->u.constant.envelope.fade_length);
2106 switch (effect->u.periodic.waveform) {
2108 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2111 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2114 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2117 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2120 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2123 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2126 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2127 params[6] = effect->u.periodic.magnitude >> 8;
2128 params[7] = effect->u.periodic.magnitude & 255;
2129 params[8] = effect->u.periodic.offset >> 8;
2130 params[9] = effect->u.periodic.offset & 255;
2131 params[10] = effect->u.periodic.period >> 8;
2132 params[11] = effect->u.periodic.period & 255;
2133 params[12] = effect->u.periodic.phase >> 8;
2134 params[13] = effect->u.periodic.phase & 255;
2135 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2136 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2137 params[16] = effect->u.periodic.envelope.attack_length & 255;
2138 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2139 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2140 params[19] = effect->u.periodic.envelope.fade_length & 255;
2142 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2143 effect->u.periodic.magnitude, effect->direction,
2144 effect->u.periodic.offset,
2145 effect->u.periodic.period,
2146 effect->u.periodic.phase);
2147 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2148 effect->u.periodic.envelope.attack_level,
2149 effect->u.periodic.envelope.attack_length,
2150 effect->u.periodic.envelope.fade_level,
2151 effect->u.periodic.envelope.fade_length);
2155 params[1] = HIDPP_FF_EFFECT_RAMP;
2156 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2157 params[6] = force >> 8;
2158 params[7] = force & 255;
2159 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2160 params[8] = force >> 8;
2161 params[9] = force & 255;
2162 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2163 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2164 params[12] = effect->u.ramp.envelope.attack_length & 255;
2165 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2166 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2167 params[15] = effect->u.ramp.envelope.fade_length & 255;
2169 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2170 effect->u.ramp.start_level,
2171 effect->u.ramp.end_level,
2172 effect->direction, force);
2173 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2174 effect->u.ramp.envelope.attack_level,
2175 effect->u.ramp.envelope.attack_length,
2176 effect->u.ramp.envelope.fade_level,
2177 effect->u.ramp.envelope.fade_length);
2183 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2184 params[6] = effect->u.condition[0].left_saturation >> 9;
2185 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2186 params[8] = effect->u.condition[0].left_coeff >> 8;
2187 params[9] = effect->u.condition[0].left_coeff & 255;
2188 params[10] = effect->u.condition[0].deadband >> 9;
2189 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2190 params[12] = effect->u.condition[0].center >> 8;
2191 params[13] = effect->u.condition[0].center & 255;
2192 params[14] = effect->u.condition[0].right_coeff >> 8;
2193 params[15] = effect->u.condition[0].right_coeff & 255;
2194 params[16] = effect->u.condition[0].right_saturation >> 9;
2195 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2197 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2198 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2199 effect->u.condition[0].left_coeff,
2200 effect->u.condition[0].left_saturation,
2201 effect->u.condition[0].right_coeff,
2202 effect->u.condition[0].right_saturation);
2203 dbg_hid(" deadband=%d, center=%d\n",
2204 effect->u.condition[0].deadband,
2205 effect->u.condition[0].center);
2208 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2212 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2215 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2217 struct hidpp_ff_private_data *data = dev->ff->private;
2220 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2222 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2224 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2227 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2229 struct hidpp_ff_private_data *data = dev->ff->private;
2232 dbg_hid("Erasing effect %d.\n", effect_id);
2234 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2237 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2239 struct hidpp_ff_private_data *data = dev->ff->private;
2240 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2242 dbg_hid("Setting autocenter to %d.\n", magnitude);
2244 /* start a standard spring effect */
2245 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2246 /* zero delay and duration */
2247 params[2] = params[3] = params[4] = params[5] = 0;
2248 /* set coeff to 25% of saturation */
2249 params[8] = params[14] = magnitude >> 11;
2250 params[9] = params[15] = (magnitude >> 3) & 255;
2251 params[6] = params[16] = magnitude >> 9;
2252 params[7] = params[17] = (magnitude >> 1) & 255;
2253 /* zero deadband and center */
2254 params[10] = params[11] = params[12] = params[13] = 0;
2256 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2259 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2261 struct hidpp_ff_private_data *data = dev->ff->private;
2264 dbg_hid("Setting gain to %d.\n", gain);
2266 params[0] = gain >> 8;
2267 params[1] = gain & 255;
2268 params[2] = 0; /* no boost */
2271 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2274 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2276 struct hid_device *hid = to_hid_device(dev);
2277 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2278 struct input_dev *idev = hidinput->input;
2279 struct hidpp_ff_private_data *data = idev->ff->private;
2281 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2284 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2286 struct hid_device *hid = to_hid_device(dev);
2287 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2288 struct input_dev *idev = hidinput->input;
2289 struct hidpp_ff_private_data *data = idev->ff->private;
2291 int range = simple_strtoul(buf, NULL, 10);
2293 range = clamp(range, 180, 900);
2295 params[0] = range >> 8;
2296 params[1] = range & 0x00FF;
2298 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2303 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2305 static void hidpp_ff_destroy(struct ff_device *ff)
2307 struct hidpp_ff_private_data *data = ff->private;
2308 struct hid_device *hid = data->hidpp->hid_dev;
2310 hid_info(hid, "Unloading HID++ force feedback.\n");
2312 device_remove_file(&hid->dev, &dev_attr_range);
2313 destroy_workqueue(data->wq);
2314 kfree(data->effect_ids);
2317 static int hidpp_ff_init(struct hidpp_device *hidpp,
2318 struct hidpp_ff_private_data *data)
2320 struct hid_device *hid = hidpp->hid_dev;
2321 struct hid_input *hidinput;
2322 struct input_dev *dev;
2323 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2324 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2325 struct ff_device *ff;
2326 int error, j, num_slots = data->num_effects;
2329 if (list_empty(&hid->inputs)) {
2330 hid_err(hid, "no inputs found\n");
2333 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2334 dev = hidinput->input;
2337 hid_err(hid, "Struct input_dev not set!\n");
2341 /* Get firmware release */
2342 version = bcdDevice & 255;
2344 /* Set supported force feedback capabilities */
2345 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2346 set_bit(hidpp_ff_effects[j], dev->ffbit);
2348 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2349 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2351 error = input_ff_create(dev, num_slots);
2354 hid_err(dev, "Failed to create FF device!\n");
2358 * Create a copy of passed data, so we can transfer memory
2359 * ownership to FF core
2361 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2364 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2365 if (!data->effect_ids) {
2369 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2371 kfree(data->effect_ids);
2376 data->hidpp = hidpp;
2377 data->version = version;
2378 for (j = 0; j < num_slots; j++)
2379 data->effect_ids[j] = -1;
2384 ff->upload = hidpp_ff_upload_effect;
2385 ff->erase = hidpp_ff_erase_effect;
2386 ff->playback = hidpp_ff_playback;
2387 ff->set_gain = hidpp_ff_set_gain;
2388 ff->set_autocenter = hidpp_ff_set_autocenter;
2389 ff->destroy = hidpp_ff_destroy;
2391 /* Create sysfs interface */
2392 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2394 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2396 /* init the hardware command queue */
2397 atomic_set(&data->workqueue_size, 0);
2399 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2405 /* ************************************************************************** */
2407 /* Device Support */
2409 /* ************************************************************************** */
2411 /* -------------------------------------------------------------------------- */
2412 /* Touchpad HID++ devices */
2413 /* -------------------------------------------------------------------------- */
2415 #define WTP_MANUAL_RESOLUTION 39
2420 u8 mt_feature_index;
2421 u8 button_feature_index;
2424 unsigned int resolution;
2427 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2428 struct hid_field *field, struct hid_usage *usage,
2429 unsigned long **bit, int *max)
2434 static void wtp_populate_input(struct hidpp_device *hidpp,
2435 struct input_dev *input_dev)
2437 struct wtp_data *wd = hidpp->private_data;
2439 __set_bit(EV_ABS, input_dev->evbit);
2440 __set_bit(EV_KEY, input_dev->evbit);
2441 __clear_bit(EV_REL, input_dev->evbit);
2442 __clear_bit(EV_LED, input_dev->evbit);
2444 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2445 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2446 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2447 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2449 /* Max pressure is not given by the devices, pick one */
2450 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2452 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2454 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2455 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2457 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2459 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2460 INPUT_MT_DROP_UNUSED);
2463 static void wtp_touch_event(struct hidpp_device *hidpp,
2464 struct hidpp_touchpad_raw_xy_finger *touch_report)
2466 struct wtp_data *wd = hidpp->private_data;
2469 if (!touch_report->finger_id || touch_report->contact_type)
2470 /* no actual data */
2473 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2475 input_mt_slot(hidpp->input, slot);
2476 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2477 touch_report->contact_status);
2478 if (touch_report->contact_status) {
2479 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2481 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2482 wd->flip_y ? wd->y_size - touch_report->y :
2484 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2485 touch_report->area);
2489 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2490 struct hidpp_touchpad_raw_xy *raw)
2494 for (i = 0; i < 2; i++)
2495 wtp_touch_event(hidpp, &(raw->fingers[i]));
2497 if (raw->end_of_frame &&
2498 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2499 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2501 if (raw->end_of_frame || raw->finger_count <= 2) {
2502 input_mt_sync_frame(hidpp->input);
2503 input_sync(hidpp->input);
2507 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2509 struct wtp_data *wd = hidpp->private_data;
2510 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2511 (data[7] >> 4) * (data[7] >> 4)) / 2;
2512 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2513 (data[13] >> 4) * (data[13] >> 4)) / 2;
2514 struct hidpp_touchpad_raw_xy raw = {
2515 .timestamp = data[1],
2519 .contact_status = !!data[7],
2520 .x = get_unaligned_le16(&data[3]),
2521 .y = get_unaligned_le16(&data[5]),
2524 .finger_id = data[2],
2527 .contact_status = !!data[13],
2528 .x = get_unaligned_le16(&data[9]),
2529 .y = get_unaligned_le16(&data[11]),
2532 .finger_id = data[8],
2535 .finger_count = wd->maxcontacts,
2537 .end_of_frame = (data[0] >> 7) == 0,
2538 .button = data[0] & 0x01,
2541 wtp_send_raw_xy_event(hidpp, &raw);
2546 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2548 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2549 struct wtp_data *wd = hidpp->private_data;
2550 struct hidpp_report *report = (struct hidpp_report *)data;
2551 struct hidpp_touchpad_raw_xy raw;
2553 if (!wd || !hidpp->input)
2559 hid_err(hdev, "Received HID report of bad size (%d)",
2563 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2564 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2565 !!(data[1] & 0x01));
2566 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2567 !!(data[1] & 0x02));
2568 input_sync(hidpp->input);
2573 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2575 case REPORT_ID_HIDPP_LONG:
2576 /* size is already checked in hidpp_raw_event. */
2577 if ((report->fap.feature_index != wd->mt_feature_index) ||
2578 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2580 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2582 wtp_send_raw_xy_event(hidpp, &raw);
2589 static int wtp_get_config(struct hidpp_device *hidpp)
2591 struct wtp_data *wd = hidpp->private_data;
2592 struct hidpp_touchpad_raw_info raw_info = {0};
2596 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2597 &wd->mt_feature_index, &feature_type);
2599 /* means that the device is not powered up */
2602 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2607 wd->x_size = raw_info.x_size;
2608 wd->y_size = raw_info.y_size;
2609 wd->maxcontacts = raw_info.maxcontacts;
2610 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2611 wd->resolution = raw_info.res;
2612 if (!wd->resolution)
2613 wd->resolution = WTP_MANUAL_RESOLUTION;
2618 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2620 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2621 struct wtp_data *wd;
2623 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2628 hidpp->private_data = wd;
2633 static int wtp_connect(struct hid_device *hdev, bool connected)
2635 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2636 struct wtp_data *wd = hidpp->private_data;
2640 ret = wtp_get_config(hidpp);
2642 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2647 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2651 /* ------------------------------------------------------------------------- */
2652 /* Logitech M560 devices */
2653 /* ------------------------------------------------------------------------- */
2656 * Logitech M560 protocol overview
2658 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2659 * the sides buttons are pressed, it sends some keyboard keys events
2660 * instead of buttons ones.
2661 * To complicate things further, the middle button keys sequence
2662 * is different from the odd press and the even press.
2664 * forward button -> Super_R
2665 * backward button -> Super_L+'d' (press only)
2666 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2667 * 2nd time: left-click (press only)
2668 * NB: press-only means that when the button is pressed, the
2669 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2670 * together sequentially; instead when the button is released, no event is
2674 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2675 * the mouse reacts differently:
2676 * - it never sends a keyboard key event
2677 * - for the three mouse button it sends:
2678 * middle button press 11<xx>0a 3500af00...
2679 * side 1 button (forward) press 11<xx>0a 3500b000...
2680 * side 2 button (backward) press 11<xx>0a 3500ae00...
2681 * middle/side1/side2 button release 11<xx>0a 35000000...
2684 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2686 /* how buttons are mapped in the report */
2687 #define M560_MOUSE_BTN_LEFT 0x01
2688 #define M560_MOUSE_BTN_RIGHT 0x02
2689 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2690 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2692 #define M560_SUB_ID 0x0a
2693 #define M560_BUTTON_MODE_REGISTER 0x35
2695 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2697 struct hidpp_report response;
2698 struct hidpp_device *hidpp_dev;
2700 hidpp_dev = hid_get_drvdata(hdev);
2702 return hidpp_send_rap_command_sync(
2704 REPORT_ID_HIDPP_SHORT,
2706 M560_BUTTON_MODE_REGISTER,
2707 (u8 *)m560_config_parameter,
2708 sizeof(m560_config_parameter),
2713 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2715 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2718 if (!hidpp->input) {
2719 hid_err(hdev, "error in parameter\n");
2724 hid_err(hdev, "error in report\n");
2728 if (data[0] == REPORT_ID_HIDPP_LONG &&
2729 data[2] == M560_SUB_ID && data[6] == 0x00) {
2731 * m560 mouse report for middle, forward and backward button
2734 * data[1] = device-id
2736 * data[5] = 0xaf -> middle
2739 * 0x00 -> release all
2745 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2748 input_report_key(hidpp->input, BTN_FORWARD, 1);
2751 input_report_key(hidpp->input, BTN_BACK, 1);
2754 input_report_key(hidpp->input, BTN_BACK, 0);
2755 input_report_key(hidpp->input, BTN_FORWARD, 0);
2756 input_report_key(hidpp->input, BTN_MIDDLE, 0);
2759 hid_err(hdev, "error in report\n");
2762 input_sync(hidpp->input);
2764 } else if (data[0] == 0x02) {
2766 * Logitech M560 mouse report
2768 * data[0] = type (0x02)
2769 * data[1..2] = buttons
2776 input_report_key(hidpp->input, BTN_LEFT,
2777 !!(data[1] & M560_MOUSE_BTN_LEFT));
2778 input_report_key(hidpp->input, BTN_RIGHT,
2779 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2781 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
2782 input_report_rel(hidpp->input, REL_HWHEEL, -1);
2783 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2785 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
2786 input_report_rel(hidpp->input, REL_HWHEEL, 1);
2787 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2791 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2792 input_report_rel(hidpp->input, REL_X, v);
2794 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2795 input_report_rel(hidpp->input, REL_Y, v);
2797 v = hid_snto32(data[6], 8);
2799 hidpp_scroll_counter_handle_scroll(hidpp->input,
2800 &hidpp->vertical_wheel_counter, v);
2802 input_sync(hidpp->input);
2808 static void m560_populate_input(struct hidpp_device *hidpp,
2809 struct input_dev *input_dev)
2811 __set_bit(EV_KEY, input_dev->evbit);
2812 __set_bit(BTN_MIDDLE, input_dev->keybit);
2813 __set_bit(BTN_RIGHT, input_dev->keybit);
2814 __set_bit(BTN_LEFT, input_dev->keybit);
2815 __set_bit(BTN_BACK, input_dev->keybit);
2816 __set_bit(BTN_FORWARD, input_dev->keybit);
2818 __set_bit(EV_REL, input_dev->evbit);
2819 __set_bit(REL_X, input_dev->relbit);
2820 __set_bit(REL_Y, input_dev->relbit);
2821 __set_bit(REL_WHEEL, input_dev->relbit);
2822 __set_bit(REL_HWHEEL, input_dev->relbit);
2823 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2824 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
2827 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2828 struct hid_field *field, struct hid_usage *usage,
2829 unsigned long **bit, int *max)
2834 /* ------------------------------------------------------------------------- */
2835 /* Logitech K400 devices */
2836 /* ------------------------------------------------------------------------- */
2839 * The Logitech K400 keyboard has an embedded touchpad which is seen
2840 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2841 * tap-to-click but the setting is not remembered accross reset, annoying some
2844 * We can toggle this feature from the host by using the feature 0x6010:
2848 struct k400_private_data {
2852 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2854 struct k400_private_data *k400 = hidpp->private_data;
2855 struct hidpp_touchpad_fw_items items = {};
2859 if (!k400->feature_index) {
2860 ret = hidpp_root_get_feature(hidpp,
2861 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2862 &k400->feature_index, &feature_type);
2864 /* means that the device is not powered up */
2868 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2875 static int k400_allocate(struct hid_device *hdev)
2877 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2878 struct k400_private_data *k400;
2880 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2885 hidpp->private_data = k400;
2890 static int k400_connect(struct hid_device *hdev, bool connected)
2892 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2894 if (!disable_tap_to_click)
2897 return k400_disable_tap_to_click(hidpp);
2900 /* ------------------------------------------------------------------------- */
2901 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2902 /* ------------------------------------------------------------------------- */
2904 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2906 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
2907 struct hidpp_ff_private_data *data)
2909 struct hidpp_report response;
2910 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
2911 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
2915 /* initialize with zero autocenter to get wheel in usable state */
2917 dbg_hid("Setting autocenter to 0.\n");
2918 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2919 HIDPP_FF_DOWNLOAD_EFFECT,
2920 params, ARRAY_SIZE(params),
2923 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
2925 data->slot_autocenter = response.fap.params[0];
2930 static int g920_get_config(struct hidpp_device *hidpp,
2931 struct hidpp_ff_private_data *data)
2933 struct hidpp_report response;
2937 memset(data, 0, sizeof(*data));
2939 /* Find feature and store for later use */
2940 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2941 &data->feature_index, &feature_type);
2945 /* Read number of slots available in device */
2946 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2953 hid_err(hidpp->hid_dev,
2954 "%s: received protocol error 0x%02x\n", __func__, ret);
2958 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2960 /* reset all forces */
2961 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2966 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
2968 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2969 HIDPP_FF_GET_APERTURE,
2973 hid_warn(hidpp->hid_dev,
2974 "Failed to read range from device!\n");
2977 900 : get_unaligned_be16(&response.fap.params[0]);
2979 /* Read the current gain values */
2980 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2981 HIDPP_FF_GET_GLOBAL_GAINS,
2985 hid_warn(hidpp->hid_dev,
2986 "Failed to read gain values from device!\n");
2988 0xffff : get_unaligned_be16(&response.fap.params[0]);
2990 /* ignore boost value at response.fap.params[2] */
2992 return g920_ff_set_autocenter(hidpp, data);
2995 /* -------------------------------------------------------------------------- */
2996 /* Logitech Dinovo Mini keyboard with builtin touchpad */
2997 /* -------------------------------------------------------------------------- */
2998 #define DINOVO_MINI_PRODUCT_ID 0xb30c
3000 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3001 struct hid_field *field, struct hid_usage *usage,
3002 unsigned long **bit, int *max)
3004 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3007 switch (usage->hid & HID_USAGE) {
3008 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3015 /* -------------------------------------------------------------------------- */
3016 /* HID++1.0 devices which use HID++ reports for their wheels */
3017 /* -------------------------------------------------------------------------- */
3018 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3020 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3021 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3022 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3025 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3036 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3042 input_report_rel(hidpp->input, REL_WHEEL, value);
3043 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3044 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3045 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3046 input_sync(hidpp->input);
3051 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3052 struct input_dev *input_dev)
3054 __set_bit(EV_REL, input_dev->evbit);
3055 __set_bit(REL_WHEEL, input_dev->relbit);
3056 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3057 __set_bit(REL_HWHEEL, input_dev->relbit);
3058 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3061 /* -------------------------------------------------------------------------- */
3062 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3063 /* -------------------------------------------------------------------------- */
3064 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3066 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3067 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3068 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3071 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3082 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3083 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3087 * Buttons are either delivered through the regular mouse report *or*
3088 * through the extra buttons report. At least for button 6 how it is
3089 * delivered differs per receiver firmware version. Even receivers with
3090 * the same usb-id show different behavior, so we handle both cases.
3092 for (i = 0; i < 8; i++)
3093 input_report_key(hidpp->input, BTN_MOUSE + i,
3094 (data[3] & (1 << i)));
3096 /* Some mice report events on button 9+, use BTN_MISC */
3097 for (i = 0; i < 8; i++)
3098 input_report_key(hidpp->input, BTN_MISC + i,
3099 (data[4] & (1 << i)));
3101 input_sync(hidpp->input);
3105 static void hidpp10_extra_mouse_buttons_populate_input(
3106 struct hidpp_device *hidpp, struct input_dev *input_dev)
3108 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3109 __set_bit(BTN_0, input_dev->keybit);
3110 __set_bit(BTN_1, input_dev->keybit);
3111 __set_bit(BTN_2, input_dev->keybit);
3112 __set_bit(BTN_3, input_dev->keybit);
3113 __set_bit(BTN_4, input_dev->keybit);
3114 __set_bit(BTN_5, input_dev->keybit);
3115 __set_bit(BTN_6, input_dev->keybit);
3116 __set_bit(BTN_7, input_dev->keybit);
3119 /* -------------------------------------------------------------------------- */
3120 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3121 /* -------------------------------------------------------------------------- */
3123 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3124 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3125 u8 *_rdesc, unsigned int *rsize)
3127 /* Note 0 terminated so we can use strnstr to search for this. */
3128 static const char consumer_rdesc_start[] = {
3129 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3130 0x09, 0x01, /* USAGE (Consumer Control) */
3131 0xA1, 0x01, /* COLLECTION (Application) */
3132 0x85, 0x03, /* REPORT_ID = 3 */
3133 0x75, 0x10, /* REPORT_SIZE (16) */
3134 0x95, 0x02, /* REPORT_COUNT (2) */
3135 0x15, 0x01, /* LOGICAL_MIN (1) */
3136 0x26, 0x00 /* LOGICAL_MAX (... */
3138 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3141 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3142 size = *rsize - (consumer_rdesc - rdesc);
3143 if (consumer_rdesc && size >= 25) {
3144 consumer_rdesc[15] = 0x7f;
3145 consumer_rdesc[16] = 0x10;
3146 consumer_rdesc[20] = 0x7f;
3147 consumer_rdesc[21] = 0x10;
3152 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3154 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3155 HIDPP_ENABLE_CONSUMER_REPORT,
3156 HIDPP_ENABLE_CONSUMER_REPORT);
3159 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3162 u8 consumer_report[5];
3167 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3168 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3172 * Build a normal consumer report (3) out of the data, this detour
3173 * is necessary to get some keyboards to report their 0x10xx usages.
3175 consumer_report[0] = 0x03;
3176 memcpy(&consumer_report[1], &data[3], 4);
3177 /* We are called from atomic context */
3178 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3179 consumer_report, 5, 1);
3184 /* -------------------------------------------------------------------------- */
3185 /* High-resolution scroll wheels */
3186 /* -------------------------------------------------------------------------- */
3188 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3193 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
3194 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3196 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3197 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
3198 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3200 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
3201 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3207 if (multiplier == 0)
3210 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3211 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3215 /* -------------------------------------------------------------------------- */
3216 /* Generic HID++ devices */
3217 /* -------------------------------------------------------------------------- */
3219 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3220 unsigned int *rsize)
3222 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3227 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3228 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3229 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3230 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3235 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3236 struct hid_field *field, struct hid_usage *usage,
3237 unsigned long **bit, int *max)
3239 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3244 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3245 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3246 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3247 field->application != HID_GD_MOUSE)
3248 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3250 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3251 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3256 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3257 struct hid_field *field, struct hid_usage *usage,
3258 unsigned long **bit, int *max)
3260 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3265 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3266 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3267 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3268 usage->code == ABS_Y || usage->code == ABS_Z ||
3269 usage->code == ABS_RZ)) {
3270 field->application = HID_GD_MULTIAXIS;
3278 static void hidpp_populate_input(struct hidpp_device *hidpp,
3279 struct input_dev *input)
3281 hidpp->input = input;
3283 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3284 wtp_populate_input(hidpp, input);
3285 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3286 m560_populate_input(hidpp, input);
3288 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3289 hidpp10_wheel_populate_input(hidpp, input);
3291 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3292 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3295 static int hidpp_input_configured(struct hid_device *hdev,
3296 struct hid_input *hidinput)
3298 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3299 struct input_dev *input = hidinput->input;
3304 hidpp_populate_input(hidpp, input);
3309 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3312 struct hidpp_report *question = hidpp->send_receive_buf;
3313 struct hidpp_report *answer = hidpp->send_receive_buf;
3314 struct hidpp_report *report = (struct hidpp_report *)data;
3318 * If the mutex is locked then we have a pending answer from a
3319 * previously sent command.
3321 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3323 * Check for a correct hidpp20 answer or the corresponding
3326 if (hidpp_match_answer(question, report) ||
3327 hidpp_match_error(question, report)) {
3329 hidpp->answer_available = true;
3330 wake_up(&hidpp->wait);
3332 * This was an answer to a command that this driver sent
3333 * We return 1 to hid-core to avoid forwarding the
3334 * command upstream as it has been treated by the driver
3341 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3342 atomic_set(&hidpp->connected,
3343 !(report->rap.params[0] & (1 << 6)));
3344 if (schedule_work(&hidpp->work) == 0)
3345 dbg_hid("%s: connect event already queued\n", __func__);
3349 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3350 ret = hidpp20_battery_event(hidpp, data, size);
3353 ret = hidpp_solar_battery_event(hidpp, data, size);
3356 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3361 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3362 ret = hidpp10_battery_event(hidpp, data, size);
3367 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3368 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3373 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3374 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3379 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3380 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3388 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3391 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3397 /* Generic HID++ processing. */
3399 case REPORT_ID_HIDPP_VERY_LONG:
3400 if (size != hidpp->very_long_report_length) {
3401 hid_err(hdev, "received hid++ report of bad size (%d)",
3405 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3407 case REPORT_ID_HIDPP_LONG:
3408 if (size != HIDPP_REPORT_LONG_LENGTH) {
3409 hid_err(hdev, "received hid++ report of bad size (%d)",
3413 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3415 case REPORT_ID_HIDPP_SHORT:
3416 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3417 hid_err(hdev, "received hid++ report of bad size (%d)",
3421 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3425 /* If no report is available for further processing, skip calling
3426 * raw_event of subclasses. */
3430 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3431 return wtp_raw_event(hdev, data, size);
3432 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3433 return m560_raw_event(hdev, data, size);
3438 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3439 struct hid_usage *usage, __s32 value)
3441 /* This function will only be called for scroll events, due to the
3442 * restriction imposed in hidpp_usages.
3444 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3445 struct hidpp_scroll_counter *counter;
3450 counter = &hidpp->vertical_wheel_counter;
3451 /* A scroll event may occur before the multiplier has been retrieved or
3452 * the input device set, or high-res scroll enabling may fail. In such
3453 * cases we must return early (falling back to default behaviour) to
3454 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3456 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
3457 || hidpp->input == NULL || counter->wheel_multiplier == 0)
3460 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3464 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3466 static atomic_t battery_no = ATOMIC_INIT(0);
3467 struct power_supply_config cfg = { .drv_data = hidpp };
3468 struct power_supply_desc *desc = &hidpp->battery.desc;
3469 enum power_supply_property *battery_props;
3470 struct hidpp_battery *battery;
3471 unsigned int num_battery_props;
3475 if (hidpp->battery.ps)
3478 hidpp->battery.feature_index = 0xff;
3479 hidpp->battery.solar_feature_index = 0xff;
3480 hidpp->battery.voltage_feature_index = 0xff;
3482 if (hidpp->protocol_major >= 2) {
3483 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3484 ret = hidpp_solar_request_battery_event(hidpp);
3486 ret = hidpp20_query_battery_voltage_info(hidpp);
3488 ret = hidpp20_query_battery_info(hidpp);
3493 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3495 ret = hidpp10_query_battery_status(hidpp);
3497 ret = hidpp10_query_battery_mileage(hidpp);
3500 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3502 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3504 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3507 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3508 hidpp_battery_props,
3509 sizeof(hidpp_battery_props),
3514 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3516 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3517 battery_props[num_battery_props++] =
3518 POWER_SUPPLY_PROP_CAPACITY;
3520 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3521 battery_props[num_battery_props++] =
3522 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3524 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3525 battery_props[num_battery_props++] =
3526 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3528 battery = &hidpp->battery;
3530 n = atomic_inc_return(&battery_no) - 1;
3531 desc->properties = battery_props;
3532 desc->num_properties = num_battery_props;
3533 desc->get_property = hidpp_battery_get_property;
3534 sprintf(battery->name, "hidpp_battery_%ld", n);
3535 desc->name = battery->name;
3536 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3537 desc->use_for_apm = 0;
3539 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3542 if (IS_ERR(battery->ps))
3543 return PTR_ERR(battery->ps);
3545 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3550 static void hidpp_overwrite_name(struct hid_device *hdev)
3552 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3555 if (hidpp->protocol_major < 2)
3558 name = hidpp_get_device_name(hidpp);
3561 hid_err(hdev, "unable to retrieve the name of the device");
3563 dbg_hid("HID++: Got name: %s\n", name);
3564 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3570 static int hidpp_input_open(struct input_dev *dev)
3572 struct hid_device *hid = input_get_drvdata(dev);
3574 return hid_hw_open(hid);
3577 static void hidpp_input_close(struct input_dev *dev)
3579 struct hid_device *hid = input_get_drvdata(dev);
3584 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3586 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3587 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3592 input_set_drvdata(input_dev, hdev);
3593 input_dev->open = hidpp_input_open;
3594 input_dev->close = hidpp_input_close;
3596 input_dev->name = hidpp->name;
3597 input_dev->phys = hdev->phys;
3598 input_dev->uniq = hdev->uniq;
3599 input_dev->id.bustype = hdev->bus;
3600 input_dev->id.vendor = hdev->vendor;
3601 input_dev->id.product = hdev->product;
3602 input_dev->id.version = hdev->version;
3603 input_dev->dev.parent = &hdev->dev;
3608 static void hidpp_connect_event(struct hidpp_device *hidpp)
3610 struct hid_device *hdev = hidpp->hid_dev;
3612 bool connected = atomic_read(&hidpp->connected);
3613 struct input_dev *input;
3614 char *name, *devm_name;
3617 if (hidpp->battery.ps) {
3618 hidpp->battery.online = false;
3619 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3620 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3621 power_supply_changed(hidpp->battery.ps);
3626 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3627 ret = wtp_connect(hdev, connected);
3630 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3631 ret = m560_send_config_command(hdev, connected);
3634 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3635 ret = k400_connect(hdev, connected);
3640 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3641 ret = hidpp10_wheel_connect(hidpp);
3646 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3647 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3652 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3653 ret = hidpp10_consumer_keys_connect(hidpp);
3658 /* the device is already connected, we can ask for its name and
3660 if (!hidpp->protocol_major) {
3661 ret = hidpp_root_get_protocol_version(hidpp);
3663 hid_err(hdev, "Can not get the protocol version.\n");
3668 if (hidpp->protocol_major >= 2) {
3671 if (!hidpp_get_wireless_feature_index(hidpp, &feature_index))
3672 hidpp->wireless_feature_index = feature_index;
3675 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3676 name = hidpp_get_device_name(hidpp);
3678 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3684 hidpp->name = devm_name;
3688 hidpp_initialize_battery(hidpp);
3690 /* forward current battery state */
3691 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3692 hidpp10_enable_battery_reporting(hidpp);
3693 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3694 hidpp10_query_battery_mileage(hidpp);
3696 hidpp10_query_battery_status(hidpp);
3697 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3698 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3699 hidpp20_query_battery_voltage_info(hidpp);
3701 hidpp20_query_battery_info(hidpp);
3703 if (hidpp->battery.ps)
3704 power_supply_changed(hidpp->battery.ps);
3706 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3707 hi_res_scroll_enable(hidpp);
3709 if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
3710 /* if the input nodes are already created, we can stop now */
3713 input = hidpp_allocate_input(hdev);
3715 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3719 hidpp_populate_input(hidpp, input);
3721 ret = input_register_device(input);
3723 input_free_device(input);
3725 hidpp->delayed_input = input;
3728 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3730 static struct attribute *sysfs_attrs[] = {
3731 &dev_attr_builtin_power_supply.attr,
3735 static const struct attribute_group ps_attribute_group = {
3736 .attrs = sysfs_attrs
3739 static int hidpp_get_report_length(struct hid_device *hdev, int id)
3741 struct hid_report_enum *re;
3742 struct hid_report *report;
3744 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3745 report = re->report_id_hash[id];
3749 return report->field[0]->report_count + 1;
3752 static u8 hidpp_validate_device(struct hid_device *hdev)
3754 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3755 int id, report_length;
3756 u8 supported_reports = 0;
3758 id = REPORT_ID_HIDPP_SHORT;
3759 report_length = hidpp_get_report_length(hdev, id);
3760 if (report_length) {
3761 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
3764 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
3767 id = REPORT_ID_HIDPP_LONG;
3768 report_length = hidpp_get_report_length(hdev, id);
3769 if (report_length) {
3770 if (report_length < HIDPP_REPORT_LONG_LENGTH)
3773 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
3776 id = REPORT_ID_HIDPP_VERY_LONG;
3777 report_length = hidpp_get_report_length(hdev, id);
3778 if (report_length) {
3779 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
3780 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3783 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
3784 hidpp->very_long_report_length = report_length;
3787 return supported_reports;
3790 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3794 static bool hidpp_application_equals(struct hid_device *hdev,
3795 unsigned int application)
3797 struct list_head *report_list;
3798 struct hid_report *report;
3800 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
3801 report = list_first_entry_or_null(report_list, struct hid_report, list);
3802 return report && report->application == application;
3805 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3807 struct hidpp_device *hidpp;
3810 unsigned int connect_mask = HID_CONNECT_DEFAULT;
3811 struct hidpp_ff_private_data data;
3813 /* report_fixup needs drvdata to be set before we call hid_parse */
3814 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
3818 hidpp->hid_dev = hdev;
3819 hidpp->name = hdev->name;
3820 hidpp->quirks = id->driver_data;
3821 hid_set_drvdata(hdev, hidpp);
3823 ret = hid_parse(hdev);
3825 hid_err(hdev, "%s:parse failed\n", __func__);
3830 * Make sure the device is HID++ capable, otherwise treat as generic HID
3832 hidpp->supported_reports = hidpp_validate_device(hdev);
3834 if (!hidpp->supported_reports) {
3835 hid_set_drvdata(hdev, NULL);
3836 devm_kfree(&hdev->dev, hidpp);
3837 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
3840 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3841 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3843 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3844 hidpp_application_equals(hdev, HID_GD_MOUSE))
3845 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
3846 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
3848 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3849 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
3850 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
3852 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3853 ret = wtp_allocate(hdev, id);
3856 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3857 ret = k400_allocate(hdev);
3862 INIT_WORK(&hidpp->work, delayed_work_cb);
3863 mutex_init(&hidpp->send_mutex);
3864 init_waitqueue_head(&hidpp->wait);
3866 /* indicates we are handling the battery properties in the kernel */
3867 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3869 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3873 * First call hid_hw_start(hdev, 0) to allow IO without connecting any
3874 * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's
3875 * name and serial number and store these in hdev->name and hdev->uniq,
3876 * before the hid-input and hidraw drivers expose these to userspace.
3878 ret = hid_hw_start(hdev, 0);
3880 hid_err(hdev, "hw start failed\n");
3881 goto hid_hw_start_fail;
3884 ret = hid_hw_open(hdev);
3886 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3888 goto hid_hw_open_fail;
3891 /* Allow incoming packets */
3892 hid_device_io_start(hdev);
3894 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3895 hidpp_unifying_init(hidpp);
3896 else if (hid_is_usb(hidpp->hid_dev))
3897 hidpp_serial_init(hidpp);
3899 connected = hidpp_root_get_protocol_version(hidpp) == 0;
3900 atomic_set(&hidpp->connected, connected);
3901 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
3904 hid_err(hdev, "Device not connected");
3905 goto hid_hw_init_fail;
3908 hidpp_overwrite_name(hdev);
3911 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
3912 ret = wtp_get_config(hidpp);
3914 goto hid_hw_init_fail;
3915 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3916 ret = g920_get_config(hidpp, &data);
3918 goto hid_hw_init_fail;
3921 schedule_work(&hidpp->work);
3922 flush_work(&hidpp->work);
3924 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
3925 connect_mask &= ~HID_CONNECT_HIDINPUT;
3927 /* Now export the actual inputs and hidraw nodes to the world */
3928 ret = hid_connect(hdev, connect_mask);
3930 hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret);
3931 goto hid_hw_init_fail;
3934 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3935 ret = hidpp_ff_init(hidpp, &data);
3937 hid_warn(hidpp->hid_dev,
3938 "Unable to initialize force feedback support, errno %d\n",
3943 * This relies on logi_dj_ll_close() being a no-op so that DJ connection
3944 * events will still be received.
3954 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3955 cancel_work_sync(&hidpp->work);
3956 mutex_destroy(&hidpp->send_mutex);
3960 static void hidpp_remove(struct hid_device *hdev)
3962 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3965 return hid_hw_stop(hdev);
3967 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3970 cancel_work_sync(&hidpp->work);
3971 mutex_destroy(&hidpp->send_mutex);
3974 #define LDJ_DEVICE(product) \
3975 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3976 USB_VENDOR_ID_LOGITECH, (product))
3978 #define L27MHZ_DEVICE(product) \
3979 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
3980 USB_VENDOR_ID_LOGITECH, (product))
3982 static const struct hid_device_id hidpp_devices[] = {
3983 { /* wireless touchpad */
3985 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3986 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
3987 { /* wireless touchpad T650 */
3989 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3990 { /* wireless touchpad T651 */
3991 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3992 USB_DEVICE_ID_LOGITECH_T651),
3993 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3994 { /* Mouse Logitech Anywhere MX */
3995 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3996 { /* Mouse Logitech Cube */
3997 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3998 { /* Mouse Logitech M335 */
3999 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4000 { /* Mouse Logitech M515 */
4001 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4002 { /* Mouse logitech M560 */
4004 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
4005 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4006 { /* Mouse Logitech M705 (firmware RQM17) */
4007 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4008 { /* Mouse Logitech M705 (firmware RQM67) */
4009 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4010 { /* Mouse Logitech M720 */
4011 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4012 { /* Mouse Logitech MX Anywhere 2 */
4013 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4014 { LDJ_DEVICE(0x4072), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4015 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4016 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4017 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4018 { /* Mouse Logitech MX Anywhere 2S */
4019 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4020 { /* Mouse Logitech MX Master */
4021 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4022 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4023 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4024 { /* Mouse Logitech MX Master 2S */
4025 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4026 { /* Mouse Logitech MX Master 3 */
4027 LDJ_DEVICE(0x4082), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4028 { /* Mouse Logitech Performance MX */
4029 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4030 { /* Keyboard logitech K400 */
4032 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4033 { /* Solar Keyboard Logitech K750 */
4035 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4036 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4038 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4039 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4041 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4042 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4044 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4046 { LDJ_DEVICE(HID_ANY_ID) },
4048 { /* Keyboard LX501 (Y-RR53) */
4049 L27MHZ_DEVICE(0x0049),
4050 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4051 { /* Keyboard MX3000 (Y-RAM74) */
4052 L27MHZ_DEVICE(0x0057),
4053 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4054 { /* Keyboard MX3200 (Y-RAV80) */
4055 L27MHZ_DEVICE(0x005c),
4056 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4057 { /* S510 Media Remote */
4058 L27MHZ_DEVICE(0x00fe),
4059 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4061 { L27MHZ_DEVICE(HID_ANY_ID) },
4063 { /* Logitech G403 Wireless Gaming Mouse over USB */
4064 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4065 { /* Logitech G703 Gaming Mouse over USB */
4066 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4067 { /* Logitech G703 Hero Gaming Mouse over USB */
4068 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4069 { /* Logitech G900 Gaming Mouse over USB */
4070 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4071 { /* Logitech G903 Gaming Mouse over USB */
4072 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4073 { /* Logitech G903 Hero Gaming Mouse over USB */
4074 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4075 { /* Logitech G920 Wheel over USB */
4076 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4077 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4078 { /* Logitech G Pro Gaming Mouse over USB */
4079 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4081 { /* MX5000 keyboard over Bluetooth */
4082 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4083 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4084 { /* Dinovo Edge keyboard over Bluetooth */
4085 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4086 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4087 { /* MX5500 keyboard over Bluetooth */
4088 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4089 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4090 { /* MX Master mouse over Bluetooth */
4091 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012),
4092 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4093 { /* MX Ergo trackball over Bluetooth */
4094 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4095 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e),
4096 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4097 { /* MX Master 3 mouse over Bluetooth */
4098 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023),
4099 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4103 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4105 static const struct hid_usage_id hidpp_usages[] = {
4106 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4107 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4110 static struct hid_driver hidpp_driver = {
4111 .name = "logitech-hidpp-device",
4112 .id_table = hidpp_devices,
4113 .report_fixup = hidpp_report_fixup,
4114 .probe = hidpp_probe,
4115 .remove = hidpp_remove,
4116 .raw_event = hidpp_raw_event,
4117 .usage_table = hidpp_usages,
4118 .event = hidpp_event,
4119 .input_configured = hidpp_input_configured,
4120 .input_mapping = hidpp_input_mapping,
4121 .input_mapped = hidpp_input_mapped,
4124 module_hid_driver(hidpp_driver);