blob: 1ac1ecc1e67cd9f1c8dfc76fc5246d1f7d63ecdd [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002/*
3 * HIDPP protocol for Logitech Unifying receivers
4 *
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
8 */
9
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/device.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010014#include <linux/input.h>
15#include <linux/usb.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040016#include <linux/hid.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
Harry Cutts4435ff22018-12-05 10:42:27 +100020#include <linux/sched/clock.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040021#include <linux/kfifo.h>
22#include <linux/input/mt.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010023#include <linux/workqueue.h>
24#include <linux/atomic.h>
25#include <linux/fixp-arith.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040026#include <asm/unaligned.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010027#include "usbhid/usbhid.h"
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040028#include "hid-ids.h"
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
33
Benjamin Tissoires9188dba2015-03-26 12:41:57 -040034static bool disable_raw_mode;
35module_param(disable_raw_mode, bool, 0644);
36MODULE_PARM_DESC(disable_raw_mode,
37 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
38
Benjamin Tissoires90cdd982015-09-03 09:08:30 -040039static bool disable_tap_to_click;
40module_param(disable_tap_to_click, bool, 0644);
41MODULE_PARM_DESC(disable_tap_to_click,
42 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
43
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040044#define REPORT_ID_HIDPP_SHORT 0x10
45#define REPORT_ID_HIDPP_LONG 0x11
Simon Wooda5ce8f52015-11-19 16:42:11 -070046#define REPORT_ID_HIDPP_VERY_LONG 0x12
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040047
48#define HIDPP_REPORT_SHORT_LENGTH 7
49#define HIDPP_REPORT_LONG_LENGTH 20
Hans de Goeded71b18f2019-04-20 13:22:12 +020050#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040051
Hans de Goede42bc4f32019-04-20 13:22:17 +020052#define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
Hans de Goede4a79bcc2019-04-20 13:22:15 +020053#define HIDPP_SUB_ID_ROLLER 0x05
54#define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
55
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040056#define HIDPP_QUIRK_CLASS_WTP BIT(0)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +020057#define HIDPP_QUIRK_CLASS_M560 BIT(1)
Benjamin Tissoires90cdd982015-09-03 09:08:30 -040058#define HIDPP_QUIRK_CLASS_K400 BIT(2)
Simon Wood7bfd2922015-11-19 16:42:12 -070059#define HIDPP_QUIRK_CLASS_G920 BIT(3)
Benjamin Tissoires696ecef2017-03-27 16:59:37 +020060#define HIDPP_QUIRK_CLASS_K750 BIT(4)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040061
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +020062/* bits 2..20 are reserved for classes */
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100063/* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -040064#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040065#define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
Simon Wood7bfd2922015-11-19 16:42:12 -070066#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
Benjamin Tissoires843c6242017-03-27 16:59:26 +020067#define HIDPP_QUIRK_UNIFYING BIT(25)
Harry Cutts4435ff22018-12-05 10:42:27 +100068#define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
69#define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
70#define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
Hans de Goede4a79bcc2019-04-20 13:22:15 +020071#define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
Hans de Goede7457bc12019-04-20 13:22:16 +020072#define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
Hans de Goede42bc4f32019-04-20 13:22:17 +020073#define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
Hans de Goede4a79bcc2019-04-20 13:22:15 +020074
75/* These are just aliases for now */
76#define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
77#define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
Harry Cutts4435ff22018-12-05 10:42:27 +100078
79/* Convenience constant to check for any high-res support. */
80#define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
81 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
82 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040083
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100084#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040085
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020086#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
87#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +020088#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
89#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020090
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040091/*
92 * There are two hidpp protocols in use, the first version hidpp10 is known
93 * as register access protocol or RAP, the second version hidpp20 is known as
94 * feature access protocol or FAP
95 *
96 * Most older devices (including the Unifying usb receiver) use the RAP protocol
97 * where as most newer devices use the FAP protocol. Both protocols are
98 * compatible with the underlying transport, which could be usb, Unifiying, or
99 * bluetooth. The message lengths are defined by the hid vendor specific report
100 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
101 * the HIDPP_LONG report type (total message length 20 bytes)
102 *
103 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
104 * messages. The Unifying receiver itself responds to RAP messages (device index
105 * is 0xFF for the receiver), and all messages (short or long) with a device
106 * index between 1 and 6 are passed untouched to the corresponding paired
107 * Unifying device.
108 *
109 * The paired device can be RAP or FAP, it will receive the message untouched
110 * from the Unifiying receiver.
111 */
112
113struct fap {
114 u8 feature_index;
115 u8 funcindex_clientid;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200116 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400117};
118
119struct rap {
120 u8 sub_id;
121 u8 reg_address;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200122 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400123};
124
125struct hidpp_report {
126 u8 report_id;
127 u8 device_index;
128 union {
129 struct fap fap;
130 struct rap rap;
131 u8 rawbytes[sizeof(struct fap)];
132 };
133} __packed;
134
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000135struct hidpp_battery {
136 u8 feature_index;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +0200137 u8 solar_feature_index;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000138 struct power_supply_desc desc;
139 struct power_supply *ps;
140 char name[64];
141 int status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200142 int capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200143 int level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200144 bool online;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000145};
146
Harry Cutts4435ff22018-12-05 10:42:27 +1000147/**
148 * struct hidpp_scroll_counter - Utility class for processing high-resolution
149 * scroll events.
150 * @dev: the input device for which events should be reported.
151 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
152 * @remainder: counts the number of high-resolution units moved since the last
153 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
154 * only be used by class methods.
155 * @direction: direction of last movement (1 or -1)
156 * @last_time: last event time, used to reset remainder after inactivity
157 */
158struct hidpp_scroll_counter {
Harry Cutts4435ff22018-12-05 10:42:27 +1000159 int wheel_multiplier;
160 int remainder;
161 int direction;
162 unsigned long long last_time;
163};
164
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400165struct hidpp_device {
166 struct hid_device *hid_dev;
Hans de Goede06104302019-04-20 13:22:13 +0200167 struct input_dev *input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400168 struct mutex send_mutex;
169 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -0500170 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400171 wait_queue_head_t wait;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200172 int very_long_report_length;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400173 bool answer_available;
174 u8 protocol_major;
175 u8 protocol_minor;
176
177 void *private_data;
178
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400179 struct work_struct work;
180 struct kfifo delayed_work_fifo;
181 atomic_t connected;
182 struct input_dev *delayed_input;
183
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400184 unsigned long quirks;
Benjamin Tissoires206d7c62017-03-27 16:59:25 +0200185 unsigned long capabilities;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400186
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000187 struct hidpp_battery battery;
Harry Cutts4435ff22018-12-05 10:42:27 +1000188 struct hidpp_scroll_counter vertical_wheel_counter;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000189};
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400190
Peter Wuf677bb12014-12-16 01:50:14 +0100191/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400192#define HIDPP_ERROR 0x8f
193#define HIDPP_ERROR_SUCCESS 0x00
194#define HIDPP_ERROR_INVALID_SUBID 0x01
195#define HIDPP_ERROR_INVALID_ADRESS 0x02
196#define HIDPP_ERROR_INVALID_VALUE 0x03
197#define HIDPP_ERROR_CONNECT_FAIL 0x04
198#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
199#define HIDPP_ERROR_ALREADY_EXISTS 0x06
200#define HIDPP_ERROR_BUSY 0x07
201#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
202#define HIDPP_ERROR_RESOURCE_ERROR 0x09
203#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
204#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
205#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100206/* HID++ 2.0 error codes */
207#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400208
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400209static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
210
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400211static int __hidpp_send_report(struct hid_device *hdev,
212 struct hidpp_report *hidpp_report)
213{
Simon Wood7bfd2922015-11-19 16:42:12 -0700214 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400215 int fields_count, ret;
216
217 switch (hidpp_report->report_id) {
218 case REPORT_ID_HIDPP_SHORT:
219 fields_count = HIDPP_REPORT_SHORT_LENGTH;
220 break;
221 case REPORT_ID_HIDPP_LONG:
222 fields_count = HIDPP_REPORT_LONG_LENGTH;
223 break;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700224 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200225 fields_count = hidpp->very_long_report_length;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700226 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400227 default:
228 return -ENODEV;
229 }
230
231 /*
232 * set the device_index as the receiver, it will be overwritten by
233 * hid_hw_request if needed
234 */
235 hidpp_report->device_index = 0xff;
236
Simon Wood7bfd2922015-11-19 16:42:12 -0700237 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
238 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
239 } else {
240 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
241 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
242 HID_REQ_SET_REPORT);
243 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400244
245 return ret == fields_count ? 0 : -1;
246}
247
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500248/**
249 * hidpp_send_message_sync() returns 0 in case of success, and something else
250 * in case of a failure.
251 * - If ' something else' is positive, that means that an error has been raised
252 * by the protocol itself.
253 * - If ' something else' is negative, that means that we had a classic error
254 * (-ENOMEM, -EPIPE, etc...)
255 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400256static int hidpp_send_message_sync(struct hidpp_device *hidpp,
257 struct hidpp_report *message,
258 struct hidpp_report *response)
259{
260 int ret;
261
262 mutex_lock(&hidpp->send_mutex);
263
264 hidpp->send_receive_buf = response;
265 hidpp->answer_available = false;
266
267 /*
268 * So that we can later validate the answer when it arrives
269 * in hidpp_raw_event
270 */
271 *response = *message;
272
273 ret = __hidpp_send_report(hidpp->hid_dev, message);
274
275 if (ret) {
276 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
277 memset(response, 0, sizeof(struct hidpp_report));
278 goto exit;
279 }
280
281 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
282 5*HZ)) {
283 dbg_hid("%s:timeout waiting for response\n", __func__);
284 memset(response, 0, sizeof(struct hidpp_report));
285 ret = -ETIMEDOUT;
286 }
287
288 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100289 response->rap.sub_id == HIDPP_ERROR) {
290 ret = response->rap.params[1];
291 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
292 goto exit;
293 }
294
Simon Wooda5ce8f52015-11-19 16:42:11 -0700295 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
296 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
297 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400298 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100299 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400300 goto exit;
301 }
302
303exit:
304 mutex_unlock(&hidpp->send_mutex);
305 return ret;
306
307}
308
309static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
310 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
311 struct hidpp_report *response)
312{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300313 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400314 int ret;
315
316 if (param_count > sizeof(message->fap.params))
317 return -EINVAL;
318
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300319 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
320 if (!message)
321 return -ENOMEM;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700322
323 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
324 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
325 else
326 message->report_id = REPORT_ID_HIDPP_LONG;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400327 message->fap.feature_index = feat_index;
328 message->fap.funcindex_clientid = funcindex_clientid;
329 memcpy(&message->fap.params, params, param_count);
330
331 ret = hidpp_send_message_sync(hidpp, message, response);
332 kfree(message);
333 return ret;
334}
335
Benjamin Tissoires33797822014-09-30 13:18:30 -0400336static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
337 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
338 struct hidpp_report *response)
339{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300340 struct hidpp_report *message;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700341 int ret, max_count;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400342
Simon Wooda5ce8f52015-11-19 16:42:11 -0700343 switch (report_id) {
344 case REPORT_ID_HIDPP_SHORT:
345 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
346 break;
347 case REPORT_ID_HIDPP_LONG:
348 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
349 break;
350 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200351 max_count = hidpp_dev->very_long_report_length - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700352 break;
353 default:
Benjamin Tissoires33797822014-09-30 13:18:30 -0400354 return -EINVAL;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700355 }
Benjamin Tissoires33797822014-09-30 13:18:30 -0400356
Simon Wooda5ce8f52015-11-19 16:42:11 -0700357 if (param_count > max_count)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400358 return -EINVAL;
359
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300360 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
361 if (!message)
362 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400363 message->report_id = report_id;
364 message->rap.sub_id = sub_id;
365 message->rap.reg_address = reg_address;
366 memcpy(&message->rap.params, params, param_count);
367
368 ret = hidpp_send_message_sync(hidpp_dev, message, response);
369 kfree(message);
370 return ret;
371}
372
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400373static void delayed_work_cb(struct work_struct *work)
374{
375 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
376 work);
377 hidpp_connect_event(hidpp);
378}
379
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400380static inline bool hidpp_match_answer(struct hidpp_report *question,
381 struct hidpp_report *answer)
382{
383 return (answer->fap.feature_index == question->fap.feature_index) &&
384 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
385}
386
387static inline bool hidpp_match_error(struct hidpp_report *question,
388 struct hidpp_report *answer)
389{
Peter Wuf677bb12014-12-16 01:50:14 +0100390 return ((answer->rap.sub_id == HIDPP_ERROR) ||
391 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400392 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
393 (answer->fap.params[0] == question->fap.funcindex_clientid);
394}
395
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400396static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
397{
398 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
399 (report->rap.sub_id == 0x41);
400}
401
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500402/**
403 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
404 */
405static void hidpp_prefix_name(char **name, int name_length)
406{
407#define PREFIX_LENGTH 9 /* "Logitech " */
408
409 int new_length;
410 char *new_name;
411
412 if (name_length > PREFIX_LENGTH &&
413 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
414 /* The prefix has is already in the name */
415 return;
416
417 new_length = PREFIX_LENGTH + name_length;
418 new_name = kzalloc(new_length, GFP_KERNEL);
419 if (!new_name)
420 return;
421
422 snprintf(new_name, new_length, "Logitech %s", *name);
423
424 kfree(*name);
425
426 *name = new_name;
427}
428
Harry Cutts4435ff22018-12-05 10:42:27 +1000429/**
430 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
431 * events given a high-resolution wheel
432 * movement.
433 * @counter: a hid_scroll_counter struct describing the wheel.
434 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
435 * units.
436 *
437 * Given a high-resolution movement, this function converts the movement into
438 * fractions of 120 and emits high-resolution scroll events for the input
439 * device. It also uses the multiplier from &struct hid_scroll_counter to
440 * emit low-resolution scroll events when appropriate for
441 * backwards-compatibility with userspace input libraries.
442 */
Hans de Goede06104302019-04-20 13:22:13 +0200443static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
444 struct hidpp_scroll_counter *counter,
Harry Cutts4435ff22018-12-05 10:42:27 +1000445 int hi_res_value)
446{
447 int low_res_value, remainder, direction;
448 unsigned long long now, previous;
449
450 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
Hans de Goede06104302019-04-20 13:22:13 +0200451 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
Harry Cutts4435ff22018-12-05 10:42:27 +1000452
453 remainder = counter->remainder;
454 direction = hi_res_value > 0 ? 1 : -1;
455
456 now = sched_clock();
457 previous = counter->last_time;
458 counter->last_time = now;
459 /*
460 * Reset the remainder after a period of inactivity or when the
461 * direction changes. This prevents the REL_WHEEL emulation point
462 * from sliding for devices that don't always provide the same
463 * number of movements per detent.
464 */
465 if (now - previous > 1000000000 || direction != counter->direction)
466 remainder = 0;
467
468 counter->direction = direction;
469 remainder += hi_res_value;
470
471 /* Some wheels will rest 7/8ths of a detent from the previous detent
472 * after slow movement, so we want the threshold for low-res events to
473 * be in the middle between two detents (e.g. after 4/8ths) as
474 * opposed to on the detents themselves (8/8ths).
475 */
476 if (abs(remainder) >= 60) {
477 /* Add (or subtract) 1 because we want to trigger when the wheel
478 * is half-way to the next detent (i.e. scroll 1 detent after a
479 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
480 * etc.).
481 */
482 low_res_value = remainder / 120;
483 if (low_res_value == 0)
484 low_res_value = (hi_res_value > 0 ? 1 : -1);
Hans de Goede06104302019-04-20 13:22:13 +0200485 input_report_rel(input_dev, REL_WHEEL, low_res_value);
Harry Cutts4435ff22018-12-05 10:42:27 +1000486 remainder -= low_res_value * 120;
487 }
488 counter->remainder = remainder;
489}
490
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400491/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400492/* HIDP++ 1.0 commands */
493/* -------------------------------------------------------------------------- */
494
495#define HIDPP_SET_REGISTER 0x80
496#define HIDPP_GET_REGISTER 0x81
497#define HIDPP_SET_LONG_REGISTER 0x82
498#define HIDPP_GET_LONG_REGISTER 0x83
499
Harry Cutts95c3d002018-12-05 10:42:26 +1000500/**
Hans de Goede35839f72019-04-20 13:22:14 +0200501 * hidpp10_set_register - Modify a HID++ 1.0 register.
Harry Cutts95c3d002018-12-05 10:42:26 +1000502 * @hidpp_dev: the device to set the register on.
503 * @register_address: the address of the register to modify.
504 * @byte: the byte of the register to modify. Should be less than 3.
Hans de Goede35839f72019-04-20 13:22:14 +0200505 * @mask: mask of the bits to modify
506 * @value: new values for the bits in mask
Harry Cutts95c3d002018-12-05 10:42:26 +1000507 * Return: 0 if successful, otherwise a negative error code.
508 */
Hans de Goede35839f72019-04-20 13:22:14 +0200509static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
510 u8 register_address, u8 byte, u8 mask, u8 value)
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200511{
512 struct hidpp_report response;
513 int ret;
514 u8 params[3] = { 0 };
515
516 ret = hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000517 REPORT_ID_HIDPP_SHORT,
518 HIDPP_GET_REGISTER,
519 register_address,
520 NULL, 0, &response);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200521 if (ret)
522 return ret;
523
524 memcpy(params, response.rap.params, 3);
525
Hans de Goede35839f72019-04-20 13:22:14 +0200526 params[byte] &= ~mask;
527 params[byte] |= value & mask;
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200528
529 return hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000530 REPORT_ID_HIDPP_SHORT,
531 HIDPP_SET_REGISTER,
532 register_address,
533 params, 3, &response);
534}
535
Hans de Goede35839f72019-04-20 13:22:14 +0200536#define HIDPP_REG_ENABLE_REPORTS 0x00
537#define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
538#define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
539#define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
540#define HIDPP_ENABLE_BAT_REPORT BIT(4)
541#define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
Harry Cutts95c3d002018-12-05 10:42:26 +1000542
543static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
544{
Hans de Goede35839f72019-04-20 13:22:14 +0200545 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
546 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
Harry Cutts95c3d002018-12-05 10:42:26 +1000547}
548
549#define HIDPP_REG_FEATURES 0x01
Hans de Goede35839f72019-04-20 13:22:14 +0200550#define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
551#define HIDPP_ENABLE_FAST_SCROLL BIT(6)
Harry Cutts95c3d002018-12-05 10:42:26 +1000552
553/* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
554static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
555{
Hans de Goede35839f72019-04-20 13:22:14 +0200556 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
557 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200558}
559
560#define HIDPP_REG_BATTERY_STATUS 0x07
561
562static int hidpp10_battery_status_map_level(u8 param)
563{
564 int level;
565
566 switch (param) {
567 case 1 ... 2:
568 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
569 break;
570 case 3 ... 4:
571 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
572 break;
573 case 5 ... 6:
574 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
575 break;
576 case 7:
577 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
578 break;
579 default:
580 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
581 }
582
583 return level;
584}
585
586static int hidpp10_battery_status_map_status(u8 param)
587{
588 int status;
589
590 switch (param) {
591 case 0x00:
592 /* discharging (in use) */
593 status = POWER_SUPPLY_STATUS_DISCHARGING;
594 break;
595 case 0x21: /* (standard) charging */
596 case 0x24: /* fast charging */
597 case 0x25: /* slow charging */
598 status = POWER_SUPPLY_STATUS_CHARGING;
599 break;
600 case 0x26: /* topping charge */
601 case 0x22: /* charge complete */
602 status = POWER_SUPPLY_STATUS_FULL;
603 break;
604 case 0x20: /* unknown */
605 status = POWER_SUPPLY_STATUS_UNKNOWN;
606 break;
607 /*
608 * 0x01...0x1F = reserved (not charging)
609 * 0x23 = charging error
610 * 0x27..0xff = reserved
611 */
612 default:
613 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
614 break;
615 }
616
617 return status;
618}
619
620static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
621{
622 struct hidpp_report response;
623 int ret, status;
624
625 ret = hidpp_send_rap_command_sync(hidpp,
626 REPORT_ID_HIDPP_SHORT,
627 HIDPP_GET_REGISTER,
628 HIDPP_REG_BATTERY_STATUS,
629 NULL, 0, &response);
630 if (ret)
631 return ret;
632
633 hidpp->battery.level =
634 hidpp10_battery_status_map_level(response.rap.params[0]);
635 status = hidpp10_battery_status_map_status(response.rap.params[1]);
636 hidpp->battery.status = status;
637 /* the capacity is only available when discharging or full */
638 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
639 status == POWER_SUPPLY_STATUS_FULL;
640
641 return 0;
642}
643
644#define HIDPP_REG_BATTERY_MILEAGE 0x0D
645
646static int hidpp10_battery_mileage_map_status(u8 param)
647{
648 int status;
649
650 switch (param >> 6) {
651 case 0x00:
652 /* discharging (in use) */
653 status = POWER_SUPPLY_STATUS_DISCHARGING;
654 break;
655 case 0x01: /* charging */
656 status = POWER_SUPPLY_STATUS_CHARGING;
657 break;
658 case 0x02: /* charge complete */
659 status = POWER_SUPPLY_STATUS_FULL;
660 break;
661 /*
662 * 0x03 = charging error
663 */
664 default:
665 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
666 break;
667 }
668
669 return status;
670}
671
672static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
673{
674 struct hidpp_report response;
675 int ret, status;
676
677 ret = hidpp_send_rap_command_sync(hidpp,
678 REPORT_ID_HIDPP_SHORT,
679 HIDPP_GET_REGISTER,
680 HIDPP_REG_BATTERY_MILEAGE,
681 NULL, 0, &response);
682 if (ret)
683 return ret;
684
685 hidpp->battery.capacity = response.rap.params[0];
686 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
687 hidpp->battery.status = status;
688 /* the capacity is only available when discharging or full */
689 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
690 status == POWER_SUPPLY_STATUS_FULL;
691
692 return 0;
693}
694
695static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
696{
697 struct hidpp_report *report = (struct hidpp_report *)data;
698 int status, capacity, level;
699 bool changed;
700
701 if (report->report_id != REPORT_ID_HIDPP_SHORT)
702 return 0;
703
704 switch (report->rap.sub_id) {
705 case HIDPP_REG_BATTERY_STATUS:
706 capacity = hidpp->battery.capacity;
707 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
708 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
709 break;
710 case HIDPP_REG_BATTERY_MILEAGE:
711 capacity = report->rap.params[0];
712 level = hidpp->battery.level;
713 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
714 break;
715 default:
716 return 0;
717 }
718
719 changed = capacity != hidpp->battery.capacity ||
720 level != hidpp->battery.level ||
721 status != hidpp->battery.status;
722
723 /* the capacity is only available when discharging or full */
724 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
725 status == POWER_SUPPLY_STATUS_FULL;
726
727 if (changed) {
728 hidpp->battery.level = level;
729 hidpp->battery.status = status;
730 if (hidpp->battery.ps)
731 power_supply_changed(hidpp->battery.ps);
732 }
733
734 return 0;
735}
736
Benjamin Tissoires33797822014-09-30 13:18:30 -0400737#define HIDPP_REG_PAIRING_INFORMATION 0xB5
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200738#define HIDPP_EXTENDED_PAIRING 0x30
739#define HIDPP_DEVICE_NAME 0x40
Benjamin Tissoires33797822014-09-30 13:18:30 -0400740
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200741static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400742{
743 struct hidpp_report response;
744 int ret;
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200745 u8 params[1] = { HIDPP_DEVICE_NAME };
Benjamin Tissoires33797822014-09-30 13:18:30 -0400746 char *name;
747 int len;
748
749 ret = hidpp_send_rap_command_sync(hidpp_dev,
750 REPORT_ID_HIDPP_SHORT,
751 HIDPP_GET_LONG_REGISTER,
752 HIDPP_REG_PAIRING_INFORMATION,
753 params, 1, &response);
754 if (ret)
755 return NULL;
756
757 len = response.rap.params[1];
758
Peter Wu3a034a72014-12-11 13:51:19 +0100759 if (2 + len > sizeof(response.rap.params))
760 return NULL;
761
Hans de Goede22bf6bd2019-04-20 13:22:06 +0200762 if (len < 4) /* logitech devices are usually at least Xddd */
763 return NULL;
764
Benjamin Tissoires33797822014-09-30 13:18:30 -0400765 name = kzalloc(len + 1, GFP_KERNEL);
766 if (!name)
767 return NULL;
768
769 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500770
771 /* include the terminating '\0' */
772 hidpp_prefix_name(&name, len + 1);
773
Benjamin Tissoires33797822014-09-30 13:18:30 -0400774 return name;
775}
776
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200777static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
778{
779 struct hidpp_report response;
780 int ret;
781 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
782
783 ret = hidpp_send_rap_command_sync(hidpp,
784 REPORT_ID_HIDPP_SHORT,
785 HIDPP_GET_LONG_REGISTER,
786 HIDPP_REG_PAIRING_INFORMATION,
787 params, 1, &response);
788 if (ret)
789 return ret;
790
791 /*
792 * We don't care about LE or BE, we will output it as a string
793 * with %4phD, so we need to keep the order.
794 */
795 *serial = *((u32 *)&response.rap.params[1]);
796 return 0;
797}
798
799static int hidpp_unifying_init(struct hidpp_device *hidpp)
800{
801 struct hid_device *hdev = hidpp->hid_dev;
802 const char *name;
803 u32 serial;
804 int ret;
805
806 ret = hidpp_unifying_get_serial(hidpp, &serial);
807 if (ret)
808 return ret;
809
810 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
811 hdev->product, &serial);
812 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
813
814 name = hidpp_unifying_get_name(hidpp);
815 if (!name)
816 return -EIO;
817
818 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
819 dbg_hid("HID++ Unifying: Got name: %s\n", name);
820
821 kfree(name);
822 return 0;
823}
824
Benjamin Tissoires33797822014-09-30 13:18:30 -0400825/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400826/* 0x0000: Root */
827/* -------------------------------------------------------------------------- */
828
829#define HIDPP_PAGE_ROOT 0x0000
830#define HIDPP_PAGE_ROOT_IDX 0x00
831
832#define CMD_ROOT_GET_FEATURE 0x01
833#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
834
835static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
836 u8 *feature_index, u8 *feature_type)
837{
838 struct hidpp_report response;
839 int ret;
840 u8 params[2] = { feature >> 8, feature & 0x00FF };
841
842 ret = hidpp_send_fap_command_sync(hidpp,
843 HIDPP_PAGE_ROOT_IDX,
844 CMD_ROOT_GET_FEATURE,
845 params, 2, &response);
846 if (ret)
847 return ret;
848
Benjamin Tissoiresa9525b82017-03-27 16:59:32 +0200849 if (response.fap.params[0] == 0)
850 return -ENOENT;
851
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400852 *feature_index = response.fap.params[0];
853 *feature_type = response.fap.params[1];
854
855 return ret;
856}
857
858static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
859{
Hans de Goede09637752019-04-20 13:22:10 +0200860 const u8 ping_byte = 0x5a;
861 u8 ping_data[3] = { 0, 0, ping_byte };
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400862 struct hidpp_report response;
863 int ret;
864
Hans de Goede09637752019-04-20 13:22:10 +0200865 ret = hidpp_send_rap_command_sync(hidpp,
866 REPORT_ID_HIDPP_SHORT,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400867 HIDPP_PAGE_ROOT_IDX,
868 CMD_ROOT_GET_PROTOCOL_VERSION,
Hans de Goede09637752019-04-20 13:22:10 +0200869 ping_data, sizeof(ping_data), &response);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400870
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500871 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400872 hidpp->protocol_major = 1;
873 hidpp->protocol_minor = 0;
Hans de Goede9576af62019-03-22 08:41:38 +0100874 goto print_version;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400875 }
876
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500877 /* the device might not be connected */
878 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
879 return -EIO;
880
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500881 if (ret > 0) {
882 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
883 __func__, ret);
884 return -EPROTO;
885 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400886 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500887 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400888
Hans de Goede09637752019-04-20 13:22:10 +0200889 if (response.rap.params[2] != ping_byte) {
890 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
891 __func__, response.rap.params[2], ping_byte);
892 return -EPROTO;
893 }
894
895 hidpp->protocol_major = response.rap.params[0];
896 hidpp->protocol_minor = response.rap.params[1];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400897
Hans de Goede9576af62019-03-22 08:41:38 +0100898print_version:
899 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
900 hidpp->protocol_major, hidpp->protocol_minor);
901 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400902}
903
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400904/* -------------------------------------------------------------------------- */
905/* 0x0005: GetDeviceNameType */
906/* -------------------------------------------------------------------------- */
907
908#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
909
910#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
911#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
912#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
913
914static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
915 u8 feature_index, u8 *nameLength)
916{
917 struct hidpp_report response;
918 int ret;
919
920 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
921 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
922
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500923 if (ret > 0) {
924 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
925 __func__, ret);
926 return -EPROTO;
927 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400928 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500929 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400930
931 *nameLength = response.fap.params[0];
932
933 return ret;
934}
935
936static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
937 u8 feature_index, u8 char_index, char *device_name, int len_buf)
938{
939 struct hidpp_report response;
940 int ret, i;
941 int count;
942
943 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
944 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
945 &response);
946
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500947 if (ret > 0) {
948 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
949 __func__, ret);
950 return -EPROTO;
951 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400952 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500953 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400954
Simon Wooda5ce8f52015-11-19 16:42:11 -0700955 switch (response.report_id) {
956 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200957 count = hidpp->very_long_report_length - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700958 break;
959 case REPORT_ID_HIDPP_LONG:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400960 count = HIDPP_REPORT_LONG_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700961 break;
962 case REPORT_ID_HIDPP_SHORT:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400963 count = HIDPP_REPORT_SHORT_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700964 break;
965 default:
966 return -EPROTO;
967 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400968
969 if (len_buf < count)
970 count = len_buf;
971
972 for (i = 0; i < count; i++)
973 device_name[i] = response.fap.params[i];
974
975 return count;
976}
977
Peter Wu02cc0972014-12-11 13:51:17 +0100978static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400979{
980 u8 feature_type;
981 u8 feature_index;
982 u8 __name_length;
983 char *name;
984 unsigned index = 0;
985 int ret;
986
987 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
988 &feature_index, &feature_type);
989 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100990 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400991
992 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
993 &__name_length);
994 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100995 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400996
997 name = kzalloc(__name_length + 1, GFP_KERNEL);
998 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100999 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001000
Peter Wu1430ee72014-12-11 13:51:18 +01001001 while (index < __name_length) {
1002 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001003 feature_index, index, name + index,
1004 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +01001005 if (ret <= 0) {
1006 kfree(name);
1007 return NULL;
1008 }
1009 index += ret;
1010 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001011
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -05001012 /* include the terminating '\0' */
1013 hidpp_prefix_name(&name, __name_length + 1);
1014
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001015 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001016}
1017
1018/* -------------------------------------------------------------------------- */
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001019/* 0x1000: Battery level status */
1020/* -------------------------------------------------------------------------- */
1021
1022#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1023
1024#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1025#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1026
1027#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1028
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001029#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1030#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1031#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1032
1033static int hidpp_map_battery_level(int capacity)
1034{
1035 if (capacity < 11)
1036 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
Hans de Goede1f87b0c2019-03-22 08:41:40 +01001037 /*
1038 * The spec says this should be < 31 but some devices report 30
1039 * with brand new batteries and Windows reports 30 as "Good".
1040 */
1041 else if (capacity < 30)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001042 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1043 else if (capacity < 81)
1044 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1045 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1046}
1047
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001048static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001049 int *next_capacity,
1050 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001051{
1052 int status;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001053
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001054 *capacity = data[0];
1055 *next_capacity = data[1];
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001056 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001057
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001058 /* When discharging, we can rely on the device reported capacity.
1059 * For all other states the device reports 0 (unknown).
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001060 */
1061 switch (data[2]) {
1062 case 0: /* discharging (in use) */
1063 status = POWER_SUPPLY_STATUS_DISCHARGING;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001064 *level = hidpp_map_battery_level(*capacity);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001065 break;
1066 case 1: /* recharging */
1067 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001068 break;
1069 case 2: /* charge in final stage */
1070 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001071 break;
1072 case 3: /* charge complete */
1073 status = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001074 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001075 *capacity = 100;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001076 break;
1077 case 4: /* recharging below optimal speed */
1078 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001079 break;
1080 /* 5 = invalid battery type
1081 6 = thermal error
1082 7 = other charging error */
1083 default:
1084 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001085 break;
1086 }
1087
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001088 return status;
1089}
1090
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001091static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1092 u8 feature_index,
1093 int *status,
1094 int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001095 int *next_capacity,
1096 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001097{
1098 struct hidpp_report response;
1099 int ret;
1100 u8 *params = (u8 *)response.fap.params;
1101
1102 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1103 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1104 NULL, 0, &response);
1105 if (ret > 0) {
1106 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1107 __func__, ret);
1108 return -EPROTO;
1109 }
1110 if (ret)
1111 return ret;
1112
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001113 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001114 next_capacity,
1115 level);
1116
1117 return 0;
1118}
1119
1120static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1121 u8 feature_index)
1122{
1123 struct hidpp_report response;
1124 int ret;
1125 u8 *params = (u8 *)response.fap.params;
1126 unsigned int level_count, flags;
1127
1128 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1129 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1130 NULL, 0, &response);
1131 if (ret > 0) {
1132 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1133 __func__, ret);
1134 return -EPROTO;
1135 }
1136 if (ret)
1137 return ret;
1138
1139 level_count = params[0];
1140 flags = params[1];
1141
1142 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1143 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1144 else
1145 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001146
1147 return 0;
1148}
1149
1150static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1151{
1152 u8 feature_type;
1153 int ret;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001154 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001155
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001156 if (hidpp->battery.feature_index == 0xff) {
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001157 ret = hidpp_root_get_feature(hidpp,
1158 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1159 &hidpp->battery.feature_index,
1160 &feature_type);
1161 if (ret)
1162 return ret;
1163 }
1164
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001165 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1166 hidpp->battery.feature_index,
1167 &status, &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001168 &next_capacity, &level);
1169 if (ret)
1170 return ret;
1171
1172 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1173 hidpp->battery.feature_index);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001174 if (ret)
1175 return ret;
1176
1177 hidpp->battery.status = status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001178 hidpp->battery.capacity = capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001179 hidpp->battery.level = level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001180 /* the capacity is only available when discharging or full */
1181 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1182 status == POWER_SUPPLY_STATUS_FULL;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001183
1184 return 0;
1185}
1186
1187static int hidpp20_battery_event(struct hidpp_device *hidpp,
1188 u8 *data, int size)
1189{
1190 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001191 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001192 bool changed;
1193
1194 if (report->fap.feature_index != hidpp->battery.feature_index ||
1195 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1196 return 0;
1197
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001198 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1199 &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001200 &next_capacity,
1201 &level);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001202
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001203 /* the capacity is only available when discharging or full */
1204 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1205 status == POWER_SUPPLY_STATUS_FULL;
1206
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001207 changed = capacity != hidpp->battery.capacity ||
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001208 level != hidpp->battery.level ||
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001209 status != hidpp->battery.status;
1210
1211 if (changed) {
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001212 hidpp->battery.level = level;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001213 hidpp->battery.capacity = capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001214 hidpp->battery.status = status;
1215 if (hidpp->battery.ps)
1216 power_supply_changed(hidpp->battery.ps);
1217 }
1218
1219 return 0;
1220}
1221
1222static enum power_supply_property hidpp_battery_props[] = {
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001223 POWER_SUPPLY_PROP_ONLINE,
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001224 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001225 POWER_SUPPLY_PROP_SCOPE,
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001226 POWER_SUPPLY_PROP_MODEL_NAME,
1227 POWER_SUPPLY_PROP_MANUFACTURER,
1228 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001229 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1230 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001231};
1232
1233static int hidpp_battery_get_property(struct power_supply *psy,
1234 enum power_supply_property psp,
1235 union power_supply_propval *val)
1236{
1237 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1238 int ret = 0;
1239
1240 switch(psp) {
1241 case POWER_SUPPLY_PROP_STATUS:
1242 val->intval = hidpp->battery.status;
1243 break;
1244 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001245 val->intval = hidpp->battery.capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001246 break;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001247 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1248 val->intval = hidpp->battery.level;
1249 break;
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001250 case POWER_SUPPLY_PROP_SCOPE:
1251 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1252 break;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001253 case POWER_SUPPLY_PROP_ONLINE:
1254 val->intval = hidpp->battery.online;
1255 break;
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001256 case POWER_SUPPLY_PROP_MODEL_NAME:
1257 if (!strncmp(hidpp->name, "Logitech ", 9))
1258 val->strval = hidpp->name + 9;
1259 else
1260 val->strval = hidpp->name;
1261 break;
1262 case POWER_SUPPLY_PROP_MANUFACTURER:
1263 val->strval = "Logitech";
1264 break;
1265 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1266 val->strval = hidpp->hid_dev->uniq;
1267 break;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001268 default:
1269 ret = -EINVAL;
1270 break;
1271 }
1272
1273 return ret;
1274}
1275
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001276/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10001277/* 0x2120: Hi-resolution scrolling */
1278/* -------------------------------------------------------------------------- */
1279
1280#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1281
1282#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1283
1284static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1285 bool enabled, u8 *multiplier)
1286{
1287 u8 feature_index;
1288 u8 feature_type;
1289 int ret;
1290 u8 params[1];
1291 struct hidpp_report response;
1292
1293 ret = hidpp_root_get_feature(hidpp,
1294 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1295 &feature_index,
1296 &feature_type);
1297 if (ret)
1298 return ret;
1299
1300 params[0] = enabled ? BIT(0) : 0;
1301 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1302 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1303 params, sizeof(params), &response);
1304 if (ret)
1305 return ret;
1306 *multiplier = response.fap.params[1];
1307 return 0;
1308}
1309
1310/* -------------------------------------------------------------------------- */
1311/* 0x2121: HiRes Wheel */
1312/* -------------------------------------------------------------------------- */
1313
1314#define HIDPP_PAGE_HIRES_WHEEL 0x2121
1315
1316#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1317#define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1318
1319static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1320 u8 *multiplier)
1321{
1322 u8 feature_index;
1323 u8 feature_type;
1324 int ret;
1325 struct hidpp_report response;
1326
1327 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1328 &feature_index, &feature_type);
1329 if (ret)
1330 goto return_default;
1331
1332 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1333 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1334 NULL, 0, &response);
1335 if (ret)
1336 goto return_default;
1337
1338 *multiplier = response.fap.params[0];
1339 return 0;
1340return_default:
1341 hid_warn(hidpp->hid_dev,
1342 "Couldn't get wheel multiplier (error %d)\n", ret);
1343 return ret;
1344}
1345
1346static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1347 bool high_resolution, bool use_hidpp)
1348{
1349 u8 feature_index;
1350 u8 feature_type;
1351 int ret;
1352 u8 params[1];
1353 struct hidpp_report response;
1354
1355 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1356 &feature_index, &feature_type);
1357 if (ret)
1358 return ret;
1359
1360 params[0] = (invert ? BIT(2) : 0) |
1361 (high_resolution ? BIT(1) : 0) |
1362 (use_hidpp ? BIT(0) : 0);
1363
1364 return hidpp_send_fap_command_sync(hidpp, feature_index,
1365 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1366 params, sizeof(params), &response);
1367}
1368
1369/* -------------------------------------------------------------------------- */
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001370/* 0x4301: Solar Keyboard */
1371/* -------------------------------------------------------------------------- */
1372
1373#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1374
1375#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1376
1377#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1378#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1379#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1380
1381static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1382{
1383 struct hidpp_report response;
1384 u8 params[2] = { 1, 1 };
1385 u8 feature_type;
1386 int ret;
1387
1388 if (hidpp->battery.feature_index == 0xff) {
1389 ret = hidpp_root_get_feature(hidpp,
1390 HIDPP_PAGE_SOLAR_KEYBOARD,
1391 &hidpp->battery.solar_feature_index,
1392 &feature_type);
1393 if (ret)
1394 return ret;
1395 }
1396
1397 ret = hidpp_send_fap_command_sync(hidpp,
1398 hidpp->battery.solar_feature_index,
1399 CMD_SOLAR_SET_LIGHT_MEASURE,
1400 params, 2, &response);
1401 if (ret > 0) {
1402 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1403 __func__, ret);
1404 return -EPROTO;
1405 }
1406 if (ret)
1407 return ret;
1408
1409 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1410
1411 return 0;
1412}
1413
1414static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1415 u8 *data, int size)
1416{
1417 struct hidpp_report *report = (struct hidpp_report *)data;
1418 int capacity, lux, status;
1419 u8 function;
1420
1421 function = report->fap.funcindex_clientid;
1422
1423
1424 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1425 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1426 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1427 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1428 return 0;
1429
1430 capacity = report->fap.params[0];
1431
1432 switch (function) {
1433 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1434 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1435 if (lux > 200)
1436 status = POWER_SUPPLY_STATUS_CHARGING;
1437 else
1438 status = POWER_SUPPLY_STATUS_DISCHARGING;
1439 break;
1440 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1441 default:
1442 if (capacity < hidpp->battery.capacity)
1443 status = POWER_SUPPLY_STATUS_DISCHARGING;
1444 else
1445 status = POWER_SUPPLY_STATUS_CHARGING;
1446
1447 }
1448
1449 if (capacity == 100)
1450 status = POWER_SUPPLY_STATUS_FULL;
1451
1452 hidpp->battery.online = true;
1453 if (capacity != hidpp->battery.capacity ||
1454 status != hidpp->battery.status) {
1455 hidpp->battery.capacity = capacity;
1456 hidpp->battery.status = status;
1457 if (hidpp->battery.ps)
1458 power_supply_changed(hidpp->battery.ps);
1459 }
1460
1461 return 0;
1462}
1463
1464/* -------------------------------------------------------------------------- */
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04001465/* 0x6010: Touchpad FW items */
1466/* -------------------------------------------------------------------------- */
1467
1468#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1469
1470#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1471
1472struct hidpp_touchpad_fw_items {
1473 uint8_t presence;
1474 uint8_t desired_state;
1475 uint8_t state;
1476 uint8_t persistent;
1477};
1478
1479/**
1480 * send a set state command to the device by reading the current items->state
1481 * field. items is then filled with the current state.
1482 */
1483static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1484 u8 feature_index,
1485 struct hidpp_touchpad_fw_items *items)
1486{
1487 struct hidpp_report response;
1488 int ret;
1489 u8 *params = (u8 *)response.fap.params;
1490
1491 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1492 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1493
1494 if (ret > 0) {
1495 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1496 __func__, ret);
1497 return -EPROTO;
1498 }
1499 if (ret)
1500 return ret;
1501
1502 items->presence = params[0];
1503 items->desired_state = params[1];
1504 items->state = params[2];
1505 items->persistent = params[3];
1506
1507 return 0;
1508}
1509
1510/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001511/* 0x6100: TouchPadRawXY */
1512/* -------------------------------------------------------------------------- */
1513
1514#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1515
1516#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001517#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1518
1519#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001520
1521#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1522#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1523
1524struct hidpp_touchpad_raw_info {
1525 u16 x_size;
1526 u16 y_size;
1527 u8 z_range;
1528 u8 area_range;
1529 u8 timestamp_unit;
1530 u8 maxcontacts;
1531 u8 origin;
1532 u16 res;
1533};
1534
1535struct hidpp_touchpad_raw_xy_finger {
1536 u8 contact_type;
1537 u8 contact_status;
1538 u16 x;
1539 u16 y;
1540 u8 z;
1541 u8 area;
1542 u8 finger_id;
1543};
1544
1545struct hidpp_touchpad_raw_xy {
1546 u16 timestamp;
1547 struct hidpp_touchpad_raw_xy_finger fingers[2];
1548 u8 spurious_flag;
1549 u8 end_of_frame;
1550 u8 finger_count;
1551 u8 button;
1552};
1553
1554static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1555 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1556{
1557 struct hidpp_report response;
1558 int ret;
1559 u8 *params = (u8 *)response.fap.params;
1560
1561 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1562 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1563
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001564 if (ret > 0) {
1565 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1566 __func__, ret);
1567 return -EPROTO;
1568 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001569 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001570 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001571
1572 raw_info->x_size = get_unaligned_be16(&params[0]);
1573 raw_info->y_size = get_unaligned_be16(&params[2]);
1574 raw_info->z_range = params[4];
1575 raw_info->area_range = params[5];
1576 raw_info->maxcontacts = params[7];
1577 raw_info->origin = params[8];
1578 /* res is given in unit per inch */
1579 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1580
1581 return ret;
1582}
1583
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001584static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1585 u8 feature_index, bool send_raw_reports,
1586 bool sensor_enhanced_settings)
1587{
1588 struct hidpp_report response;
1589
1590 /*
1591 * Params:
1592 * bit 0 - enable raw
1593 * bit 1 - 16bit Z, no area
1594 * bit 2 - enhanced sensitivity
1595 * bit 3 - width, height (4 bits each) instead of area
1596 * bit 4 - send raw + gestures (degrades smoothness)
1597 * remaining bits - reserved
1598 */
1599 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1600
1601 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1602 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1603}
1604
1605static void hidpp_touchpad_touch_event(u8 *data,
1606 struct hidpp_touchpad_raw_xy_finger *finger)
1607{
1608 u8 x_m = data[0] << 2;
1609 u8 y_m = data[2] << 2;
1610
1611 finger->x = x_m << 6 | data[1];
1612 finger->y = y_m << 6 | data[3];
1613
1614 finger->contact_type = data[0] >> 6;
1615 finger->contact_status = data[2] >> 6;
1616
1617 finger->z = data[4];
1618 finger->area = data[5];
1619 finger->finger_id = data[6] >> 4;
1620}
1621
1622static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1623 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1624{
1625 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1626 raw_xy->end_of_frame = data[8] & 0x01;
1627 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1628 raw_xy->finger_count = data[15] & 0x0f;
1629 raw_xy->button = (data[8] >> 2) & 0x01;
1630
1631 if (raw_xy->finger_count) {
1632 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1633 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1634 }
1635}
1636
Edwin Veldsff21a632016-01-11 00:25:15 +01001637/* -------------------------------------------------------------------------- */
1638/* 0x8123: Force feedback support */
1639/* -------------------------------------------------------------------------- */
1640
1641#define HIDPP_FF_GET_INFO 0x01
1642#define HIDPP_FF_RESET_ALL 0x11
1643#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1644#define HIDPP_FF_SET_EFFECT_STATE 0x31
1645#define HIDPP_FF_DESTROY_EFFECT 0x41
1646#define HIDPP_FF_GET_APERTURE 0x51
1647#define HIDPP_FF_SET_APERTURE 0x61
1648#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1649#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1650
1651#define HIDPP_FF_EFFECT_STATE_GET 0x00
1652#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1653#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1654#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1655
1656#define HIDPP_FF_EFFECT_CONSTANT 0x00
1657#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1658#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1659#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1660#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1661#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1662#define HIDPP_FF_EFFECT_SPRING 0x06
1663#define HIDPP_FF_EFFECT_DAMPER 0x07
1664#define HIDPP_FF_EFFECT_FRICTION 0x08
1665#define HIDPP_FF_EFFECT_INERTIA 0x09
1666#define HIDPP_FF_EFFECT_RAMP 0x0A
1667
1668#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1669
1670#define HIDPP_FF_EFFECTID_NONE -1
1671#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1672
1673#define HIDPP_FF_MAX_PARAMS 20
1674#define HIDPP_FF_RESERVED_SLOTS 1
1675
1676struct hidpp_ff_private_data {
1677 struct hidpp_device *hidpp;
1678 u8 feature_index;
1679 u8 version;
1680 u16 gain;
1681 s16 range;
1682 u8 slot_autocenter;
1683 u8 num_effects;
1684 int *effect_ids;
1685 struct workqueue_struct *wq;
1686 atomic_t workqueue_size;
1687};
1688
1689struct hidpp_ff_work_data {
1690 struct work_struct work;
1691 struct hidpp_ff_private_data *data;
1692 int effect_id;
1693 u8 command;
1694 u8 params[HIDPP_FF_MAX_PARAMS];
1695 u8 size;
1696};
1697
Peter Huttererfef33602018-12-05 10:42:25 +10001698static const signed short hidpp_ff_effects[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001699 FF_CONSTANT,
1700 FF_PERIODIC,
1701 FF_SINE,
1702 FF_SQUARE,
1703 FF_SAW_UP,
1704 FF_SAW_DOWN,
1705 FF_TRIANGLE,
1706 FF_SPRING,
1707 FF_DAMPER,
1708 FF_AUTOCENTER,
1709 FF_GAIN,
1710 -1
1711};
1712
Peter Huttererfef33602018-12-05 10:42:25 +10001713static const signed short hidpp_ff_effects_v2[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001714 FF_RAMP,
1715 FF_FRICTION,
1716 FF_INERTIA,
1717 -1
1718};
1719
1720static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1721 HIDPP_FF_EFFECT_SPRING,
1722 HIDPP_FF_EFFECT_FRICTION,
1723 HIDPP_FF_EFFECT_DAMPER,
1724 HIDPP_FF_EFFECT_INERTIA
1725};
1726
1727static const char *HIDPP_FF_CONDITION_NAMES[] = {
1728 "spring",
1729 "friction",
1730 "damper",
1731 "inertia"
1732};
1733
1734
1735static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1736{
1737 int i;
1738
1739 for (i = 0; i < data->num_effects; i++)
1740 if (data->effect_ids[i] == effect_id)
1741 return i+1;
1742
1743 return 0;
1744}
1745
1746static void hidpp_ff_work_handler(struct work_struct *w)
1747{
1748 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1749 struct hidpp_ff_private_data *data = wd->data;
1750 struct hidpp_report response;
1751 u8 slot;
1752 int ret;
1753
1754 /* add slot number if needed */
1755 switch (wd->effect_id) {
1756 case HIDPP_FF_EFFECTID_AUTOCENTER:
1757 wd->params[0] = data->slot_autocenter;
1758 break;
1759 case HIDPP_FF_EFFECTID_NONE:
1760 /* leave slot as zero */
1761 break;
1762 default:
1763 /* find current slot for effect */
1764 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1765 break;
1766 }
1767
1768 /* send command and wait for reply */
1769 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1770 wd->command, wd->params, wd->size, &response);
1771
1772 if (ret) {
1773 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1774 goto out;
1775 }
1776
1777 /* parse return data */
1778 switch (wd->command) {
1779 case HIDPP_FF_DOWNLOAD_EFFECT:
1780 slot = response.fap.params[0];
1781 if (slot > 0 && slot <= data->num_effects) {
1782 if (wd->effect_id >= 0)
1783 /* regular effect uploaded */
1784 data->effect_ids[slot-1] = wd->effect_id;
1785 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1786 /* autocenter spring uploaded */
1787 data->slot_autocenter = slot;
1788 }
1789 break;
1790 case HIDPP_FF_DESTROY_EFFECT:
1791 if (wd->effect_id >= 0)
1792 /* regular effect destroyed */
1793 data->effect_ids[wd->params[0]-1] = -1;
1794 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1795 /* autocenter spring destoyed */
1796 data->slot_autocenter = 0;
1797 break;
1798 case HIDPP_FF_SET_GLOBAL_GAINS:
1799 data->gain = (wd->params[0] << 8) + wd->params[1];
1800 break;
1801 case HIDPP_FF_SET_APERTURE:
1802 data->range = (wd->params[0] << 8) + wd->params[1];
1803 break;
1804 default:
1805 /* no action needed */
1806 break;
1807 }
1808
1809out:
1810 atomic_dec(&data->workqueue_size);
1811 kfree(wd);
1812}
1813
1814static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1815{
1816 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1817 int s;
1818
1819 if (!wd)
1820 return -ENOMEM;
1821
1822 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1823
1824 wd->data = data;
1825 wd->effect_id = effect_id;
1826 wd->command = command;
1827 wd->size = size;
1828 memcpy(wd->params, params, size);
1829
1830 atomic_inc(&data->workqueue_size);
1831 queue_work(data->wq, &wd->work);
1832
1833 /* warn about excessive queue size */
1834 s = atomic_read(&data->workqueue_size);
1835 if (s >= 20 && s % 20 == 0)
1836 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1837
1838 return 0;
1839}
1840
1841static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1842{
1843 struct hidpp_ff_private_data *data = dev->ff->private;
1844 u8 params[20];
1845 u8 size;
1846 int force;
1847
1848 /* set common parameters */
1849 params[2] = effect->replay.length >> 8;
1850 params[3] = effect->replay.length & 255;
1851 params[4] = effect->replay.delay >> 8;
1852 params[5] = effect->replay.delay & 255;
1853
1854 switch (effect->type) {
1855 case FF_CONSTANT:
1856 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1857 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1858 params[6] = force >> 8;
1859 params[7] = force & 255;
1860 params[8] = effect->u.constant.envelope.attack_level >> 7;
1861 params[9] = effect->u.constant.envelope.attack_length >> 8;
1862 params[10] = effect->u.constant.envelope.attack_length & 255;
1863 params[11] = effect->u.constant.envelope.fade_level >> 7;
1864 params[12] = effect->u.constant.envelope.fade_length >> 8;
1865 params[13] = effect->u.constant.envelope.fade_length & 255;
1866 size = 14;
1867 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1868 effect->u.constant.level,
1869 effect->direction, force);
1870 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1871 effect->u.constant.envelope.attack_level,
1872 effect->u.constant.envelope.attack_length,
1873 effect->u.constant.envelope.fade_level,
1874 effect->u.constant.envelope.fade_length);
1875 break;
1876 case FF_PERIODIC:
1877 {
1878 switch (effect->u.periodic.waveform) {
1879 case FF_SINE:
1880 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1881 break;
1882 case FF_SQUARE:
1883 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1884 break;
1885 case FF_SAW_UP:
1886 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1887 break;
1888 case FF_SAW_DOWN:
1889 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1890 break;
1891 case FF_TRIANGLE:
1892 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1893 break;
1894 default:
1895 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1896 return -EINVAL;
1897 }
1898 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1899 params[6] = effect->u.periodic.magnitude >> 8;
1900 params[7] = effect->u.periodic.magnitude & 255;
1901 params[8] = effect->u.periodic.offset >> 8;
1902 params[9] = effect->u.periodic.offset & 255;
1903 params[10] = effect->u.periodic.period >> 8;
1904 params[11] = effect->u.periodic.period & 255;
1905 params[12] = effect->u.periodic.phase >> 8;
1906 params[13] = effect->u.periodic.phase & 255;
1907 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1908 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1909 params[16] = effect->u.periodic.envelope.attack_length & 255;
1910 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1911 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1912 params[19] = effect->u.periodic.envelope.fade_length & 255;
1913 size = 20;
1914 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1915 effect->u.periodic.magnitude, effect->direction,
1916 effect->u.periodic.offset,
1917 effect->u.periodic.period,
1918 effect->u.periodic.phase);
1919 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1920 effect->u.periodic.envelope.attack_level,
1921 effect->u.periodic.envelope.attack_length,
1922 effect->u.periodic.envelope.fade_level,
1923 effect->u.periodic.envelope.fade_length);
1924 break;
1925 }
1926 case FF_RAMP:
1927 params[1] = HIDPP_FF_EFFECT_RAMP;
1928 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1929 params[6] = force >> 8;
1930 params[7] = force & 255;
1931 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1932 params[8] = force >> 8;
1933 params[9] = force & 255;
1934 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1935 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1936 params[12] = effect->u.ramp.envelope.attack_length & 255;
1937 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1938 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1939 params[15] = effect->u.ramp.envelope.fade_length & 255;
1940 size = 16;
1941 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1942 effect->u.ramp.start_level,
1943 effect->u.ramp.end_level,
1944 effect->direction, force);
1945 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1946 effect->u.ramp.envelope.attack_level,
1947 effect->u.ramp.envelope.attack_length,
1948 effect->u.ramp.envelope.fade_level,
1949 effect->u.ramp.envelope.fade_length);
1950 break;
1951 case FF_FRICTION:
1952 case FF_INERTIA:
1953 case FF_SPRING:
1954 case FF_DAMPER:
1955 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1956 params[6] = effect->u.condition[0].left_saturation >> 9;
1957 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1958 params[8] = effect->u.condition[0].left_coeff >> 8;
1959 params[9] = effect->u.condition[0].left_coeff & 255;
1960 params[10] = effect->u.condition[0].deadband >> 9;
1961 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1962 params[12] = effect->u.condition[0].center >> 8;
1963 params[13] = effect->u.condition[0].center & 255;
1964 params[14] = effect->u.condition[0].right_coeff >> 8;
1965 params[15] = effect->u.condition[0].right_coeff & 255;
1966 params[16] = effect->u.condition[0].right_saturation >> 9;
1967 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1968 size = 18;
1969 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1970 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1971 effect->u.condition[0].left_coeff,
1972 effect->u.condition[0].left_saturation,
1973 effect->u.condition[0].right_coeff,
1974 effect->u.condition[0].right_saturation);
1975 dbg_hid(" deadband=%d, center=%d\n",
1976 effect->u.condition[0].deadband,
1977 effect->u.condition[0].center);
1978 break;
1979 default:
1980 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1981 return -EINVAL;
1982 }
1983
1984 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1985}
1986
1987static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1988{
1989 struct hidpp_ff_private_data *data = dev->ff->private;
1990 u8 params[2];
1991
1992 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1993
1994 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1995
1996 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1997}
1998
1999static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2000{
2001 struct hidpp_ff_private_data *data = dev->ff->private;
2002 u8 slot = 0;
2003
2004 dbg_hid("Erasing effect %d.\n", effect_id);
2005
2006 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2007}
2008
2009static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2010{
2011 struct hidpp_ff_private_data *data = dev->ff->private;
2012 u8 params[18];
2013
2014 dbg_hid("Setting autocenter to %d.\n", magnitude);
2015
2016 /* start a standard spring effect */
2017 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2018 /* zero delay and duration */
2019 params[2] = params[3] = params[4] = params[5] = 0;
2020 /* set coeff to 25% of saturation */
2021 params[8] = params[14] = magnitude >> 11;
2022 params[9] = params[15] = (magnitude >> 3) & 255;
2023 params[6] = params[16] = magnitude >> 9;
2024 params[7] = params[17] = (magnitude >> 1) & 255;
2025 /* zero deadband and center */
2026 params[10] = params[11] = params[12] = params[13] = 0;
2027
2028 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2029}
2030
2031static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2032{
2033 struct hidpp_ff_private_data *data = dev->ff->private;
2034 u8 params[4];
2035
2036 dbg_hid("Setting gain to %d.\n", gain);
2037
2038 params[0] = gain >> 8;
2039 params[1] = gain & 255;
2040 params[2] = 0; /* no boost */
2041 params[3] = 0;
2042
2043 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2044}
2045
2046static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2047{
2048 struct hid_device *hid = to_hid_device(dev);
2049 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2050 struct input_dev *idev = hidinput->input;
2051 struct hidpp_ff_private_data *data = idev->ff->private;
2052
2053 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2054}
2055
2056static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2057{
2058 struct hid_device *hid = to_hid_device(dev);
2059 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2060 struct input_dev *idev = hidinput->input;
2061 struct hidpp_ff_private_data *data = idev->ff->private;
2062 u8 params[2];
2063 int range = simple_strtoul(buf, NULL, 10);
2064
2065 range = clamp(range, 180, 900);
2066
2067 params[0] = range >> 8;
2068 params[1] = range & 0x00FF;
2069
2070 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2071
2072 return count;
2073}
2074
2075static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2076
2077static void hidpp_ff_destroy(struct ff_device *ff)
2078{
2079 struct hidpp_ff_private_data *data = ff->private;
2080
2081 kfree(data->effect_ids);
2082}
2083
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01002084static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
Edwin Veldsff21a632016-01-11 00:25:15 +01002085{
2086 struct hid_device *hid = hidpp->hid_dev;
Alan Sternd9d4b1e2019-10-03 14:53:59 -04002087 struct hid_input *hidinput;
2088 struct input_dev *dev;
Edwin Veldsff21a632016-01-11 00:25:15 +01002089 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2090 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2091 struct ff_device *ff;
2092 struct hidpp_report response;
2093 struct hidpp_ff_private_data *data;
2094 int error, j, num_slots;
2095 u8 version;
2096
Alan Sternd9d4b1e2019-10-03 14:53:59 -04002097 if (list_empty(&hid->inputs)) {
2098 hid_err(hid, "no inputs found\n");
2099 return -ENODEV;
2100 }
2101 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2102 dev = hidinput->input;
2103
Edwin Veldsff21a632016-01-11 00:25:15 +01002104 if (!dev) {
2105 hid_err(hid, "Struct input_dev not set!\n");
2106 return -EINVAL;
2107 }
2108
2109 /* Get firmware release */
2110 version = bcdDevice & 255;
2111
2112 /* Set supported force feedback capabilities */
Peter Huttererfef33602018-12-05 10:42:25 +10002113 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2114 set_bit(hidpp_ff_effects[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002115 if (version > 1)
Peter Huttererfef33602018-12-05 10:42:25 +10002116 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2117 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002118
2119 /* Read number of slots available in device */
2120 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2121 HIDPP_FF_GET_INFO, NULL, 0, &response);
2122 if (error) {
2123 if (error < 0)
2124 return error;
2125 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2126 __func__, error);
2127 return -EPROTO;
2128 }
2129
2130 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2131
2132 error = input_ff_create(dev, num_slots);
2133
2134 if (error) {
2135 hid_err(dev, "Failed to create FF device!\n");
2136 return error;
2137 }
2138
2139 data = kzalloc(sizeof(*data), GFP_KERNEL);
2140 if (!data)
2141 return -ENOMEM;
2142 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2143 if (!data->effect_ids) {
2144 kfree(data);
2145 return -ENOMEM;
2146 }
Kangjie Lu6c44b152019-03-14 00:24:02 -05002147 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2148 if (!data->wq) {
2149 kfree(data->effect_ids);
2150 kfree(data);
2151 return -ENOMEM;
2152 }
2153
Edwin Veldsff21a632016-01-11 00:25:15 +01002154 data->hidpp = hidpp;
2155 data->feature_index = feature_index;
2156 data->version = version;
2157 data->slot_autocenter = 0;
2158 data->num_effects = num_slots;
2159 for (j = 0; j < num_slots; j++)
2160 data->effect_ids[j] = -1;
2161
2162 ff = dev->ff;
2163 ff->private = data;
2164
2165 ff->upload = hidpp_ff_upload_effect;
2166 ff->erase = hidpp_ff_erase_effect;
2167 ff->playback = hidpp_ff_playback;
2168 ff->set_gain = hidpp_ff_set_gain;
2169 ff->set_autocenter = hidpp_ff_set_autocenter;
2170 ff->destroy = hidpp_ff_destroy;
2171
2172
2173 /* reset all forces */
2174 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2175 HIDPP_FF_RESET_ALL, NULL, 0, &response);
2176
2177 /* Read current Range */
2178 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2179 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
2180 if (error)
2181 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
2182 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
2183
2184 /* Create sysfs interface */
2185 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2186 if (error)
2187 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2188
2189 /* Read the current gain values */
2190 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2191 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
2192 if (error)
2193 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
2194 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
2195 /* ignore boost value at response.fap.params[2] */
2196
2197 /* init the hardware command queue */
Edwin Veldsff21a632016-01-11 00:25:15 +01002198 atomic_set(&data->workqueue_size, 0);
2199
2200 /* initialize with zero autocenter to get wheel in usable state */
2201 hidpp_ff_set_autocenter(dev, 0);
2202
Colin Ian Kingdf47b242017-07-14 14:37:12 +01002203 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2204 version);
Edwin Veldsff21a632016-01-11 00:25:15 +01002205
2206 return 0;
2207}
2208
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01002209static int hidpp_ff_deinit(struct hid_device *hid)
Edwin Veldsff21a632016-01-11 00:25:15 +01002210{
2211 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2212 struct input_dev *dev = hidinput->input;
2213 struct hidpp_ff_private_data *data;
2214
2215 if (!dev) {
2216 hid_err(hid, "Struct input_dev not found!\n");
2217 return -EINVAL;
2218 }
2219
2220 hid_info(hid, "Unloading HID++ force feedback.\n");
2221 data = dev->ff->private;
2222 if (!data) {
2223 hid_err(hid, "Private data not found!\n");
2224 return -EINVAL;
2225 }
2226
2227 destroy_workqueue(data->wq);
2228 device_remove_file(&hid->dev, &dev_attr_range);
2229
2230 return 0;
2231}
2232
2233
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002234/* ************************************************************************** */
2235/* */
2236/* Device Support */
2237/* */
2238/* ************************************************************************** */
2239
2240/* -------------------------------------------------------------------------- */
2241/* Touchpad HID++ devices */
2242/* -------------------------------------------------------------------------- */
2243
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002244#define WTP_MANUAL_RESOLUTION 39
2245
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002246struct wtp_data {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002247 u16 x_size, y_size;
2248 u8 finger_count;
2249 u8 mt_feature_index;
2250 u8 button_feature_index;
2251 u8 maxcontacts;
2252 bool flip_y;
2253 unsigned int resolution;
2254};
2255
2256static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2257 struct hid_field *field, struct hid_usage *usage,
2258 unsigned long **bit, int *max)
2259{
2260 return -1;
2261}
2262
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002263static void wtp_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02002264 struct input_dev *input_dev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002265{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002266 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002267
2268 __set_bit(EV_ABS, input_dev->evbit);
2269 __set_bit(EV_KEY, input_dev->evbit);
2270 __clear_bit(EV_REL, input_dev->evbit);
2271 __clear_bit(EV_LED, input_dev->evbit);
2272
2273 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2274 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2275 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2276 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2277
2278 /* Max pressure is not given by the devices, pick one */
2279 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2280
2281 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2282
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002283 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2284 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2285 else
2286 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002287
2288 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2289 INPUT_MT_DROP_UNUSED);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002290}
2291
Hans de Goede06104302019-04-20 13:22:13 +02002292static void wtp_touch_event(struct hidpp_device *hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002293 struct hidpp_touchpad_raw_xy_finger *touch_report)
2294{
Hans de Goede06104302019-04-20 13:22:13 +02002295 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002296 int slot;
2297
2298 if (!touch_report->finger_id || touch_report->contact_type)
2299 /* no actual data */
2300 return;
2301
Hans de Goede06104302019-04-20 13:22:13 +02002302 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002303
Hans de Goede06104302019-04-20 13:22:13 +02002304 input_mt_slot(hidpp->input, slot);
2305 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002306 touch_report->contact_status);
2307 if (touch_report->contact_status) {
Hans de Goede06104302019-04-20 13:22:13 +02002308 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002309 touch_report->x);
Hans de Goede06104302019-04-20 13:22:13 +02002310 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002311 wd->flip_y ? wd->y_size - touch_report->y :
2312 touch_report->y);
Hans de Goede06104302019-04-20 13:22:13 +02002313 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002314 touch_report->area);
2315 }
2316}
2317
2318static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2319 struct hidpp_touchpad_raw_xy *raw)
2320{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002321 int i;
2322
2323 for (i = 0; i < 2; i++)
Hans de Goede06104302019-04-20 13:22:13 +02002324 wtp_touch_event(hidpp, &(raw->fingers[i]));
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002325
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002326 if (raw->end_of_frame &&
2327 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Hans de Goede06104302019-04-20 13:22:13 +02002328 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002329
2330 if (raw->end_of_frame || raw->finger_count <= 2) {
Hans de Goede06104302019-04-20 13:22:13 +02002331 input_mt_sync_frame(hidpp->input);
2332 input_sync(hidpp->input);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002333 }
2334}
2335
2336static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2337{
2338 struct wtp_data *wd = hidpp->private_data;
2339 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2340 (data[7] >> 4) * (data[7] >> 4)) / 2;
2341 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2342 (data[13] >> 4) * (data[13] >> 4)) / 2;
2343 struct hidpp_touchpad_raw_xy raw = {
2344 .timestamp = data[1],
2345 .fingers = {
2346 {
2347 .contact_type = 0,
2348 .contact_status = !!data[7],
2349 .x = get_unaligned_le16(&data[3]),
2350 .y = get_unaligned_le16(&data[5]),
2351 .z = c1_area,
2352 .area = c1_area,
2353 .finger_id = data[2],
2354 }, {
2355 .contact_type = 0,
2356 .contact_status = !!data[13],
2357 .x = get_unaligned_le16(&data[9]),
2358 .y = get_unaligned_le16(&data[11]),
2359 .z = c2_area,
2360 .area = c2_area,
2361 .finger_id = data[8],
2362 }
2363 },
2364 .finger_count = wd->maxcontacts,
2365 .spurious_flag = 0,
2366 .end_of_frame = (data[0] >> 7) == 0,
2367 .button = data[0] & 0x01,
2368 };
2369
2370 wtp_send_raw_xy_event(hidpp, &raw);
2371
2372 return 1;
2373}
2374
2375static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2376{
2377 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2378 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002379 struct hidpp_report *report = (struct hidpp_report *)data;
2380 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002381
Hans de Goede06104302019-04-20 13:22:13 +02002382 if (!wd || !hidpp->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002383 return 1;
2384
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002385 switch (data[0]) {
2386 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +01002387 if (size < 2) {
2388 hid_err(hdev, "Received HID report of bad size (%d)",
2389 size);
2390 return 1;
2391 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002392 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
Hans de Goede06104302019-04-20 13:22:13 +02002393 input_event(hidpp->input, EV_KEY, BTN_LEFT,
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002394 !!(data[1] & 0x01));
Hans de Goede06104302019-04-20 13:22:13 +02002395 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002396 !!(data[1] & 0x02));
Hans de Goede06104302019-04-20 13:22:13 +02002397 input_sync(hidpp->input);
Peter Wu8abd8202014-12-16 01:50:16 +01002398 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002399 } else {
2400 if (size < 21)
2401 return 1;
2402 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2403 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002404 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +01002405 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002406 if ((report->fap.feature_index != wd->mt_feature_index) ||
2407 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2408 return 1;
2409 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2410
2411 wtp_send_raw_xy_event(hidpp, &raw);
2412 return 0;
2413 }
2414
2415 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002416}
2417
2418static int wtp_get_config(struct hidpp_device *hidpp)
2419{
2420 struct wtp_data *wd = hidpp->private_data;
2421 struct hidpp_touchpad_raw_info raw_info = {0};
2422 u8 feature_type;
2423 int ret;
2424
2425 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2426 &wd->mt_feature_index, &feature_type);
2427 if (ret)
2428 /* means that the device is not powered up */
2429 return ret;
2430
2431 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2432 &raw_info);
2433 if (ret)
2434 return ret;
2435
2436 wd->x_size = raw_info.x_size;
2437 wd->y_size = raw_info.y_size;
2438 wd->maxcontacts = raw_info.maxcontacts;
2439 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2440 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002441 if (!wd->resolution)
2442 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002443
2444 return 0;
2445}
2446
2447static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2448{
2449 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2450 struct wtp_data *wd;
2451
2452 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2453 GFP_KERNEL);
2454 if (!wd)
2455 return -ENOMEM;
2456
2457 hidpp->private_data = wd;
2458
2459 return 0;
2460};
2461
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002462static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002463{
2464 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2465 struct wtp_data *wd = hidpp->private_data;
2466 int ret;
2467
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002468 if (!wd->x_size) {
2469 ret = wtp_get_config(hidpp);
2470 if (ret) {
2471 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002472 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002473 }
2474 }
2475
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002476 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002477 true, true);
2478}
2479
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002480/* ------------------------------------------------------------------------- */
2481/* Logitech M560 devices */
2482/* ------------------------------------------------------------------------- */
2483
2484/*
2485 * Logitech M560 protocol overview
2486 *
2487 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2488 * the sides buttons are pressed, it sends some keyboard keys events
2489 * instead of buttons ones.
2490 * To complicate things further, the middle button keys sequence
2491 * is different from the odd press and the even press.
2492 *
2493 * forward button -> Super_R
2494 * backward button -> Super_L+'d' (press only)
2495 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2496 * 2nd time: left-click (press only)
2497 * NB: press-only means that when the button is pressed, the
2498 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2499 * together sequentially; instead when the button is released, no event is
2500 * generated !
2501 *
2502 * With the command
2503 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2504 * the mouse reacts differently:
2505 * - it never sends a keyboard key event
2506 * - for the three mouse button it sends:
2507 * middle button press 11<xx>0a 3500af00...
2508 * side 1 button (forward) press 11<xx>0a 3500b000...
2509 * side 2 button (backward) press 11<xx>0a 3500ae00...
2510 * middle/side1/side2 button release 11<xx>0a 35000000...
2511 */
2512
2513static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2514
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002515/* how buttons are mapped in the report */
2516#define M560_MOUSE_BTN_LEFT 0x01
2517#define M560_MOUSE_BTN_RIGHT 0x02
2518#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2519#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2520
2521#define M560_SUB_ID 0x0a
2522#define M560_BUTTON_MODE_REGISTER 0x35
2523
2524static int m560_send_config_command(struct hid_device *hdev, bool connected)
2525{
2526 struct hidpp_report response;
2527 struct hidpp_device *hidpp_dev;
2528
2529 hidpp_dev = hid_get_drvdata(hdev);
2530
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002531 return hidpp_send_rap_command_sync(
2532 hidpp_dev,
2533 REPORT_ID_HIDPP_SHORT,
2534 M560_SUB_ID,
2535 M560_BUTTON_MODE_REGISTER,
2536 (u8 *)m560_config_parameter,
2537 sizeof(m560_config_parameter),
2538 &response
2539 );
2540}
2541
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002542static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2543{
2544 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002545
2546 /* sanity check */
Hans de Goede06104302019-04-20 13:22:13 +02002547 if (!hidpp->input) {
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002548 hid_err(hdev, "error in parameter\n");
2549 return -EINVAL;
2550 }
2551
2552 if (size < 7) {
2553 hid_err(hdev, "error in report\n");
2554 return 0;
2555 }
2556
2557 if (data[0] == REPORT_ID_HIDPP_LONG &&
2558 data[2] == M560_SUB_ID && data[6] == 0x00) {
2559 /*
2560 * m560 mouse report for middle, forward and backward button
2561 *
2562 * data[0] = 0x11
2563 * data[1] = device-id
2564 * data[2] = 0x0a
2565 * data[5] = 0xaf -> middle
2566 * 0xb0 -> forward
2567 * 0xae -> backward
2568 * 0x00 -> release all
2569 * data[6] = 0x00
2570 */
2571
2572 switch (data[5]) {
2573 case 0xaf:
Hans de Goede06104302019-04-20 13:22:13 +02002574 input_report_key(hidpp->input, BTN_MIDDLE, 1);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002575 break;
2576 case 0xb0:
Hans de Goede06104302019-04-20 13:22:13 +02002577 input_report_key(hidpp->input, BTN_FORWARD, 1);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002578 break;
2579 case 0xae:
Hans de Goede06104302019-04-20 13:22:13 +02002580 input_report_key(hidpp->input, BTN_BACK, 1);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002581 break;
2582 case 0x00:
Hans de Goede06104302019-04-20 13:22:13 +02002583 input_report_key(hidpp->input, BTN_BACK, 0);
2584 input_report_key(hidpp->input, BTN_FORWARD, 0);
2585 input_report_key(hidpp->input, BTN_MIDDLE, 0);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002586 break;
2587 default:
2588 hid_err(hdev, "error in report\n");
2589 return 0;
2590 }
Hans de Goede06104302019-04-20 13:22:13 +02002591 input_sync(hidpp->input);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002592
2593 } else if (data[0] == 0x02) {
2594 /*
2595 * Logitech M560 mouse report
2596 *
2597 * data[0] = type (0x02)
2598 * data[1..2] = buttons
2599 * data[3..5] = xy
2600 * data[6] = wheel
2601 */
2602
2603 int v;
2604
Hans de Goede06104302019-04-20 13:22:13 +02002605 input_report_key(hidpp->input, BTN_LEFT,
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002606 !!(data[1] & M560_MOUSE_BTN_LEFT));
Hans de Goede06104302019-04-20 13:22:13 +02002607 input_report_key(hidpp->input, BTN_RIGHT,
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002608 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2609
Harry Cutts4435ff22018-12-05 10:42:27 +10002610 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
Hans de Goede06104302019-04-20 13:22:13 +02002611 input_report_rel(hidpp->input, REL_HWHEEL, -1);
2612 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
Harry Cutts4435ff22018-12-05 10:42:27 +10002613 -120);
2614 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
Hans de Goede06104302019-04-20 13:22:13 +02002615 input_report_rel(hidpp->input, REL_HWHEEL, 1);
2616 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
Harry Cutts4435ff22018-12-05 10:42:27 +10002617 120);
2618 }
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002619
2620 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
Hans de Goede06104302019-04-20 13:22:13 +02002621 input_report_rel(hidpp->input, REL_X, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002622
2623 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
Hans de Goede06104302019-04-20 13:22:13 +02002624 input_report_rel(hidpp->input, REL_Y, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002625
2626 v = hid_snto32(data[6], 8);
Peter Huttererfd357592019-03-20 08:48:23 +10002627 if (v != 0)
Hans de Goede06104302019-04-20 13:22:13 +02002628 hidpp_scroll_counter_handle_scroll(hidpp->input,
Peter Huttererfd357592019-03-20 08:48:23 +10002629 &hidpp->vertical_wheel_counter, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002630
Hans de Goede06104302019-04-20 13:22:13 +02002631 input_sync(hidpp->input);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002632 }
2633
2634 return 1;
2635}
2636
2637static void m560_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02002638 struct input_dev *input_dev)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002639{
Hans de Goede06104302019-04-20 13:22:13 +02002640 __set_bit(EV_KEY, input_dev->evbit);
2641 __set_bit(BTN_MIDDLE, input_dev->keybit);
2642 __set_bit(BTN_RIGHT, input_dev->keybit);
2643 __set_bit(BTN_LEFT, input_dev->keybit);
2644 __set_bit(BTN_BACK, input_dev->keybit);
2645 __set_bit(BTN_FORWARD, input_dev->keybit);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002646
Hans de Goede06104302019-04-20 13:22:13 +02002647 __set_bit(EV_REL, input_dev->evbit);
2648 __set_bit(REL_X, input_dev->relbit);
2649 __set_bit(REL_Y, input_dev->relbit);
2650 __set_bit(REL_WHEEL, input_dev->relbit);
2651 __set_bit(REL_HWHEEL, input_dev->relbit);
2652 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2653 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002654}
2655
2656static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2657 struct hid_field *field, struct hid_usage *usage,
2658 unsigned long **bit, int *max)
2659{
2660 return -1;
2661}
2662
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002663/* ------------------------------------------------------------------------- */
2664/* Logitech K400 devices */
2665/* ------------------------------------------------------------------------- */
2666
2667/*
2668 * The Logitech K400 keyboard has an embedded touchpad which is seen
2669 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2670 * tap-to-click but the setting is not remembered accross reset, annoying some
2671 * users.
2672 *
2673 * We can toggle this feature from the host by using the feature 0x6010:
2674 * Touchpad FW items
2675 */
2676
2677struct k400_private_data {
2678 u8 feature_index;
2679};
2680
2681static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2682{
2683 struct k400_private_data *k400 = hidpp->private_data;
2684 struct hidpp_touchpad_fw_items items = {};
2685 int ret;
2686 u8 feature_type;
2687
2688 if (!k400->feature_index) {
2689 ret = hidpp_root_get_feature(hidpp,
2690 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2691 &k400->feature_index, &feature_type);
2692 if (ret)
2693 /* means that the device is not powered up */
2694 return ret;
2695 }
2696
2697 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2698 if (ret)
2699 return ret;
2700
2701 return 0;
2702}
2703
2704static int k400_allocate(struct hid_device *hdev)
2705{
2706 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2707 struct k400_private_data *k400;
2708
2709 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2710 GFP_KERNEL);
2711 if (!k400)
2712 return -ENOMEM;
2713
2714 hidpp->private_data = k400;
2715
2716 return 0;
2717};
2718
2719static int k400_connect(struct hid_device *hdev, bool connected)
2720{
2721 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2722
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002723 if (!disable_tap_to_click)
2724 return 0;
2725
2726 return k400_disable_tap_to_click(hidpp);
2727}
2728
Simon Wood7f4b49f2015-11-19 16:42:13 -07002729/* ------------------------------------------------------------------------- */
2730/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2731/* ------------------------------------------------------------------------- */
2732
2733#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2734
Simon Wood7f4b49f2015-11-19 16:42:13 -07002735static int g920_get_config(struct hidpp_device *hidpp)
2736{
Simon Wood7f4b49f2015-11-19 16:42:13 -07002737 u8 feature_type;
2738 u8 feature_index;
2739 int ret;
2740
Simon Wood7f4b49f2015-11-19 16:42:13 -07002741 /* Find feature and store for later use */
2742 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2743 &feature_index, &feature_type);
2744 if (ret)
2745 return ret;
2746
Edwin Veldsff21a632016-01-11 00:25:15 +01002747 ret = hidpp_ff_init(hidpp, feature_index);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002748 if (ret)
Edwin Veldsff21a632016-01-11 00:25:15 +01002749 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2750 ret);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002751
2752 return 0;
2753}
2754
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002755/* -------------------------------------------------------------------------- */
Hans de Goede4a79bcc2019-04-20 13:22:15 +02002756/* HID++1.0 devices which use HID++ reports for their wheels */
2757/* -------------------------------------------------------------------------- */
2758static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
2759{
2760 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2761 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
2762 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
2763}
2764
2765static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
2766 u8 *data, int size)
2767{
2768 s8 value, hvalue;
2769
2770 if (!hidpp->input)
2771 return -EINVAL;
2772
2773 if (size < 7)
2774 return 0;
2775
2776 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
2777 return 0;
2778
2779 value = data[3];
2780 hvalue = data[4];
2781
2782 input_report_rel(hidpp->input, REL_WHEEL, value);
2783 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
2784 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
2785 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
2786 input_sync(hidpp->input);
2787
2788 return 1;
2789}
2790
2791static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
2792 struct input_dev *input_dev)
2793{
2794 __set_bit(EV_REL, input_dev->evbit);
2795 __set_bit(REL_WHEEL, input_dev->relbit);
2796 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2797 __set_bit(REL_HWHEEL, input_dev->relbit);
2798 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
2799}
2800
2801/* -------------------------------------------------------------------------- */
Hans de Goede7457bc12019-04-20 13:22:16 +02002802/* HID++1.0 mice which use HID++ reports for extra mouse buttons */
2803/* -------------------------------------------------------------------------- */
2804static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
2805{
2806 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2807 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
2808 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
2809}
2810
2811static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
2812 u8 *data, int size)
2813{
2814 int i;
2815
2816 if (!hidpp->input)
2817 return -EINVAL;
2818
2819 if (size < 7)
2820 return 0;
2821
2822 if (data[0] != REPORT_ID_HIDPP_SHORT ||
2823 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
2824 return 0;
2825
2826 /*
2827 * Buttons are either delivered through the regular mouse report *or*
2828 * through the extra buttons report. At least for button 6 how it is
2829 * delivered differs per receiver firmware version. Even receivers with
2830 * the same usb-id show different behavior, so we handle both cases.
2831 */
2832 for (i = 0; i < 8; i++)
2833 input_report_key(hidpp->input, BTN_MOUSE + i,
2834 (data[3] & (1 << i)));
2835
2836 /* Some mice report events on button 9+, use BTN_MISC */
2837 for (i = 0; i < 8; i++)
2838 input_report_key(hidpp->input, BTN_MISC + i,
2839 (data[4] & (1 << i)));
2840
2841 input_sync(hidpp->input);
2842 return 1;
2843}
2844
2845static void hidpp10_extra_mouse_buttons_populate_input(
2846 struct hidpp_device *hidpp, struct input_dev *input_dev)
2847{
2848 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
2849 __set_bit(BTN_0, input_dev->keybit);
2850 __set_bit(BTN_1, input_dev->keybit);
2851 __set_bit(BTN_2, input_dev->keybit);
2852 __set_bit(BTN_3, input_dev->keybit);
2853 __set_bit(BTN_4, input_dev->keybit);
2854 __set_bit(BTN_5, input_dev->keybit);
2855 __set_bit(BTN_6, input_dev->keybit);
2856 __set_bit(BTN_7, input_dev->keybit);
2857}
2858
2859/* -------------------------------------------------------------------------- */
Hans de Goede42bc4f32019-04-20 13:22:17 +02002860/* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
2861/* -------------------------------------------------------------------------- */
2862
2863/* Find the consumer-page input report desc and change Maximums to 0x107f */
2864static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
2865 u8 *_rdesc, unsigned int *rsize)
2866{
2867 /* Note 0 terminated so we can use strnstr to search for this. */
Colin Ian Kinga96a8a52019-05-10 14:17:22 +01002868 static const char consumer_rdesc_start[] = {
Hans de Goede42bc4f32019-04-20 13:22:17 +02002869 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
2870 0x09, 0x01, /* USAGE (Consumer Control) */
2871 0xA1, 0x01, /* COLLECTION (Application) */
2872 0x85, 0x03, /* REPORT_ID = 3 */
2873 0x75, 0x10, /* REPORT_SIZE (16) */
2874 0x95, 0x02, /* REPORT_COUNT (2) */
2875 0x15, 0x01, /* LOGICAL_MIN (1) */
2876 0x26, 0x00 /* LOGICAL_MAX (... */
2877 };
2878 char *consumer_rdesc, *rdesc = (char *)_rdesc;
2879 unsigned int size;
2880
2881 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
2882 size = *rsize - (consumer_rdesc - rdesc);
2883 if (consumer_rdesc && size >= 25) {
2884 consumer_rdesc[15] = 0x7f;
2885 consumer_rdesc[16] = 0x10;
2886 consumer_rdesc[20] = 0x7f;
2887 consumer_rdesc[21] = 0x10;
2888 }
2889 return _rdesc;
2890}
2891
2892static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
2893{
2894 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2895 HIDPP_ENABLE_CONSUMER_REPORT,
2896 HIDPP_ENABLE_CONSUMER_REPORT);
2897}
2898
2899static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
2900 u8 *data, int size)
2901{
2902 u8 consumer_report[5];
2903
2904 if (size < 7)
2905 return 0;
2906
2907 if (data[0] != REPORT_ID_HIDPP_SHORT ||
2908 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
2909 return 0;
2910
2911 /*
2912 * Build a normal consumer report (3) out of the data, this detour
2913 * is necessary to get some keyboards to report their 0x10xx usages.
2914 */
2915 consumer_report[0] = 0x03;
2916 memcpy(&consumer_report[1], &data[3], 4);
2917 /* We are called from atomic context */
2918 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
2919 consumer_report, 5, 1);
2920
2921 return 1;
2922}
2923
2924/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10002925/* High-resolution scroll wheels */
2926/* -------------------------------------------------------------------------- */
2927
2928static int hi_res_scroll_enable(struct hidpp_device *hidpp)
2929{
2930 int ret;
2931 u8 multiplier = 1;
2932
2933 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
2934 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
2935 if (ret == 0)
2936 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
2937 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
2938 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
2939 &multiplier);
2940 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
2941 ret = hidpp10_enable_scrolling_acceleration(hidpp);
2942 multiplier = 8;
2943 }
2944 if (ret)
2945 return ret;
2946
2947 if (multiplier == 0)
2948 multiplier = 1;
2949
2950 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
2951 hid_info(hidpp->hid_dev, "multiplier = %d\n", multiplier);
2952 return 0;
2953}
2954
2955/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002956/* Generic HID++ devices */
2957/* -------------------------------------------------------------------------- */
2958
Hans de Goede42bc4f32019-04-20 13:22:17 +02002959static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
2960 unsigned int *rsize)
2961{
2962 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2963
2964 if (!hidpp)
2965 return rdesc;
2966
2967 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
2968 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
2969 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
2970 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
2971
2972 return rdesc;
2973}
2974
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002975static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2976 struct hid_field *field, struct hid_usage *usage,
2977 unsigned long **bit, int *max)
2978{
2979 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2980
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002981 if (!hidpp)
2982 return 0;
2983
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002984 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2985 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002986 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2987 field->application != HID_GD_MOUSE)
2988 return m560_input_mapping(hdev, hi, field, usage, bit, max);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002989
2990 return 0;
2991}
2992
Simon Wood0b1804e2015-11-19 16:42:15 -07002993static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2994 struct hid_field *field, struct hid_usage *usage,
2995 unsigned long **bit, int *max)
2996{
2997 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2998
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002999 if (!hidpp)
3000 return 0;
3001
Simon Wood0b1804e2015-11-19 16:42:15 -07003002 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3003 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3004 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3005 usage->code == ABS_Y || usage->code == ABS_Z ||
3006 usage->code == ABS_RZ)) {
3007 field->application = HID_GD_MULTIAXIS;
3008 }
3009 }
3010
3011 return 0;
3012}
3013
3014
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003015static void hidpp_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02003016 struct input_dev *input)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003017{
Hans de Goede06104302019-04-20 13:22:13 +02003018 hidpp->input = input;
3019
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003020 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
Hans de Goedee54abaf2019-04-20 13:22:09 +02003021 wtp_populate_input(hidpp, input);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003022 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
Hans de Goedee54abaf2019-04-20 13:22:09 +02003023 m560_populate_input(hidpp, input);
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003024
3025 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3026 hidpp10_wheel_populate_input(hidpp, input);
Hans de Goede7457bc12019-04-20 13:22:16 +02003027
3028 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3029 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003030}
3031
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07003032static int hidpp_input_configured(struct hid_device *hdev,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003033 struct hid_input *hidinput)
3034{
3035 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003036 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003037
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003038 if (!hidpp)
3039 return 0;
3040
Hans de Goedee54abaf2019-04-20 13:22:09 +02003041 hidpp_populate_input(hidpp, input);
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07003042
3043 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003044}
3045
3046static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3047 int size)
3048{
3049 struct hidpp_report *question = hidpp->send_receive_buf;
3050 struct hidpp_report *answer = hidpp->send_receive_buf;
3051 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02003052 int ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003053
3054 /*
3055 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +01003056 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003057 */
3058 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3059 /*
3060 * Check for a correct hidpp20 answer or the corresponding
3061 * error
3062 */
3063 if (hidpp_match_answer(question, report) ||
3064 hidpp_match_error(question, report)) {
3065 *answer = *report;
3066 hidpp->answer_available = true;
3067 wake_up(&hidpp->wait);
3068 /*
3069 * This was an answer to a command that this driver sent
3070 * We return 1 to hid-core to avoid forwarding the
3071 * command upstream as it has been treated by the driver
3072 */
3073
3074 return 1;
3075 }
3076 }
3077
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003078 if (unlikely(hidpp_report_is_connect_event(report))) {
3079 atomic_set(&hidpp->connected,
3080 !(report->rap.params[0] & (1 << 6)));
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003081 if (schedule_work(&hidpp->work) == 0)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003082 dbg_hid("%s: connect event already queued\n", __func__);
3083 return 1;
3084 }
3085
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02003086 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3087 ret = hidpp20_battery_event(hidpp, data, size);
3088 if (ret != 0)
3089 return ret;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003090 ret = hidpp_solar_battery_event(hidpp, data, size);
3091 if (ret != 0)
3092 return ret;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02003093 }
3094
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003095 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3096 ret = hidpp10_battery_event(hidpp, data, size);
3097 if (ret != 0)
3098 return ret;
3099 }
3100
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003101 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3102 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3103 if (ret != 0)
3104 return ret;
3105 }
3106
Hans de Goede7457bc12019-04-20 13:22:16 +02003107 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3108 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3109 if (ret != 0)
3110 return ret;
3111 }
3112
Hans de Goede42bc4f32019-04-20 13:22:17 +02003113 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3114 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3115 if (ret != 0)
3116 return ret;
3117 }
3118
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003119 return 0;
3120}
3121
3122static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3123 u8 *data, int size)
3124{
3125 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01003126 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003127
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003128 if (!hidpp)
3129 return 0;
3130
Peter Wue529fea2014-12-17 00:23:51 +01003131 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003132 switch (data[0]) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07003133 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +02003134 if (size != hidpp->very_long_report_length) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07003135 hid_err(hdev, "received hid++ report of bad size (%d)",
3136 size);
3137 return 1;
3138 }
3139 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3140 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003141 case REPORT_ID_HIDPP_LONG:
3142 if (size != HIDPP_REPORT_LONG_LENGTH) {
3143 hid_err(hdev, "received hid++ report of bad size (%d)",
3144 size);
3145 return 1;
3146 }
Peter Wue529fea2014-12-17 00:23:51 +01003147 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3148 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003149 case REPORT_ID_HIDPP_SHORT:
3150 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3151 hid_err(hdev, "received hid++ report of bad size (%d)",
3152 size);
3153 return 1;
3154 }
Peter Wue529fea2014-12-17 00:23:51 +01003155 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3156 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003157 }
3158
Peter Wue529fea2014-12-17 00:23:51 +01003159 /* If no report is available for further processing, skip calling
3160 * raw_event of subclasses. */
3161 if (ret != 0)
3162 return ret;
3163
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003164 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3165 return wtp_raw_event(hdev, data, size);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003166 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3167 return m560_raw_event(hdev, data, size);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003168
3169 return 0;
3170}
3171
Harry Cutts4435ff22018-12-05 10:42:27 +10003172static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3173 struct hid_usage *usage, __s32 value)
3174{
3175 /* This function will only be called for scroll events, due to the
3176 * restriction imposed in hidpp_usages.
3177 */
3178 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003179 struct hidpp_scroll_counter *counter;
3180
3181 if (!hidpp)
3182 return 0;
3183
3184 counter = &hidpp->vertical_wheel_counter;
Harry Cutts4435ff22018-12-05 10:42:27 +10003185 /* A scroll event may occur before the multiplier has been retrieved or
3186 * the input device set, or high-res scroll enabling may fail. In such
3187 * cases we must return early (falling back to default behaviour) to
3188 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3189 */
3190 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
Hans de Goede06104302019-04-20 13:22:13 +02003191 || hidpp->input == NULL || counter->wheel_multiplier == 0)
Harry Cutts4435ff22018-12-05 10:42:27 +10003192 return 0;
3193
Hans de Goede06104302019-04-20 13:22:13 +02003194 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
Harry Cutts4435ff22018-12-05 10:42:27 +10003195 return 1;
3196}
3197
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003198static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3199{
3200 static atomic_t battery_no = ATOMIC_INIT(0);
3201 struct power_supply_config cfg = { .drv_data = hidpp };
3202 struct power_supply_desc *desc = &hidpp->battery.desc;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003203 enum power_supply_property *battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003204 struct hidpp_battery *battery;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003205 unsigned int num_battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003206 unsigned long n;
3207 int ret;
3208
3209 if (hidpp->battery.ps)
3210 return 0;
3211
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003212 hidpp->battery.feature_index = 0xff;
3213 hidpp->battery.solar_feature_index = 0xff;
3214
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003215 if (hidpp->protocol_major >= 2) {
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003216 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3217 ret = hidpp_solar_request_battery_event(hidpp);
3218 else
3219 ret = hidpp20_query_battery_info(hidpp);
3220
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003221 if (ret)
3222 return ret;
3223 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3224 } else {
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003225 ret = hidpp10_query_battery_status(hidpp);
3226 if (ret) {
3227 ret = hidpp10_query_battery_mileage(hidpp);
3228 if (ret)
3229 return -ENOENT;
3230 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3231 } else {
3232 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3233 }
3234 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003235 }
3236
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003237 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3238 hidpp_battery_props,
3239 sizeof(hidpp_battery_props),
3240 GFP_KERNEL);
Gustavo A. R. Silva929b60a2017-07-07 00:12:13 -05003241 if (!battery_props)
3242 return -ENOMEM;
3243
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003244 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
3245
3246 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3247 battery_props[num_battery_props++] =
3248 POWER_SUPPLY_PROP_CAPACITY;
3249
3250 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3251 battery_props[num_battery_props++] =
3252 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3253
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003254 battery = &hidpp->battery;
3255
3256 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003257 desc->properties = battery_props;
3258 desc->num_properties = num_battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003259 desc->get_property = hidpp_battery_get_property;
3260 sprintf(battery->name, "hidpp_battery_%ld", n);
3261 desc->name = battery->name;
3262 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3263 desc->use_for_apm = 0;
3264
3265 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3266 &battery->desc,
3267 &cfg);
3268 if (IS_ERR(battery->ps))
3269 return PTR_ERR(battery->ps);
3270
3271 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3272
3273 return ret;
3274}
3275
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003276static void hidpp_overwrite_name(struct hid_device *hdev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003277{
3278 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3279 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003280
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003281 if (hidpp->protocol_major < 2)
Benjamin Tissoiresb4f8ce02017-03-27 16:59:24 +02003282 return;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003283
3284 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003285
Simon Wood7bfd2922015-11-19 16:42:12 -07003286 if (!name) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003287 hid_err(hdev, "unable to retrieve the name of the device");
Simon Wood7bfd2922015-11-19 16:42:12 -07003288 } else {
3289 dbg_hid("HID++: Got name: %s\n", name);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003290 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
Simon Wood7bfd2922015-11-19 16:42:12 -07003291 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003292
3293 kfree(name);
3294}
3295
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003296static int hidpp_input_open(struct input_dev *dev)
3297{
3298 struct hid_device *hid = input_get_drvdata(dev);
3299
3300 return hid_hw_open(hid);
3301}
3302
3303static void hidpp_input_close(struct input_dev *dev)
3304{
3305 struct hid_device *hid = input_get_drvdata(dev);
3306
3307 hid_hw_close(hid);
3308}
3309
3310static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3311{
3312 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003313 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003314
3315 if (!input_dev)
3316 return NULL;
3317
3318 input_set_drvdata(input_dev, hdev);
3319 input_dev->open = hidpp_input_open;
3320 input_dev->close = hidpp_input_close;
3321
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003322 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003323 input_dev->phys = hdev->phys;
3324 input_dev->uniq = hdev->uniq;
3325 input_dev->id.bustype = hdev->bus;
3326 input_dev->id.vendor = hdev->vendor;
3327 input_dev->id.product = hdev->product;
3328 input_dev->id.version = hdev->version;
3329 input_dev->dev.parent = &hdev->dev;
3330
3331 return input_dev;
3332}
3333
3334static void hidpp_connect_event(struct hidpp_device *hidpp)
3335{
3336 struct hid_device *hdev = hidpp->hid_dev;
3337 int ret = 0;
3338 bool connected = atomic_read(&hidpp->connected);
3339 struct input_dev *input;
3340 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003341
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003342 if (!connected) {
3343 if (hidpp->battery.ps) {
3344 hidpp->battery.online = false;
3345 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003346 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003347 power_supply_changed(hidpp->battery.ps);
3348 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003349 return;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003350 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003351
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003352 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3353 ret = wtp_connect(hdev, connected);
3354 if (ret)
3355 return;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003356 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3357 ret = m560_send_config_command(hdev, connected);
3358 if (ret)
3359 return;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003360 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3361 ret = k400_connect(hdev, connected);
3362 if (ret)
3363 return;
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003364 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003365
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003366 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3367 ret = hidpp10_wheel_connect(hidpp);
3368 if (ret)
3369 return;
3370 }
3371
Hans de Goede7457bc12019-04-20 13:22:16 +02003372 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3373 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3374 if (ret)
3375 return;
3376 }
3377
Hans de Goede42bc4f32019-04-20 13:22:17 +02003378 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3379 ret = hidpp10_consumer_keys_connect(hidpp);
3380 if (ret)
3381 return;
3382 }
3383
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003384 /* the device is already connected, we can ask for its name and
3385 * protocol */
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003386 if (!hidpp->protocol_major) {
Hans de Goede090760d2019-03-22 08:41:39 +01003387 ret = hidpp_root_get_protocol_version(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003388 if (ret) {
3389 hid_err(hdev, "Can not get the protocol version.\n");
3390 return;
3391 }
3392 }
3393
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003394 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003395 name = hidpp_get_device_name(hidpp);
Hans de Goede2ddf07f2019-04-20 13:22:07 +02003396 if (name) {
3397 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3398 "%s", name);
3399 kfree(name);
3400 if (!devm_name)
3401 return;
3402
3403 hidpp->name = devm_name;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003404 }
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003405 }
3406
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003407 hidpp_initialize_battery(hidpp);
3408
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003409 /* forward current battery state */
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003410 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3411 hidpp10_enable_battery_reporting(hidpp);
3412 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3413 hidpp10_query_battery_mileage(hidpp);
3414 else
3415 hidpp10_query_battery_status(hidpp);
3416 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003417 hidpp20_query_battery_info(hidpp);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003418 }
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003419 if (hidpp->battery.ps)
3420 power_supply_changed(hidpp->battery.ps);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003421
Harry Cutts4435ff22018-12-05 10:42:27 +10003422 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3423 hi_res_scroll_enable(hidpp);
3424
Benjamin Tissoires29368362017-03-27 16:59:28 +02003425 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3426 /* if the input nodes are already created, we can stop now */
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003427 return;
3428
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003429 input = hidpp_allocate_input(hdev);
3430 if (!input) {
3431 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3432 return;
3433 }
3434
Hans de Goedee54abaf2019-04-20 13:22:09 +02003435 hidpp_populate_input(hidpp, input);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003436
3437 ret = input_register_device(input);
3438 if (ret)
3439 input_free_device(input);
3440
3441 hidpp->delayed_input = input;
3442}
3443
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003444static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3445
3446static struct attribute *sysfs_attrs[] = {
3447 &dev_attr_builtin_power_supply.attr,
3448 NULL
3449};
3450
Arvind Yadav35a33cb2017-08-03 16:59:04 +05303451static const struct attribute_group ps_attribute_group = {
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003452 .attrs = sysfs_attrs
3453};
3454
Hans de Goeded71b18f2019-04-20 13:22:12 +02003455static int hidpp_get_report_length(struct hid_device *hdev, int id)
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003456{
3457 struct hid_report_enum *re;
3458 struct hid_report *report;
3459
Hans de Goeded71b18f2019-04-20 13:22:12 +02003460 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3461 report = re->report_id_hash[id];
3462 if (!report)
3463 return 0;
3464
3465 return report->field[0]->report_count + 1;
3466}
3467
3468static bool hidpp_validate_report(struct hid_device *hdev, int id,
3469 int expected_length, bool optional)
3470{
3471 int report_length;
3472
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003473 if (id >= HID_MAX_IDS || id < 0) {
3474 hid_err(hdev, "invalid HID report id %u\n", id);
3475 return false;
3476 }
3477
Hans de Goeded71b18f2019-04-20 13:22:12 +02003478 report_length = hidpp_get_report_length(hdev, id);
3479 if (!report_length)
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003480 return optional;
3481
Hans de Goeded71b18f2019-04-20 13:22:12 +02003482 if (report_length < expected_length) {
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003483 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3484 return false;
3485 }
3486
3487 return true;
3488}
3489
3490static bool hidpp_validate_device(struct hid_device *hdev)
3491{
3492 return hidpp_validate_report(hdev, REPORT_ID_HIDPP_SHORT,
Hans de Goeded71b18f2019-04-20 13:22:12 +02003493 HIDPP_REPORT_SHORT_LENGTH, false) &&
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003494 hidpp_validate_report(hdev, REPORT_ID_HIDPP_LONG,
Hans de Goeded71b18f2019-04-20 13:22:12 +02003495 HIDPP_REPORT_LONG_LENGTH, true);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003496}
3497
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003498static bool hidpp_application_equals(struct hid_device *hdev,
3499 unsigned int application)
3500{
3501 struct list_head *report_list;
3502 struct hid_report *report;
3503
3504 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
3505 report = list_first_entry_or_null(report_list, struct hid_report, list);
3506 return report && report->application == application;
3507}
3508
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003509static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3510{
3511 struct hidpp_device *hidpp;
3512 int ret;
3513 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003514 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003515
Hans de Goede42bc4f32019-04-20 13:22:17 +02003516 /* report_fixup needs drvdata to be set before we call hid_parse */
3517 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
3518 if (!hidpp)
3519 return -ENOMEM;
3520
3521 hidpp->hid_dev = hdev;
3522 hidpp->name = hdev->name;
3523 hidpp->quirks = id->driver_data;
3524 hid_set_drvdata(hdev, hidpp);
3525
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003526 ret = hid_parse(hdev);
3527 if (ret) {
3528 hid_err(hdev, "%s:parse failed\n", __func__);
3529 return ret;
3530 }
3531
3532 /*
3533 * Make sure the device is HID++ capable, otherwise treat as generic HID
3534 */
Hans de Goede42bc4f32019-04-20 13:22:17 +02003535 if (!hidpp_validate_device(hdev)) {
3536 hid_set_drvdata(hdev, NULL);
3537 devm_kfree(&hdev->dev, hidpp);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003538 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Hans de Goede42bc4f32019-04-20 13:22:17 +02003539 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003540
Hans de Goeded71b18f2019-04-20 13:22:12 +02003541 hidpp->very_long_report_length =
3542 hidpp_get_report_length(hdev, REPORT_ID_HIDPP_VERY_LONG);
3543 if (hidpp->very_long_report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3544 hidpp->very_long_report_length = HIDPP_REPORT_VERY_LONG_MAX_LENGTH;
3545
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003546 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3547 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3548
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003549 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3550 hidpp_application_equals(hdev, HID_GD_MOUSE))
Hans de Goede7457bc12019-04-20 13:22:16 +02003551 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
3552 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003553
Hans de Goede42bc4f32019-04-20 13:22:17 +02003554 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3555 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
3556 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
3557
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003558 if (disable_raw_mode) {
3559 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003560 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003561 }
3562
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003563 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3564 ret = wtp_allocate(hdev, id);
3565 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003566 return ret;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003567 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3568 ret = k400_allocate(hdev);
3569 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003570 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003571 }
3572
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003573 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003574 mutex_init(&hidpp->send_mutex);
3575 init_waitqueue_head(&hidpp->wait);
3576
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003577 /* indicates we are handling the battery properties in the kernel */
3578 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3579 if (ret)
3580 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3581 hdev->name);
3582
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003583 /*
3584 * Plain USB connections need to actually call start and open
3585 * on the transport driver to allow incoming data.
3586 */
3587 ret = hid_hw_start(hdev, 0);
3588 if (ret) {
3589 hid_err(hdev, "hw start failed\n");
3590 goto hid_hw_start_fail;
Simon Wood7bfd2922015-11-19 16:42:12 -07003591 }
3592
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003593 ret = hid_hw_open(hdev);
3594 if (ret < 0) {
3595 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3596 __func__, ret);
3597 hid_hw_stop(hdev);
3598 goto hid_hw_open_fail;
3599 }
Simon Wood7bfd2922015-11-19 16:42:12 -07003600
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003601 /* Allow incoming packets */
3602 hid_device_io_start(hdev);
3603
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003604 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3605 hidpp_unifying_init(hidpp);
3606
Hans de Goede090760d2019-03-22 08:41:39 +01003607 connected = hidpp_root_get_protocol_version(hidpp) == 0;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003608 atomic_set(&hidpp->connected, connected);
3609 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003610 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02003611 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003612 hid_err(hdev, "Device not connected");
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003613 goto hid_hw_init_fail;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003614 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003615
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003616 hidpp_overwrite_name(hdev);
3617 }
Benjamin Tissoires33797822014-09-30 13:18:30 -04003618
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003619 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003620 ret = wtp_get_config(hidpp);
3621 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003622 goto hid_hw_init_fail;
Simon Wood7f4b49f2015-11-19 16:42:13 -07003623 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3624 ret = g920_get_config(hidpp);
3625 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003626 goto hid_hw_init_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003627 }
3628
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003629 hidpp_connect_event(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003630
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003631 /* Reset the HID node state */
3632 hid_device_io_stop(hdev);
3633 hid_hw_close(hdev);
3634 hid_hw_stop(hdev);
3635
3636 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3637 connect_mask &= ~HID_CONNECT_HIDINPUT;
3638
3639 /* Now export the actual inputs and hidraw nodes to the world */
3640 ret = hid_hw_start(hdev, connect_mask);
3641 if (ret) {
3642 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3643 goto hid_hw_start_fail;
3644 }
3645
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003646 return ret;
3647
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003648hid_hw_init_fail:
3649 hid_hw_close(hdev);
3650hid_hw_open_fail:
3651 hid_hw_stop(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003652hid_hw_start_fail:
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003653 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003654 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003655 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003656 return ret;
3657}
3658
3659static void hidpp_remove(struct hid_device *hdev)
3660{
3661 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3662
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003663 if (!hidpp)
3664 return hid_hw_stop(hdev);
3665
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003666 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3667
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003668 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)
Edwin Veldsff21a632016-01-11 00:25:15 +01003669 hidpp_ff_deinit(hdev);
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003670
Simon Wood7bfd2922015-11-19 16:42:12 -07003671 hid_hw_stop(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003672 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003673 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003674}
3675
Harry Cutts4435ff22018-12-05 10:42:27 +10003676#define LDJ_DEVICE(product) \
3677 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3678 USB_VENDOR_ID_LOGITECH, (product))
3679
Hans de Goede754a3082019-04-20 13:22:11 +02003680#define L27MHZ_DEVICE(product) \
3681 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
3682 USB_VENDOR_ID_LOGITECH, (product))
3683
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003684static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04003685 { /* wireless touchpad */
Harry Cutts16767222018-12-05 10:42:28 +10003686 LDJ_DEVICE(0x4011),
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04003687 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3688 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003689 { /* wireless touchpad T650 */
Harry Cutts16767222018-12-05 10:42:28 +10003690 LDJ_DEVICE(0x4101),
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003691 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003692 { /* wireless touchpad T651 */
3693 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3694 USB_DEVICE_ID_LOGITECH_T651),
3695 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Harry Cutts4435ff22018-12-05 10:42:27 +10003696 { /* Mouse Logitech Anywhere MX */
3697 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3698 { /* Mouse Logitech Cube */
3699 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3700 { /* Mouse Logitech M335 */
3701 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3702 { /* Mouse Logitech M515 */
3703 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003704 { /* Mouse logitech M560 */
Harry Cutts4435ff22018-12-05 10:42:27 +10003705 LDJ_DEVICE(0x402d),
3706 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
3707 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3708 { /* Mouse Logitech M705 (firmware RQM17) */
3709 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3710 { /* Mouse Logitech M705 (firmware RQM67) */
3711 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3712 { /* Mouse Logitech M720 */
3713 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3714 { /* Mouse Logitech MX Anywhere 2 */
3715 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3716 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3717 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3718 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3719 { /* Mouse Logitech MX Anywhere 2S */
3720 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3721 { /* Mouse Logitech MX Master */
3722 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3723 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3724 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3725 { /* Mouse Logitech MX Master 2S */
3726 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3727 { /* Mouse Logitech Performance MX */
3728 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003729 { /* Keyboard logitech K400 */
Harry Cutts16767222018-12-05 10:42:28 +10003730 LDJ_DEVICE(0x4024),
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003731 .driver_data = HIDPP_QUIRK_CLASS_K400 },
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003732 { /* Solar Keyboard Logitech K750 */
Harry Cutts16767222018-12-05 10:42:28 +10003733 LDJ_DEVICE(0x4002),
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003734 .driver_data = HIDPP_QUIRK_CLASS_K750 },
Hans de Goede42bc4f32019-04-20 13:22:17 +02003735 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
3736 LDJ_DEVICE(0xb305),
3737 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Hans de Goedeced2c0c2019-04-28 21:25:52 +02003738 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
3739 LDJ_DEVICE(0xb30b),
3740 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003741
Harry Cutts16767222018-12-05 10:42:28 +10003742 { LDJ_DEVICE(HID_ANY_ID) },
Simon Wood7bfd2922015-11-19 16:42:12 -07003743
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003744 { /* Keyboard LX501 (Y-RR53) */
3745 L27MHZ_DEVICE(0x0049),
3746 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
3747 { /* Keyboard MX3000 (Y-RAM74) */
3748 L27MHZ_DEVICE(0x0057),
3749 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
3750 { /* Keyboard MX3200 (Y-RAV80) */
3751 L27MHZ_DEVICE(0x005c),
3752 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
Hans de Goede24098772019-05-12 12:36:02 +02003753 { /* S510 Media Remote */
3754 L27MHZ_DEVICE(0x00fe),
3755 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003756
Hans de Goede754a3082019-04-20 13:22:11 +02003757 { L27MHZ_DEVICE(HID_ANY_ID) },
3758
Filipe Laíns27fc32f2019-07-16 08:37:26 +01003759 { /* Logitech G403 Wireless Gaming Mouse over USB */
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003760 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
Filipe Laíns27fc32f2019-07-16 08:37:26 +01003761 { /* Logitech G703 Gaming Mouse over USB */
3762 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
3763 { /* Logitech G703 Hero Gaming Mouse over USB */
3764 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003765 { /* Logitech G900 Gaming Mouse over USB */
3766 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
Filipe Laíns27fc32f2019-07-16 08:37:26 +01003767 { /* Logitech G903 Gaming Mouse over USB */
3768 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
3769 { /* Logitech G903 Hero Gaming Mouse over USB */
3770 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003771 { /* Logitech G920 Wheel over USB */
3772 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
Simon Wood7bfd2922015-11-19 16:42:12 -07003773 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
Filipe Laíns27fc32f2019-07-16 08:37:26 +01003774 { /* Logitech G Pro Gaming Mouse over USB */
3775 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
Hans de Goede42bc4f32019-04-20 13:22:17 +02003776
3777 { /* MX5000 keyboard over Bluetooth */
3778 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
3779 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Hans de Goedeced2c0c2019-04-28 21:25:52 +02003780 { /* MX5500 keyboard over Bluetooth */
3781 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
3782 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003783 {}
3784};
3785
3786MODULE_DEVICE_TABLE(hid, hidpp_devices);
3787
Harry Cutts4435ff22018-12-05 10:42:27 +10003788static const struct hid_usage_id hidpp_usages[] = {
3789 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
3790 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
3791};
3792
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003793static struct hid_driver hidpp_driver = {
3794 .name = "logitech-hidpp-device",
3795 .id_table = hidpp_devices,
Hans de Goede42bc4f32019-04-20 13:22:17 +02003796 .report_fixup = hidpp_report_fixup,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003797 .probe = hidpp_probe,
3798 .remove = hidpp_remove,
3799 .raw_event = hidpp_raw_event,
Harry Cutts4435ff22018-12-05 10:42:27 +10003800 .usage_table = hidpp_usages,
3801 .event = hidpp_event,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003802 .input_configured = hidpp_input_configured,
3803 .input_mapping = hidpp_input_mapping,
Simon Wood0b1804e2015-11-19 16:42:15 -07003804 .input_mapped = hidpp_input_mapped,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003805};
3806
3807module_hid_driver(hidpp_driver);