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