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