1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * HIDPP protocol for Logitech receivers
4 *
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
8 */
9
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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"
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
33MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
34
35static bool disable_tap_to_click;
36module_param(disable_tap_to_click, bool, 0644);
37MODULE_PARM_DESC(disable_tap_to_click,
38 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
39
40/* Define a non-zero software ID to identify our own requests */
41#define LINUX_KERNEL_SW_ID 0x01
42
43#define REPORT_ID_HIDPP_SHORT 0x10
44#define REPORT_ID_HIDPP_LONG 0x11
45#define REPORT_ID_HIDPP_VERY_LONG 0x12
46
47#define HIDPP_REPORT_SHORT_LENGTH 7
48#define HIDPP_REPORT_LONG_LENGTH 20
49#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
50
51#define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
52#define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
53#define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
54
55#define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
56#define HIDPP_SUB_ID_ROLLER 0x05
57#define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
58#define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
59#define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
60
61#define HIDPP_QUIRK_CLASS_WTP BIT(0)
62#define HIDPP_QUIRK_CLASS_M560 BIT(1)
63#define HIDPP_QUIRK_CLASS_K400 BIT(2)
64#define HIDPP_QUIRK_CLASS_G920 BIT(3)
65#define HIDPP_QUIRK_CLASS_K750 BIT(4)
66
67/* bits 2..20 are reserved for classes */
68/* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
69#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
70#define HIDPP_QUIRK_DELAYED_INIT BIT(23)
71#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
72#define HIDPP_QUIRK_HIDPP_WHEELS BIT(25)
73#define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(26)
74#define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(27)
75#define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(28)
76#define HIDPP_QUIRK_WIRELESS_STATUS BIT(29)
77
78/* These are just aliases for now */
79#define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
80#define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
81
82/* Convenience constant to check for any high-res support. */
83#define HIDPP_CAPABILITY_HI_RES_SCROLL (HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \
84 HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \
85 HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL)
86
87#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
88#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
89#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
90#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
91#define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
92#define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
93#define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
94#define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL BIT(7)
95#define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL BIT(8)
96#define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL BIT(9)
97#define HIDPP_CAPABILITY_ADC_MEASUREMENT BIT(10)
98
99#define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
100
101/*
102 * There are two hidpp protocols in use, the first version hidpp10 is known
103 * as register access protocol or RAP, the second version hidpp20 is known as
104 * feature access protocol or FAP
105 *
106 * Most older devices (including the Unifying usb receiver) use the RAP protocol
107 * where as most newer devices use the FAP protocol. Both protocols are
108 * compatible with the underlying transport, which could be usb, Unifiying, or
109 * bluetooth. The message lengths are defined by the hid vendor specific report
110 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
111 * the HIDPP_LONG report type (total message length 20 bytes)
112 *
113 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
114 * messages. The Unifying receiver itself responds to RAP messages (device index
115 * is 0xFF for the receiver), and all messages (short or long) with a device
116 * index between 1 and 6 are passed untouched to the corresponding paired
117 * Unifying device.
118 *
119 * The paired device can be RAP or FAP, it will receive the message untouched
120 * from the Unifiying receiver.
121 */
122
123struct fap {
124 u8 feature_index;
125 u8 funcindex_clientid;
126 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
127};
128
129struct rap {
130 u8 sub_id;
131 u8 reg_address;
132 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
133};
134
135struct hidpp_report {
136 u8 report_id;
137 u8 device_index;
138 union {
139 struct fap fap;
140 struct rap rap;
141 u8 rawbytes[sizeof(struct fap)];
142 };
143} __packed;
144
145struct hidpp_battery {
146 u8 feature_index;
147 u8 solar_feature_index;
148 u8 voltage_feature_index;
149 u8 adc_measurement_feature_index;
150 struct power_supply_desc desc;
151 struct power_supply *ps;
152 char name[64];
153 int status;
154 int capacity;
155 int level;
156 int voltage;
157 int charge_type;
158 bool online;
159 u8 supported_levels_1004;
160};
161
162/**
163 * struct hidpp_scroll_counter - Utility class for processing high-resolution
164 * scroll events.
165 * @dev: the input device for which events should be reported.
166 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
167 * @remainder: counts the number of high-resolution units moved since the last
168 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
169 * only be used by class methods.
170 * @direction: direction of last movement (1 or -1)
171 * @last_time: last event time, used to reset remainder after inactivity
172 */
173struct hidpp_scroll_counter {
174 int wheel_multiplier;
175 int remainder;
176 int direction;
177 unsigned long long last_time;
178};
179
180struct hidpp_device {
181 struct hid_device *hid_dev;
182 struct input_dev *input;
183 struct mutex send_mutex;
184 void *send_receive_buf;
185 char *name; /* will never be NULL and should not be freed */
186 wait_queue_head_t wait;
187 int very_long_report_length;
188 bool answer_available;
189 u8 protocol_major;
190 u8 protocol_minor;
191
192 void *private_data;
193
194 struct work_struct work;
195 struct kfifo delayed_work_fifo;
196 struct input_dev *delayed_input;
197
198 unsigned long quirks;
199 unsigned long capabilities;
200 u8 supported_reports;
201
202 struct hidpp_battery battery;
203 struct hidpp_scroll_counter vertical_wheel_counter;
204
205 u8 wireless_feature_index;
206};
207
208/* HID++ 1.0 error codes */
209#define HIDPP_ERROR 0x8f
210#define HIDPP_ERROR_SUCCESS 0x00
211#define HIDPP_ERROR_INVALID_SUBID 0x01
212#define HIDPP_ERROR_INVALID_ADRESS 0x02
213#define HIDPP_ERROR_INVALID_VALUE 0x03
214#define HIDPP_ERROR_CONNECT_FAIL 0x04
215#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
216#define HIDPP_ERROR_ALREADY_EXISTS 0x06
217#define HIDPP_ERROR_BUSY 0x07
218#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
219#define HIDPP_ERROR_RESOURCE_ERROR 0x09
220#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
221#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
222#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
223/* HID++ 2.0 error codes */
224#define HIDPP20_ERROR_NO_ERROR 0x00
225#define HIDPP20_ERROR_UNKNOWN 0x01
226#define HIDPP20_ERROR_INVALID_ARGS 0x02
227#define HIDPP20_ERROR_OUT_OF_RANGE 0x03
228#define HIDPP20_ERROR_HW_ERROR 0x04
229#define HIDPP20_ERROR_NOT_ALLOWED 0x05
230#define HIDPP20_ERROR_INVALID_FEATURE_INDEX 0x06
231#define HIDPP20_ERROR_INVALID_FUNCTION_ID 0x07
232#define HIDPP20_ERROR_BUSY 0x08
233#define HIDPP20_ERROR_UNSUPPORTED 0x09
234#define HIDPP20_ERROR 0xff
235
236static int __hidpp_send_report(struct hid_device *hdev,
237 struct hidpp_report *hidpp_report)
238{
239 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
240 int fields_count, ret;
241
242 switch (hidpp_report->report_id) {
243 case REPORT_ID_HIDPP_SHORT:
244 fields_count = HIDPP_REPORT_SHORT_LENGTH;
245 break;
246 case REPORT_ID_HIDPP_LONG:
247 fields_count = HIDPP_REPORT_LONG_LENGTH;
248 break;
249 case REPORT_ID_HIDPP_VERY_LONG:
250 fields_count = hidpp->very_long_report_length;
251 break;
252 default:
253 return -ENODEV;
254 }
255
256 /*
257 * set the device_index as the receiver, it will be overwritten by
258 * hid_hw_request if needed
259 */
260 hidpp_report->device_index = 0xff;
261
262 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
263 ret = hid_hw_output_report(hdev, buf: (u8 *)hidpp_report, len: fields_count);
264 } else {
265 ret = hid_hw_raw_request(hdev, reportnum: hidpp_report->report_id,
266 buf: (u8 *)hidpp_report, len: fields_count, rtype: HID_OUTPUT_REPORT,
267 reqtype: HID_REQ_SET_REPORT);
268 }
269
270 return ret == fields_count ? 0 : -1;
271}
272
273/*
274 * Effectively send the message to the device, waiting for its answer.
275 *
276 * Must be called with hidpp->send_mutex locked
277 *
278 * Same return protocol than hidpp_send_message_sync():
279 * - success on 0
280 * - negative error means transport error
281 * - positive value means protocol error
282 */
283static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp,
284 struct hidpp_report *message,
285 struct hidpp_report *response)
286{
287 int ret;
288
289 __must_hold(&hidpp->send_mutex);
290
291 hidpp->send_receive_buf = response;
292 hidpp->answer_available = false;
293
294 /*
295 * So that we can later validate the answer when it arrives
296 * in hidpp_raw_event
297 */
298 *response = *message;
299
300 ret = __hidpp_send_report(hdev: hidpp->hid_dev, hidpp_report: message);
301 if (ret) {
302 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
303 memset(response, 0, sizeof(struct hidpp_report));
304 return ret;
305 }
306
307 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
308 5*HZ)) {
309 dbg_hid("%s:timeout waiting for response\n", __func__);
310 memset(response, 0, sizeof(struct hidpp_report));
311 return -ETIMEDOUT;
312 }
313
314 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
315 response->rap.sub_id == HIDPP_ERROR) {
316 ret = response->rap.params[1];
317 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
318 return ret;
319 }
320
321 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
322 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
323 response->fap.feature_index == HIDPP20_ERROR) {
324 ret = response->fap.params[1];
325 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
326 return ret;
327 }
328
329 return 0;
330}
331
332/*
333 * hidpp_send_message_sync() returns 0 in case of success, and something else
334 * in case of a failure.
335 *
336 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
337 * value.
338 */
339static int hidpp_send_message_sync(struct hidpp_device *hidpp,
340 struct hidpp_report *message,
341 struct hidpp_report *response)
342{
343 int ret;
344 int max_retries = 3;
345
346 mutex_lock(&hidpp->send_mutex);
347
348 do {
349 ret = __do_hidpp_send_message_sync(hidpp, message, response);
350 if (ret != HIDPP20_ERROR_BUSY)
351 break;
352
353 dbg_hid("%s:got busy hidpp 2.0 error %02X, retrying\n", __func__, ret);
354 } while (--max_retries);
355
356 mutex_unlock(lock: &hidpp->send_mutex);
357 return ret;
358
359}
360
361/*
362 * hidpp_send_fap_command_sync() returns 0 in case of success, and something else
363 * in case of a failure.
364 *
365 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
366 * value.
367 */
368static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
369 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
370 struct hidpp_report *response)
371{
372 struct hidpp_report *message;
373 int ret;
374
375 if (param_count > sizeof(message->fap.params)) {
376 hid_dbg(hidpp->hid_dev,
377 "Invalid number of parameters passed to command (%d != %llu)\n",
378 param_count,
379 (unsigned long long) sizeof(message->fap.params));
380 return -EINVAL;
381 }
382
383 message = kzalloc(size: sizeof(struct hidpp_report), GFP_KERNEL);
384 if (!message)
385 return -ENOMEM;
386
387 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
388 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
389 else
390 message->report_id = REPORT_ID_HIDPP_LONG;
391 message->fap.feature_index = feat_index;
392 message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID;
393 memcpy(&message->fap.params, params, param_count);
394
395 ret = hidpp_send_message_sync(hidpp, message, response);
396 kfree(objp: message);
397 return ret;
398}
399
400/*
401 * hidpp_send_rap_command_sync() returns 0 in case of success, and something else
402 * in case of a failure.
403 *
404 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned
405 * value.
406 */
407static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
408 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
409 struct hidpp_report *response)
410{
411 struct hidpp_report *message;
412 int ret, max_count;
413
414 /* Send as long report if short reports are not supported. */
415 if (report_id == REPORT_ID_HIDPP_SHORT &&
416 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
417 report_id = REPORT_ID_HIDPP_LONG;
418
419 switch (report_id) {
420 case REPORT_ID_HIDPP_SHORT:
421 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
422 break;
423 case REPORT_ID_HIDPP_LONG:
424 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
425 break;
426 case REPORT_ID_HIDPP_VERY_LONG:
427 max_count = hidpp_dev->very_long_report_length - 4;
428 break;
429 default:
430 return -EINVAL;
431 }
432
433 if (param_count > max_count)
434 return -EINVAL;
435
436 message = kzalloc(size: sizeof(struct hidpp_report), GFP_KERNEL);
437 if (!message)
438 return -ENOMEM;
439 message->report_id = report_id;
440 message->rap.sub_id = sub_id;
441 message->rap.reg_address = reg_address;
442 memcpy(&message->rap.params, params, param_count);
443
444 ret = hidpp_send_message_sync(hidpp: hidpp_dev, message, response);
445 kfree(objp: message);
446 return ret;
447}
448
449static inline bool hidpp_match_answer(struct hidpp_report *question,
450 struct hidpp_report *answer)
451{
452 return (answer->fap.feature_index == question->fap.feature_index) &&
453 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
454}
455
456static inline bool hidpp_match_error(struct hidpp_report *question,
457 struct hidpp_report *answer)
458{
459 return ((answer->rap.sub_id == HIDPP_ERROR) ||
460 (answer->fap.feature_index == HIDPP20_ERROR)) &&
461 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
462 (answer->fap.params[0] == question->fap.funcindex_clientid);
463}
464
465static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
466 struct hidpp_report *report)
467{
468 return (hidpp->wireless_feature_index &&
469 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
470 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
471 (report->rap.sub_id == 0x41));
472}
473
474/*
475 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
476 */
477static void hidpp_prefix_name(char **name, int name_length)
478{
479#define PREFIX_LENGTH 9 /* "Logitech " */
480
481 int new_length;
482 char *new_name;
483
484 if (name_length > PREFIX_LENGTH &&
485 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
486 /* The prefix has is already in the name */
487 return;
488
489 new_length = PREFIX_LENGTH + name_length;
490 new_name = kzalloc(size: new_length, GFP_KERNEL);
491 if (!new_name)
492 return;
493
494 snprintf(buf: new_name, size: new_length, fmt: "Logitech %s", *name);
495
496 kfree(objp: *name);
497
498 *name = new_name;
499}
500
501/*
502 * Updates the USB wireless_status based on whether the headset
503 * is turned on and reachable.
504 */
505static void hidpp_update_usb_wireless_status(struct hidpp_device *hidpp)
506{
507 struct hid_device *hdev = hidpp->hid_dev;
508 struct usb_interface *intf;
509
510 if (!(hidpp->quirks & HIDPP_QUIRK_WIRELESS_STATUS))
511 return;
512 if (!hid_is_usb(hdev))
513 return;
514
515 intf = to_usb_interface(hdev->dev.parent);
516 usb_set_wireless_status(iface: intf, status: hidpp->battery.online ?
517 USB_WIRELESS_STATUS_CONNECTED :
518 USB_WIRELESS_STATUS_DISCONNECTED);
519}
520
521/**
522 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
523 * events given a high-resolution wheel
524 * movement.
525 * @input_dev: Pointer to the input device
526 * @counter: a hid_scroll_counter struct describing the wheel.
527 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
528 * units.
529 *
530 * Given a high-resolution movement, this function converts the movement into
531 * fractions of 120 and emits high-resolution scroll events for the input
532 * device. It also uses the multiplier from &struct hid_scroll_counter to
533 * emit low-resolution scroll events when appropriate for
534 * backwards-compatibility with userspace input libraries.
535 */
536static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
537 struct hidpp_scroll_counter *counter,
538 int hi_res_value)
539{
540 int low_res_value, remainder, direction;
541 unsigned long long now, previous;
542
543 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
544 input_report_rel(dev: input_dev, REL_WHEEL_HI_RES, value: hi_res_value);
545
546 remainder = counter->remainder;
547 direction = hi_res_value > 0 ? 1 : -1;
548
549 now = sched_clock();
550 previous = counter->last_time;
551 counter->last_time = now;
552 /*
553 * Reset the remainder after a period of inactivity or when the
554 * direction changes. This prevents the REL_WHEEL emulation point
555 * from sliding for devices that don't always provide the same
556 * number of movements per detent.
557 */
558 if (now - previous > 1000000000 || direction != counter->direction)
559 remainder = 0;
560
561 counter->direction = direction;
562 remainder += hi_res_value;
563
564 /* Some wheels will rest 7/8ths of a detent from the previous detent
565 * after slow movement, so we want the threshold for low-res events to
566 * be in the middle between two detents (e.g. after 4/8ths) as
567 * opposed to on the detents themselves (8/8ths).
568 */
569 if (abs(remainder) >= 60) {
570 /* Add (or subtract) 1 because we want to trigger when the wheel
571 * is half-way to the next detent (i.e. scroll 1 detent after a
572 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
573 * etc.).
574 */
575 low_res_value = remainder / 120;
576 if (low_res_value == 0)
577 low_res_value = (hi_res_value > 0 ? 1 : -1);
578 input_report_rel(dev: input_dev, REL_WHEEL, value: low_res_value);
579 remainder -= low_res_value * 120;
580 }
581 counter->remainder = remainder;
582}
583
584/* -------------------------------------------------------------------------- */
585/* HIDP++ 1.0 commands */
586/* -------------------------------------------------------------------------- */
587
588#define HIDPP_SET_REGISTER 0x80
589#define HIDPP_GET_REGISTER 0x81
590#define HIDPP_SET_LONG_REGISTER 0x82
591#define HIDPP_GET_LONG_REGISTER 0x83
592
593/**
594 * hidpp10_set_register - Modify a HID++ 1.0 register.
595 * @hidpp_dev: the device to set the register on.
596 * @register_address: the address of the register to modify.
597 * @byte: the byte of the register to modify. Should be less than 3.
598 * @mask: mask of the bits to modify
599 * @value: new values for the bits in mask
600 * Return: 0 if successful, otherwise a negative error code.
601 */
602static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
603 u8 register_address, u8 byte, u8 mask, u8 value)
604{
605 struct hidpp_report response;
606 int ret;
607 u8 params[3] = { 0 };
608
609 ret = hidpp_send_rap_command_sync(hidpp_dev,
610 REPORT_ID_HIDPP_SHORT,
611 HIDPP_GET_REGISTER,
612 reg_address: register_address,
613 NULL, param_count: 0, response: &response);
614 if (ret)
615 return ret;
616
617 memcpy(params, response.rap.params, 3);
618
619 params[byte] &= ~mask;
620 params[byte] |= value & mask;
621
622 return hidpp_send_rap_command_sync(hidpp_dev,
623 REPORT_ID_HIDPP_SHORT,
624 HIDPP_SET_REGISTER,
625 reg_address: register_address,
626 params, param_count: 3, response: &response);
627}
628
629#define HIDPP_REG_ENABLE_REPORTS 0x00
630#define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
631#define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
632#define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
633#define HIDPP_ENABLE_BAT_REPORT BIT(4)
634#define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
635
636static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
637{
638 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, byte: 0,
639 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
640}
641
642#define HIDPP_REG_FEATURES 0x01
643#define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
644#define HIDPP_ENABLE_FAST_SCROLL BIT(6)
645
646/* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
647static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
648{
649 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, byte: 0,
650 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
651}
652
653#define HIDPP_REG_BATTERY_STATUS 0x07
654
655static int hidpp10_battery_status_map_level(u8 param)
656{
657 int level;
658
659 switch (param) {
660 case 1 ... 2:
661 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
662 break;
663 case 3 ... 4:
664 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
665 break;
666 case 5 ... 6:
667 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
668 break;
669 case 7:
670 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
671 break;
672 default:
673 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
674 }
675
676 return level;
677}
678
679static int hidpp10_battery_status_map_status(u8 param)
680{
681 int status;
682
683 switch (param) {
684 case 0x00:
685 /* discharging (in use) */
686 status = POWER_SUPPLY_STATUS_DISCHARGING;
687 break;
688 case 0x21: /* (standard) charging */
689 case 0x24: /* fast charging */
690 case 0x25: /* slow charging */
691 status = POWER_SUPPLY_STATUS_CHARGING;
692 break;
693 case 0x26: /* topping charge */
694 case 0x22: /* charge complete */
695 status = POWER_SUPPLY_STATUS_FULL;
696 break;
697 case 0x20: /* unknown */
698 status = POWER_SUPPLY_STATUS_UNKNOWN;
699 break;
700 /*
701 * 0x01...0x1F = reserved (not charging)
702 * 0x23 = charging error
703 * 0x27..0xff = reserved
704 */
705 default:
706 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
707 break;
708 }
709
710 return status;
711}
712
713static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
714{
715 struct hidpp_report response;
716 int ret, status;
717
718 ret = hidpp_send_rap_command_sync(hidpp_dev: hidpp,
719 REPORT_ID_HIDPP_SHORT,
720 HIDPP_GET_REGISTER,
721 HIDPP_REG_BATTERY_STATUS,
722 NULL, param_count: 0, response: &response);
723 if (ret)
724 return ret;
725
726 hidpp->battery.level =
727 hidpp10_battery_status_map_level(param: response.rap.params[0]);
728 status = hidpp10_battery_status_map_status(param: response.rap.params[1]);
729 hidpp->battery.status = status;
730 /* the capacity is only available when discharging or full */
731 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
732 status == POWER_SUPPLY_STATUS_FULL;
733
734 return 0;
735}
736
737#define HIDPP_REG_BATTERY_MILEAGE 0x0D
738
739static int hidpp10_battery_mileage_map_status(u8 param)
740{
741 int status;
742
743 switch (param >> 6) {
744 case 0x00:
745 /* discharging (in use) */
746 status = POWER_SUPPLY_STATUS_DISCHARGING;
747 break;
748 case 0x01: /* charging */
749 status = POWER_SUPPLY_STATUS_CHARGING;
750 break;
751 case 0x02: /* charge complete */
752 status = POWER_SUPPLY_STATUS_FULL;
753 break;
754 /*
755 * 0x03 = charging error
756 */
757 default:
758 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
759 break;
760 }
761
762 return status;
763}
764
765static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
766{
767 struct hidpp_report response;
768 int ret, status;
769
770 ret = hidpp_send_rap_command_sync(hidpp_dev: hidpp,
771 REPORT_ID_HIDPP_SHORT,
772 HIDPP_GET_REGISTER,
773 HIDPP_REG_BATTERY_MILEAGE,
774 NULL, param_count: 0, response: &response);
775 if (ret)
776 return ret;
777
778 hidpp->battery.capacity = response.rap.params[0];
779 status = hidpp10_battery_mileage_map_status(param: response.rap.params[2]);
780 hidpp->battery.status = status;
781 /* the capacity is only available when discharging or full */
782 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
783 status == POWER_SUPPLY_STATUS_FULL;
784
785 return 0;
786}
787
788static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
789{
790 struct hidpp_report *report = (struct hidpp_report *)data;
791 int status, capacity, level;
792 bool changed;
793
794 if (report->report_id != REPORT_ID_HIDPP_SHORT)
795 return 0;
796
797 switch (report->rap.sub_id) {
798 case HIDPP_REG_BATTERY_STATUS:
799 capacity = hidpp->battery.capacity;
800 level = hidpp10_battery_status_map_level(param: report->rawbytes[1]);
801 status = hidpp10_battery_status_map_status(param: report->rawbytes[2]);
802 break;
803 case HIDPP_REG_BATTERY_MILEAGE:
804 capacity = report->rap.params[0];
805 level = hidpp->battery.level;
806 status = hidpp10_battery_mileage_map_status(param: report->rawbytes[3]);
807 break;
808 default:
809 return 0;
810 }
811
812 changed = capacity != hidpp->battery.capacity ||
813 level != hidpp->battery.level ||
814 status != hidpp->battery.status;
815
816 /* the capacity is only available when discharging or full */
817 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
818 status == POWER_SUPPLY_STATUS_FULL;
819
820 if (changed) {
821 hidpp->battery.level = level;
822 hidpp->battery.status = status;
823 if (hidpp->battery.ps)
824 power_supply_changed(psy: hidpp->battery.ps);
825 }
826
827 return 0;
828}
829
830#define HIDPP_REG_PAIRING_INFORMATION 0xB5
831#define HIDPP_EXTENDED_PAIRING 0x30
832#define HIDPP_DEVICE_NAME 0x40
833
834static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
835{
836 struct hidpp_report response;
837 int ret;
838 u8 params[1] = { HIDPP_DEVICE_NAME };
839 char *name;
840 int len;
841
842 ret = hidpp_send_rap_command_sync(hidpp_dev,
843 REPORT_ID_HIDPP_SHORT,
844 HIDPP_GET_LONG_REGISTER,
845 HIDPP_REG_PAIRING_INFORMATION,
846 params, param_count: 1, response: &response);
847 if (ret)
848 return NULL;
849
850 len = response.rap.params[1];
851
852 if (2 + len > sizeof(response.rap.params))
853 return NULL;
854
855 if (len < 4) /* logitech devices are usually at least Xddd */
856 return NULL;
857
858 name = kzalloc(size: len + 1, GFP_KERNEL);
859 if (!name)
860 return NULL;
861
862 memcpy(name, &response.rap.params[2], len);
863
864 /* include the terminating '\0' */
865 hidpp_prefix_name(name: &name, name_length: len + 1);
866
867 return name;
868}
869
870static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
871{
872 struct hidpp_report response;
873 int ret;
874 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
875
876 ret = hidpp_send_rap_command_sync(hidpp_dev: hidpp,
877 REPORT_ID_HIDPP_SHORT,
878 HIDPP_GET_LONG_REGISTER,
879 HIDPP_REG_PAIRING_INFORMATION,
880 params, param_count: 1, response: &response);
881 if (ret)
882 return ret;
883
884 /*
885 * We don't care about LE or BE, we will output it as a string
886 * with %4phD, so we need to keep the order.
887 */
888 *serial = *((u32 *)&response.rap.params[1]);
889 return 0;
890}
891
892static int hidpp_unifying_init(struct hidpp_device *hidpp)
893{
894 struct hid_device *hdev = hidpp->hid_dev;
895 const char *name;
896 u32 serial;
897 int ret;
898
899 ret = hidpp_unifying_get_serial(hidpp, serial: &serial);
900 if (ret)
901 return ret;
902
903 snprintf(buf: hdev->uniq, size: sizeof(hdev->uniq), fmt: "%4phD", &serial);
904 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
905
906 name = hidpp_unifying_get_name(hidpp_dev: hidpp);
907 if (!name)
908 return -EIO;
909
910 snprintf(buf: hdev->name, size: sizeof(hdev->name), fmt: "%s", name);
911 dbg_hid("HID++ Unifying: Got name: %s\n", name);
912
913 kfree(objp: name);
914 return 0;
915}
916
917/* -------------------------------------------------------------------------- */
918/* 0x0000: Root */
919/* -------------------------------------------------------------------------- */
920
921#define HIDPP_PAGE_ROOT 0x0000
922#define HIDPP_PAGE_ROOT_IDX 0x00
923
924#define CMD_ROOT_GET_FEATURE 0x00
925#define CMD_ROOT_GET_PROTOCOL_VERSION 0x10
926
927static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
928 u8 *feature_index, u8 *feature_type)
929{
930 struct hidpp_report response;
931 int ret;
932 u8 params[2] = { feature >> 8, feature & 0x00FF };
933
934 ret = hidpp_send_fap_command_sync(hidpp,
935 HIDPP_PAGE_ROOT_IDX,
936 CMD_ROOT_GET_FEATURE,
937 params, param_count: 2, response: &response);
938 if (ret)
939 return ret;
940
941 if (response.fap.params[0] == 0)
942 return -ENOENT;
943
944 *feature_index = response.fap.params[0];
945 *feature_type = response.fap.params[1];
946
947 return ret;
948}
949
950static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
951{
952 const u8 ping_byte = 0x5a;
953 u8 ping_data[3] = { 0, 0, ping_byte };
954 struct hidpp_report response;
955 int ret;
956
957 ret = hidpp_send_rap_command_sync(hidpp_dev: hidpp,
958 REPORT_ID_HIDPP_SHORT,
959 HIDPP_PAGE_ROOT_IDX,
960 CMD_ROOT_GET_PROTOCOL_VERSION | LINUX_KERNEL_SW_ID,
961 params: ping_data, param_count: sizeof(ping_data), response: &response);
962
963 if (ret == HIDPP_ERROR_INVALID_SUBID) {
964 hidpp->protocol_major = 1;
965 hidpp->protocol_minor = 0;
966 goto print_version;
967 }
968
969 /* the device might not be connected */
970 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
971 return -EIO;
972
973 if (ret > 0) {
974 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
975 __func__, ret);
976 return -EPROTO;
977 }
978 if (ret)
979 return ret;
980
981 if (response.rap.params[2] != ping_byte) {
982 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
983 __func__, response.rap.params[2], ping_byte);
984 return -EPROTO;
985 }
986
987 hidpp->protocol_major = response.rap.params[0];
988 hidpp->protocol_minor = response.rap.params[1];
989
990print_version:
991 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
992 hidpp->protocol_major, hidpp->protocol_minor);
993 return 0;
994}
995
996/* -------------------------------------------------------------------------- */
997/* 0x0003: Device Information */
998/* -------------------------------------------------------------------------- */
999
1000#define HIDPP_PAGE_DEVICE_INFORMATION 0x0003
1001
1002#define CMD_GET_DEVICE_INFO 0x00
1003
1004static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
1005{
1006 struct hidpp_report response;
1007 u8 feature_type;
1008 u8 feature_index;
1009 int ret;
1010
1011 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
1012 feature_index: &feature_index,
1013 feature_type: &feature_type);
1014 if (ret)
1015 return ret;
1016
1017 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1018 CMD_GET_DEVICE_INFO,
1019 NULL, param_count: 0, response: &response);
1020 if (ret)
1021 return ret;
1022
1023 /* See hidpp_unifying_get_serial() */
1024 *serial = *((u32 *)&response.rap.params[1]);
1025 return 0;
1026}
1027
1028static int hidpp_serial_init(struct hidpp_device *hidpp)
1029{
1030 struct hid_device *hdev = hidpp->hid_dev;
1031 u32 serial;
1032 int ret;
1033
1034 ret = hidpp_get_serial(hidpp, serial: &serial);
1035 if (ret)
1036 return ret;
1037
1038 snprintf(buf: hdev->uniq, size: sizeof(hdev->uniq), fmt: "%4phD", &serial);
1039 dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
1040
1041 return 0;
1042}
1043
1044/* -------------------------------------------------------------------------- */
1045/* 0x0005: GetDeviceNameType */
1046/* -------------------------------------------------------------------------- */
1047
1048#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
1049
1050#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x00
1051#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x10
1052#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x20
1053
1054static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
1055 u8 feature_index, u8 *nameLength)
1056{
1057 struct hidpp_report response;
1058 int ret;
1059
1060 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1061 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, param_count: 0, response: &response);
1062
1063 if (ret > 0) {
1064 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1065 __func__, ret);
1066 return -EPROTO;
1067 }
1068 if (ret)
1069 return ret;
1070
1071 *nameLength = response.fap.params[0];
1072
1073 return ret;
1074}
1075
1076static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
1077 u8 feature_index, u8 char_index, char *device_name, int len_buf)
1078{
1079 struct hidpp_report response;
1080 int ret, i;
1081 int count;
1082
1083 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1084 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, params: &char_index, param_count: 1,
1085 response: &response);
1086
1087 if (ret > 0) {
1088 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1089 __func__, ret);
1090 return -EPROTO;
1091 }
1092 if (ret)
1093 return ret;
1094
1095 switch (response.report_id) {
1096 case REPORT_ID_HIDPP_VERY_LONG:
1097 count = hidpp->very_long_report_length - 4;
1098 break;
1099 case REPORT_ID_HIDPP_LONG:
1100 count = HIDPP_REPORT_LONG_LENGTH - 4;
1101 break;
1102 case REPORT_ID_HIDPP_SHORT:
1103 count = HIDPP_REPORT_SHORT_LENGTH - 4;
1104 break;
1105 default:
1106 return -EPROTO;
1107 }
1108
1109 if (len_buf < count)
1110 count = len_buf;
1111
1112 for (i = 0; i < count; i++)
1113 device_name[i] = response.fap.params[i];
1114
1115 return count;
1116}
1117
1118static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1119{
1120 u8 feature_type;
1121 u8 feature_index;
1122 u8 __name_length;
1123 char *name;
1124 unsigned index = 0;
1125 int ret;
1126
1127 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1128 feature_index: &feature_index, feature_type: &feature_type);
1129 if (ret)
1130 return NULL;
1131
1132 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1133 nameLength: &__name_length);
1134 if (ret)
1135 return NULL;
1136
1137 name = kzalloc(size: __name_length + 1, GFP_KERNEL);
1138 if (!name)
1139 return NULL;
1140
1141 while (index < __name_length) {
1142 ret = hidpp_devicenametype_get_device_name(hidpp,
1143 feature_index, char_index: index, device_name: name + index,
1144 len_buf: __name_length - index);
1145 if (ret <= 0) {
1146 kfree(objp: name);
1147 return NULL;
1148 }
1149 index += ret;
1150 }
1151
1152 /* include the terminating '\0' */
1153 hidpp_prefix_name(name: &name, name_length: __name_length + 1);
1154
1155 return name;
1156}
1157
1158/* -------------------------------------------------------------------------- */
1159/* 0x1000: Battery level status */
1160/* -------------------------------------------------------------------------- */
1161
1162#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1163
1164#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1165#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1166
1167#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1168
1169#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1170#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1171#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1172
1173static int hidpp_map_battery_level(int capacity)
1174{
1175 if (capacity < 11)
1176 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1177 /*
1178 * The spec says this should be < 31 but some devices report 30
1179 * with brand new batteries and Windows reports 30 as "Good".
1180 */
1181 else if (capacity < 30)
1182 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1183 else if (capacity < 81)
1184 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1185 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1186}
1187
1188static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1189 int *next_capacity,
1190 int *level)
1191{
1192 int status;
1193
1194 *capacity = data[0];
1195 *next_capacity = data[1];
1196 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1197
1198 /* When discharging, we can rely on the device reported capacity.
1199 * For all other states the device reports 0 (unknown).
1200 */
1201 switch (data[2]) {
1202 case 0: /* discharging (in use) */
1203 status = POWER_SUPPLY_STATUS_DISCHARGING;
1204 *level = hidpp_map_battery_level(capacity: *capacity);
1205 break;
1206 case 1: /* recharging */
1207 status = POWER_SUPPLY_STATUS_CHARGING;
1208 break;
1209 case 2: /* charge in final stage */
1210 status = POWER_SUPPLY_STATUS_CHARGING;
1211 break;
1212 case 3: /* charge complete */
1213 status = POWER_SUPPLY_STATUS_FULL;
1214 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1215 *capacity = 100;
1216 break;
1217 case 4: /* recharging below optimal speed */
1218 status = POWER_SUPPLY_STATUS_CHARGING;
1219 break;
1220 /* 5 = invalid battery type
1221 6 = thermal error
1222 7 = other charging error */
1223 default:
1224 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1225 break;
1226 }
1227
1228 return status;
1229}
1230
1231static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1232 u8 feature_index,
1233 int *status,
1234 int *capacity,
1235 int *next_capacity,
1236 int *level)
1237{
1238 struct hidpp_report response;
1239 int ret;
1240 u8 *params = (u8 *)response.fap.params;
1241
1242 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1243 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1244 NULL, param_count: 0, response: &response);
1245 /* Ignore these intermittent errors */
1246 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1247 return -EIO;
1248 if (ret > 0) {
1249 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1250 __func__, ret);
1251 return -EPROTO;
1252 }
1253 if (ret)
1254 return ret;
1255
1256 *status = hidpp20_batterylevel_map_status_capacity(data: params, capacity,
1257 next_capacity,
1258 level);
1259
1260 return 0;
1261}
1262
1263static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1264 u8 feature_index)
1265{
1266 struct hidpp_report response;
1267 int ret;
1268 u8 *params = (u8 *)response.fap.params;
1269 unsigned int level_count, flags;
1270
1271 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1272 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1273 NULL, param_count: 0, response: &response);
1274 if (ret > 0) {
1275 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1276 __func__, ret);
1277 return -EPROTO;
1278 }
1279 if (ret)
1280 return ret;
1281
1282 level_count = params[0];
1283 flags = params[1];
1284
1285 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1286 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1287 else
1288 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1289
1290 return 0;
1291}
1292
1293static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1294{
1295 u8 feature_type;
1296 int ret;
1297 int status, capacity, next_capacity, level;
1298
1299 if (hidpp->battery.feature_index == 0xff) {
1300 ret = hidpp_root_get_feature(hidpp,
1301 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1302 feature_index: &hidpp->battery.feature_index,
1303 feature_type: &feature_type);
1304 if (ret)
1305 return ret;
1306 }
1307
1308 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1309 feature_index: hidpp->battery.feature_index,
1310 status: &status, capacity: &capacity,
1311 next_capacity: &next_capacity, level: &level);
1312 if (ret)
1313 return ret;
1314
1315 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1316 feature_index: hidpp->battery.feature_index);
1317 if (ret)
1318 return ret;
1319
1320 hidpp->battery.status = status;
1321 hidpp->battery.capacity = capacity;
1322 hidpp->battery.level = level;
1323 /* the capacity is only available when discharging or full */
1324 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1325 status == POWER_SUPPLY_STATUS_FULL;
1326
1327 return 0;
1328}
1329
1330static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1331 u8 *data, int size)
1332{
1333 struct hidpp_report *report = (struct hidpp_report *)data;
1334 int status, capacity, next_capacity, level;
1335 bool changed;
1336
1337 if (report->fap.feature_index != hidpp->battery.feature_index ||
1338 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1339 return 0;
1340
1341 status = hidpp20_batterylevel_map_status_capacity(data: report->fap.params,
1342 capacity: &capacity,
1343 next_capacity: &next_capacity,
1344 level: &level);
1345
1346 /* the capacity is only available when discharging or full */
1347 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1348 status == POWER_SUPPLY_STATUS_FULL;
1349
1350 changed = capacity != hidpp->battery.capacity ||
1351 level != hidpp->battery.level ||
1352 status != hidpp->battery.status;
1353
1354 if (changed) {
1355 hidpp->battery.level = level;
1356 hidpp->battery.capacity = capacity;
1357 hidpp->battery.status = status;
1358 if (hidpp->battery.ps)
1359 power_supply_changed(psy: hidpp->battery.ps);
1360 }
1361
1362 return 0;
1363}
1364
1365/* -------------------------------------------------------------------------- */
1366/* 0x1001: Battery voltage */
1367/* -------------------------------------------------------------------------- */
1368
1369#define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1370
1371#define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1372
1373#define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1374
1375static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1376 int *level, int *charge_type)
1377{
1378 int status;
1379
1380 long flags = (long) data[2];
1381 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1382
1383 if (flags & 0x80)
1384 switch (flags & 0x07) {
1385 case 0:
1386 status = POWER_SUPPLY_STATUS_CHARGING;
1387 break;
1388 case 1:
1389 status = POWER_SUPPLY_STATUS_FULL;
1390 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1391 break;
1392 case 2:
1393 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1394 break;
1395 default:
1396 status = POWER_SUPPLY_STATUS_UNKNOWN;
1397 break;
1398 }
1399 else
1400 status = POWER_SUPPLY_STATUS_DISCHARGING;
1401
1402 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1403 if (test_bit(3, &flags)) {
1404 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1405 }
1406 if (test_bit(4, &flags)) {
1407 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1408 }
1409 if (test_bit(5, &flags)) {
1410 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1411 }
1412
1413 *voltage = get_unaligned_be16(p: data);
1414
1415 return status;
1416}
1417
1418static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1419 u8 feature_index,
1420 int *status, int *voltage,
1421 int *level, int *charge_type)
1422{
1423 struct hidpp_report response;
1424 int ret;
1425 u8 *params = (u8 *)response.fap.params;
1426
1427 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1428 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1429 NULL, param_count: 0, response: &response);
1430
1431 if (ret > 0) {
1432 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1433 __func__, ret);
1434 return -EPROTO;
1435 }
1436 if (ret)
1437 return ret;
1438
1439 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1440
1441 *status = hidpp20_battery_map_status_voltage(data: params, voltage,
1442 level, charge_type);
1443
1444 return 0;
1445}
1446
1447static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage)
1448{
1449 /* NB: This voltage curve doesn't necessarily map perfectly to all
1450 * devices that implement the BATTERY_VOLTAGE feature. This is because
1451 * there are a few devices that use different battery technology.
1452 */
1453
1454 static const int voltages[100] = {
1455 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075,
1456 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997,
1457 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929,
1458 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865,
1459 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815,
1460 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781,
1461 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754,
1462 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720,
1463 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677,
1464 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537
1465 };
1466
1467 int i;
1468
1469 if (unlikely(voltage < 3500 || voltage >= 5000))
1470 hid_warn_once(hid_dev,
1471 "%s: possibly using the wrong voltage curve\n",
1472 __func__);
1473
1474 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1475 if (voltage >= voltages[i])
1476 return ARRAY_SIZE(voltages) - i;
1477 }
1478
1479 return 0;
1480}
1481
1482static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1483{
1484 u8 feature_type;
1485 int ret;
1486 int status, voltage, level, charge_type;
1487
1488 if (hidpp->battery.voltage_feature_index == 0xff) {
1489 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1490 feature_index: &hidpp->battery.voltage_feature_index,
1491 feature_type: &feature_type);
1492 if (ret)
1493 return ret;
1494 }
1495
1496 ret = hidpp20_battery_get_battery_voltage(hidpp,
1497 feature_index: hidpp->battery.voltage_feature_index,
1498 status: &status, voltage: &voltage, level: &level, charge_type: &charge_type);
1499
1500 if (ret)
1501 return ret;
1502
1503 hidpp->battery.status = status;
1504 hidpp->battery.voltage = voltage;
1505 hidpp->battery.capacity = hidpp20_map_battery_capacity(hid_dev: hidpp->hid_dev,
1506 voltage);
1507 hidpp->battery.level = level;
1508 hidpp->battery.charge_type = charge_type;
1509 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1510
1511 return 0;
1512}
1513
1514static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1515 u8 *data, int size)
1516{
1517 struct hidpp_report *report = (struct hidpp_report *)data;
1518 int status, voltage, level, charge_type;
1519
1520 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1521 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1522 return 0;
1523
1524 status = hidpp20_battery_map_status_voltage(data: report->fap.params, voltage: &voltage,
1525 level: &level, charge_type: &charge_type);
1526
1527 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1528
1529 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1530 hidpp->battery.voltage = voltage;
1531 hidpp->battery.capacity = hidpp20_map_battery_capacity(hid_dev: hidpp->hid_dev,
1532 voltage);
1533 hidpp->battery.status = status;
1534 hidpp->battery.level = level;
1535 hidpp->battery.charge_type = charge_type;
1536 if (hidpp->battery.ps)
1537 power_supply_changed(psy: hidpp->battery.ps);
1538 }
1539 return 0;
1540}
1541
1542/* -------------------------------------------------------------------------- */
1543/* 0x1004: Unified battery */
1544/* -------------------------------------------------------------------------- */
1545
1546#define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1547
1548#define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1549#define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1550
1551#define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1552
1553#define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1554#define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1555#define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1556#define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1557
1558#define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1559#define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1560
1561static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1562 u8 feature_index)
1563{
1564 struct hidpp_report response;
1565 int ret;
1566 u8 *params = (u8 *)response.fap.params;
1567
1568 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1569 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1570 /* we have already set the device capabilities, so let's skip */
1571 return 0;
1572 }
1573
1574 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1575 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1576 NULL, param_count: 0, response: &response);
1577 /* Ignore these intermittent errors */
1578 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1579 return -EIO;
1580 if (ret > 0) {
1581 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1582 __func__, ret);
1583 return -EPROTO;
1584 }
1585 if (ret)
1586 return ret;
1587
1588 /*
1589 * If the device supports state of charge (battery percentage) we won't
1590 * export the battery level information. there are 4 possible battery
1591 * levels and they all are optional, this means that the device might
1592 * not support any of them, we are just better off with the battery
1593 * percentage.
1594 */
1595 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1596 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1597 hidpp->battery.supported_levels_1004 = 0;
1598 } else {
1599 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1600 hidpp->battery.supported_levels_1004 = params[0];
1601 }
1602
1603 return 0;
1604}
1605
1606static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1607 u8 charging_status,
1608 u8 external_power_status)
1609{
1610 int status;
1611
1612 switch (charging_status) {
1613 case 0: /* discharging */
1614 status = POWER_SUPPLY_STATUS_DISCHARGING;
1615 break;
1616 case 1: /* charging */
1617 case 2: /* charging slow */
1618 status = POWER_SUPPLY_STATUS_CHARGING;
1619 break;
1620 case 3: /* complete */
1621 status = POWER_SUPPLY_STATUS_FULL;
1622 break;
1623 case 4: /* error */
1624 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1625 hid_info(hidpp->hid_dev, "%s: charging error",
1626 hidpp->name);
1627 break;
1628 default:
1629 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1630 break;
1631 }
1632
1633 return status;
1634}
1635
1636static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1637 u8 battery_level)
1638{
1639 /* cler unsupported level bits */
1640 battery_level &= hidpp->battery.supported_levels_1004;
1641
1642 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1643 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1644 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1645 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1646 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1647 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1648 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1649 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1650
1651 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1652}
1653
1654static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1655 u8 feature_index,
1656 u8 *state_of_charge,
1657 int *status,
1658 int *level)
1659{
1660 struct hidpp_report response;
1661 int ret;
1662 u8 *params = (u8 *)response.fap.params;
1663
1664 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1665 CMD_UNIFIED_BATTERY_GET_STATUS,
1666 NULL, param_count: 0, response: &response);
1667 /* Ignore these intermittent errors */
1668 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1669 return -EIO;
1670 if (ret > 0) {
1671 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1672 __func__, ret);
1673 return -EPROTO;
1674 }
1675 if (ret)
1676 return ret;
1677
1678 *state_of_charge = params[0];
1679 *status = hidpp20_unifiedbattery_map_status(hidpp, charging_status: params[2], external_power_status: params[3]);
1680 *level = hidpp20_unifiedbattery_map_level(hidpp, battery_level: params[1]);
1681
1682 return 0;
1683}
1684
1685static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1686{
1687 u8 feature_type;
1688 int ret;
1689 u8 state_of_charge;
1690 int status, level;
1691
1692 if (hidpp->battery.feature_index == 0xff) {
1693 ret = hidpp_root_get_feature(hidpp,
1694 HIDPP_PAGE_UNIFIED_BATTERY,
1695 feature_index: &hidpp->battery.feature_index,
1696 feature_type: &feature_type);
1697 if (ret)
1698 return ret;
1699 }
1700
1701 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1702 feature_index: hidpp->battery.feature_index);
1703 if (ret)
1704 return ret;
1705
1706 ret = hidpp20_unifiedbattery_get_status(hidpp,
1707 feature_index: hidpp->battery.feature_index,
1708 state_of_charge: &state_of_charge,
1709 status: &status,
1710 level: &level);
1711 if (ret)
1712 return ret;
1713
1714 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1715 hidpp->battery.capacity = state_of_charge;
1716 hidpp->battery.status = status;
1717 hidpp->battery.level = level;
1718 hidpp->battery.online = true;
1719
1720 return 0;
1721}
1722
1723static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1724 u8 *data, int size)
1725{
1726 struct hidpp_report *report = (struct hidpp_report *)data;
1727 u8 *params = (u8 *)report->fap.params;
1728 int state_of_charge, status, level;
1729 bool changed;
1730
1731 if (report->fap.feature_index != hidpp->battery.feature_index ||
1732 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1733 return 0;
1734
1735 state_of_charge = params[0];
1736 status = hidpp20_unifiedbattery_map_status(hidpp, charging_status: params[2], external_power_status: params[3]);
1737 level = hidpp20_unifiedbattery_map_level(hidpp, battery_level: params[1]);
1738
1739 changed = status != hidpp->battery.status ||
1740 (state_of_charge != hidpp->battery.capacity &&
1741 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1742 (level != hidpp->battery.level &&
1743 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1744
1745 if (changed) {
1746 hidpp->battery.capacity = state_of_charge;
1747 hidpp->battery.status = status;
1748 hidpp->battery.level = level;
1749 if (hidpp->battery.ps)
1750 power_supply_changed(psy: hidpp->battery.ps);
1751 }
1752
1753 return 0;
1754}
1755
1756/* -------------------------------------------------------------------------- */
1757/* Battery feature helpers */
1758/* -------------------------------------------------------------------------- */
1759
1760static enum power_supply_property hidpp_battery_props[] = {
1761 POWER_SUPPLY_PROP_ONLINE,
1762 POWER_SUPPLY_PROP_STATUS,
1763 POWER_SUPPLY_PROP_SCOPE,
1764 POWER_SUPPLY_PROP_MODEL_NAME,
1765 POWER_SUPPLY_PROP_MANUFACTURER,
1766 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1767 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1768 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1769 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
1770};
1771
1772static int hidpp_battery_get_property(struct power_supply *psy,
1773 enum power_supply_property psp,
1774 union power_supply_propval *val)
1775{
1776 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1777 int ret = 0;
1778
1779 switch(psp) {
1780 case POWER_SUPPLY_PROP_STATUS:
1781 val->intval = hidpp->battery.status;
1782 break;
1783 case POWER_SUPPLY_PROP_CAPACITY:
1784 val->intval = hidpp->battery.capacity;
1785 break;
1786 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1787 val->intval = hidpp->battery.level;
1788 break;
1789 case POWER_SUPPLY_PROP_SCOPE:
1790 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1791 break;
1792 case POWER_SUPPLY_PROP_ONLINE:
1793 val->intval = hidpp->battery.online;
1794 break;
1795 case POWER_SUPPLY_PROP_MODEL_NAME:
1796 if (!strncmp(hidpp->name, "Logitech ", 9))
1797 val->strval = hidpp->name + 9;
1798 else
1799 val->strval = hidpp->name;
1800 break;
1801 case POWER_SUPPLY_PROP_MANUFACTURER:
1802 val->strval = "Logitech";
1803 break;
1804 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1805 val->strval = hidpp->hid_dev->uniq;
1806 break;
1807 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1808 /* hardware reports voltage in mV. sysfs expects uV */
1809 val->intval = hidpp->battery.voltage * 1000;
1810 break;
1811 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1812 val->intval = hidpp->battery.charge_type;
1813 break;
1814 default:
1815 ret = -EINVAL;
1816 break;
1817 }
1818
1819 return ret;
1820}
1821
1822/* -------------------------------------------------------------------------- */
1823/* 0x1d4b: Wireless device status */
1824/* -------------------------------------------------------------------------- */
1825#define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1826
1827static int hidpp_get_wireless_feature_index(struct hidpp_device *hidpp, u8 *feature_index)
1828{
1829 u8 feature_type;
1830 int ret;
1831
1832 ret = hidpp_root_get_feature(hidpp,
1833 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1834 feature_index, feature_type: &feature_type);
1835
1836 return ret;
1837}
1838
1839/* -------------------------------------------------------------------------- */
1840/* 0x1f20: ADC measurement */
1841/* -------------------------------------------------------------------------- */
1842
1843#define HIDPP_PAGE_ADC_MEASUREMENT 0x1f20
1844
1845#define CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT 0x00
1846
1847#define EVENT_ADC_MEASUREMENT_STATUS_BROADCAST 0x00
1848
1849static int hidpp20_map_adc_measurement_1f20_capacity(struct hid_device *hid_dev, int voltage)
1850{
1851 /* NB: This voltage curve doesn't necessarily map perfectly to all
1852 * devices that implement the ADC_MEASUREMENT feature. This is because
1853 * there are a few devices that use different battery technology.
1854 *
1855 * Adapted from:
1856 * https://github.com/Sapd/HeadsetControl/blob/acd972be0468e039b93aae81221f20a54d2d60f7/src/devices/logitech_g633_g933_935.c#L44-L52
1857 */
1858 static const int voltages[100] = {
1859 4030, 4024, 4018, 4011, 4003, 3994, 3985, 3975, 3963, 3951,
1860 3937, 3922, 3907, 3893, 3880, 3868, 3857, 3846, 3837, 3828,
1861 3820, 3812, 3805, 3798, 3791, 3785, 3779, 3773, 3768, 3762,
1862 3757, 3752, 3747, 3742, 3738, 3733, 3729, 3724, 3720, 3716,
1863 3712, 3708, 3704, 3700, 3696, 3692, 3688, 3685, 3681, 3677,
1864 3674, 3670, 3667, 3663, 3660, 3657, 3653, 3650, 3646, 3643,
1865 3640, 3637, 3633, 3630, 3627, 3624, 3620, 3617, 3614, 3611,
1866 3608, 3604, 3601, 3598, 3595, 3592, 3589, 3585, 3582, 3579,
1867 3576, 3573, 3569, 3566, 3563, 3560, 3556, 3553, 3550, 3546,
1868 3543, 3539, 3536, 3532, 3529, 3525, 3499, 3466, 3433, 3399,
1869 };
1870
1871 int i;
1872
1873 if (voltage == 0)
1874 return 0;
1875
1876 if (unlikely(voltage < 3400 || voltage >= 5000))
1877 hid_warn_once(hid_dev,
1878 "%s: possibly using the wrong voltage curve\n",
1879 __func__);
1880
1881 for (i = 0; i < ARRAY_SIZE(voltages); i++) {
1882 if (voltage >= voltages[i])
1883 return ARRAY_SIZE(voltages) - i;
1884 }
1885
1886 return 0;
1887}
1888
1889static int hidpp20_map_adc_measurement_1f20(u8 data[3], int *voltage)
1890{
1891 int status;
1892 u8 flags;
1893
1894 flags = data[2];
1895
1896 switch (flags) {
1897 case 0x01:
1898 status = POWER_SUPPLY_STATUS_DISCHARGING;
1899 break;
1900 case 0x03:
1901 status = POWER_SUPPLY_STATUS_CHARGING;
1902 break;
1903 case 0x07:
1904 status = POWER_SUPPLY_STATUS_FULL;
1905 break;
1906 case 0x0F:
1907 default:
1908 status = POWER_SUPPLY_STATUS_UNKNOWN;
1909 break;
1910 }
1911
1912 *voltage = get_unaligned_be16(p: data);
1913
1914 dbg_hid("Parsed 1f20 data as flag 0x%02x voltage %dmV\n",
1915 flags, *voltage);
1916
1917 return status;
1918}
1919
1920/* Return value is whether the device is online */
1921static bool hidpp20_get_adc_measurement_1f20(struct hidpp_device *hidpp,
1922 u8 feature_index,
1923 int *status, int *voltage)
1924{
1925 struct hidpp_report response;
1926 int ret;
1927 u8 *params = (u8 *)response.fap.params;
1928
1929 *status = POWER_SUPPLY_STATUS_UNKNOWN;
1930 *voltage = 0;
1931 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
1932 CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT,
1933 NULL, param_count: 0, response: &response);
1934
1935 if (ret > 0) {
1936 hid_dbg(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1937 __func__, ret);
1938 return false;
1939 }
1940
1941 *status = hidpp20_map_adc_measurement_1f20(data: params, voltage);
1942 return true;
1943}
1944
1945static int hidpp20_query_adc_measurement_info_1f20(struct hidpp_device *hidpp)
1946{
1947 u8 feature_type;
1948
1949 if (hidpp->battery.adc_measurement_feature_index == 0xff) {
1950 int ret;
1951
1952 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_ADC_MEASUREMENT,
1953 feature_index: &hidpp->battery.adc_measurement_feature_index,
1954 feature_type: &feature_type);
1955 if (ret)
1956 return ret;
1957
1958 hidpp->capabilities |= HIDPP_CAPABILITY_ADC_MEASUREMENT;
1959 }
1960
1961 hidpp->battery.online = hidpp20_get_adc_measurement_1f20(hidpp,
1962 feature_index: hidpp->battery.adc_measurement_feature_index,
1963 status: &hidpp->battery.status,
1964 voltage: &hidpp->battery.voltage);
1965 hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hid_dev: hidpp->hid_dev,
1966 voltage: hidpp->battery.voltage);
1967 hidpp_update_usb_wireless_status(hidpp);
1968
1969 return 0;
1970}
1971
1972static int hidpp20_adc_measurement_event_1f20(struct hidpp_device *hidpp,
1973 u8 *data, int size)
1974{
1975 struct hidpp_report *report = (struct hidpp_report *)data;
1976 int status, voltage;
1977
1978 if (report->fap.feature_index != hidpp->battery.adc_measurement_feature_index ||
1979 report->fap.funcindex_clientid != EVENT_ADC_MEASUREMENT_STATUS_BROADCAST)
1980 return 0;
1981
1982 status = hidpp20_map_adc_measurement_1f20(data: report->fap.params, voltage: &voltage);
1983
1984 hidpp->battery.online = status != POWER_SUPPLY_STATUS_UNKNOWN;
1985
1986 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1987 hidpp->battery.status = status;
1988 hidpp->battery.voltage = voltage;
1989 hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hid_dev: hidpp->hid_dev, voltage);
1990 if (hidpp->battery.ps)
1991 power_supply_changed(psy: hidpp->battery.ps);
1992 hidpp_update_usb_wireless_status(hidpp);
1993 }
1994 return 0;
1995}
1996
1997/* -------------------------------------------------------------------------- */
1998/* 0x2120: Hi-resolution scrolling */
1999/* -------------------------------------------------------------------------- */
2000
2001#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
2002
2003#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
2004
2005static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
2006 bool enabled, u8 *multiplier)
2007{
2008 u8 feature_index;
2009 u8 feature_type;
2010 int ret;
2011 u8 params[1];
2012 struct hidpp_report response;
2013
2014 ret = hidpp_root_get_feature(hidpp,
2015 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
2016 feature_index: &feature_index,
2017 feature_type: &feature_type);
2018 if (ret)
2019 return ret;
2020
2021 params[0] = enabled ? BIT(0) : 0;
2022 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
2023 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
2024 params, param_count: sizeof(params), response: &response);
2025 if (ret)
2026 return ret;
2027 *multiplier = response.fap.params[1];
2028 return 0;
2029}
2030
2031/* -------------------------------------------------------------------------- */
2032/* 0x2121: HiRes Wheel */
2033/* -------------------------------------------------------------------------- */
2034
2035#define HIDPP_PAGE_HIRES_WHEEL 0x2121
2036
2037#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
2038#define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
2039
2040static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
2041 u8 *multiplier)
2042{
2043 u8 feature_index;
2044 u8 feature_type;
2045 int ret;
2046 struct hidpp_report response;
2047
2048 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
2049 feature_index: &feature_index, feature_type: &feature_type);
2050 if (ret)
2051 goto return_default;
2052
2053 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
2054 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
2055 NULL, param_count: 0, response: &response);
2056 if (ret)
2057 goto return_default;
2058
2059 *multiplier = response.fap.params[0];
2060 return 0;
2061return_default:
2062 hid_warn(hidpp->hid_dev,
2063 "Couldn't get wheel multiplier (error %d)\n", ret);
2064 return ret;
2065}
2066
2067static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
2068 bool high_resolution, bool use_hidpp)
2069{
2070 u8 feature_index;
2071 u8 feature_type;
2072 int ret;
2073 u8 params[1];
2074 struct hidpp_report response;
2075
2076 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
2077 feature_index: &feature_index, feature_type: &feature_type);
2078 if (ret)
2079 return ret;
2080
2081 params[0] = (invert ? BIT(2) : 0) |
2082 (high_resolution ? BIT(1) : 0) |
2083 (use_hidpp ? BIT(0) : 0);
2084
2085 return hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
2086 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
2087 params, param_count: sizeof(params), response: &response);
2088}
2089
2090/* -------------------------------------------------------------------------- */
2091/* 0x4301: Solar Keyboard */
2092/* -------------------------------------------------------------------------- */
2093
2094#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
2095
2096#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
2097
2098#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
2099#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
2100#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
2101
2102static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
2103{
2104 struct hidpp_report response;
2105 u8 params[2] = { 1, 1 };
2106 u8 feature_type;
2107 int ret;
2108
2109 if (hidpp->battery.feature_index == 0xff) {
2110 ret = hidpp_root_get_feature(hidpp,
2111 HIDPP_PAGE_SOLAR_KEYBOARD,
2112 feature_index: &hidpp->battery.solar_feature_index,
2113 feature_type: &feature_type);
2114 if (ret)
2115 return ret;
2116 }
2117
2118 ret = hidpp_send_fap_command_sync(hidpp,
2119 feat_index: hidpp->battery.solar_feature_index,
2120 CMD_SOLAR_SET_LIGHT_MEASURE,
2121 params, param_count: 2, response: &response);
2122 if (ret > 0) {
2123 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2124 __func__, ret);
2125 return -EPROTO;
2126 }
2127 if (ret)
2128 return ret;
2129
2130 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2131
2132 return 0;
2133}
2134
2135static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
2136 u8 *data, int size)
2137{
2138 struct hidpp_report *report = (struct hidpp_report *)data;
2139 int capacity, lux, status;
2140 u8 function;
2141
2142 function = report->fap.funcindex_clientid;
2143
2144
2145 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
2146 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
2147 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
2148 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
2149 return 0;
2150
2151 capacity = report->fap.params[0];
2152
2153 switch (function) {
2154 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
2155 lux = (report->fap.params[1] << 8) | report->fap.params[2];
2156 if (lux > 200)
2157 status = POWER_SUPPLY_STATUS_CHARGING;
2158 else
2159 status = POWER_SUPPLY_STATUS_DISCHARGING;
2160 break;
2161 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
2162 default:
2163 if (capacity < hidpp->battery.capacity)
2164 status = POWER_SUPPLY_STATUS_DISCHARGING;
2165 else
2166 status = POWER_SUPPLY_STATUS_CHARGING;
2167
2168 }
2169
2170 if (capacity == 100)
2171 status = POWER_SUPPLY_STATUS_FULL;
2172
2173 hidpp->battery.online = true;
2174 if (capacity != hidpp->battery.capacity ||
2175 status != hidpp->battery.status) {
2176 hidpp->battery.capacity = capacity;
2177 hidpp->battery.status = status;
2178 if (hidpp->battery.ps)
2179 power_supply_changed(psy: hidpp->battery.ps);
2180 }
2181
2182 return 0;
2183}
2184
2185/* -------------------------------------------------------------------------- */
2186/* 0x6010: Touchpad FW items */
2187/* -------------------------------------------------------------------------- */
2188
2189#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
2190
2191#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
2192
2193struct hidpp_touchpad_fw_items {
2194 uint8_t presence;
2195 uint8_t desired_state;
2196 uint8_t state;
2197 uint8_t persistent;
2198};
2199
2200/*
2201 * send a set state command to the device by reading the current items->state
2202 * field. items is then filled with the current state.
2203 */
2204static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
2205 u8 feature_index,
2206 struct hidpp_touchpad_fw_items *items)
2207{
2208 struct hidpp_report response;
2209 int ret;
2210 u8 *params = (u8 *)response.fap.params;
2211
2212 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
2213 CMD_TOUCHPAD_FW_ITEMS_SET, params: &items->state, param_count: 1, response: &response);
2214
2215 if (ret > 0) {
2216 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2217 __func__, ret);
2218 return -EPROTO;
2219 }
2220 if (ret)
2221 return ret;
2222
2223 items->presence = params[0];
2224 items->desired_state = params[1];
2225 items->state = params[2];
2226 items->persistent = params[3];
2227
2228 return 0;
2229}
2230
2231/* -------------------------------------------------------------------------- */
2232/* 0x6100: TouchPadRawXY */
2233/* -------------------------------------------------------------------------- */
2234
2235#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
2236
2237#define CMD_TOUCHPAD_GET_RAW_INFO 0x00
2238#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x20
2239
2240#define EVENT_TOUCHPAD_RAW_XY 0x00
2241
2242#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
2243#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
2244
2245struct hidpp_touchpad_raw_info {
2246 u16 x_size;
2247 u16 y_size;
2248 u8 z_range;
2249 u8 area_range;
2250 u8 timestamp_unit;
2251 u8 maxcontacts;
2252 u8 origin;
2253 u16 res;
2254};
2255
2256struct hidpp_touchpad_raw_xy_finger {
2257 u8 contact_type;
2258 u8 contact_status;
2259 u16 x;
2260 u16 y;
2261 u8 z;
2262 u8 area;
2263 u8 finger_id;
2264};
2265
2266struct hidpp_touchpad_raw_xy {
2267 u16 timestamp;
2268 struct hidpp_touchpad_raw_xy_finger fingers[2];
2269 u8 spurious_flag;
2270 u8 end_of_frame;
2271 u8 finger_count;
2272 u8 button;
2273};
2274
2275static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
2276 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
2277{
2278 struct hidpp_report response;
2279 int ret;
2280 u8 *params = (u8 *)response.fap.params;
2281
2282 ret = hidpp_send_fap_command_sync(hidpp, feat_index: feature_index,
2283 CMD_TOUCHPAD_GET_RAW_INFO, NULL, param_count: 0, response: &response);
2284
2285 if (ret > 0) {
2286 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2287 __func__, ret);
2288 return -EPROTO;
2289 }
2290 if (ret)
2291 return ret;
2292
2293 raw_info->x_size = get_unaligned_be16(p: &params[0]);
2294 raw_info->y_size = get_unaligned_be16(p: &params[2]);
2295 raw_info->z_range = params[4];
2296 raw_info->area_range = params[5];
2297 raw_info->maxcontacts = params[7];
2298 raw_info->origin = params[8];
2299 /* res is given in unit per inch */
2300 raw_info->res = get_unaligned_be16(p: &params[13]) * 2 / 51;
2301
2302 return ret;
2303}
2304
2305static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
2306 u8 feature_index, bool send_raw_reports,
2307 bool sensor_enhanced_settings)
2308{
2309 struct hidpp_report response;
2310
2311 /*
2312 * Params:
2313 * bit 0 - enable raw
2314 * bit 1 - 16bit Z, no area
2315 * bit 2 - enhanced sensitivity
2316 * bit 3 - width, height (4 bits each) instead of area
2317 * bit 4 - send raw + gestures (degrades smoothness)
2318 * remaining bits - reserved
2319 */
2320 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2321
2322 return hidpp_send_fap_command_sync(hidpp: hidpp_dev, feat_index: feature_index,
2323 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, params: &params, param_count: 1, response: &response);
2324}
2325
2326static void hidpp_touchpad_touch_event(u8 *data,
2327 struct hidpp_touchpad_raw_xy_finger *finger)
2328{
2329 u8 x_m = data[0] << 2;
2330 u8 y_m = data[2] << 2;
2331
2332 finger->x = x_m << 6 | data[1];
2333 finger->y = y_m << 6 | data[3];
2334
2335 finger->contact_type = data[0] >> 6;
2336 finger->contact_status = data[2] >> 6;
2337
2338 finger->z = data[4];
2339 finger->area = data[5];
2340 finger->finger_id = data[6] >> 4;
2341}
2342
2343static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2344 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2345{
2346 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2347 raw_xy->end_of_frame = data[8] & 0x01;
2348 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2349 raw_xy->finger_count = data[15] & 0x0f;
2350 raw_xy->button = (data[8] >> 2) & 0x01;
2351
2352 if (raw_xy->finger_count) {
2353 hidpp_touchpad_touch_event(data: &data[2], finger: &raw_xy->fingers[0]);
2354 hidpp_touchpad_touch_event(data: &data[9], finger: &raw_xy->fingers[1]);
2355 }
2356}
2357
2358/* -------------------------------------------------------------------------- */
2359/* 0x8123: Force feedback support */
2360/* -------------------------------------------------------------------------- */
2361
2362#define HIDPP_FF_GET_INFO 0x01
2363#define HIDPP_FF_RESET_ALL 0x11
2364#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2365#define HIDPP_FF_SET_EFFECT_STATE 0x31
2366#define HIDPP_FF_DESTROY_EFFECT 0x41
2367#define HIDPP_FF_GET_APERTURE 0x51
2368#define HIDPP_FF_SET_APERTURE 0x61
2369#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2370#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2371
2372#define HIDPP_FF_EFFECT_STATE_GET 0x00
2373#define HIDPP_FF_EFFECT_STATE_STOP 0x01
2374#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2375#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2376
2377#define HIDPP_FF_EFFECT_CONSTANT 0x00
2378#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2379#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2380#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2381#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2382#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2383#define HIDPP_FF_EFFECT_SPRING 0x06
2384#define HIDPP_FF_EFFECT_DAMPER 0x07
2385#define HIDPP_FF_EFFECT_FRICTION 0x08
2386#define HIDPP_FF_EFFECT_INERTIA 0x09
2387#define HIDPP_FF_EFFECT_RAMP 0x0A
2388
2389#define HIDPP_FF_EFFECT_AUTOSTART 0x80
2390
2391#define HIDPP_FF_EFFECTID_NONE -1
2392#define HIDPP_FF_EFFECTID_AUTOCENTER -2
2393#define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2394
2395#define HIDPP_FF_MAX_PARAMS 20
2396#define HIDPP_FF_RESERVED_SLOTS 1
2397
2398struct hidpp_ff_private_data {
2399 struct hidpp_device *hidpp;
2400 u8 feature_index;
2401 u8 version;
2402 u16 gain;
2403 s16 range;
2404 u8 slot_autocenter;
2405 u8 num_effects;
2406 int *effect_ids;
2407 struct workqueue_struct *wq;
2408 atomic_t workqueue_size;
2409};
2410
2411struct hidpp_ff_work_data {
2412 struct work_struct work;
2413 struct hidpp_ff_private_data *data;
2414 int effect_id;
2415 u8 command;
2416 u8 params[HIDPP_FF_MAX_PARAMS];
2417 u8 size;
2418};
2419
2420static const signed short hidpp_ff_effects[] = {
2421 FF_CONSTANT,
2422 FF_PERIODIC,
2423 FF_SINE,
2424 FF_SQUARE,
2425 FF_SAW_UP,
2426 FF_SAW_DOWN,
2427 FF_TRIANGLE,
2428 FF_SPRING,
2429 FF_DAMPER,
2430 FF_AUTOCENTER,
2431 FF_GAIN,
2432 -1
2433};
2434
2435static const signed short hidpp_ff_effects_v2[] = {
2436 FF_RAMP,
2437 FF_FRICTION,
2438 FF_INERTIA,
2439 -1
2440};
2441
2442static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2443 HIDPP_FF_EFFECT_SPRING,
2444 HIDPP_FF_EFFECT_FRICTION,
2445 HIDPP_FF_EFFECT_DAMPER,
2446 HIDPP_FF_EFFECT_INERTIA
2447};
2448
2449static const char *HIDPP_FF_CONDITION_NAMES[] = {
2450 "spring",
2451 "friction",
2452 "damper",
2453 "inertia"
2454};
2455
2456
2457static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2458{
2459 int i;
2460
2461 for (i = 0; i < data->num_effects; i++)
2462 if (data->effect_ids[i] == effect_id)
2463 return i+1;
2464
2465 return 0;
2466}
2467
2468static void hidpp_ff_work_handler(struct work_struct *w)
2469{
2470 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2471 struct hidpp_ff_private_data *data = wd->data;
2472 struct hidpp_report response;
2473 u8 slot;
2474 int ret;
2475
2476 /* add slot number if needed */
2477 switch (wd->effect_id) {
2478 case HIDPP_FF_EFFECTID_AUTOCENTER:
2479 wd->params[0] = data->slot_autocenter;
2480 break;
2481 case HIDPP_FF_EFFECTID_NONE:
2482 /* leave slot as zero */
2483 break;
2484 default:
2485 /* find current slot for effect */
2486 wd->params[0] = hidpp_ff_find_effect(data, effect_id: wd->effect_id);
2487 break;
2488 }
2489
2490 /* send command and wait for reply */
2491 ret = hidpp_send_fap_command_sync(hidpp: data->hidpp, feat_index: data->feature_index,
2492 funcindex_clientid: wd->command, params: wd->params, param_count: wd->size, response: &response);
2493
2494 if (ret) {
2495 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2496 goto out;
2497 }
2498
2499 /* parse return data */
2500 switch (wd->command) {
2501 case HIDPP_FF_DOWNLOAD_EFFECT:
2502 slot = response.fap.params[0];
2503 if (slot > 0 && slot <= data->num_effects) {
2504 if (wd->effect_id >= 0)
2505 /* regular effect uploaded */
2506 data->effect_ids[slot-1] = wd->effect_id;
2507 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2508 /* autocenter spring uploaded */
2509 data->slot_autocenter = slot;
2510 }
2511 break;
2512 case HIDPP_FF_DESTROY_EFFECT:
2513 if (wd->effect_id >= 0)
2514 /* regular effect destroyed */
2515 data->effect_ids[wd->params[0]-1] = -1;
2516 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2517 /* autocenter spring destoyed */
2518 data->slot_autocenter = 0;
2519 break;
2520 case HIDPP_FF_SET_GLOBAL_GAINS:
2521 data->gain = (wd->params[0] << 8) + wd->params[1];
2522 break;
2523 case HIDPP_FF_SET_APERTURE:
2524 data->range = (wd->params[0] << 8) + wd->params[1];
2525 break;
2526 default:
2527 /* no action needed */
2528 break;
2529 }
2530
2531out:
2532 atomic_dec(v: &data->workqueue_size);
2533 kfree(objp: wd);
2534}
2535
2536static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2537{
2538 struct hidpp_ff_work_data *wd = kzalloc(size: sizeof(*wd), GFP_KERNEL);
2539 int s;
2540
2541 if (!wd)
2542 return -ENOMEM;
2543
2544 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2545
2546 wd->data = data;
2547 wd->effect_id = effect_id;
2548 wd->command = command;
2549 wd->size = size;
2550 memcpy(wd->params, params, size);
2551
2552 s = atomic_inc_return(v: &data->workqueue_size);
2553 queue_work(wq: data->wq, work: &wd->work);
2554
2555 /* warn about excessive queue size */
2556 if (s >= 20 && s % 20 == 0)
2557 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2558
2559 return 0;
2560}
2561
2562static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2563{
2564 struct hidpp_ff_private_data *data = dev->ff->private;
2565 u8 params[20];
2566 u8 size;
2567 int force;
2568
2569 /* set common parameters */
2570 params[2] = effect->replay.length >> 8;
2571 params[3] = effect->replay.length & 255;
2572 params[4] = effect->replay.delay >> 8;
2573 params[5] = effect->replay.delay & 255;
2574
2575 switch (effect->type) {
2576 case FF_CONSTANT:
2577 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2578 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2579 params[6] = force >> 8;
2580 params[7] = force & 255;
2581 params[8] = effect->u.constant.envelope.attack_level >> 7;
2582 params[9] = effect->u.constant.envelope.attack_length >> 8;
2583 params[10] = effect->u.constant.envelope.attack_length & 255;
2584 params[11] = effect->u.constant.envelope.fade_level >> 7;
2585 params[12] = effect->u.constant.envelope.fade_length >> 8;
2586 params[13] = effect->u.constant.envelope.fade_length & 255;
2587 size = 14;
2588 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2589 effect->u.constant.level,
2590 effect->direction, force);
2591 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2592 effect->u.constant.envelope.attack_level,
2593 effect->u.constant.envelope.attack_length,
2594 effect->u.constant.envelope.fade_level,
2595 effect->u.constant.envelope.fade_length);
2596 break;
2597 case FF_PERIODIC:
2598 {
2599 switch (effect->u.periodic.waveform) {
2600 case FF_SINE:
2601 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2602 break;
2603 case FF_SQUARE:
2604 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2605 break;
2606 case FF_SAW_UP:
2607 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2608 break;
2609 case FF_SAW_DOWN:
2610 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2611 break;
2612 case FF_TRIANGLE:
2613 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2614 break;
2615 default:
2616 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2617 return -EINVAL;
2618 }
2619 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2620 params[6] = effect->u.periodic.magnitude >> 8;
2621 params[7] = effect->u.periodic.magnitude & 255;
2622 params[8] = effect->u.periodic.offset >> 8;
2623 params[9] = effect->u.periodic.offset & 255;
2624 params[10] = effect->u.periodic.period >> 8;
2625 params[11] = effect->u.periodic.period & 255;
2626 params[12] = effect->u.periodic.phase >> 8;
2627 params[13] = effect->u.periodic.phase & 255;
2628 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2629 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2630 params[16] = effect->u.periodic.envelope.attack_length & 255;
2631 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2632 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2633 params[19] = effect->u.periodic.envelope.fade_length & 255;
2634 size = 20;
2635 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2636 effect->u.periodic.magnitude, effect->direction,
2637 effect->u.periodic.offset,
2638 effect->u.periodic.period,
2639 effect->u.periodic.phase);
2640 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2641 effect->u.periodic.envelope.attack_level,
2642 effect->u.periodic.envelope.attack_length,
2643 effect->u.periodic.envelope.fade_level,
2644 effect->u.periodic.envelope.fade_length);
2645 break;
2646 }
2647 case FF_RAMP:
2648 params[1] = HIDPP_FF_EFFECT_RAMP;
2649 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2650 params[6] = force >> 8;
2651 params[7] = force & 255;
2652 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2653 params[8] = force >> 8;
2654 params[9] = force & 255;
2655 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2656 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2657 params[12] = effect->u.ramp.envelope.attack_length & 255;
2658 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2659 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2660 params[15] = effect->u.ramp.envelope.fade_length & 255;
2661 size = 16;
2662 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2663 effect->u.ramp.start_level,
2664 effect->u.ramp.end_level,
2665 effect->direction, force);
2666 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2667 effect->u.ramp.envelope.attack_level,
2668 effect->u.ramp.envelope.attack_length,
2669 effect->u.ramp.envelope.fade_level,
2670 effect->u.ramp.envelope.fade_length);
2671 break;
2672 case FF_FRICTION:
2673 case FF_INERTIA:
2674 case FF_SPRING:
2675 case FF_DAMPER:
2676 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2677 params[6] = effect->u.condition[0].left_saturation >> 9;
2678 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2679 params[8] = effect->u.condition[0].left_coeff >> 8;
2680 params[9] = effect->u.condition[0].left_coeff & 255;
2681 params[10] = effect->u.condition[0].deadband >> 9;
2682 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2683 params[12] = effect->u.condition[0].center >> 8;
2684 params[13] = effect->u.condition[0].center & 255;
2685 params[14] = effect->u.condition[0].right_coeff >> 8;
2686 params[15] = effect->u.condition[0].right_coeff & 255;
2687 params[16] = effect->u.condition[0].right_saturation >> 9;
2688 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2689 size = 18;
2690 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2691 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2692 effect->u.condition[0].left_coeff,
2693 effect->u.condition[0].left_saturation,
2694 effect->u.condition[0].right_coeff,
2695 effect->u.condition[0].right_saturation);
2696 dbg_hid(" deadband=%d, center=%d\n",
2697 effect->u.condition[0].deadband,
2698 effect->u.condition[0].center);
2699 break;
2700 default:
2701 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2702 return -EINVAL;
2703 }
2704
2705 return hidpp_ff_queue_work(data, effect_id: effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2706}
2707
2708static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2709{
2710 struct hidpp_ff_private_data *data = dev->ff->private;
2711 u8 params[2];
2712
2713 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2714
2715 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2716
2717 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2718}
2719
2720static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2721{
2722 struct hidpp_ff_private_data *data = dev->ff->private;
2723 u8 slot = 0;
2724
2725 dbg_hid("Erasing effect %d.\n", effect_id);
2726
2727 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, params: &slot, size: 1);
2728}
2729
2730static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2731{
2732 struct hidpp_ff_private_data *data = dev->ff->private;
2733 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2734
2735 dbg_hid("Setting autocenter to %d.\n", magnitude);
2736
2737 /* start a standard spring effect */
2738 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2739 /* zero delay and duration */
2740 params[2] = params[3] = params[4] = params[5] = 0;
2741 /* set coeff to 25% of saturation */
2742 params[8] = params[14] = magnitude >> 11;
2743 params[9] = params[15] = (magnitude >> 3) & 255;
2744 params[6] = params[16] = magnitude >> 9;
2745 params[7] = params[17] = (magnitude >> 1) & 255;
2746 /* zero deadband and center */
2747 params[10] = params[11] = params[12] = params[13] = 0;
2748
2749 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2750}
2751
2752static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2753{
2754 struct hidpp_ff_private_data *data = dev->ff->private;
2755 u8 params[4];
2756
2757 dbg_hid("Setting gain to %d.\n", gain);
2758
2759 params[0] = gain >> 8;
2760 params[1] = gain & 255;
2761 params[2] = 0; /* no boost */
2762 params[3] = 0;
2763
2764 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2765}
2766
2767static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2768{
2769 struct hid_device *hid = to_hid_device(dev);
2770 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2771 struct input_dev *idev = hidinput->input;
2772 struct hidpp_ff_private_data *data = idev->ff->private;
2773
2774 return scnprintf(buf, PAGE_SIZE, fmt: "%u\n", data->range);
2775}
2776
2777static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2778{
2779 struct hid_device *hid = to_hid_device(dev);
2780 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2781 struct input_dev *idev = hidinput->input;
2782 struct hidpp_ff_private_data *data = idev->ff->private;
2783 u8 params[2];
2784 int range = simple_strtoul(buf, NULL, 10);
2785
2786 range = clamp(range, 180, 900);
2787
2788 params[0] = range >> 8;
2789 params[1] = range & 0x00FF;
2790
2791 hidpp_ff_queue_work(data, effect_id: -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2792
2793 return count;
2794}
2795
2796static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2797
2798static void hidpp_ff_destroy(struct ff_device *ff)
2799{
2800 struct hidpp_ff_private_data *data = ff->private;
2801 struct hid_device *hid = data->hidpp->hid_dev;
2802
2803 hid_info(hid, "Unloading HID++ force feedback.\n");
2804
2805 device_remove_file(dev: &hid->dev, attr: &dev_attr_range);
2806 destroy_workqueue(wq: data->wq);
2807 kfree(objp: data->effect_ids);
2808}
2809
2810static int hidpp_ff_init(struct hidpp_device *hidpp,
2811 struct hidpp_ff_private_data *data)
2812{
2813 struct hid_device *hid = hidpp->hid_dev;
2814 struct hid_input *hidinput;
2815 struct input_dev *dev;
2816 struct usb_device_descriptor *udesc;
2817 u16 bcdDevice;
2818 struct ff_device *ff;
2819 int error, j, num_slots = data->num_effects;
2820 u8 version;
2821
2822 if (!hid_is_usb(hdev: hid)) {
2823 hid_err(hid, "device is not USB\n");
2824 return -ENODEV;
2825 }
2826
2827 if (list_empty(head: &hid->inputs)) {
2828 hid_err(hid, "no inputs found\n");
2829 return -ENODEV;
2830 }
2831 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2832 dev = hidinput->input;
2833
2834 if (!dev) {
2835 hid_err(hid, "Struct input_dev not set!\n");
2836 return -EINVAL;
2837 }
2838
2839 /* Get firmware release */
2840 udesc = &(hid_to_usb_dev(hid)->descriptor);
2841 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2842 version = bcdDevice & 255;
2843
2844 /* Set supported force feedback capabilities */
2845 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2846 set_bit(nr: hidpp_ff_effects[j], addr: dev->ffbit);
2847 if (version > 1)
2848 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2849 set_bit(nr: hidpp_ff_effects_v2[j], addr: dev->ffbit);
2850
2851 error = input_ff_create(dev, max_effects: num_slots);
2852
2853 if (error) {
2854 hid_err(dev, "Failed to create FF device!\n");
2855 return error;
2856 }
2857 /*
2858 * Create a copy of passed data, so we can transfer memory
2859 * ownership to FF core
2860 */
2861 data = kmemdup(p: data, size: sizeof(*data), GFP_KERNEL);
2862 if (!data)
2863 return -ENOMEM;
2864 data->effect_ids = kcalloc(n: num_slots, size: sizeof(int), GFP_KERNEL);
2865 if (!data->effect_ids) {
2866 kfree(objp: data);
2867 return -ENOMEM;
2868 }
2869 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2870 if (!data->wq) {
2871 kfree(objp: data->effect_ids);
2872 kfree(objp: data);
2873 return -ENOMEM;
2874 }
2875
2876 data->hidpp = hidpp;
2877 data->version = version;
2878 for (j = 0; j < num_slots; j++)
2879 data->effect_ids[j] = -1;
2880
2881 ff = dev->ff;
2882 ff->private = data;
2883
2884 ff->upload = hidpp_ff_upload_effect;
2885 ff->erase = hidpp_ff_erase_effect;
2886 ff->playback = hidpp_ff_playback;
2887 ff->set_gain = hidpp_ff_set_gain;
2888 ff->set_autocenter = hidpp_ff_set_autocenter;
2889 ff->destroy = hidpp_ff_destroy;
2890
2891 /* Create sysfs interface */
2892 error = device_create_file(device: &(hidpp->hid_dev->dev), entry: &dev_attr_range);
2893 if (error)
2894 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2895
2896 /* init the hardware command queue */
2897 atomic_set(v: &data->workqueue_size, i: 0);
2898
2899 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2900 version);
2901
2902 return 0;
2903}
2904
2905/* ************************************************************************** */
2906/* */
2907/* Device Support */
2908/* */
2909/* ************************************************************************** */
2910
2911/* -------------------------------------------------------------------------- */
2912/* Touchpad HID++ devices */
2913/* -------------------------------------------------------------------------- */
2914
2915#define WTP_MANUAL_RESOLUTION 39
2916
2917struct wtp_data {
2918 u16 x_size, y_size;
2919 u8 finger_count;
2920 u8 mt_feature_index;
2921 u8 button_feature_index;
2922 u8 maxcontacts;
2923 bool flip_y;
2924 unsigned int resolution;
2925};
2926
2927static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2928 struct hid_field *field, struct hid_usage *usage,
2929 unsigned long **bit, int *max)
2930{
2931 return -1;
2932}
2933
2934static void wtp_populate_input(struct hidpp_device *hidpp,
2935 struct input_dev *input_dev)
2936{
2937 struct wtp_data *wd = hidpp->private_data;
2938
2939 __set_bit(EV_ABS, input_dev->evbit);
2940 __set_bit(EV_KEY, input_dev->evbit);
2941 __clear_bit(EV_REL, input_dev->evbit);
2942 __clear_bit(EV_LED, input_dev->evbit);
2943
2944 input_set_abs_params(dev: input_dev, ABS_MT_POSITION_X, min: 0, max: wd->x_size, fuzz: 0, flat: 0);
2945 input_abs_set_res(dev: input_dev, ABS_MT_POSITION_X, val: wd->resolution);
2946 input_set_abs_params(dev: input_dev, ABS_MT_POSITION_Y, min: 0, max: wd->y_size, fuzz: 0, flat: 0);
2947 input_abs_set_res(dev: input_dev, ABS_MT_POSITION_Y, val: wd->resolution);
2948
2949 /* Max pressure is not given by the devices, pick one */
2950 input_set_abs_params(dev: input_dev, ABS_MT_PRESSURE, min: 0, max: 50, fuzz: 0, flat: 0);
2951
2952 input_set_capability(dev: input_dev, EV_KEY, BTN_LEFT);
2953
2954 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2955 input_set_capability(dev: input_dev, EV_KEY, BTN_RIGHT);
2956 else
2957 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2958
2959 input_mt_init_slots(dev: input_dev, num_slots: wd->maxcontacts, INPUT_MT_POINTER |
2960 INPUT_MT_DROP_UNUSED);
2961}
2962
2963static void wtp_touch_event(struct hidpp_device *hidpp,
2964 struct hidpp_touchpad_raw_xy_finger *touch_report)
2965{
2966 struct wtp_data *wd = hidpp->private_data;
2967 int slot;
2968
2969 if (!touch_report->finger_id || touch_report->contact_type)
2970 /* no actual data */
2971 return;
2972
2973 slot = input_mt_get_slot_by_key(dev: hidpp->input, key: touch_report->finger_id);
2974
2975 input_mt_slot(dev: hidpp->input, slot);
2976 input_mt_report_slot_state(dev: hidpp->input, MT_TOOL_FINGER,
2977 active: touch_report->contact_status);
2978 if (touch_report->contact_status) {
2979 input_event(dev: hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2980 value: touch_report->x);
2981 input_event(dev: hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2982 value: wd->flip_y ? wd->y_size - touch_report->y :
2983 touch_report->y);
2984 input_event(dev: hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2985 value: touch_report->area);
2986 }
2987}
2988
2989static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2990 struct hidpp_touchpad_raw_xy *raw)
2991{
2992 int i;
2993
2994 for (i = 0; i < 2; i++)
2995 wtp_touch_event(hidpp, touch_report: &(raw->fingers[i]));
2996
2997 if (raw->end_of_frame &&
2998 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2999 input_event(dev: hidpp->input, EV_KEY, BTN_LEFT, value: raw->button);
3000
3001 if (raw->end_of_frame || raw->finger_count <= 2) {
3002 input_mt_sync_frame(dev: hidpp->input);
3003 input_sync(dev: hidpp->input);
3004 }
3005}
3006
3007static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
3008{
3009 struct wtp_data *wd = hidpp->private_data;
3010 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
3011 (data[7] >> 4) * (data[7] >> 4)) / 2;
3012 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
3013 (data[13] >> 4) * (data[13] >> 4)) / 2;
3014 struct hidpp_touchpad_raw_xy raw = {
3015 .timestamp = data[1],
3016 .fingers = {
3017 {
3018 .contact_type = 0,
3019 .contact_status = !!data[7],
3020 .x = get_unaligned_le16(p: &data[3]),
3021 .y = get_unaligned_le16(p: &data[5]),
3022 .z = c1_area,
3023 .area = c1_area,
3024 .finger_id = data[2],
3025 }, {
3026 .contact_type = 0,
3027 .contact_status = !!data[13],
3028 .x = get_unaligned_le16(p: &data[9]),
3029 .y = get_unaligned_le16(p: &data[11]),
3030 .z = c2_area,
3031 .area = c2_area,
3032 .finger_id = data[8],
3033 }
3034 },
3035 .finger_count = wd->maxcontacts,
3036 .spurious_flag = 0,
3037 .end_of_frame = (data[0] >> 7) == 0,
3038 .button = data[0] & 0x01,
3039 };
3040
3041 wtp_send_raw_xy_event(hidpp, raw: &raw);
3042
3043 return 1;
3044}
3045
3046static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
3047{
3048 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3049 struct wtp_data *wd = hidpp->private_data;
3050 struct hidpp_report *report = (struct hidpp_report *)data;
3051 struct hidpp_touchpad_raw_xy raw;
3052
3053 if (!wd || !hidpp->input)
3054 return 1;
3055
3056 switch (data[0]) {
3057 case 0x02:
3058 if (size < 2) {
3059 hid_err(hdev, "Received HID report of bad size (%d)",
3060 size);
3061 return 1;
3062 }
3063 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
3064 input_event(dev: hidpp->input, EV_KEY, BTN_LEFT,
3065 value: !!(data[1] & 0x01));
3066 input_event(dev: hidpp->input, EV_KEY, BTN_RIGHT,
3067 value: !!(data[1] & 0x02));
3068 input_sync(dev: hidpp->input);
3069 return 0;
3070 } else {
3071 if (size < 21)
3072 return 1;
3073 return wtp_mouse_raw_xy_event(hidpp, data: &data[7]);
3074 }
3075 case REPORT_ID_HIDPP_LONG:
3076 /* size is already checked in hidpp_raw_event. */
3077 if ((report->fap.feature_index != wd->mt_feature_index) ||
3078 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
3079 return 1;
3080 hidpp_touchpad_raw_xy_event(hidpp_dev: hidpp, data: data + 4, raw_xy: &raw);
3081
3082 wtp_send_raw_xy_event(hidpp, raw: &raw);
3083 return 0;
3084 }
3085
3086 return 0;
3087}
3088
3089static int wtp_get_config(struct hidpp_device *hidpp)
3090{
3091 struct wtp_data *wd = hidpp->private_data;
3092 struct hidpp_touchpad_raw_info raw_info = {0};
3093 u8 feature_type;
3094 int ret;
3095
3096 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
3097 feature_index: &wd->mt_feature_index, feature_type: &feature_type);
3098 if (ret)
3099 /* means that the device is not powered up */
3100 return ret;
3101
3102 ret = hidpp_touchpad_get_raw_info(hidpp, feature_index: wd->mt_feature_index,
3103 raw_info: &raw_info);
3104 if (ret)
3105 return ret;
3106
3107 wd->x_size = raw_info.x_size;
3108 wd->y_size = raw_info.y_size;
3109 wd->maxcontacts = raw_info.maxcontacts;
3110 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
3111 wd->resolution = raw_info.res;
3112 if (!wd->resolution)
3113 wd->resolution = WTP_MANUAL_RESOLUTION;
3114
3115 return 0;
3116}
3117
3118static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
3119{
3120 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3121 struct wtp_data *wd;
3122
3123 wd = devm_kzalloc(dev: &hdev->dev, size: sizeof(struct wtp_data),
3124 GFP_KERNEL);
3125 if (!wd)
3126 return -ENOMEM;
3127
3128 hidpp->private_data = wd;
3129
3130 return 0;
3131};
3132
3133static int wtp_connect(struct hid_device *hdev)
3134{
3135 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3136 struct wtp_data *wd = hidpp->private_data;
3137 int ret;
3138
3139 if (!wd->x_size) {
3140 ret = wtp_get_config(hidpp);
3141 if (ret) {
3142 hid_err(hdev, "Can not get wtp config: %d\n", ret);
3143 return ret;
3144 }
3145 }
3146
3147 return hidpp_touchpad_set_raw_report_state(hidpp_dev: hidpp, feature_index: wd->mt_feature_index,
3148 send_raw_reports: true, sensor_enhanced_settings: true);
3149}
3150
3151/* ------------------------------------------------------------------------- */
3152/* Logitech M560 devices */
3153/* ------------------------------------------------------------------------- */
3154
3155/*
3156 * Logitech M560 protocol overview
3157 *
3158 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
3159 * the sides buttons are pressed, it sends some keyboard keys events
3160 * instead of buttons ones.
3161 * To complicate things further, the middle button keys sequence
3162 * is different from the odd press and the even press.
3163 *
3164 * forward button -> Super_R
3165 * backward button -> Super_L+'d' (press only)
3166 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
3167 * 2nd time: left-click (press only)
3168 * NB: press-only means that when the button is pressed, the
3169 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
3170 * together sequentially; instead when the button is released, no event is
3171 * generated !
3172 *
3173 * With the command
3174 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
3175 * the mouse reacts differently:
3176 * - it never sends a keyboard key event
3177 * - for the three mouse button it sends:
3178 * middle button press 11<xx>0a 3500af00...
3179 * side 1 button (forward) press 11<xx>0a 3500b000...
3180 * side 2 button (backward) press 11<xx>0a 3500ae00...
3181 * middle/side1/side2 button release 11<xx>0a 35000000...
3182 */
3183
3184static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
3185
3186/* how buttons are mapped in the report */
3187#define M560_MOUSE_BTN_LEFT 0x01
3188#define M560_MOUSE_BTN_RIGHT 0x02
3189#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
3190#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
3191
3192#define M560_SUB_ID 0x0a
3193#define M560_BUTTON_MODE_REGISTER 0x35
3194
3195static int m560_send_config_command(struct hid_device *hdev)
3196{
3197 struct hidpp_report response;
3198 struct hidpp_device *hidpp_dev;
3199
3200 hidpp_dev = hid_get_drvdata(hdev);
3201
3202 return hidpp_send_rap_command_sync(
3203 hidpp_dev,
3204 REPORT_ID_HIDPP_SHORT,
3205 M560_SUB_ID,
3206 M560_BUTTON_MODE_REGISTER,
3207 params: (u8 *)m560_config_parameter,
3208 param_count: sizeof(m560_config_parameter),
3209 response: &response
3210 );
3211}
3212
3213static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
3214{
3215 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3216
3217 /* sanity check */
3218 if (!hidpp->input) {
3219 hid_err(hdev, "error in parameter\n");
3220 return -EINVAL;
3221 }
3222
3223 if (size < 7) {
3224 hid_err(hdev, "error in report\n");
3225 return 0;
3226 }
3227
3228 if (data[0] == REPORT_ID_HIDPP_LONG &&
3229 data[2] == M560_SUB_ID && data[6] == 0x00) {
3230 /*
3231 * m560 mouse report for middle, forward and backward button
3232 *
3233 * data[0] = 0x11
3234 * data[1] = device-id
3235 * data[2] = 0x0a
3236 * data[5] = 0xaf -> middle
3237 * 0xb0 -> forward
3238 * 0xae -> backward
3239 * 0x00 -> release all
3240 * data[6] = 0x00
3241 */
3242
3243 switch (data[5]) {
3244 case 0xaf:
3245 input_report_key(dev: hidpp->input, BTN_MIDDLE, value: 1);
3246 break;
3247 case 0xb0:
3248 input_report_key(dev: hidpp->input, BTN_FORWARD, value: 1);
3249 break;
3250 case 0xae:
3251 input_report_key(dev: hidpp->input, BTN_BACK, value: 1);
3252 break;
3253 case 0x00:
3254 input_report_key(dev: hidpp->input, BTN_BACK, value: 0);
3255 input_report_key(dev: hidpp->input, BTN_FORWARD, value: 0);
3256 input_report_key(dev: hidpp->input, BTN_MIDDLE, value: 0);
3257 break;
3258 default:
3259 hid_err(hdev, "error in report\n");
3260 return 0;
3261 }
3262 input_sync(dev: hidpp->input);
3263
3264 } else if (data[0] == 0x02) {
3265 /*
3266 * Logitech M560 mouse report
3267 *
3268 * data[0] = type (0x02)
3269 * data[1..2] = buttons
3270 * data[3..5] = xy
3271 * data[6] = wheel
3272 */
3273
3274 int v;
3275
3276 input_report_key(dev: hidpp->input, BTN_LEFT,
3277 value: !!(data[1] & M560_MOUSE_BTN_LEFT));
3278 input_report_key(dev: hidpp->input, BTN_RIGHT,
3279 value: !!(data[1] & M560_MOUSE_BTN_RIGHT));
3280
3281 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3282 input_report_rel(dev: hidpp->input, REL_HWHEEL, value: -1);
3283 input_report_rel(dev: hidpp->input, REL_HWHEEL_HI_RES,
3284 value: -120);
3285 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3286 input_report_rel(dev: hidpp->input, REL_HWHEEL, value: 1);
3287 input_report_rel(dev: hidpp->input, REL_HWHEEL_HI_RES,
3288 value: 120);
3289 }
3290
3291 v = hid_snto32(value: hid_field_extract(hid: hdev, report: data+3, offset: 0, n: 12), n: 12);
3292 input_report_rel(dev: hidpp->input, REL_X, value: v);
3293
3294 v = hid_snto32(value: hid_field_extract(hid: hdev, report: data+3, offset: 12, n: 12), n: 12);
3295 input_report_rel(dev: hidpp->input, REL_Y, value: v);
3296
3297 v = hid_snto32(value: data[6], n: 8);
3298 if (v != 0)
3299 hidpp_scroll_counter_handle_scroll(input_dev: hidpp->input,
3300 counter: &hidpp->vertical_wheel_counter, hi_res_value: v);
3301
3302 input_sync(dev: hidpp->input);
3303 }
3304
3305 return 1;
3306}
3307
3308static void m560_populate_input(struct hidpp_device *hidpp,
3309 struct input_dev *input_dev)
3310{
3311 __set_bit(EV_KEY, input_dev->evbit);
3312 __set_bit(BTN_MIDDLE, input_dev->keybit);
3313 __set_bit(BTN_RIGHT, input_dev->keybit);
3314 __set_bit(BTN_LEFT, input_dev->keybit);
3315 __set_bit(BTN_BACK, input_dev->keybit);
3316 __set_bit(BTN_FORWARD, input_dev->keybit);
3317
3318 __set_bit(EV_REL, input_dev->evbit);
3319 __set_bit(REL_X, input_dev->relbit);
3320 __set_bit(REL_Y, input_dev->relbit);
3321 __set_bit(REL_WHEEL, input_dev->relbit);
3322 __set_bit(REL_HWHEEL, input_dev->relbit);
3323 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3324 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3325}
3326
3327static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3328 struct hid_field *field, struct hid_usage *usage,
3329 unsigned long **bit, int *max)
3330{
3331 return -1;
3332}
3333
3334/* ------------------------------------------------------------------------- */
3335/* Logitech K400 devices */
3336/* ------------------------------------------------------------------------- */
3337
3338/*
3339 * The Logitech K400 keyboard has an embedded touchpad which is seen
3340 * as a mouse from the OS point of view. There is a hardware shortcut to disable
3341 * tap-to-click but the setting is not remembered accross reset, annoying some
3342 * users.
3343 *
3344 * We can toggle this feature from the host by using the feature 0x6010:
3345 * Touchpad FW items
3346 */
3347
3348struct k400_private_data {
3349 u8 feature_index;
3350};
3351
3352static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3353{
3354 struct k400_private_data *k400 = hidpp->private_data;
3355 struct hidpp_touchpad_fw_items items = {};
3356 int ret;
3357 u8 feature_type;
3358
3359 if (!k400->feature_index) {
3360 ret = hidpp_root_get_feature(hidpp,
3361 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3362 feature_index: &k400->feature_index, feature_type: &feature_type);
3363 if (ret)
3364 /* means that the device is not powered up */
3365 return ret;
3366 }
3367
3368 ret = hidpp_touchpad_fw_items_set(hidpp, feature_index: k400->feature_index, items: &items);
3369 if (ret)
3370 return ret;
3371
3372 return 0;
3373}
3374
3375static int k400_allocate(struct hid_device *hdev)
3376{
3377 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3378 struct k400_private_data *k400;
3379
3380 k400 = devm_kzalloc(dev: &hdev->dev, size: sizeof(struct k400_private_data),
3381 GFP_KERNEL);
3382 if (!k400)
3383 return -ENOMEM;
3384
3385 hidpp->private_data = k400;
3386
3387 return 0;
3388};
3389
3390static int k400_connect(struct hid_device *hdev)
3391{
3392 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3393
3394 if (!disable_tap_to_click)
3395 return 0;
3396
3397 return k400_disable_tap_to_click(hidpp);
3398}
3399
3400/* ------------------------------------------------------------------------- */
3401/* Logitech G920 Driving Force Racing Wheel for Xbox One */
3402/* ------------------------------------------------------------------------- */
3403
3404#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3405
3406static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3407 struct hidpp_ff_private_data *data)
3408{
3409 struct hidpp_report response;
3410 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3411 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3412 };
3413 int ret;
3414
3415 /* initialize with zero autocenter to get wheel in usable state */
3416
3417 dbg_hid("Setting autocenter to 0.\n");
3418 ret = hidpp_send_fap_command_sync(hidpp, feat_index: data->feature_index,
3419 HIDPP_FF_DOWNLOAD_EFFECT,
3420 params, ARRAY_SIZE(params),
3421 response: &response);
3422 if (ret)
3423 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3424 else
3425 data->slot_autocenter = response.fap.params[0];
3426
3427 return ret;
3428}
3429
3430static int g920_get_config(struct hidpp_device *hidpp,
3431 struct hidpp_ff_private_data *data)
3432{
3433 struct hidpp_report response;
3434 u8 feature_type;
3435 int ret;
3436
3437 memset(data, 0, sizeof(*data));
3438
3439 /* Find feature and store for later use */
3440 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3441 feature_index: &data->feature_index, feature_type: &feature_type);
3442 if (ret)
3443 return ret;
3444
3445 /* Read number of slots available in device */
3446 ret = hidpp_send_fap_command_sync(hidpp, feat_index: data->feature_index,
3447 HIDPP_FF_GET_INFO,
3448 NULL, param_count: 0,
3449 response: &response);
3450 if (ret) {
3451 if (ret < 0)
3452 return ret;
3453 hid_err(hidpp->hid_dev,
3454 "%s: received protocol error 0x%02x\n", __func__, ret);
3455 return -EPROTO;
3456 }
3457
3458 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3459
3460 /* reset all forces */
3461 ret = hidpp_send_fap_command_sync(hidpp, feat_index: data->feature_index,
3462 HIDPP_FF_RESET_ALL,
3463 NULL, param_count: 0,
3464 response: &response);
3465 if (ret)
3466 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3467
3468 ret = hidpp_send_fap_command_sync(hidpp, feat_index: data->feature_index,
3469 HIDPP_FF_GET_APERTURE,
3470 NULL, param_count: 0,
3471 response: &response);
3472 if (ret) {
3473 hid_warn(hidpp->hid_dev,
3474 "Failed to read range from device!\n");
3475 }
3476 data->range = ret ?
3477 900 : get_unaligned_be16(p: &response.fap.params[0]);
3478
3479 /* Read the current gain values */
3480 ret = hidpp_send_fap_command_sync(hidpp, feat_index: data->feature_index,
3481 HIDPP_FF_GET_GLOBAL_GAINS,
3482 NULL, param_count: 0,
3483 response: &response);
3484 if (ret)
3485 hid_warn(hidpp->hid_dev,
3486 "Failed to read gain values from device!\n");
3487 data->gain = ret ?
3488 0xffff : get_unaligned_be16(p: &response.fap.params[0]);
3489
3490 /* ignore boost value at response.fap.params[2] */
3491
3492 return g920_ff_set_autocenter(hidpp, data);
3493}
3494
3495/* -------------------------------------------------------------------------- */
3496/* Logitech Dinovo Mini keyboard with builtin touchpad */
3497/* -------------------------------------------------------------------------- */
3498#define DINOVO_MINI_PRODUCT_ID 0xb30c
3499
3500static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3501 struct hid_field *field, struct hid_usage *usage,
3502 unsigned long **bit, int *max)
3503{
3504 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3505 return 0;
3506
3507 switch (usage->hid & HID_USAGE) {
3508 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3509 default:
3510 return 0;
3511 }
3512 return 1;
3513}
3514
3515/* -------------------------------------------------------------------------- */
3516/* HID++1.0 devices which use HID++ reports for their wheels */
3517/* -------------------------------------------------------------------------- */
3518static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3519{
3520 return hidpp10_set_register(hidpp_dev: hidpp, HIDPP_REG_ENABLE_REPORTS, byte: 0,
3521 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3522 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3523}
3524
3525static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3526 u8 *data, int size)
3527{
3528 s8 value, hvalue;
3529
3530 if (!hidpp->input)
3531 return -EINVAL;
3532
3533 if (size < 7)
3534 return 0;
3535
3536 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3537 return 0;
3538
3539 value = data[3];
3540 hvalue = data[4];
3541
3542 input_report_rel(dev: hidpp->input, REL_WHEEL, value);
3543 input_report_rel(dev: hidpp->input, REL_WHEEL_HI_RES, value: value * 120);
3544 input_report_rel(dev: hidpp->input, REL_HWHEEL, value: hvalue);
3545 input_report_rel(dev: hidpp->input, REL_HWHEEL_HI_RES, value: hvalue * 120);
3546 input_sync(dev: hidpp->input);
3547
3548 return 1;
3549}
3550
3551static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3552 struct input_dev *input_dev)
3553{
3554 __set_bit(EV_REL, input_dev->evbit);
3555 __set_bit(REL_WHEEL, input_dev->relbit);
3556 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3557 __set_bit(REL_HWHEEL, input_dev->relbit);
3558 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3559}
3560
3561/* -------------------------------------------------------------------------- */
3562/* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3563/* -------------------------------------------------------------------------- */
3564static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3565{
3566 return hidpp10_set_register(hidpp_dev: hidpp, HIDPP_REG_ENABLE_REPORTS, byte: 0,
3567 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3568 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3569}
3570
3571static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3572 u8 *data, int size)
3573{
3574 int i;
3575
3576 if (!hidpp->input)
3577 return -EINVAL;
3578
3579 if (size < 7)
3580 return 0;
3581
3582 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3583 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3584 return 0;
3585
3586 /*
3587 * Buttons are either delivered through the regular mouse report *or*
3588 * through the extra buttons report. At least for button 6 how it is
3589 * delivered differs per receiver firmware version. Even receivers with
3590 * the same usb-id show different behavior, so we handle both cases.
3591 */
3592 for (i = 0; i < 8; i++)
3593 input_report_key(dev: hidpp->input, BTN_MOUSE + i,
3594 value: (data[3] & (1 << i)));
3595
3596 /* Some mice report events on button 9+, use BTN_MISC */
3597 for (i = 0; i < 8; i++)
3598 input_report_key(dev: hidpp->input, BTN_MISC + i,
3599 value: (data[4] & (1 << i)));
3600
3601 input_sync(dev: hidpp->input);
3602 return 1;
3603}
3604
3605static void hidpp10_extra_mouse_buttons_populate_input(
3606 struct hidpp_device *hidpp, struct input_dev *input_dev)
3607{
3608 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3609 __set_bit(BTN_0, input_dev->keybit);
3610 __set_bit(BTN_1, input_dev->keybit);
3611 __set_bit(BTN_2, input_dev->keybit);
3612 __set_bit(BTN_3, input_dev->keybit);
3613 __set_bit(BTN_4, input_dev->keybit);
3614 __set_bit(BTN_5, input_dev->keybit);
3615 __set_bit(BTN_6, input_dev->keybit);
3616 __set_bit(BTN_7, input_dev->keybit);
3617}
3618
3619/* -------------------------------------------------------------------------- */
3620/* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3621/* -------------------------------------------------------------------------- */
3622
3623/* Find the consumer-page input report desc and change Maximums to 0x107f */
3624static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3625 u8 *_rdesc, unsigned int *rsize)
3626{
3627 /* Note 0 terminated so we can use strnstr to search for this. */
3628 static const char consumer_rdesc_start[] = {
3629 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3630 0x09, 0x01, /* USAGE (Consumer Control) */
3631 0xA1, 0x01, /* COLLECTION (Application) */
3632 0x85, 0x03, /* REPORT_ID = 3 */
3633 0x75, 0x10, /* REPORT_SIZE (16) */
3634 0x95, 0x02, /* REPORT_COUNT (2) */
3635 0x15, 0x01, /* LOGICAL_MIN (1) */
3636 0x26, 0x00 /* LOGICAL_MAX (... */
3637 };
3638 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3639 unsigned int size;
3640
3641 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3642 size = *rsize - (consumer_rdesc - rdesc);
3643 if (consumer_rdesc && size >= 25) {
3644 consumer_rdesc[15] = 0x7f;
3645 consumer_rdesc[16] = 0x10;
3646 consumer_rdesc[20] = 0x7f;
3647 consumer_rdesc[21] = 0x10;
3648 }
3649 return _rdesc;
3650}
3651
3652static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3653{
3654 return hidpp10_set_register(hidpp_dev: hidpp, HIDPP_REG_ENABLE_REPORTS, byte: 0,
3655 HIDPP_ENABLE_CONSUMER_REPORT,
3656 HIDPP_ENABLE_CONSUMER_REPORT);
3657}
3658
3659static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3660 u8 *data, int size)
3661{
3662 u8 consumer_report[5];
3663
3664 if (size < 7)
3665 return 0;
3666
3667 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3668 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3669 return 0;
3670
3671 /*
3672 * Build a normal consumer report (3) out of the data, this detour
3673 * is necessary to get some keyboards to report their 0x10xx usages.
3674 */
3675 consumer_report[0] = 0x03;
3676 memcpy(&consumer_report[1], &data[3], 4);
3677 /* We are called from atomic context */
3678 hid_report_raw_event(hid: hidpp->hid_dev, type: HID_INPUT_REPORT,
3679 data: consumer_report, size: 5, interrupt: 1);
3680
3681 return 1;
3682}
3683
3684/* -------------------------------------------------------------------------- */
3685/* High-resolution scroll wheels */
3686/* -------------------------------------------------------------------------- */
3687
3688static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3689{
3690 int ret;
3691 u8 multiplier = 1;
3692
3693 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
3694 ret = hidpp_hrw_set_wheel_mode(hidpp, invert: false, high_resolution: true, use_hidpp: false);
3695 if (ret == 0)
3696 ret = hidpp_hrw_get_wheel_capability(hidpp, multiplier: &multiplier);
3697 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
3698 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, enabled: true,
3699 multiplier: &multiplier);
3700 } else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
3701 ret = hidpp10_enable_scrolling_acceleration(hidpp_dev: hidpp);
3702 multiplier = 8;
3703 }
3704 if (ret) {
3705 hid_dbg(hidpp->hid_dev,
3706 "Could not enable hi-res scrolling: %d\n", ret);
3707 return ret;
3708 }
3709
3710 if (multiplier == 0) {
3711 hid_dbg(hidpp->hid_dev,
3712 "Invalid multiplier 0 from device, setting it to 1\n");
3713 multiplier = 1;
3714 }
3715
3716 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3717 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3718 return 0;
3719}
3720
3721static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
3722{
3723 int ret;
3724 unsigned long capabilities;
3725
3726 capabilities = hidpp->capabilities;
3727
3728 if (hidpp->protocol_major >= 2) {
3729 u8 feature_index;
3730 u8 feature_type;
3731
3732 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
3733 feature_index: &feature_index, feature_type: &feature_type);
3734 if (!ret) {
3735 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
3736 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
3737 return 0;
3738 }
3739 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
3740 feature_index: &feature_index, feature_type: &feature_type);
3741 if (!ret) {
3742 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
3743 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
3744 }
3745 } else {
3746 /* We cannot detect fast scrolling support on HID++ 1.0 devices */
3747 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) {
3748 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
3749 hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
3750 }
3751 }
3752
3753 if (hidpp->capabilities == capabilities)
3754 hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
3755 return 0;
3756}
3757
3758/* -------------------------------------------------------------------------- */
3759/* Generic HID++ devices */
3760/* -------------------------------------------------------------------------- */
3761
3762static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3763 unsigned int *rsize)
3764{
3765 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3766
3767 if (!hidpp)
3768 return rdesc;
3769
3770 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3771 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3772 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3773 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc: rdesc, rsize);
3774
3775 return rdesc;
3776}
3777
3778static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3779 struct hid_field *field, struct hid_usage *usage,
3780 unsigned long **bit, int *max)
3781{
3782 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3783
3784 if (!hidpp)
3785 return 0;
3786
3787 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3788 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3789 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3790 field->application != HID_GD_MOUSE)
3791 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3792
3793 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3794 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3795
3796 return 0;
3797}
3798
3799static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3800 struct hid_field *field, struct hid_usage *usage,
3801 unsigned long **bit, int *max)
3802{
3803 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3804
3805 if (!hidpp)
3806 return 0;
3807
3808 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3809 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3810 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3811 usage->code == ABS_Y || usage->code == ABS_Z ||
3812 usage->code == ABS_RZ)) {
3813 field->application = HID_GD_MULTIAXIS;
3814 }
3815 }
3816
3817 return 0;
3818}
3819
3820
3821static void hidpp_populate_input(struct hidpp_device *hidpp,
3822 struct input_dev *input)
3823{
3824 hidpp->input = input;
3825
3826 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3827 wtp_populate_input(hidpp, input_dev: input);
3828 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3829 m560_populate_input(hidpp, input_dev: input);
3830
3831 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3832 hidpp10_wheel_populate_input(hidpp, input_dev: input);
3833
3834 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3835 hidpp10_extra_mouse_buttons_populate_input(hidpp, input_dev: input);
3836}
3837
3838static int hidpp_input_configured(struct hid_device *hdev,
3839 struct hid_input *hidinput)
3840{
3841 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3842 struct input_dev *input = hidinput->input;
3843
3844 if (!hidpp)
3845 return 0;
3846
3847 hidpp_populate_input(hidpp, input);
3848
3849 return 0;
3850}
3851
3852static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3853 int size)
3854{
3855 struct hidpp_report *question = hidpp->send_receive_buf;
3856 struct hidpp_report *answer = hidpp->send_receive_buf;
3857 struct hidpp_report *report = (struct hidpp_report *)data;
3858 int ret;
3859
3860 /*
3861 * If the mutex is locked then we have a pending answer from a
3862 * previously sent command.
3863 */
3864 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3865 /*
3866 * Check for a correct hidpp20 answer or the corresponding
3867 * error
3868 */
3869 if (hidpp_match_answer(question, answer: report) ||
3870 hidpp_match_error(question, answer: report)) {
3871 *answer = *report;
3872 hidpp->answer_available = true;
3873 wake_up(&hidpp->wait);
3874 /*
3875 * This was an answer to a command that this driver sent
3876 * We return 1 to hid-core to avoid forwarding the
3877 * command upstream as it has been treated by the driver
3878 */
3879
3880 return 1;
3881 }
3882 }
3883
3884 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3885 if (schedule_work(work: &hidpp->work) == 0)
3886 dbg_hid("%s: connect event already queued\n", __func__);
3887 return 1;
3888 }
3889
3890 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3891 data[0] == REPORT_ID_HIDPP_SHORT &&
3892 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3893 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3894 dev_err_ratelimited(&hidpp->hid_dev->dev,
3895 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3896 dev_err_ratelimited(&hidpp->hid_dev->dev,
3897 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3898 }
3899
3900 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3901 ret = hidpp20_battery_event_1000(hidpp, data, size);
3902 if (ret != 0)
3903 return ret;
3904 ret = hidpp20_battery_event_1004(hidpp, data, size);
3905 if (ret != 0)
3906 return ret;
3907 ret = hidpp_solar_battery_event(hidpp, data, size);
3908 if (ret != 0)
3909 return ret;
3910 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3911 if (ret != 0)
3912 return ret;
3913 ret = hidpp20_adc_measurement_event_1f20(hidpp, data, size);
3914 if (ret != 0)
3915 return ret;
3916 }
3917
3918 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3919 ret = hidpp10_battery_event(hidpp, data, size);
3920 if (ret != 0)
3921 return ret;
3922 }
3923
3924 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3925 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3926 if (ret != 0)
3927 return ret;
3928 }
3929
3930 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3931 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3932 if (ret != 0)
3933 return ret;
3934 }
3935
3936 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3937 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3938 if (ret != 0)
3939 return ret;
3940 }
3941
3942 return 0;
3943}
3944
3945static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3946 u8 *data, int size)
3947{
3948 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3949 int ret = 0;
3950
3951 if (!hidpp)
3952 return 0;
3953
3954 /* Generic HID++ processing. */
3955 switch (data[0]) {
3956 case REPORT_ID_HIDPP_VERY_LONG:
3957 if (size != hidpp->very_long_report_length) {
3958 hid_err(hdev, "received hid++ report of bad size (%d)",
3959 size);
3960 return 1;
3961 }
3962 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3963 break;
3964 case REPORT_ID_HIDPP_LONG:
3965 if (size != HIDPP_REPORT_LONG_LENGTH) {
3966 hid_err(hdev, "received hid++ report of bad size (%d)",
3967 size);
3968 return 1;
3969 }
3970 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3971 break;
3972 case REPORT_ID_HIDPP_SHORT:
3973 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3974 hid_err(hdev, "received hid++ report of bad size (%d)",
3975 size);
3976 return 1;
3977 }
3978 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3979 break;
3980 }
3981
3982 /* If no report is available for further processing, skip calling
3983 * raw_event of subclasses. */
3984 if (ret != 0)
3985 return ret;
3986
3987 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3988 return wtp_raw_event(hdev, data, size);
3989 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3990 return m560_raw_event(hdev, data, size);
3991
3992 return 0;
3993}
3994
3995static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3996 struct hid_usage *usage, __s32 value)
3997{
3998 /* This function will only be called for scroll events, due to the
3999 * restriction imposed in hidpp_usages.
4000 */
4001 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4002 struct hidpp_scroll_counter *counter;
4003
4004 if (!hidpp)
4005 return 0;
4006
4007 counter = &hidpp->vertical_wheel_counter;
4008 /* A scroll event may occur before the multiplier has been retrieved or
4009 * the input device set, or high-res scroll enabling may fail. In such
4010 * cases we must return early (falling back to default behaviour) to
4011 * avoid a crash in hidpp_scroll_counter_handle_scroll.
4012 */
4013 if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4014 || value == 0 || hidpp->input == NULL
4015 || counter->wheel_multiplier == 0)
4016 return 0;
4017
4018 hidpp_scroll_counter_handle_scroll(input_dev: hidpp->input, counter, hi_res_value: value);
4019 return 1;
4020}
4021
4022static int hidpp_initialize_battery(struct hidpp_device *hidpp)
4023{
4024 static atomic_t battery_no = ATOMIC_INIT(0);
4025 struct power_supply_config cfg = { .drv_data = hidpp };
4026 struct power_supply_desc *desc = &hidpp->battery.desc;
4027 enum power_supply_property *battery_props;
4028 struct hidpp_battery *battery;
4029 unsigned int num_battery_props;
4030 unsigned long n;
4031 int ret;
4032
4033 if (hidpp->battery.ps)
4034 return 0;
4035
4036 hidpp->battery.feature_index = 0xff;
4037 hidpp->battery.solar_feature_index = 0xff;
4038 hidpp->battery.voltage_feature_index = 0xff;
4039 hidpp->battery.adc_measurement_feature_index = 0xff;
4040
4041 if (hidpp->protocol_major >= 2) {
4042 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
4043 ret = hidpp_solar_request_battery_event(hidpp);
4044 else {
4045 /* we only support one battery feature right now, so let's
4046 first check the ones that support battery level first
4047 and leave voltage for last */
4048 ret = hidpp20_query_battery_info_1000(hidpp);
4049 if (ret)
4050 ret = hidpp20_query_battery_info_1004(hidpp);
4051 if (ret)
4052 ret = hidpp20_query_battery_voltage_info(hidpp);
4053 if (ret)
4054 ret = hidpp20_query_adc_measurement_info_1f20(hidpp);
4055 }
4056
4057 if (ret)
4058 return ret;
4059 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
4060 } else {
4061 ret = hidpp10_query_battery_status(hidpp);
4062 if (ret) {
4063 ret = hidpp10_query_battery_mileage(hidpp);
4064 if (ret)
4065 return -ENOENT;
4066 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
4067 } else {
4068 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
4069 }
4070 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
4071 }
4072
4073 battery_props = devm_kmemdup(dev: &hidpp->hid_dev->dev,
4074 src: hidpp_battery_props,
4075 len: sizeof(hidpp_battery_props),
4076 GFP_KERNEL);
4077 if (!battery_props)
4078 return -ENOMEM;
4079
4080 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
4081
4082 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
4083 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
4084 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4085 hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4086 battery_props[num_battery_props++] =
4087 POWER_SUPPLY_PROP_CAPACITY;
4088
4089 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
4090 battery_props[num_battery_props++] =
4091 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
4092
4093 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4094 hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4095 battery_props[num_battery_props++] =
4096 POWER_SUPPLY_PROP_VOLTAGE_NOW;
4097
4098 battery = &hidpp->battery;
4099
4100 n = atomic_inc_return(v: &battery_no) - 1;
4101 desc->properties = battery_props;
4102 desc->num_properties = num_battery_props;
4103 desc->get_property = hidpp_battery_get_property;
4104 sprintf(buf: battery->name, fmt: "hidpp_battery_%ld", n);
4105 desc->name = battery->name;
4106 desc->type = POWER_SUPPLY_TYPE_BATTERY;
4107 desc->use_for_apm = 0;
4108
4109 battery->ps = devm_power_supply_register(parent: &hidpp->hid_dev->dev,
4110 desc: &battery->desc,
4111 cfg: &cfg);
4112 if (IS_ERR(ptr: battery->ps))
4113 return PTR_ERR(ptr: battery->ps);
4114
4115 power_supply_powers(psy: battery->ps, dev: &hidpp->hid_dev->dev);
4116
4117 return ret;
4118}
4119
4120/* Get name + serial for USB and Bluetooth HID++ devices */
4121static void hidpp_non_unifying_init(struct hidpp_device *hidpp)
4122{
4123 struct hid_device *hdev = hidpp->hid_dev;
4124 char *name;
4125
4126 /* Bluetooth devices already have their serialnr set */
4127 if (hid_is_usb(hdev))
4128 hidpp_serial_init(hidpp);
4129
4130 name = hidpp_get_device_name(hidpp);
4131 if (name) {
4132 dbg_hid("HID++: Got name: %s\n", name);
4133 snprintf(buf: hdev->name, size: sizeof(hdev->name), fmt: "%s", name);
4134 kfree(objp: name);
4135 }
4136}
4137
4138static int hidpp_input_open(struct input_dev *dev)
4139{
4140 struct hid_device *hid = input_get_drvdata(dev);
4141
4142 return hid_hw_open(hdev: hid);
4143}
4144
4145static void hidpp_input_close(struct input_dev *dev)
4146{
4147 struct hid_device *hid = input_get_drvdata(dev);
4148
4149 hid_hw_close(hdev: hid);
4150}
4151
4152static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
4153{
4154 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
4155 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4156
4157 if (!input_dev)
4158 return NULL;
4159
4160 input_set_drvdata(dev: input_dev, data: hdev);
4161 input_dev->open = hidpp_input_open;
4162 input_dev->close = hidpp_input_close;
4163
4164 input_dev->name = hidpp->name;
4165 input_dev->phys = hdev->phys;
4166 input_dev->uniq = hdev->uniq;
4167 input_dev->id.bustype = hdev->bus;
4168 input_dev->id.vendor = hdev->vendor;
4169 input_dev->id.product = hdev->product;
4170 input_dev->id.version = hdev->version;
4171 input_dev->dev.parent = &hdev->dev;
4172
4173 return input_dev;
4174}
4175
4176static void hidpp_connect_event(struct work_struct *work)
4177{
4178 struct hidpp_device *hidpp = container_of(work, struct hidpp_device, work);
4179 struct hid_device *hdev = hidpp->hid_dev;
4180 struct input_dev *input;
4181 char *name, *devm_name;
4182 int ret;
4183
4184 /* Get device version to check if it is connected */
4185 ret = hidpp_root_get_protocol_version(hidpp);
4186 if (ret) {
4187 hid_info(hidpp->hid_dev, "Disconnected\n");
4188 if (hidpp->battery.ps) {
4189 hidpp->battery.online = false;
4190 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
4191 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
4192 power_supply_changed(psy: hidpp->battery.ps);
4193 }
4194 return;
4195 }
4196
4197 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4198 ret = wtp_connect(hdev);
4199 if (ret)
4200 return;
4201 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
4202 ret = m560_send_config_command(hdev);
4203 if (ret)
4204 return;
4205 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4206 ret = k400_connect(hdev);
4207 if (ret)
4208 return;
4209 }
4210
4211 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
4212 ret = hidpp10_wheel_connect(hidpp);
4213 if (ret)
4214 return;
4215 }
4216
4217 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
4218 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
4219 if (ret)
4220 return;
4221 }
4222
4223 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
4224 ret = hidpp10_consumer_keys_connect(hidpp);
4225 if (ret)
4226 return;
4227 }
4228
4229 if (hidpp->protocol_major >= 2) {
4230 u8 feature_index;
4231
4232 if (!hidpp_get_wireless_feature_index(hidpp, feature_index: &feature_index))
4233 hidpp->wireless_feature_index = feature_index;
4234 }
4235
4236 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
4237 name = hidpp_get_device_name(hidpp);
4238 if (name) {
4239 devm_name = devm_kasprintf(dev: &hdev->dev, GFP_KERNEL,
4240 fmt: "%s", name);
4241 kfree(objp: name);
4242 if (!devm_name)
4243 return;
4244
4245 hidpp->name = devm_name;
4246 }
4247 }
4248
4249 hidpp_initialize_battery(hidpp);
4250 if (!hid_is_usb(hdev: hidpp->hid_dev))
4251 hidpp_initialize_hires_scroll(hidpp);
4252
4253 /* forward current battery state */
4254 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
4255 hidpp10_enable_battery_reporting(hidpp_dev: hidpp);
4256 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
4257 hidpp10_query_battery_mileage(hidpp);
4258 else
4259 hidpp10_query_battery_status(hidpp);
4260 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
4261 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
4262 hidpp20_query_battery_voltage_info(hidpp);
4263 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
4264 hidpp20_query_battery_info_1004(hidpp);
4265 else if (hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4266 hidpp20_query_adc_measurement_info_1f20(hidpp);
4267 else
4268 hidpp20_query_battery_info_1000(hidpp);
4269 }
4270 if (hidpp->battery.ps)
4271 power_supply_changed(psy: hidpp->battery.ps);
4272
4273 if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4274 hi_res_scroll_enable(hidpp);
4275
4276 if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
4277 /* if the input nodes are already created, we can stop now */
4278 return;
4279
4280 input = hidpp_allocate_input(hdev);
4281 if (!input) {
4282 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
4283 return;
4284 }
4285
4286 hidpp_populate_input(hidpp, input);
4287
4288 ret = input_register_device(input);
4289 if (ret) {
4290 input_free_device(dev: input);
4291 return;
4292 }
4293
4294 hidpp->delayed_input = input;
4295}
4296
4297static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
4298
4299static struct attribute *sysfs_attrs[] = {
4300 &dev_attr_builtin_power_supply.attr,
4301 NULL
4302};
4303
4304static const struct attribute_group ps_attribute_group = {
4305 .attrs = sysfs_attrs
4306};
4307
4308static int hidpp_get_report_length(struct hid_device *hdev, int id)
4309{
4310 struct hid_report_enum *re;
4311 struct hid_report *report;
4312
4313 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
4314 report = re->report_id_hash[id];
4315 if (!report)
4316 return 0;
4317
4318 return report->field[0]->report_count + 1;
4319}
4320
4321static u8 hidpp_validate_device(struct hid_device *hdev)
4322{
4323 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4324 int id, report_length;
4325 u8 supported_reports = 0;
4326
4327 id = REPORT_ID_HIDPP_SHORT;
4328 report_length = hidpp_get_report_length(hdev, id);
4329 if (report_length) {
4330 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4331 goto bad_device;
4332
4333 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4334 }
4335
4336 id = REPORT_ID_HIDPP_LONG;
4337 report_length = hidpp_get_report_length(hdev, id);
4338 if (report_length) {
4339 if (report_length < HIDPP_REPORT_LONG_LENGTH)
4340 goto bad_device;
4341
4342 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4343 }
4344
4345 id = REPORT_ID_HIDPP_VERY_LONG;
4346 report_length = hidpp_get_report_length(hdev, id);
4347 if (report_length) {
4348 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4349 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4350 goto bad_device;
4351
4352 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4353 hidpp->very_long_report_length = report_length;
4354 }
4355
4356 return supported_reports;
4357
4358bad_device:
4359 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4360 return false;
4361}
4362
4363static bool hidpp_application_equals(struct hid_device *hdev,
4364 unsigned int application)
4365{
4366 struct list_head *report_list;
4367 struct hid_report *report;
4368
4369 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4370 report = list_first_entry_or_null(report_list, struct hid_report, list);
4371 return report && report->application == application;
4372}
4373
4374static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4375{
4376 struct hidpp_device *hidpp;
4377 int ret;
4378 unsigned int connect_mask = HID_CONNECT_DEFAULT;
4379
4380 /* report_fixup needs drvdata to be set before we call hid_parse */
4381 hidpp = devm_kzalloc(dev: &hdev->dev, size: sizeof(*hidpp), GFP_KERNEL);
4382 if (!hidpp)
4383 return -ENOMEM;
4384
4385 hidpp->hid_dev = hdev;
4386 hidpp->name = hdev->name;
4387 hidpp->quirks = id->driver_data;
4388 hid_set_drvdata(hdev, data: hidpp);
4389
4390 ret = hid_parse(hdev);
4391 if (ret) {
4392 hid_err(hdev, "%s:parse failed\n", __func__);
4393 return ret;
4394 }
4395
4396 /*
4397 * Make sure the device is HID++ capable, otherwise treat as generic HID
4398 */
4399 hidpp->supported_reports = hidpp_validate_device(hdev);
4400
4401 if (!hidpp->supported_reports) {
4402 hid_set_drvdata(hdev, NULL);
4403 devm_kfree(dev: &hdev->dev, p: hidpp);
4404 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4405 }
4406
4407 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4408 hidpp_application_equals(hdev, HID_GD_MOUSE))
4409 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4410 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4411
4412 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4413 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4414 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4415
4416 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4417 ret = wtp_allocate(hdev, id);
4418 if (ret)
4419 return ret;
4420 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4421 ret = k400_allocate(hdev);
4422 if (ret)
4423 return ret;
4424 }
4425
4426 INIT_WORK(&hidpp->work, hidpp_connect_event);
4427 mutex_init(&hidpp->send_mutex);
4428 init_waitqueue_head(&hidpp->wait);
4429
4430 /* indicates we are handling the battery properties in the kernel */
4431 ret = sysfs_create_group(kobj: &hdev->dev.kobj, grp: &ps_attribute_group);
4432 if (ret)
4433 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4434 hdev->name);
4435
4436 /*
4437 * First call hid_hw_start(hdev, 0) to allow IO without connecting any
4438 * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's
4439 * name and serial number and store these in hdev->name and hdev->uniq,
4440 * before the hid-input and hidraw drivers expose these to userspace.
4441 */
4442 ret = hid_hw_start(hdev, connect_mask: 0);
4443 if (ret) {
4444 hid_err(hdev, "hw start failed\n");
4445 goto hid_hw_start_fail;
4446 }
4447
4448 ret = hid_hw_open(hdev);
4449 if (ret < 0) {
4450 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4451 __func__, ret);
4452 goto hid_hw_open_fail;
4453 }
4454
4455 /* Allow incoming packets */
4456 hid_device_io_start(hid: hdev);
4457
4458 /* Get name + serial, store in hdev->name + hdev->uniq */
4459 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4460 hidpp_unifying_init(hidpp);
4461 else
4462 hidpp_non_unifying_init(hidpp);
4463
4464 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
4465 connect_mask &= ~HID_CONNECT_HIDINPUT;
4466
4467 /* Now export the actual inputs and hidraw nodes to the world */
4468 hid_device_io_stop(hid: hdev);
4469 ret = hid_connect(hid: hdev, connect_mask);
4470 if (ret) {
4471 hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret);
4472 goto hid_hw_init_fail;
4473 }
4474
4475 /* Check for connected devices now that incoming packets will not be disabled again */
4476 hid_device_io_start(hid: hdev);
4477 schedule_work(work: &hidpp->work);
4478 flush_work(work: &hidpp->work);
4479
4480 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4481 struct hidpp_ff_private_data data;
4482
4483 ret = g920_get_config(hidpp, data: &data);
4484 if (!ret)
4485 ret = hidpp_ff_init(hidpp, data: &data);
4486
4487 if (ret)
4488 hid_warn(hidpp->hid_dev,
4489 "Unable to initialize force feedback support, errno %d\n",
4490 ret);
4491 }
4492
4493 /*
4494 * This relies on logi_dj_ll_close() being a no-op so that DJ connection
4495 * events will still be received.
4496 */
4497 hid_hw_close(hdev);
4498 return ret;
4499
4500hid_hw_init_fail:
4501 hid_hw_close(hdev);
4502hid_hw_open_fail:
4503 hid_hw_stop(hdev);
4504hid_hw_start_fail:
4505 sysfs_remove_group(kobj: &hdev->dev.kobj, grp: &ps_attribute_group);
4506 cancel_work_sync(work: &hidpp->work);
4507 mutex_destroy(lock: &hidpp->send_mutex);
4508 return ret;
4509}
4510
4511static void hidpp_remove(struct hid_device *hdev)
4512{
4513 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4514
4515 if (!hidpp)
4516 return hid_hw_stop(hdev);
4517
4518 sysfs_remove_group(kobj: &hdev->dev.kobj, grp: &ps_attribute_group);
4519
4520 hid_hw_stop(hdev);
4521 cancel_work_sync(work: &hidpp->work);
4522 mutex_destroy(lock: &hidpp->send_mutex);
4523}
4524
4525#define LDJ_DEVICE(product) \
4526 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4527 USB_VENDOR_ID_LOGITECH, (product))
4528
4529#define L27MHZ_DEVICE(product) \
4530 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4531 USB_VENDOR_ID_LOGITECH, (product))
4532
4533static const struct hid_device_id hidpp_devices[] = {
4534 { /* wireless touchpad */
4535 LDJ_DEVICE(0x4011),
4536 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4537 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4538 { /* wireless touchpad T650 */
4539 LDJ_DEVICE(0x4101),
4540 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4541 { /* wireless touchpad T651 */
4542 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4543 USB_DEVICE_ID_LOGITECH_T651),
4544 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4545 { /* Mouse Logitech Anywhere MX */
4546 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4547 { /* Mouse logitech M560 */
4548 LDJ_DEVICE(0x402d),
4549 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
4550 { /* Mouse Logitech M705 (firmware RQM17) */
4551 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4552 { /* Mouse Logitech Performance MX */
4553 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4554 { /* Keyboard logitech K400 */
4555 LDJ_DEVICE(0x4024),
4556 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4557 { /* Solar Keyboard Logitech K750 */
4558 LDJ_DEVICE(0x4002),
4559 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4560 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4561 LDJ_DEVICE(0xb305),
4562 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4563 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4564 LDJ_DEVICE(0xb309),
4565 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4566 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4567 LDJ_DEVICE(0xb30b),
4568 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4569
4570 { LDJ_DEVICE(HID_ANY_ID) },
4571
4572 { /* Keyboard LX501 (Y-RR53) */
4573 L27MHZ_DEVICE(0x0049),
4574 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4575 { /* Keyboard MX3000 (Y-RAM74) */
4576 L27MHZ_DEVICE(0x0057),
4577 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4578 { /* Keyboard MX3200 (Y-RAV80) */
4579 L27MHZ_DEVICE(0x005c),
4580 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4581 { /* S510 Media Remote */
4582 L27MHZ_DEVICE(0x00fe),
4583 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4584
4585 { L27MHZ_DEVICE(HID_ANY_ID) },
4586
4587 { /* Logitech G403 Wireless Gaming Mouse over USB */
4588 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4589 { /* Logitech G502 Lightspeed Wireless Gaming Mouse over USB */
4590 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC08D) },
4591 { /* Logitech G703 Gaming Mouse over USB */
4592 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4593 { /* Logitech G703 Hero Gaming Mouse over USB */
4594 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4595 { /* Logitech G900 Gaming Mouse over USB */
4596 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4597 { /* Logitech G903 Gaming Mouse over USB */
4598 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4599 { /* Logitech G903 Hero Gaming Mouse over USB */
4600 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4601 { /* Logitech G915 TKL Keyboard over USB */
4602 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC343) },
4603 { /* Logitech G920 Wheel over USB */
4604 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4605 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4606 { /* Logitech G923 Wheel (Xbox version) over USB */
4607 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G923_XBOX_WHEEL),
4608 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS },
4609 { /* Logitech G Pro Gaming Mouse over USB */
4610 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4611 { /* Logitech G Pro X Superlight Gaming Mouse over USB */
4612 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC094) },
4613
4614 { /* G935 Gaming Headset */
4615 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0x0a87),
4616 .driver_data = HIDPP_QUIRK_WIRELESS_STATUS },
4617
4618 { /* MX5000 keyboard over Bluetooth */
4619 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4620 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4621 { /* Dinovo Edge keyboard over Bluetooth */
4622 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4623 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4624 { /* MX5500 keyboard over Bluetooth */
4625 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4626 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4627 { /* Logitech G915 TKL keyboard over Bluetooth */
4628 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb35f) },
4629 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4630 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4631 { /* MX Master mouse over Bluetooth */
4632 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
4633 { /* M720 Triathlon mouse over Bluetooth */
4634 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb015) },
4635 { /* MX Ergo trackball over Bluetooth */
4636 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4637 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
4638 { /* Signature M650 over Bluetooth */
4639 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb02a) },
4640 { /* MX Master 3 mouse over Bluetooth */
4641 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
4642 { /* MX Anywhere 3 mouse over Bluetooth */
4643 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb025) },
4644 { /* MX Master 3S mouse over Bluetooth */
4645 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) },
4646 {}
4647};
4648
4649MODULE_DEVICE_TABLE(hid, hidpp_devices);
4650
4651static const struct hid_usage_id hidpp_usages[] = {
4652 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4653 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4654};
4655
4656static struct hid_driver hidpp_driver = {
4657 .name = "logitech-hidpp-device",
4658 .id_table = hidpp_devices,
4659 .report_fixup = hidpp_report_fixup,
4660 .probe = hidpp_probe,
4661 .remove = hidpp_remove,
4662 .raw_event = hidpp_raw_event,
4663 .usage_table = hidpp_usages,
4664 .event = hidpp_event,
4665 .input_configured = hidpp_input_configured,
4666 .input_mapping = hidpp_input_mapping,
4667 .input_mapped = hidpp_input_mapped,
4668};
4669
4670module_hid_driver(hidpp_driver);
4671

source code of linux/drivers/hid/hid-logitech-hidpp.c