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