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.
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/input.h>
19 #include <linux/usb.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/kfifo.h>
25 #include <linux/input/mt.h>
26 #include <linux/workqueue.h>
27 #include <linux/atomic.h>
28 #include <linux/fixp-arith.h>
29 #include <asm/unaligned.h>
30 #include "usbhid/usbhid.h"
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
35 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
37 static bool disable_raw_mode;
38 module_param(disable_raw_mode, bool, 0644);
39 MODULE_PARM_DESC(disable_raw_mode,
40 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
42 static bool disable_tap_to_click;
43 module_param(disable_tap_to_click, bool, 0644);
44 MODULE_PARM_DESC(disable_tap_to_click,
45 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
47 #define REPORT_ID_HIDPP_SHORT 0x10
48 #define REPORT_ID_HIDPP_LONG 0x11
49 #define REPORT_ID_HIDPP_VERY_LONG 0x12
51 #define HIDPP_REPORT_SHORT_LENGTH 7
52 #define HIDPP_REPORT_LONG_LENGTH 20
53 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
55 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
56 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
57 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
58 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
60 /* bits 2..20 are reserved for classes */
61 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
62 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
63 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
64 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
65 #define HIDPP_QUIRK_UNIFYING BIT(25)
67 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
69 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
70 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
71 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
72 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
75 * There are two hidpp protocols in use, the first version hidpp10 is known
76 * as register access protocol or RAP, the second version hidpp20 is known as
77 * feature access protocol or FAP
79 * Most older devices (including the Unifying usb receiver) use the RAP protocol
80 * where as most newer devices use the FAP protocol. Both protocols are
81 * compatible with the underlying transport, which could be usb, Unifiying, or
82 * bluetooth. The message lengths are defined by the hid vendor specific report
83 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
84 * the HIDPP_LONG report type (total message length 20 bytes)
86 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
87 * messages. The Unifying receiver itself responds to RAP messages (device index
88 * is 0xFF for the receiver), and all messages (short or long) with a device
89 * index between 1 and 6 are passed untouched to the corresponding paired
92 * The paired device can be RAP or FAP, it will receive the message untouched
93 * from the Unifiying receiver.
98 u8 funcindex_clientid;
99 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
105 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
108 struct hidpp_report {
114 u8 rawbytes[sizeof(struct fap)];
118 struct hidpp_battery {
120 struct power_supply_desc desc;
121 struct power_supply *ps;
129 struct hidpp_device {
130 struct hid_device *hid_dev;
131 struct mutex send_mutex;
132 void *send_receive_buf;
133 char *name; /* will never be NULL and should not be freed */
134 wait_queue_head_t wait;
135 bool answer_available;
141 struct work_struct work;
142 struct kfifo delayed_work_fifo;
144 struct input_dev *delayed_input;
146 unsigned long quirks;
147 unsigned long capabilities;
149 struct hidpp_battery battery;
152 /* HID++ 1.0 error codes */
153 #define HIDPP_ERROR 0x8f
154 #define HIDPP_ERROR_SUCCESS 0x00
155 #define HIDPP_ERROR_INVALID_SUBID 0x01
156 #define HIDPP_ERROR_INVALID_ADRESS 0x02
157 #define HIDPP_ERROR_INVALID_VALUE 0x03
158 #define HIDPP_ERROR_CONNECT_FAIL 0x04
159 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
160 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
161 #define HIDPP_ERROR_BUSY 0x07
162 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
163 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
164 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
165 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
166 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
167 /* HID++ 2.0 error codes */
168 #define HIDPP20_ERROR 0xff
170 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
172 static int __hidpp_send_report(struct hid_device *hdev,
173 struct hidpp_report *hidpp_report)
175 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
176 int fields_count, ret;
178 hidpp = hid_get_drvdata(hdev);
180 switch (hidpp_report->report_id) {
181 case REPORT_ID_HIDPP_SHORT:
182 fields_count = HIDPP_REPORT_SHORT_LENGTH;
184 case REPORT_ID_HIDPP_LONG:
185 fields_count = HIDPP_REPORT_LONG_LENGTH;
187 case REPORT_ID_HIDPP_VERY_LONG:
188 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
195 * set the device_index as the receiver, it will be overwritten by
196 * hid_hw_request if needed
198 hidpp_report->device_index = 0xff;
200 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
201 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
203 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
204 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
208 return ret == fields_count ? 0 : -1;
212 * hidpp_send_message_sync() returns 0 in case of success, and something else
213 * in case of a failure.
214 * - If ' something else' is positive, that means that an error has been raised
215 * by the protocol itself.
216 * - If ' something else' is negative, that means that we had a classic error
217 * (-ENOMEM, -EPIPE, etc...)
219 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
220 struct hidpp_report *message,
221 struct hidpp_report *response)
225 mutex_lock(&hidpp->send_mutex);
227 hidpp->send_receive_buf = response;
228 hidpp->answer_available = false;
231 * So that we can later validate the answer when it arrives
234 *response = *message;
236 ret = __hidpp_send_report(hidpp->hid_dev, message);
239 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
240 memset(response, 0, sizeof(struct hidpp_report));
244 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
246 dbg_hid("%s:timeout waiting for response\n", __func__);
247 memset(response, 0, sizeof(struct hidpp_report));
251 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
252 response->rap.sub_id == HIDPP_ERROR) {
253 ret = response->rap.params[1];
254 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
258 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
259 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
260 response->fap.feature_index == HIDPP20_ERROR) {
261 ret = response->fap.params[1];
262 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
267 mutex_unlock(&hidpp->send_mutex);
272 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
273 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
274 struct hidpp_report *response)
276 struct hidpp_report *message;
279 if (param_count > sizeof(message->fap.params))
282 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
286 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
287 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
289 message->report_id = REPORT_ID_HIDPP_LONG;
290 message->fap.feature_index = feat_index;
291 message->fap.funcindex_clientid = funcindex_clientid;
292 memcpy(&message->fap.params, params, param_count);
294 ret = hidpp_send_message_sync(hidpp, message, response);
299 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
300 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
301 struct hidpp_report *response)
303 struct hidpp_report *message;
307 case REPORT_ID_HIDPP_SHORT:
308 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
310 case REPORT_ID_HIDPP_LONG:
311 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
313 case REPORT_ID_HIDPP_VERY_LONG:
314 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
320 if (param_count > max_count)
323 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
326 message->report_id = report_id;
327 message->rap.sub_id = sub_id;
328 message->rap.reg_address = reg_address;
329 memcpy(&message->rap.params, params, param_count);
331 ret = hidpp_send_message_sync(hidpp_dev, message, response);
336 static void delayed_work_cb(struct work_struct *work)
338 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
340 hidpp_connect_event(hidpp);
343 static inline bool hidpp_match_answer(struct hidpp_report *question,
344 struct hidpp_report *answer)
346 return (answer->fap.feature_index == question->fap.feature_index) &&
347 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
350 static inline bool hidpp_match_error(struct hidpp_report *question,
351 struct hidpp_report *answer)
353 return ((answer->rap.sub_id == HIDPP_ERROR) ||
354 (answer->fap.feature_index == HIDPP20_ERROR)) &&
355 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
356 (answer->fap.params[0] == question->fap.funcindex_clientid);
359 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
361 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
362 (report->rap.sub_id == 0x41);
366 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
368 static void hidpp_prefix_name(char **name, int name_length)
370 #define PREFIX_LENGTH 9 /* "Logitech " */
375 if (name_length > PREFIX_LENGTH &&
376 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
377 /* The prefix has is already in the name */
380 new_length = PREFIX_LENGTH + name_length;
381 new_name = kzalloc(new_length, GFP_KERNEL);
385 snprintf(new_name, new_length, "Logitech %s", *name);
392 /* -------------------------------------------------------------------------- */
393 /* HIDP++ 1.0 commands */
394 /* -------------------------------------------------------------------------- */
396 #define HIDPP_SET_REGISTER 0x80
397 #define HIDPP_GET_REGISTER 0x81
398 #define HIDPP_SET_LONG_REGISTER 0x82
399 #define HIDPP_GET_LONG_REGISTER 0x83
401 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
402 #define HIDPP_EXTENDED_PAIRING 0x30
403 #define HIDPP_DEVICE_NAME 0x40
405 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
407 struct hidpp_report response;
409 u8 params[1] = { HIDPP_DEVICE_NAME };
413 ret = hidpp_send_rap_command_sync(hidpp_dev,
414 REPORT_ID_HIDPP_SHORT,
415 HIDPP_GET_LONG_REGISTER,
416 HIDPP_REG_PAIRING_INFORMATION,
417 params, 1, &response);
421 len = response.rap.params[1];
423 if (2 + len > sizeof(response.rap.params))
426 name = kzalloc(len + 1, GFP_KERNEL);
430 memcpy(name, &response.rap.params[2], len);
432 /* include the terminating '\0' */
433 hidpp_prefix_name(&name, len + 1);
438 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
440 struct hidpp_report response;
442 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
444 ret = hidpp_send_rap_command_sync(hidpp,
445 REPORT_ID_HIDPP_SHORT,
446 HIDPP_GET_LONG_REGISTER,
447 HIDPP_REG_PAIRING_INFORMATION,
448 params, 1, &response);
453 * We don't care about LE or BE, we will output it as a string
454 * with %4phD, so we need to keep the order.
456 *serial = *((u32 *)&response.rap.params[1]);
460 static int hidpp_unifying_init(struct hidpp_device *hidpp)
462 struct hid_device *hdev = hidpp->hid_dev;
467 ret = hidpp_unifying_get_serial(hidpp, &serial);
471 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
472 hdev->product, &serial);
473 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
475 name = hidpp_unifying_get_name(hidpp);
479 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
480 dbg_hid("HID++ Unifying: Got name: %s\n", name);
486 /* -------------------------------------------------------------------------- */
488 /* -------------------------------------------------------------------------- */
490 #define HIDPP_PAGE_ROOT 0x0000
491 #define HIDPP_PAGE_ROOT_IDX 0x00
493 #define CMD_ROOT_GET_FEATURE 0x01
494 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
496 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
497 u8 *feature_index, u8 *feature_type)
499 struct hidpp_report response;
501 u8 params[2] = { feature >> 8, feature & 0x00FF };
503 ret = hidpp_send_fap_command_sync(hidpp,
505 CMD_ROOT_GET_FEATURE,
506 params, 2, &response);
510 if (response.fap.params[0] == 0)
513 *feature_index = response.fap.params[0];
514 *feature_type = response.fap.params[1];
519 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
521 struct hidpp_report response;
524 ret = hidpp_send_fap_command_sync(hidpp,
526 CMD_ROOT_GET_PROTOCOL_VERSION,
529 if (ret == HIDPP_ERROR_INVALID_SUBID) {
530 hidpp->protocol_major = 1;
531 hidpp->protocol_minor = 0;
535 /* the device might not be connected */
536 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
540 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
547 hidpp->protocol_major = response.fap.params[0];
548 hidpp->protocol_minor = response.fap.params[1];
553 static bool hidpp_is_connected(struct hidpp_device *hidpp)
557 ret = hidpp_root_get_protocol_version(hidpp);
559 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
560 hidpp->protocol_major, hidpp->protocol_minor);
564 /* -------------------------------------------------------------------------- */
565 /* 0x0005: GetDeviceNameType */
566 /* -------------------------------------------------------------------------- */
568 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
570 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
571 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
572 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
574 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
575 u8 feature_index, u8 *nameLength)
577 struct hidpp_report response;
580 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
581 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
584 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
591 *nameLength = response.fap.params[0];
596 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
597 u8 feature_index, u8 char_index, char *device_name, int len_buf)
599 struct hidpp_report response;
603 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
604 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
608 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
615 switch (response.report_id) {
616 case REPORT_ID_HIDPP_VERY_LONG:
617 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
619 case REPORT_ID_HIDPP_LONG:
620 count = HIDPP_REPORT_LONG_LENGTH - 4;
622 case REPORT_ID_HIDPP_SHORT:
623 count = HIDPP_REPORT_SHORT_LENGTH - 4;
632 for (i = 0; i < count; i++)
633 device_name[i] = response.fap.params[i];
638 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
647 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
648 &feature_index, &feature_type);
652 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
657 name = kzalloc(__name_length + 1, GFP_KERNEL);
661 while (index < __name_length) {
662 ret = hidpp_devicenametype_get_device_name(hidpp,
663 feature_index, index, name + index,
664 __name_length - index);
672 /* include the terminating '\0' */
673 hidpp_prefix_name(&name, __name_length + 1);
678 /* -------------------------------------------------------------------------- */
679 /* 0x1000: Battery level status */
680 /* -------------------------------------------------------------------------- */
682 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
684 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
685 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
687 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
689 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
690 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
691 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
693 static int hidpp_map_battery_level(int capacity)
696 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
697 else if (capacity < 31)
698 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
699 else if (capacity < 81)
700 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
701 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
704 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
711 *next_capacity = data[1];
712 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
714 /* When discharging, we can rely on the device reported capacity.
715 * For all other states the device reports 0 (unknown).
718 case 0: /* discharging (in use) */
719 status = POWER_SUPPLY_STATUS_DISCHARGING;
720 *level = hidpp_map_battery_level(*capacity);
722 case 1: /* recharging */
723 status = POWER_SUPPLY_STATUS_CHARGING;
725 case 2: /* charge in final stage */
726 status = POWER_SUPPLY_STATUS_CHARGING;
728 case 3: /* charge complete */
729 status = POWER_SUPPLY_STATUS_FULL;
730 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
733 case 4: /* recharging below optimal speed */
734 status = POWER_SUPPLY_STATUS_CHARGING;
736 /* 5 = invalid battery type
738 7 = other charging error */
740 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
747 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
754 struct hidpp_report response;
756 u8 *params = (u8 *)response.fap.params;
758 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
759 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
762 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
769 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
776 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
779 struct hidpp_report response;
781 u8 *params = (u8 *)response.fap.params;
782 unsigned int level_count, flags;
784 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
785 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
788 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
795 level_count = params[0];
798 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
799 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
801 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
806 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
810 int status, capacity, next_capacity, level;
812 if (hidpp->battery.feature_index == 0) {
813 ret = hidpp_root_get_feature(hidpp,
814 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
815 &hidpp->battery.feature_index,
821 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
822 hidpp->battery.feature_index,
824 &next_capacity, &level);
828 ret = hidpp20_batterylevel_get_battery_info(hidpp,
829 hidpp->battery.feature_index);
833 hidpp->battery.status = status;
834 hidpp->battery.capacity = capacity;
835 hidpp->battery.level = level;
836 /* the capacity is only available when discharging or full */
837 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
838 status == POWER_SUPPLY_STATUS_FULL;
843 static int hidpp20_battery_event(struct hidpp_device *hidpp,
846 struct hidpp_report *report = (struct hidpp_report *)data;
847 int status, capacity, next_capacity, level;
850 if (report->fap.feature_index != hidpp->battery.feature_index ||
851 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
854 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
859 /* the capacity is only available when discharging or full */
860 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
861 status == POWER_SUPPLY_STATUS_FULL;
863 changed = capacity != hidpp->battery.capacity ||
864 level != hidpp->battery.level ||
865 status != hidpp->battery.status;
868 hidpp->battery.level = level;
869 hidpp->battery.capacity = capacity;
870 hidpp->battery.status = status;
871 if (hidpp->battery.ps)
872 power_supply_changed(hidpp->battery.ps);
878 static enum power_supply_property hidpp_battery_props[] = {
879 POWER_SUPPLY_PROP_ONLINE,
880 POWER_SUPPLY_PROP_STATUS,
881 POWER_SUPPLY_PROP_SCOPE,
882 POWER_SUPPLY_PROP_MODEL_NAME,
883 POWER_SUPPLY_PROP_MANUFACTURER,
884 POWER_SUPPLY_PROP_SERIAL_NUMBER,
885 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
886 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
889 static int hidpp_battery_get_property(struct power_supply *psy,
890 enum power_supply_property psp,
891 union power_supply_propval *val)
893 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
897 case POWER_SUPPLY_PROP_STATUS:
898 val->intval = hidpp->battery.status;
900 case POWER_SUPPLY_PROP_CAPACITY:
901 val->intval = hidpp->battery.capacity;
903 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
904 val->intval = hidpp->battery.level;
906 case POWER_SUPPLY_PROP_SCOPE:
907 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
909 case POWER_SUPPLY_PROP_ONLINE:
910 val->intval = hidpp->battery.online;
912 case POWER_SUPPLY_PROP_MODEL_NAME:
913 if (!strncmp(hidpp->name, "Logitech ", 9))
914 val->strval = hidpp->name + 9;
916 val->strval = hidpp->name;
918 case POWER_SUPPLY_PROP_MANUFACTURER:
919 val->strval = "Logitech";
921 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
922 val->strval = hidpp->hid_dev->uniq;
932 /* -------------------------------------------------------------------------- */
933 /* 0x6010: Touchpad FW items */
934 /* -------------------------------------------------------------------------- */
936 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
938 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
940 struct hidpp_touchpad_fw_items {
942 uint8_t desired_state;
948 * send a set state command to the device by reading the current items->state
949 * field. items is then filled with the current state.
951 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
953 struct hidpp_touchpad_fw_items *items)
955 struct hidpp_report response;
957 u8 *params = (u8 *)response.fap.params;
959 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
960 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
963 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
970 items->presence = params[0];
971 items->desired_state = params[1];
972 items->state = params[2];
973 items->persistent = params[3];
978 /* -------------------------------------------------------------------------- */
979 /* 0x6100: TouchPadRawXY */
980 /* -------------------------------------------------------------------------- */
982 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
984 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
985 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
987 #define EVENT_TOUCHPAD_RAW_XY 0x00
989 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
990 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
992 struct hidpp_touchpad_raw_info {
1003 struct hidpp_touchpad_raw_xy_finger {
1013 struct hidpp_touchpad_raw_xy {
1015 struct hidpp_touchpad_raw_xy_finger fingers[2];
1022 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1023 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1025 struct hidpp_report response;
1027 u8 *params = (u8 *)response.fap.params;
1029 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1030 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1033 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1040 raw_info->x_size = get_unaligned_be16(¶ms[0]);
1041 raw_info->y_size = get_unaligned_be16(¶ms[2]);
1042 raw_info->z_range = params[4];
1043 raw_info->area_range = params[5];
1044 raw_info->maxcontacts = params[7];
1045 raw_info->origin = params[8];
1046 /* res is given in unit per inch */
1047 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
1052 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1053 u8 feature_index, bool send_raw_reports,
1054 bool sensor_enhanced_settings)
1056 struct hidpp_report response;
1060 * bit 0 - enable raw
1061 * bit 1 - 16bit Z, no area
1062 * bit 2 - enhanced sensitivity
1063 * bit 3 - width, height (4 bits each) instead of area
1064 * bit 4 - send raw + gestures (degrades smoothness)
1065 * remaining bits - reserved
1067 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1069 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1070 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
1073 static void hidpp_touchpad_touch_event(u8 *data,
1074 struct hidpp_touchpad_raw_xy_finger *finger)
1076 u8 x_m = data[0] << 2;
1077 u8 y_m = data[2] << 2;
1079 finger->x = x_m << 6 | data[1];
1080 finger->y = y_m << 6 | data[3];
1082 finger->contact_type = data[0] >> 6;
1083 finger->contact_status = data[2] >> 6;
1085 finger->z = data[4];
1086 finger->area = data[5];
1087 finger->finger_id = data[6] >> 4;
1090 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1091 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1093 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1094 raw_xy->end_of_frame = data[8] & 0x01;
1095 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1096 raw_xy->finger_count = data[15] & 0x0f;
1097 raw_xy->button = (data[8] >> 2) & 0x01;
1099 if (raw_xy->finger_count) {
1100 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1101 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1105 /* -------------------------------------------------------------------------- */
1106 /* 0x8123: Force feedback support */
1107 /* -------------------------------------------------------------------------- */
1109 #define HIDPP_FF_GET_INFO 0x01
1110 #define HIDPP_FF_RESET_ALL 0x11
1111 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1112 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1113 #define HIDPP_FF_DESTROY_EFFECT 0x41
1114 #define HIDPP_FF_GET_APERTURE 0x51
1115 #define HIDPP_FF_SET_APERTURE 0x61
1116 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1117 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1119 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1120 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1121 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1122 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1124 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1125 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1126 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1127 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1128 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1129 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1130 #define HIDPP_FF_EFFECT_SPRING 0x06
1131 #define HIDPP_FF_EFFECT_DAMPER 0x07
1132 #define HIDPP_FF_EFFECT_FRICTION 0x08
1133 #define HIDPP_FF_EFFECT_INERTIA 0x09
1134 #define HIDPP_FF_EFFECT_RAMP 0x0A
1136 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1138 #define HIDPP_FF_EFFECTID_NONE -1
1139 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1141 #define HIDPP_FF_MAX_PARAMS 20
1142 #define HIDPP_FF_RESERVED_SLOTS 1
1144 struct hidpp_ff_private_data {
1145 struct hidpp_device *hidpp;
1153 struct workqueue_struct *wq;
1154 atomic_t workqueue_size;
1157 struct hidpp_ff_work_data {
1158 struct work_struct work;
1159 struct hidpp_ff_private_data *data;
1162 u8 params[HIDPP_FF_MAX_PARAMS];
1166 static const signed short hiddpp_ff_effects[] = {
1181 static const signed short hiddpp_ff_effects_v2[] = {
1188 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1189 HIDPP_FF_EFFECT_SPRING,
1190 HIDPP_FF_EFFECT_FRICTION,
1191 HIDPP_FF_EFFECT_DAMPER,
1192 HIDPP_FF_EFFECT_INERTIA
1195 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1203 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1207 for (i = 0; i < data->num_effects; i++)
1208 if (data->effect_ids[i] == effect_id)
1214 static void hidpp_ff_work_handler(struct work_struct *w)
1216 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1217 struct hidpp_ff_private_data *data = wd->data;
1218 struct hidpp_report response;
1222 /* add slot number if needed */
1223 switch (wd->effect_id) {
1224 case HIDPP_FF_EFFECTID_AUTOCENTER:
1225 wd->params[0] = data->slot_autocenter;
1227 case HIDPP_FF_EFFECTID_NONE:
1228 /* leave slot as zero */
1231 /* find current slot for effect */
1232 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1236 /* send command and wait for reply */
1237 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1238 wd->command, wd->params, wd->size, &response);
1241 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1245 /* parse return data */
1246 switch (wd->command) {
1247 case HIDPP_FF_DOWNLOAD_EFFECT:
1248 slot = response.fap.params[0];
1249 if (slot > 0 && slot <= data->num_effects) {
1250 if (wd->effect_id >= 0)
1251 /* regular effect uploaded */
1252 data->effect_ids[slot-1] = wd->effect_id;
1253 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1254 /* autocenter spring uploaded */
1255 data->slot_autocenter = slot;
1258 case HIDPP_FF_DESTROY_EFFECT:
1259 if (wd->effect_id >= 0)
1260 /* regular effect destroyed */
1261 data->effect_ids[wd->params[0]-1] = -1;
1262 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1263 /* autocenter spring destoyed */
1264 data->slot_autocenter = 0;
1266 case HIDPP_FF_SET_GLOBAL_GAINS:
1267 data->gain = (wd->params[0] << 8) + wd->params[1];
1269 case HIDPP_FF_SET_APERTURE:
1270 data->range = (wd->params[0] << 8) + wd->params[1];
1273 /* no action needed */
1278 atomic_dec(&data->workqueue_size);
1282 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1284 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1290 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1293 wd->effect_id = effect_id;
1294 wd->command = command;
1296 memcpy(wd->params, params, size);
1298 atomic_inc(&data->workqueue_size);
1299 queue_work(data->wq, &wd->work);
1301 /* warn about excessive queue size */
1302 s = atomic_read(&data->workqueue_size);
1303 if (s >= 20 && s % 20 == 0)
1304 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1309 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1311 struct hidpp_ff_private_data *data = dev->ff->private;
1316 /* set common parameters */
1317 params[2] = effect->replay.length >> 8;
1318 params[3] = effect->replay.length & 255;
1319 params[4] = effect->replay.delay >> 8;
1320 params[5] = effect->replay.delay & 255;
1322 switch (effect->type) {
1324 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1325 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1326 params[6] = force >> 8;
1327 params[7] = force & 255;
1328 params[8] = effect->u.constant.envelope.attack_level >> 7;
1329 params[9] = effect->u.constant.envelope.attack_length >> 8;
1330 params[10] = effect->u.constant.envelope.attack_length & 255;
1331 params[11] = effect->u.constant.envelope.fade_level >> 7;
1332 params[12] = effect->u.constant.envelope.fade_length >> 8;
1333 params[13] = effect->u.constant.envelope.fade_length & 255;
1335 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1336 effect->u.constant.level,
1337 effect->direction, force);
1338 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1339 effect->u.constant.envelope.attack_level,
1340 effect->u.constant.envelope.attack_length,
1341 effect->u.constant.envelope.fade_level,
1342 effect->u.constant.envelope.fade_length);
1346 switch (effect->u.periodic.waveform) {
1348 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1351 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1354 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1357 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1360 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1363 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1366 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1367 params[6] = effect->u.periodic.magnitude >> 8;
1368 params[7] = effect->u.periodic.magnitude & 255;
1369 params[8] = effect->u.periodic.offset >> 8;
1370 params[9] = effect->u.periodic.offset & 255;
1371 params[10] = effect->u.periodic.period >> 8;
1372 params[11] = effect->u.periodic.period & 255;
1373 params[12] = effect->u.periodic.phase >> 8;
1374 params[13] = effect->u.periodic.phase & 255;
1375 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1376 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1377 params[16] = effect->u.periodic.envelope.attack_length & 255;
1378 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1379 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1380 params[19] = effect->u.periodic.envelope.fade_length & 255;
1382 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1383 effect->u.periodic.magnitude, effect->direction,
1384 effect->u.periodic.offset,
1385 effect->u.periodic.period,
1386 effect->u.periodic.phase);
1387 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1388 effect->u.periodic.envelope.attack_level,
1389 effect->u.periodic.envelope.attack_length,
1390 effect->u.periodic.envelope.fade_level,
1391 effect->u.periodic.envelope.fade_length);
1395 params[1] = HIDPP_FF_EFFECT_RAMP;
1396 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1397 params[6] = force >> 8;
1398 params[7] = force & 255;
1399 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1400 params[8] = force >> 8;
1401 params[9] = force & 255;
1402 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1403 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1404 params[12] = effect->u.ramp.envelope.attack_length & 255;
1405 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1406 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1407 params[15] = effect->u.ramp.envelope.fade_length & 255;
1409 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1410 effect->u.ramp.start_level,
1411 effect->u.ramp.end_level,
1412 effect->direction, force);
1413 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1414 effect->u.ramp.envelope.attack_level,
1415 effect->u.ramp.envelope.attack_length,
1416 effect->u.ramp.envelope.fade_level,
1417 effect->u.ramp.envelope.fade_length);
1423 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1424 params[6] = effect->u.condition[0].left_saturation >> 9;
1425 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1426 params[8] = effect->u.condition[0].left_coeff >> 8;
1427 params[9] = effect->u.condition[0].left_coeff & 255;
1428 params[10] = effect->u.condition[0].deadband >> 9;
1429 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1430 params[12] = effect->u.condition[0].center >> 8;
1431 params[13] = effect->u.condition[0].center & 255;
1432 params[14] = effect->u.condition[0].right_coeff >> 8;
1433 params[15] = effect->u.condition[0].right_coeff & 255;
1434 params[16] = effect->u.condition[0].right_saturation >> 9;
1435 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1437 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1438 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1439 effect->u.condition[0].left_coeff,
1440 effect->u.condition[0].left_saturation,
1441 effect->u.condition[0].right_coeff,
1442 effect->u.condition[0].right_saturation);
1443 dbg_hid(" deadband=%d, center=%d\n",
1444 effect->u.condition[0].deadband,
1445 effect->u.condition[0].center);
1448 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1452 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1455 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1457 struct hidpp_ff_private_data *data = dev->ff->private;
1460 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1462 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1464 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1467 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1469 struct hidpp_ff_private_data *data = dev->ff->private;
1472 dbg_hid("Erasing effect %d.\n", effect_id);
1474 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1477 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1479 struct hidpp_ff_private_data *data = dev->ff->private;
1482 dbg_hid("Setting autocenter to %d.\n", magnitude);
1484 /* start a standard spring effect */
1485 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1486 /* zero delay and duration */
1487 params[2] = params[3] = params[4] = params[5] = 0;
1488 /* set coeff to 25% of saturation */
1489 params[8] = params[14] = magnitude >> 11;
1490 params[9] = params[15] = (magnitude >> 3) & 255;
1491 params[6] = params[16] = magnitude >> 9;
1492 params[7] = params[17] = (magnitude >> 1) & 255;
1493 /* zero deadband and center */
1494 params[10] = params[11] = params[12] = params[13] = 0;
1496 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1499 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1501 struct hidpp_ff_private_data *data = dev->ff->private;
1504 dbg_hid("Setting gain to %d.\n", gain);
1506 params[0] = gain >> 8;
1507 params[1] = gain & 255;
1508 params[2] = 0; /* no boost */
1511 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1514 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1516 struct hid_device *hid = to_hid_device(dev);
1517 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1518 struct input_dev *idev = hidinput->input;
1519 struct hidpp_ff_private_data *data = idev->ff->private;
1521 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1524 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1526 struct hid_device *hid = to_hid_device(dev);
1527 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1528 struct input_dev *idev = hidinput->input;
1529 struct hidpp_ff_private_data *data = idev->ff->private;
1531 int range = simple_strtoul(buf, NULL, 10);
1533 range = clamp(range, 180, 900);
1535 params[0] = range >> 8;
1536 params[1] = range & 0x00FF;
1538 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1543 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1545 static void hidpp_ff_destroy(struct ff_device *ff)
1547 struct hidpp_ff_private_data *data = ff->private;
1549 kfree(data->effect_ids);
1552 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
1554 struct hid_device *hid = hidpp->hid_dev;
1555 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1556 struct input_dev *dev = hidinput->input;
1557 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1558 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1559 struct ff_device *ff;
1560 struct hidpp_report response;
1561 struct hidpp_ff_private_data *data;
1562 int error, j, num_slots;
1566 hid_err(hid, "Struct input_dev not set!\n");
1570 /* Get firmware release */
1571 version = bcdDevice & 255;
1573 /* Set supported force feedback capabilities */
1574 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1575 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1577 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1578 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1580 /* Read number of slots available in device */
1581 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1582 HIDPP_FF_GET_INFO, NULL, 0, &response);
1586 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1591 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1593 error = input_ff_create(dev, num_slots);
1596 hid_err(dev, "Failed to create FF device!\n");
1600 data = kzalloc(sizeof(*data), GFP_KERNEL);
1603 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1604 if (!data->effect_ids) {
1608 data->hidpp = hidpp;
1609 data->feature_index = feature_index;
1610 data->version = version;
1611 data->slot_autocenter = 0;
1612 data->num_effects = num_slots;
1613 for (j = 0; j < num_slots; j++)
1614 data->effect_ids[j] = -1;
1619 ff->upload = hidpp_ff_upload_effect;
1620 ff->erase = hidpp_ff_erase_effect;
1621 ff->playback = hidpp_ff_playback;
1622 ff->set_gain = hidpp_ff_set_gain;
1623 ff->set_autocenter = hidpp_ff_set_autocenter;
1624 ff->destroy = hidpp_ff_destroy;
1627 /* reset all forces */
1628 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1629 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1631 /* Read current Range */
1632 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1633 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1635 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1636 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1638 /* Create sysfs interface */
1639 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1641 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1643 /* Read the current gain values */
1644 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1645 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1647 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1648 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1649 /* ignore boost value at response.fap.params[2] */
1651 /* init the hardware command queue */
1652 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1653 atomic_set(&data->workqueue_size, 0);
1655 /* initialize with zero autocenter to get wheel in usable state */
1656 hidpp_ff_set_autocenter(dev, 0);
1658 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1663 static int hidpp_ff_deinit(struct hid_device *hid)
1665 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1666 struct input_dev *dev = hidinput->input;
1667 struct hidpp_ff_private_data *data;
1670 hid_err(hid, "Struct input_dev not found!\n");
1674 hid_info(hid, "Unloading HID++ force feedback.\n");
1675 data = dev->ff->private;
1677 hid_err(hid, "Private data not found!\n");
1681 destroy_workqueue(data->wq);
1682 device_remove_file(&hid->dev, &dev_attr_range);
1688 /* ************************************************************************** */
1690 /* Device Support */
1692 /* ************************************************************************** */
1694 /* -------------------------------------------------------------------------- */
1695 /* Touchpad HID++ devices */
1696 /* -------------------------------------------------------------------------- */
1698 #define WTP_MANUAL_RESOLUTION 39
1701 struct input_dev *input;
1704 u8 mt_feature_index;
1705 u8 button_feature_index;
1708 unsigned int resolution;
1711 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1712 struct hid_field *field, struct hid_usage *usage,
1713 unsigned long **bit, int *max)
1718 static void wtp_populate_input(struct hidpp_device *hidpp,
1719 struct input_dev *input_dev, bool origin_is_hid_core)
1721 struct wtp_data *wd = hidpp->private_data;
1723 __set_bit(EV_ABS, input_dev->evbit);
1724 __set_bit(EV_KEY, input_dev->evbit);
1725 __clear_bit(EV_REL, input_dev->evbit);
1726 __clear_bit(EV_LED, input_dev->evbit);
1728 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
1729 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
1730 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
1731 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
1733 /* Max pressure is not given by the devices, pick one */
1734 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
1736 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
1738 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
1739 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
1741 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1743 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
1744 INPUT_MT_DROP_UNUSED);
1746 wd->input = input_dev;
1749 static void wtp_touch_event(struct wtp_data *wd,
1750 struct hidpp_touchpad_raw_xy_finger *touch_report)
1754 if (!touch_report->finger_id || touch_report->contact_type)
1755 /* no actual data */
1758 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
1760 input_mt_slot(wd->input, slot);
1761 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
1762 touch_report->contact_status);
1763 if (touch_report->contact_status) {
1764 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
1766 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
1767 wd->flip_y ? wd->y_size - touch_report->y :
1769 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
1770 touch_report->area);
1774 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
1775 struct hidpp_touchpad_raw_xy *raw)
1777 struct wtp_data *wd = hidpp->private_data;
1780 for (i = 0; i < 2; i++)
1781 wtp_touch_event(wd, &(raw->fingers[i]));
1783 if (raw->end_of_frame &&
1784 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
1785 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
1787 if (raw->end_of_frame || raw->finger_count <= 2) {
1788 input_mt_sync_frame(wd->input);
1789 input_sync(wd->input);
1793 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
1795 struct wtp_data *wd = hidpp->private_data;
1796 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
1797 (data[7] >> 4) * (data[7] >> 4)) / 2;
1798 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
1799 (data[13] >> 4) * (data[13] >> 4)) / 2;
1800 struct hidpp_touchpad_raw_xy raw = {
1801 .timestamp = data[1],
1805 .contact_status = !!data[7],
1806 .x = get_unaligned_le16(&data[3]),
1807 .y = get_unaligned_le16(&data[5]),
1810 .finger_id = data[2],
1813 .contact_status = !!data[13],
1814 .x = get_unaligned_le16(&data[9]),
1815 .y = get_unaligned_le16(&data[11]),
1818 .finger_id = data[8],
1821 .finger_count = wd->maxcontacts,
1823 .end_of_frame = (data[0] >> 7) == 0,
1824 .button = data[0] & 0x01,
1827 wtp_send_raw_xy_event(hidpp, &raw);
1832 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
1834 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1835 struct wtp_data *wd = hidpp->private_data;
1836 struct hidpp_report *report = (struct hidpp_report *)data;
1837 struct hidpp_touchpad_raw_xy raw;
1839 if (!wd || !wd->input)
1845 hid_err(hdev, "Received HID report of bad size (%d)",
1849 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
1850 input_event(wd->input, EV_KEY, BTN_LEFT,
1851 !!(data[1] & 0x01));
1852 input_event(wd->input, EV_KEY, BTN_RIGHT,
1853 !!(data[1] & 0x02));
1854 input_sync(wd->input);
1859 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
1861 case REPORT_ID_HIDPP_LONG:
1862 /* size is already checked in hidpp_raw_event. */
1863 if ((report->fap.feature_index != wd->mt_feature_index) ||
1864 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
1866 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
1868 wtp_send_raw_xy_event(hidpp, &raw);
1875 static int wtp_get_config(struct hidpp_device *hidpp)
1877 struct wtp_data *wd = hidpp->private_data;
1878 struct hidpp_touchpad_raw_info raw_info = {0};
1882 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
1883 &wd->mt_feature_index, &feature_type);
1885 /* means that the device is not powered up */
1888 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
1893 wd->x_size = raw_info.x_size;
1894 wd->y_size = raw_info.y_size;
1895 wd->maxcontacts = raw_info.maxcontacts;
1896 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
1897 wd->resolution = raw_info.res;
1898 if (!wd->resolution)
1899 wd->resolution = WTP_MANUAL_RESOLUTION;
1904 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
1906 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1907 struct wtp_data *wd;
1909 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
1914 hidpp->private_data = wd;
1919 static int wtp_connect(struct hid_device *hdev, bool connected)
1921 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1922 struct wtp_data *wd = hidpp->private_data;
1926 ret = wtp_get_config(hidpp);
1928 hid_err(hdev, "Can not get wtp config: %d\n", ret);
1933 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
1937 /* ------------------------------------------------------------------------- */
1938 /* Logitech M560 devices */
1939 /* ------------------------------------------------------------------------- */
1942 * Logitech M560 protocol overview
1944 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1945 * the sides buttons are pressed, it sends some keyboard keys events
1946 * instead of buttons ones.
1947 * To complicate things further, the middle button keys sequence
1948 * is different from the odd press and the even press.
1950 * forward button -> Super_R
1951 * backward button -> Super_L+'d' (press only)
1952 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1953 * 2nd time: left-click (press only)
1954 * NB: press-only means that when the button is pressed, the
1955 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1956 * together sequentially; instead when the button is released, no event is
1960 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1961 * the mouse reacts differently:
1962 * - it never sends a keyboard key event
1963 * - for the three mouse button it sends:
1964 * middle button press 11<xx>0a 3500af00...
1965 * side 1 button (forward) press 11<xx>0a 3500b000...
1966 * side 2 button (backward) press 11<xx>0a 3500ae00...
1967 * middle/side1/side2 button release 11<xx>0a 35000000...
1970 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1972 struct m560_private_data {
1973 struct input_dev *input;
1976 /* how buttons are mapped in the report */
1977 #define M560_MOUSE_BTN_LEFT 0x01
1978 #define M560_MOUSE_BTN_RIGHT 0x02
1979 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1980 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1982 #define M560_SUB_ID 0x0a
1983 #define M560_BUTTON_MODE_REGISTER 0x35
1985 static int m560_send_config_command(struct hid_device *hdev, bool connected)
1987 struct hidpp_report response;
1988 struct hidpp_device *hidpp_dev;
1990 hidpp_dev = hid_get_drvdata(hdev);
1992 return hidpp_send_rap_command_sync(
1994 REPORT_ID_HIDPP_SHORT,
1996 M560_BUTTON_MODE_REGISTER,
1997 (u8 *)m560_config_parameter,
1998 sizeof(m560_config_parameter),
2003 static int m560_allocate(struct hid_device *hdev)
2005 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2006 struct m560_private_data *d;
2008 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2013 hidpp->private_data = d;
2018 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2020 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2021 struct m560_private_data *mydata = hidpp->private_data;
2024 if (!mydata || !mydata->input) {
2025 hid_err(hdev, "error in parameter\n");
2030 hid_err(hdev, "error in report\n");
2034 if (data[0] == REPORT_ID_HIDPP_LONG &&
2035 data[2] == M560_SUB_ID && data[6] == 0x00) {
2037 * m560 mouse report for middle, forward and backward button
2040 * data[1] = device-id
2042 * data[5] = 0xaf -> middle
2045 * 0x00 -> release all
2051 input_report_key(mydata->input, BTN_MIDDLE, 1);
2054 input_report_key(mydata->input, BTN_FORWARD, 1);
2057 input_report_key(mydata->input, BTN_BACK, 1);
2060 input_report_key(mydata->input, BTN_BACK, 0);
2061 input_report_key(mydata->input, BTN_FORWARD, 0);
2062 input_report_key(mydata->input, BTN_MIDDLE, 0);
2065 hid_err(hdev, "error in report\n");
2068 input_sync(mydata->input);
2070 } else if (data[0] == 0x02) {
2072 * Logitech M560 mouse report
2074 * data[0] = type (0x02)
2075 * data[1..2] = buttons
2082 input_report_key(mydata->input, BTN_LEFT,
2083 !!(data[1] & M560_MOUSE_BTN_LEFT));
2084 input_report_key(mydata->input, BTN_RIGHT,
2085 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2087 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2088 input_report_rel(mydata->input, REL_HWHEEL, -1);
2089 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2090 input_report_rel(mydata->input, REL_HWHEEL, 1);
2092 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2093 input_report_rel(mydata->input, REL_X, v);
2095 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2096 input_report_rel(mydata->input, REL_Y, v);
2098 v = hid_snto32(data[6], 8);
2099 input_report_rel(mydata->input, REL_WHEEL, v);
2101 input_sync(mydata->input);
2107 static void m560_populate_input(struct hidpp_device *hidpp,
2108 struct input_dev *input_dev, bool origin_is_hid_core)
2110 struct m560_private_data *mydata = hidpp->private_data;
2112 mydata->input = input_dev;
2114 __set_bit(EV_KEY, mydata->input->evbit);
2115 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2116 __set_bit(BTN_RIGHT, mydata->input->keybit);
2117 __set_bit(BTN_LEFT, mydata->input->keybit);
2118 __set_bit(BTN_BACK, mydata->input->keybit);
2119 __set_bit(BTN_FORWARD, mydata->input->keybit);
2121 __set_bit(EV_REL, mydata->input->evbit);
2122 __set_bit(REL_X, mydata->input->relbit);
2123 __set_bit(REL_Y, mydata->input->relbit);
2124 __set_bit(REL_WHEEL, mydata->input->relbit);
2125 __set_bit(REL_HWHEEL, mydata->input->relbit);
2128 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2129 struct hid_field *field, struct hid_usage *usage,
2130 unsigned long **bit, int *max)
2135 /* ------------------------------------------------------------------------- */
2136 /* Logitech K400 devices */
2137 /* ------------------------------------------------------------------------- */
2140 * The Logitech K400 keyboard has an embedded touchpad which is seen
2141 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2142 * tap-to-click but the setting is not remembered accross reset, annoying some
2145 * We can toggle this feature from the host by using the feature 0x6010:
2149 struct k400_private_data {
2153 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2155 struct k400_private_data *k400 = hidpp->private_data;
2156 struct hidpp_touchpad_fw_items items = {};
2160 if (!k400->feature_index) {
2161 ret = hidpp_root_get_feature(hidpp,
2162 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2163 &k400->feature_index, &feature_type);
2165 /* means that the device is not powered up */
2169 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2176 static int k400_allocate(struct hid_device *hdev)
2178 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2179 struct k400_private_data *k400;
2181 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2186 hidpp->private_data = k400;
2191 static int k400_connect(struct hid_device *hdev, bool connected)
2193 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2195 if (!disable_tap_to_click)
2198 return k400_disable_tap_to_click(hidpp);
2201 /* ------------------------------------------------------------------------- */
2202 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2203 /* ------------------------------------------------------------------------- */
2205 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2207 static int g920_get_config(struct hidpp_device *hidpp)
2213 /* Find feature and store for later use */
2214 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2215 &feature_index, &feature_type);
2219 ret = hidpp_ff_init(hidpp, feature_index);
2221 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2227 /* -------------------------------------------------------------------------- */
2228 /* Generic HID++ devices */
2229 /* -------------------------------------------------------------------------- */
2231 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2232 struct hid_field *field, struct hid_usage *usage,
2233 unsigned long **bit, int *max)
2235 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2237 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2238 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2239 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2240 field->application != HID_GD_MOUSE)
2241 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2246 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2247 struct hid_field *field, struct hid_usage *usage,
2248 unsigned long **bit, int *max)
2250 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2252 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2253 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2254 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2255 usage->code == ABS_Y || usage->code == ABS_Z ||
2256 usage->code == ABS_RZ)) {
2257 field->application = HID_GD_MULTIAXIS;
2265 static void hidpp_populate_input(struct hidpp_device *hidpp,
2266 struct input_dev *input, bool origin_is_hid_core)
2268 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2269 wtp_populate_input(hidpp, input, origin_is_hid_core);
2270 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2271 m560_populate_input(hidpp, input, origin_is_hid_core);
2274 static int hidpp_input_configured(struct hid_device *hdev,
2275 struct hid_input *hidinput)
2277 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2278 struct input_dev *input = hidinput->input;
2280 hidpp_populate_input(hidpp, input, true);
2285 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2288 struct hidpp_report *question = hidpp->send_receive_buf;
2289 struct hidpp_report *answer = hidpp->send_receive_buf;
2290 struct hidpp_report *report = (struct hidpp_report *)data;
2294 * If the mutex is locked then we have a pending answer from a
2295 * previously sent command.
2297 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2299 * Check for a correct hidpp20 answer or the corresponding
2302 if (hidpp_match_answer(question, report) ||
2303 hidpp_match_error(question, report)) {
2305 hidpp->answer_available = true;
2306 wake_up(&hidpp->wait);
2308 * This was an answer to a command that this driver sent
2309 * We return 1 to hid-core to avoid forwarding the
2310 * command upstream as it has been treated by the driver
2317 if (unlikely(hidpp_report_is_connect_event(report))) {
2318 atomic_set(&hidpp->connected,
2319 !(report->rap.params[0] & (1 << 6)));
2320 if (schedule_work(&hidpp->work) == 0)
2321 dbg_hid("%s: connect event already queued\n", __func__);
2325 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2326 ret = hidpp20_battery_event(hidpp, data, size);
2334 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2337 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2340 /* Generic HID++ processing. */
2342 case REPORT_ID_HIDPP_VERY_LONG:
2343 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2344 hid_err(hdev, "received hid++ report of bad size (%d)",
2348 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2350 case REPORT_ID_HIDPP_LONG:
2351 if (size != HIDPP_REPORT_LONG_LENGTH) {
2352 hid_err(hdev, "received hid++ report of bad size (%d)",
2356 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2358 case REPORT_ID_HIDPP_SHORT:
2359 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2360 hid_err(hdev, "received hid++ report of bad size (%d)",
2364 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2368 /* If no report is available for further processing, skip calling
2369 * raw_event of subclasses. */
2373 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2374 return wtp_raw_event(hdev, data, size);
2375 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2376 return m560_raw_event(hdev, data, size);
2381 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2383 static atomic_t battery_no = ATOMIC_INIT(0);
2384 struct power_supply_config cfg = { .drv_data = hidpp };
2385 struct power_supply_desc *desc = &hidpp->battery.desc;
2386 enum power_supply_property *battery_props;
2387 struct hidpp_battery *battery;
2388 unsigned int num_battery_props;
2392 if (hidpp->battery.ps)
2395 if (hidpp->protocol_major >= 2) {
2396 ret = hidpp20_query_battery_info(hidpp);
2399 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2404 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
2405 hidpp_battery_props,
2406 sizeof(hidpp_battery_props),
2408 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
2410 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2411 battery_props[num_battery_props++] =
2412 POWER_SUPPLY_PROP_CAPACITY;
2414 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
2415 battery_props[num_battery_props++] =
2416 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
2418 battery = &hidpp->battery;
2420 n = atomic_inc_return(&battery_no) - 1;
2421 desc->properties = battery_props;
2422 desc->num_properties = num_battery_props;
2423 desc->get_property = hidpp_battery_get_property;
2424 sprintf(battery->name, "hidpp_battery_%ld", n);
2425 desc->name = battery->name;
2426 desc->type = POWER_SUPPLY_TYPE_BATTERY;
2427 desc->use_for_apm = 0;
2429 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2432 if (IS_ERR(battery->ps))
2433 return PTR_ERR(battery->ps);
2435 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2440 static void hidpp_overwrite_name(struct hid_device *hdev)
2442 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2445 if (hidpp->protocol_major < 2)
2448 name = hidpp_get_device_name(hidpp);
2451 hid_err(hdev, "unable to retrieve the name of the device");
2453 dbg_hid("HID++: Got name: %s\n", name);
2454 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
2460 static int hidpp_input_open(struct input_dev *dev)
2462 struct hid_device *hid = input_get_drvdata(dev);
2464 return hid_hw_open(hid);
2467 static void hidpp_input_close(struct input_dev *dev)
2469 struct hid_device *hid = input_get_drvdata(dev);
2474 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2476 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
2477 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2482 input_set_drvdata(input_dev, hdev);
2483 input_dev->open = hidpp_input_open;
2484 input_dev->close = hidpp_input_close;
2486 input_dev->name = hidpp->name;
2487 input_dev->phys = hdev->phys;
2488 input_dev->uniq = hdev->uniq;
2489 input_dev->id.bustype = hdev->bus;
2490 input_dev->id.vendor = hdev->vendor;
2491 input_dev->id.product = hdev->product;
2492 input_dev->id.version = hdev->version;
2493 input_dev->dev.parent = &hdev->dev;
2498 static void hidpp_connect_event(struct hidpp_device *hidpp)
2500 struct hid_device *hdev = hidpp->hid_dev;
2502 bool connected = atomic_read(&hidpp->connected);
2503 struct input_dev *input;
2504 char *name, *devm_name;
2507 if (hidpp->battery.ps) {
2508 hidpp->battery.online = false;
2509 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
2510 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2511 power_supply_changed(hidpp->battery.ps);
2516 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2517 ret = wtp_connect(hdev, connected);
2520 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2521 ret = m560_send_config_command(hdev, connected);
2524 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2525 ret = k400_connect(hdev, connected);
2530 /* the device is already connected, we can ask for its name and
2532 if (!hidpp->protocol_major) {
2533 ret = !hidpp_is_connected(hidpp);
2535 hid_err(hdev, "Can not get the protocol version.\n");
2538 hid_info(hdev, "HID++ %u.%u device connected.\n",
2539 hidpp->protocol_major, hidpp->protocol_minor);
2542 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
2543 name = hidpp_get_device_name(hidpp);
2546 "unable to retrieve the name of the device");
2550 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2555 hidpp->name = devm_name;
2558 hidpp_initialize_battery(hidpp);
2560 /* forward current battery state */
2561 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2562 hidpp20_query_battery_info(hidpp);
2563 if (hidpp->battery.ps)
2564 power_supply_changed(hidpp->battery.ps);
2567 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
2568 /* if the input nodes are already created, we can stop now */
2571 input = hidpp_allocate_input(hdev);
2573 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2577 hidpp_populate_input(hidpp, input, false);
2579 ret = input_register_device(input);
2581 input_free_device(input);
2583 hidpp->delayed_input = input;
2586 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2588 struct hidpp_device *hidpp;
2591 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2593 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2598 hidpp->hid_dev = hdev;
2599 hidpp->name = hdev->name;
2600 hid_set_drvdata(hdev, hidpp);
2602 hidpp->quirks = id->driver_data;
2604 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
2605 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
2607 if (disable_raw_mode) {
2608 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
2609 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
2612 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2613 ret = wtp_allocate(hdev, id);
2616 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2617 ret = m560_allocate(hdev);
2620 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2621 ret = k400_allocate(hdev);
2626 INIT_WORK(&hidpp->work, delayed_work_cb);
2627 mutex_init(&hidpp->send_mutex);
2628 init_waitqueue_head(&hidpp->wait);
2630 ret = hid_parse(hdev);
2632 hid_err(hdev, "%s:parse failed\n", __func__);
2633 goto hid_parse_fail;
2636 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2637 connect_mask &= ~HID_CONNECT_HIDINPUT;
2639 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2640 ret = hid_hw_start(hdev, connect_mask);
2642 hid_err(hdev, "hw start failed\n");
2643 goto hid_hw_start_fail;
2645 ret = hid_hw_open(hdev);
2647 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
2650 goto hid_hw_start_fail;
2655 /* Allow incoming packets */
2656 hid_device_io_start(hdev);
2658 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
2659 hidpp_unifying_init(hidpp);
2661 connected = hidpp_is_connected(hidpp);
2662 atomic_set(&hidpp->connected, connected);
2663 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
2666 hid_err(hdev, "Device not connected");
2667 goto hid_hw_open_failed;
2670 hid_info(hdev, "HID++ %u.%u device connected.\n",
2671 hidpp->protocol_major, hidpp->protocol_minor);
2673 hidpp_overwrite_name(hdev);
2676 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
2677 ret = wtp_get_config(hidpp);
2679 goto hid_hw_open_failed;
2680 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2681 ret = g920_get_config(hidpp);
2683 goto hid_hw_open_failed;
2686 /* Block incoming packets */
2687 hid_device_io_stop(hdev);
2689 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2690 ret = hid_hw_start(hdev, connect_mask);
2692 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
2693 goto hid_hw_start_fail;
2697 /* Allow incoming packets */
2698 hid_device_io_start(hdev);
2700 hidpp_connect_event(hidpp);
2705 hid_device_io_stop(hdev);
2706 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2712 cancel_work_sync(&hidpp->work);
2713 mutex_destroy(&hidpp->send_mutex);
2715 hid_set_drvdata(hdev, NULL);
2719 static void hidpp_remove(struct hid_device *hdev)
2721 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2723 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2724 hidpp_ff_deinit(hdev);
2728 cancel_work_sync(&hidpp->work);
2729 mutex_destroy(&hidpp->send_mutex);
2732 static const struct hid_device_id hidpp_devices[] = {
2733 { /* wireless touchpad */
2734 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2735 USB_VENDOR_ID_LOGITECH, 0x4011),
2736 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
2737 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
2738 { /* wireless touchpad T650 */
2739 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2740 USB_VENDOR_ID_LOGITECH, 0x4101),
2741 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
2742 { /* wireless touchpad T651 */
2743 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
2744 USB_DEVICE_ID_LOGITECH_T651),
2745 .driver_data = HIDPP_QUIRK_CLASS_WTP },
2746 { /* Mouse logitech M560 */
2747 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2748 USB_VENDOR_ID_LOGITECH, 0x402d),
2749 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
2750 { /* Keyboard logitech K400 */
2751 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2752 USB_VENDOR_ID_LOGITECH, 0x4024),
2753 .driver_data = HIDPP_QUIRK_CLASS_K400 },
2755 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2756 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
2758 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
2759 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
2763 MODULE_DEVICE_TABLE(hid, hidpp_devices);
2765 static struct hid_driver hidpp_driver = {
2766 .name = "logitech-hidpp-device",
2767 .id_table = hidpp_devices,
2768 .probe = hidpp_probe,
2769 .remove = hidpp_remove,
2770 .raw_event = hidpp_raw_event,
2771 .input_configured = hidpp_input_configured,
2772 .input_mapping = hidpp_input_mapping,
2773 .input_mapped = hidpp_input_mapped,
2776 module_hid_driver(hidpp_driver);