1 /*
2  *  HIDPP protocol for Logitech Unifying receivers
3  *
4  *  Copyright (c) 2011 Logitech (c)
5  *  Copyright (c) 2012-2013 Google (c)
6  *  Copyright (c) 2013-2014 Red Hat Inc.
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; version 2 of the License.
13  */
14 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/device.h>
18 #include <linux/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/kfifo.h>
23 #include <linux/input/mt.h>
24 #include <asm/unaligned.h>
25 #include "hid-ids.h"
26 
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
30 
31 static bool disable_raw_mode;
32 module_param(disable_raw_mode, bool, 0644);
33 MODULE_PARM_DESC(disable_raw_mode,
34 	"Disable Raw mode reporting for touchpads and keep firmware gestures.");
35 
36 #define REPORT_ID_HIDPP_SHORT			0x10
37 #define REPORT_ID_HIDPP_LONG			0x11
38 
39 #define HIDPP_REPORT_SHORT_LENGTH		7
40 #define HIDPP_REPORT_LONG_LENGTH		20
41 
42 #define HIDPP_QUIRK_CLASS_WTP			BIT(0)
43 #define HIDPP_QUIRK_CLASS_M560			BIT(1)
44 
45 /* bits 2..20 are reserved for classes */
46 #define HIDPP_QUIRK_DELAYED_INIT		BIT(21)
47 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS	BIT(22)
48 
49 /*
50  * There are two hidpp protocols in use, the first version hidpp10 is known
51  * as register access protocol or RAP, the second version hidpp20 is known as
52  * feature access protocol or FAP
53  *
54  * Most older devices (including the Unifying usb receiver) use the RAP protocol
55  * where as most newer devices use the FAP protocol. Both protocols are
56  * compatible with the underlying transport, which could be usb, Unifiying, or
57  * bluetooth. The message lengths are defined by the hid vendor specific report
58  * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
59  * the HIDPP_LONG report type (total message length 20 bytes)
60  *
61  * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
62  * messages. The Unifying receiver itself responds to RAP messages (device index
63  * is 0xFF for the receiver), and all messages (short or long) with a device
64  * index between 1 and 6 are passed untouched to the corresponding paired
65  * Unifying device.
66  *
67  * The paired device can be RAP or FAP, it will receive the message untouched
68  * from the Unifiying receiver.
69  */
70 
71 struct fap {
72 	u8 feature_index;
73 	u8 funcindex_clientid;
74 	u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
75 };
76 
77 struct rap {
78 	u8 sub_id;
79 	u8 reg_address;
80 	u8 params[HIDPP_REPORT_LONG_LENGTH - 4U];
81 };
82 
83 struct hidpp_report {
84 	u8 report_id;
85 	u8 device_index;
86 	union {
87 		struct fap fap;
88 		struct rap rap;
89 		u8 rawbytes[sizeof(struct fap)];
90 	};
91 } __packed;
92 
93 struct hidpp_device {
94 	struct hid_device *hid_dev;
95 	struct mutex send_mutex;
96 	void *send_receive_buf;
97 	char *name;		/* will never be NULL and should not be freed */
98 	wait_queue_head_t wait;
99 	bool answer_available;
100 	u8 protocol_major;
101 	u8 protocol_minor;
102 
103 	void *private_data;
104 
105 	struct work_struct work;
106 	struct kfifo delayed_work_fifo;
107 	atomic_t connected;
108 	struct input_dev *delayed_input;
109 
110 	unsigned long quirks;
111 };
112 
113 
114 /* HID++ 1.0 error codes */
115 #define HIDPP_ERROR				0x8f
116 #define HIDPP_ERROR_SUCCESS			0x00
117 #define HIDPP_ERROR_INVALID_SUBID		0x01
118 #define HIDPP_ERROR_INVALID_ADRESS		0x02
119 #define HIDPP_ERROR_INVALID_VALUE		0x03
120 #define HIDPP_ERROR_CONNECT_FAIL		0x04
121 #define HIDPP_ERROR_TOO_MANY_DEVICES		0x05
122 #define HIDPP_ERROR_ALREADY_EXISTS		0x06
123 #define HIDPP_ERROR_BUSY			0x07
124 #define HIDPP_ERROR_UNKNOWN_DEVICE		0x08
125 #define HIDPP_ERROR_RESOURCE_ERROR		0x09
126 #define HIDPP_ERROR_REQUEST_UNAVAILABLE		0x0a
127 #define HIDPP_ERROR_INVALID_PARAM_VALUE		0x0b
128 #define HIDPP_ERROR_WRONG_PIN_CODE		0x0c
129 /* HID++ 2.0 error codes */
130 #define HIDPP20_ERROR				0xff
131 
132 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
133 
134 static int __hidpp_send_report(struct hid_device *hdev,
135 				struct hidpp_report *hidpp_report)
136 {
137 	int fields_count, ret;
138 
139 	switch (hidpp_report->report_id) {
140 	case REPORT_ID_HIDPP_SHORT:
141 		fields_count = HIDPP_REPORT_SHORT_LENGTH;
142 		break;
143 	case REPORT_ID_HIDPP_LONG:
144 		fields_count = HIDPP_REPORT_LONG_LENGTH;
145 		break;
146 	default:
147 		return -ENODEV;
148 	}
149 
150 	/*
151 	 * set the device_index as the receiver, it will be overwritten by
152 	 * hid_hw_request if needed
153 	 */
154 	hidpp_report->device_index = 0xff;
155 
156 	ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
157 		(u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
158 		HID_REQ_SET_REPORT);
159 
160 	return ret == fields_count ? 0 : -1;
161 }
162 
163 /**
164  * hidpp_send_message_sync() returns 0 in case of success, and something else
165  * in case of a failure.
166  * - If ' something else' is positive, that means that an error has been raised
167  *   by the protocol itself.
168  * - If ' something else' is negative, that means that we had a classic error
169  *   (-ENOMEM, -EPIPE, etc...)
170  */
171 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
172 	struct hidpp_report *message,
173 	struct hidpp_report *response)
174 {
175 	int ret;
176 
177 	mutex_lock(&hidpp->send_mutex);
178 
179 	hidpp->send_receive_buf = response;
180 	hidpp->answer_available = false;
181 
182 	/*
183 	 * So that we can later validate the answer when it arrives
184 	 * in hidpp_raw_event
185 	 */
186 	*response = *message;
187 
188 	ret = __hidpp_send_report(hidpp->hid_dev, message);
189 
190 	if (ret) {
191 		dbg_hid("__hidpp_send_report returned err: %d\n", ret);
192 		memset(response, 0, sizeof(struct hidpp_report));
193 		goto exit;
194 	}
195 
196 	if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
197 				5*HZ)) {
198 		dbg_hid("%s:timeout waiting for response\n", __func__);
199 		memset(response, 0, sizeof(struct hidpp_report));
200 		ret = -ETIMEDOUT;
201 	}
202 
203 	if (response->report_id == REPORT_ID_HIDPP_SHORT &&
204 	    response->rap.sub_id == HIDPP_ERROR) {
205 		ret = response->rap.params[1];
206 		dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
207 		goto exit;
208 	}
209 
210 	if (response->report_id == REPORT_ID_HIDPP_LONG &&
211 	    response->fap.feature_index == HIDPP20_ERROR) {
212 		ret = response->fap.params[1];
213 		dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
214 		goto exit;
215 	}
216 
217 exit:
218 	mutex_unlock(&hidpp->send_mutex);
219 	return ret;
220 
221 }
222 
223 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
224 	u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
225 	struct hidpp_report *response)
226 {
227 	struct hidpp_report *message;
228 	int ret;
229 
230 	if (param_count > sizeof(message->fap.params))
231 		return -EINVAL;
232 
233 	message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
234 	if (!message)
235 		return -ENOMEM;
236 	message->report_id = REPORT_ID_HIDPP_LONG;
237 	message->fap.feature_index = feat_index;
238 	message->fap.funcindex_clientid = funcindex_clientid;
239 	memcpy(&message->fap.params, params, param_count);
240 
241 	ret = hidpp_send_message_sync(hidpp, message, response);
242 	kfree(message);
243 	return ret;
244 }
245 
246 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
247 	u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
248 	struct hidpp_report *response)
249 {
250 	struct hidpp_report *message;
251 	int ret;
252 
253 	if ((report_id != REPORT_ID_HIDPP_SHORT) &&
254 	    (report_id != REPORT_ID_HIDPP_LONG))
255 		return -EINVAL;
256 
257 	if (param_count > sizeof(message->rap.params))
258 		return -EINVAL;
259 
260 	message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
261 	if (!message)
262 		return -ENOMEM;
263 	message->report_id = report_id;
264 	message->rap.sub_id = sub_id;
265 	message->rap.reg_address = reg_address;
266 	memcpy(&message->rap.params, params, param_count);
267 
268 	ret = hidpp_send_message_sync(hidpp_dev, message, response);
269 	kfree(message);
270 	return ret;
271 }
272 
273 static void delayed_work_cb(struct work_struct *work)
274 {
275 	struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
276 							work);
277 	hidpp_connect_event(hidpp);
278 }
279 
280 static inline bool hidpp_match_answer(struct hidpp_report *question,
281 		struct hidpp_report *answer)
282 {
283 	return (answer->fap.feature_index == question->fap.feature_index) &&
284 	   (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
285 }
286 
287 static inline bool hidpp_match_error(struct hidpp_report *question,
288 		struct hidpp_report *answer)
289 {
290 	return ((answer->rap.sub_id == HIDPP_ERROR) ||
291 	    (answer->fap.feature_index == HIDPP20_ERROR)) &&
292 	    (answer->fap.funcindex_clientid == question->fap.feature_index) &&
293 	    (answer->fap.params[0] == question->fap.funcindex_clientid);
294 }
295 
296 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
297 {
298 	return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
299 		(report->rap.sub_id == 0x41);
300 }
301 
302 /**
303  * hidpp_prefix_name() prefixes the current given name with "Logitech ".
304  */
305 static void hidpp_prefix_name(char **name, int name_length)
306 {
307 #define PREFIX_LENGTH 9 /* "Logitech " */
308 
309 	int new_length;
310 	char *new_name;
311 
312 	if (name_length > PREFIX_LENGTH &&
313 	    strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
314 		/* The prefix has is already in the name */
315 		return;
316 
317 	new_length = PREFIX_LENGTH + name_length;
318 	new_name = kzalloc(new_length, GFP_KERNEL);
319 	if (!new_name)
320 		return;
321 
322 	snprintf(new_name, new_length, "Logitech %s", *name);
323 
324 	kfree(*name);
325 
326 	*name = new_name;
327 }
328 
329 /* -------------------------------------------------------------------------- */
330 /* HIDP++ 1.0 commands                                                        */
331 /* -------------------------------------------------------------------------- */
332 
333 #define HIDPP_SET_REGISTER				0x80
334 #define HIDPP_GET_REGISTER				0x81
335 #define HIDPP_SET_LONG_REGISTER				0x82
336 #define HIDPP_GET_LONG_REGISTER				0x83
337 
338 #define HIDPP_REG_PAIRING_INFORMATION			0xB5
339 #define DEVICE_NAME					0x40
340 
341 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
342 {
343 	struct hidpp_report response;
344 	int ret;
345 	/* hid-logitech-dj is in charge of setting the right device index */
346 	u8 params[1] = { DEVICE_NAME };
347 	char *name;
348 	int len;
349 
350 	ret = hidpp_send_rap_command_sync(hidpp_dev,
351 					REPORT_ID_HIDPP_SHORT,
352 					HIDPP_GET_LONG_REGISTER,
353 					HIDPP_REG_PAIRING_INFORMATION,
354 					params, 1, &response);
355 	if (ret)
356 		return NULL;
357 
358 	len = response.rap.params[1];
359 
360 	if (2 + len > sizeof(response.rap.params))
361 		return NULL;
362 
363 	name = kzalloc(len + 1, GFP_KERNEL);
364 	if (!name)
365 		return NULL;
366 
367 	memcpy(name, &response.rap.params[2], len);
368 
369 	/* include the terminating '\0' */
370 	hidpp_prefix_name(&name, len + 1);
371 
372 	return name;
373 }
374 
375 /* -------------------------------------------------------------------------- */
376 /* 0x0000: Root                                                               */
377 /* -------------------------------------------------------------------------- */
378 
379 #define HIDPP_PAGE_ROOT					0x0000
380 #define HIDPP_PAGE_ROOT_IDX				0x00
381 
382 #define CMD_ROOT_GET_FEATURE				0x01
383 #define CMD_ROOT_GET_PROTOCOL_VERSION			0x11
384 
385 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
386 	u8 *feature_index, u8 *feature_type)
387 {
388 	struct hidpp_report response;
389 	int ret;
390 	u8 params[2] = { feature >> 8, feature & 0x00FF };
391 
392 	ret = hidpp_send_fap_command_sync(hidpp,
393 			HIDPP_PAGE_ROOT_IDX,
394 			CMD_ROOT_GET_FEATURE,
395 			params, 2, &response);
396 	if (ret)
397 		return ret;
398 
399 	*feature_index = response.fap.params[0];
400 	*feature_type = response.fap.params[1];
401 
402 	return ret;
403 }
404 
405 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
406 {
407 	struct hidpp_report response;
408 	int ret;
409 
410 	ret = hidpp_send_fap_command_sync(hidpp,
411 			HIDPP_PAGE_ROOT_IDX,
412 			CMD_ROOT_GET_PROTOCOL_VERSION,
413 			NULL, 0, &response);
414 
415 	if (ret == HIDPP_ERROR_INVALID_SUBID) {
416 		hidpp->protocol_major = 1;
417 		hidpp->protocol_minor = 0;
418 		return 0;
419 	}
420 
421 	/* the device might not be connected */
422 	if (ret == HIDPP_ERROR_RESOURCE_ERROR)
423 		return -EIO;
424 
425 	if (ret > 0) {
426 		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
427 			__func__, ret);
428 		return -EPROTO;
429 	}
430 	if (ret)
431 		return ret;
432 
433 	hidpp->protocol_major = response.fap.params[0];
434 	hidpp->protocol_minor = response.fap.params[1];
435 
436 	return ret;
437 }
438 
439 static bool hidpp_is_connected(struct hidpp_device *hidpp)
440 {
441 	int ret;
442 
443 	ret = hidpp_root_get_protocol_version(hidpp);
444 	if (!ret)
445 		hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
446 			hidpp->protocol_major, hidpp->protocol_minor);
447 	return ret == 0;
448 }
449 
450 /* -------------------------------------------------------------------------- */
451 /* 0x0005: GetDeviceNameType                                                  */
452 /* -------------------------------------------------------------------------- */
453 
454 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE			0x0005
455 
456 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT		0x01
457 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME	0x11
458 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE		0x21
459 
460 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
461 	u8 feature_index, u8 *nameLength)
462 {
463 	struct hidpp_report response;
464 	int ret;
465 
466 	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
467 		CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
468 
469 	if (ret > 0) {
470 		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
471 			__func__, ret);
472 		return -EPROTO;
473 	}
474 	if (ret)
475 		return ret;
476 
477 	*nameLength = response.fap.params[0];
478 
479 	return ret;
480 }
481 
482 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
483 	u8 feature_index, u8 char_index, char *device_name, int len_buf)
484 {
485 	struct hidpp_report response;
486 	int ret, i;
487 	int count;
488 
489 	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
490 		CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
491 		&response);
492 
493 	if (ret > 0) {
494 		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
495 			__func__, ret);
496 		return -EPROTO;
497 	}
498 	if (ret)
499 		return ret;
500 
501 	if (response.report_id == REPORT_ID_HIDPP_LONG)
502 		count = HIDPP_REPORT_LONG_LENGTH - 4;
503 	else
504 		count = HIDPP_REPORT_SHORT_LENGTH - 4;
505 
506 	if (len_buf < count)
507 		count = len_buf;
508 
509 	for (i = 0; i < count; i++)
510 		device_name[i] = response.fap.params[i];
511 
512 	return count;
513 }
514 
515 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
516 {
517 	u8 feature_type;
518 	u8 feature_index;
519 	u8 __name_length;
520 	char *name;
521 	unsigned index = 0;
522 	int ret;
523 
524 	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
525 		&feature_index, &feature_type);
526 	if (ret)
527 		return NULL;
528 
529 	ret = hidpp_devicenametype_get_count(hidpp, feature_index,
530 		&__name_length);
531 	if (ret)
532 		return NULL;
533 
534 	name = kzalloc(__name_length + 1, GFP_KERNEL);
535 	if (!name)
536 		return NULL;
537 
538 	while (index < __name_length) {
539 		ret = hidpp_devicenametype_get_device_name(hidpp,
540 			feature_index, index, name + index,
541 			__name_length - index);
542 		if (ret <= 0) {
543 			kfree(name);
544 			return NULL;
545 		}
546 		index += ret;
547 	}
548 
549 	/* include the terminating '\0' */
550 	hidpp_prefix_name(&name, __name_length + 1);
551 
552 	return name;
553 }
554 
555 /* -------------------------------------------------------------------------- */
556 /* 0x6100: TouchPadRawXY                                                      */
557 /* -------------------------------------------------------------------------- */
558 
559 #define HIDPP_PAGE_TOUCHPAD_RAW_XY			0x6100
560 
561 #define CMD_TOUCHPAD_GET_RAW_INFO			0x01
562 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE		0x21
563 
564 #define EVENT_TOUCHPAD_RAW_XY				0x00
565 
566 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT		0x01
567 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT		0x03
568 
569 struct hidpp_touchpad_raw_info {
570 	u16 x_size;
571 	u16 y_size;
572 	u8 z_range;
573 	u8 area_range;
574 	u8 timestamp_unit;
575 	u8 maxcontacts;
576 	u8 origin;
577 	u16 res;
578 };
579 
580 struct hidpp_touchpad_raw_xy_finger {
581 	u8 contact_type;
582 	u8 contact_status;
583 	u16 x;
584 	u16 y;
585 	u8 z;
586 	u8 area;
587 	u8 finger_id;
588 };
589 
590 struct hidpp_touchpad_raw_xy {
591 	u16 timestamp;
592 	struct hidpp_touchpad_raw_xy_finger fingers[2];
593 	u8 spurious_flag;
594 	u8 end_of_frame;
595 	u8 finger_count;
596 	u8 button;
597 };
598 
599 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
600 	u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
601 {
602 	struct hidpp_report response;
603 	int ret;
604 	u8 *params = (u8 *)response.fap.params;
605 
606 	ret = hidpp_send_fap_command_sync(hidpp, feature_index,
607 		CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
608 
609 	if (ret > 0) {
610 		hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
611 			__func__, ret);
612 		return -EPROTO;
613 	}
614 	if (ret)
615 		return ret;
616 
617 	raw_info->x_size = get_unaligned_be16(&params[0]);
618 	raw_info->y_size = get_unaligned_be16(&params[2]);
619 	raw_info->z_range = params[4];
620 	raw_info->area_range = params[5];
621 	raw_info->maxcontacts = params[7];
622 	raw_info->origin = params[8];
623 	/* res is given in unit per inch */
624 	raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
625 
626 	return ret;
627 }
628 
629 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
630 		u8 feature_index, bool send_raw_reports,
631 		bool sensor_enhanced_settings)
632 {
633 	struct hidpp_report response;
634 
635 	/*
636 	 * Params:
637 	 *   bit 0 - enable raw
638 	 *   bit 1 - 16bit Z, no area
639 	 *   bit 2 - enhanced sensitivity
640 	 *   bit 3 - width, height (4 bits each) instead of area
641 	 *   bit 4 - send raw + gestures (degrades smoothness)
642 	 *   remaining bits - reserved
643 	 */
644 	u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
645 
646 	return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
647 		CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
648 }
649 
650 static void hidpp_touchpad_touch_event(u8 *data,
651 	struct hidpp_touchpad_raw_xy_finger *finger)
652 {
653 	u8 x_m = data[0] << 2;
654 	u8 y_m = data[2] << 2;
655 
656 	finger->x = x_m << 6 | data[1];
657 	finger->y = y_m << 6 | data[3];
658 
659 	finger->contact_type = data[0] >> 6;
660 	finger->contact_status = data[2] >> 6;
661 
662 	finger->z = data[4];
663 	finger->area = data[5];
664 	finger->finger_id = data[6] >> 4;
665 }
666 
667 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
668 		u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
669 {
670 	memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
671 	raw_xy->end_of_frame = data[8] & 0x01;
672 	raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
673 	raw_xy->finger_count = data[15] & 0x0f;
674 	raw_xy->button = (data[8] >> 2) & 0x01;
675 
676 	if (raw_xy->finger_count) {
677 		hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
678 		hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
679 	}
680 }
681 
682 /* ************************************************************************** */
683 /*                                                                            */
684 /* Device Support                                                             */
685 /*                                                                            */
686 /* ************************************************************************** */
687 
688 /* -------------------------------------------------------------------------- */
689 /* Touchpad HID++ devices                                                     */
690 /* -------------------------------------------------------------------------- */
691 
692 #define WTP_MANUAL_RESOLUTION				39
693 
694 struct wtp_data {
695 	struct input_dev *input;
696 	u16 x_size, y_size;
697 	u8 finger_count;
698 	u8 mt_feature_index;
699 	u8 button_feature_index;
700 	u8 maxcontacts;
701 	bool flip_y;
702 	unsigned int resolution;
703 };
704 
705 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
706 		struct hid_field *field, struct hid_usage *usage,
707 		unsigned long **bit, int *max)
708 {
709 	return -1;
710 }
711 
712 static void wtp_populate_input(struct hidpp_device *hidpp,
713 		struct input_dev *input_dev, bool origin_is_hid_core)
714 {
715 	struct wtp_data *wd = hidpp->private_data;
716 
717 	__set_bit(EV_ABS, input_dev->evbit);
718 	__set_bit(EV_KEY, input_dev->evbit);
719 	__clear_bit(EV_REL, input_dev->evbit);
720 	__clear_bit(EV_LED, input_dev->evbit);
721 
722 	input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
723 	input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
724 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
725 	input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
726 
727 	/* Max pressure is not given by the devices, pick one */
728 	input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
729 
730 	input_set_capability(input_dev, EV_KEY, BTN_LEFT);
731 
732 	if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
733 		input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
734 	else
735 		__set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
736 
737 	input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
738 		INPUT_MT_DROP_UNUSED);
739 
740 	wd->input = input_dev;
741 }
742 
743 static void wtp_touch_event(struct wtp_data *wd,
744 	struct hidpp_touchpad_raw_xy_finger *touch_report)
745 {
746 	int slot;
747 
748 	if (!touch_report->finger_id || touch_report->contact_type)
749 		/* no actual data */
750 		return;
751 
752 	slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
753 
754 	input_mt_slot(wd->input, slot);
755 	input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
756 					touch_report->contact_status);
757 	if (touch_report->contact_status) {
758 		input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
759 				touch_report->x);
760 		input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
761 				wd->flip_y ? wd->y_size - touch_report->y :
762 					     touch_report->y);
763 		input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
764 				touch_report->area);
765 	}
766 }
767 
768 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
769 		struct hidpp_touchpad_raw_xy *raw)
770 {
771 	struct wtp_data *wd = hidpp->private_data;
772 	int i;
773 
774 	for (i = 0; i < 2; i++)
775 		wtp_touch_event(wd, &(raw->fingers[i]));
776 
777 	if (raw->end_of_frame &&
778 	    !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
779 		input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
780 
781 	if (raw->end_of_frame || raw->finger_count <= 2) {
782 		input_mt_sync_frame(wd->input);
783 		input_sync(wd->input);
784 	}
785 }
786 
787 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
788 {
789 	struct wtp_data *wd = hidpp->private_data;
790 	u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
791 		      (data[7] >> 4) * (data[7] >> 4)) / 2;
792 	u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
793 		      (data[13] >> 4) * (data[13] >> 4)) / 2;
794 	struct hidpp_touchpad_raw_xy raw = {
795 		.timestamp = data[1],
796 		.fingers = {
797 			{
798 				.contact_type = 0,
799 				.contact_status = !!data[7],
800 				.x = get_unaligned_le16(&data[3]),
801 				.y = get_unaligned_le16(&data[5]),
802 				.z = c1_area,
803 				.area = c1_area,
804 				.finger_id = data[2],
805 			}, {
806 				.contact_type = 0,
807 				.contact_status = !!data[13],
808 				.x = get_unaligned_le16(&data[9]),
809 				.y = get_unaligned_le16(&data[11]),
810 				.z = c2_area,
811 				.area = c2_area,
812 				.finger_id = data[8],
813 			}
814 		},
815 		.finger_count = wd->maxcontacts,
816 		.spurious_flag = 0,
817 		.end_of_frame = (data[0] >> 7) == 0,
818 		.button = data[0] & 0x01,
819 	};
820 
821 	wtp_send_raw_xy_event(hidpp, &raw);
822 
823 	return 1;
824 }
825 
826 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
827 {
828 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
829 	struct wtp_data *wd = hidpp->private_data;
830 	struct hidpp_report *report = (struct hidpp_report *)data;
831 	struct hidpp_touchpad_raw_xy raw;
832 
833 	if (!wd || !wd->input)
834 		return 1;
835 
836 	switch (data[0]) {
837 	case 0x02:
838 		if (size < 2) {
839 			hid_err(hdev, "Received HID report of bad size (%d)",
840 				size);
841 			return 1;
842 		}
843 		if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
844 			input_event(wd->input, EV_KEY, BTN_LEFT,
845 					!!(data[1] & 0x01));
846 			input_event(wd->input, EV_KEY, BTN_RIGHT,
847 					!!(data[1] & 0x02));
848 			input_sync(wd->input);
849 			return 0;
850 		} else {
851 			if (size < 21)
852 				return 1;
853 			return wtp_mouse_raw_xy_event(hidpp, &data[7]);
854 		}
855 	case REPORT_ID_HIDPP_LONG:
856 		/* size is already checked in hidpp_raw_event. */
857 		if ((report->fap.feature_index != wd->mt_feature_index) ||
858 		    (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
859 			return 1;
860 		hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
861 
862 		wtp_send_raw_xy_event(hidpp, &raw);
863 		return 0;
864 	}
865 
866 	return 0;
867 }
868 
869 static int wtp_get_config(struct hidpp_device *hidpp)
870 {
871 	struct wtp_data *wd = hidpp->private_data;
872 	struct hidpp_touchpad_raw_info raw_info = {0};
873 	u8 feature_type;
874 	int ret;
875 
876 	ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
877 		&wd->mt_feature_index, &feature_type);
878 	if (ret)
879 		/* means that the device is not powered up */
880 		return ret;
881 
882 	ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
883 		&raw_info);
884 	if (ret)
885 		return ret;
886 
887 	wd->x_size = raw_info.x_size;
888 	wd->y_size = raw_info.y_size;
889 	wd->maxcontacts = raw_info.maxcontacts;
890 	wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
891 	wd->resolution = raw_info.res;
892 	if (!wd->resolution)
893 		wd->resolution = WTP_MANUAL_RESOLUTION;
894 
895 	return 0;
896 }
897 
898 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
899 {
900 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
901 	struct wtp_data *wd;
902 
903 	wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
904 			GFP_KERNEL);
905 	if (!wd)
906 		return -ENOMEM;
907 
908 	hidpp->private_data = wd;
909 
910 	return 0;
911 };
912 
913 static int wtp_connect(struct hid_device *hdev, bool connected)
914 {
915 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
916 	struct wtp_data *wd = hidpp->private_data;
917 	int ret;
918 
919 	if (!connected)
920 		return 0;
921 
922 	if (!wd->x_size) {
923 		ret = wtp_get_config(hidpp);
924 		if (ret) {
925 			hid_err(hdev, "Can not get wtp config: %d\n", ret);
926 			return ret;
927 		}
928 	}
929 
930 	return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
931 			true, true);
932 }
933 
934 /* ------------------------------------------------------------------------- */
935 /* Logitech M560 devices                                                     */
936 /* ------------------------------------------------------------------------- */
937 
938 /*
939  * Logitech M560 protocol overview
940  *
941  * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
942  * the sides buttons are pressed, it sends some keyboard keys events
943  * instead of buttons ones.
944  * To complicate things further, the middle button keys sequence
945  * is different from the odd press and the even press.
946  *
947  * forward button -> Super_R
948  * backward button -> Super_L+'d' (press only)
949  * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
950  *                  2nd time: left-click (press only)
951  * NB: press-only means that when the button is pressed, the
952  * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
953  * together sequentially; instead when the button is released, no event is
954  * generated !
955  *
956  * With the command
957  *	10<xx>0a 3500af03 (where <xx> is the mouse id),
958  * the mouse reacts differently:
959  * - it never sends a keyboard key event
960  * - for the three mouse button it sends:
961  *	middle button               press   11<xx>0a 3500af00...
962  *	side 1 button (forward)     press   11<xx>0a 3500b000...
963  *	side 2 button (backward)    press   11<xx>0a 3500ae00...
964  *	middle/side1/side2 button   release 11<xx>0a 35000000...
965  */
966 
967 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
968 
969 struct m560_private_data {
970 	struct input_dev *input;
971 };
972 
973 /* how buttons are mapped in the report */
974 #define M560_MOUSE_BTN_LEFT		0x01
975 #define M560_MOUSE_BTN_RIGHT		0x02
976 #define M560_MOUSE_BTN_WHEEL_LEFT	0x08
977 #define M560_MOUSE_BTN_WHEEL_RIGHT	0x10
978 
979 #define M560_SUB_ID			0x0a
980 #define M560_BUTTON_MODE_REGISTER	0x35
981 
982 static int m560_send_config_command(struct hid_device *hdev, bool connected)
983 {
984 	struct hidpp_report response;
985 	struct hidpp_device *hidpp_dev;
986 
987 	hidpp_dev = hid_get_drvdata(hdev);
988 
989 	if (!connected)
990 		return -ENODEV;
991 
992 	return hidpp_send_rap_command_sync(
993 		hidpp_dev,
994 		REPORT_ID_HIDPP_SHORT,
995 		M560_SUB_ID,
996 		M560_BUTTON_MODE_REGISTER,
997 		(u8 *)m560_config_parameter,
998 		sizeof(m560_config_parameter),
999 		&response
1000 	);
1001 }
1002 
1003 static int m560_allocate(struct hid_device *hdev)
1004 {
1005 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1006 	struct m560_private_data *d;
1007 
1008 	d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1009 			GFP_KERNEL);
1010 	if (!d)
1011 		return -ENOMEM;
1012 
1013 	hidpp->private_data = d;
1014 
1015 	return 0;
1016 };
1017 
1018 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1019 {
1020 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1021 	struct m560_private_data *mydata = hidpp->private_data;
1022 
1023 	/* sanity check */
1024 	if (!mydata || !mydata->input) {
1025 		hid_err(hdev, "error in parameter\n");
1026 		return -EINVAL;
1027 	}
1028 
1029 	if (size < 7) {
1030 		hid_err(hdev, "error in report\n");
1031 		return 0;
1032 	}
1033 
1034 	if (data[0] == REPORT_ID_HIDPP_LONG &&
1035 	    data[2] == M560_SUB_ID && data[6] == 0x00) {
1036 		/*
1037 		 * m560 mouse report for middle, forward and backward button
1038 		 *
1039 		 * data[0] = 0x11
1040 		 * data[1] = device-id
1041 		 * data[2] = 0x0a
1042 		 * data[5] = 0xaf -> middle
1043 		 *	     0xb0 -> forward
1044 		 *	     0xae -> backward
1045 		 *	     0x00 -> release all
1046 		 * data[6] = 0x00
1047 		 */
1048 
1049 		switch (data[5]) {
1050 		case 0xaf:
1051 			input_report_key(mydata->input, BTN_MIDDLE, 1);
1052 			break;
1053 		case 0xb0:
1054 			input_report_key(mydata->input, BTN_FORWARD, 1);
1055 			break;
1056 		case 0xae:
1057 			input_report_key(mydata->input, BTN_BACK, 1);
1058 			break;
1059 		case 0x00:
1060 			input_report_key(mydata->input, BTN_BACK, 0);
1061 			input_report_key(mydata->input, BTN_FORWARD, 0);
1062 			input_report_key(mydata->input, BTN_MIDDLE, 0);
1063 			break;
1064 		default:
1065 			hid_err(hdev, "error in report\n");
1066 			return 0;
1067 		}
1068 		input_sync(mydata->input);
1069 
1070 	} else if (data[0] == 0x02) {
1071 		/*
1072 		 * Logitech M560 mouse report
1073 		 *
1074 		 * data[0] = type (0x02)
1075 		 * data[1..2] = buttons
1076 		 * data[3..5] = xy
1077 		 * data[6] = wheel
1078 		 */
1079 
1080 		int v;
1081 
1082 		input_report_key(mydata->input, BTN_LEFT,
1083 			!!(data[1] & M560_MOUSE_BTN_LEFT));
1084 		input_report_key(mydata->input, BTN_RIGHT,
1085 			!!(data[1] & M560_MOUSE_BTN_RIGHT));
1086 
1087 		if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
1088 			input_report_rel(mydata->input, REL_HWHEEL, -1);
1089 		else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
1090 			input_report_rel(mydata->input, REL_HWHEEL, 1);
1091 
1092 		v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
1093 		input_report_rel(mydata->input, REL_X, v);
1094 
1095 		v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
1096 		input_report_rel(mydata->input, REL_Y, v);
1097 
1098 		v = hid_snto32(data[6], 8);
1099 		input_report_rel(mydata->input, REL_WHEEL, v);
1100 
1101 		input_sync(mydata->input);
1102 	}
1103 
1104 	return 1;
1105 }
1106 
1107 static void m560_populate_input(struct hidpp_device *hidpp,
1108 		struct input_dev *input_dev, bool origin_is_hid_core)
1109 {
1110 	struct m560_private_data *mydata = hidpp->private_data;
1111 
1112 	mydata->input = input_dev;
1113 
1114 	__set_bit(EV_KEY, mydata->input->evbit);
1115 	__set_bit(BTN_MIDDLE, mydata->input->keybit);
1116 	__set_bit(BTN_RIGHT, mydata->input->keybit);
1117 	__set_bit(BTN_LEFT, mydata->input->keybit);
1118 	__set_bit(BTN_BACK, mydata->input->keybit);
1119 	__set_bit(BTN_FORWARD, mydata->input->keybit);
1120 
1121 	__set_bit(EV_REL, mydata->input->evbit);
1122 	__set_bit(REL_X, mydata->input->relbit);
1123 	__set_bit(REL_Y, mydata->input->relbit);
1124 	__set_bit(REL_WHEEL, mydata->input->relbit);
1125 	__set_bit(REL_HWHEEL, mydata->input->relbit);
1126 }
1127 
1128 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1129 		struct hid_field *field, struct hid_usage *usage,
1130 		unsigned long **bit, int *max)
1131 {
1132 	return -1;
1133 }
1134 
1135 /* -------------------------------------------------------------------------- */
1136 /* Generic HID++ devices                                                      */
1137 /* -------------------------------------------------------------------------- */
1138 
1139 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1140 		struct hid_field *field, struct hid_usage *usage,
1141 		unsigned long **bit, int *max)
1142 {
1143 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1144 
1145 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1146 		return wtp_input_mapping(hdev, hi, field, usage, bit, max);
1147 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
1148 			field->application != HID_GD_MOUSE)
1149 		return m560_input_mapping(hdev, hi, field, usage, bit, max);
1150 
1151 	return 0;
1152 }
1153 
1154 static void hidpp_populate_input(struct hidpp_device *hidpp,
1155 		struct input_dev *input, bool origin_is_hid_core)
1156 {
1157 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1158 		wtp_populate_input(hidpp, input, origin_is_hid_core);
1159 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1160 		m560_populate_input(hidpp, input, origin_is_hid_core);
1161 }
1162 
1163 static void hidpp_input_configured(struct hid_device *hdev,
1164 				struct hid_input *hidinput)
1165 {
1166 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1167 	struct input_dev *input = hidinput->input;
1168 
1169 	hidpp_populate_input(hidpp, input, true);
1170 }
1171 
1172 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
1173 		int size)
1174 {
1175 	struct hidpp_report *question = hidpp->send_receive_buf;
1176 	struct hidpp_report *answer = hidpp->send_receive_buf;
1177 	struct hidpp_report *report = (struct hidpp_report *)data;
1178 
1179 	/*
1180 	 * If the mutex is locked then we have a pending answer from a
1181 	 * previously sent command.
1182 	 */
1183 	if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
1184 		/*
1185 		 * Check for a correct hidpp20 answer or the corresponding
1186 		 * error
1187 		 */
1188 		if (hidpp_match_answer(question, report) ||
1189 				hidpp_match_error(question, report)) {
1190 			*answer = *report;
1191 			hidpp->answer_available = true;
1192 			wake_up(&hidpp->wait);
1193 			/*
1194 			 * This was an answer to a command that this driver sent
1195 			 * We return 1 to hid-core to avoid forwarding the
1196 			 * command upstream as it has been treated by the driver
1197 			 */
1198 
1199 			return 1;
1200 		}
1201 	}
1202 
1203 	if (unlikely(hidpp_report_is_connect_event(report))) {
1204 		atomic_set(&hidpp->connected,
1205 				!(report->rap.params[0] & (1 << 6)));
1206 		if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) &&
1207 		    (schedule_work(&hidpp->work) == 0))
1208 			dbg_hid("%s: connect event already queued\n", __func__);
1209 		return 1;
1210 	}
1211 
1212 	return 0;
1213 }
1214 
1215 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1216 		u8 *data, int size)
1217 {
1218 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1219 	int ret = 0;
1220 
1221 	/* Generic HID++ processing. */
1222 	switch (data[0]) {
1223 	case REPORT_ID_HIDPP_LONG:
1224 		if (size != HIDPP_REPORT_LONG_LENGTH) {
1225 			hid_err(hdev, "received hid++ report of bad size (%d)",
1226 				size);
1227 			return 1;
1228 		}
1229 		ret = hidpp_raw_hidpp_event(hidpp, data, size);
1230 		break;
1231 	case REPORT_ID_HIDPP_SHORT:
1232 		if (size != HIDPP_REPORT_SHORT_LENGTH) {
1233 			hid_err(hdev, "received hid++ report of bad size (%d)",
1234 				size);
1235 			return 1;
1236 		}
1237 		ret = hidpp_raw_hidpp_event(hidpp, data, size);
1238 		break;
1239 	}
1240 
1241 	/* If no report is available for further processing, skip calling
1242 	 * raw_event of subclasses. */
1243 	if (ret != 0)
1244 		return ret;
1245 
1246 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1247 		return wtp_raw_event(hdev, data, size);
1248 	else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1249 		return m560_raw_event(hdev, data, size);
1250 
1251 	return 0;
1252 }
1253 
1254 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
1255 {
1256 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1257 	char *name;
1258 
1259 	if (use_unifying)
1260 		/*
1261 		 * the device is connected through an Unifying receiver, and
1262 		 * might not be already connected.
1263 		 * Ask the receiver for its name.
1264 		 */
1265 		name = hidpp_get_unifying_name(hidpp);
1266 	else
1267 		name = hidpp_get_device_name(hidpp);
1268 
1269 	if (!name)
1270 		hid_err(hdev, "unable to retrieve the name of the device");
1271 	else
1272 		snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1273 
1274 	kfree(name);
1275 }
1276 
1277 static int hidpp_input_open(struct input_dev *dev)
1278 {
1279 	struct hid_device *hid = input_get_drvdata(dev);
1280 
1281 	return hid_hw_open(hid);
1282 }
1283 
1284 static void hidpp_input_close(struct input_dev *dev)
1285 {
1286 	struct hid_device *hid = input_get_drvdata(dev);
1287 
1288 	hid_hw_close(hid);
1289 }
1290 
1291 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1292 {
1293 	struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
1294 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1295 
1296 	if (!input_dev)
1297 		return NULL;
1298 
1299 	input_set_drvdata(input_dev, hdev);
1300 	input_dev->open = hidpp_input_open;
1301 	input_dev->close = hidpp_input_close;
1302 
1303 	input_dev->name = hidpp->name;
1304 	input_dev->phys = hdev->phys;
1305 	input_dev->uniq = hdev->uniq;
1306 	input_dev->id.bustype = hdev->bus;
1307 	input_dev->id.vendor  = hdev->vendor;
1308 	input_dev->id.product = hdev->product;
1309 	input_dev->id.version = hdev->version;
1310 	input_dev->dev.parent = &hdev->dev;
1311 
1312 	return input_dev;
1313 }
1314 
1315 static void hidpp_connect_event(struct hidpp_device *hidpp)
1316 {
1317 	struct hid_device *hdev = hidpp->hid_dev;
1318 	int ret = 0;
1319 	bool connected = atomic_read(&hidpp->connected);
1320 	struct input_dev *input;
1321 	char *name, *devm_name;
1322 
1323 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1324 		ret = wtp_connect(hdev, connected);
1325 		if (ret)
1326 			return;
1327 	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1328 		ret = m560_send_config_command(hdev, connected);
1329 		if (ret)
1330 			return;
1331 	}
1332 
1333 	if (!connected || hidpp->delayed_input)
1334 		return;
1335 
1336 	if (!hidpp->protocol_major) {
1337 		ret = !hidpp_is_connected(hidpp);
1338 		if (ret) {
1339 			hid_err(hdev, "Can not get the protocol version.\n");
1340 			return;
1341 		}
1342 	}
1343 
1344 	/* the device is already connected, we can ask for its name and
1345 	 * protocol */
1346 	hid_info(hdev, "HID++ %u.%u device connected.\n",
1347 		 hidpp->protocol_major, hidpp->protocol_minor);
1348 
1349 	if (!hidpp->name || hidpp->name == hdev->name) {
1350 		name = hidpp_get_device_name(hidpp);
1351 		if (!name) {
1352 			hid_err(hdev,
1353 				"unable to retrieve the name of the device");
1354 			return;
1355 		}
1356 
1357 		devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1358 		kfree(name);
1359 		if (!devm_name)
1360 			return;
1361 
1362 		hidpp->name = devm_name;
1363 	}
1364 
1365 	input = hidpp_allocate_input(hdev);
1366 	if (!input) {
1367 		hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1368 		return;
1369 	}
1370 
1371 	hidpp_populate_input(hidpp, input, false);
1372 
1373 	ret = input_register_device(input);
1374 	if (ret)
1375 		input_free_device(input);
1376 
1377 	hidpp->delayed_input = input;
1378 }
1379 
1380 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1381 {
1382 	struct hidpp_device *hidpp;
1383 	int ret;
1384 	bool connected;
1385 	unsigned int connect_mask = HID_CONNECT_DEFAULT;
1386 
1387 	hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1388 			GFP_KERNEL);
1389 	if (!hidpp)
1390 		return -ENOMEM;
1391 
1392 	hidpp->hid_dev = hdev;
1393 	hidpp->name = hdev->name;
1394 	hid_set_drvdata(hdev, hidpp);
1395 
1396 	hidpp->quirks = id->driver_data;
1397 
1398 	if (disable_raw_mode) {
1399 		hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
1400 		hidpp->quirks &= ~HIDPP_QUIRK_DELAYED_INIT;
1401 	}
1402 
1403 	if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1404 		ret = wtp_allocate(hdev, id);
1405 		if (ret)
1406 			goto allocate_fail;
1407 	} else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1408 		ret = m560_allocate(hdev);
1409 		if (ret)
1410 			goto allocate_fail;
1411 	}
1412 
1413 	INIT_WORK(&hidpp->work, delayed_work_cb);
1414 	mutex_init(&hidpp->send_mutex);
1415 	init_waitqueue_head(&hidpp->wait);
1416 
1417 	ret = hid_parse(hdev);
1418 	if (ret) {
1419 		hid_err(hdev, "%s:parse failed\n", __func__);
1420 		goto hid_parse_fail;
1421 	}
1422 
1423 	/* Allow incoming packets */
1424 	hid_device_io_start(hdev);
1425 
1426 	connected = hidpp_is_connected(hidpp);
1427 	if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1428 		if (!connected) {
1429 			ret = -ENODEV;
1430 			hid_err(hdev, "Device not connected");
1431 			hid_device_io_stop(hdev);
1432 			goto hid_parse_fail;
1433 		}
1434 
1435 		hid_info(hdev, "HID++ %u.%u device connected.\n",
1436 			 hidpp->protocol_major, hidpp->protocol_minor);
1437 	}
1438 
1439 	hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
1440 	atomic_set(&hidpp->connected, connected);
1441 
1442 	if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
1443 		ret = wtp_get_config(hidpp);
1444 		if (ret)
1445 			goto hid_parse_fail;
1446 	}
1447 
1448 	/* Block incoming packets */
1449 	hid_device_io_stop(hdev);
1450 
1451 	if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT)
1452 		connect_mask &= ~HID_CONNECT_HIDINPUT;
1453 
1454 	ret = hid_hw_start(hdev, connect_mask);
1455 	if (ret) {
1456 		hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1457 		goto hid_hw_start_fail;
1458 	}
1459 
1460 	if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) {
1461 		/* Allow incoming packets */
1462 		hid_device_io_start(hdev);
1463 
1464 		hidpp_connect_event(hidpp);
1465 	}
1466 
1467 	return ret;
1468 
1469 hid_hw_start_fail:
1470 hid_parse_fail:
1471 	cancel_work_sync(&hidpp->work);
1472 	mutex_destroy(&hidpp->send_mutex);
1473 allocate_fail:
1474 	hid_set_drvdata(hdev, NULL);
1475 	return ret;
1476 }
1477 
1478 static void hidpp_remove(struct hid_device *hdev)
1479 {
1480 	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1481 
1482 	cancel_work_sync(&hidpp->work);
1483 	mutex_destroy(&hidpp->send_mutex);
1484 	hid_hw_stop(hdev);
1485 }
1486 
1487 static const struct hid_device_id hidpp_devices[] = {
1488 	{ /* wireless touchpad */
1489 	  HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1490 		USB_VENDOR_ID_LOGITECH, 0x4011),
1491 	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1492 			 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
1493 	{ /* wireless touchpad T650 */
1494 	  HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1495 		USB_VENDOR_ID_LOGITECH, 0x4101),
1496 	  .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
1497 	{ /* wireless touchpad T651 */
1498 	  HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1499 		USB_DEVICE_ID_LOGITECH_T651),
1500 	  .driver_data = HIDPP_QUIRK_CLASS_WTP },
1501 	{ /* Mouse logitech M560 */
1502 	  HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1503 		USB_VENDOR_ID_LOGITECH, 0x402d),
1504 	  .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
1505 
1506 	{ HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1507 		USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1508 	{}
1509 };
1510 
1511 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1512 
1513 static struct hid_driver hidpp_driver = {
1514 	.name = "logitech-hidpp-device",
1515 	.id_table = hidpp_devices,
1516 	.probe = hidpp_probe,
1517 	.remove = hidpp_remove,
1518 	.raw_event = hidpp_raw_event,
1519 	.input_configured = hidpp_input_configured,
1520 	.input_mapping = hidpp_input_mapping,
1521 };
1522 
1523 module_hid_driver(hidpp_driver);
1524