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