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