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