f2fs: flush dirty nat entries when exceeding threshold
[linux-2.6/btrfs-unstable.git] / drivers / hid / hid-logitech-hidpp.c
blobbd2ab476c65ef9f73a04a1f732dc7f75a3b49835
1 /*
2 * HIDPP protocol for Logitech Unifying receivers
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
7 */
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
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.");
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).");
41 #define REPORT_ID_HIDPP_SHORT 0x10
42 #define REPORT_ID_HIDPP_LONG 0x11
43 #define REPORT_ID_HIDPP_VERY_LONG 0x12
45 #define HIDPP_REPORT_SHORT_LENGTH 7
46 #define HIDPP_REPORT_LONG_LENGTH 20
47 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
49 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
50 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
51 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
52 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
54 /* bits 2..20 are reserved for classes */
55 #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21)
56 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
57 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
58 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
60 #define HIDPP_QUIRK_DELAYED_INIT (HIDPP_QUIRK_NO_HIDINPUT | \
61 HIDPP_QUIRK_CONNECT_EVENTS)
64 * There are two hidpp protocols in use, the first version hidpp10 is known
65 * as register access protocol or RAP, the second version hidpp20 is known as
66 * feature access protocol or FAP
68 * Most older devices (including the Unifying usb receiver) use the RAP protocol
69 * where as most newer devices use the FAP protocol. Both protocols are
70 * compatible with the underlying transport, which could be usb, Unifiying, or
71 * bluetooth. The message lengths are defined by the hid vendor specific report
72 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
73 * the HIDPP_LONG report type (total message length 20 bytes)
75 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
76 * messages. The Unifying receiver itself responds to RAP messages (device index
77 * is 0xFF for the receiver), and all messages (short or long) with a device
78 * index between 1 and 6 are passed untouched to the corresponding paired
79 * Unifying device.
81 * The paired device can be RAP or FAP, it will receive the message untouched
82 * from the Unifiying receiver.
85 struct fap {
86 u8 feature_index;
87 u8 funcindex_clientid;
88 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
91 struct rap {
92 u8 sub_id;
93 u8 reg_address;
94 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
97 struct hidpp_report {
98 u8 report_id;
99 u8 device_index;
100 union {
101 struct fap fap;
102 struct rap rap;
103 u8 rawbytes[sizeof(struct fap)];
105 } __packed;
107 struct hidpp_device {
108 struct hid_device *hid_dev;
109 struct mutex send_mutex;
110 void *send_receive_buf;
111 char *name; /* will never be NULL and should not be freed */
112 wait_queue_head_t wait;
113 bool answer_available;
114 u8 protocol_major;
115 u8 protocol_minor;
117 void *private_data;
119 struct work_struct work;
120 struct kfifo delayed_work_fifo;
121 atomic_t connected;
122 struct input_dev *delayed_input;
124 unsigned long quirks;
128 /* HID++ 1.0 error codes */
129 #define HIDPP_ERROR 0x8f
130 #define HIDPP_ERROR_SUCCESS 0x00
131 #define HIDPP_ERROR_INVALID_SUBID 0x01
132 #define HIDPP_ERROR_INVALID_ADRESS 0x02
133 #define HIDPP_ERROR_INVALID_VALUE 0x03
134 #define HIDPP_ERROR_CONNECT_FAIL 0x04
135 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
136 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
137 #define HIDPP_ERROR_BUSY 0x07
138 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
139 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
140 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
141 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
142 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
143 /* HID++ 2.0 error codes */
144 #define HIDPP20_ERROR 0xff
146 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
148 static int __hidpp_send_report(struct hid_device *hdev,
149 struct hidpp_report *hidpp_report)
151 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
152 int fields_count, ret;
154 hidpp = hid_get_drvdata(hdev);
156 switch (hidpp_report->report_id) {
157 case REPORT_ID_HIDPP_SHORT:
158 fields_count = HIDPP_REPORT_SHORT_LENGTH;
159 break;
160 case REPORT_ID_HIDPP_LONG:
161 fields_count = HIDPP_REPORT_LONG_LENGTH;
162 break;
163 case REPORT_ID_HIDPP_VERY_LONG:
164 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
165 break;
166 default:
167 return -ENODEV;
171 * set the device_index as the receiver, it will be overwritten by
172 * hid_hw_request if needed
174 hidpp_report->device_index = 0xff;
176 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
177 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
178 } else {
179 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
180 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
181 HID_REQ_SET_REPORT);
184 return ret == fields_count ? 0 : -1;
188 * hidpp_send_message_sync() returns 0 in case of success, and something else
189 * in case of a failure.
190 * - If ' something else' is positive, that means that an error has been raised
191 * by the protocol itself.
192 * - If ' something else' is negative, that means that we had a classic error
193 * (-ENOMEM, -EPIPE, etc...)
195 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
196 struct hidpp_report *message,
197 struct hidpp_report *response)
199 int ret;
201 mutex_lock(&hidpp->send_mutex);
203 hidpp->send_receive_buf = response;
204 hidpp->answer_available = false;
207 * So that we can later validate the answer when it arrives
208 * in hidpp_raw_event
210 *response = *message;
212 ret = __hidpp_send_report(hidpp->hid_dev, message);
214 if (ret) {
215 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
216 memset(response, 0, sizeof(struct hidpp_report));
217 goto exit;
220 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
221 5*HZ)) {
222 dbg_hid("%s:timeout waiting for response\n", __func__);
223 memset(response, 0, sizeof(struct hidpp_report));
224 ret = -ETIMEDOUT;
227 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
228 response->rap.sub_id == HIDPP_ERROR) {
229 ret = response->rap.params[1];
230 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
231 goto exit;
234 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
235 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
236 response->fap.feature_index == HIDPP20_ERROR) {
237 ret = response->fap.params[1];
238 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
239 goto exit;
242 exit:
243 mutex_unlock(&hidpp->send_mutex);
244 return ret;
248 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
249 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
250 struct hidpp_report *response)
252 struct hidpp_report *message;
253 int ret;
255 if (param_count > sizeof(message->fap.params))
256 return -EINVAL;
258 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
259 if (!message)
260 return -ENOMEM;
262 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
263 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
264 else
265 message->report_id = REPORT_ID_HIDPP_LONG;
266 message->fap.feature_index = feat_index;
267 message->fap.funcindex_clientid = funcindex_clientid;
268 memcpy(&message->fap.params, params, param_count);
270 ret = hidpp_send_message_sync(hidpp, message, response);
271 kfree(message);
272 return ret;
275 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
276 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
277 struct hidpp_report *response)
279 struct hidpp_report *message;
280 int ret, max_count;
282 switch (report_id) {
283 case REPORT_ID_HIDPP_SHORT:
284 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
285 break;
286 case REPORT_ID_HIDPP_LONG:
287 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
288 break;
289 case REPORT_ID_HIDPP_VERY_LONG:
290 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
291 break;
292 default:
293 return -EINVAL;
296 if (param_count > max_count)
297 return -EINVAL;
299 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
300 if (!message)
301 return -ENOMEM;
302 message->report_id = report_id;
303 message->rap.sub_id = sub_id;
304 message->rap.reg_address = reg_address;
305 memcpy(&message->rap.params, params, param_count);
307 ret = hidpp_send_message_sync(hidpp_dev, message, response);
308 kfree(message);
309 return ret;
312 static void delayed_work_cb(struct work_struct *work)
314 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
315 work);
316 hidpp_connect_event(hidpp);
319 static inline bool hidpp_match_answer(struct hidpp_report *question,
320 struct hidpp_report *answer)
322 return (answer->fap.feature_index == question->fap.feature_index) &&
323 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
326 static inline bool hidpp_match_error(struct hidpp_report *question,
327 struct hidpp_report *answer)
329 return ((answer->rap.sub_id == HIDPP_ERROR) ||
330 (answer->fap.feature_index == HIDPP20_ERROR)) &&
331 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
332 (answer->fap.params[0] == question->fap.funcindex_clientid);
335 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
337 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
338 (report->rap.sub_id == 0x41);
342 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
344 static void hidpp_prefix_name(char **name, int name_length)
346 #define PREFIX_LENGTH 9 /* "Logitech " */
348 int new_length;
349 char *new_name;
351 if (name_length > PREFIX_LENGTH &&
352 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
353 /* The prefix has is already in the name */
354 return;
356 new_length = PREFIX_LENGTH + name_length;
357 new_name = kzalloc(new_length, GFP_KERNEL);
358 if (!new_name)
359 return;
361 snprintf(new_name, new_length, "Logitech %s", *name);
363 kfree(*name);
365 *name = new_name;
368 /* -------------------------------------------------------------------------- */
369 /* HIDP++ 1.0 commands */
370 /* -------------------------------------------------------------------------- */
372 #define HIDPP_SET_REGISTER 0x80
373 #define HIDPP_GET_REGISTER 0x81
374 #define HIDPP_SET_LONG_REGISTER 0x82
375 #define HIDPP_GET_LONG_REGISTER 0x83
377 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
378 #define DEVICE_NAME 0x40
380 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
382 struct hidpp_report response;
383 int ret;
384 /* hid-logitech-dj is in charge of setting the right device index */
385 u8 params[1] = { DEVICE_NAME };
386 char *name;
387 int len;
389 ret = hidpp_send_rap_command_sync(hidpp_dev,
390 REPORT_ID_HIDPP_SHORT,
391 HIDPP_GET_LONG_REGISTER,
392 HIDPP_REG_PAIRING_INFORMATION,
393 params, 1, &response);
394 if (ret)
395 return NULL;
397 len = response.rap.params[1];
399 if (2 + len > sizeof(response.rap.params))
400 return NULL;
402 name = kzalloc(len + 1, GFP_KERNEL);
403 if (!name)
404 return NULL;
406 memcpy(name, &response.rap.params[2], len);
408 /* include the terminating '\0' */
409 hidpp_prefix_name(&name, len + 1);
411 return name;
414 /* -------------------------------------------------------------------------- */
415 /* 0x0000: Root */
416 /* -------------------------------------------------------------------------- */
418 #define HIDPP_PAGE_ROOT 0x0000
419 #define HIDPP_PAGE_ROOT_IDX 0x00
421 #define CMD_ROOT_GET_FEATURE 0x01
422 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
424 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
425 u8 *feature_index, u8 *feature_type)
427 struct hidpp_report response;
428 int ret;
429 u8 params[2] = { feature >> 8, feature & 0x00FF };
431 ret = hidpp_send_fap_command_sync(hidpp,
432 HIDPP_PAGE_ROOT_IDX,
433 CMD_ROOT_GET_FEATURE,
434 params, 2, &response);
435 if (ret)
436 return ret;
438 *feature_index = response.fap.params[0];
439 *feature_type = response.fap.params[1];
441 return ret;
444 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
446 struct hidpp_report response;
447 int ret;
449 ret = hidpp_send_fap_command_sync(hidpp,
450 HIDPP_PAGE_ROOT_IDX,
451 CMD_ROOT_GET_PROTOCOL_VERSION,
452 NULL, 0, &response);
454 if (ret == HIDPP_ERROR_INVALID_SUBID) {
455 hidpp->protocol_major = 1;
456 hidpp->protocol_minor = 0;
457 return 0;
460 /* the device might not be connected */
461 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
462 return -EIO;
464 if (ret > 0) {
465 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
466 __func__, ret);
467 return -EPROTO;
469 if (ret)
470 return ret;
472 hidpp->protocol_major = response.fap.params[0];
473 hidpp->protocol_minor = response.fap.params[1];
475 return ret;
478 static bool hidpp_is_connected(struct hidpp_device *hidpp)
480 int ret;
482 ret = hidpp_root_get_protocol_version(hidpp);
483 if (!ret)
484 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
485 hidpp->protocol_major, hidpp->protocol_minor);
486 return ret == 0;
489 /* -------------------------------------------------------------------------- */
490 /* 0x0005: GetDeviceNameType */
491 /* -------------------------------------------------------------------------- */
493 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
495 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
496 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
497 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
499 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
500 u8 feature_index, u8 *nameLength)
502 struct hidpp_report response;
503 int ret;
505 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
506 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
508 if (ret > 0) {
509 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
510 __func__, ret);
511 return -EPROTO;
513 if (ret)
514 return ret;
516 *nameLength = response.fap.params[0];
518 return ret;
521 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
522 u8 feature_index, u8 char_index, char *device_name, int len_buf)
524 struct hidpp_report response;
525 int ret, i;
526 int count;
528 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
529 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
530 &response);
532 if (ret > 0) {
533 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
534 __func__, ret);
535 return -EPROTO;
537 if (ret)
538 return ret;
540 switch (response.report_id) {
541 case REPORT_ID_HIDPP_VERY_LONG:
542 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
543 break;
544 case REPORT_ID_HIDPP_LONG:
545 count = HIDPP_REPORT_LONG_LENGTH - 4;
546 break;
547 case REPORT_ID_HIDPP_SHORT:
548 count = HIDPP_REPORT_SHORT_LENGTH - 4;
549 break;
550 default:
551 return -EPROTO;
554 if (len_buf < count)
555 count = len_buf;
557 for (i = 0; i < count; i++)
558 device_name[i] = response.fap.params[i];
560 return count;
563 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
565 u8 feature_type;
566 u8 feature_index;
567 u8 __name_length;
568 char *name;
569 unsigned index = 0;
570 int ret;
572 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
573 &feature_index, &feature_type);
574 if (ret)
575 return NULL;
577 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
578 &__name_length);
579 if (ret)
580 return NULL;
582 name = kzalloc(__name_length + 1, GFP_KERNEL);
583 if (!name)
584 return NULL;
586 while (index < __name_length) {
587 ret = hidpp_devicenametype_get_device_name(hidpp,
588 feature_index, index, name + index,
589 __name_length - index);
590 if (ret <= 0) {
591 kfree(name);
592 return NULL;
594 index += ret;
597 /* include the terminating '\0' */
598 hidpp_prefix_name(&name, __name_length + 1);
600 return name;
603 /* -------------------------------------------------------------------------- */
604 /* 0x6010: Touchpad FW items */
605 /* -------------------------------------------------------------------------- */
607 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
609 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
611 struct hidpp_touchpad_fw_items {
612 uint8_t presence;
613 uint8_t desired_state;
614 uint8_t state;
615 uint8_t persistent;
619 * send a set state command to the device by reading the current items->state
620 * field. items is then filled with the current state.
622 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
623 u8 feature_index,
624 struct hidpp_touchpad_fw_items *items)
626 struct hidpp_report response;
627 int ret;
628 u8 *params = (u8 *)response.fap.params;
630 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
631 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
633 if (ret > 0) {
634 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
635 __func__, ret);
636 return -EPROTO;
638 if (ret)
639 return ret;
641 items->presence = params[0];
642 items->desired_state = params[1];
643 items->state = params[2];
644 items->persistent = params[3];
646 return 0;
649 /* -------------------------------------------------------------------------- */
650 /* 0x6100: TouchPadRawXY */
651 /* -------------------------------------------------------------------------- */
653 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
655 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
656 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
658 #define EVENT_TOUCHPAD_RAW_XY 0x00
660 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
661 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
663 struct hidpp_touchpad_raw_info {
664 u16 x_size;
665 u16 y_size;
666 u8 z_range;
667 u8 area_range;
668 u8 timestamp_unit;
669 u8 maxcontacts;
670 u8 origin;
671 u16 res;
674 struct hidpp_touchpad_raw_xy_finger {
675 u8 contact_type;
676 u8 contact_status;
677 u16 x;
678 u16 y;
679 u8 z;
680 u8 area;
681 u8 finger_id;
684 struct hidpp_touchpad_raw_xy {
685 u16 timestamp;
686 struct hidpp_touchpad_raw_xy_finger fingers[2];
687 u8 spurious_flag;
688 u8 end_of_frame;
689 u8 finger_count;
690 u8 button;
693 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
694 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
696 struct hidpp_report response;
697 int ret;
698 u8 *params = (u8 *)response.fap.params;
700 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
701 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
703 if (ret > 0) {
704 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
705 __func__, ret);
706 return -EPROTO;
708 if (ret)
709 return ret;
711 raw_info->x_size = get_unaligned_be16(&params[0]);
712 raw_info->y_size = get_unaligned_be16(&params[2]);
713 raw_info->z_range = params[4];
714 raw_info->area_range = params[5];
715 raw_info->maxcontacts = params[7];
716 raw_info->origin = params[8];
717 /* res is given in unit per inch */
718 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
720 return ret;
723 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
724 u8 feature_index, bool send_raw_reports,
725 bool sensor_enhanced_settings)
727 struct hidpp_report response;
730 * Params:
731 * bit 0 - enable raw
732 * bit 1 - 16bit Z, no area
733 * bit 2 - enhanced sensitivity
734 * bit 3 - width, height (4 bits each) instead of area
735 * bit 4 - send raw + gestures (degrades smoothness)
736 * remaining bits - reserved
738 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
740 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
741 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
744 static void hidpp_touchpad_touch_event(u8 *data,
745 struct hidpp_touchpad_raw_xy_finger *finger)
747 u8 x_m = data[0] << 2;
748 u8 y_m = data[2] << 2;
750 finger->x = x_m << 6 | data[1];
751 finger->y = y_m << 6 | data[3];
753 finger->contact_type = data[0] >> 6;
754 finger->contact_status = data[2] >> 6;
756 finger->z = data[4];
757 finger->area = data[5];
758 finger->finger_id = data[6] >> 4;
761 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
762 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
764 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
765 raw_xy->end_of_frame = data[8] & 0x01;
766 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
767 raw_xy->finger_count = data[15] & 0x0f;
768 raw_xy->button = (data[8] >> 2) & 0x01;
770 if (raw_xy->finger_count) {
771 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
772 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
776 /* ************************************************************************** */
777 /* */
778 /* Device Support */
779 /* */
780 /* ************************************************************************** */
782 /* -------------------------------------------------------------------------- */
783 /* Touchpad HID++ devices */
784 /* -------------------------------------------------------------------------- */
786 #define WTP_MANUAL_RESOLUTION 39
788 struct wtp_data {
789 struct input_dev *input;
790 u16 x_size, y_size;
791 u8 finger_count;
792 u8 mt_feature_index;
793 u8 button_feature_index;
794 u8 maxcontacts;
795 bool flip_y;
796 unsigned int resolution;
799 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
800 struct hid_field *field, struct hid_usage *usage,
801 unsigned long **bit, int *max)
803 return -1;
806 static void wtp_populate_input(struct hidpp_device *hidpp,
807 struct input_dev *input_dev, bool origin_is_hid_core)
809 struct wtp_data *wd = hidpp->private_data;
811 __set_bit(EV_ABS, input_dev->evbit);
812 __set_bit(EV_KEY, input_dev->evbit);
813 __clear_bit(EV_REL, input_dev->evbit);
814 __clear_bit(EV_LED, input_dev->evbit);
816 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
817 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
818 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
819 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
821 /* Max pressure is not given by the devices, pick one */
822 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
824 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
826 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
827 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
828 else
829 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
831 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
832 INPUT_MT_DROP_UNUSED);
834 wd->input = input_dev;
837 static void wtp_touch_event(struct wtp_data *wd,
838 struct hidpp_touchpad_raw_xy_finger *touch_report)
840 int slot;
842 if (!touch_report->finger_id || touch_report->contact_type)
843 /* no actual data */
844 return;
846 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
848 input_mt_slot(wd->input, slot);
849 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
850 touch_report->contact_status);
851 if (touch_report->contact_status) {
852 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
853 touch_report->x);
854 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
855 wd->flip_y ? wd->y_size - touch_report->y :
856 touch_report->y);
857 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
858 touch_report->area);
862 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
863 struct hidpp_touchpad_raw_xy *raw)
865 struct wtp_data *wd = hidpp->private_data;
866 int i;
868 for (i = 0; i < 2; i++)
869 wtp_touch_event(wd, &(raw->fingers[i]));
871 if (raw->end_of_frame &&
872 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
873 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
875 if (raw->end_of_frame || raw->finger_count <= 2) {
876 input_mt_sync_frame(wd->input);
877 input_sync(wd->input);
881 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
883 struct wtp_data *wd = hidpp->private_data;
884 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
885 (data[7] >> 4) * (data[7] >> 4)) / 2;
886 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
887 (data[13] >> 4) * (data[13] >> 4)) / 2;
888 struct hidpp_touchpad_raw_xy raw = {
889 .timestamp = data[1],
890 .fingers = {
892 .contact_type = 0,
893 .contact_status = !!data[7],
894 .x = get_unaligned_le16(&data[3]),
895 .y = get_unaligned_le16(&data[5]),
896 .z = c1_area,
897 .area = c1_area,
898 .finger_id = data[2],
899 }, {
900 .contact_type = 0,
901 .contact_status = !!data[13],
902 .x = get_unaligned_le16(&data[9]),
903 .y = get_unaligned_le16(&data[11]),
904 .z = c2_area,
905 .area = c2_area,
906 .finger_id = data[8],
909 .finger_count = wd->maxcontacts,
910 .spurious_flag = 0,
911 .end_of_frame = (data[0] >> 7) == 0,
912 .button = data[0] & 0x01,
915 wtp_send_raw_xy_event(hidpp, &raw);
917 return 1;
920 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
922 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
923 struct wtp_data *wd = hidpp->private_data;
924 struct hidpp_report *report = (struct hidpp_report *)data;
925 struct hidpp_touchpad_raw_xy raw;
927 if (!wd || !wd->input)
928 return 1;
930 switch (data[0]) {
931 case 0x02:
932 if (size < 2) {
933 hid_err(hdev, "Received HID report of bad size (%d)",
934 size);
935 return 1;
937 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
938 input_event(wd->input, EV_KEY, BTN_LEFT,
939 !!(data[1] & 0x01));
940 input_event(wd->input, EV_KEY, BTN_RIGHT,
941 !!(data[1] & 0x02));
942 input_sync(wd->input);
943 return 0;
944 } else {
945 if (size < 21)
946 return 1;
947 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
949 case REPORT_ID_HIDPP_LONG:
950 /* size is already checked in hidpp_raw_event. */
951 if ((report->fap.feature_index != wd->mt_feature_index) ||
952 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
953 return 1;
954 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
956 wtp_send_raw_xy_event(hidpp, &raw);
957 return 0;
960 return 0;
963 static int wtp_get_config(struct hidpp_device *hidpp)
965 struct wtp_data *wd = hidpp->private_data;
966 struct hidpp_touchpad_raw_info raw_info = {0};
967 u8 feature_type;
968 int ret;
970 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
971 &wd->mt_feature_index, &feature_type);
972 if (ret)
973 /* means that the device is not powered up */
974 return ret;
976 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
977 &raw_info);
978 if (ret)
979 return ret;
981 wd->x_size = raw_info.x_size;
982 wd->y_size = raw_info.y_size;
983 wd->maxcontacts = raw_info.maxcontacts;
984 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
985 wd->resolution = raw_info.res;
986 if (!wd->resolution)
987 wd->resolution = WTP_MANUAL_RESOLUTION;
989 return 0;
992 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
994 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
995 struct wtp_data *wd;
997 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
998 GFP_KERNEL);
999 if (!wd)
1000 return -ENOMEM;
1002 hidpp->private_data = wd;
1004 return 0;
1007 static int wtp_connect(struct hid_device *hdev, bool connected)
1009 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1010 struct wtp_data *wd = hidpp->private_data;
1011 int ret;
1013 if (!connected)
1014 return 0;
1016 if (!wd->x_size) {
1017 ret = wtp_get_config(hidpp);
1018 if (ret) {
1019 hid_err(hdev, "Can not get wtp config: %d\n", ret);
1020 return ret;
1024 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
1025 true, true);
1028 /* ------------------------------------------------------------------------- */
1029 /* Logitech M560 devices */
1030 /* ------------------------------------------------------------------------- */
1033 * Logitech M560 protocol overview
1035 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1036 * the sides buttons are pressed, it sends some keyboard keys events
1037 * instead of buttons ones.
1038 * To complicate things further, the middle button keys sequence
1039 * is different from the odd press and the even press.
1041 * forward button -> Super_R
1042 * backward button -> Super_L+'d' (press only)
1043 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1044 * 2nd time: left-click (press only)
1045 * NB: press-only means that when the button is pressed, the
1046 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1047 * together sequentially; instead when the button is released, no event is
1048 * generated !
1050 * With the command
1051 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1052 * the mouse reacts differently:
1053 * - it never sends a keyboard key event
1054 * - for the three mouse button it sends:
1055 * middle button press 11<xx>0a 3500af00...
1056 * side 1 button (forward) press 11<xx>0a 3500b000...
1057 * side 2 button (backward) press 11<xx>0a 3500ae00...
1058 * middle/side1/side2 button release 11<xx>0a 35000000...
1061 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1063 struct m560_private_data {
1064 struct input_dev *input;
1067 /* how buttons are mapped in the report */
1068 #define M560_MOUSE_BTN_LEFT 0x01
1069 #define M560_MOUSE_BTN_RIGHT 0x02
1070 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1071 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1073 #define M560_SUB_ID 0x0a
1074 #define M560_BUTTON_MODE_REGISTER 0x35
1076 static int m560_send_config_command(struct hid_device *hdev, bool connected)
1078 struct hidpp_report response;
1079 struct hidpp_device *hidpp_dev;
1081 hidpp_dev = hid_get_drvdata(hdev);
1083 if (!connected)
1084 return -ENODEV;
1086 return hidpp_send_rap_command_sync(
1087 hidpp_dev,
1088 REPORT_ID_HIDPP_SHORT,
1089 M560_SUB_ID,
1090 M560_BUTTON_MODE_REGISTER,
1091 (u8 *)m560_config_parameter,
1092 sizeof(m560_config_parameter),
1093 &response
1097 static int m560_allocate(struct hid_device *hdev)
1099 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1100 struct m560_private_data *d;
1102 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1103 GFP_KERNEL);
1104 if (!d)
1105 return -ENOMEM;
1107 hidpp->private_data = d;
1109 return 0;
1112 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1114 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1115 struct m560_private_data *mydata = hidpp->private_data;
1117 /* sanity check */
1118 if (!mydata || !mydata->input) {
1119 hid_err(hdev, "error in parameter\n");
1120 return -EINVAL;
1123 if (size < 7) {
1124 hid_err(hdev, "error in report\n");
1125 return 0;
1128 if (data[0] == REPORT_ID_HIDPP_LONG &&
1129 data[2] == M560_SUB_ID && data[6] == 0x00) {
1131 * m560 mouse report for middle, forward and backward button
1133 * data[0] = 0x11
1134 * data[1] = device-id
1135 * data[2] = 0x0a
1136 * data[5] = 0xaf -> middle
1137 * 0xb0 -> forward
1138 * 0xae -> backward
1139 * 0x00 -> release all
1140 * data[6] = 0x00
1143 switch (data[5]) {
1144 case 0xaf:
1145 input_report_key(mydata->input, BTN_MIDDLE, 1);
1146 break;
1147 case 0xb0:
1148 input_report_key(mydata->input, BTN_FORWARD, 1);
1149 break;
1150 case 0xae:
1151 input_report_key(mydata->input, BTN_BACK, 1);
1152 break;
1153 case 0x00:
1154 input_report_key(mydata->input, BTN_BACK, 0);
1155 input_report_key(mydata->input, BTN_FORWARD, 0);
1156 input_report_key(mydata->input, BTN_MIDDLE, 0);
1157 break;
1158 default:
1159 hid_err(hdev, "error in report\n");
1160 return 0;
1162 input_sync(mydata->input);
1164 } else if (data[0] == 0x02) {
1166 * Logitech M560 mouse report
1168 * data[0] = type (0x02)
1169 * data[1..2] = buttons
1170 * data[3..5] = xy
1171 * data[6] = wheel
1174 int v;
1176 input_report_key(mydata->input, BTN_LEFT,
1177 !!(data[1] & M560_MOUSE_BTN_LEFT));
1178 input_report_key(mydata->input, BTN_RIGHT,
1179 !!(data[1] & M560_MOUSE_BTN_RIGHT));
1181 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
1182 input_report_rel(mydata->input, REL_HWHEEL, -1);
1183 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
1184 input_report_rel(mydata->input, REL_HWHEEL, 1);
1186 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
1187 input_report_rel(mydata->input, REL_X, v);
1189 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
1190 input_report_rel(mydata->input, REL_Y, v);
1192 v = hid_snto32(data[6], 8);
1193 input_report_rel(mydata->input, REL_WHEEL, v);
1195 input_sync(mydata->input);
1198 return 1;
1201 static void m560_populate_input(struct hidpp_device *hidpp,
1202 struct input_dev *input_dev, bool origin_is_hid_core)
1204 struct m560_private_data *mydata = hidpp->private_data;
1206 mydata->input = input_dev;
1208 __set_bit(EV_KEY, mydata->input->evbit);
1209 __set_bit(BTN_MIDDLE, mydata->input->keybit);
1210 __set_bit(BTN_RIGHT, mydata->input->keybit);
1211 __set_bit(BTN_LEFT, mydata->input->keybit);
1212 __set_bit(BTN_BACK, mydata->input->keybit);
1213 __set_bit(BTN_FORWARD, mydata->input->keybit);
1215 __set_bit(EV_REL, mydata->input->evbit);
1216 __set_bit(REL_X, mydata->input->relbit);
1217 __set_bit(REL_Y, mydata->input->relbit);
1218 __set_bit(REL_WHEEL, mydata->input->relbit);
1219 __set_bit(REL_HWHEEL, mydata->input->relbit);
1222 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1223 struct hid_field *field, struct hid_usage *usage,
1224 unsigned long **bit, int *max)
1226 return -1;
1229 /* ------------------------------------------------------------------------- */
1230 /* Logitech K400 devices */
1231 /* ------------------------------------------------------------------------- */
1234 * The Logitech K400 keyboard has an embedded touchpad which is seen
1235 * as a mouse from the OS point of view. There is a hardware shortcut to disable
1236 * tap-to-click but the setting is not remembered accross reset, annoying some
1237 * users.
1239 * We can toggle this feature from the host by using the feature 0x6010:
1240 * Touchpad FW items
1243 struct k400_private_data {
1244 u8 feature_index;
1247 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
1249 struct k400_private_data *k400 = hidpp->private_data;
1250 struct hidpp_touchpad_fw_items items = {};
1251 int ret;
1252 u8 feature_type;
1254 if (!k400->feature_index) {
1255 ret = hidpp_root_get_feature(hidpp,
1256 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
1257 &k400->feature_index, &feature_type);
1258 if (ret)
1259 /* means that the device is not powered up */
1260 return ret;
1263 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
1264 if (ret)
1265 return ret;
1267 return 0;
1270 static int k400_allocate(struct hid_device *hdev)
1272 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1273 struct k400_private_data *k400;
1275 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
1276 GFP_KERNEL);
1277 if (!k400)
1278 return -ENOMEM;
1280 hidpp->private_data = k400;
1282 return 0;
1285 static int k400_connect(struct hid_device *hdev, bool connected)
1287 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1289 if (!connected)
1290 return 0;
1292 if (!disable_tap_to_click)
1293 return 0;
1295 return k400_disable_tap_to_click(hidpp);
1298 /* ------------------------------------------------------------------------- */
1299 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
1300 /* ------------------------------------------------------------------------- */
1302 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
1304 /* Using session ID = 1 */
1305 #define CMD_G920_FORCE_GET_APERTURE 0x51
1306 #define CMD_G920_FORCE_SET_APERTURE 0x61
1308 struct g920_private_data {
1309 u8 force_feature;
1310 u16 range;
1313 static ssize_t g920_range_show(struct device *dev, struct device_attribute *attr,
1314 char *buf)
1316 struct hid_device *hid = to_hid_device(dev);
1317 struct hidpp_device *hidpp = hid_get_drvdata(hid);
1318 struct g920_private_data *pdata;
1320 pdata = hidpp->private_data;
1321 if (!pdata) {
1322 hid_err(hid, "Private driver data not found!\n");
1323 return -EINVAL;
1326 return scnprintf(buf, PAGE_SIZE, "%u\n", pdata->range);
1329 static ssize_t g920_range_store(struct device *dev, struct device_attribute *attr,
1330 const char *buf, size_t count)
1332 struct hid_device *hid = to_hid_device(dev);
1333 struct hidpp_device *hidpp = hid_get_drvdata(hid);
1334 struct g920_private_data *pdata;
1335 struct hidpp_report response;
1336 u8 params[2];
1337 int ret;
1338 u16 range = simple_strtoul(buf, NULL, 10);
1340 pdata = hidpp->private_data;
1341 if (!pdata) {
1342 hid_err(hid, "Private driver data not found!\n");
1343 return -EINVAL;
1346 if (range < 180)
1347 range = 180;
1348 else if (range > 900)
1349 range = 900;
1351 params[0] = range >> 8;
1352 params[1] = range & 0x00FF;
1354 ret = hidpp_send_fap_command_sync(hidpp, pdata->force_feature,
1355 CMD_G920_FORCE_SET_APERTURE, params, 2, &response);
1356 if (ret)
1357 return ret;
1359 pdata->range = range;
1360 return count;
1363 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, g920_range_show, g920_range_store);
1365 static int g920_allocate(struct hid_device *hdev)
1367 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1368 struct g920_private_data *pdata;
1370 pdata = devm_kzalloc(&hdev->dev, sizeof(struct g920_private_data),
1371 GFP_KERNEL);
1372 if (!pdata)
1373 return -ENOMEM;
1375 hidpp->private_data = pdata;
1377 return 0;
1380 static int g920_get_config(struct hidpp_device *hidpp)
1382 struct g920_private_data *pdata = hidpp->private_data;
1383 struct hidpp_report response;
1384 u8 feature_type;
1385 u8 feature_index;
1386 int ret;
1388 pdata = hidpp->private_data;
1389 if (!pdata) {
1390 hid_err(hidpp->hid_dev, "Private driver data not found!\n");
1391 return -EINVAL;
1394 /* Find feature and store for later use */
1395 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
1396 &feature_index, &feature_type);
1397 if (ret)
1398 return ret;
1400 pdata->force_feature = feature_index;
1402 /* Read current Range */
1403 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1404 CMD_G920_FORCE_GET_APERTURE, NULL, 0, &response);
1405 if (ret > 0) {
1406 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1407 __func__, ret);
1408 return -EPROTO;
1410 if (ret)
1411 return ret;
1413 pdata->range = get_unaligned_be16(&response.fap.params[0]);
1415 /* Create sysfs interface */
1416 ret = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1417 if (ret)
1418 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d\n", ret);
1420 return 0;
1423 /* -------------------------------------------------------------------------- */
1424 /* Generic HID++ devices */
1425 /* -------------------------------------------------------------------------- */
1427 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1428 struct hid_field *field, struct hid_usage *usage,
1429 unsigned long **bit, int *max)
1431 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1433 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1434 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
1435 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
1436 field->application != HID_GD_MOUSE)
1437 return m560_input_mapping(hdev, hi, field, usage, bit, max);
1439 return 0;
1442 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
1443 struct hid_field *field, struct hid_usage *usage,
1444 unsigned long **bit, int *max)
1446 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1448 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
1449 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1450 if (usage->type == EV_ABS && (usage->code == ABS_X ||
1451 usage->code == ABS_Y || usage->code == ABS_Z ||
1452 usage->code == ABS_RZ)) {
1453 field->application = HID_GD_MULTIAXIS;
1457 return 0;
1461 static void hidpp_populate_input(struct hidpp_device *hidpp,
1462 struct input_dev *input, bool origin_is_hid_core)
1464 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1465 wtp_populate_input(hidpp, input, origin_is_hid_core);
1466 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1467 m560_populate_input(hidpp, input, origin_is_hid_core);
1470 static int hidpp_input_configured(struct hid_device *hdev,
1471 struct hid_input *hidinput)
1473 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1474 struct input_dev *input = hidinput->input;
1476 hidpp_populate_input(hidpp, input, true);
1478 return 0;
1481 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
1482 int size)
1484 struct hidpp_report *question = hidpp->send_receive_buf;
1485 struct hidpp_report *answer = hidpp->send_receive_buf;
1486 struct hidpp_report *report = (struct hidpp_report *)data;
1489 * If the mutex is locked then we have a pending answer from a
1490 * previously sent command.
1492 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
1494 * Check for a correct hidpp20 answer or the corresponding
1495 * error
1497 if (hidpp_match_answer(question, report) ||
1498 hidpp_match_error(question, report)) {
1499 *answer = *report;
1500 hidpp->answer_available = true;
1501 wake_up(&hidpp->wait);
1503 * This was an answer to a command that this driver sent
1504 * We return 1 to hid-core to avoid forwarding the
1505 * command upstream as it has been treated by the driver
1508 return 1;
1512 if (unlikely(hidpp_report_is_connect_event(report))) {
1513 atomic_set(&hidpp->connected,
1514 !(report->rap.params[0] & (1 << 6)));
1515 if ((hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) &&
1516 (schedule_work(&hidpp->work) == 0))
1517 dbg_hid("%s: connect event already queued\n", __func__);
1518 return 1;
1521 return 0;
1524 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1525 u8 *data, int size)
1527 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1528 int ret = 0;
1530 /* Generic HID++ processing. */
1531 switch (data[0]) {
1532 case REPORT_ID_HIDPP_VERY_LONG:
1533 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
1534 hid_err(hdev, "received hid++ report of bad size (%d)",
1535 size);
1536 return 1;
1538 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1539 break;
1540 case REPORT_ID_HIDPP_LONG:
1541 if (size != HIDPP_REPORT_LONG_LENGTH) {
1542 hid_err(hdev, "received hid++ report of bad size (%d)",
1543 size);
1544 return 1;
1546 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1547 break;
1548 case REPORT_ID_HIDPP_SHORT:
1549 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1550 hid_err(hdev, "received hid++ report of bad size (%d)",
1551 size);
1552 return 1;
1554 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1555 break;
1558 /* If no report is available for further processing, skip calling
1559 * raw_event of subclasses. */
1560 if (ret != 0)
1561 return ret;
1563 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1564 return wtp_raw_event(hdev, data, size);
1565 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1566 return m560_raw_event(hdev, data, size);
1568 return 0;
1571 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
1573 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1574 char *name;
1576 if (use_unifying)
1578 * the device is connected through an Unifying receiver, and
1579 * might not be already connected.
1580 * Ask the receiver for its name.
1582 name = hidpp_get_unifying_name(hidpp);
1583 else
1584 name = hidpp_get_device_name(hidpp);
1586 if (!name) {
1587 hid_err(hdev, "unable to retrieve the name of the device");
1588 } else {
1589 dbg_hid("HID++: Got name: %s\n", name);
1590 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1593 kfree(name);
1596 static int hidpp_input_open(struct input_dev *dev)
1598 struct hid_device *hid = input_get_drvdata(dev);
1600 return hid_hw_open(hid);
1603 static void hidpp_input_close(struct input_dev *dev)
1605 struct hid_device *hid = input_get_drvdata(dev);
1607 hid_hw_close(hid);
1610 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1612 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
1613 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1615 if (!input_dev)
1616 return NULL;
1618 input_set_drvdata(input_dev, hdev);
1619 input_dev->open = hidpp_input_open;
1620 input_dev->close = hidpp_input_close;
1622 input_dev->name = hidpp->name;
1623 input_dev->phys = hdev->phys;
1624 input_dev->uniq = hdev->uniq;
1625 input_dev->id.bustype = hdev->bus;
1626 input_dev->id.vendor = hdev->vendor;
1627 input_dev->id.product = hdev->product;
1628 input_dev->id.version = hdev->version;
1629 input_dev->dev.parent = &hdev->dev;
1631 return input_dev;
1634 static void hidpp_connect_event(struct hidpp_device *hidpp)
1636 struct hid_device *hdev = hidpp->hid_dev;
1637 int ret = 0;
1638 bool connected = atomic_read(&hidpp->connected);
1639 struct input_dev *input;
1640 char *name, *devm_name;
1642 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1643 ret = wtp_connect(hdev, connected);
1644 if (ret)
1645 return;
1646 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1647 ret = m560_send_config_command(hdev, connected);
1648 if (ret)
1649 return;
1650 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
1651 ret = k400_connect(hdev, connected);
1652 if (ret)
1653 return;
1656 if (!connected || hidpp->delayed_input)
1657 return;
1659 /* the device is already connected, we can ask for its name and
1660 * protocol */
1661 if (!hidpp->protocol_major) {
1662 ret = !hidpp_is_connected(hidpp);
1663 if (ret) {
1664 hid_err(hdev, "Can not get the protocol version.\n");
1665 return;
1667 hid_info(hdev, "HID++ %u.%u device connected.\n",
1668 hidpp->protocol_major, hidpp->protocol_minor);
1671 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
1672 /* if HID created the input nodes for us, we can stop now */
1673 return;
1675 if (!hidpp->name || hidpp->name == hdev->name) {
1676 name = hidpp_get_device_name(hidpp);
1677 if (!name) {
1678 hid_err(hdev,
1679 "unable to retrieve the name of the device");
1680 return;
1683 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1684 kfree(name);
1685 if (!devm_name)
1686 return;
1688 hidpp->name = devm_name;
1691 input = hidpp_allocate_input(hdev);
1692 if (!input) {
1693 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1694 return;
1697 hidpp_populate_input(hidpp, input, false);
1699 ret = input_register_device(input);
1700 if (ret)
1701 input_free_device(input);
1703 hidpp->delayed_input = input;
1706 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1708 struct hidpp_device *hidpp;
1709 int ret;
1710 bool connected;
1711 unsigned int connect_mask = HID_CONNECT_DEFAULT;
1713 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1714 GFP_KERNEL);
1715 if (!hidpp)
1716 return -ENOMEM;
1718 hidpp->hid_dev = hdev;
1719 hidpp->name = hdev->name;
1720 hid_set_drvdata(hdev, hidpp);
1722 hidpp->quirks = id->driver_data;
1724 if (disable_raw_mode) {
1725 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
1726 hidpp->quirks &= ~HIDPP_QUIRK_CONNECT_EVENTS;
1727 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
1730 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1731 ret = wtp_allocate(hdev, id);
1732 if (ret)
1733 goto allocate_fail;
1734 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1735 ret = m560_allocate(hdev);
1736 if (ret)
1737 goto allocate_fail;
1738 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
1739 ret = k400_allocate(hdev);
1740 if (ret)
1741 goto allocate_fail;
1742 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1743 ret = g920_allocate(hdev);
1744 if (ret)
1745 goto allocate_fail;
1748 INIT_WORK(&hidpp->work, delayed_work_cb);
1749 mutex_init(&hidpp->send_mutex);
1750 init_waitqueue_head(&hidpp->wait);
1752 ret = hid_parse(hdev);
1753 if (ret) {
1754 hid_err(hdev, "%s:parse failed\n", __func__);
1755 goto hid_parse_fail;
1758 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
1759 connect_mask &= ~HID_CONNECT_HIDINPUT;
1761 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1762 ret = hid_hw_start(hdev, connect_mask);
1763 if (ret) {
1764 hid_err(hdev, "hw start failed\n");
1765 goto hid_hw_start_fail;
1767 ret = hid_hw_open(hdev);
1768 if (ret < 0) {
1769 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1770 __func__, ret);
1771 hid_hw_stop(hdev);
1772 goto hid_hw_start_fail;
1777 /* Allow incoming packets */
1778 hid_device_io_start(hdev);
1780 connected = hidpp_is_connected(hidpp);
1781 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1782 if (!connected) {
1783 ret = -ENODEV;
1784 hid_err(hdev, "Device not connected");
1785 goto hid_hw_open_failed;
1788 hid_info(hdev, "HID++ %u.%u device connected.\n",
1789 hidpp->protocol_major, hidpp->protocol_minor);
1792 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
1793 atomic_set(&hidpp->connected, connected);
1795 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
1796 ret = wtp_get_config(hidpp);
1797 if (ret)
1798 goto hid_hw_open_failed;
1799 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
1800 ret = g920_get_config(hidpp);
1801 if (ret)
1802 goto hid_hw_open_failed;
1805 /* Block incoming packets */
1806 hid_device_io_stop(hdev);
1808 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
1809 ret = hid_hw_start(hdev, connect_mask);
1810 if (ret) {
1811 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1812 goto hid_hw_start_fail;
1816 if (hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) {
1817 /* Allow incoming packets */
1818 hid_device_io_start(hdev);
1820 hidpp_connect_event(hidpp);
1823 return ret;
1825 hid_hw_open_failed:
1826 hid_device_io_stop(hdev);
1827 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1828 device_remove_file(&hdev->dev, &dev_attr_range);
1829 hid_hw_close(hdev);
1830 hid_hw_stop(hdev);
1832 hid_hw_start_fail:
1833 hid_parse_fail:
1834 cancel_work_sync(&hidpp->work);
1835 mutex_destroy(&hidpp->send_mutex);
1836 allocate_fail:
1837 hid_set_drvdata(hdev, NULL);
1838 return ret;
1841 static void hidpp_remove(struct hid_device *hdev)
1843 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1845 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1846 device_remove_file(&hdev->dev, &dev_attr_range);
1847 hid_hw_close(hdev);
1849 hid_hw_stop(hdev);
1850 cancel_work_sync(&hidpp->work);
1851 mutex_destroy(&hidpp->send_mutex);
1854 static const struct hid_device_id hidpp_devices[] = {
1855 { /* wireless touchpad */
1856 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1857 USB_VENDOR_ID_LOGITECH, 0x4011),
1858 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1859 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
1860 { /* wireless touchpad T650 */
1861 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1862 USB_VENDOR_ID_LOGITECH, 0x4101),
1863 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
1864 { /* wireless touchpad T651 */
1865 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1866 USB_DEVICE_ID_LOGITECH_T651),
1867 .driver_data = HIDPP_QUIRK_CLASS_WTP },
1868 { /* Mouse logitech M560 */
1869 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1870 USB_VENDOR_ID_LOGITECH, 0x402d),
1871 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
1872 { /* Keyboard logitech K400 */
1873 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1874 USB_VENDOR_ID_LOGITECH, 0x4024),
1875 .driver_data = HIDPP_QUIRK_CONNECT_EVENTS | HIDPP_QUIRK_CLASS_K400 },
1877 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1878 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1880 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
1881 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
1885 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1887 static struct hid_driver hidpp_driver = {
1888 .name = "logitech-hidpp-device",
1889 .id_table = hidpp_devices,
1890 .probe = hidpp_probe,
1891 .remove = hidpp_remove,
1892 .raw_event = hidpp_raw_event,
1893 .input_configured = hidpp_input_configured,
1894 .input_mapping = hidpp_input_mapping,
1895 .input_mapped = hidpp_input_mapped,
1898 module_hid_driver(hidpp_driver);