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