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