treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / hid / hid-logitech-hidpp.c
blob70e1cb928bf038876cd603e99302ea7e5687c426
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HIDPP protocol for Logitech Unifying receivers
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
8 */
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"
28 #include "hid-ids.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_raw_mode;
35 module_param(disable_raw_mode, bool, 0644);
36 MODULE_PARM_DESC(disable_raw_mode,
37 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
39 static bool disable_tap_to_click;
40 module_param(disable_tap_to_click, bool, 0644);
41 MODULE_PARM_DESC(disable_tap_to_click,
42 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
44 #define REPORT_ID_HIDPP_SHORT 0x10
45 #define REPORT_ID_HIDPP_LONG 0x11
46 #define REPORT_ID_HIDPP_VERY_LONG 0x12
48 #define HIDPP_REPORT_SHORT_LENGTH 7
49 #define HIDPP_REPORT_LONG_LENGTH 20
50 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
52 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
53 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
54 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
56 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
57 #define HIDPP_SUB_ID_ROLLER 0x05
58 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
60 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
61 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
62 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
63 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
64 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
66 /* bits 2..20 are reserved for classes */
67 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
68 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
69 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
70 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
71 #define HIDPP_QUIRK_UNIFYING BIT(25)
72 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
73 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
74 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
75 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
76 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
77 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
79 /* These are just aliases for now */
80 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
81 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
83 /* Convenience constant to check for any high-res support. */
84 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
85 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
86 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
88 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
90 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
91 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
92 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
93 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
94 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
97 * There are two hidpp protocols in use, the first version hidpp10 is known
98 * as register access protocol or RAP, the second version hidpp20 is known as
99 * feature access protocol or FAP
101 * Most older devices (including the Unifying usb receiver) use the RAP protocol
102 * where as most newer devices use the FAP protocol. Both protocols are
103 * compatible with the underlying transport, which could be usb, Unifiying, or
104 * bluetooth. The message lengths are defined by the hid vendor specific report
105 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
106 * the HIDPP_LONG report type (total message length 20 bytes)
108 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
109 * messages. The Unifying receiver itself responds to RAP messages (device index
110 * is 0xFF for the receiver), and all messages (short or long) with a device
111 * index between 1 and 6 are passed untouched to the corresponding paired
112 * Unifying device.
114 * The paired device can be RAP or FAP, it will receive the message untouched
115 * from the Unifiying receiver.
118 struct fap {
119 u8 feature_index;
120 u8 funcindex_clientid;
121 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
124 struct rap {
125 u8 sub_id;
126 u8 reg_address;
127 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
130 struct hidpp_report {
131 u8 report_id;
132 u8 device_index;
133 union {
134 struct fap fap;
135 struct rap rap;
136 u8 rawbytes[sizeof(struct fap)];
138 } __packed;
140 struct hidpp_battery {
141 u8 feature_index;
142 u8 solar_feature_index;
143 u8 voltage_feature_index;
144 struct power_supply_desc desc;
145 struct power_supply *ps;
146 char name[64];
147 int status;
148 int capacity;
149 int level;
150 int voltage;
151 int charge_type;
152 bool online;
156 * struct hidpp_scroll_counter - Utility class for processing high-resolution
157 * scroll events.
158 * @dev: the input device for which events should be reported.
159 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
160 * @remainder: counts the number of high-resolution units moved since the last
161 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
162 * only be used by class methods.
163 * @direction: direction of last movement (1 or -1)
164 * @last_time: last event time, used to reset remainder after inactivity
166 struct hidpp_scroll_counter {
167 int wheel_multiplier;
168 int remainder;
169 int direction;
170 unsigned long long last_time;
173 struct hidpp_device {
174 struct hid_device *hid_dev;
175 struct input_dev *input;
176 struct mutex send_mutex;
177 void *send_receive_buf;
178 char *name; /* will never be NULL and should not be freed */
179 wait_queue_head_t wait;
180 int very_long_report_length;
181 bool answer_available;
182 u8 protocol_major;
183 u8 protocol_minor;
185 void *private_data;
187 struct work_struct work;
188 struct kfifo delayed_work_fifo;
189 atomic_t connected;
190 struct input_dev *delayed_input;
192 unsigned long quirks;
193 unsigned long capabilities;
194 u8 supported_reports;
196 struct hidpp_battery battery;
197 struct hidpp_scroll_counter vertical_wheel_counter;
199 u8 wireless_feature_index;
202 /* HID++ 1.0 error codes */
203 #define HIDPP_ERROR 0x8f
204 #define HIDPP_ERROR_SUCCESS 0x00
205 #define HIDPP_ERROR_INVALID_SUBID 0x01
206 #define HIDPP_ERROR_INVALID_ADRESS 0x02
207 #define HIDPP_ERROR_INVALID_VALUE 0x03
208 #define HIDPP_ERROR_CONNECT_FAIL 0x04
209 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
210 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
211 #define HIDPP_ERROR_BUSY 0x07
212 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
213 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
214 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
215 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
216 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
217 /* HID++ 2.0 error codes */
218 #define HIDPP20_ERROR 0xff
220 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
222 static int __hidpp_send_report(struct hid_device *hdev,
223 struct hidpp_report *hidpp_report)
225 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
226 int fields_count, ret;
228 switch (hidpp_report->report_id) {
229 case REPORT_ID_HIDPP_SHORT:
230 fields_count = HIDPP_REPORT_SHORT_LENGTH;
231 break;
232 case REPORT_ID_HIDPP_LONG:
233 fields_count = HIDPP_REPORT_LONG_LENGTH;
234 break;
235 case REPORT_ID_HIDPP_VERY_LONG:
236 fields_count = hidpp->very_long_report_length;
237 break;
238 default:
239 return -ENODEV;
243 * set the device_index as the receiver, it will be overwritten by
244 * hid_hw_request if needed
246 hidpp_report->device_index = 0xff;
248 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
249 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
250 } else {
251 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
252 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
253 HID_REQ_SET_REPORT);
256 return ret == fields_count ? 0 : -1;
260 * hidpp_send_message_sync() returns 0 in case of success, and something else
261 * in case of a failure.
262 * - If ' something else' is positive, that means that an error has been raised
263 * by the protocol itself.
264 * - If ' something else' is negative, that means that we had a classic error
265 * (-ENOMEM, -EPIPE, etc...)
267 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
268 struct hidpp_report *message,
269 struct hidpp_report *response)
271 int ret;
273 mutex_lock(&hidpp->send_mutex);
275 hidpp->send_receive_buf = response;
276 hidpp->answer_available = false;
279 * So that we can later validate the answer when it arrives
280 * in hidpp_raw_event
282 *response = *message;
284 ret = __hidpp_send_report(hidpp->hid_dev, message);
286 if (ret) {
287 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
288 memset(response, 0, sizeof(struct hidpp_report));
289 goto exit;
292 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
293 5*HZ)) {
294 dbg_hid("%s:timeout waiting for response\n", __func__);
295 memset(response, 0, sizeof(struct hidpp_report));
296 ret = -ETIMEDOUT;
299 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
300 response->rap.sub_id == HIDPP_ERROR) {
301 ret = response->rap.params[1];
302 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
303 goto exit;
306 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
307 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
308 response->fap.feature_index == HIDPP20_ERROR) {
309 ret = response->fap.params[1];
310 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
311 goto exit;
314 exit:
315 mutex_unlock(&hidpp->send_mutex);
316 return ret;
320 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
321 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
322 struct hidpp_report *response)
324 struct hidpp_report *message;
325 int ret;
327 if (param_count > sizeof(message->fap.params))
328 return -EINVAL;
330 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
331 if (!message)
332 return -ENOMEM;
334 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
335 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
336 else
337 message->report_id = REPORT_ID_HIDPP_LONG;
338 message->fap.feature_index = feat_index;
339 message->fap.funcindex_clientid = funcindex_clientid;
340 memcpy(&message->fap.params, params, param_count);
342 ret = hidpp_send_message_sync(hidpp, message, response);
343 kfree(message);
344 return ret;
347 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
348 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
349 struct hidpp_report *response)
351 struct hidpp_report *message;
352 int ret, max_count;
354 /* Send as long report if short reports are not supported. */
355 if (report_id == REPORT_ID_HIDPP_SHORT &&
356 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
357 report_id = REPORT_ID_HIDPP_LONG;
359 switch (report_id) {
360 case REPORT_ID_HIDPP_SHORT:
361 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
362 break;
363 case REPORT_ID_HIDPP_LONG:
364 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
365 break;
366 case REPORT_ID_HIDPP_VERY_LONG:
367 max_count = hidpp_dev->very_long_report_length - 4;
368 break;
369 default:
370 return -EINVAL;
373 if (param_count > max_count)
374 return -EINVAL;
376 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
377 if (!message)
378 return -ENOMEM;
379 message->report_id = report_id;
380 message->rap.sub_id = sub_id;
381 message->rap.reg_address = reg_address;
382 memcpy(&message->rap.params, params, param_count);
384 ret = hidpp_send_message_sync(hidpp_dev, message, response);
385 kfree(message);
386 return ret;
389 static void delayed_work_cb(struct work_struct *work)
391 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
392 work);
393 hidpp_connect_event(hidpp);
396 static inline bool hidpp_match_answer(struct hidpp_report *question,
397 struct hidpp_report *answer)
399 return (answer->fap.feature_index == question->fap.feature_index) &&
400 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
403 static inline bool hidpp_match_error(struct hidpp_report *question,
404 struct hidpp_report *answer)
406 return ((answer->rap.sub_id == HIDPP_ERROR) ||
407 (answer->fap.feature_index == HIDPP20_ERROR)) &&
408 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
409 (answer->fap.params[0] == question->fap.funcindex_clientid);
412 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
413 struct hidpp_report *report)
415 return (hidpp->wireless_feature_index &&
416 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
417 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
418 (report->rap.sub_id == 0x41));
422 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
424 static void hidpp_prefix_name(char **name, int name_length)
426 #define PREFIX_LENGTH 9 /* "Logitech " */
428 int new_length;
429 char *new_name;
431 if (name_length > PREFIX_LENGTH &&
432 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
433 /* The prefix has is already in the name */
434 return;
436 new_length = PREFIX_LENGTH + name_length;
437 new_name = kzalloc(new_length, GFP_KERNEL);
438 if (!new_name)
439 return;
441 snprintf(new_name, new_length, "Logitech %s", *name);
443 kfree(*name);
445 *name = new_name;
449 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
450 * events given a high-resolution wheel
451 * movement.
452 * @counter: a hid_scroll_counter struct describing the wheel.
453 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
454 * units.
456 * Given a high-resolution movement, this function converts the movement into
457 * fractions of 120 and emits high-resolution scroll events for the input
458 * device. It also uses the multiplier from &struct hid_scroll_counter to
459 * emit low-resolution scroll events when appropriate for
460 * backwards-compatibility with userspace input libraries.
462 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
463 struct hidpp_scroll_counter *counter,
464 int hi_res_value)
466 int low_res_value, remainder, direction;
467 unsigned long long now, previous;
469 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
470 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
472 remainder = counter->remainder;
473 direction = hi_res_value > 0 ? 1 : -1;
475 now = sched_clock();
476 previous = counter->last_time;
477 counter->last_time = now;
479 * Reset the remainder after a period of inactivity or when the
480 * direction changes. This prevents the REL_WHEEL emulation point
481 * from sliding for devices that don't always provide the same
482 * number of movements per detent.
484 if (now - previous > 1000000000 || direction != counter->direction)
485 remainder = 0;
487 counter->direction = direction;
488 remainder += hi_res_value;
490 /* Some wheels will rest 7/8ths of a detent from the previous detent
491 * after slow movement, so we want the threshold for low-res events to
492 * be in the middle between two detents (e.g. after 4/8ths) as
493 * opposed to on the detents themselves (8/8ths).
495 if (abs(remainder) >= 60) {
496 /* Add (or subtract) 1 because we want to trigger when the wheel
497 * is half-way to the next detent (i.e. scroll 1 detent after a
498 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
499 * etc.).
501 low_res_value = remainder / 120;
502 if (low_res_value == 0)
503 low_res_value = (hi_res_value > 0 ? 1 : -1);
504 input_report_rel(input_dev, REL_WHEEL, low_res_value);
505 remainder -= low_res_value * 120;
507 counter->remainder = remainder;
510 /* -------------------------------------------------------------------------- */
511 /* HIDP++ 1.0 commands */
512 /* -------------------------------------------------------------------------- */
514 #define HIDPP_SET_REGISTER 0x80
515 #define HIDPP_GET_REGISTER 0x81
516 #define HIDPP_SET_LONG_REGISTER 0x82
517 #define HIDPP_GET_LONG_REGISTER 0x83
520 * hidpp10_set_register - Modify a HID++ 1.0 register.
521 * @hidpp_dev: the device to set the register on.
522 * @register_address: the address of the register to modify.
523 * @byte: the byte of the register to modify. Should be less than 3.
524 * @mask: mask of the bits to modify
525 * @value: new values for the bits in mask
526 * Return: 0 if successful, otherwise a negative error code.
528 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
529 u8 register_address, u8 byte, u8 mask, u8 value)
531 struct hidpp_report response;
532 int ret;
533 u8 params[3] = { 0 };
535 ret = hidpp_send_rap_command_sync(hidpp_dev,
536 REPORT_ID_HIDPP_SHORT,
537 HIDPP_GET_REGISTER,
538 register_address,
539 NULL, 0, &response);
540 if (ret)
541 return ret;
543 memcpy(params, response.rap.params, 3);
545 params[byte] &= ~mask;
546 params[byte] |= value & mask;
548 return hidpp_send_rap_command_sync(hidpp_dev,
549 REPORT_ID_HIDPP_SHORT,
550 HIDPP_SET_REGISTER,
551 register_address,
552 params, 3, &response);
555 #define HIDPP_REG_ENABLE_REPORTS 0x00
556 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
557 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
558 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
559 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
560 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
562 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
564 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
565 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
568 #define HIDPP_REG_FEATURES 0x01
569 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
570 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
572 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
573 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
575 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
576 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
579 #define HIDPP_REG_BATTERY_STATUS 0x07
581 static int hidpp10_battery_status_map_level(u8 param)
583 int level;
585 switch (param) {
586 case 1 ... 2:
587 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
588 break;
589 case 3 ... 4:
590 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
591 break;
592 case 5 ... 6:
593 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
594 break;
595 case 7:
596 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
597 break;
598 default:
599 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
602 return level;
605 static int hidpp10_battery_status_map_status(u8 param)
607 int status;
609 switch (param) {
610 case 0x00:
611 /* discharging (in use) */
612 status = POWER_SUPPLY_STATUS_DISCHARGING;
613 break;
614 case 0x21: /* (standard) charging */
615 case 0x24: /* fast charging */
616 case 0x25: /* slow charging */
617 status = POWER_SUPPLY_STATUS_CHARGING;
618 break;
619 case 0x26: /* topping charge */
620 case 0x22: /* charge complete */
621 status = POWER_SUPPLY_STATUS_FULL;
622 break;
623 case 0x20: /* unknown */
624 status = POWER_SUPPLY_STATUS_UNKNOWN;
625 break;
627 * 0x01...0x1F = reserved (not charging)
628 * 0x23 = charging error
629 * 0x27..0xff = reserved
631 default:
632 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
633 break;
636 return status;
639 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
641 struct hidpp_report response;
642 int ret, status;
644 ret = hidpp_send_rap_command_sync(hidpp,
645 REPORT_ID_HIDPP_SHORT,
646 HIDPP_GET_REGISTER,
647 HIDPP_REG_BATTERY_STATUS,
648 NULL, 0, &response);
649 if (ret)
650 return ret;
652 hidpp->battery.level =
653 hidpp10_battery_status_map_level(response.rap.params[0]);
654 status = hidpp10_battery_status_map_status(response.rap.params[1]);
655 hidpp->battery.status = status;
656 /* the capacity is only available when discharging or full */
657 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
658 status == POWER_SUPPLY_STATUS_FULL;
660 return 0;
663 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
665 static int hidpp10_battery_mileage_map_status(u8 param)
667 int status;
669 switch (param >> 6) {
670 case 0x00:
671 /* discharging (in use) */
672 status = POWER_SUPPLY_STATUS_DISCHARGING;
673 break;
674 case 0x01: /* charging */
675 status = POWER_SUPPLY_STATUS_CHARGING;
676 break;
677 case 0x02: /* charge complete */
678 status = POWER_SUPPLY_STATUS_FULL;
679 break;
681 * 0x03 = charging error
683 default:
684 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
685 break;
688 return status;
691 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
693 struct hidpp_report response;
694 int ret, status;
696 ret = hidpp_send_rap_command_sync(hidpp,
697 REPORT_ID_HIDPP_SHORT,
698 HIDPP_GET_REGISTER,
699 HIDPP_REG_BATTERY_MILEAGE,
700 NULL, 0, &response);
701 if (ret)
702 return ret;
704 hidpp->battery.capacity = response.rap.params[0];
705 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
706 hidpp->battery.status = status;
707 /* the capacity is only available when discharging or full */
708 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
709 status == POWER_SUPPLY_STATUS_FULL;
711 return 0;
714 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
716 struct hidpp_report *report = (struct hidpp_report *)data;
717 int status, capacity, level;
718 bool changed;
720 if (report->report_id != REPORT_ID_HIDPP_SHORT)
721 return 0;
723 switch (report->rap.sub_id) {
724 case HIDPP_REG_BATTERY_STATUS:
725 capacity = hidpp->battery.capacity;
726 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
727 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
728 break;
729 case HIDPP_REG_BATTERY_MILEAGE:
730 capacity = report->rap.params[0];
731 level = hidpp->battery.level;
732 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
733 break;
734 default:
735 return 0;
738 changed = capacity != hidpp->battery.capacity ||
739 level != hidpp->battery.level ||
740 status != hidpp->battery.status;
742 /* the capacity is only available when discharging or full */
743 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
744 status == POWER_SUPPLY_STATUS_FULL;
746 if (changed) {
747 hidpp->battery.level = level;
748 hidpp->battery.status = status;
749 if (hidpp->battery.ps)
750 power_supply_changed(hidpp->battery.ps);
753 return 0;
756 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
757 #define HIDPP_EXTENDED_PAIRING 0x30
758 #define HIDPP_DEVICE_NAME 0x40
760 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
762 struct hidpp_report response;
763 int ret;
764 u8 params[1] = { HIDPP_DEVICE_NAME };
765 char *name;
766 int len;
768 ret = hidpp_send_rap_command_sync(hidpp_dev,
769 REPORT_ID_HIDPP_SHORT,
770 HIDPP_GET_LONG_REGISTER,
771 HIDPP_REG_PAIRING_INFORMATION,
772 params, 1, &response);
773 if (ret)
774 return NULL;
776 len = response.rap.params[1];
778 if (2 + len > sizeof(response.rap.params))
779 return NULL;
781 if (len < 4) /* logitech devices are usually at least Xddd */
782 return NULL;
784 name = kzalloc(len + 1, GFP_KERNEL);
785 if (!name)
786 return NULL;
788 memcpy(name, &response.rap.params[2], len);
790 /* include the terminating '\0' */
791 hidpp_prefix_name(&name, len + 1);
793 return name;
796 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
798 struct hidpp_report response;
799 int ret;
800 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
802 ret = hidpp_send_rap_command_sync(hidpp,
803 REPORT_ID_HIDPP_SHORT,
804 HIDPP_GET_LONG_REGISTER,
805 HIDPP_REG_PAIRING_INFORMATION,
806 params, 1, &response);
807 if (ret)
808 return ret;
811 * We don't care about LE or BE, we will output it as a string
812 * with %4phD, so we need to keep the order.
814 *serial = *((u32 *)&response.rap.params[1]);
815 return 0;
818 static int hidpp_unifying_init(struct hidpp_device *hidpp)
820 struct hid_device *hdev = hidpp->hid_dev;
821 const char *name;
822 u32 serial;
823 int ret;
825 ret = hidpp_unifying_get_serial(hidpp, &serial);
826 if (ret)
827 return ret;
829 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
830 hdev->product, &serial);
831 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
833 name = hidpp_unifying_get_name(hidpp);
834 if (!name)
835 return -EIO;
837 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
838 dbg_hid("HID++ Unifying: Got name: %s\n", name);
840 kfree(name);
841 return 0;
844 /* -------------------------------------------------------------------------- */
845 /* 0x0000: Root */
846 /* -------------------------------------------------------------------------- */
848 #define HIDPP_PAGE_ROOT 0x0000
849 #define HIDPP_PAGE_ROOT_IDX 0x00
851 #define CMD_ROOT_GET_FEATURE 0x01
852 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
854 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
855 u8 *feature_index, u8 *feature_type)
857 struct hidpp_report response;
858 int ret;
859 u8 params[2] = { feature >> 8, feature & 0x00FF };
861 ret = hidpp_send_fap_command_sync(hidpp,
862 HIDPP_PAGE_ROOT_IDX,
863 CMD_ROOT_GET_FEATURE,
864 params, 2, &response);
865 if (ret)
866 return ret;
868 if (response.fap.params[0] == 0)
869 return -ENOENT;
871 *feature_index = response.fap.params[0];
872 *feature_type = response.fap.params[1];
874 return ret;
877 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
879 const u8 ping_byte = 0x5a;
880 u8 ping_data[3] = { 0, 0, ping_byte };
881 struct hidpp_report response;
882 int ret;
884 ret = hidpp_send_rap_command_sync(hidpp,
885 REPORT_ID_HIDPP_SHORT,
886 HIDPP_PAGE_ROOT_IDX,
887 CMD_ROOT_GET_PROTOCOL_VERSION,
888 ping_data, sizeof(ping_data), &response);
890 if (ret == HIDPP_ERROR_INVALID_SUBID) {
891 hidpp->protocol_major = 1;
892 hidpp->protocol_minor = 0;
893 goto print_version;
896 /* the device might not be connected */
897 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
898 return -EIO;
900 if (ret > 0) {
901 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
902 __func__, ret);
903 return -EPROTO;
905 if (ret)
906 return ret;
908 if (response.rap.params[2] != ping_byte) {
909 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
910 __func__, response.rap.params[2], ping_byte);
911 return -EPROTO;
914 hidpp->protocol_major = response.rap.params[0];
915 hidpp->protocol_minor = response.rap.params[1];
917 print_version:
918 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
919 hidpp->protocol_major, hidpp->protocol_minor);
920 return 0;
923 /* -------------------------------------------------------------------------- */
924 /* 0x0005: GetDeviceNameType */
925 /* -------------------------------------------------------------------------- */
927 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
929 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
930 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
931 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
933 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
934 u8 feature_index, u8 *nameLength)
936 struct hidpp_report response;
937 int ret;
939 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
940 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
942 if (ret > 0) {
943 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
944 __func__, ret);
945 return -EPROTO;
947 if (ret)
948 return ret;
950 *nameLength = response.fap.params[0];
952 return ret;
955 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
956 u8 feature_index, u8 char_index, char *device_name, int len_buf)
958 struct hidpp_report response;
959 int ret, i;
960 int count;
962 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
963 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
964 &response);
966 if (ret > 0) {
967 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
968 __func__, ret);
969 return -EPROTO;
971 if (ret)
972 return ret;
974 switch (response.report_id) {
975 case REPORT_ID_HIDPP_VERY_LONG:
976 count = hidpp->very_long_report_length - 4;
977 break;
978 case REPORT_ID_HIDPP_LONG:
979 count = HIDPP_REPORT_LONG_LENGTH - 4;
980 break;
981 case REPORT_ID_HIDPP_SHORT:
982 count = HIDPP_REPORT_SHORT_LENGTH - 4;
983 break;
984 default:
985 return -EPROTO;
988 if (len_buf < count)
989 count = len_buf;
991 for (i = 0; i < count; i++)
992 device_name[i] = response.fap.params[i];
994 return count;
997 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
999 u8 feature_type;
1000 u8 feature_index;
1001 u8 __name_length;
1002 char *name;
1003 unsigned index = 0;
1004 int ret;
1006 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1007 &feature_index, &feature_type);
1008 if (ret)
1009 return NULL;
1011 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1012 &__name_length);
1013 if (ret)
1014 return NULL;
1016 name = kzalloc(__name_length + 1, GFP_KERNEL);
1017 if (!name)
1018 return NULL;
1020 while (index < __name_length) {
1021 ret = hidpp_devicenametype_get_device_name(hidpp,
1022 feature_index, index, name + index,
1023 __name_length - index);
1024 if (ret <= 0) {
1025 kfree(name);
1026 return NULL;
1028 index += ret;
1031 /* include the terminating '\0' */
1032 hidpp_prefix_name(&name, __name_length + 1);
1034 return name;
1037 /* -------------------------------------------------------------------------- */
1038 /* 0x1000: Battery level status */
1039 /* -------------------------------------------------------------------------- */
1041 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1043 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1044 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1046 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1048 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1049 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1050 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1052 static int hidpp_map_battery_level(int capacity)
1054 if (capacity < 11)
1055 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1057 * The spec says this should be < 31 but some devices report 30
1058 * with brand new batteries and Windows reports 30 as "Good".
1060 else if (capacity < 30)
1061 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1062 else if (capacity < 81)
1063 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1064 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1067 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1068 int *next_capacity,
1069 int *level)
1071 int status;
1073 *capacity = data[0];
1074 *next_capacity = data[1];
1075 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1077 /* When discharging, we can rely on the device reported capacity.
1078 * For all other states the device reports 0 (unknown).
1080 switch (data[2]) {
1081 case 0: /* discharging (in use) */
1082 status = POWER_SUPPLY_STATUS_DISCHARGING;
1083 *level = hidpp_map_battery_level(*capacity);
1084 break;
1085 case 1: /* recharging */
1086 status = POWER_SUPPLY_STATUS_CHARGING;
1087 break;
1088 case 2: /* charge in final stage */
1089 status = POWER_SUPPLY_STATUS_CHARGING;
1090 break;
1091 case 3: /* charge complete */
1092 status = POWER_SUPPLY_STATUS_FULL;
1093 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1094 *capacity = 100;
1095 break;
1096 case 4: /* recharging below optimal speed */
1097 status = POWER_SUPPLY_STATUS_CHARGING;
1098 break;
1099 /* 5 = invalid battery type
1100 6 = thermal error
1101 7 = other charging error */
1102 default:
1103 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1104 break;
1107 return status;
1110 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1111 u8 feature_index,
1112 int *status,
1113 int *capacity,
1114 int *next_capacity,
1115 int *level)
1117 struct hidpp_report response;
1118 int ret;
1119 u8 *params = (u8 *)response.fap.params;
1121 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1122 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1123 NULL, 0, &response);
1124 /* Ignore these intermittent errors */
1125 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1126 return -EIO;
1127 if (ret > 0) {
1128 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1129 __func__, ret);
1130 return -EPROTO;
1132 if (ret)
1133 return ret;
1135 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1136 next_capacity,
1137 level);
1139 return 0;
1142 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1143 u8 feature_index)
1145 struct hidpp_report response;
1146 int ret;
1147 u8 *params = (u8 *)response.fap.params;
1148 unsigned int level_count, flags;
1150 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1151 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1152 NULL, 0, &response);
1153 if (ret > 0) {
1154 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1155 __func__, ret);
1156 return -EPROTO;
1158 if (ret)
1159 return ret;
1161 level_count = params[0];
1162 flags = params[1];
1164 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1165 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1166 else
1167 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1169 return 0;
1172 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1174 u8 feature_type;
1175 int ret;
1176 int status, capacity, next_capacity, level;
1178 if (hidpp->battery.feature_index == 0xff) {
1179 ret = hidpp_root_get_feature(hidpp,
1180 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1181 &hidpp->battery.feature_index,
1182 &feature_type);
1183 if (ret)
1184 return ret;
1187 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1188 hidpp->battery.feature_index,
1189 &status, &capacity,
1190 &next_capacity, &level);
1191 if (ret)
1192 return ret;
1194 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1195 hidpp->battery.feature_index);
1196 if (ret)
1197 return ret;
1199 hidpp->battery.status = status;
1200 hidpp->battery.capacity = capacity;
1201 hidpp->battery.level = level;
1202 /* the capacity is only available when discharging or full */
1203 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1204 status == POWER_SUPPLY_STATUS_FULL;
1206 return 0;
1209 static int hidpp20_battery_event(struct hidpp_device *hidpp,
1210 u8 *data, int size)
1212 struct hidpp_report *report = (struct hidpp_report *)data;
1213 int status, capacity, next_capacity, level;
1214 bool changed;
1216 if (report->fap.feature_index != hidpp->battery.feature_index ||
1217 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1218 return 0;
1220 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1221 &capacity,
1222 &next_capacity,
1223 &level);
1225 /* the capacity is only available when discharging or full */
1226 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1227 status == POWER_SUPPLY_STATUS_FULL;
1229 changed = capacity != hidpp->battery.capacity ||
1230 level != hidpp->battery.level ||
1231 status != hidpp->battery.status;
1233 if (changed) {
1234 hidpp->battery.level = level;
1235 hidpp->battery.capacity = capacity;
1236 hidpp->battery.status = status;
1237 if (hidpp->battery.ps)
1238 power_supply_changed(hidpp->battery.ps);
1241 return 0;
1244 /* -------------------------------------------------------------------------- */
1245 /* 0x1001: Battery voltage */
1246 /* -------------------------------------------------------------------------- */
1248 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1250 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1252 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1254 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1255 int *level, int *charge_type)
1257 int status;
1259 long charge_sts = (long)data[2];
1261 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1262 switch (data[2] & 0xe0) {
1263 case 0x00:
1264 status = POWER_SUPPLY_STATUS_CHARGING;
1265 break;
1266 case 0x20:
1267 status = POWER_SUPPLY_STATUS_FULL;
1268 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1269 break;
1270 case 0x40:
1271 status = POWER_SUPPLY_STATUS_DISCHARGING;
1272 break;
1273 case 0xe0:
1274 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1275 break;
1276 default:
1277 status = POWER_SUPPLY_STATUS_UNKNOWN;
1280 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1281 if (test_bit(3, &charge_sts)) {
1282 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1284 if (test_bit(4, &charge_sts)) {
1285 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1288 if (test_bit(5, &charge_sts)) {
1289 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1292 *voltage = get_unaligned_be16(data);
1294 return status;
1297 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1298 u8 feature_index,
1299 int *status, int *voltage,
1300 int *level, int *charge_type)
1302 struct hidpp_report response;
1303 int ret;
1304 u8 *params = (u8 *)response.fap.params;
1306 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1307 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1308 NULL, 0, &response);
1310 if (ret > 0) {
1311 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1312 __func__, ret);
1313 return -EPROTO;
1315 if (ret)
1316 return ret;
1318 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1320 *status = hidpp20_battery_map_status_voltage(params, voltage,
1321 level, charge_type);
1323 return 0;
1326 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1328 u8 feature_type;
1329 int ret;
1330 int status, voltage, level, charge_type;
1332 if (hidpp->battery.voltage_feature_index == 0xff) {
1333 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1334 &hidpp->battery.voltage_feature_index,
1335 &feature_type);
1336 if (ret)
1337 return ret;
1340 ret = hidpp20_battery_get_battery_voltage(hidpp,
1341 hidpp->battery.voltage_feature_index,
1342 &status, &voltage, &level, &charge_type);
1344 if (ret)
1345 return ret;
1347 hidpp->battery.status = status;
1348 hidpp->battery.voltage = voltage;
1349 hidpp->battery.level = level;
1350 hidpp->battery.charge_type = charge_type;
1351 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1353 return 0;
1356 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1357 u8 *data, int size)
1359 struct hidpp_report *report = (struct hidpp_report *)data;
1360 int status, voltage, level, charge_type;
1362 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1363 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1364 return 0;
1366 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1367 &level, &charge_type);
1369 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1371 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1372 hidpp->battery.voltage = voltage;
1373 hidpp->battery.status = status;
1374 hidpp->battery.level = level;
1375 hidpp->battery.charge_type = charge_type;
1376 if (hidpp->battery.ps)
1377 power_supply_changed(hidpp->battery.ps);
1379 return 0;
1382 static enum power_supply_property hidpp_battery_props[] = {
1383 POWER_SUPPLY_PROP_ONLINE,
1384 POWER_SUPPLY_PROP_STATUS,
1385 POWER_SUPPLY_PROP_SCOPE,
1386 POWER_SUPPLY_PROP_MODEL_NAME,
1387 POWER_SUPPLY_PROP_MANUFACTURER,
1388 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1389 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1390 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1391 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1394 static int hidpp_battery_get_property(struct power_supply *psy,
1395 enum power_supply_property psp,
1396 union power_supply_propval *val)
1398 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1399 int ret = 0;
1401 switch(psp) {
1402 case POWER_SUPPLY_PROP_STATUS:
1403 val->intval = hidpp->battery.status;
1404 break;
1405 case POWER_SUPPLY_PROP_CAPACITY:
1406 val->intval = hidpp->battery.capacity;
1407 break;
1408 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1409 val->intval = hidpp->battery.level;
1410 break;
1411 case POWER_SUPPLY_PROP_SCOPE:
1412 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1413 break;
1414 case POWER_SUPPLY_PROP_ONLINE:
1415 val->intval = hidpp->battery.online;
1416 break;
1417 case POWER_SUPPLY_PROP_MODEL_NAME:
1418 if (!strncmp(hidpp->name, "Logitech ", 9))
1419 val->strval = hidpp->name + 9;
1420 else
1421 val->strval = hidpp->name;
1422 break;
1423 case POWER_SUPPLY_PROP_MANUFACTURER:
1424 val->strval = "Logitech";
1425 break;
1426 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1427 val->strval = hidpp->hid_dev->uniq;
1428 break;
1429 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1430 /* hardware reports voltage in in mV. sysfs expects uV */
1431 val->intval = hidpp->battery.voltage * 1000;
1432 break;
1433 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1434 val->intval = hidpp->battery.charge_type;
1435 break;
1436 default:
1437 ret = -EINVAL;
1438 break;
1441 return ret;
1444 /* -------------------------------------------------------------------------- */
1445 /* 0x1d4b: Wireless device status */
1446 /* -------------------------------------------------------------------------- */
1447 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1449 static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1451 u8 feature_type;
1452 int ret;
1454 ret = hidpp_root_get_feature(hidpp,
1455 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1456 &hidpp->wireless_feature_index,
1457 &feature_type);
1459 return ret;
1462 /* -------------------------------------------------------------------------- */
1463 /* 0x2120: Hi-resolution scrolling */
1464 /* -------------------------------------------------------------------------- */
1466 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1468 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1470 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1471 bool enabled, u8 *multiplier)
1473 u8 feature_index;
1474 u8 feature_type;
1475 int ret;
1476 u8 params[1];
1477 struct hidpp_report response;
1479 ret = hidpp_root_get_feature(hidpp,
1480 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1481 &feature_index,
1482 &feature_type);
1483 if (ret)
1484 return ret;
1486 params[0] = enabled ? BIT(0) : 0;
1487 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1488 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1489 params, sizeof(params), &response);
1490 if (ret)
1491 return ret;
1492 *multiplier = response.fap.params[1];
1493 return 0;
1496 /* -------------------------------------------------------------------------- */
1497 /* 0x2121: HiRes Wheel */
1498 /* -------------------------------------------------------------------------- */
1500 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1502 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1503 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1505 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1506 u8 *multiplier)
1508 u8 feature_index;
1509 u8 feature_type;
1510 int ret;
1511 struct hidpp_report response;
1513 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1514 &feature_index, &feature_type);
1515 if (ret)
1516 goto return_default;
1518 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1519 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1520 NULL, 0, &response);
1521 if (ret)
1522 goto return_default;
1524 *multiplier = response.fap.params[0];
1525 return 0;
1526 return_default:
1527 hid_warn(hidpp->hid_dev,
1528 "Couldn't get wheel multiplier (error %d)\n", ret);
1529 return ret;
1532 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1533 bool high_resolution, bool use_hidpp)
1535 u8 feature_index;
1536 u8 feature_type;
1537 int ret;
1538 u8 params[1];
1539 struct hidpp_report response;
1541 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1542 &feature_index, &feature_type);
1543 if (ret)
1544 return ret;
1546 params[0] = (invert ? BIT(2) : 0) |
1547 (high_resolution ? BIT(1) : 0) |
1548 (use_hidpp ? BIT(0) : 0);
1550 return hidpp_send_fap_command_sync(hidpp, feature_index,
1551 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1552 params, sizeof(params), &response);
1555 /* -------------------------------------------------------------------------- */
1556 /* 0x4301: Solar Keyboard */
1557 /* -------------------------------------------------------------------------- */
1559 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1561 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1563 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1564 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1565 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1567 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1569 struct hidpp_report response;
1570 u8 params[2] = { 1, 1 };
1571 u8 feature_type;
1572 int ret;
1574 if (hidpp->battery.feature_index == 0xff) {
1575 ret = hidpp_root_get_feature(hidpp,
1576 HIDPP_PAGE_SOLAR_KEYBOARD,
1577 &hidpp->battery.solar_feature_index,
1578 &feature_type);
1579 if (ret)
1580 return ret;
1583 ret = hidpp_send_fap_command_sync(hidpp,
1584 hidpp->battery.solar_feature_index,
1585 CMD_SOLAR_SET_LIGHT_MEASURE,
1586 params, 2, &response);
1587 if (ret > 0) {
1588 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1589 __func__, ret);
1590 return -EPROTO;
1592 if (ret)
1593 return ret;
1595 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1597 return 0;
1600 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1601 u8 *data, int size)
1603 struct hidpp_report *report = (struct hidpp_report *)data;
1604 int capacity, lux, status;
1605 u8 function;
1607 function = report->fap.funcindex_clientid;
1610 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1611 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1612 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1613 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1614 return 0;
1616 capacity = report->fap.params[0];
1618 switch (function) {
1619 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1620 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1621 if (lux > 200)
1622 status = POWER_SUPPLY_STATUS_CHARGING;
1623 else
1624 status = POWER_SUPPLY_STATUS_DISCHARGING;
1625 break;
1626 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1627 default:
1628 if (capacity < hidpp->battery.capacity)
1629 status = POWER_SUPPLY_STATUS_DISCHARGING;
1630 else
1631 status = POWER_SUPPLY_STATUS_CHARGING;
1635 if (capacity == 100)
1636 status = POWER_SUPPLY_STATUS_FULL;
1638 hidpp->battery.online = true;
1639 if (capacity != hidpp->battery.capacity ||
1640 status != hidpp->battery.status) {
1641 hidpp->battery.capacity = capacity;
1642 hidpp->battery.status = status;
1643 if (hidpp->battery.ps)
1644 power_supply_changed(hidpp->battery.ps);
1647 return 0;
1650 /* -------------------------------------------------------------------------- */
1651 /* 0x6010: Touchpad FW items */
1652 /* -------------------------------------------------------------------------- */
1654 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1656 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1658 struct hidpp_touchpad_fw_items {
1659 uint8_t presence;
1660 uint8_t desired_state;
1661 uint8_t state;
1662 uint8_t persistent;
1666 * send a set state command to the device by reading the current items->state
1667 * field. items is then filled with the current state.
1669 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1670 u8 feature_index,
1671 struct hidpp_touchpad_fw_items *items)
1673 struct hidpp_report response;
1674 int ret;
1675 u8 *params = (u8 *)response.fap.params;
1677 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1678 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1680 if (ret > 0) {
1681 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1682 __func__, ret);
1683 return -EPROTO;
1685 if (ret)
1686 return ret;
1688 items->presence = params[0];
1689 items->desired_state = params[1];
1690 items->state = params[2];
1691 items->persistent = params[3];
1693 return 0;
1696 /* -------------------------------------------------------------------------- */
1697 /* 0x6100: TouchPadRawXY */
1698 /* -------------------------------------------------------------------------- */
1700 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1702 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1703 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1705 #define EVENT_TOUCHPAD_RAW_XY 0x00
1707 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1708 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1710 struct hidpp_touchpad_raw_info {
1711 u16 x_size;
1712 u16 y_size;
1713 u8 z_range;
1714 u8 area_range;
1715 u8 timestamp_unit;
1716 u8 maxcontacts;
1717 u8 origin;
1718 u16 res;
1721 struct hidpp_touchpad_raw_xy_finger {
1722 u8 contact_type;
1723 u8 contact_status;
1724 u16 x;
1725 u16 y;
1726 u8 z;
1727 u8 area;
1728 u8 finger_id;
1731 struct hidpp_touchpad_raw_xy {
1732 u16 timestamp;
1733 struct hidpp_touchpad_raw_xy_finger fingers[2];
1734 u8 spurious_flag;
1735 u8 end_of_frame;
1736 u8 finger_count;
1737 u8 button;
1740 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1741 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1743 struct hidpp_report response;
1744 int ret;
1745 u8 *params = (u8 *)response.fap.params;
1747 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1748 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1750 if (ret > 0) {
1751 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1752 __func__, ret);
1753 return -EPROTO;
1755 if (ret)
1756 return ret;
1758 raw_info->x_size = get_unaligned_be16(&params[0]);
1759 raw_info->y_size = get_unaligned_be16(&params[2]);
1760 raw_info->z_range = params[4];
1761 raw_info->area_range = params[5];
1762 raw_info->maxcontacts = params[7];
1763 raw_info->origin = params[8];
1764 /* res is given in unit per inch */
1765 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1767 return ret;
1770 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1771 u8 feature_index, bool send_raw_reports,
1772 bool sensor_enhanced_settings)
1774 struct hidpp_report response;
1777 * Params:
1778 * bit 0 - enable raw
1779 * bit 1 - 16bit Z, no area
1780 * bit 2 - enhanced sensitivity
1781 * bit 3 - width, height (4 bits each) instead of area
1782 * bit 4 - send raw + gestures (degrades smoothness)
1783 * remaining bits - reserved
1785 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1787 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1788 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1791 static void hidpp_touchpad_touch_event(u8 *data,
1792 struct hidpp_touchpad_raw_xy_finger *finger)
1794 u8 x_m = data[0] << 2;
1795 u8 y_m = data[2] << 2;
1797 finger->x = x_m << 6 | data[1];
1798 finger->y = y_m << 6 | data[3];
1800 finger->contact_type = data[0] >> 6;
1801 finger->contact_status = data[2] >> 6;
1803 finger->z = data[4];
1804 finger->area = data[5];
1805 finger->finger_id = data[6] >> 4;
1808 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1809 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1811 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1812 raw_xy->end_of_frame = data[8] & 0x01;
1813 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1814 raw_xy->finger_count = data[15] & 0x0f;
1815 raw_xy->button = (data[8] >> 2) & 0x01;
1817 if (raw_xy->finger_count) {
1818 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1819 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1823 /* -------------------------------------------------------------------------- */
1824 /* 0x8123: Force feedback support */
1825 /* -------------------------------------------------------------------------- */
1827 #define HIDPP_FF_GET_INFO 0x01
1828 #define HIDPP_FF_RESET_ALL 0x11
1829 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1830 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1831 #define HIDPP_FF_DESTROY_EFFECT 0x41
1832 #define HIDPP_FF_GET_APERTURE 0x51
1833 #define HIDPP_FF_SET_APERTURE 0x61
1834 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1835 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1837 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1838 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1839 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1840 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1842 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1843 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1844 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1845 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1846 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1847 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1848 #define HIDPP_FF_EFFECT_SPRING 0x06
1849 #define HIDPP_FF_EFFECT_DAMPER 0x07
1850 #define HIDPP_FF_EFFECT_FRICTION 0x08
1851 #define HIDPP_FF_EFFECT_INERTIA 0x09
1852 #define HIDPP_FF_EFFECT_RAMP 0x0A
1854 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1856 #define HIDPP_FF_EFFECTID_NONE -1
1857 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1858 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
1860 #define HIDPP_FF_MAX_PARAMS 20
1861 #define HIDPP_FF_RESERVED_SLOTS 1
1863 struct hidpp_ff_private_data {
1864 struct hidpp_device *hidpp;
1865 u8 feature_index;
1866 u8 version;
1867 u16 gain;
1868 s16 range;
1869 u8 slot_autocenter;
1870 u8 num_effects;
1871 int *effect_ids;
1872 struct workqueue_struct *wq;
1873 atomic_t workqueue_size;
1876 struct hidpp_ff_work_data {
1877 struct work_struct work;
1878 struct hidpp_ff_private_data *data;
1879 int effect_id;
1880 u8 command;
1881 u8 params[HIDPP_FF_MAX_PARAMS];
1882 u8 size;
1885 static const signed short hidpp_ff_effects[] = {
1886 FF_CONSTANT,
1887 FF_PERIODIC,
1888 FF_SINE,
1889 FF_SQUARE,
1890 FF_SAW_UP,
1891 FF_SAW_DOWN,
1892 FF_TRIANGLE,
1893 FF_SPRING,
1894 FF_DAMPER,
1895 FF_AUTOCENTER,
1896 FF_GAIN,
1900 static const signed short hidpp_ff_effects_v2[] = {
1901 FF_RAMP,
1902 FF_FRICTION,
1903 FF_INERTIA,
1907 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1908 HIDPP_FF_EFFECT_SPRING,
1909 HIDPP_FF_EFFECT_FRICTION,
1910 HIDPP_FF_EFFECT_DAMPER,
1911 HIDPP_FF_EFFECT_INERTIA
1914 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1915 "spring",
1916 "friction",
1917 "damper",
1918 "inertia"
1922 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1924 int i;
1926 for (i = 0; i < data->num_effects; i++)
1927 if (data->effect_ids[i] == effect_id)
1928 return i+1;
1930 return 0;
1933 static void hidpp_ff_work_handler(struct work_struct *w)
1935 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1936 struct hidpp_ff_private_data *data = wd->data;
1937 struct hidpp_report response;
1938 u8 slot;
1939 int ret;
1941 /* add slot number if needed */
1942 switch (wd->effect_id) {
1943 case HIDPP_FF_EFFECTID_AUTOCENTER:
1944 wd->params[0] = data->slot_autocenter;
1945 break;
1946 case HIDPP_FF_EFFECTID_NONE:
1947 /* leave slot as zero */
1948 break;
1949 default:
1950 /* find current slot for effect */
1951 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1952 break;
1955 /* send command and wait for reply */
1956 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1957 wd->command, wd->params, wd->size, &response);
1959 if (ret) {
1960 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1961 goto out;
1964 /* parse return data */
1965 switch (wd->command) {
1966 case HIDPP_FF_DOWNLOAD_EFFECT:
1967 slot = response.fap.params[0];
1968 if (slot > 0 && slot <= data->num_effects) {
1969 if (wd->effect_id >= 0)
1970 /* regular effect uploaded */
1971 data->effect_ids[slot-1] = wd->effect_id;
1972 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1973 /* autocenter spring uploaded */
1974 data->slot_autocenter = slot;
1976 break;
1977 case HIDPP_FF_DESTROY_EFFECT:
1978 if (wd->effect_id >= 0)
1979 /* regular effect destroyed */
1980 data->effect_ids[wd->params[0]-1] = -1;
1981 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1982 /* autocenter spring destoyed */
1983 data->slot_autocenter = 0;
1984 break;
1985 case HIDPP_FF_SET_GLOBAL_GAINS:
1986 data->gain = (wd->params[0] << 8) + wd->params[1];
1987 break;
1988 case HIDPP_FF_SET_APERTURE:
1989 data->range = (wd->params[0] << 8) + wd->params[1];
1990 break;
1991 default:
1992 /* no action needed */
1993 break;
1996 out:
1997 atomic_dec(&data->workqueue_size);
1998 kfree(wd);
2001 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2003 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2004 int s;
2006 if (!wd)
2007 return -ENOMEM;
2009 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2011 wd->data = data;
2012 wd->effect_id = effect_id;
2013 wd->command = command;
2014 wd->size = size;
2015 memcpy(wd->params, params, size);
2017 atomic_inc(&data->workqueue_size);
2018 queue_work(data->wq, &wd->work);
2020 /* warn about excessive queue size */
2021 s = atomic_read(&data->workqueue_size);
2022 if (s >= 20 && s % 20 == 0)
2023 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2025 return 0;
2028 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2030 struct hidpp_ff_private_data *data = dev->ff->private;
2031 u8 params[20];
2032 u8 size;
2033 int force;
2035 /* set common parameters */
2036 params[2] = effect->replay.length >> 8;
2037 params[3] = effect->replay.length & 255;
2038 params[4] = effect->replay.delay >> 8;
2039 params[5] = effect->replay.delay & 255;
2041 switch (effect->type) {
2042 case FF_CONSTANT:
2043 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2044 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2045 params[6] = force >> 8;
2046 params[7] = force & 255;
2047 params[8] = effect->u.constant.envelope.attack_level >> 7;
2048 params[9] = effect->u.constant.envelope.attack_length >> 8;
2049 params[10] = effect->u.constant.envelope.attack_length & 255;
2050 params[11] = effect->u.constant.envelope.fade_level >> 7;
2051 params[12] = effect->u.constant.envelope.fade_length >> 8;
2052 params[13] = effect->u.constant.envelope.fade_length & 255;
2053 size = 14;
2054 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2055 effect->u.constant.level,
2056 effect->direction, force);
2057 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2058 effect->u.constant.envelope.attack_level,
2059 effect->u.constant.envelope.attack_length,
2060 effect->u.constant.envelope.fade_level,
2061 effect->u.constant.envelope.fade_length);
2062 break;
2063 case FF_PERIODIC:
2065 switch (effect->u.periodic.waveform) {
2066 case FF_SINE:
2067 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2068 break;
2069 case FF_SQUARE:
2070 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2071 break;
2072 case FF_SAW_UP:
2073 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2074 break;
2075 case FF_SAW_DOWN:
2076 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2077 break;
2078 case FF_TRIANGLE:
2079 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2080 break;
2081 default:
2082 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2083 return -EINVAL;
2085 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2086 params[6] = effect->u.periodic.magnitude >> 8;
2087 params[7] = effect->u.periodic.magnitude & 255;
2088 params[8] = effect->u.periodic.offset >> 8;
2089 params[9] = effect->u.periodic.offset & 255;
2090 params[10] = effect->u.periodic.period >> 8;
2091 params[11] = effect->u.periodic.period & 255;
2092 params[12] = effect->u.periodic.phase >> 8;
2093 params[13] = effect->u.periodic.phase & 255;
2094 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2095 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2096 params[16] = effect->u.periodic.envelope.attack_length & 255;
2097 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2098 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2099 params[19] = effect->u.periodic.envelope.fade_length & 255;
2100 size = 20;
2101 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2102 effect->u.periodic.magnitude, effect->direction,
2103 effect->u.periodic.offset,
2104 effect->u.periodic.period,
2105 effect->u.periodic.phase);
2106 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2107 effect->u.periodic.envelope.attack_level,
2108 effect->u.periodic.envelope.attack_length,
2109 effect->u.periodic.envelope.fade_level,
2110 effect->u.periodic.envelope.fade_length);
2111 break;
2113 case FF_RAMP:
2114 params[1] = HIDPP_FF_EFFECT_RAMP;
2115 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2116 params[6] = force >> 8;
2117 params[7] = force & 255;
2118 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2119 params[8] = force >> 8;
2120 params[9] = force & 255;
2121 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2122 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2123 params[12] = effect->u.ramp.envelope.attack_length & 255;
2124 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2125 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2126 params[15] = effect->u.ramp.envelope.fade_length & 255;
2127 size = 16;
2128 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2129 effect->u.ramp.start_level,
2130 effect->u.ramp.end_level,
2131 effect->direction, force);
2132 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2133 effect->u.ramp.envelope.attack_level,
2134 effect->u.ramp.envelope.attack_length,
2135 effect->u.ramp.envelope.fade_level,
2136 effect->u.ramp.envelope.fade_length);
2137 break;
2138 case FF_FRICTION:
2139 case FF_INERTIA:
2140 case FF_SPRING:
2141 case FF_DAMPER:
2142 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2143 params[6] = effect->u.condition[0].left_saturation >> 9;
2144 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2145 params[8] = effect->u.condition[0].left_coeff >> 8;
2146 params[9] = effect->u.condition[0].left_coeff & 255;
2147 params[10] = effect->u.condition[0].deadband >> 9;
2148 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2149 params[12] = effect->u.condition[0].center >> 8;
2150 params[13] = effect->u.condition[0].center & 255;
2151 params[14] = effect->u.condition[0].right_coeff >> 8;
2152 params[15] = effect->u.condition[0].right_coeff & 255;
2153 params[16] = effect->u.condition[0].right_saturation >> 9;
2154 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2155 size = 18;
2156 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2157 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2158 effect->u.condition[0].left_coeff,
2159 effect->u.condition[0].left_saturation,
2160 effect->u.condition[0].right_coeff,
2161 effect->u.condition[0].right_saturation);
2162 dbg_hid(" deadband=%d, center=%d\n",
2163 effect->u.condition[0].deadband,
2164 effect->u.condition[0].center);
2165 break;
2166 default:
2167 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2168 return -EINVAL;
2171 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2174 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2176 struct hidpp_ff_private_data *data = dev->ff->private;
2177 u8 params[2];
2179 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2181 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2183 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2186 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2188 struct hidpp_ff_private_data *data = dev->ff->private;
2189 u8 slot = 0;
2191 dbg_hid("Erasing effect %d.\n", effect_id);
2193 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2196 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2198 struct hidpp_ff_private_data *data = dev->ff->private;
2199 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2201 dbg_hid("Setting autocenter to %d.\n", magnitude);
2203 /* start a standard spring effect */
2204 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2205 /* zero delay and duration */
2206 params[2] = params[3] = params[4] = params[5] = 0;
2207 /* set coeff to 25% of saturation */
2208 params[8] = params[14] = magnitude >> 11;
2209 params[9] = params[15] = (magnitude >> 3) & 255;
2210 params[6] = params[16] = magnitude >> 9;
2211 params[7] = params[17] = (magnitude >> 1) & 255;
2212 /* zero deadband and center */
2213 params[10] = params[11] = params[12] = params[13] = 0;
2215 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2218 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2220 struct hidpp_ff_private_data *data = dev->ff->private;
2221 u8 params[4];
2223 dbg_hid("Setting gain to %d.\n", gain);
2225 params[0] = gain >> 8;
2226 params[1] = gain & 255;
2227 params[2] = 0; /* no boost */
2228 params[3] = 0;
2230 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2233 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2235 struct hid_device *hid = to_hid_device(dev);
2236 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2237 struct input_dev *idev = hidinput->input;
2238 struct hidpp_ff_private_data *data = idev->ff->private;
2240 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2243 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2245 struct hid_device *hid = to_hid_device(dev);
2246 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2247 struct input_dev *idev = hidinput->input;
2248 struct hidpp_ff_private_data *data = idev->ff->private;
2249 u8 params[2];
2250 int range = simple_strtoul(buf, NULL, 10);
2252 range = clamp(range, 180, 900);
2254 params[0] = range >> 8;
2255 params[1] = range & 0x00FF;
2257 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2259 return count;
2262 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2264 static void hidpp_ff_destroy(struct ff_device *ff)
2266 struct hidpp_ff_private_data *data = ff->private;
2267 struct hid_device *hid = data->hidpp->hid_dev;
2269 hid_info(hid, "Unloading HID++ force feedback.\n");
2271 device_remove_file(&hid->dev, &dev_attr_range);
2272 destroy_workqueue(data->wq);
2273 kfree(data->effect_ids);
2276 static int hidpp_ff_init(struct hidpp_device *hidpp,
2277 struct hidpp_ff_private_data *data)
2279 struct hid_device *hid = hidpp->hid_dev;
2280 struct hid_input *hidinput;
2281 struct input_dev *dev;
2282 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2283 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2284 struct ff_device *ff;
2285 int error, j, num_slots = data->num_effects;
2286 u8 version;
2288 if (list_empty(&hid->inputs)) {
2289 hid_err(hid, "no inputs found\n");
2290 return -ENODEV;
2292 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2293 dev = hidinput->input;
2295 if (!dev) {
2296 hid_err(hid, "Struct input_dev not set!\n");
2297 return -EINVAL;
2300 /* Get firmware release */
2301 version = bcdDevice & 255;
2303 /* Set supported force feedback capabilities */
2304 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2305 set_bit(hidpp_ff_effects[j], dev->ffbit);
2306 if (version > 1)
2307 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2308 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2310 error = input_ff_create(dev, num_slots);
2312 if (error) {
2313 hid_err(dev, "Failed to create FF device!\n");
2314 return error;
2317 * Create a copy of passed data, so we can transfer memory
2318 * ownership to FF core
2320 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2321 if (!data)
2322 return -ENOMEM;
2323 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2324 if (!data->effect_ids) {
2325 kfree(data);
2326 return -ENOMEM;
2328 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2329 if (!data->wq) {
2330 kfree(data->effect_ids);
2331 kfree(data);
2332 return -ENOMEM;
2335 data->hidpp = hidpp;
2336 data->version = version;
2337 for (j = 0; j < num_slots; j++)
2338 data->effect_ids[j] = -1;
2340 ff = dev->ff;
2341 ff->private = data;
2343 ff->upload = hidpp_ff_upload_effect;
2344 ff->erase = hidpp_ff_erase_effect;
2345 ff->playback = hidpp_ff_playback;
2346 ff->set_gain = hidpp_ff_set_gain;
2347 ff->set_autocenter = hidpp_ff_set_autocenter;
2348 ff->destroy = hidpp_ff_destroy;
2350 /* Create sysfs interface */
2351 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2352 if (error)
2353 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2355 /* init the hardware command queue */
2356 atomic_set(&data->workqueue_size, 0);
2358 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2359 version);
2361 return 0;
2364 /* ************************************************************************** */
2365 /* */
2366 /* Device Support */
2367 /* */
2368 /* ************************************************************************** */
2370 /* -------------------------------------------------------------------------- */
2371 /* Touchpad HID++ devices */
2372 /* -------------------------------------------------------------------------- */
2374 #define WTP_MANUAL_RESOLUTION 39
2376 struct wtp_data {
2377 u16 x_size, y_size;
2378 u8 finger_count;
2379 u8 mt_feature_index;
2380 u8 button_feature_index;
2381 u8 maxcontacts;
2382 bool flip_y;
2383 unsigned int resolution;
2386 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2387 struct hid_field *field, struct hid_usage *usage,
2388 unsigned long **bit, int *max)
2390 return -1;
2393 static void wtp_populate_input(struct hidpp_device *hidpp,
2394 struct input_dev *input_dev)
2396 struct wtp_data *wd = hidpp->private_data;
2398 __set_bit(EV_ABS, input_dev->evbit);
2399 __set_bit(EV_KEY, input_dev->evbit);
2400 __clear_bit(EV_REL, input_dev->evbit);
2401 __clear_bit(EV_LED, input_dev->evbit);
2403 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2404 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2405 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2406 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2408 /* Max pressure is not given by the devices, pick one */
2409 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2411 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2413 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2414 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2415 else
2416 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2418 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2419 INPUT_MT_DROP_UNUSED);
2422 static void wtp_touch_event(struct hidpp_device *hidpp,
2423 struct hidpp_touchpad_raw_xy_finger *touch_report)
2425 struct wtp_data *wd = hidpp->private_data;
2426 int slot;
2428 if (!touch_report->finger_id || touch_report->contact_type)
2429 /* no actual data */
2430 return;
2432 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2434 input_mt_slot(hidpp->input, slot);
2435 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2436 touch_report->contact_status);
2437 if (touch_report->contact_status) {
2438 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2439 touch_report->x);
2440 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2441 wd->flip_y ? wd->y_size - touch_report->y :
2442 touch_report->y);
2443 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2444 touch_report->area);
2448 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2449 struct hidpp_touchpad_raw_xy *raw)
2451 int i;
2453 for (i = 0; i < 2; i++)
2454 wtp_touch_event(hidpp, &(raw->fingers[i]));
2456 if (raw->end_of_frame &&
2457 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2458 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2460 if (raw->end_of_frame || raw->finger_count <= 2) {
2461 input_mt_sync_frame(hidpp->input);
2462 input_sync(hidpp->input);
2466 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2468 struct wtp_data *wd = hidpp->private_data;
2469 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2470 (data[7] >> 4) * (data[7] >> 4)) / 2;
2471 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2472 (data[13] >> 4) * (data[13] >> 4)) / 2;
2473 struct hidpp_touchpad_raw_xy raw = {
2474 .timestamp = data[1],
2475 .fingers = {
2477 .contact_type = 0,
2478 .contact_status = !!data[7],
2479 .x = get_unaligned_le16(&data[3]),
2480 .y = get_unaligned_le16(&data[5]),
2481 .z = c1_area,
2482 .area = c1_area,
2483 .finger_id = data[2],
2484 }, {
2485 .contact_type = 0,
2486 .contact_status = !!data[13],
2487 .x = get_unaligned_le16(&data[9]),
2488 .y = get_unaligned_le16(&data[11]),
2489 .z = c2_area,
2490 .area = c2_area,
2491 .finger_id = data[8],
2494 .finger_count = wd->maxcontacts,
2495 .spurious_flag = 0,
2496 .end_of_frame = (data[0] >> 7) == 0,
2497 .button = data[0] & 0x01,
2500 wtp_send_raw_xy_event(hidpp, &raw);
2502 return 1;
2505 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2507 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2508 struct wtp_data *wd = hidpp->private_data;
2509 struct hidpp_report *report = (struct hidpp_report *)data;
2510 struct hidpp_touchpad_raw_xy raw;
2512 if (!wd || !hidpp->input)
2513 return 1;
2515 switch (data[0]) {
2516 case 0x02:
2517 if (size < 2) {
2518 hid_err(hdev, "Received HID report of bad size (%d)",
2519 size);
2520 return 1;
2522 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2523 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2524 !!(data[1] & 0x01));
2525 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2526 !!(data[1] & 0x02));
2527 input_sync(hidpp->input);
2528 return 0;
2529 } else {
2530 if (size < 21)
2531 return 1;
2532 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2534 case REPORT_ID_HIDPP_LONG:
2535 /* size is already checked in hidpp_raw_event. */
2536 if ((report->fap.feature_index != wd->mt_feature_index) ||
2537 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2538 return 1;
2539 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2541 wtp_send_raw_xy_event(hidpp, &raw);
2542 return 0;
2545 return 0;
2548 static int wtp_get_config(struct hidpp_device *hidpp)
2550 struct wtp_data *wd = hidpp->private_data;
2551 struct hidpp_touchpad_raw_info raw_info = {0};
2552 u8 feature_type;
2553 int ret;
2555 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2556 &wd->mt_feature_index, &feature_type);
2557 if (ret)
2558 /* means that the device is not powered up */
2559 return ret;
2561 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2562 &raw_info);
2563 if (ret)
2564 return ret;
2566 wd->x_size = raw_info.x_size;
2567 wd->y_size = raw_info.y_size;
2568 wd->maxcontacts = raw_info.maxcontacts;
2569 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2570 wd->resolution = raw_info.res;
2571 if (!wd->resolution)
2572 wd->resolution = WTP_MANUAL_RESOLUTION;
2574 return 0;
2577 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2579 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2580 struct wtp_data *wd;
2582 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2583 GFP_KERNEL);
2584 if (!wd)
2585 return -ENOMEM;
2587 hidpp->private_data = wd;
2589 return 0;
2592 static int wtp_connect(struct hid_device *hdev, bool connected)
2594 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2595 struct wtp_data *wd = hidpp->private_data;
2596 int ret;
2598 if (!wd->x_size) {
2599 ret = wtp_get_config(hidpp);
2600 if (ret) {
2601 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2602 return ret;
2606 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2607 true, true);
2610 /* ------------------------------------------------------------------------- */
2611 /* Logitech M560 devices */
2612 /* ------------------------------------------------------------------------- */
2615 * Logitech M560 protocol overview
2617 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2618 * the sides buttons are pressed, it sends some keyboard keys events
2619 * instead of buttons ones.
2620 * To complicate things further, the middle button keys sequence
2621 * is different from the odd press and the even press.
2623 * forward button -> Super_R
2624 * backward button -> Super_L+'d' (press only)
2625 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2626 * 2nd time: left-click (press only)
2627 * NB: press-only means that when the button is pressed, the
2628 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2629 * together sequentially; instead when the button is released, no event is
2630 * generated !
2632 * With the command
2633 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2634 * the mouse reacts differently:
2635 * - it never sends a keyboard key event
2636 * - for the three mouse button it sends:
2637 * middle button press 11<xx>0a 3500af00...
2638 * side 1 button (forward) press 11<xx>0a 3500b000...
2639 * side 2 button (backward) press 11<xx>0a 3500ae00...
2640 * middle/side1/side2 button release 11<xx>0a 35000000...
2643 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2645 /* how buttons are mapped in the report */
2646 #define M560_MOUSE_BTN_LEFT 0x01
2647 #define M560_MOUSE_BTN_RIGHT 0x02
2648 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2649 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2651 #define M560_SUB_ID 0x0a
2652 #define M560_BUTTON_MODE_REGISTER 0x35
2654 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2656 struct hidpp_report response;
2657 struct hidpp_device *hidpp_dev;
2659 hidpp_dev = hid_get_drvdata(hdev);
2661 return hidpp_send_rap_command_sync(
2662 hidpp_dev,
2663 REPORT_ID_HIDPP_SHORT,
2664 M560_SUB_ID,
2665 M560_BUTTON_MODE_REGISTER,
2666 (u8 *)m560_config_parameter,
2667 sizeof(m560_config_parameter),
2668 &response
2672 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2674 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2676 /* sanity check */
2677 if (!hidpp->input) {
2678 hid_err(hdev, "error in parameter\n");
2679 return -EINVAL;
2682 if (size < 7) {
2683 hid_err(hdev, "error in report\n");
2684 return 0;
2687 if (data[0] == REPORT_ID_HIDPP_LONG &&
2688 data[2] == M560_SUB_ID && data[6] == 0x00) {
2690 * m560 mouse report for middle, forward and backward button
2692 * data[0] = 0x11
2693 * data[1] = device-id
2694 * data[2] = 0x0a
2695 * data[5] = 0xaf -> middle
2696 * 0xb0 -> forward
2697 * 0xae -> backward
2698 * 0x00 -> release all
2699 * data[6] = 0x00
2702 switch (data[5]) {
2703 case 0xaf:
2704 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2705 break;
2706 case 0xb0:
2707 input_report_key(hidpp->input, BTN_FORWARD, 1);
2708 break;
2709 case 0xae:
2710 input_report_key(hidpp->input, BTN_BACK, 1);
2711 break;
2712 case 0x00:
2713 input_report_key(hidpp->input, BTN_BACK, 0);
2714 input_report_key(hidpp->input, BTN_FORWARD, 0);
2715 input_report_key(hidpp->input, BTN_MIDDLE, 0);
2716 break;
2717 default:
2718 hid_err(hdev, "error in report\n");
2719 return 0;
2721 input_sync(hidpp->input);
2723 } else if (data[0] == 0x02) {
2725 * Logitech M560 mouse report
2727 * data[0] = type (0x02)
2728 * data[1..2] = buttons
2729 * data[3..5] = xy
2730 * data[6] = wheel
2733 int v;
2735 input_report_key(hidpp->input, BTN_LEFT,
2736 !!(data[1] & M560_MOUSE_BTN_LEFT));
2737 input_report_key(hidpp->input, BTN_RIGHT,
2738 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2740 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
2741 input_report_rel(hidpp->input, REL_HWHEEL, -1);
2742 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2743 -120);
2744 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
2745 input_report_rel(hidpp->input, REL_HWHEEL, 1);
2746 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2747 120);
2750 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2751 input_report_rel(hidpp->input, REL_X, v);
2753 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2754 input_report_rel(hidpp->input, REL_Y, v);
2756 v = hid_snto32(data[6], 8);
2757 if (v != 0)
2758 hidpp_scroll_counter_handle_scroll(hidpp->input,
2759 &hidpp->vertical_wheel_counter, v);
2761 input_sync(hidpp->input);
2764 return 1;
2767 static void m560_populate_input(struct hidpp_device *hidpp,
2768 struct input_dev *input_dev)
2770 __set_bit(EV_KEY, input_dev->evbit);
2771 __set_bit(BTN_MIDDLE, input_dev->keybit);
2772 __set_bit(BTN_RIGHT, input_dev->keybit);
2773 __set_bit(BTN_LEFT, input_dev->keybit);
2774 __set_bit(BTN_BACK, input_dev->keybit);
2775 __set_bit(BTN_FORWARD, input_dev->keybit);
2777 __set_bit(EV_REL, input_dev->evbit);
2778 __set_bit(REL_X, input_dev->relbit);
2779 __set_bit(REL_Y, input_dev->relbit);
2780 __set_bit(REL_WHEEL, input_dev->relbit);
2781 __set_bit(REL_HWHEEL, input_dev->relbit);
2782 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2783 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
2786 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2787 struct hid_field *field, struct hid_usage *usage,
2788 unsigned long **bit, int *max)
2790 return -1;
2793 /* ------------------------------------------------------------------------- */
2794 /* Logitech K400 devices */
2795 /* ------------------------------------------------------------------------- */
2798 * The Logitech K400 keyboard has an embedded touchpad which is seen
2799 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2800 * tap-to-click but the setting is not remembered accross reset, annoying some
2801 * users.
2803 * We can toggle this feature from the host by using the feature 0x6010:
2804 * Touchpad FW items
2807 struct k400_private_data {
2808 u8 feature_index;
2811 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2813 struct k400_private_data *k400 = hidpp->private_data;
2814 struct hidpp_touchpad_fw_items items = {};
2815 int ret;
2816 u8 feature_type;
2818 if (!k400->feature_index) {
2819 ret = hidpp_root_get_feature(hidpp,
2820 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2821 &k400->feature_index, &feature_type);
2822 if (ret)
2823 /* means that the device is not powered up */
2824 return ret;
2827 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2828 if (ret)
2829 return ret;
2831 return 0;
2834 static int k400_allocate(struct hid_device *hdev)
2836 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2837 struct k400_private_data *k400;
2839 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2840 GFP_KERNEL);
2841 if (!k400)
2842 return -ENOMEM;
2844 hidpp->private_data = k400;
2846 return 0;
2849 static int k400_connect(struct hid_device *hdev, bool connected)
2851 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2853 if (!disable_tap_to_click)
2854 return 0;
2856 return k400_disable_tap_to_click(hidpp);
2859 /* ------------------------------------------------------------------------- */
2860 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2861 /* ------------------------------------------------------------------------- */
2863 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2865 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
2866 struct hidpp_ff_private_data *data)
2868 struct hidpp_report response;
2869 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
2870 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
2872 int ret;
2874 /* initialize with zero autocenter to get wheel in usable state */
2876 dbg_hid("Setting autocenter to 0.\n");
2877 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2878 HIDPP_FF_DOWNLOAD_EFFECT,
2879 params, ARRAY_SIZE(params),
2880 &response);
2881 if (ret)
2882 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
2883 else
2884 data->slot_autocenter = response.fap.params[0];
2886 return ret;
2889 static int g920_get_config(struct hidpp_device *hidpp,
2890 struct hidpp_ff_private_data *data)
2892 struct hidpp_report response;
2893 u8 feature_type;
2894 int ret;
2896 memset(data, 0, sizeof(*data));
2898 /* Find feature and store for later use */
2899 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2900 &data->feature_index, &feature_type);
2901 if (ret)
2902 return ret;
2904 /* Read number of slots available in device */
2905 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2906 HIDPP_FF_GET_INFO,
2907 NULL, 0,
2908 &response);
2909 if (ret) {
2910 if (ret < 0)
2911 return ret;
2912 hid_err(hidpp->hid_dev,
2913 "%s: received protocol error 0x%02x\n", __func__, ret);
2914 return -EPROTO;
2917 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2919 /* reset all forces */
2920 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2921 HIDPP_FF_RESET_ALL,
2922 NULL, 0,
2923 &response);
2924 if (ret)
2925 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
2927 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2928 HIDPP_FF_GET_APERTURE,
2929 NULL, 0,
2930 &response);
2931 if (ret) {
2932 hid_warn(hidpp->hid_dev,
2933 "Failed to read range from device!\n");
2935 data->range = ret ?
2936 900 : get_unaligned_be16(&response.fap.params[0]);
2938 /* Read the current gain values */
2939 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2940 HIDPP_FF_GET_GLOBAL_GAINS,
2941 NULL, 0,
2942 &response);
2943 if (ret)
2944 hid_warn(hidpp->hid_dev,
2945 "Failed to read gain values from device!\n");
2946 data->gain = ret ?
2947 0xffff : get_unaligned_be16(&response.fap.params[0]);
2949 /* ignore boost value at response.fap.params[2] */
2951 return g920_ff_set_autocenter(hidpp, data);
2954 /* -------------------------------------------------------------------------- */
2955 /* HID++1.0 devices which use HID++ reports for their wheels */
2956 /* -------------------------------------------------------------------------- */
2957 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
2959 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2960 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
2961 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
2964 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
2965 u8 *data, int size)
2967 s8 value, hvalue;
2969 if (!hidpp->input)
2970 return -EINVAL;
2972 if (size < 7)
2973 return 0;
2975 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
2976 return 0;
2978 value = data[3];
2979 hvalue = data[4];
2981 input_report_rel(hidpp->input, REL_WHEEL, value);
2982 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
2983 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
2984 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
2985 input_sync(hidpp->input);
2987 return 1;
2990 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
2991 struct input_dev *input_dev)
2993 __set_bit(EV_REL, input_dev->evbit);
2994 __set_bit(REL_WHEEL, input_dev->relbit);
2995 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2996 __set_bit(REL_HWHEEL, input_dev->relbit);
2997 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3000 /* -------------------------------------------------------------------------- */
3001 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3002 /* -------------------------------------------------------------------------- */
3003 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3005 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3006 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3007 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3010 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3011 u8 *data, int size)
3013 int i;
3015 if (!hidpp->input)
3016 return -EINVAL;
3018 if (size < 7)
3019 return 0;
3021 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3022 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3023 return 0;
3026 * Buttons are either delivered through the regular mouse report *or*
3027 * through the extra buttons report. At least for button 6 how it is
3028 * delivered differs per receiver firmware version. Even receivers with
3029 * the same usb-id show different behavior, so we handle both cases.
3031 for (i = 0; i < 8; i++)
3032 input_report_key(hidpp->input, BTN_MOUSE + i,
3033 (data[3] & (1 << i)));
3035 /* Some mice report events on button 9+, use BTN_MISC */
3036 for (i = 0; i < 8; i++)
3037 input_report_key(hidpp->input, BTN_MISC + i,
3038 (data[4] & (1 << i)));
3040 input_sync(hidpp->input);
3041 return 1;
3044 static void hidpp10_extra_mouse_buttons_populate_input(
3045 struct hidpp_device *hidpp, struct input_dev *input_dev)
3047 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3048 __set_bit(BTN_0, input_dev->keybit);
3049 __set_bit(BTN_1, input_dev->keybit);
3050 __set_bit(BTN_2, input_dev->keybit);
3051 __set_bit(BTN_3, input_dev->keybit);
3052 __set_bit(BTN_4, input_dev->keybit);
3053 __set_bit(BTN_5, input_dev->keybit);
3054 __set_bit(BTN_6, input_dev->keybit);
3055 __set_bit(BTN_7, input_dev->keybit);
3058 /* -------------------------------------------------------------------------- */
3059 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3060 /* -------------------------------------------------------------------------- */
3062 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3063 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3064 u8 *_rdesc, unsigned int *rsize)
3066 /* Note 0 terminated so we can use strnstr to search for this. */
3067 static const char consumer_rdesc_start[] = {
3068 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3069 0x09, 0x01, /* USAGE (Consumer Control) */
3070 0xA1, 0x01, /* COLLECTION (Application) */
3071 0x85, 0x03, /* REPORT_ID = 3 */
3072 0x75, 0x10, /* REPORT_SIZE (16) */
3073 0x95, 0x02, /* REPORT_COUNT (2) */
3074 0x15, 0x01, /* LOGICAL_MIN (1) */
3075 0x26, 0x00 /* LOGICAL_MAX (... */
3077 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3078 unsigned int size;
3080 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3081 size = *rsize - (consumer_rdesc - rdesc);
3082 if (consumer_rdesc && size >= 25) {
3083 consumer_rdesc[15] = 0x7f;
3084 consumer_rdesc[16] = 0x10;
3085 consumer_rdesc[20] = 0x7f;
3086 consumer_rdesc[21] = 0x10;
3088 return _rdesc;
3091 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3093 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3094 HIDPP_ENABLE_CONSUMER_REPORT,
3095 HIDPP_ENABLE_CONSUMER_REPORT);
3098 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3099 u8 *data, int size)
3101 u8 consumer_report[5];
3103 if (size < 7)
3104 return 0;
3106 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3107 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3108 return 0;
3111 * Build a normal consumer report (3) out of the data, this detour
3112 * is necessary to get some keyboards to report their 0x10xx usages.
3114 consumer_report[0] = 0x03;
3115 memcpy(&consumer_report[1], &data[3], 4);
3116 /* We are called from atomic context */
3117 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3118 consumer_report, 5, 1);
3120 return 1;
3123 /* -------------------------------------------------------------------------- */
3124 /* High-resolution scroll wheels */
3125 /* -------------------------------------------------------------------------- */
3127 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3129 int ret;
3130 u8 multiplier = 1;
3132 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
3133 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3134 if (ret == 0)
3135 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3136 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
3137 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3138 &multiplier);
3139 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
3140 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3141 multiplier = 8;
3143 if (ret)
3144 return ret;
3146 if (multiplier == 0)
3147 multiplier = 1;
3149 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3150 hid_info(hidpp->hid_dev, "multiplier = %d\n", multiplier);
3151 return 0;
3154 /* -------------------------------------------------------------------------- */
3155 /* Generic HID++ devices */
3156 /* -------------------------------------------------------------------------- */
3158 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3159 unsigned int *rsize)
3161 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3163 if (!hidpp)
3164 return rdesc;
3166 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3167 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3168 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3169 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3171 return rdesc;
3174 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3175 struct hid_field *field, struct hid_usage *usage,
3176 unsigned long **bit, int *max)
3178 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3180 if (!hidpp)
3181 return 0;
3183 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3184 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3185 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3186 field->application != HID_GD_MOUSE)
3187 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3189 return 0;
3192 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3193 struct hid_field *field, struct hid_usage *usage,
3194 unsigned long **bit, int *max)
3196 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3198 if (!hidpp)
3199 return 0;
3201 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3202 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3203 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3204 usage->code == ABS_Y || usage->code == ABS_Z ||
3205 usage->code == ABS_RZ)) {
3206 field->application = HID_GD_MULTIAXIS;
3210 return 0;
3214 static void hidpp_populate_input(struct hidpp_device *hidpp,
3215 struct input_dev *input)
3217 hidpp->input = input;
3219 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3220 wtp_populate_input(hidpp, input);
3221 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3222 m560_populate_input(hidpp, input);
3224 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3225 hidpp10_wheel_populate_input(hidpp, input);
3227 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3228 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3231 static int hidpp_input_configured(struct hid_device *hdev,
3232 struct hid_input *hidinput)
3234 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3235 struct input_dev *input = hidinput->input;
3237 if (!hidpp)
3238 return 0;
3240 hidpp_populate_input(hidpp, input);
3242 return 0;
3245 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3246 int size)
3248 struct hidpp_report *question = hidpp->send_receive_buf;
3249 struct hidpp_report *answer = hidpp->send_receive_buf;
3250 struct hidpp_report *report = (struct hidpp_report *)data;
3251 int ret;
3254 * If the mutex is locked then we have a pending answer from a
3255 * previously sent command.
3257 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3259 * Check for a correct hidpp20 answer or the corresponding
3260 * error
3262 if (hidpp_match_answer(question, report) ||
3263 hidpp_match_error(question, report)) {
3264 *answer = *report;
3265 hidpp->answer_available = true;
3266 wake_up(&hidpp->wait);
3268 * This was an answer to a command that this driver sent
3269 * We return 1 to hid-core to avoid forwarding the
3270 * command upstream as it has been treated by the driver
3273 return 1;
3277 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3278 atomic_set(&hidpp->connected,
3279 !(report->rap.params[0] & (1 << 6)));
3280 if (schedule_work(&hidpp->work) == 0)
3281 dbg_hid("%s: connect event already queued\n", __func__);
3282 return 1;
3285 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3286 ret = hidpp20_battery_event(hidpp, data, size);
3287 if (ret != 0)
3288 return ret;
3289 ret = hidpp_solar_battery_event(hidpp, data, size);
3290 if (ret != 0)
3291 return ret;
3292 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3293 if (ret != 0)
3294 return ret;
3297 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3298 ret = hidpp10_battery_event(hidpp, data, size);
3299 if (ret != 0)
3300 return ret;
3303 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3304 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3305 if (ret != 0)
3306 return ret;
3309 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3310 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3311 if (ret != 0)
3312 return ret;
3315 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3316 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3317 if (ret != 0)
3318 return ret;
3321 return 0;
3324 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3325 u8 *data, int size)
3327 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3328 int ret = 0;
3330 if (!hidpp)
3331 return 0;
3333 /* Generic HID++ processing. */
3334 switch (data[0]) {
3335 case REPORT_ID_HIDPP_VERY_LONG:
3336 if (size != hidpp->very_long_report_length) {
3337 hid_err(hdev, "received hid++ report of bad size (%d)",
3338 size);
3339 return 1;
3341 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3342 break;
3343 case REPORT_ID_HIDPP_LONG:
3344 if (size != HIDPP_REPORT_LONG_LENGTH) {
3345 hid_err(hdev, "received hid++ report of bad size (%d)",
3346 size);
3347 return 1;
3349 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3350 break;
3351 case REPORT_ID_HIDPP_SHORT:
3352 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3353 hid_err(hdev, "received hid++ report of bad size (%d)",
3354 size);
3355 return 1;
3357 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3358 break;
3361 /* If no report is available for further processing, skip calling
3362 * raw_event of subclasses. */
3363 if (ret != 0)
3364 return ret;
3366 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3367 return wtp_raw_event(hdev, data, size);
3368 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3369 return m560_raw_event(hdev, data, size);
3371 return 0;
3374 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3375 struct hid_usage *usage, __s32 value)
3377 /* This function will only be called for scroll events, due to the
3378 * restriction imposed in hidpp_usages.
3380 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3381 struct hidpp_scroll_counter *counter;
3383 if (!hidpp)
3384 return 0;
3386 counter = &hidpp->vertical_wheel_counter;
3387 /* A scroll event may occur before the multiplier has been retrieved or
3388 * the input device set, or high-res scroll enabling may fail. In such
3389 * cases we must return early (falling back to default behaviour) to
3390 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3392 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
3393 || hidpp->input == NULL || counter->wheel_multiplier == 0)
3394 return 0;
3396 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3397 return 1;
3400 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3402 static atomic_t battery_no = ATOMIC_INIT(0);
3403 struct power_supply_config cfg = { .drv_data = hidpp };
3404 struct power_supply_desc *desc = &hidpp->battery.desc;
3405 enum power_supply_property *battery_props;
3406 struct hidpp_battery *battery;
3407 unsigned int num_battery_props;
3408 unsigned long n;
3409 int ret;
3411 if (hidpp->battery.ps)
3412 return 0;
3414 hidpp->battery.feature_index = 0xff;
3415 hidpp->battery.solar_feature_index = 0xff;
3416 hidpp->battery.voltage_feature_index = 0xff;
3418 if (hidpp->protocol_major >= 2) {
3419 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3420 ret = hidpp_solar_request_battery_event(hidpp);
3421 else {
3422 ret = hidpp20_query_battery_voltage_info(hidpp);
3423 if (ret)
3424 ret = hidpp20_query_battery_info(hidpp);
3427 if (ret)
3428 return ret;
3429 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3430 } else {
3431 ret = hidpp10_query_battery_status(hidpp);
3432 if (ret) {
3433 ret = hidpp10_query_battery_mileage(hidpp);
3434 if (ret)
3435 return -ENOENT;
3436 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3437 } else {
3438 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3440 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3443 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3444 hidpp_battery_props,
3445 sizeof(hidpp_battery_props),
3446 GFP_KERNEL);
3447 if (!battery_props)
3448 return -ENOMEM;
3450 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3452 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3453 battery_props[num_battery_props++] =
3454 POWER_SUPPLY_PROP_CAPACITY;
3456 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3457 battery_props[num_battery_props++] =
3458 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3460 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3461 battery_props[num_battery_props++] =
3462 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3464 battery = &hidpp->battery;
3466 n = atomic_inc_return(&battery_no) - 1;
3467 desc->properties = battery_props;
3468 desc->num_properties = num_battery_props;
3469 desc->get_property = hidpp_battery_get_property;
3470 sprintf(battery->name, "hidpp_battery_%ld", n);
3471 desc->name = battery->name;
3472 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3473 desc->use_for_apm = 0;
3475 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3476 &battery->desc,
3477 &cfg);
3478 if (IS_ERR(battery->ps))
3479 return PTR_ERR(battery->ps);
3481 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3483 return ret;
3486 static void hidpp_overwrite_name(struct hid_device *hdev)
3488 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3489 char *name;
3491 if (hidpp->protocol_major < 2)
3492 return;
3494 name = hidpp_get_device_name(hidpp);
3496 if (!name) {
3497 hid_err(hdev, "unable to retrieve the name of the device");
3498 } else {
3499 dbg_hid("HID++: Got name: %s\n", name);
3500 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3503 kfree(name);
3506 static int hidpp_input_open(struct input_dev *dev)
3508 struct hid_device *hid = input_get_drvdata(dev);
3510 return hid_hw_open(hid);
3513 static void hidpp_input_close(struct input_dev *dev)
3515 struct hid_device *hid = input_get_drvdata(dev);
3517 hid_hw_close(hid);
3520 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3522 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3523 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3525 if (!input_dev)
3526 return NULL;
3528 input_set_drvdata(input_dev, hdev);
3529 input_dev->open = hidpp_input_open;
3530 input_dev->close = hidpp_input_close;
3532 input_dev->name = hidpp->name;
3533 input_dev->phys = hdev->phys;
3534 input_dev->uniq = hdev->uniq;
3535 input_dev->id.bustype = hdev->bus;
3536 input_dev->id.vendor = hdev->vendor;
3537 input_dev->id.product = hdev->product;
3538 input_dev->id.version = hdev->version;
3539 input_dev->dev.parent = &hdev->dev;
3541 return input_dev;
3544 static void hidpp_connect_event(struct hidpp_device *hidpp)
3546 struct hid_device *hdev = hidpp->hid_dev;
3547 int ret = 0;
3548 bool connected = atomic_read(&hidpp->connected);
3549 struct input_dev *input;
3550 char *name, *devm_name;
3552 if (!connected) {
3553 if (hidpp->battery.ps) {
3554 hidpp->battery.online = false;
3555 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3556 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3557 power_supply_changed(hidpp->battery.ps);
3559 return;
3562 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3563 ret = wtp_connect(hdev, connected);
3564 if (ret)
3565 return;
3566 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3567 ret = m560_send_config_command(hdev, connected);
3568 if (ret)
3569 return;
3570 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3571 ret = k400_connect(hdev, connected);
3572 if (ret)
3573 return;
3576 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3577 ret = hidpp10_wheel_connect(hidpp);
3578 if (ret)
3579 return;
3582 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3583 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3584 if (ret)
3585 return;
3588 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3589 ret = hidpp10_consumer_keys_connect(hidpp);
3590 if (ret)
3591 return;
3594 /* the device is already connected, we can ask for its name and
3595 * protocol */
3596 if (!hidpp->protocol_major) {
3597 ret = hidpp_root_get_protocol_version(hidpp);
3598 if (ret) {
3599 hid_err(hdev, "Can not get the protocol version.\n");
3600 return;
3604 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3605 name = hidpp_get_device_name(hidpp);
3606 if (name) {
3607 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3608 "%s", name);
3609 kfree(name);
3610 if (!devm_name)
3611 return;
3613 hidpp->name = devm_name;
3617 hidpp_initialize_battery(hidpp);
3619 /* forward current battery state */
3620 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3621 hidpp10_enable_battery_reporting(hidpp);
3622 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3623 hidpp10_query_battery_mileage(hidpp);
3624 else
3625 hidpp10_query_battery_status(hidpp);
3626 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3627 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3628 hidpp20_query_battery_voltage_info(hidpp);
3629 else
3630 hidpp20_query_battery_info(hidpp);
3632 if (hidpp->battery.ps)
3633 power_supply_changed(hidpp->battery.ps);
3635 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3636 hi_res_scroll_enable(hidpp);
3638 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3639 /* if the input nodes are already created, we can stop now */
3640 return;
3642 input = hidpp_allocate_input(hdev);
3643 if (!input) {
3644 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3645 return;
3648 hidpp_populate_input(hidpp, input);
3650 ret = input_register_device(input);
3651 if (ret)
3652 input_free_device(input);
3654 hidpp->delayed_input = input;
3657 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3659 static struct attribute *sysfs_attrs[] = {
3660 &dev_attr_builtin_power_supply.attr,
3661 NULL
3664 static const struct attribute_group ps_attribute_group = {
3665 .attrs = sysfs_attrs
3668 static int hidpp_get_report_length(struct hid_device *hdev, int id)
3670 struct hid_report_enum *re;
3671 struct hid_report *report;
3673 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3674 report = re->report_id_hash[id];
3675 if (!report)
3676 return 0;
3678 return report->field[0]->report_count + 1;
3681 static u8 hidpp_validate_device(struct hid_device *hdev)
3683 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3684 int id, report_length;
3685 u8 supported_reports = 0;
3687 id = REPORT_ID_HIDPP_SHORT;
3688 report_length = hidpp_get_report_length(hdev, id);
3689 if (report_length) {
3690 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
3691 goto bad_device;
3693 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
3696 id = REPORT_ID_HIDPP_LONG;
3697 report_length = hidpp_get_report_length(hdev, id);
3698 if (report_length) {
3699 if (report_length < HIDPP_REPORT_LONG_LENGTH)
3700 goto bad_device;
3702 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
3705 id = REPORT_ID_HIDPP_VERY_LONG;
3706 report_length = hidpp_get_report_length(hdev, id);
3707 if (report_length) {
3708 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
3709 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3710 goto bad_device;
3712 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
3713 hidpp->very_long_report_length = report_length;
3716 return supported_reports;
3718 bad_device:
3719 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3720 return false;
3723 static bool hidpp_application_equals(struct hid_device *hdev,
3724 unsigned int application)
3726 struct list_head *report_list;
3727 struct hid_report *report;
3729 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
3730 report = list_first_entry_or_null(report_list, struct hid_report, list);
3731 return report && report->application == application;
3734 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3736 struct hidpp_device *hidpp;
3737 int ret;
3738 bool connected;
3739 unsigned int connect_mask = HID_CONNECT_DEFAULT;
3740 struct hidpp_ff_private_data data;
3742 /* report_fixup needs drvdata to be set before we call hid_parse */
3743 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
3744 if (!hidpp)
3745 return -ENOMEM;
3747 hidpp->hid_dev = hdev;
3748 hidpp->name = hdev->name;
3749 hidpp->quirks = id->driver_data;
3750 hid_set_drvdata(hdev, hidpp);
3752 ret = hid_parse(hdev);
3753 if (ret) {
3754 hid_err(hdev, "%s:parse failed\n", __func__);
3755 return ret;
3759 * Make sure the device is HID++ capable, otherwise treat as generic HID
3761 hidpp->supported_reports = hidpp_validate_device(hdev);
3763 if (!hidpp->supported_reports) {
3764 hid_set_drvdata(hdev, NULL);
3765 devm_kfree(&hdev->dev, hidpp);
3766 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
3769 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3770 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3772 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3773 hidpp_application_equals(hdev, HID_GD_MOUSE))
3774 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
3775 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
3777 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3778 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
3779 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
3781 if (disable_raw_mode) {
3782 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
3783 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
3786 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3787 ret = wtp_allocate(hdev, id);
3788 if (ret)
3789 return ret;
3790 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3791 ret = k400_allocate(hdev);
3792 if (ret)
3793 return ret;
3796 INIT_WORK(&hidpp->work, delayed_work_cb);
3797 mutex_init(&hidpp->send_mutex);
3798 init_waitqueue_head(&hidpp->wait);
3800 /* indicates we are handling the battery properties in the kernel */
3801 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3802 if (ret)
3803 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3804 hdev->name);
3807 * Plain USB connections need to actually call start and open
3808 * on the transport driver to allow incoming data.
3810 ret = hid_hw_start(hdev, 0);
3811 if (ret) {
3812 hid_err(hdev, "hw start failed\n");
3813 goto hid_hw_start_fail;
3816 ret = hid_hw_open(hdev);
3817 if (ret < 0) {
3818 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3819 __func__, ret);
3820 goto hid_hw_open_fail;
3823 /* Allow incoming packets */
3824 hid_device_io_start(hdev);
3826 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3827 hidpp_unifying_init(hidpp);
3829 connected = hidpp_root_get_protocol_version(hidpp) == 0;
3830 atomic_set(&hidpp->connected, connected);
3831 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
3832 if (!connected) {
3833 ret = -ENODEV;
3834 hid_err(hdev, "Device not connected");
3835 goto hid_hw_init_fail;
3838 hidpp_overwrite_name(hdev);
3841 if (connected && hidpp->protocol_major >= 2) {
3842 ret = hidpp_set_wireless_feature_index(hidpp);
3843 if (ret == -ENOENT)
3844 hidpp->wireless_feature_index = 0;
3845 else if (ret)
3846 goto hid_hw_init_fail;
3849 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
3850 ret = wtp_get_config(hidpp);
3851 if (ret)
3852 goto hid_hw_init_fail;
3853 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3854 ret = g920_get_config(hidpp, &data);
3855 if (ret)
3856 goto hid_hw_init_fail;
3859 hidpp_connect_event(hidpp);
3861 /* Reset the HID node state */
3862 hid_device_io_stop(hdev);
3863 hid_hw_close(hdev);
3864 hid_hw_stop(hdev);
3866 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3867 connect_mask &= ~HID_CONNECT_HIDINPUT;
3869 /* Now export the actual inputs and hidraw nodes to the world */
3870 ret = hid_hw_start(hdev, connect_mask);
3871 if (ret) {
3872 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3873 goto hid_hw_start_fail;
3876 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3877 ret = hidpp_ff_init(hidpp, &data);
3878 if (ret)
3879 hid_warn(hidpp->hid_dev,
3880 "Unable to initialize force feedback support, errno %d\n",
3881 ret);
3884 return ret;
3886 hid_hw_init_fail:
3887 hid_hw_close(hdev);
3888 hid_hw_open_fail:
3889 hid_hw_stop(hdev);
3890 hid_hw_start_fail:
3891 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3892 cancel_work_sync(&hidpp->work);
3893 mutex_destroy(&hidpp->send_mutex);
3894 return ret;
3897 static void hidpp_remove(struct hid_device *hdev)
3899 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3901 if (!hidpp)
3902 return hid_hw_stop(hdev);
3904 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3906 hid_hw_stop(hdev);
3907 cancel_work_sync(&hidpp->work);
3908 mutex_destroy(&hidpp->send_mutex);
3911 #define LDJ_DEVICE(product) \
3912 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3913 USB_VENDOR_ID_LOGITECH, (product))
3915 #define L27MHZ_DEVICE(product) \
3916 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
3917 USB_VENDOR_ID_LOGITECH, (product))
3919 static const struct hid_device_id hidpp_devices[] = {
3920 { /* wireless touchpad */
3921 LDJ_DEVICE(0x4011),
3922 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3923 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
3924 { /* wireless touchpad T650 */
3925 LDJ_DEVICE(0x4101),
3926 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3927 { /* wireless touchpad T651 */
3928 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3929 USB_DEVICE_ID_LOGITECH_T651),
3930 .driver_data = HIDPP_QUIRK_CLASS_WTP },
3931 { /* Mouse Logitech Anywhere MX */
3932 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3933 { /* Mouse Logitech Cube */
3934 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3935 { /* Mouse Logitech M335 */
3936 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3937 { /* Mouse Logitech M515 */
3938 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3939 { /* Mouse logitech M560 */
3940 LDJ_DEVICE(0x402d),
3941 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
3942 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3943 { /* Mouse Logitech M705 (firmware RQM17) */
3944 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3945 { /* Mouse Logitech M705 (firmware RQM67) */
3946 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3947 { /* Mouse Logitech M720 */
3948 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3949 { /* Mouse Logitech MX Anywhere 2 */
3950 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3951 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3952 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3953 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3954 { /* Mouse Logitech MX Anywhere 2S */
3955 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3956 { /* Mouse Logitech MX Master */
3957 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3958 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3959 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3960 { /* Mouse Logitech MX Master 2S */
3961 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3962 { /* Mouse Logitech MX Master 3 */
3963 LDJ_DEVICE(0x4082), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3964 { /* Mouse Logitech Performance MX */
3965 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3966 { /* Keyboard logitech K400 */
3967 LDJ_DEVICE(0x4024),
3968 .driver_data = HIDPP_QUIRK_CLASS_K400 },
3969 { /* Solar Keyboard Logitech K750 */
3970 LDJ_DEVICE(0x4002),
3971 .driver_data = HIDPP_QUIRK_CLASS_K750 },
3972 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
3973 LDJ_DEVICE(0xb305),
3974 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
3975 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
3976 LDJ_DEVICE(0xb30b),
3977 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
3979 { LDJ_DEVICE(HID_ANY_ID) },
3981 { /* Keyboard LX501 (Y-RR53) */
3982 L27MHZ_DEVICE(0x0049),
3983 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
3984 { /* Keyboard MX3000 (Y-RAM74) */
3985 L27MHZ_DEVICE(0x0057),
3986 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
3987 { /* Keyboard MX3200 (Y-RAV80) */
3988 L27MHZ_DEVICE(0x005c),
3989 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
3990 { /* S510 Media Remote */
3991 L27MHZ_DEVICE(0x00fe),
3992 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
3994 { L27MHZ_DEVICE(HID_ANY_ID) },
3996 { /* Logitech G403 Wireless Gaming Mouse over USB */
3997 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
3998 { /* Logitech G703 Gaming Mouse over USB */
3999 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4000 { /* Logitech G703 Hero Gaming Mouse over USB */
4001 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4002 { /* Logitech G900 Gaming Mouse over USB */
4003 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4004 { /* Logitech G903 Gaming Mouse over USB */
4005 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4006 { /* Logitech G903 Hero Gaming Mouse over USB */
4007 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4008 { /* Logitech G920 Wheel over USB */
4009 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4010 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4011 { /* Logitech G Pro Gaming Mouse over USB */
4012 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4014 { /* MX5000 keyboard over Bluetooth */
4015 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4016 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4017 { /* MX5500 keyboard over Bluetooth */
4018 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4019 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4020 { /* MX Master mouse over Bluetooth */
4021 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012),
4022 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4023 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e),
4024 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4025 { /* MX Master 3 mouse over Bluetooth */
4026 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023),
4027 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4031 MODULE_DEVICE_TABLE(hid, hidpp_devices);
4033 static const struct hid_usage_id hidpp_usages[] = {
4034 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4035 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4038 static struct hid_driver hidpp_driver = {
4039 .name = "logitech-hidpp-device",
4040 .id_table = hidpp_devices,
4041 .report_fixup = hidpp_report_fixup,
4042 .probe = hidpp_probe,
4043 .remove = hidpp_remove,
4044 .raw_event = hidpp_raw_event,
4045 .usage_table = hidpp_usages,
4046 .event = hidpp_event,
4047 .input_configured = hidpp_input_configured,
4048 .input_mapping = hidpp_input_mapping,
4049 .input_mapped = hidpp_input_mapped,
4052 module_hid_driver(hidpp_driver);