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