xref: /linux/drivers/hid/hid-logitech-hidpp.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
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_obj(&hdev->dev, struct wtp_data);
3139 	if (!wd)
3140 		return -ENOMEM;
3141 
3142 	hidpp->private_data = wd;
3143 
3144 	return 0;
3145 };
3146 
3147 static int wtp_connect(struct hid_device *hdev)
3148 {
3149 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3150 	struct wtp_data *wd = hidpp->private_data;
3151 	int ret;
3152 
3153 	if (!wd->x_size) {
3154 		ret = wtp_get_config(hidpp);
3155 		if (ret) {
3156 			hid_err(hdev, "Can not get wtp config: %d\n", ret);
3157 			return ret;
3158 		}
3159 	}
3160 
3161 	return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
3162 			true, true);
3163 }
3164 
3165 /* ------------------------------------------------------------------------- */
3166 /* Logitech M560 devices                                                     */
3167 /* ------------------------------------------------------------------------- */
3168 
3169 /*
3170  * Logitech M560 protocol overview
3171  *
3172  * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
3173  * the sides buttons are pressed, it sends some keyboard keys events
3174  * instead of buttons ones.
3175  * To complicate things further, the middle button keys sequence
3176  * is different from the odd press and the even press.
3177  *
3178  * forward button -> Super_R
3179  * backward button -> Super_L+'d' (press only)
3180  * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
3181  *                  2nd time: left-click (press only)
3182  * NB: press-only means that when the button is pressed, the
3183  * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
3184  * together sequentially; instead when the button is released, no event is
3185  * generated !
3186  *
3187  * With the command
3188  *	10<xx>0a 3500af03 (where <xx> is the mouse id),
3189  * the mouse reacts differently:
3190  * - it never sends a keyboard key event
3191  * - for the three mouse button it sends:
3192  *	middle button               press   11<xx>0a 3500af00...
3193  *	side 1 button (forward)     press   11<xx>0a 3500b000...
3194  *	side 2 button (backward)    press   11<xx>0a 3500ae00...
3195  *	middle/side1/side2 button   release 11<xx>0a 35000000...
3196  */
3197 
3198 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
3199 
3200 /* how buttons are mapped in the report */
3201 #define M560_MOUSE_BTN_LEFT		0x01
3202 #define M560_MOUSE_BTN_RIGHT		0x02
3203 #define M560_MOUSE_BTN_WHEEL_LEFT	0x08
3204 #define M560_MOUSE_BTN_WHEEL_RIGHT	0x10
3205 
3206 #define M560_SUB_ID			0x0a
3207 #define M560_BUTTON_MODE_REGISTER	0x35
3208 
3209 static int m560_send_config_command(struct hid_device *hdev)
3210 {
3211 	struct hidpp_report response;
3212 	struct hidpp_device *hidpp_dev;
3213 
3214 	hidpp_dev = hid_get_drvdata(hdev);
3215 
3216 	return hidpp_send_rap_command_sync(
3217 		hidpp_dev,
3218 		REPORT_ID_HIDPP_SHORT,
3219 		M560_SUB_ID,
3220 		M560_BUTTON_MODE_REGISTER,
3221 		(u8 *)m560_config_parameter,
3222 		sizeof(m560_config_parameter),
3223 		&response
3224 	);
3225 }
3226 
3227 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
3228 {
3229 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3230 
3231 	/* sanity check */
3232 	if (!hidpp->input) {
3233 		hid_err(hdev, "error in parameter\n");
3234 		return -EINVAL;
3235 	}
3236 
3237 	if (size < 7) {
3238 		hid_err(hdev, "error in report\n");
3239 		return 0;
3240 	}
3241 
3242 	if (data[0] == REPORT_ID_HIDPP_LONG &&
3243 	    data[2] == M560_SUB_ID && data[6] == 0x00) {
3244 		/*
3245 		 * m560 mouse report for middle, forward and backward button
3246 		 *
3247 		 * data[0] = 0x11
3248 		 * data[1] = device-id
3249 		 * data[2] = 0x0a
3250 		 * data[5] = 0xaf -> middle
3251 		 *	     0xb0 -> forward
3252 		 *	     0xae -> backward
3253 		 *	     0x00 -> release all
3254 		 * data[6] = 0x00
3255 		 */
3256 
3257 		switch (data[5]) {
3258 		case 0xaf:
3259 			input_report_key(hidpp->input, BTN_MIDDLE, 1);
3260 			break;
3261 		case 0xb0:
3262 			input_report_key(hidpp->input, BTN_FORWARD, 1);
3263 			break;
3264 		case 0xae:
3265 			input_report_key(hidpp->input, BTN_BACK, 1);
3266 			break;
3267 		case 0x00:
3268 			input_report_key(hidpp->input, BTN_BACK, 0);
3269 			input_report_key(hidpp->input, BTN_FORWARD, 0);
3270 			input_report_key(hidpp->input, BTN_MIDDLE, 0);
3271 			break;
3272 		default:
3273 			hid_err(hdev, "error in report\n");
3274 			return 0;
3275 		}
3276 		input_sync(hidpp->input);
3277 
3278 	} else if (data[0] == 0x02) {
3279 		/*
3280 		 * Logitech M560 mouse report
3281 		 *
3282 		 * data[0] = type (0x02)
3283 		 * data[1..2] = buttons
3284 		 * data[3..5] = xy
3285 		 * data[6] = wheel
3286 		 */
3287 
3288 		int v;
3289 
3290 		input_report_key(hidpp->input, BTN_LEFT,
3291 			!!(data[1] & M560_MOUSE_BTN_LEFT));
3292 		input_report_key(hidpp->input, BTN_RIGHT,
3293 			!!(data[1] & M560_MOUSE_BTN_RIGHT));
3294 
3295 		if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
3296 			input_report_rel(hidpp->input, REL_HWHEEL, -1);
3297 			input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3298 					 -120);
3299 		} else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
3300 			input_report_rel(hidpp->input, REL_HWHEEL, 1);
3301 			input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
3302 					 120);
3303 		}
3304 
3305 		v = sign_extend32(hid_field_extract(hdev, data + 3, 0, 12), 11);
3306 		input_report_rel(hidpp->input, REL_X, v);
3307 
3308 		v = sign_extend32(hid_field_extract(hdev, data + 3, 12, 12), 11);
3309 		input_report_rel(hidpp->input, REL_Y, v);
3310 
3311 		v = sign_extend32(data[6], 7);
3312 		if (v != 0)
3313 			hidpp_scroll_counter_handle_scroll(hidpp->input,
3314 					&hidpp->vertical_wheel_counter, v);
3315 
3316 		input_sync(hidpp->input);
3317 	}
3318 
3319 	return 1;
3320 }
3321 
3322 static void m560_populate_input(struct hidpp_device *hidpp,
3323 				struct input_dev *input_dev)
3324 {
3325 	__set_bit(EV_KEY, input_dev->evbit);
3326 	__set_bit(BTN_MIDDLE, input_dev->keybit);
3327 	__set_bit(BTN_RIGHT, input_dev->keybit);
3328 	__set_bit(BTN_LEFT, input_dev->keybit);
3329 	__set_bit(BTN_BACK, input_dev->keybit);
3330 	__set_bit(BTN_FORWARD, input_dev->keybit);
3331 
3332 	__set_bit(EV_REL, input_dev->evbit);
3333 	__set_bit(REL_X, input_dev->relbit);
3334 	__set_bit(REL_Y, input_dev->relbit);
3335 	__set_bit(REL_WHEEL, input_dev->relbit);
3336 	__set_bit(REL_HWHEEL, input_dev->relbit);
3337 	__set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3338 	__set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3339 }
3340 
3341 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3342 		struct hid_field *field, struct hid_usage *usage,
3343 		unsigned long **bit, int *max)
3344 {
3345 	return -1;
3346 }
3347 
3348 /* ------------------------------------------------------------------------- */
3349 /* Logitech K400 devices                                                     */
3350 /* ------------------------------------------------------------------------- */
3351 
3352 /*
3353  * The Logitech K400 keyboard has an embedded touchpad which is seen
3354  * as a mouse from the OS point of view. There is a hardware shortcut to disable
3355  * tap-to-click but the setting is not remembered accross reset, annoying some
3356  * users.
3357  *
3358  * We can toggle this feature from the host by using the feature 0x6010:
3359  * Touchpad FW items
3360  */
3361 
3362 struct k400_private_data {
3363 	u8 feature_index;
3364 };
3365 
3366 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3367 {
3368 	struct k400_private_data *k400 = hidpp->private_data;
3369 	struct hidpp_touchpad_fw_items items = {};
3370 	int ret;
3371 
3372 	if (!k400->feature_index) {
3373 		ret = hidpp_root_get_feature(hidpp,
3374 			HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3375 			&k400->feature_index);
3376 		if (ret)
3377 			/* means that the device is not powered up */
3378 			return ret;
3379 	}
3380 
3381 	ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3382 	if (ret)
3383 		return ret;
3384 
3385 	return 0;
3386 }
3387 
3388 static int k400_allocate(struct hid_device *hdev)
3389 {
3390 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3391 	struct k400_private_data *k400;
3392 
3393 	k400 = devm_kzalloc_obj(&hdev->dev, struct k400_private_data);
3394 	if (!k400)
3395 		return -ENOMEM;
3396 
3397 	hidpp->private_data = k400;
3398 
3399 	return 0;
3400 };
3401 
3402 static int k400_connect(struct hid_device *hdev)
3403 {
3404 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3405 
3406 	if (!disable_tap_to_click)
3407 		return 0;
3408 
3409 	return k400_disable_tap_to_click(hidpp);
3410 }
3411 
3412 /* ------------------------------------------------------------------------- */
3413 /* Logitech G920 Driving Force Racing Wheel for Xbox One                     */
3414 /* ------------------------------------------------------------------------- */
3415 
3416 #define HIDPP_PAGE_G920_FORCE_FEEDBACK			0x8123
3417 
3418 static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3419 				  struct hidpp_ff_private_data *data)
3420 {
3421 	struct hidpp_report response;
3422 	u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3423 		[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3424 	};
3425 	int ret;
3426 
3427 	/* initialize with zero autocenter to get wheel in usable state */
3428 
3429 	dbg_hid("Setting autocenter to 0.\n");
3430 	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3431 					  HIDPP_FF_DOWNLOAD_EFFECT,
3432 					  params, ARRAY_SIZE(params),
3433 					  &response);
3434 	if (ret)
3435 		hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3436 	else
3437 		data->slot_autocenter = response.fap.params[0];
3438 
3439 	return ret;
3440 }
3441 
3442 static int g920_get_config(struct hidpp_device *hidpp,
3443 			   struct hidpp_ff_private_data *data)
3444 {
3445 	struct hidpp_report response;
3446 	int ret;
3447 
3448 	memset(data, 0, sizeof(*data));
3449 
3450 	/* Find feature and store for later use */
3451 	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3452 				     &data->feature_index);
3453 	if (ret)
3454 		return ret;
3455 
3456 	/* Read number of slots available in device */
3457 	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3458 					  HIDPP_FF_GET_INFO,
3459 					  NULL, 0,
3460 					  &response);
3461 	if (ret) {
3462 		if (ret < 0)
3463 			return ret;
3464 		hid_err(hidpp->hid_dev,
3465 			"%s: received protocol error 0x%02x\n", __func__, ret);
3466 		return -EPROTO;
3467 	}
3468 
3469 	data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3470 
3471 	/* reset all forces */
3472 	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3473 					  HIDPP_FF_RESET_ALL,
3474 					  NULL, 0,
3475 					  &response);
3476 	if (ret)
3477 		hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3478 
3479 	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3480 					  HIDPP_FF_GET_APERTURE,
3481 					  NULL, 0,
3482 					  &response);
3483 	if (ret) {
3484 		hid_warn(hidpp->hid_dev,
3485 			 "Failed to read range from device!\n");
3486 	}
3487 	data->range = ret ?
3488 		900 : get_unaligned_be16(&response.fap.params[0]);
3489 
3490 	/* Read the current gain values */
3491 	ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3492 					  HIDPP_FF_GET_GLOBAL_GAINS,
3493 					  NULL, 0,
3494 					  &response);
3495 	if (ret)
3496 		hid_warn(hidpp->hid_dev,
3497 			 "Failed to read gain values from device!\n");
3498 	data->gain = ret ?
3499 		0xffff : get_unaligned_be16(&response.fap.params[0]);
3500 
3501 	/* ignore boost value at response.fap.params[2] */
3502 
3503 	return g920_ff_set_autocenter(hidpp, data);
3504 }
3505 
3506 /* -------------------------------------------------------------------------- */
3507 /* Logitech Dinovo Mini keyboard with builtin touchpad                        */
3508 /* -------------------------------------------------------------------------- */
3509 #define DINOVO_MINI_PRODUCT_ID		0xb30c
3510 
3511 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3512 		struct hid_field *field, struct hid_usage *usage,
3513 		unsigned long **bit, int *max)
3514 {
3515 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3516 		return 0;
3517 
3518 	switch (usage->hid & HID_USAGE) {
3519 	case 0x00d: lg_map_key_clear(KEY_MEDIA);	break;
3520 	default:
3521 		return 0;
3522 	}
3523 	return 1;
3524 }
3525 
3526 /* -------------------------------------------------------------------------- */
3527 /* HID++1.0 devices which use HID++ reports for their wheels                  */
3528 /* -------------------------------------------------------------------------- */
3529 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3530 {
3531 	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3532 			HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3533 			HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3534 }
3535 
3536 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3537 				   u8 *data, int size)
3538 {
3539 	s8 value, hvalue;
3540 
3541 	if (!hidpp->input)
3542 		return -EINVAL;
3543 
3544 	if (size < 7)
3545 		return 0;
3546 
3547 	if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3548 		return 0;
3549 
3550 	value = data[3];
3551 	hvalue = data[4];
3552 
3553 	input_report_rel(hidpp->input, REL_WHEEL, value);
3554 	input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3555 	input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3556 	input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3557 	input_sync(hidpp->input);
3558 
3559 	return 1;
3560 }
3561 
3562 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3563 					 struct input_dev *input_dev)
3564 {
3565 	__set_bit(EV_REL, input_dev->evbit);
3566 	__set_bit(REL_WHEEL, input_dev->relbit);
3567 	__set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3568 	__set_bit(REL_HWHEEL, input_dev->relbit);
3569 	__set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3570 }
3571 
3572 /* -------------------------------------------------------------------------- */
3573 /* HID++1.0 mice which use HID++ reports for extra mouse buttons              */
3574 /* -------------------------------------------------------------------------- */
3575 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3576 {
3577 	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3578 				    HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3579 				    HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3580 }
3581 
3582 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3583 				    u8 *data, int size)
3584 {
3585 	int i;
3586 
3587 	if (!hidpp->input)
3588 		return -EINVAL;
3589 
3590 	if (size < 7)
3591 		return 0;
3592 
3593 	if (data[0] != REPORT_ID_HIDPP_SHORT ||
3594 	    data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3595 		return 0;
3596 
3597 	/*
3598 	 * Buttons are either delivered through the regular mouse report *or*
3599 	 * through the extra buttons report. At least for button 6 how it is
3600 	 * delivered differs per receiver firmware version. Even receivers with
3601 	 * the same usb-id show different behavior, so we handle both cases.
3602 	 */
3603 	for (i = 0; i < 8; i++)
3604 		input_report_key(hidpp->input, BTN_MOUSE + i,
3605 				 (data[3] & (1 << i)));
3606 
3607 	/* Some mice report events on button 9+, use BTN_MISC */
3608 	for (i = 0; i < 8; i++)
3609 		input_report_key(hidpp->input, BTN_MISC + i,
3610 				 (data[4] & (1 << i)));
3611 
3612 	input_sync(hidpp->input);
3613 	return 1;
3614 }
3615 
3616 /* -------------------------------------------------------------------------- */
3617 /* HID++2.0 reprogrammable controls                                           */
3618 /* -------------------------------------------------------------------------- */
3619 
3620 #define HIDPP_PAGE_REPROG_CONTROLS_V4			0x1b04
3621 
3622 #define HIDPP_REPROG_CONTROLS_GET_COUNT			0x00
3623 #define HIDPP_REPROG_CONTROLS_GET_CID_INFO		0x10
3624 #define HIDPP_REPROG_CONTROLS_SET_CONTROL_REPORTING	0x30
3625 
3626 #define HIDPP_REPROG_CONTROLS_FLAG_MOUSE		BIT(0)
3627 #define HIDPP_REPROG_CONTROLS_FLAG_DIVERT		BIT(5)
3628 
3629 #define HIDPP_REPROG_CONTROLS_TEMPORARY_DIVERTED	BIT(0)
3630 #define HIDPP_REPROG_CONTROLS_CHANGE_TEMPORARY_DIVERT	BIT(1)
3631 
3632 #define HIDPP_REPROG_CONTROLS_EVENT_DIVERTED		0x00
3633 
3634 #define HIDPP_REPROG_CONTROL_BACK			0x0053
3635 #define HIDPP_REPROG_CONTROL_FORWARD			0x0056
3636 
3637 #define HIDPP_PRODUCT_SIGNATURE_M650			0xb02a
3638 
3639 struct hidpp_reprog_control_mapping {
3640 	u16 control;
3641 	u16 code;
3642 };
3643 
3644 static const struct hidpp_reprog_control_mapping m650_reprog_control_mappings[] = {
3645 	{ HIDPP_REPROG_CONTROL_BACK, BTN_BACK },
3646 	{ HIDPP_REPROG_CONTROL_FORWARD, BTN_FORWARD },
3647 	{ }
3648 };
3649 
3650 static const struct hidpp_reprog_control_mapping *
3651 hidpp20_reprog_controls_get_mappings(struct hidpp_device *hidpp)
3652 {
3653 	switch (hidpp->hid_dev->product) {
3654 	case HIDPP_PRODUCT_SIGNATURE_M650:
3655 		return m650_reprog_control_mappings;
3656 	}
3657 
3658 	return NULL;
3659 }
3660 
3661 static int hidpp20_reprog_controls_get_count(struct hidpp_device *hidpp)
3662 {
3663 	struct hidpp_report response;
3664 	u8 feature_index = hidpp->reprog_controls_feature_index;
3665 	u8 cmd = HIDPP_REPROG_CONTROLS_GET_COUNT;
3666 	int ret;
3667 
3668 	ret = hidpp_send_fap_command_sync(hidpp, feature_index, cmd, NULL, 0,
3669 					  &response);
3670 	if (ret > 0)
3671 		return -EPROTO;
3672 	if (ret)
3673 		return ret;
3674 
3675 	return response.fap.params[0];
3676 }
3677 
3678 static int hidpp20_reprog_controls_get_cid_info(struct hidpp_device *hidpp,
3679 						u8 index, u16 *control,
3680 						u8 *flags)
3681 {
3682 	struct hidpp_report response;
3683 	u8 feature_index = hidpp->reprog_controls_feature_index;
3684 	u8 cmd = HIDPP_REPROG_CONTROLS_GET_CID_INFO;
3685 	int ret;
3686 
3687 	ret = hidpp_send_fap_command_sync(hidpp, feature_index, cmd, &index,
3688 					  sizeof(index), &response);
3689 	if (ret > 0)
3690 		return -EPROTO;
3691 	if (ret)
3692 		return ret;
3693 
3694 	*control = get_unaligned_be16(&response.fap.params[0]);
3695 	*flags = response.fap.params[4];
3696 
3697 	return 0;
3698 }
3699 
3700 static bool hidpp20_reprog_controls_find_control(struct hidpp_device *hidpp,
3701 						 u16 control)
3702 {
3703 	int count, ret;
3704 	u16 cid;
3705 	u8 flags;
3706 	int i;
3707 
3708 	count = hidpp20_reprog_controls_get_count(hidpp);
3709 	if (count < 0)
3710 		return false;
3711 
3712 	for (i = 0; i < count; i++) {
3713 		ret = hidpp20_reprog_controls_get_cid_info(hidpp, i, &cid,
3714 							   &flags);
3715 		if (ret)
3716 			return false;
3717 
3718 		if (cid == control)
3719 			return (flags & HIDPP_REPROG_CONTROLS_FLAG_MOUSE) &&
3720 			       (flags & HIDPP_REPROG_CONTROLS_FLAG_DIVERT);
3721 	}
3722 
3723 	return false;
3724 }
3725 
3726 static int hidpp20_reprog_controls_set_control_reporting(struct hidpp_device *hidpp,
3727 							 u16 control, u8 flags)
3728 {
3729 	struct hidpp_report response;
3730 	u8 params[5];
3731 
3732 	put_unaligned_be16(control, &params[0]);
3733 	params[2] = flags;
3734 	put_unaligned_be16(control, &params[3]);
3735 
3736 	return hidpp_send_fap_command_sync(hidpp,
3737 					   hidpp->reprog_controls_feature_index,
3738 					   HIDPP_REPROG_CONTROLS_SET_CONTROL_REPORTING,
3739 					   params, sizeof(params), &response);
3740 }
3741 
3742 static void hidpp20_reprog_controls_connect(struct hidpp_device *hidpp)
3743 {
3744 	const struct hidpp_reprog_control_mapping *mapping;
3745 	u8 flags = HIDPP_REPROG_CONTROLS_TEMPORARY_DIVERTED |
3746 		   HIDPP_REPROG_CONTROLS_CHANGE_TEMPORARY_DIVERT;
3747 
3748 	if (!(hidpp->quirks & HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS))
3749 		return;
3750 
3751 	if (!hidpp->reprog_controls)
3752 		return;
3753 
3754 	if (hidpp_root_get_feature(hidpp, HIDPP_PAGE_REPROG_CONTROLS_V4,
3755 				   &hidpp->reprog_controls_feature_index))
3756 		return;
3757 
3758 	for (mapping = hidpp->reprog_controls; mapping->control; mapping++) {
3759 		if (!hidpp20_reprog_controls_find_control(hidpp, mapping->control))
3760 			continue;
3761 
3762 		hidpp20_reprog_controls_set_control_reporting(hidpp,
3763 							      mapping->control,
3764 							      flags);
3765 	}
3766 }
3767 
3768 static int hidpp20_reprog_controls_raw_event(struct hidpp_device *hidpp,
3769 					     u8 *data, int size)
3770 {
3771 	const struct hidpp_reprog_control_mapping *mapping;
3772 	struct hidpp_report *report = (struct hidpp_report *)data;
3773 	u16 controls[4];
3774 	bool pressed;
3775 	unsigned int i, j;
3776 
3777 	if (!(hidpp->quirks & HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS) ||
3778 	    !hidpp->input ||
3779 	    !hidpp->reprog_controls ||
3780 	    hidpp->reprog_controls_feature_index == 0xff)
3781 		return 0;
3782 
3783 	if (size < HIDPP_REPORT_LONG_LENGTH ||
3784 	    report->fap.feature_index != hidpp->reprog_controls_feature_index ||
3785 	    report->fap.funcindex_clientid != HIDPP_REPROG_CONTROLS_EVENT_DIVERTED)
3786 		return 0;
3787 
3788 	for (i = 0; i < ARRAY_SIZE(controls); i++)
3789 		controls[i] = get_unaligned_be16(&report->fap.params[i * 2]);
3790 
3791 	for (mapping = hidpp->reprog_controls; mapping->control; mapping++) {
3792 		pressed = false;
3793 
3794 		for (j = 0; j < ARRAY_SIZE(controls); j++) {
3795 			if (controls[j] == mapping->control) {
3796 				pressed = true;
3797 				break;
3798 			}
3799 		}
3800 
3801 		input_report_key(hidpp->input, mapping->code, pressed);
3802 	}
3803 
3804 	input_sync(hidpp->input);
3805 
3806 	return 1;
3807 }
3808 
3809 static void hidpp20_reprog_controls_populate_input(struct hidpp_device *hidpp,
3810 						   struct input_dev *input_dev)
3811 {
3812 	const struct hidpp_reprog_control_mapping *mapping;
3813 
3814 	if (!hidpp->reprog_controls)
3815 		return;
3816 
3817 	for (mapping = hidpp->reprog_controls; mapping->control; mapping++)
3818 		input_set_capability(input_dev, EV_KEY, mapping->code);
3819 }
3820 
3821 static void hidpp10_extra_mouse_buttons_populate_input(
3822 			struct hidpp_device *hidpp, struct input_dev *input_dev)
3823 {
3824 	/* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3825 	__set_bit(BTN_0, input_dev->keybit);
3826 	__set_bit(BTN_1, input_dev->keybit);
3827 	__set_bit(BTN_2, input_dev->keybit);
3828 	__set_bit(BTN_3, input_dev->keybit);
3829 	__set_bit(BTN_4, input_dev->keybit);
3830 	__set_bit(BTN_5, input_dev->keybit);
3831 	__set_bit(BTN_6, input_dev->keybit);
3832 	__set_bit(BTN_7, input_dev->keybit);
3833 }
3834 
3835 /* -------------------------------------------------------------------------- */
3836 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3837 /* -------------------------------------------------------------------------- */
3838 
3839 /* Find the consumer-page input report desc and change Maximums to 0x107f */
3840 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3841 					      u8 *_rdesc, unsigned int *rsize)
3842 {
3843 	/* Note 0 terminated so we can use strnstr to search for this. */
3844 	static const char consumer_rdesc_start[] = {
3845 		0x05, 0x0C,	/* USAGE_PAGE (Consumer Devices)       */
3846 		0x09, 0x01,	/* USAGE (Consumer Control)            */
3847 		0xA1, 0x01,	/* COLLECTION (Application)            */
3848 		0x85, 0x03,	/* REPORT_ID = 3                       */
3849 		0x75, 0x10,	/* REPORT_SIZE (16)                    */
3850 		0x95, 0x02,	/* REPORT_COUNT (2)                    */
3851 		0x15, 0x01,	/* LOGICAL_MIN (1)                     */
3852 		0x26, 0x00	/* LOGICAL_MAX (...                    */
3853 	};
3854 	char *consumer_rdesc, *rdesc = (char *)_rdesc;
3855 	unsigned int size;
3856 
3857 	consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3858 	size = *rsize - (consumer_rdesc - rdesc);
3859 	if (consumer_rdesc && size >= 25) {
3860 		consumer_rdesc[15] = 0x7f;
3861 		consumer_rdesc[16] = 0x10;
3862 		consumer_rdesc[20] = 0x7f;
3863 		consumer_rdesc[21] = 0x10;
3864 	}
3865 	return _rdesc;
3866 }
3867 
3868 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3869 {
3870 	return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3871 				    HIDPP_ENABLE_CONSUMER_REPORT,
3872 				    HIDPP_ENABLE_CONSUMER_REPORT);
3873 }
3874 
3875 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3876 					   u8 *data, int size)
3877 {
3878 	u8 consumer_report[5];
3879 
3880 	if (size < 7)
3881 		return 0;
3882 
3883 	if (data[0] != REPORT_ID_HIDPP_SHORT ||
3884 	    data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3885 		return 0;
3886 
3887 	/*
3888 	 * Build a normal consumer report (3) out of the data, this detour
3889 	 * is necessary to get some keyboards to report their 0x10xx usages.
3890 	 */
3891 	consumer_report[0] = 0x03;
3892 	memcpy(&consumer_report[1], &data[3], 4);
3893 	/* We are called from atomic context */
3894 	hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3895 			     consumer_report, sizeof(consumer_report), 5, 1);
3896 
3897 	return 1;
3898 }
3899 
3900 /* -------------------------------------------------------------------------- */
3901 /* High-resolution scroll wheels                                              */
3902 /* -------------------------------------------------------------------------- */
3903 
3904 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3905 {
3906 	int ret;
3907 	u8 multiplier = 1;
3908 
3909 	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
3910 		ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3911 		if (ret == 0)
3912 			ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3913 	} else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) {
3914 		ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3915 							   &multiplier);
3916 	} else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ {
3917 		ret = hidpp10_enable_scrolling_acceleration(hidpp);
3918 		multiplier = 8;
3919 	}
3920 	if (ret) {
3921 		hid_dbg(hidpp->hid_dev,
3922 			"Could not enable hi-res scrolling: %d\n", ret);
3923 		return ret;
3924 	}
3925 
3926 	if (multiplier == 0) {
3927 		hid_dbg(hidpp->hid_dev,
3928 			"Invalid multiplier 0 from device, setting it to 1\n");
3929 		multiplier = 1;
3930 	}
3931 
3932 	hidpp->hires_wheel_multiplier = multiplier;
3933 	hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3934 	hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3935 	return 0;
3936 }
3937 
3938 static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp)
3939 {
3940 	int ret;
3941 	unsigned long capabilities;
3942 
3943 	hidpp->hires_wheel_feature_index = 0xff;
3944 	capabilities = hidpp->capabilities;
3945 
3946 	if (hidpp->protocol_major >= 2) {
3947 		u8 feature_index;
3948 
3949 		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
3950 					     &feature_index);
3951 		if (!ret) {
3952 			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL;
3953 			hidpp->hires_wheel_feature_index = feature_index;
3954 			hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n");
3955 			return 0;
3956 		}
3957 		ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
3958 					     &feature_index);
3959 		if (!ret) {
3960 			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL;
3961 			hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n");
3962 		}
3963 	} else {
3964 		/* We cannot detect fast scrolling support on HID++ 1.0 devices */
3965 		if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) {
3966 			hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL;
3967 			hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n");
3968 		}
3969 	}
3970 
3971 	if (hidpp->capabilities == capabilities)
3972 		hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n");
3973 	return 0;
3974 }
3975 
3976 static int hidpp20_hires_wheel_raw_event(struct hidpp_device *hidpp,
3977 						u8 *data, int size)
3978 {
3979 	if (hidpp->hires_wheel_feature_index == 0xff)
3980 		return 0;
3981 
3982 	if (size < 5)
3983 		return 0;
3984 
3985 	if (data[0] != REPORT_ID_HIDPP_LONG ||
3986 	    data[2] != hidpp->hires_wheel_feature_index)
3987 		return 0;
3988 
3989 	if ((data[3] & 0xf0) == CMD_HIRES_WHEEL_SET_WHEEL_MODE) {
3990 		u8 mode = data[4];
3991 		bool hires = (mode & 0x02) != 0;
3992 		int new_multiplier = (hires && hidpp->hires_wheel_multiplier > 0)
3993 			? hidpp->hires_wheel_multiplier : 1;
3994 		hidpp->vertical_wheel_counter.wheel_multiplier = new_multiplier;
3995 		return 1;
3996 	}
3997 
3998 	return 0;
3999 }
4000 
4001 /* -------------------------------------------------------------------------- */
4002 /* Generic HID++ devices                                                      */
4003 /* -------------------------------------------------------------------------- */
4004 
4005 static const u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
4006 				    unsigned int *rsize)
4007 {
4008 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4009 
4010 	if (!hidpp)
4011 		return rdesc;
4012 
4013 	/* For 27 MHz keyboards the quirk gets set after hid_parse. */
4014 	if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
4015 	    (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
4016 		rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
4017 
4018 	return rdesc;
4019 }
4020 
4021 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
4022 		struct hid_field *field, struct hid_usage *usage,
4023 		unsigned long **bit, int *max)
4024 {
4025 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4026 
4027 	if (!hidpp)
4028 		return 0;
4029 
4030 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
4031 		return wtp_input_mapping(hdev, hi, field, usage, bit, max);
4032 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
4033 			field->application != HID_GD_MOUSE)
4034 		return m560_input_mapping(hdev, hi, field, usage, bit, max);
4035 
4036 	if (hdev->product == DINOVO_MINI_PRODUCT_ID)
4037 		return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
4038 
4039 	return 0;
4040 }
4041 
4042 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
4043 		struct hid_field *field, struct hid_usage *usage,
4044 		unsigned long **bit, int *max)
4045 {
4046 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4047 
4048 	if (!hidpp)
4049 		return 0;
4050 
4051 	/* Ensure that Logitech G920 is not given a default fuzz/flat value */
4052 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4053 		if (usage->type == EV_ABS && (usage->code == ABS_X ||
4054 				usage->code == ABS_Y || usage->code == ABS_Z ||
4055 				usage->code == ABS_RZ)) {
4056 			field->application = HID_GD_MULTIAXIS;
4057 		}
4058 	}
4059 
4060 	return 0;
4061 }
4062 
4063 
4064 static void hidpp_populate_input(struct hidpp_device *hidpp,
4065 				 struct input_dev *input)
4066 {
4067 	hidpp->input = input;
4068 
4069 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
4070 		wtp_populate_input(hidpp, input);
4071 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
4072 		m560_populate_input(hidpp, input);
4073 
4074 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
4075 		hidpp10_wheel_populate_input(hidpp, input);
4076 
4077 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
4078 		hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
4079 
4080 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS)
4081 		hidpp20_reprog_controls_populate_input(hidpp, input);
4082 }
4083 
4084 static int hidpp_input_configured(struct hid_device *hdev, struct hid_input *hidinput)
4085 {
4086 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4087 	struct input_dev *input = hidinput->input;
4088 	int ret;
4089 
4090 	if (!hidpp)
4091 		return 0;
4092 
4093 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4094 		struct hidpp_ff_private_data data;
4095 
4096 		if (!list_is_first(&hidinput->list, &hdev->inputs))
4097 			return 0;
4098 
4099 		ret = g920_get_config(hidpp, &data);
4100 		if (!ret)
4101 			ret = hidpp_ff_init(hidpp, &data);
4102 
4103 		if (ret) {
4104 			hid_warn(hidpp->hid_dev,
4105 				 "Unable to initialize force feedback support, errno %d\n",
4106 				 ret);
4107 		}
4108 	}
4109 
4110 	hidpp_populate_input(hidpp, input);
4111 
4112 	return 0;
4113 }
4114 
4115 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
4116 		int size)
4117 {
4118 	struct hidpp_report *question, *answer;
4119 	struct hidpp_report *report = (struct hidpp_report *)data;
4120 	int ret;
4121 	int last_online;
4122 
4123 	/*
4124 	 * If the mutex is locked then we have a pending answer from a
4125 	 * previously sent command.
4126 	 */
4127 	if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
4128 		question = hidpp->send_receive_buf;
4129 		answer = hidpp->send_receive_buf;
4130 
4131 		if (!question)
4132 			return 0;
4133 
4134 		/*
4135 		 * Check for a correct hidpp20 answer or the corresponding
4136 		 * error
4137 		 */
4138 		if (hidpp_match_answer(question, report) ||
4139 				hidpp_match_error(question, report)) {
4140 			*answer = *report;
4141 			hidpp->answer_available = true;
4142 			wake_up(&hidpp->wait);
4143 			/*
4144 			 * This was an answer to a command that this driver sent
4145 			 * We return 1 to hid-core to avoid forwarding the
4146 			 * command upstream as it has been treated by the driver
4147 			 */
4148 
4149 			return 1;
4150 		}
4151 	}
4152 
4153 	if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
4154 		if (schedule_work(&hidpp->work) == 0)
4155 			dbg_hid("%s: connect event already queued\n", __func__);
4156 		return 1;
4157 	}
4158 
4159 	if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4160 	    data[0] == REPORT_ID_HIDPP_SHORT &&
4161 	    data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
4162 	    (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
4163 		dev_err_ratelimited(&hidpp->hid_dev->dev,
4164 			"Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
4165 		dev_err_ratelimited(&hidpp->hid_dev->dev,
4166 			"See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
4167 	}
4168 
4169 	last_online = hidpp->battery.online;
4170 	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
4171 		ret = hidpp20_battery_event_1000(hidpp, data, size);
4172 		if (ret != 0)
4173 			return ret;
4174 		ret = hidpp20_battery_event_1004(hidpp, data, size);
4175 		if (ret != 0)
4176 			return ret;
4177 		ret = hidpp_solar_battery_event(hidpp, data, size);
4178 		if (ret != 0)
4179 			return ret;
4180 		ret = hidpp20_battery_voltage_event(hidpp, data, size);
4181 		if (ret != 0)
4182 			return ret;
4183 		ret = hidpp20_adc_measurement_event_1f20(hidpp, data, size);
4184 		if (ret != 0)
4185 			return ret;
4186 	}
4187 
4188 	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
4189 		ret = hidpp10_battery_event(hidpp, data, size);
4190 		if (ret != 0)
4191 			return ret;
4192 	}
4193 
4194 	if (hidpp->quirks & HIDPP_QUIRK_RESET_HI_RES_SCROLL) {
4195 		if (last_online == 0 && hidpp->battery.online == 1)
4196 			schedule_work(&hidpp->reset_hi_res_work);
4197 	}
4198 
4199 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
4200 		ret = hidpp10_wheel_raw_event(hidpp, data, size);
4201 		if (ret != 0)
4202 			return ret;
4203 	}
4204 
4205 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
4206 		ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
4207 		if (ret != 0)
4208 			return ret;
4209 	}
4210 
4211 	ret = hidpp20_reprog_controls_raw_event(hidpp, data, size);
4212 	if (ret != 0)
4213 		return ret;
4214 
4215 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
4216 		ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
4217 		if (ret != 0)
4218 			return ret;
4219 	}
4220 
4221 	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) {
4222 		ret = hidpp20_hires_wheel_raw_event(hidpp, data, size);
4223 		if (ret != 0)
4224 			return ret;
4225 	}
4226 
4227 	return 0;
4228 }
4229 
4230 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
4231 		u8 *data, int size)
4232 {
4233 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4234 	int ret = 0;
4235 
4236 	if (!hidpp)
4237 		return 0;
4238 
4239 	/* Generic HID++ processing. */
4240 	switch (data[0]) {
4241 	case REPORT_ID_HIDPP_VERY_LONG:
4242 		if (size != hidpp->very_long_report_length) {
4243 			hid_err(hdev, "received hid++ report of bad size (%d)",
4244 				size);
4245 			return 1;
4246 		}
4247 		ret = hidpp_raw_hidpp_event(hidpp, data, size);
4248 		break;
4249 	case REPORT_ID_HIDPP_LONG:
4250 		if (size != HIDPP_REPORT_LONG_LENGTH) {
4251 			hid_err(hdev, "received hid++ report of bad size (%d)",
4252 				size);
4253 			return 1;
4254 		}
4255 		ret = hidpp_raw_hidpp_event(hidpp, data, size);
4256 		break;
4257 	case REPORT_ID_HIDPP_SHORT:
4258 		if (size != HIDPP_REPORT_SHORT_LENGTH) {
4259 			hid_err(hdev, "received hid++ report of bad size (%d)",
4260 				size);
4261 			return 1;
4262 		}
4263 		ret = hidpp_raw_hidpp_event(hidpp, data, size);
4264 		break;
4265 	}
4266 
4267 	/* If no report is available for further processing, skip calling
4268 	 * raw_event of subclasses. */
4269 	if (ret != 0)
4270 		return ret;
4271 
4272 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
4273 		return wtp_raw_event(hdev, data, size);
4274 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
4275 		return m560_raw_event(hdev, data, size);
4276 
4277 	return 0;
4278 }
4279 
4280 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
4281 	struct hid_usage *usage, __s32 value)
4282 {
4283 	/* This function will only be called for scroll events, due to the
4284 	 * restriction imposed in hidpp_usages.
4285 	 */
4286 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4287 	struct hidpp_scroll_counter *counter;
4288 
4289 	if (!hidpp)
4290 		return 0;
4291 
4292 	counter = &hidpp->vertical_wheel_counter;
4293 	/* A scroll event may occur before the multiplier has been retrieved or
4294 	 * the input device set, or high-res scroll enabling may fail. In such
4295 	 * cases we must return early (falling back to default behaviour) to
4296 	 * avoid a crash in hidpp_scroll_counter_handle_scroll.
4297 	 */
4298 	if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4299 	    || value == 0 || hidpp->input == NULL
4300 	    || counter->wheel_multiplier == 0)
4301 		return 0;
4302 
4303 	hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
4304 	return 1;
4305 }
4306 
4307 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
4308 {
4309 	static atomic_t battery_no = ATOMIC_INIT(0);
4310 	struct power_supply_config cfg = { .drv_data = hidpp };
4311 	struct power_supply_desc *desc = &hidpp->battery.desc;
4312 	enum power_supply_property *battery_props;
4313 	struct hidpp_battery *battery;
4314 	unsigned int num_battery_props;
4315 	unsigned long n;
4316 	int ret;
4317 
4318 	if (hidpp->battery.ps)
4319 		return 0;
4320 
4321 	hidpp->battery.feature_index = 0xff;
4322 	hidpp->battery.solar_feature_index = 0xff;
4323 	hidpp->battery.voltage_feature_index = 0xff;
4324 	hidpp->battery.adc_measurement_feature_index = 0xff;
4325 
4326 	if (hidpp->protocol_major >= 2) {
4327 		if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
4328 			ret = hidpp_solar_request_battery_event(hidpp);
4329 		else {
4330 			/* we only support one battery feature right now, so let's
4331 			   first check the ones that support battery level first
4332 			   and leave voltage for last */
4333 			ret = hidpp20_query_battery_info_1000(hidpp);
4334 			if (ret)
4335 				ret = hidpp20_query_battery_info_1004(hidpp);
4336 			if (ret)
4337 				ret = hidpp20_query_battery_voltage_info(hidpp);
4338 			if (ret)
4339 				ret = hidpp20_query_adc_measurement_info_1f20(hidpp);
4340 		}
4341 
4342 		if (ret)
4343 			return ret;
4344 		hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
4345 	} else {
4346 		ret = hidpp10_query_battery_status(hidpp);
4347 		if (ret) {
4348 			ret = hidpp10_query_battery_mileage(hidpp);
4349 			if (ret)
4350 				return -ENOENT;
4351 			hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
4352 		} else {
4353 			hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
4354 		}
4355 		hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
4356 	}
4357 
4358 	battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
4359 				     hidpp_battery_props,
4360 				     sizeof(hidpp_battery_props),
4361 				     GFP_KERNEL);
4362 	if (!battery_props)
4363 		return -ENOMEM;
4364 
4365 	num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
4366 
4367 	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
4368 	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE ||
4369 	    hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4370 	    hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4371 		battery_props[num_battery_props++] =
4372 				POWER_SUPPLY_PROP_CAPACITY;
4373 
4374 	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
4375 		battery_props[num_battery_props++] =
4376 				POWER_SUPPLY_PROP_CAPACITY_LEVEL;
4377 
4378 	if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE ||
4379 	    hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4380 		battery_props[num_battery_props++] =
4381 			POWER_SUPPLY_PROP_VOLTAGE_NOW;
4382 
4383 	battery = &hidpp->battery;
4384 
4385 	n = atomic_inc_return(&battery_no) - 1;
4386 	desc->properties = battery_props;
4387 	desc->num_properties = num_battery_props;
4388 	desc->get_property = hidpp_battery_get_property;
4389 	sprintf(battery->name, "hidpp_battery_%ld", n);
4390 	desc->name = battery->name;
4391 	desc->type = POWER_SUPPLY_TYPE_BATTERY;
4392 	desc->use_for_apm = 0;
4393 
4394 	battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
4395 						 &battery->desc,
4396 						 &cfg);
4397 	if (IS_ERR(battery->ps))
4398 		return PTR_ERR(battery->ps);
4399 
4400 	power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
4401 
4402 	return ret;
4403 }
4404 
4405 /* Get name + serial for USB and Bluetooth HID++ devices */
4406 static void hidpp_non_unifying_init(struct hidpp_device *hidpp)
4407 {
4408 	struct hid_device *hdev = hidpp->hid_dev;
4409 	char *name;
4410 
4411 	/* Bluetooth devices already have their serialnr set */
4412 	if (hid_is_usb(hdev))
4413 		hidpp_serial_init(hidpp);
4414 
4415 	name = hidpp_get_device_name(hidpp);
4416 	if (name) {
4417 		dbg_hid("HID++: Got name: %s\n", name);
4418 		snprintf(hdev->name, sizeof(hdev->name), "%s", name);
4419 		kfree(name);
4420 	}
4421 }
4422 
4423 static int hidpp_input_open(struct input_dev *dev)
4424 {
4425 	struct hid_device *hid = input_get_drvdata(dev);
4426 
4427 	return hid_hw_open(hid);
4428 }
4429 
4430 static void hidpp_input_close(struct input_dev *dev)
4431 {
4432 	struct hid_device *hid = input_get_drvdata(dev);
4433 
4434 	hid_hw_close(hid);
4435 }
4436 
4437 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
4438 {
4439 	struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
4440 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4441 
4442 	if (!input_dev)
4443 		return NULL;
4444 
4445 	input_set_drvdata(input_dev, hdev);
4446 	input_dev->open = hidpp_input_open;
4447 	input_dev->close = hidpp_input_close;
4448 
4449 	input_dev->name = hidpp->name;
4450 	input_dev->phys = hdev->phys;
4451 	input_dev->uniq = hdev->uniq;
4452 	input_dev->id.bustype = hdev->bus;
4453 	input_dev->id.vendor  = hdev->vendor;
4454 	input_dev->id.product = hdev->product;
4455 	input_dev->id.version = hdev->version;
4456 	input_dev->dev.parent = &hdev->dev;
4457 
4458 	return input_dev;
4459 }
4460 
4461 static void hidpp_connect_event(struct work_struct *work)
4462 {
4463 	struct hidpp_device *hidpp = container_of(work, struct hidpp_device, work);
4464 	struct hid_device *hdev = hidpp->hid_dev;
4465 	struct input_dev *input;
4466 	char *name, *devm_name;
4467 	int ret;
4468 
4469 	/* Get device version to check if it is connected */
4470 	ret = hidpp_root_get_protocol_version(hidpp);
4471 	if (ret) {
4472 		hid_dbg(hidpp->hid_dev, "Disconnected\n");
4473 		if (hidpp->battery.ps) {
4474 			hidpp->battery.online = false;
4475 			hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
4476 			hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
4477 			power_supply_changed(hidpp->battery.ps);
4478 		}
4479 		return;
4480 	}
4481 
4482 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4483 		ret = wtp_connect(hdev);
4484 		if (ret)
4485 			return;
4486 	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
4487 		ret = m560_send_config_command(hdev);
4488 		if (ret)
4489 			return;
4490 	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4491 		ret = k400_connect(hdev);
4492 		if (ret)
4493 			return;
4494 	}
4495 
4496 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
4497 		ret = hidpp10_wheel_connect(hidpp);
4498 		if (ret)
4499 			return;
4500 	}
4501 
4502 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
4503 		ret = hidpp10_extra_mouse_buttons_connect(hidpp);
4504 		if (ret)
4505 			return;
4506 	}
4507 
4508 	hidpp20_reprog_controls_connect(hidpp);
4509 
4510 	if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
4511 		ret = hidpp10_consumer_keys_connect(hidpp);
4512 		if (ret)
4513 			return;
4514 	}
4515 
4516 	if (hidpp->protocol_major >= 2) {
4517 		u8 feature_index;
4518 
4519 		if (!hidpp_get_wireless_feature_index(hidpp, &feature_index))
4520 			hidpp->wireless_feature_index = feature_index;
4521 	}
4522 
4523 	if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
4524 		name = hidpp_get_device_name(hidpp);
4525 		if (name) {
4526 			devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
4527 						   "%s", name);
4528 			kfree(name);
4529 			if (!devm_name)
4530 				return;
4531 
4532 			hidpp->name = devm_name;
4533 		}
4534 	}
4535 
4536 	hidpp_initialize_battery(hidpp);
4537 	if (!hid_is_usb(hidpp->hid_dev))
4538 		hidpp_initialize_hires_scroll(hidpp);
4539 
4540 	/* forward current battery state */
4541 	if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
4542 		hidpp10_enable_battery_reporting(hidpp);
4543 		if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
4544 			hidpp10_query_battery_mileage(hidpp);
4545 		else
4546 			hidpp10_query_battery_status(hidpp);
4547 	} else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
4548 		if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
4549 			hidpp20_query_battery_voltage_info(hidpp);
4550 		else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
4551 			hidpp20_query_battery_info_1004(hidpp);
4552 		else if (hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT)
4553 			hidpp20_query_adc_measurement_info_1f20(hidpp);
4554 		else
4555 			hidpp20_query_battery_info_1000(hidpp);
4556 	}
4557 	if (hidpp->battery.ps)
4558 		power_supply_changed(hidpp->battery.ps);
4559 
4560 	if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL)
4561 		hi_res_scroll_enable(hidpp);
4562 
4563 	if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input)
4564 		/* if the input nodes are already created, we can stop now */
4565 		return;
4566 
4567 	input = hidpp_allocate_input(hdev);
4568 	if (!input) {
4569 		hid_err(hdev, "cannot allocate new input device: %d\n", ret);
4570 		return;
4571 	}
4572 
4573 	hidpp_populate_input(hidpp, input);
4574 
4575 	ret = input_register_device(input);
4576 	if (ret) {
4577 		hidpp->input = NULL;
4578 		input_free_device(input);
4579 		return;
4580 	}
4581 
4582 	hidpp->delayed_input = input;
4583 }
4584 
4585 static void hidpp_reset_hi_res_handler(struct work_struct *work)
4586 {
4587 	struct hidpp_device *hidpp = container_of(work, struct hidpp_device, reset_hi_res_work);
4588 
4589 	hi_res_scroll_enable(hidpp);
4590 }
4591 
4592 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
4593 
4594 static struct attribute *sysfs_attrs[] = {
4595 	&dev_attr_builtin_power_supply.attr,
4596 	NULL
4597 };
4598 
4599 static const struct attribute_group ps_attribute_group = {
4600 	.attrs = sysfs_attrs
4601 };
4602 
4603 static int hidpp_get_report_length(struct hid_device *hdev, int id)
4604 {
4605 	struct hid_report_enum *re;
4606 	struct hid_report *report;
4607 
4608 	re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
4609 	report = re->report_id_hash[id];
4610 	if (!report || !report->maxfield)
4611 		return 0;
4612 
4613 	return report->field[0]->report_count + 1;
4614 }
4615 
4616 static u8 hidpp_validate_device(struct hid_device *hdev)
4617 {
4618 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4619 	int id, report_length;
4620 	u8 supported_reports = 0;
4621 
4622 	id = REPORT_ID_HIDPP_SHORT;
4623 	report_length = hidpp_get_report_length(hdev, id);
4624 	if (report_length) {
4625 		if (report_length < HIDPP_REPORT_SHORT_LENGTH)
4626 			goto bad_device;
4627 
4628 		supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
4629 	}
4630 
4631 	id = REPORT_ID_HIDPP_LONG;
4632 	report_length = hidpp_get_report_length(hdev, id);
4633 	if (report_length) {
4634 		if (report_length < HIDPP_REPORT_LONG_LENGTH)
4635 			goto bad_device;
4636 
4637 		supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
4638 	}
4639 
4640 	id = REPORT_ID_HIDPP_VERY_LONG;
4641 	report_length = hidpp_get_report_length(hdev, id);
4642 	if (report_length) {
4643 		if (report_length < HIDPP_REPORT_LONG_LENGTH ||
4644 		    report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
4645 			goto bad_device;
4646 
4647 		supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
4648 		hidpp->very_long_report_length = report_length;
4649 	}
4650 
4651 	return supported_reports;
4652 
4653 bad_device:
4654 	hid_warn(hdev, "not enough values in hidpp report %d\n", id);
4655 	return false;
4656 }
4657 
4658 static bool hidpp_application_equals(struct hid_device *hdev,
4659 				     unsigned int application)
4660 {
4661 	struct list_head *report_list;
4662 	struct hid_report *report;
4663 
4664 	report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4665 	report = list_first_entry_or_null(report_list, struct hid_report, list);
4666 	return report && report->application == application;
4667 }
4668 
4669 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4670 {
4671 	struct hidpp_device *hidpp;
4672 	int ret;
4673 	unsigned int connect_mask = HID_CONNECT_DEFAULT;
4674 
4675 	/* report_fixup needs drvdata to be set before we call hid_parse */
4676 	hidpp = devm_kzalloc_obj(&hdev->dev, *hidpp);
4677 	if (!hidpp)
4678 		return -ENOMEM;
4679 
4680 	hidpp->hid_dev = hdev;
4681 	hidpp->name = hdev->name;
4682 	hidpp->quirks = id->driver_data;
4683 	hidpp->reprog_controls_feature_index = 0xff;
4684 	hidpp->reprog_controls = hidpp20_reprog_controls_get_mappings(hidpp);
4685 	hid_set_drvdata(hdev, hidpp);
4686 
4687 	ret = hid_parse(hdev);
4688 	if (ret) {
4689 		hid_err(hdev, "%s:parse failed\n", __func__);
4690 		return ret;
4691 	}
4692 
4693 	/*
4694 	 * Make sure the device is HID++ capable, otherwise treat as generic HID
4695 	 */
4696 	hidpp->supported_reports = hidpp_validate_device(hdev);
4697 
4698 	if (!hidpp->supported_reports) {
4699 		hid_set_drvdata(hdev, NULL);
4700 		devm_kfree(&hdev->dev, hidpp);
4701 		return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4702 	}
4703 
4704 	if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4705 	    hidpp_application_equals(hdev, HID_GD_MOUSE))
4706 		hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4707 				 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4708 
4709 	if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4710 	    hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4711 		hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4712 
4713 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4714 		ret = wtp_allocate(hdev, id);
4715 		if (ret)
4716 			return ret;
4717 	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4718 		ret = k400_allocate(hdev);
4719 		if (ret)
4720 			return ret;
4721 	}
4722 
4723 	INIT_WORK(&hidpp->work, hidpp_connect_event);
4724 	INIT_WORK(&hidpp->reset_hi_res_work, hidpp_reset_hi_res_handler);
4725 	mutex_init(&hidpp->send_mutex);
4726 	init_waitqueue_head(&hidpp->wait);
4727 
4728 	/* indicates we are handling the battery properties in the kernel */
4729 	ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4730 	if (ret)
4731 		hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4732 			 hdev->name);
4733 
4734 	/*
4735 	 * First call hid_hw_start(hdev, 0) to allow IO without connecting any
4736 	 * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's
4737 	 * name and serial number and store these in hdev->name and hdev->uniq,
4738 	 * before the hid-input and hidraw drivers expose these to userspace.
4739 	 */
4740 	ret = hid_hw_start(hdev, 0);
4741 	if (ret) {
4742 		hid_err(hdev, "hw start failed\n");
4743 		goto hid_hw_start_fail;
4744 	}
4745 
4746 	ret = hid_hw_open(hdev);
4747 	if (ret < 0) {
4748 		dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4749 			__func__, ret);
4750 		goto hid_hw_open_fail;
4751 	}
4752 
4753 	/* Allow incoming packets */
4754 	hid_device_io_start(hdev);
4755 
4756 	/* Get name + serial, store in hdev->name + hdev->uniq */
4757 	if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4758 		hidpp_unifying_init(hidpp);
4759 	else
4760 		hidpp_non_unifying_init(hidpp);
4761 
4762 	if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
4763 		connect_mask &= ~HID_CONNECT_HIDINPUT;
4764 
4765 	/* Now export the actual inputs and hidraw nodes to the world */
4766 	hid_device_io_stop(hdev);
4767 	ret = hid_connect(hdev, connect_mask);
4768 	if (ret) {
4769 		hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret);
4770 		goto hid_hw_init_fail;
4771 	}
4772 
4773 	/* Check for connected devices now that incoming packets will not be disabled again */
4774 	hid_device_io_start(hdev);
4775 	schedule_work(&hidpp->work);
4776 	flush_work(&hidpp->work);
4777 
4778 	/*
4779 	 * This relies on logi_dj_ll_close() being a no-op so that DJ connection
4780 	 * events will still be received.
4781 	 */
4782 	hid_hw_close(hdev);
4783 	return ret;
4784 
4785 hid_hw_init_fail:
4786 	hid_hw_close(hdev);
4787 hid_hw_open_fail:
4788 	hid_hw_stop(hdev);
4789 hid_hw_start_fail:
4790 	sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4791 	cancel_work_sync(&hidpp->work);
4792 	cancel_work_sync(&hidpp->reset_hi_res_work);
4793 	mutex_destroy(&hidpp->send_mutex);
4794 	return ret;
4795 }
4796 
4797 static void hidpp_remove(struct hid_device *hdev)
4798 {
4799 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4800 
4801 	if (!hidpp)
4802 		return hid_hw_stop(hdev);
4803 
4804 	sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4805 
4806 	hid_hw_stop(hdev);
4807 	cancel_work_sync(&hidpp->work);
4808 	cancel_work_sync(&hidpp->reset_hi_res_work);
4809 	mutex_destroy(&hidpp->send_mutex);
4810 }
4811 
4812 #define LDJ_DEVICE(product) \
4813 	HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4814 		   USB_VENDOR_ID_LOGITECH, (product))
4815 
4816 #define L27MHZ_DEVICE(product) \
4817 	HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4818 		   USB_VENDOR_ID_LOGITECH, (product))
4819 
4820 static const struct hid_device_id hidpp_devices[] = {
4821 	{ /* wireless touchpad */
4822 	  LDJ_DEVICE(0x4011),
4823 	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4824 			 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4825 	{ /* wireless touchpad T650 */
4826 	  LDJ_DEVICE(0x4101),
4827 	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4828 	{ /* wireless touchpad T651 */
4829 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4830 		USB_DEVICE_ID_LOGITECH_T651),
4831 	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4832 	{ /* Mouse Logitech Anywhere MX */
4833 	  LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4834 	{ /* Mouse logitech M560 */
4835 	  LDJ_DEVICE(0x402d),
4836 	  .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
4837 	{ /* Mouse Logitech M705 (firmware RQM17) */
4838 	  LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4839 	{ /* Mouse Logitech Performance MX */
4840 	  LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4841 	{ /* Keyboard logitech K400 */
4842 	  LDJ_DEVICE(0x4024),
4843 	  .driver_data = HIDPP_QUIRK_CLASS_K400 },
4844 	{ /* Solar Keyboard Logitech K750 */
4845 	  LDJ_DEVICE(0x4002),
4846 	  .driver_data = HIDPP_QUIRK_CLASS_K750 },
4847 	{ /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4848 	  LDJ_DEVICE(0xb305),
4849 	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4850 	{ /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4851 	  LDJ_DEVICE(0xb309),
4852 	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4853 	{ /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4854 	  LDJ_DEVICE(0xb30b),
4855 	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4856 	{ /* Logitech G502 Lightspeed Wireless Gaming Mouse */
4857 	  LDJ_DEVICE(0x407f),
4858 	  .driver_data = HIDPP_QUIRK_RESET_HI_RES_SCROLL },
4859 
4860 	{ LDJ_DEVICE(HID_ANY_ID) },
4861 
4862 	{ /* Keyboard LX501 (Y-RR53) */
4863 	  L27MHZ_DEVICE(0x0049),
4864 	  .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4865 	{ /* Keyboard MX3000 (Y-RAM74) */
4866 	  L27MHZ_DEVICE(0x0057),
4867 	  .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4868 	{ /* Keyboard MX3200 (Y-RAV80) */
4869 	  L27MHZ_DEVICE(0x005c),
4870 	  .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4871 	{ /* S510 Media Remote */
4872 	  L27MHZ_DEVICE(0x00fe),
4873 	  .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4874 
4875 	{ L27MHZ_DEVICE(HID_ANY_ID) },
4876 
4877 	{ /* Logitech G403 Wireless Gaming Mouse over USB */
4878 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4879 	{ /* Logitech G502 Lightspeed Wireless Gaming Mouse over USB */
4880 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC08D) },
4881 	{ /* Logitech G502 X Plus Wireless Gaming Mouse over USB */
4882 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC095) },
4883 	{ /* Logitech G502 X Lightspeed Wireless Gaming Mouse over USB */
4884 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC098) },
4885 	{ /* Logitech G703 Gaming Mouse over USB */
4886 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4887 	{ /* Logitech G703 Hero Gaming Mouse over USB */
4888 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4889 	{ /* Logitech G900 Gaming Mouse over USB */
4890 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4891 	{ /* Logitech G903 Gaming Mouse over USB */
4892 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4893 	{ /* Logitech G Pro Gaming Mouse over USB */
4894 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4895 	{ /* MX Vertical over USB */
4896 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC08A) },
4897 	{ /* Logitech G903 Hero Gaming Mouse over USB */
4898 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4899 	{ /* Logitech G915 TKL Keyboard over USB */
4900 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC343) },
4901 	{ /* Logitech G920 Wheel over USB */
4902 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4903 		.driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4904 	{ /* Logitech G923 Wheel (Xbox version) over USB */
4905 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G923_XBOX_WHEEL),
4906 		.driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS },
4907 	{ /* Logitech G Pro X Superlight Gaming Mouse over USB */
4908 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC094) },
4909 	{ /* Logitech G Pro X Superlight 2 Gaming Mouse over USB */
4910 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC09b) },
4911 	{ /* Logitech G PRO 2 LIGHTSPEED Wireless Mouse over USB */
4912 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xc09a) },
4913 
4914 	{ /* G935 Gaming Headset */
4915 	  HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0x0a87),
4916 		.driver_data = HIDPP_QUIRK_WIRELESS_STATUS },
4917 
4918 	{ /* MX5000 keyboard over Bluetooth */
4919 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4920 	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4921 	{ /* Dinovo Edge keyboard over Bluetooth */
4922 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4923 	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4924 	{ /* MX5500 keyboard over Bluetooth */
4925 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4926 	  .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4927 	{ /* Logitech G915 TKL keyboard over Bluetooth */
4928 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb35f) },
4929 	{ /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */
4930 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4931 	{ /* MX Master mouse over Bluetooth */
4932 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) },
4933 	{ /* M720 Triathlon mouse over Bluetooth */
4934 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb015) },
4935 	{ /* MX Master 2S mouse over Bluetooth */
4936 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb019) },
4937 	{ /* MX Ergo trackball over Bluetooth */
4938 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4939 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) },
4940 	{ /* MX Vertical mouse over Bluetooth */
4941 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb020) },
4942 	{ /* Signature M650 over Bluetooth */
4943 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4944 			       HIDPP_PRODUCT_SIGNATURE_M650),
4945 	  .driver_data = HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS },
4946 	{ /* MX Master 3 mouse over Bluetooth */
4947 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) },
4948 	{ /* MX Anywhere 3 mouse over Bluetooth */
4949 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb025) },
4950 	{ /* MX Master 3S mouse over Bluetooth */
4951 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) },
4952 	{ /* MX Anywhere 3S mouse over Bluetooth */
4953 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb037) },
4954 	{ /* MX Anywhere 3SB mouse over Bluetooth */
4955 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb038) },
4956 	{ /* Slim Solar+ K980 Keyboard over Bluetooth */
4957 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb391) },
4958 	{ /* MX Master 4 mouse over Bluetooth */
4959 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb042) },
4960 	{ /* Logitech Signature K650 over Bluetooth */
4961 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb36f) },
4962 	{ /* Logitech Signature K650 B2B over Bluetooth */
4963 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb370) },
4964 	{ /* Logitech Pebble Keys 2 K380S over Bluetooth */
4965 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb377) },
4966 	{ /* Logitech Casa Pop-Up Desk over Bluetooth */
4967 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb371) },
4968 	{ /* Logitech Casa Pop-Up Desk B2B over Bluetooth */
4969 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb374) },
4970 	{ /* Logitech Wave Keys over Bluetooth */
4971 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb383) },
4972 	{ /* Logitech Wave Keys B2B over Bluetooth */
4973 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb384) },
4974 	{ /* Logitech Signature Slim K950 over Bluetooth */
4975 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb386) },
4976 	{ /* Logitech Signature Slim K950 B2B over Bluetooth */
4977 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb388) },
4978 	{ /* Logitech MX Keys S over Bluetooth */
4979 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb378) },
4980 	{ /* Logitech MX Keys S B2B over Bluetooth */
4981 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb380) },
4982 	{ /* Logitech Keys-To-Go 2 over Bluetooth */
4983 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb38c) },
4984 	{ /* Logitech Pop Icon Keys over Bluetooth */
4985 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb38f) },
4986 	{ /* Logitech MX Keys Mini over Bluetooth */
4987 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb369) },
4988 	{ /* Logitech MX Keys Mini B2B over Bluetooth */
4989 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb36e) },
4990 	{ /* Logitech Signature Slim Solar+ K980 B2B over Bluetooth */
4991 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb394) },
4992 	{ /* Logitech Bluetooth Keyboard K250/K251 over Bluetooth */
4993 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb397) },
4994 	{ /* Logitech Signature Comfort K880 over Bluetooth */
4995 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb39c) },
4996 	{ /* Logitech Signature Comfort K880 B2B over Bluetooth */
4997 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb39d) },
4998 	{}
4999 };
5000 
5001 MODULE_DEVICE_TABLE(hid, hidpp_devices);
5002 
5003 static const struct hid_usage_id hidpp_usages[] = {
5004 	{ HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
5005 	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
5006 };
5007 
5008 static struct hid_driver hidpp_driver = {
5009 	.name = "logitech-hidpp-device",
5010 	.id_table = hidpp_devices,
5011 	.report_fixup = hidpp_report_fixup,
5012 	.probe = hidpp_probe,
5013 	.remove = hidpp_remove,
5014 	.raw_event = hidpp_raw_event,
5015 	.usage_table = hidpp_usages,
5016 	.event = hidpp_event,
5017 	.input_configured = hidpp_input_configured,
5018 	.input_mapping = hidpp_input_mapping,
5019 	.input_mapped = hidpp_input_mapped,
5020 };
5021 
5022 module_hid_driver(hidpp_driver);
5023