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