blob: ecf1b1e22e1a2624ff613b567338e3b8e8479430 [file] [log] [blame]
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001/*
2 * HIDPP protocol for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/device.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010018#include <linux/input.h>
19#include <linux/usb.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040020#include <linux/hid.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/sched.h>
Harry Cutts4435ff22018-12-05 10:42:27 +100024#include <linux/sched/clock.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040025#include <linux/kfifo.h>
26#include <linux/input/mt.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010027#include <linux/workqueue.h>
28#include <linux/atomic.h>
29#include <linux/fixp-arith.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040030#include <asm/unaligned.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010031#include "usbhid/usbhid.h"
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040032#include "hid-ids.h"
33
34MODULE_LICENSE("GPL");
35MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
36MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
37
Benjamin Tissoires9188dba2015-03-26 12:41:57 -040038static bool disable_raw_mode;
39module_param(disable_raw_mode, bool, 0644);
40MODULE_PARM_DESC(disable_raw_mode,
41 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
42
Benjamin Tissoires90cdd982015-09-03 09:08:30 -040043static bool disable_tap_to_click;
44module_param(disable_tap_to_click, bool, 0644);
45MODULE_PARM_DESC(disable_tap_to_click,
46 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
47
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040048#define REPORT_ID_HIDPP_SHORT 0x10
49#define REPORT_ID_HIDPP_LONG 0x11
Simon Wooda5ce8f52015-11-19 16:42:11 -070050#define REPORT_ID_HIDPP_VERY_LONG 0x12
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040051
52#define HIDPP_REPORT_SHORT_LENGTH 7
53#define HIDPP_REPORT_LONG_LENGTH 20
Hans de Goeded71b18f2019-04-20 13:22:12 +020054#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040055
56#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)
71
72/* Convenience constant to check for any high-res support. */
73#define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
74 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
75 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040076
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100077#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040078
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020079#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
80#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +020081#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
82#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020083
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040084/*
85 * There are two hidpp protocols in use, the first version hidpp10 is known
86 * as register access protocol or RAP, the second version hidpp20 is known as
87 * feature access protocol or FAP
88 *
89 * Most older devices (including the Unifying usb receiver) use the RAP protocol
90 * where as most newer devices use the FAP protocol. Both protocols are
91 * compatible with the underlying transport, which could be usb, Unifiying, or
92 * bluetooth. The message lengths are defined by the hid vendor specific report
93 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
94 * the HIDPP_LONG report type (total message length 20 bytes)
95 *
96 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
97 * messages. The Unifying receiver itself responds to RAP messages (device index
98 * is 0xFF for the receiver), and all messages (short or long) with a device
99 * index between 1 and 6 are passed untouched to the corresponding paired
100 * Unifying device.
101 *
102 * The paired device can be RAP or FAP, it will receive the message untouched
103 * from the Unifiying receiver.
104 */
105
106struct fap {
107 u8 feature_index;
108 u8 funcindex_clientid;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200109 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400110};
111
112struct rap {
113 u8 sub_id;
114 u8 reg_address;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200115 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400116};
117
118struct hidpp_report {
119 u8 report_id;
120 u8 device_index;
121 union {
122 struct fap fap;
123 struct rap rap;
124 u8 rawbytes[sizeof(struct fap)];
125 };
126} __packed;
127
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000128struct hidpp_battery {
129 u8 feature_index;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +0200130 u8 solar_feature_index;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000131 struct power_supply_desc desc;
132 struct power_supply *ps;
133 char name[64];
134 int status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200135 int capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200136 int level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200137 bool online;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000138};
139
Harry Cutts4435ff22018-12-05 10:42:27 +1000140/**
141 * struct hidpp_scroll_counter - Utility class for processing high-resolution
142 * scroll events.
143 * @dev: the input device for which events should be reported.
144 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
145 * @remainder: counts the number of high-resolution units moved since the last
146 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
147 * only be used by class methods.
148 * @direction: direction of last movement (1 or -1)
149 * @last_time: last event time, used to reset remainder after inactivity
150 */
151struct hidpp_scroll_counter {
152 struct input_dev *dev;
153 int wheel_multiplier;
154 int remainder;
155 int direction;
156 unsigned long long last_time;
157};
158
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400159struct hidpp_device {
160 struct hid_device *hid_dev;
161 struct mutex send_mutex;
162 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -0500163 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400164 wait_queue_head_t wait;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200165 int very_long_report_length;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400166 bool answer_available;
167 u8 protocol_major;
168 u8 protocol_minor;
169
170 void *private_data;
171
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400172 struct work_struct work;
173 struct kfifo delayed_work_fifo;
174 atomic_t connected;
175 struct input_dev *delayed_input;
176
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400177 unsigned long quirks;
Benjamin Tissoires206d7c62017-03-27 16:59:25 +0200178 unsigned long capabilities;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400179
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000180 struct hidpp_battery battery;
Harry Cutts4435ff22018-12-05 10:42:27 +1000181 struct hidpp_scroll_counter vertical_wheel_counter;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000182};
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400183
Peter Wuf677bb12014-12-16 01:50:14 +0100184/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400185#define HIDPP_ERROR 0x8f
186#define HIDPP_ERROR_SUCCESS 0x00
187#define HIDPP_ERROR_INVALID_SUBID 0x01
188#define HIDPP_ERROR_INVALID_ADRESS 0x02
189#define HIDPP_ERROR_INVALID_VALUE 0x03
190#define HIDPP_ERROR_CONNECT_FAIL 0x04
191#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
192#define HIDPP_ERROR_ALREADY_EXISTS 0x06
193#define HIDPP_ERROR_BUSY 0x07
194#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
195#define HIDPP_ERROR_RESOURCE_ERROR 0x09
196#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
197#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
198#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100199/* HID++ 2.0 error codes */
200#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400201
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400202static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
203
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400204static int __hidpp_send_report(struct hid_device *hdev,
205 struct hidpp_report *hidpp_report)
206{
Simon Wood7bfd2922015-11-19 16:42:12 -0700207 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400208 int fields_count, ret;
209
210 switch (hidpp_report->report_id) {
211 case REPORT_ID_HIDPP_SHORT:
212 fields_count = HIDPP_REPORT_SHORT_LENGTH;
213 break;
214 case REPORT_ID_HIDPP_LONG:
215 fields_count = HIDPP_REPORT_LONG_LENGTH;
216 break;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700217 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200218 fields_count = hidpp->very_long_report_length;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700219 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400220 default:
221 return -ENODEV;
222 }
223
224 /*
225 * set the device_index as the receiver, it will be overwritten by
226 * hid_hw_request if needed
227 */
228 hidpp_report->device_index = 0xff;
229
Simon Wood7bfd2922015-11-19 16:42:12 -0700230 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
231 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
232 } else {
233 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
234 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
235 HID_REQ_SET_REPORT);
236 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400237
238 return ret == fields_count ? 0 : -1;
239}
240
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500241/**
242 * hidpp_send_message_sync() returns 0 in case of success, and something else
243 * in case of a failure.
244 * - If ' something else' is positive, that means that an error has been raised
245 * by the protocol itself.
246 * - If ' something else' is negative, that means that we had a classic error
247 * (-ENOMEM, -EPIPE, etc...)
248 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400249static int hidpp_send_message_sync(struct hidpp_device *hidpp,
250 struct hidpp_report *message,
251 struct hidpp_report *response)
252{
253 int ret;
254
255 mutex_lock(&hidpp->send_mutex);
256
257 hidpp->send_receive_buf = response;
258 hidpp->answer_available = false;
259
260 /*
261 * So that we can later validate the answer when it arrives
262 * in hidpp_raw_event
263 */
264 *response = *message;
265
266 ret = __hidpp_send_report(hidpp->hid_dev, message);
267
268 if (ret) {
269 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
270 memset(response, 0, sizeof(struct hidpp_report));
271 goto exit;
272 }
273
274 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
275 5*HZ)) {
276 dbg_hid("%s:timeout waiting for response\n", __func__);
277 memset(response, 0, sizeof(struct hidpp_report));
278 ret = -ETIMEDOUT;
279 }
280
281 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100282 response->rap.sub_id == HIDPP_ERROR) {
283 ret = response->rap.params[1];
284 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
285 goto exit;
286 }
287
Simon Wooda5ce8f52015-11-19 16:42:11 -0700288 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
289 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
290 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400291 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100292 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400293 goto exit;
294 }
295
296exit:
297 mutex_unlock(&hidpp->send_mutex);
298 return ret;
299
300}
301
302static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
303 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
304 struct hidpp_report *response)
305{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300306 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400307 int ret;
308
309 if (param_count > sizeof(message->fap.params))
310 return -EINVAL;
311
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300312 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
313 if (!message)
314 return -ENOMEM;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700315
316 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
317 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
318 else
319 message->report_id = REPORT_ID_HIDPP_LONG;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400320 message->fap.feature_index = feat_index;
321 message->fap.funcindex_clientid = funcindex_clientid;
322 memcpy(&message->fap.params, params, param_count);
323
324 ret = hidpp_send_message_sync(hidpp, message, response);
325 kfree(message);
326 return ret;
327}
328
Benjamin Tissoires33797822014-09-30 13:18:30 -0400329static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
330 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
331 struct hidpp_report *response)
332{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300333 struct hidpp_report *message;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700334 int ret, max_count;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400335
Simon Wooda5ce8f52015-11-19 16:42:11 -0700336 switch (report_id) {
337 case REPORT_ID_HIDPP_SHORT:
338 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
339 break;
340 case REPORT_ID_HIDPP_LONG:
341 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
342 break;
343 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200344 max_count = hidpp_dev->very_long_report_length - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700345 break;
346 default:
Benjamin Tissoires33797822014-09-30 13:18:30 -0400347 return -EINVAL;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700348 }
Benjamin Tissoires33797822014-09-30 13:18:30 -0400349
Simon Wooda5ce8f52015-11-19 16:42:11 -0700350 if (param_count > max_count)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400351 return -EINVAL;
352
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300353 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
354 if (!message)
355 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400356 message->report_id = report_id;
357 message->rap.sub_id = sub_id;
358 message->rap.reg_address = reg_address;
359 memcpy(&message->rap.params, params, param_count);
360
361 ret = hidpp_send_message_sync(hidpp_dev, message, response);
362 kfree(message);
363 return ret;
364}
365
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400366static void delayed_work_cb(struct work_struct *work)
367{
368 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
369 work);
370 hidpp_connect_event(hidpp);
371}
372
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400373static inline bool hidpp_match_answer(struct hidpp_report *question,
374 struct hidpp_report *answer)
375{
376 return (answer->fap.feature_index == question->fap.feature_index) &&
377 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
378}
379
380static inline bool hidpp_match_error(struct hidpp_report *question,
381 struct hidpp_report *answer)
382{
Peter Wuf677bb12014-12-16 01:50:14 +0100383 return ((answer->rap.sub_id == HIDPP_ERROR) ||
384 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400385 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
386 (answer->fap.params[0] == question->fap.funcindex_clientid);
387}
388
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400389static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
390{
391 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
392 (report->rap.sub_id == 0x41);
393}
394
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500395/**
396 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
397 */
398static void hidpp_prefix_name(char **name, int name_length)
399{
400#define PREFIX_LENGTH 9 /* "Logitech " */
401
402 int new_length;
403 char *new_name;
404
405 if (name_length > PREFIX_LENGTH &&
406 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
407 /* The prefix has is already in the name */
408 return;
409
410 new_length = PREFIX_LENGTH + name_length;
411 new_name = kzalloc(new_length, GFP_KERNEL);
412 if (!new_name)
413 return;
414
415 snprintf(new_name, new_length, "Logitech %s", *name);
416
417 kfree(*name);
418
419 *name = new_name;
420}
421
Harry Cutts4435ff22018-12-05 10:42:27 +1000422/**
423 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
424 * events given a high-resolution wheel
425 * movement.
426 * @counter: a hid_scroll_counter struct describing the wheel.
427 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
428 * units.
429 *
430 * Given a high-resolution movement, this function converts the movement into
431 * fractions of 120 and emits high-resolution scroll events for the input
432 * device. It also uses the multiplier from &struct hid_scroll_counter to
433 * emit low-resolution scroll events when appropriate for
434 * backwards-compatibility with userspace input libraries.
435 */
436static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter *counter,
437 int hi_res_value)
438{
439 int low_res_value, remainder, direction;
440 unsigned long long now, previous;
441
442 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
443 input_report_rel(counter->dev, REL_WHEEL_HI_RES, hi_res_value);
444
445 remainder = counter->remainder;
446 direction = hi_res_value > 0 ? 1 : -1;
447
448 now = sched_clock();
449 previous = counter->last_time;
450 counter->last_time = now;
451 /*
452 * Reset the remainder after a period of inactivity or when the
453 * direction changes. This prevents the REL_WHEEL emulation point
454 * from sliding for devices that don't always provide the same
455 * number of movements per detent.
456 */
457 if (now - previous > 1000000000 || direction != counter->direction)
458 remainder = 0;
459
460 counter->direction = direction;
461 remainder += hi_res_value;
462
463 /* Some wheels will rest 7/8ths of a detent from the previous detent
464 * after slow movement, so we want the threshold for low-res events to
465 * be in the middle between two detents (e.g. after 4/8ths) as
466 * opposed to on the detents themselves (8/8ths).
467 */
468 if (abs(remainder) >= 60) {
469 /* Add (or subtract) 1 because we want to trigger when the wheel
470 * is half-way to the next detent (i.e. scroll 1 detent after a
471 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
472 * etc.).
473 */
474 low_res_value = remainder / 120;
475 if (low_res_value == 0)
476 low_res_value = (hi_res_value > 0 ? 1 : -1);
477 input_report_rel(counter->dev, REL_WHEEL, low_res_value);
478 remainder -= low_res_value * 120;
479 }
480 counter->remainder = remainder;
481}
482
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400483/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400484/* HIDP++ 1.0 commands */
485/* -------------------------------------------------------------------------- */
486
487#define HIDPP_SET_REGISTER 0x80
488#define HIDPP_GET_REGISTER 0x81
489#define HIDPP_SET_LONG_REGISTER 0x82
490#define HIDPP_GET_LONG_REGISTER 0x83
491
Harry Cutts95c3d002018-12-05 10:42:26 +1000492/**
493 * hidpp10_set_register_bit() - Sets a single bit in a HID++ 1.0 register.
494 * @hidpp_dev: the device to set the register on.
495 * @register_address: the address of the register to modify.
496 * @byte: the byte of the register to modify. Should be less than 3.
497 * Return: 0 if successful, otherwise a negative error code.
498 */
499static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev,
500 u8 register_address, u8 byte, u8 bit)
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200501{
502 struct hidpp_report response;
503 int ret;
504 u8 params[3] = { 0 };
505
506 ret = hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000507 REPORT_ID_HIDPP_SHORT,
508 HIDPP_GET_REGISTER,
509 register_address,
510 NULL, 0, &response);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200511 if (ret)
512 return ret;
513
514 memcpy(params, response.rap.params, 3);
515
Harry Cutts95c3d002018-12-05 10:42:26 +1000516 params[byte] |= BIT(bit);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200517
518 return hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000519 REPORT_ID_HIDPP_SHORT,
520 HIDPP_SET_REGISTER,
521 register_address,
522 params, 3, &response);
523}
524
525
526#define HIDPP_REG_GENERAL 0x00
527
528static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
529{
530 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_GENERAL, 0, 4);
531}
532
533#define HIDPP_REG_FEATURES 0x01
534
535/* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
536static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
537{
538 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_FEATURES, 0, 6);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200539}
540
541#define HIDPP_REG_BATTERY_STATUS 0x07
542
543static int hidpp10_battery_status_map_level(u8 param)
544{
545 int level;
546
547 switch (param) {
548 case 1 ... 2:
549 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
550 break;
551 case 3 ... 4:
552 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
553 break;
554 case 5 ... 6:
555 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
556 break;
557 case 7:
558 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
559 break;
560 default:
561 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
562 }
563
564 return level;
565}
566
567static int hidpp10_battery_status_map_status(u8 param)
568{
569 int status;
570
571 switch (param) {
572 case 0x00:
573 /* discharging (in use) */
574 status = POWER_SUPPLY_STATUS_DISCHARGING;
575 break;
576 case 0x21: /* (standard) charging */
577 case 0x24: /* fast charging */
578 case 0x25: /* slow charging */
579 status = POWER_SUPPLY_STATUS_CHARGING;
580 break;
581 case 0x26: /* topping charge */
582 case 0x22: /* charge complete */
583 status = POWER_SUPPLY_STATUS_FULL;
584 break;
585 case 0x20: /* unknown */
586 status = POWER_SUPPLY_STATUS_UNKNOWN;
587 break;
588 /*
589 * 0x01...0x1F = reserved (not charging)
590 * 0x23 = charging error
591 * 0x27..0xff = reserved
592 */
593 default:
594 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
595 break;
596 }
597
598 return status;
599}
600
601static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
602{
603 struct hidpp_report response;
604 int ret, status;
605
606 ret = hidpp_send_rap_command_sync(hidpp,
607 REPORT_ID_HIDPP_SHORT,
608 HIDPP_GET_REGISTER,
609 HIDPP_REG_BATTERY_STATUS,
610 NULL, 0, &response);
611 if (ret)
612 return ret;
613
614 hidpp->battery.level =
615 hidpp10_battery_status_map_level(response.rap.params[0]);
616 status = hidpp10_battery_status_map_status(response.rap.params[1]);
617 hidpp->battery.status = status;
618 /* the capacity is only available when discharging or full */
619 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
620 status == POWER_SUPPLY_STATUS_FULL;
621
622 return 0;
623}
624
625#define HIDPP_REG_BATTERY_MILEAGE 0x0D
626
627static int hidpp10_battery_mileage_map_status(u8 param)
628{
629 int status;
630
631 switch (param >> 6) {
632 case 0x00:
633 /* discharging (in use) */
634 status = POWER_SUPPLY_STATUS_DISCHARGING;
635 break;
636 case 0x01: /* charging */
637 status = POWER_SUPPLY_STATUS_CHARGING;
638 break;
639 case 0x02: /* charge complete */
640 status = POWER_SUPPLY_STATUS_FULL;
641 break;
642 /*
643 * 0x03 = charging error
644 */
645 default:
646 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
647 break;
648 }
649
650 return status;
651}
652
653static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
654{
655 struct hidpp_report response;
656 int ret, status;
657
658 ret = hidpp_send_rap_command_sync(hidpp,
659 REPORT_ID_HIDPP_SHORT,
660 HIDPP_GET_REGISTER,
661 HIDPP_REG_BATTERY_MILEAGE,
662 NULL, 0, &response);
663 if (ret)
664 return ret;
665
666 hidpp->battery.capacity = response.rap.params[0];
667 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
668 hidpp->battery.status = status;
669 /* the capacity is only available when discharging or full */
670 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
671 status == POWER_SUPPLY_STATUS_FULL;
672
673 return 0;
674}
675
676static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
677{
678 struct hidpp_report *report = (struct hidpp_report *)data;
679 int status, capacity, level;
680 bool changed;
681
682 if (report->report_id != REPORT_ID_HIDPP_SHORT)
683 return 0;
684
685 switch (report->rap.sub_id) {
686 case HIDPP_REG_BATTERY_STATUS:
687 capacity = hidpp->battery.capacity;
688 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
689 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
690 break;
691 case HIDPP_REG_BATTERY_MILEAGE:
692 capacity = report->rap.params[0];
693 level = hidpp->battery.level;
694 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
695 break;
696 default:
697 return 0;
698 }
699
700 changed = capacity != hidpp->battery.capacity ||
701 level != hidpp->battery.level ||
702 status != hidpp->battery.status;
703
704 /* the capacity is only available when discharging or full */
705 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
706 status == POWER_SUPPLY_STATUS_FULL;
707
708 if (changed) {
709 hidpp->battery.level = level;
710 hidpp->battery.status = status;
711 if (hidpp->battery.ps)
712 power_supply_changed(hidpp->battery.ps);
713 }
714
715 return 0;
716}
717
Benjamin Tissoires33797822014-09-30 13:18:30 -0400718#define HIDPP_REG_PAIRING_INFORMATION 0xB5
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200719#define HIDPP_EXTENDED_PAIRING 0x30
720#define HIDPP_DEVICE_NAME 0x40
Benjamin Tissoires33797822014-09-30 13:18:30 -0400721
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200722static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400723{
724 struct hidpp_report response;
725 int ret;
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200726 u8 params[1] = { HIDPP_DEVICE_NAME };
Benjamin Tissoires33797822014-09-30 13:18:30 -0400727 char *name;
728 int len;
729
730 ret = hidpp_send_rap_command_sync(hidpp_dev,
731 REPORT_ID_HIDPP_SHORT,
732 HIDPP_GET_LONG_REGISTER,
733 HIDPP_REG_PAIRING_INFORMATION,
734 params, 1, &response);
735 if (ret)
736 return NULL;
737
738 len = response.rap.params[1];
739
Peter Wu3a034a7a2014-12-11 13:51:19 +0100740 if (2 + len > sizeof(response.rap.params))
741 return NULL;
742
Hans de Goede22bf6bd2019-04-20 13:22:06 +0200743 if (len < 4) /* logitech devices are usually at least Xddd */
744 return NULL;
745
Benjamin Tissoires33797822014-09-30 13:18:30 -0400746 name = kzalloc(len + 1, GFP_KERNEL);
747 if (!name)
748 return NULL;
749
750 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500751
752 /* include the terminating '\0' */
753 hidpp_prefix_name(&name, len + 1);
754
Benjamin Tissoires33797822014-09-30 13:18:30 -0400755 return name;
756}
757
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200758static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
759{
760 struct hidpp_report response;
761 int ret;
762 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
763
764 ret = hidpp_send_rap_command_sync(hidpp,
765 REPORT_ID_HIDPP_SHORT,
766 HIDPP_GET_LONG_REGISTER,
767 HIDPP_REG_PAIRING_INFORMATION,
768 params, 1, &response);
769 if (ret)
770 return ret;
771
772 /*
773 * We don't care about LE or BE, we will output it as a string
774 * with %4phD, so we need to keep the order.
775 */
776 *serial = *((u32 *)&response.rap.params[1]);
777 return 0;
778}
779
780static int hidpp_unifying_init(struct hidpp_device *hidpp)
781{
782 struct hid_device *hdev = hidpp->hid_dev;
783 const char *name;
784 u32 serial;
785 int ret;
786
787 ret = hidpp_unifying_get_serial(hidpp, &serial);
788 if (ret)
789 return ret;
790
791 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
792 hdev->product, &serial);
793 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
794
795 name = hidpp_unifying_get_name(hidpp);
796 if (!name)
797 return -EIO;
798
799 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
800 dbg_hid("HID++ Unifying: Got name: %s\n", name);
801
802 kfree(name);
803 return 0;
804}
805
Benjamin Tissoires33797822014-09-30 13:18:30 -0400806/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400807/* 0x0000: Root */
808/* -------------------------------------------------------------------------- */
809
810#define HIDPP_PAGE_ROOT 0x0000
811#define HIDPP_PAGE_ROOT_IDX 0x00
812
813#define CMD_ROOT_GET_FEATURE 0x01
814#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
815
816static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
817 u8 *feature_index, u8 *feature_type)
818{
819 struct hidpp_report response;
820 int ret;
821 u8 params[2] = { feature >> 8, feature & 0x00FF };
822
823 ret = hidpp_send_fap_command_sync(hidpp,
824 HIDPP_PAGE_ROOT_IDX,
825 CMD_ROOT_GET_FEATURE,
826 params, 2, &response);
827 if (ret)
828 return ret;
829
Benjamin Tissoiresa9525b82017-03-27 16:59:32 +0200830 if (response.fap.params[0] == 0)
831 return -ENOENT;
832
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400833 *feature_index = response.fap.params[0];
834 *feature_type = response.fap.params[1];
835
836 return ret;
837}
838
839static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
840{
Hans de Goede09637752019-04-20 13:22:10 +0200841 const u8 ping_byte = 0x5a;
842 u8 ping_data[3] = { 0, 0, ping_byte };
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400843 struct hidpp_report response;
844 int ret;
845
Hans de Goede09637752019-04-20 13:22:10 +0200846 ret = hidpp_send_rap_command_sync(hidpp,
847 REPORT_ID_HIDPP_SHORT,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400848 HIDPP_PAGE_ROOT_IDX,
849 CMD_ROOT_GET_PROTOCOL_VERSION,
Hans de Goede09637752019-04-20 13:22:10 +0200850 ping_data, sizeof(ping_data), &response);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400851
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500852 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400853 hidpp->protocol_major = 1;
854 hidpp->protocol_minor = 0;
Hans de Goede9576af62019-03-22 08:41:38 +0100855 goto print_version;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400856 }
857
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500858 /* the device might not be connected */
859 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
860 return -EIO;
861
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500862 if (ret > 0) {
863 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
864 __func__, ret);
865 return -EPROTO;
866 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400867 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500868 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400869
Hans de Goede09637752019-04-20 13:22:10 +0200870 if (response.rap.params[2] != ping_byte) {
871 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
872 __func__, response.rap.params[2], ping_byte);
873 return -EPROTO;
874 }
875
876 hidpp->protocol_major = response.rap.params[0];
877 hidpp->protocol_minor = response.rap.params[1];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400878
Hans de Goede9576af62019-03-22 08:41:38 +0100879print_version:
880 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
881 hidpp->protocol_major, hidpp->protocol_minor);
882 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400883}
884
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400885/* -------------------------------------------------------------------------- */
886/* 0x0005: GetDeviceNameType */
887/* -------------------------------------------------------------------------- */
888
889#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
890
891#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
892#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
893#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
894
895static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
896 u8 feature_index, u8 *nameLength)
897{
898 struct hidpp_report response;
899 int ret;
900
901 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
902 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
903
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500904 if (ret > 0) {
905 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
906 __func__, ret);
907 return -EPROTO;
908 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400909 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500910 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400911
912 *nameLength = response.fap.params[0];
913
914 return ret;
915}
916
917static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
918 u8 feature_index, u8 char_index, char *device_name, int len_buf)
919{
920 struct hidpp_report response;
921 int ret, i;
922 int count;
923
924 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
925 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
926 &response);
927
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500928 if (ret > 0) {
929 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
930 __func__, ret);
931 return -EPROTO;
932 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400933 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500934 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400935
Simon Wooda5ce8f52015-11-19 16:42:11 -0700936 switch (response.report_id) {
937 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200938 count = hidpp->very_long_report_length - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700939 break;
940 case REPORT_ID_HIDPP_LONG:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400941 count = HIDPP_REPORT_LONG_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700942 break;
943 case REPORT_ID_HIDPP_SHORT:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400944 count = HIDPP_REPORT_SHORT_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700945 break;
946 default:
947 return -EPROTO;
948 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400949
950 if (len_buf < count)
951 count = len_buf;
952
953 for (i = 0; i < count; i++)
954 device_name[i] = response.fap.params[i];
955
956 return count;
957}
958
Peter Wu02cc0972014-12-11 13:51:17 +0100959static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400960{
961 u8 feature_type;
962 u8 feature_index;
963 u8 __name_length;
964 char *name;
965 unsigned index = 0;
966 int ret;
967
968 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
969 &feature_index, &feature_type);
970 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100971 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400972
973 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
974 &__name_length);
975 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100976 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400977
978 name = kzalloc(__name_length + 1, GFP_KERNEL);
979 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100980 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400981
Peter Wu1430ee72014-12-11 13:51:18 +0100982 while (index < __name_length) {
983 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400984 feature_index, index, name + index,
985 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +0100986 if (ret <= 0) {
987 kfree(name);
988 return NULL;
989 }
990 index += ret;
991 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400992
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500993 /* include the terminating '\0' */
994 hidpp_prefix_name(&name, __name_length + 1);
995
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400996 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400997}
998
999/* -------------------------------------------------------------------------- */
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001000/* 0x1000: Battery level status */
1001/* -------------------------------------------------------------------------- */
1002
1003#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1004
1005#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1006#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1007
1008#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1009
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001010#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1011#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1012#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1013
1014static int hidpp_map_battery_level(int capacity)
1015{
1016 if (capacity < 11)
1017 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
Hans de Goede1f87b0c2019-03-22 08:41:40 +01001018 /*
1019 * The spec says this should be < 31 but some devices report 30
1020 * with brand new batteries and Windows reports 30 as "Good".
1021 */
1022 else if (capacity < 30)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001023 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1024 else if (capacity < 81)
1025 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1026 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1027}
1028
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001029static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001030 int *next_capacity,
1031 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001032{
1033 int status;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001034
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001035 *capacity = data[0];
1036 *next_capacity = data[1];
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001037 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001038
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001039 /* When discharging, we can rely on the device reported capacity.
1040 * For all other states the device reports 0 (unknown).
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001041 */
1042 switch (data[2]) {
1043 case 0: /* discharging (in use) */
1044 status = POWER_SUPPLY_STATUS_DISCHARGING;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001045 *level = hidpp_map_battery_level(*capacity);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001046 break;
1047 case 1: /* recharging */
1048 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001049 break;
1050 case 2: /* charge in final stage */
1051 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001052 break;
1053 case 3: /* charge complete */
1054 status = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001055 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001056 *capacity = 100;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001057 break;
1058 case 4: /* recharging below optimal speed */
1059 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001060 break;
1061 /* 5 = invalid battery type
1062 6 = thermal error
1063 7 = other charging error */
1064 default:
1065 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001066 break;
1067 }
1068
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001069 return status;
1070}
1071
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001072static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1073 u8 feature_index,
1074 int *status,
1075 int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001076 int *next_capacity,
1077 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001078{
1079 struct hidpp_report response;
1080 int ret;
1081 u8 *params = (u8 *)response.fap.params;
1082
1083 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1084 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1085 NULL, 0, &response);
1086 if (ret > 0) {
1087 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1088 __func__, ret);
1089 return -EPROTO;
1090 }
1091 if (ret)
1092 return ret;
1093
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001094 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001095 next_capacity,
1096 level);
1097
1098 return 0;
1099}
1100
1101static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1102 u8 feature_index)
1103{
1104 struct hidpp_report response;
1105 int ret;
1106 u8 *params = (u8 *)response.fap.params;
1107 unsigned int level_count, flags;
1108
1109 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1110 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1111 NULL, 0, &response);
1112 if (ret > 0) {
1113 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1114 __func__, ret);
1115 return -EPROTO;
1116 }
1117 if (ret)
1118 return ret;
1119
1120 level_count = params[0];
1121 flags = params[1];
1122
1123 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1124 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1125 else
1126 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001127
1128 return 0;
1129}
1130
1131static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1132{
1133 u8 feature_type;
1134 int ret;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001135 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001136
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001137 if (hidpp->battery.feature_index == 0xff) {
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001138 ret = hidpp_root_get_feature(hidpp,
1139 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1140 &hidpp->battery.feature_index,
1141 &feature_type);
1142 if (ret)
1143 return ret;
1144 }
1145
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001146 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1147 hidpp->battery.feature_index,
1148 &status, &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001149 &next_capacity, &level);
1150 if (ret)
1151 return ret;
1152
1153 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1154 hidpp->battery.feature_index);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001155 if (ret)
1156 return ret;
1157
1158 hidpp->battery.status = status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001159 hidpp->battery.capacity = capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001160 hidpp->battery.level = level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001161 /* the capacity is only available when discharging or full */
1162 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1163 status == POWER_SUPPLY_STATUS_FULL;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001164
1165 return 0;
1166}
1167
1168static int hidpp20_battery_event(struct hidpp_device *hidpp,
1169 u8 *data, int size)
1170{
1171 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001172 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001173 bool changed;
1174
1175 if (report->fap.feature_index != hidpp->battery.feature_index ||
1176 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1177 return 0;
1178
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001179 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1180 &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001181 &next_capacity,
1182 &level);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001183
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001184 /* the capacity is only available when discharging or full */
1185 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1186 status == POWER_SUPPLY_STATUS_FULL;
1187
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001188 changed = capacity != hidpp->battery.capacity ||
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001189 level != hidpp->battery.level ||
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001190 status != hidpp->battery.status;
1191
1192 if (changed) {
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001193 hidpp->battery.level = level;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001194 hidpp->battery.capacity = capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001195 hidpp->battery.status = status;
1196 if (hidpp->battery.ps)
1197 power_supply_changed(hidpp->battery.ps);
1198 }
1199
1200 return 0;
1201}
1202
1203static enum power_supply_property hidpp_battery_props[] = {
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001204 POWER_SUPPLY_PROP_ONLINE,
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001205 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001206 POWER_SUPPLY_PROP_SCOPE,
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001207 POWER_SUPPLY_PROP_MODEL_NAME,
1208 POWER_SUPPLY_PROP_MANUFACTURER,
1209 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001210 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1211 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001212};
1213
1214static int hidpp_battery_get_property(struct power_supply *psy,
1215 enum power_supply_property psp,
1216 union power_supply_propval *val)
1217{
1218 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1219 int ret = 0;
1220
1221 switch(psp) {
1222 case POWER_SUPPLY_PROP_STATUS:
1223 val->intval = hidpp->battery.status;
1224 break;
1225 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001226 val->intval = hidpp->battery.capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001227 break;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001228 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1229 val->intval = hidpp->battery.level;
1230 break;
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001231 case POWER_SUPPLY_PROP_SCOPE:
1232 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1233 break;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001234 case POWER_SUPPLY_PROP_ONLINE:
1235 val->intval = hidpp->battery.online;
1236 break;
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001237 case POWER_SUPPLY_PROP_MODEL_NAME:
1238 if (!strncmp(hidpp->name, "Logitech ", 9))
1239 val->strval = hidpp->name + 9;
1240 else
1241 val->strval = hidpp->name;
1242 break;
1243 case POWER_SUPPLY_PROP_MANUFACTURER:
1244 val->strval = "Logitech";
1245 break;
1246 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1247 val->strval = hidpp->hid_dev->uniq;
1248 break;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001249 default:
1250 ret = -EINVAL;
1251 break;
1252 }
1253
1254 return ret;
1255}
1256
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001257/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10001258/* 0x2120: Hi-resolution scrolling */
1259/* -------------------------------------------------------------------------- */
1260
1261#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1262
1263#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1264
1265static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1266 bool enabled, u8 *multiplier)
1267{
1268 u8 feature_index;
1269 u8 feature_type;
1270 int ret;
1271 u8 params[1];
1272 struct hidpp_report response;
1273
1274 ret = hidpp_root_get_feature(hidpp,
1275 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1276 &feature_index,
1277 &feature_type);
1278 if (ret)
1279 return ret;
1280
1281 params[0] = enabled ? BIT(0) : 0;
1282 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1283 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1284 params, sizeof(params), &response);
1285 if (ret)
1286 return ret;
1287 *multiplier = response.fap.params[1];
1288 return 0;
1289}
1290
1291/* -------------------------------------------------------------------------- */
1292/* 0x2121: HiRes Wheel */
1293/* -------------------------------------------------------------------------- */
1294
1295#define HIDPP_PAGE_HIRES_WHEEL 0x2121
1296
1297#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1298#define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1299
1300static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1301 u8 *multiplier)
1302{
1303 u8 feature_index;
1304 u8 feature_type;
1305 int ret;
1306 struct hidpp_report response;
1307
1308 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1309 &feature_index, &feature_type);
1310 if (ret)
1311 goto return_default;
1312
1313 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1314 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1315 NULL, 0, &response);
1316 if (ret)
1317 goto return_default;
1318
1319 *multiplier = response.fap.params[0];
1320 return 0;
1321return_default:
1322 hid_warn(hidpp->hid_dev,
1323 "Couldn't get wheel multiplier (error %d)\n", ret);
1324 return ret;
1325}
1326
1327static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1328 bool high_resolution, bool use_hidpp)
1329{
1330 u8 feature_index;
1331 u8 feature_type;
1332 int ret;
1333 u8 params[1];
1334 struct hidpp_report response;
1335
1336 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1337 &feature_index, &feature_type);
1338 if (ret)
1339 return ret;
1340
1341 params[0] = (invert ? BIT(2) : 0) |
1342 (high_resolution ? BIT(1) : 0) |
1343 (use_hidpp ? BIT(0) : 0);
1344
1345 return hidpp_send_fap_command_sync(hidpp, feature_index,
1346 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1347 params, sizeof(params), &response);
1348}
1349
1350/* -------------------------------------------------------------------------- */
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001351/* 0x4301: Solar Keyboard */
1352/* -------------------------------------------------------------------------- */
1353
1354#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1355
1356#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1357
1358#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1359#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1360#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1361
1362static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1363{
1364 struct hidpp_report response;
1365 u8 params[2] = { 1, 1 };
1366 u8 feature_type;
1367 int ret;
1368
1369 if (hidpp->battery.feature_index == 0xff) {
1370 ret = hidpp_root_get_feature(hidpp,
1371 HIDPP_PAGE_SOLAR_KEYBOARD,
1372 &hidpp->battery.solar_feature_index,
1373 &feature_type);
1374 if (ret)
1375 return ret;
1376 }
1377
1378 ret = hidpp_send_fap_command_sync(hidpp,
1379 hidpp->battery.solar_feature_index,
1380 CMD_SOLAR_SET_LIGHT_MEASURE,
1381 params, 2, &response);
1382 if (ret > 0) {
1383 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1384 __func__, ret);
1385 return -EPROTO;
1386 }
1387 if (ret)
1388 return ret;
1389
1390 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1391
1392 return 0;
1393}
1394
1395static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1396 u8 *data, int size)
1397{
1398 struct hidpp_report *report = (struct hidpp_report *)data;
1399 int capacity, lux, status;
1400 u8 function;
1401
1402 function = report->fap.funcindex_clientid;
1403
1404
1405 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1406 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1407 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1408 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1409 return 0;
1410
1411 capacity = report->fap.params[0];
1412
1413 switch (function) {
1414 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1415 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1416 if (lux > 200)
1417 status = POWER_SUPPLY_STATUS_CHARGING;
1418 else
1419 status = POWER_SUPPLY_STATUS_DISCHARGING;
1420 break;
1421 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1422 default:
1423 if (capacity < hidpp->battery.capacity)
1424 status = POWER_SUPPLY_STATUS_DISCHARGING;
1425 else
1426 status = POWER_SUPPLY_STATUS_CHARGING;
1427
1428 }
1429
1430 if (capacity == 100)
1431 status = POWER_SUPPLY_STATUS_FULL;
1432
1433 hidpp->battery.online = true;
1434 if (capacity != hidpp->battery.capacity ||
1435 status != hidpp->battery.status) {
1436 hidpp->battery.capacity = capacity;
1437 hidpp->battery.status = status;
1438 if (hidpp->battery.ps)
1439 power_supply_changed(hidpp->battery.ps);
1440 }
1441
1442 return 0;
1443}
1444
1445/* -------------------------------------------------------------------------- */
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04001446/* 0x6010: Touchpad FW items */
1447/* -------------------------------------------------------------------------- */
1448
1449#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1450
1451#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1452
1453struct hidpp_touchpad_fw_items {
1454 uint8_t presence;
1455 uint8_t desired_state;
1456 uint8_t state;
1457 uint8_t persistent;
1458};
1459
1460/**
1461 * send a set state command to the device by reading the current items->state
1462 * field. items is then filled with the current state.
1463 */
1464static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1465 u8 feature_index,
1466 struct hidpp_touchpad_fw_items *items)
1467{
1468 struct hidpp_report response;
1469 int ret;
1470 u8 *params = (u8 *)response.fap.params;
1471
1472 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1473 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1474
1475 if (ret > 0) {
1476 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1477 __func__, ret);
1478 return -EPROTO;
1479 }
1480 if (ret)
1481 return ret;
1482
1483 items->presence = params[0];
1484 items->desired_state = params[1];
1485 items->state = params[2];
1486 items->persistent = params[3];
1487
1488 return 0;
1489}
1490
1491/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001492/* 0x6100: TouchPadRawXY */
1493/* -------------------------------------------------------------------------- */
1494
1495#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1496
1497#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001498#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1499
1500#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001501
1502#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1503#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1504
1505struct hidpp_touchpad_raw_info {
1506 u16 x_size;
1507 u16 y_size;
1508 u8 z_range;
1509 u8 area_range;
1510 u8 timestamp_unit;
1511 u8 maxcontacts;
1512 u8 origin;
1513 u16 res;
1514};
1515
1516struct hidpp_touchpad_raw_xy_finger {
1517 u8 contact_type;
1518 u8 contact_status;
1519 u16 x;
1520 u16 y;
1521 u8 z;
1522 u8 area;
1523 u8 finger_id;
1524};
1525
1526struct hidpp_touchpad_raw_xy {
1527 u16 timestamp;
1528 struct hidpp_touchpad_raw_xy_finger fingers[2];
1529 u8 spurious_flag;
1530 u8 end_of_frame;
1531 u8 finger_count;
1532 u8 button;
1533};
1534
1535static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1536 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1537{
1538 struct hidpp_report response;
1539 int ret;
1540 u8 *params = (u8 *)response.fap.params;
1541
1542 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1543 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1544
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001545 if (ret > 0) {
1546 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1547 __func__, ret);
1548 return -EPROTO;
1549 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001550 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001551 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001552
1553 raw_info->x_size = get_unaligned_be16(&params[0]);
1554 raw_info->y_size = get_unaligned_be16(&params[2]);
1555 raw_info->z_range = params[4];
1556 raw_info->area_range = params[5];
1557 raw_info->maxcontacts = params[7];
1558 raw_info->origin = params[8];
1559 /* res is given in unit per inch */
1560 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1561
1562 return ret;
1563}
1564
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001565static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1566 u8 feature_index, bool send_raw_reports,
1567 bool sensor_enhanced_settings)
1568{
1569 struct hidpp_report response;
1570
1571 /*
1572 * Params:
1573 * bit 0 - enable raw
1574 * bit 1 - 16bit Z, no area
1575 * bit 2 - enhanced sensitivity
1576 * bit 3 - width, height (4 bits each) instead of area
1577 * bit 4 - send raw + gestures (degrades smoothness)
1578 * remaining bits - reserved
1579 */
1580 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1581
1582 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1583 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1584}
1585
1586static void hidpp_touchpad_touch_event(u8 *data,
1587 struct hidpp_touchpad_raw_xy_finger *finger)
1588{
1589 u8 x_m = data[0] << 2;
1590 u8 y_m = data[2] << 2;
1591
1592 finger->x = x_m << 6 | data[1];
1593 finger->y = y_m << 6 | data[3];
1594
1595 finger->contact_type = data[0] >> 6;
1596 finger->contact_status = data[2] >> 6;
1597
1598 finger->z = data[4];
1599 finger->area = data[5];
1600 finger->finger_id = data[6] >> 4;
1601}
1602
1603static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1604 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1605{
1606 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1607 raw_xy->end_of_frame = data[8] & 0x01;
1608 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1609 raw_xy->finger_count = data[15] & 0x0f;
1610 raw_xy->button = (data[8] >> 2) & 0x01;
1611
1612 if (raw_xy->finger_count) {
1613 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1614 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1615 }
1616}
1617
Edwin Veldsff21a632016-01-11 00:25:15 +01001618/* -------------------------------------------------------------------------- */
1619/* 0x8123: Force feedback support */
1620/* -------------------------------------------------------------------------- */
1621
1622#define HIDPP_FF_GET_INFO 0x01
1623#define HIDPP_FF_RESET_ALL 0x11
1624#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1625#define HIDPP_FF_SET_EFFECT_STATE 0x31
1626#define HIDPP_FF_DESTROY_EFFECT 0x41
1627#define HIDPP_FF_GET_APERTURE 0x51
1628#define HIDPP_FF_SET_APERTURE 0x61
1629#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1630#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1631
1632#define HIDPP_FF_EFFECT_STATE_GET 0x00
1633#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1634#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1635#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1636
1637#define HIDPP_FF_EFFECT_CONSTANT 0x00
1638#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1639#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1640#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1641#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1642#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1643#define HIDPP_FF_EFFECT_SPRING 0x06
1644#define HIDPP_FF_EFFECT_DAMPER 0x07
1645#define HIDPP_FF_EFFECT_FRICTION 0x08
1646#define HIDPP_FF_EFFECT_INERTIA 0x09
1647#define HIDPP_FF_EFFECT_RAMP 0x0A
1648
1649#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1650
1651#define HIDPP_FF_EFFECTID_NONE -1
1652#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1653
1654#define HIDPP_FF_MAX_PARAMS 20
1655#define HIDPP_FF_RESERVED_SLOTS 1
1656
1657struct hidpp_ff_private_data {
1658 struct hidpp_device *hidpp;
1659 u8 feature_index;
1660 u8 version;
1661 u16 gain;
1662 s16 range;
1663 u8 slot_autocenter;
1664 u8 num_effects;
1665 int *effect_ids;
1666 struct workqueue_struct *wq;
1667 atomic_t workqueue_size;
1668};
1669
1670struct hidpp_ff_work_data {
1671 struct work_struct work;
1672 struct hidpp_ff_private_data *data;
1673 int effect_id;
1674 u8 command;
1675 u8 params[HIDPP_FF_MAX_PARAMS];
1676 u8 size;
1677};
1678
Peter Huttererfef33602018-12-05 10:42:25 +10001679static const signed short hidpp_ff_effects[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001680 FF_CONSTANT,
1681 FF_PERIODIC,
1682 FF_SINE,
1683 FF_SQUARE,
1684 FF_SAW_UP,
1685 FF_SAW_DOWN,
1686 FF_TRIANGLE,
1687 FF_SPRING,
1688 FF_DAMPER,
1689 FF_AUTOCENTER,
1690 FF_GAIN,
1691 -1
1692};
1693
Peter Huttererfef33602018-12-05 10:42:25 +10001694static const signed short hidpp_ff_effects_v2[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001695 FF_RAMP,
1696 FF_FRICTION,
1697 FF_INERTIA,
1698 -1
1699};
1700
1701static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1702 HIDPP_FF_EFFECT_SPRING,
1703 HIDPP_FF_EFFECT_FRICTION,
1704 HIDPP_FF_EFFECT_DAMPER,
1705 HIDPP_FF_EFFECT_INERTIA
1706};
1707
1708static const char *HIDPP_FF_CONDITION_NAMES[] = {
1709 "spring",
1710 "friction",
1711 "damper",
1712 "inertia"
1713};
1714
1715
1716static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1717{
1718 int i;
1719
1720 for (i = 0; i < data->num_effects; i++)
1721 if (data->effect_ids[i] == effect_id)
1722 return i+1;
1723
1724 return 0;
1725}
1726
1727static void hidpp_ff_work_handler(struct work_struct *w)
1728{
1729 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1730 struct hidpp_ff_private_data *data = wd->data;
1731 struct hidpp_report response;
1732 u8 slot;
1733 int ret;
1734
1735 /* add slot number if needed */
1736 switch (wd->effect_id) {
1737 case HIDPP_FF_EFFECTID_AUTOCENTER:
1738 wd->params[0] = data->slot_autocenter;
1739 break;
1740 case HIDPP_FF_EFFECTID_NONE:
1741 /* leave slot as zero */
1742 break;
1743 default:
1744 /* find current slot for effect */
1745 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1746 break;
1747 }
1748
1749 /* send command and wait for reply */
1750 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1751 wd->command, wd->params, wd->size, &response);
1752
1753 if (ret) {
1754 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1755 goto out;
1756 }
1757
1758 /* parse return data */
1759 switch (wd->command) {
1760 case HIDPP_FF_DOWNLOAD_EFFECT:
1761 slot = response.fap.params[0];
1762 if (slot > 0 && slot <= data->num_effects) {
1763 if (wd->effect_id >= 0)
1764 /* regular effect uploaded */
1765 data->effect_ids[slot-1] = wd->effect_id;
1766 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1767 /* autocenter spring uploaded */
1768 data->slot_autocenter = slot;
1769 }
1770 break;
1771 case HIDPP_FF_DESTROY_EFFECT:
1772 if (wd->effect_id >= 0)
1773 /* regular effect destroyed */
1774 data->effect_ids[wd->params[0]-1] = -1;
1775 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1776 /* autocenter spring destoyed */
1777 data->slot_autocenter = 0;
1778 break;
1779 case HIDPP_FF_SET_GLOBAL_GAINS:
1780 data->gain = (wd->params[0] << 8) + wd->params[1];
1781 break;
1782 case HIDPP_FF_SET_APERTURE:
1783 data->range = (wd->params[0] << 8) + wd->params[1];
1784 break;
1785 default:
1786 /* no action needed */
1787 break;
1788 }
1789
1790out:
1791 atomic_dec(&data->workqueue_size);
1792 kfree(wd);
1793}
1794
1795static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1796{
1797 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1798 int s;
1799
1800 if (!wd)
1801 return -ENOMEM;
1802
1803 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1804
1805 wd->data = data;
1806 wd->effect_id = effect_id;
1807 wd->command = command;
1808 wd->size = size;
1809 memcpy(wd->params, params, size);
1810
1811 atomic_inc(&data->workqueue_size);
1812 queue_work(data->wq, &wd->work);
1813
1814 /* warn about excessive queue size */
1815 s = atomic_read(&data->workqueue_size);
1816 if (s >= 20 && s % 20 == 0)
1817 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1818
1819 return 0;
1820}
1821
1822static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1823{
1824 struct hidpp_ff_private_data *data = dev->ff->private;
1825 u8 params[20];
1826 u8 size;
1827 int force;
1828
1829 /* set common parameters */
1830 params[2] = effect->replay.length >> 8;
1831 params[3] = effect->replay.length & 255;
1832 params[4] = effect->replay.delay >> 8;
1833 params[5] = effect->replay.delay & 255;
1834
1835 switch (effect->type) {
1836 case FF_CONSTANT:
1837 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1838 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1839 params[6] = force >> 8;
1840 params[7] = force & 255;
1841 params[8] = effect->u.constant.envelope.attack_level >> 7;
1842 params[9] = effect->u.constant.envelope.attack_length >> 8;
1843 params[10] = effect->u.constant.envelope.attack_length & 255;
1844 params[11] = effect->u.constant.envelope.fade_level >> 7;
1845 params[12] = effect->u.constant.envelope.fade_length >> 8;
1846 params[13] = effect->u.constant.envelope.fade_length & 255;
1847 size = 14;
1848 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1849 effect->u.constant.level,
1850 effect->direction, force);
1851 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1852 effect->u.constant.envelope.attack_level,
1853 effect->u.constant.envelope.attack_length,
1854 effect->u.constant.envelope.fade_level,
1855 effect->u.constant.envelope.fade_length);
1856 break;
1857 case FF_PERIODIC:
1858 {
1859 switch (effect->u.periodic.waveform) {
1860 case FF_SINE:
1861 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1862 break;
1863 case FF_SQUARE:
1864 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1865 break;
1866 case FF_SAW_UP:
1867 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1868 break;
1869 case FF_SAW_DOWN:
1870 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1871 break;
1872 case FF_TRIANGLE:
1873 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1874 break;
1875 default:
1876 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1877 return -EINVAL;
1878 }
1879 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1880 params[6] = effect->u.periodic.magnitude >> 8;
1881 params[7] = effect->u.periodic.magnitude & 255;
1882 params[8] = effect->u.periodic.offset >> 8;
1883 params[9] = effect->u.periodic.offset & 255;
1884 params[10] = effect->u.periodic.period >> 8;
1885 params[11] = effect->u.periodic.period & 255;
1886 params[12] = effect->u.periodic.phase >> 8;
1887 params[13] = effect->u.periodic.phase & 255;
1888 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1889 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1890 params[16] = effect->u.periodic.envelope.attack_length & 255;
1891 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1892 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1893 params[19] = effect->u.periodic.envelope.fade_length & 255;
1894 size = 20;
1895 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1896 effect->u.periodic.magnitude, effect->direction,
1897 effect->u.periodic.offset,
1898 effect->u.periodic.period,
1899 effect->u.periodic.phase);
1900 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1901 effect->u.periodic.envelope.attack_level,
1902 effect->u.periodic.envelope.attack_length,
1903 effect->u.periodic.envelope.fade_level,
1904 effect->u.periodic.envelope.fade_length);
1905 break;
1906 }
1907 case FF_RAMP:
1908 params[1] = HIDPP_FF_EFFECT_RAMP;
1909 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1910 params[6] = force >> 8;
1911 params[7] = force & 255;
1912 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1913 params[8] = force >> 8;
1914 params[9] = force & 255;
1915 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1916 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1917 params[12] = effect->u.ramp.envelope.attack_length & 255;
1918 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1919 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1920 params[15] = effect->u.ramp.envelope.fade_length & 255;
1921 size = 16;
1922 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1923 effect->u.ramp.start_level,
1924 effect->u.ramp.end_level,
1925 effect->direction, force);
1926 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1927 effect->u.ramp.envelope.attack_level,
1928 effect->u.ramp.envelope.attack_length,
1929 effect->u.ramp.envelope.fade_level,
1930 effect->u.ramp.envelope.fade_length);
1931 break;
1932 case FF_FRICTION:
1933 case FF_INERTIA:
1934 case FF_SPRING:
1935 case FF_DAMPER:
1936 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1937 params[6] = effect->u.condition[0].left_saturation >> 9;
1938 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1939 params[8] = effect->u.condition[0].left_coeff >> 8;
1940 params[9] = effect->u.condition[0].left_coeff & 255;
1941 params[10] = effect->u.condition[0].deadband >> 9;
1942 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1943 params[12] = effect->u.condition[0].center >> 8;
1944 params[13] = effect->u.condition[0].center & 255;
1945 params[14] = effect->u.condition[0].right_coeff >> 8;
1946 params[15] = effect->u.condition[0].right_coeff & 255;
1947 params[16] = effect->u.condition[0].right_saturation >> 9;
1948 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1949 size = 18;
1950 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1951 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1952 effect->u.condition[0].left_coeff,
1953 effect->u.condition[0].left_saturation,
1954 effect->u.condition[0].right_coeff,
1955 effect->u.condition[0].right_saturation);
1956 dbg_hid(" deadband=%d, center=%d\n",
1957 effect->u.condition[0].deadband,
1958 effect->u.condition[0].center);
1959 break;
1960 default:
1961 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1962 return -EINVAL;
1963 }
1964
1965 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1966}
1967
1968static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1969{
1970 struct hidpp_ff_private_data *data = dev->ff->private;
1971 u8 params[2];
1972
1973 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1974
1975 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1976
1977 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1978}
1979
1980static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1981{
1982 struct hidpp_ff_private_data *data = dev->ff->private;
1983 u8 slot = 0;
1984
1985 dbg_hid("Erasing effect %d.\n", effect_id);
1986
1987 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1988}
1989
1990static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1991{
1992 struct hidpp_ff_private_data *data = dev->ff->private;
1993 u8 params[18];
1994
1995 dbg_hid("Setting autocenter to %d.\n", magnitude);
1996
1997 /* start a standard spring effect */
1998 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1999 /* zero delay and duration */
2000 params[2] = params[3] = params[4] = params[5] = 0;
2001 /* set coeff to 25% of saturation */
2002 params[8] = params[14] = magnitude >> 11;
2003 params[9] = params[15] = (magnitude >> 3) & 255;
2004 params[6] = params[16] = magnitude >> 9;
2005 params[7] = params[17] = (magnitude >> 1) & 255;
2006 /* zero deadband and center */
2007 params[10] = params[11] = params[12] = params[13] = 0;
2008
2009 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2010}
2011
2012static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2013{
2014 struct hidpp_ff_private_data *data = dev->ff->private;
2015 u8 params[4];
2016
2017 dbg_hid("Setting gain to %d.\n", gain);
2018
2019 params[0] = gain >> 8;
2020 params[1] = gain & 255;
2021 params[2] = 0; /* no boost */
2022 params[3] = 0;
2023
2024 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2025}
2026
2027static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2028{
2029 struct hid_device *hid = to_hid_device(dev);
2030 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2031 struct input_dev *idev = hidinput->input;
2032 struct hidpp_ff_private_data *data = idev->ff->private;
2033
2034 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2035}
2036
2037static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2038{
2039 struct hid_device *hid = to_hid_device(dev);
2040 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2041 struct input_dev *idev = hidinput->input;
2042 struct hidpp_ff_private_data *data = idev->ff->private;
2043 u8 params[2];
2044 int range = simple_strtoul(buf, NULL, 10);
2045
2046 range = clamp(range, 180, 900);
2047
2048 params[0] = range >> 8;
2049 params[1] = range & 0x00FF;
2050
2051 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2052
2053 return count;
2054}
2055
2056static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2057
2058static void hidpp_ff_destroy(struct ff_device *ff)
2059{
2060 struct hidpp_ff_private_data *data = ff->private;
2061
2062 kfree(data->effect_ids);
2063}
2064
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01002065static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
Edwin Veldsff21a632016-01-11 00:25:15 +01002066{
2067 struct hid_device *hid = hidpp->hid_dev;
2068 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2069 struct input_dev *dev = hidinput->input;
2070 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2071 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2072 struct ff_device *ff;
2073 struct hidpp_report response;
2074 struct hidpp_ff_private_data *data;
2075 int error, j, num_slots;
2076 u8 version;
2077
2078 if (!dev) {
2079 hid_err(hid, "Struct input_dev not set!\n");
2080 return -EINVAL;
2081 }
2082
2083 /* Get firmware release */
2084 version = bcdDevice & 255;
2085
2086 /* Set supported force feedback capabilities */
Peter Huttererfef33602018-12-05 10:42:25 +10002087 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2088 set_bit(hidpp_ff_effects[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002089 if (version > 1)
Peter Huttererfef33602018-12-05 10:42:25 +10002090 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2091 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002092
2093 /* Read number of slots available in device */
2094 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2095 HIDPP_FF_GET_INFO, NULL, 0, &response);
2096 if (error) {
2097 if (error < 0)
2098 return error;
2099 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2100 __func__, error);
2101 return -EPROTO;
2102 }
2103
2104 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2105
2106 error = input_ff_create(dev, num_slots);
2107
2108 if (error) {
2109 hid_err(dev, "Failed to create FF device!\n");
2110 return error;
2111 }
2112
2113 data = kzalloc(sizeof(*data), GFP_KERNEL);
2114 if (!data)
2115 return -ENOMEM;
2116 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2117 if (!data->effect_ids) {
2118 kfree(data);
2119 return -ENOMEM;
2120 }
Kangjie Lu6c44b152019-03-14 00:24:02 -05002121 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2122 if (!data->wq) {
2123 kfree(data->effect_ids);
2124 kfree(data);
2125 return -ENOMEM;
2126 }
2127
Edwin Veldsff21a632016-01-11 00:25:15 +01002128 data->hidpp = hidpp;
2129 data->feature_index = feature_index;
2130 data->version = version;
2131 data->slot_autocenter = 0;
2132 data->num_effects = num_slots;
2133 for (j = 0; j < num_slots; j++)
2134 data->effect_ids[j] = -1;
2135
2136 ff = dev->ff;
2137 ff->private = data;
2138
2139 ff->upload = hidpp_ff_upload_effect;
2140 ff->erase = hidpp_ff_erase_effect;
2141 ff->playback = hidpp_ff_playback;
2142 ff->set_gain = hidpp_ff_set_gain;
2143 ff->set_autocenter = hidpp_ff_set_autocenter;
2144 ff->destroy = hidpp_ff_destroy;
2145
2146
2147 /* reset all forces */
2148 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2149 HIDPP_FF_RESET_ALL, NULL, 0, &response);
2150
2151 /* Read current Range */
2152 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2153 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
2154 if (error)
2155 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
2156 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
2157
2158 /* Create sysfs interface */
2159 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2160 if (error)
2161 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2162
2163 /* Read the current gain values */
2164 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2165 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
2166 if (error)
2167 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
2168 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
2169 /* ignore boost value at response.fap.params[2] */
2170
2171 /* init the hardware command queue */
Edwin Veldsff21a632016-01-11 00:25:15 +01002172 atomic_set(&data->workqueue_size, 0);
2173
2174 /* initialize with zero autocenter to get wheel in usable state */
2175 hidpp_ff_set_autocenter(dev, 0);
2176
Colin Ian Kingdf47b242017-07-14 14:37:12 +01002177 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2178 version);
Edwin Veldsff21a632016-01-11 00:25:15 +01002179
2180 return 0;
2181}
2182
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01002183static int hidpp_ff_deinit(struct hid_device *hid)
Edwin Veldsff21a632016-01-11 00:25:15 +01002184{
2185 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2186 struct input_dev *dev = hidinput->input;
2187 struct hidpp_ff_private_data *data;
2188
2189 if (!dev) {
2190 hid_err(hid, "Struct input_dev not found!\n");
2191 return -EINVAL;
2192 }
2193
2194 hid_info(hid, "Unloading HID++ force feedback.\n");
2195 data = dev->ff->private;
2196 if (!data) {
2197 hid_err(hid, "Private data not found!\n");
2198 return -EINVAL;
2199 }
2200
2201 destroy_workqueue(data->wq);
2202 device_remove_file(&hid->dev, &dev_attr_range);
2203
2204 return 0;
2205}
2206
2207
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002208/* ************************************************************************** */
2209/* */
2210/* Device Support */
2211/* */
2212/* ************************************************************************** */
2213
2214/* -------------------------------------------------------------------------- */
2215/* Touchpad HID++ devices */
2216/* -------------------------------------------------------------------------- */
2217
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002218#define WTP_MANUAL_RESOLUTION 39
2219
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002220struct wtp_data {
2221 struct input_dev *input;
2222 u16 x_size, y_size;
2223 u8 finger_count;
2224 u8 mt_feature_index;
2225 u8 button_feature_index;
2226 u8 maxcontacts;
2227 bool flip_y;
2228 unsigned int resolution;
2229};
2230
2231static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2232 struct hid_field *field, struct hid_usage *usage,
2233 unsigned long **bit, int *max)
2234{
2235 return -1;
2236}
2237
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002238static void wtp_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02002239 struct input_dev *input_dev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002240{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002241 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002242
2243 __set_bit(EV_ABS, input_dev->evbit);
2244 __set_bit(EV_KEY, input_dev->evbit);
2245 __clear_bit(EV_REL, input_dev->evbit);
2246 __clear_bit(EV_LED, input_dev->evbit);
2247
2248 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2249 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2250 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2251 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2252
2253 /* Max pressure is not given by the devices, pick one */
2254 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2255
2256 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2257
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002258 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2259 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2260 else
2261 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002262
2263 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2264 INPUT_MT_DROP_UNUSED);
2265
2266 wd->input = input_dev;
2267}
2268
2269static void wtp_touch_event(struct wtp_data *wd,
2270 struct hidpp_touchpad_raw_xy_finger *touch_report)
2271{
2272 int slot;
2273
2274 if (!touch_report->finger_id || touch_report->contact_type)
2275 /* no actual data */
2276 return;
2277
2278 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2279
2280 input_mt_slot(wd->input, slot);
2281 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2282 touch_report->contact_status);
2283 if (touch_report->contact_status) {
2284 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2285 touch_report->x);
2286 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2287 wd->flip_y ? wd->y_size - touch_report->y :
2288 touch_report->y);
2289 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2290 touch_report->area);
2291 }
2292}
2293
2294static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2295 struct hidpp_touchpad_raw_xy *raw)
2296{
2297 struct wtp_data *wd = hidpp->private_data;
2298 int i;
2299
2300 for (i = 0; i < 2; i++)
2301 wtp_touch_event(wd, &(raw->fingers[i]));
2302
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002303 if (raw->end_of_frame &&
2304 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002305 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2306
2307 if (raw->end_of_frame || raw->finger_count <= 2) {
2308 input_mt_sync_frame(wd->input);
2309 input_sync(wd->input);
2310 }
2311}
2312
2313static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2314{
2315 struct wtp_data *wd = hidpp->private_data;
2316 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2317 (data[7] >> 4) * (data[7] >> 4)) / 2;
2318 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2319 (data[13] >> 4) * (data[13] >> 4)) / 2;
2320 struct hidpp_touchpad_raw_xy raw = {
2321 .timestamp = data[1],
2322 .fingers = {
2323 {
2324 .contact_type = 0,
2325 .contact_status = !!data[7],
2326 .x = get_unaligned_le16(&data[3]),
2327 .y = get_unaligned_le16(&data[5]),
2328 .z = c1_area,
2329 .area = c1_area,
2330 .finger_id = data[2],
2331 }, {
2332 .contact_type = 0,
2333 .contact_status = !!data[13],
2334 .x = get_unaligned_le16(&data[9]),
2335 .y = get_unaligned_le16(&data[11]),
2336 .z = c2_area,
2337 .area = c2_area,
2338 .finger_id = data[8],
2339 }
2340 },
2341 .finger_count = wd->maxcontacts,
2342 .spurious_flag = 0,
2343 .end_of_frame = (data[0] >> 7) == 0,
2344 .button = data[0] & 0x01,
2345 };
2346
2347 wtp_send_raw_xy_event(hidpp, &raw);
2348
2349 return 1;
2350}
2351
2352static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2353{
2354 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2355 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002356 struct hidpp_report *report = (struct hidpp_report *)data;
2357 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002358
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002359 if (!wd || !wd->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002360 return 1;
2361
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002362 switch (data[0]) {
2363 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +01002364 if (size < 2) {
2365 hid_err(hdev, "Received HID report of bad size (%d)",
2366 size);
2367 return 1;
2368 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002369 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2370 input_event(wd->input, EV_KEY, BTN_LEFT,
2371 !!(data[1] & 0x01));
2372 input_event(wd->input, EV_KEY, BTN_RIGHT,
2373 !!(data[1] & 0x02));
2374 input_sync(wd->input);
Peter Wu8abd8202014-12-16 01:50:16 +01002375 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002376 } else {
2377 if (size < 21)
2378 return 1;
2379 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2380 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002381 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +01002382 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002383 if ((report->fap.feature_index != wd->mt_feature_index) ||
2384 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2385 return 1;
2386 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2387
2388 wtp_send_raw_xy_event(hidpp, &raw);
2389 return 0;
2390 }
2391
2392 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002393}
2394
2395static int wtp_get_config(struct hidpp_device *hidpp)
2396{
2397 struct wtp_data *wd = hidpp->private_data;
2398 struct hidpp_touchpad_raw_info raw_info = {0};
2399 u8 feature_type;
2400 int ret;
2401
2402 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2403 &wd->mt_feature_index, &feature_type);
2404 if (ret)
2405 /* means that the device is not powered up */
2406 return ret;
2407
2408 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2409 &raw_info);
2410 if (ret)
2411 return ret;
2412
2413 wd->x_size = raw_info.x_size;
2414 wd->y_size = raw_info.y_size;
2415 wd->maxcontacts = raw_info.maxcontacts;
2416 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2417 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002418 if (!wd->resolution)
2419 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002420
2421 return 0;
2422}
2423
2424static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2425{
2426 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2427 struct wtp_data *wd;
2428
2429 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2430 GFP_KERNEL);
2431 if (!wd)
2432 return -ENOMEM;
2433
2434 hidpp->private_data = wd;
2435
2436 return 0;
2437};
2438
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002439static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002440{
2441 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2442 struct wtp_data *wd = hidpp->private_data;
2443 int ret;
2444
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002445 if (!wd->x_size) {
2446 ret = wtp_get_config(hidpp);
2447 if (ret) {
2448 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002449 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002450 }
2451 }
2452
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002453 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002454 true, true);
2455}
2456
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002457/* ------------------------------------------------------------------------- */
2458/* Logitech M560 devices */
2459/* ------------------------------------------------------------------------- */
2460
2461/*
2462 * Logitech M560 protocol overview
2463 *
2464 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2465 * the sides buttons are pressed, it sends some keyboard keys events
2466 * instead of buttons ones.
2467 * To complicate things further, the middle button keys sequence
2468 * is different from the odd press and the even press.
2469 *
2470 * forward button -> Super_R
2471 * backward button -> Super_L+'d' (press only)
2472 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2473 * 2nd time: left-click (press only)
2474 * NB: press-only means that when the button is pressed, the
2475 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2476 * together sequentially; instead when the button is released, no event is
2477 * generated !
2478 *
2479 * With the command
2480 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2481 * the mouse reacts differently:
2482 * - it never sends a keyboard key event
2483 * - for the three mouse button it sends:
2484 * middle button press 11<xx>0a 3500af00...
2485 * side 1 button (forward) press 11<xx>0a 3500b000...
2486 * side 2 button (backward) press 11<xx>0a 3500ae00...
2487 * middle/side1/side2 button release 11<xx>0a 35000000...
2488 */
2489
2490static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2491
2492struct m560_private_data {
2493 struct input_dev *input;
2494};
2495
2496/* how buttons are mapped in the report */
2497#define M560_MOUSE_BTN_LEFT 0x01
2498#define M560_MOUSE_BTN_RIGHT 0x02
2499#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2500#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2501
2502#define M560_SUB_ID 0x0a
2503#define M560_BUTTON_MODE_REGISTER 0x35
2504
2505static int m560_send_config_command(struct hid_device *hdev, bool connected)
2506{
2507 struct hidpp_report response;
2508 struct hidpp_device *hidpp_dev;
2509
2510 hidpp_dev = hid_get_drvdata(hdev);
2511
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002512 return hidpp_send_rap_command_sync(
2513 hidpp_dev,
2514 REPORT_ID_HIDPP_SHORT,
2515 M560_SUB_ID,
2516 M560_BUTTON_MODE_REGISTER,
2517 (u8 *)m560_config_parameter,
2518 sizeof(m560_config_parameter),
2519 &response
2520 );
2521}
2522
2523static int m560_allocate(struct hid_device *hdev)
2524{
2525 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2526 struct m560_private_data *d;
2527
2528 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2529 GFP_KERNEL);
2530 if (!d)
2531 return -ENOMEM;
2532
2533 hidpp->private_data = d;
2534
2535 return 0;
2536};
2537
2538static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2539{
2540 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2541 struct m560_private_data *mydata = hidpp->private_data;
2542
2543 /* sanity check */
2544 if (!mydata || !mydata->input) {
2545 hid_err(hdev, "error in parameter\n");
2546 return -EINVAL;
2547 }
2548
2549 if (size < 7) {
2550 hid_err(hdev, "error in report\n");
2551 return 0;
2552 }
2553
2554 if (data[0] == REPORT_ID_HIDPP_LONG &&
2555 data[2] == M560_SUB_ID && data[6] == 0x00) {
2556 /*
2557 * m560 mouse report for middle, forward and backward button
2558 *
2559 * data[0] = 0x11
2560 * data[1] = device-id
2561 * data[2] = 0x0a
2562 * data[5] = 0xaf -> middle
2563 * 0xb0 -> forward
2564 * 0xae -> backward
2565 * 0x00 -> release all
2566 * data[6] = 0x00
2567 */
2568
2569 switch (data[5]) {
2570 case 0xaf:
2571 input_report_key(mydata->input, BTN_MIDDLE, 1);
2572 break;
2573 case 0xb0:
2574 input_report_key(mydata->input, BTN_FORWARD, 1);
2575 break;
2576 case 0xae:
2577 input_report_key(mydata->input, BTN_BACK, 1);
2578 break;
2579 case 0x00:
2580 input_report_key(mydata->input, BTN_BACK, 0);
2581 input_report_key(mydata->input, BTN_FORWARD, 0);
2582 input_report_key(mydata->input, BTN_MIDDLE, 0);
2583 break;
2584 default:
2585 hid_err(hdev, "error in report\n");
2586 return 0;
2587 }
2588 input_sync(mydata->input);
2589
2590 } else if (data[0] == 0x02) {
2591 /*
2592 * Logitech M560 mouse report
2593 *
2594 * data[0] = type (0x02)
2595 * data[1..2] = buttons
2596 * data[3..5] = xy
2597 * data[6] = wheel
2598 */
2599
2600 int v;
2601
2602 input_report_key(mydata->input, BTN_LEFT,
2603 !!(data[1] & M560_MOUSE_BTN_LEFT));
2604 input_report_key(mydata->input, BTN_RIGHT,
2605 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2606
Harry Cutts4435ff22018-12-05 10:42:27 +10002607 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002608 input_report_rel(mydata->input, REL_HWHEEL, -1);
Harry Cutts4435ff22018-12-05 10:42:27 +10002609 input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2610 -120);
2611 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002612 input_report_rel(mydata->input, REL_HWHEEL, 1);
Harry Cutts4435ff22018-12-05 10:42:27 +10002613 input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2614 120);
2615 }
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002616
2617 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2618 input_report_rel(mydata->input, REL_X, v);
2619
2620 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2621 input_report_rel(mydata->input, REL_Y, v);
2622
2623 v = hid_snto32(data[6], 8);
Peter Huttererfd35759c2019-03-20 08:48:23 +10002624 if (v != 0)
2625 hidpp_scroll_counter_handle_scroll(
2626 &hidpp->vertical_wheel_counter, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002627
2628 input_sync(mydata->input);
2629 }
2630
2631 return 1;
2632}
2633
2634static void m560_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02002635 struct input_dev *input_dev)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002636{
2637 struct m560_private_data *mydata = hidpp->private_data;
2638
2639 mydata->input = input_dev;
2640
2641 __set_bit(EV_KEY, mydata->input->evbit);
2642 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2643 __set_bit(BTN_RIGHT, mydata->input->keybit);
2644 __set_bit(BTN_LEFT, mydata->input->keybit);
2645 __set_bit(BTN_BACK, mydata->input->keybit);
2646 __set_bit(BTN_FORWARD, mydata->input->keybit);
2647
2648 __set_bit(EV_REL, mydata->input->evbit);
2649 __set_bit(REL_X, mydata->input->relbit);
2650 __set_bit(REL_Y, mydata->input->relbit);
2651 __set_bit(REL_WHEEL, mydata->input->relbit);
2652 __set_bit(REL_HWHEEL, mydata->input->relbit);
Harry Cutts4435ff22018-12-05 10:42:27 +10002653 __set_bit(REL_WHEEL_HI_RES, mydata->input->relbit);
2654 __set_bit(REL_HWHEEL_HI_RES, mydata->input->relbit);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002655}
2656
2657static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2658 struct hid_field *field, struct hid_usage *usage,
2659 unsigned long **bit, int *max)
2660{
2661 return -1;
2662}
2663
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002664/* ------------------------------------------------------------------------- */
2665/* Logitech K400 devices */
2666/* ------------------------------------------------------------------------- */
2667
2668/*
2669 * The Logitech K400 keyboard has an embedded touchpad which is seen
2670 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2671 * tap-to-click but the setting is not remembered accross reset, annoying some
2672 * users.
2673 *
2674 * We can toggle this feature from the host by using the feature 0x6010:
2675 * Touchpad FW items
2676 */
2677
2678struct k400_private_data {
2679 u8 feature_index;
2680};
2681
2682static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2683{
2684 struct k400_private_data *k400 = hidpp->private_data;
2685 struct hidpp_touchpad_fw_items items = {};
2686 int ret;
2687 u8 feature_type;
2688
2689 if (!k400->feature_index) {
2690 ret = hidpp_root_get_feature(hidpp,
2691 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2692 &k400->feature_index, &feature_type);
2693 if (ret)
2694 /* means that the device is not powered up */
2695 return ret;
2696 }
2697
2698 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2699 if (ret)
2700 return ret;
2701
2702 return 0;
2703}
2704
2705static int k400_allocate(struct hid_device *hdev)
2706{
2707 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2708 struct k400_private_data *k400;
2709
2710 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2711 GFP_KERNEL);
2712 if (!k400)
2713 return -ENOMEM;
2714
2715 hidpp->private_data = k400;
2716
2717 return 0;
2718};
2719
2720static int k400_connect(struct hid_device *hdev, bool connected)
2721{
2722 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2723
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002724 if (!disable_tap_to_click)
2725 return 0;
2726
2727 return k400_disable_tap_to_click(hidpp);
2728}
2729
Simon Wood7f4b49f2015-11-19 16:42:13 -07002730/* ------------------------------------------------------------------------- */
2731/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2732/* ------------------------------------------------------------------------- */
2733
2734#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2735
Simon Wood7f4b49f2015-11-19 16:42:13 -07002736static int g920_get_config(struct hidpp_device *hidpp)
2737{
Simon Wood7f4b49f2015-11-19 16:42:13 -07002738 u8 feature_type;
2739 u8 feature_index;
2740 int ret;
2741
Simon Wood7f4b49f2015-11-19 16:42:13 -07002742 /* Find feature and store for later use */
2743 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2744 &feature_index, &feature_type);
2745 if (ret)
2746 return ret;
2747
Edwin Veldsff21a632016-01-11 00:25:15 +01002748 ret = hidpp_ff_init(hidpp, feature_index);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002749 if (ret)
Edwin Veldsff21a632016-01-11 00:25:15 +01002750 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2751 ret);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002752
2753 return 0;
2754}
2755
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002756/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10002757/* High-resolution scroll wheels */
2758/* -------------------------------------------------------------------------- */
2759
2760static int hi_res_scroll_enable(struct hidpp_device *hidpp)
2761{
2762 int ret;
2763 u8 multiplier = 1;
2764
2765 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
2766 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
2767 if (ret == 0)
2768 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
2769 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
2770 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
2771 &multiplier);
2772 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
2773 ret = hidpp10_enable_scrolling_acceleration(hidpp);
2774 multiplier = 8;
2775 }
2776 if (ret)
2777 return ret;
2778
2779 if (multiplier == 0)
2780 multiplier = 1;
2781
2782 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
2783 hid_info(hidpp->hid_dev, "multiplier = %d\n", multiplier);
2784 return 0;
2785}
2786
2787/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002788/* Generic HID++ devices */
2789/* -------------------------------------------------------------------------- */
2790
2791static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2792 struct hid_field *field, struct hid_usage *usage,
2793 unsigned long **bit, int *max)
2794{
2795 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2796
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002797 if (!hidpp)
2798 return 0;
2799
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002800 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2801 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002802 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2803 field->application != HID_GD_MOUSE)
2804 return m560_input_mapping(hdev, hi, field, usage, bit, max);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002805
2806 return 0;
2807}
2808
Simon Wood0b1804e2015-11-19 16:42:15 -07002809static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2810 struct hid_field *field, struct hid_usage *usage,
2811 unsigned long **bit, int *max)
2812{
2813 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2814
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002815 if (!hidpp)
2816 return 0;
2817
Simon Wood0b1804e2015-11-19 16:42:15 -07002818 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2819 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2820 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2821 usage->code == ABS_Y || usage->code == ABS_Z ||
2822 usage->code == ABS_RZ)) {
2823 field->application = HID_GD_MULTIAXIS;
2824 }
2825 }
2826
2827 return 0;
2828}
2829
2830
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002831static void hidpp_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02002832 struct input_dev *input)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002833{
2834 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
Hans de Goedee54abaf2019-04-20 13:22:09 +02002835 wtp_populate_input(hidpp, input);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002836 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
Hans de Goedee54abaf2019-04-20 13:22:09 +02002837 m560_populate_input(hidpp, input);
Harry Cutts4435ff22018-12-05 10:42:27 +10002838
2839 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
2840 hidpp->vertical_wheel_counter.dev = input;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002841}
2842
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002843static int hidpp_input_configured(struct hid_device *hdev,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002844 struct hid_input *hidinput)
2845{
2846 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002847 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002848
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002849 if (!hidpp)
2850 return 0;
2851
Hans de Goedee54abaf2019-04-20 13:22:09 +02002852 hidpp_populate_input(hidpp, input);
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002853
2854 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002855}
2856
2857static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2858 int size)
2859{
2860 struct hidpp_report *question = hidpp->send_receive_buf;
2861 struct hidpp_report *answer = hidpp->send_receive_buf;
2862 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002863 int ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002864
2865 /*
2866 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +01002867 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002868 */
2869 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2870 /*
2871 * Check for a correct hidpp20 answer or the corresponding
2872 * error
2873 */
2874 if (hidpp_match_answer(question, report) ||
2875 hidpp_match_error(question, report)) {
2876 *answer = *report;
2877 hidpp->answer_available = true;
2878 wake_up(&hidpp->wait);
2879 /*
2880 * This was an answer to a command that this driver sent
2881 * We return 1 to hid-core to avoid forwarding the
2882 * command upstream as it has been treated by the driver
2883 */
2884
2885 return 1;
2886 }
2887 }
2888
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002889 if (unlikely(hidpp_report_is_connect_event(report))) {
2890 atomic_set(&hidpp->connected,
2891 !(report->rap.params[0] & (1 << 6)));
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002892 if (schedule_work(&hidpp->work) == 0)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002893 dbg_hid("%s: connect event already queued\n", __func__);
2894 return 1;
2895 }
2896
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002897 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2898 ret = hidpp20_battery_event(hidpp, data, size);
2899 if (ret != 0)
2900 return ret;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02002901 ret = hidpp_solar_battery_event(hidpp, data, size);
2902 if (ret != 0)
2903 return ret;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002904 }
2905
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02002906 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2907 ret = hidpp10_battery_event(hidpp, data, size);
2908 if (ret != 0)
2909 return ret;
2910 }
2911
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002912 return 0;
2913}
2914
2915static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2916 u8 *data, int size)
2917{
2918 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01002919 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002920
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002921 if (!hidpp)
2922 return 0;
2923
Peter Wue529fea2014-12-17 00:23:51 +01002924 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002925 switch (data[0]) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07002926 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +02002927 if (size != hidpp->very_long_report_length) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07002928 hid_err(hdev, "received hid++ report of bad size (%d)",
2929 size);
2930 return 1;
2931 }
2932 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2933 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002934 case REPORT_ID_HIDPP_LONG:
2935 if (size != HIDPP_REPORT_LONG_LENGTH) {
2936 hid_err(hdev, "received hid++ report of bad size (%d)",
2937 size);
2938 return 1;
2939 }
Peter Wue529fea2014-12-17 00:23:51 +01002940 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2941 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002942 case REPORT_ID_HIDPP_SHORT:
2943 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2944 hid_err(hdev, "received hid++ report of bad size (%d)",
2945 size);
2946 return 1;
2947 }
Peter Wue529fea2014-12-17 00:23:51 +01002948 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2949 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002950 }
2951
Peter Wue529fea2014-12-17 00:23:51 +01002952 /* If no report is available for further processing, skip calling
2953 * raw_event of subclasses. */
2954 if (ret != 0)
2955 return ret;
2956
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002957 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2958 return wtp_raw_event(hdev, data, size);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002959 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2960 return m560_raw_event(hdev, data, size);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002961
2962 return 0;
2963}
2964
Harry Cutts4435ff22018-12-05 10:42:27 +10002965static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
2966 struct hid_usage *usage, __s32 value)
2967{
2968 /* This function will only be called for scroll events, due to the
2969 * restriction imposed in hidpp_usages.
2970 */
2971 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002972 struct hidpp_scroll_counter *counter;
2973
2974 if (!hidpp)
2975 return 0;
2976
2977 counter = &hidpp->vertical_wheel_counter;
Harry Cutts4435ff22018-12-05 10:42:27 +10002978 /* A scroll event may occur before the multiplier has been retrieved or
2979 * the input device set, or high-res scroll enabling may fail. In such
2980 * cases we must return early (falling back to default behaviour) to
2981 * avoid a crash in hidpp_scroll_counter_handle_scroll.
2982 */
2983 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
2984 || counter->dev == NULL || counter->wheel_multiplier == 0)
2985 return 0;
2986
2987 hidpp_scroll_counter_handle_scroll(counter, value);
2988 return 1;
2989}
2990
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02002991static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2992{
2993 static atomic_t battery_no = ATOMIC_INIT(0);
2994 struct power_supply_config cfg = { .drv_data = hidpp };
2995 struct power_supply_desc *desc = &hidpp->battery.desc;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002996 enum power_supply_property *battery_props;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02002997 struct hidpp_battery *battery;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002998 unsigned int num_battery_props;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02002999 unsigned long n;
3000 int ret;
3001
3002 if (hidpp->battery.ps)
3003 return 0;
3004
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003005 hidpp->battery.feature_index = 0xff;
3006 hidpp->battery.solar_feature_index = 0xff;
3007
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003008 if (hidpp->protocol_major >= 2) {
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003009 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3010 ret = hidpp_solar_request_battery_event(hidpp);
3011 else
3012 ret = hidpp20_query_battery_info(hidpp);
3013
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003014 if (ret)
3015 return ret;
3016 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3017 } else {
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003018 ret = hidpp10_query_battery_status(hidpp);
3019 if (ret) {
3020 ret = hidpp10_query_battery_mileage(hidpp);
3021 if (ret)
3022 return -ENOENT;
3023 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3024 } else {
3025 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3026 }
3027 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003028 }
3029
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003030 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3031 hidpp_battery_props,
3032 sizeof(hidpp_battery_props),
3033 GFP_KERNEL);
Gustavo A. R. Silva929b60a2017-07-07 00:12:13 -05003034 if (!battery_props)
3035 return -ENOMEM;
3036
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003037 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
3038
3039 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3040 battery_props[num_battery_props++] =
3041 POWER_SUPPLY_PROP_CAPACITY;
3042
3043 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3044 battery_props[num_battery_props++] =
3045 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3046
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003047 battery = &hidpp->battery;
3048
3049 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003050 desc->properties = battery_props;
3051 desc->num_properties = num_battery_props;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003052 desc->get_property = hidpp_battery_get_property;
3053 sprintf(battery->name, "hidpp_battery_%ld", n);
3054 desc->name = battery->name;
3055 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3056 desc->use_for_apm = 0;
3057
3058 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3059 &battery->desc,
3060 &cfg);
3061 if (IS_ERR(battery->ps))
3062 return PTR_ERR(battery->ps);
3063
3064 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3065
3066 return ret;
3067}
3068
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003069static void hidpp_overwrite_name(struct hid_device *hdev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003070{
3071 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3072 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003073
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003074 if (hidpp->protocol_major < 2)
Benjamin Tissoiresb4f8ce02017-03-27 16:59:24 +02003075 return;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003076
3077 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003078
Simon Wood7bfd2922015-11-19 16:42:12 -07003079 if (!name) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003080 hid_err(hdev, "unable to retrieve the name of the device");
Simon Wood7bfd2922015-11-19 16:42:12 -07003081 } else {
3082 dbg_hid("HID++: Got name: %s\n", name);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003083 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
Simon Wood7bfd2922015-11-19 16:42:12 -07003084 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003085
3086 kfree(name);
3087}
3088
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003089static int hidpp_input_open(struct input_dev *dev)
3090{
3091 struct hid_device *hid = input_get_drvdata(dev);
3092
3093 return hid_hw_open(hid);
3094}
3095
3096static void hidpp_input_close(struct input_dev *dev)
3097{
3098 struct hid_device *hid = input_get_drvdata(dev);
3099
3100 hid_hw_close(hid);
3101}
3102
3103static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3104{
3105 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003106 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003107
3108 if (!input_dev)
3109 return NULL;
3110
3111 input_set_drvdata(input_dev, hdev);
3112 input_dev->open = hidpp_input_open;
3113 input_dev->close = hidpp_input_close;
3114
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003115 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003116 input_dev->phys = hdev->phys;
3117 input_dev->uniq = hdev->uniq;
3118 input_dev->id.bustype = hdev->bus;
3119 input_dev->id.vendor = hdev->vendor;
3120 input_dev->id.product = hdev->product;
3121 input_dev->id.version = hdev->version;
3122 input_dev->dev.parent = &hdev->dev;
3123
3124 return input_dev;
3125}
3126
3127static void hidpp_connect_event(struct hidpp_device *hidpp)
3128{
3129 struct hid_device *hdev = hidpp->hid_dev;
3130 int ret = 0;
3131 bool connected = atomic_read(&hidpp->connected);
3132 struct input_dev *input;
3133 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003134
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003135 if (!connected) {
3136 if (hidpp->battery.ps) {
3137 hidpp->battery.online = false;
3138 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003139 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003140 power_supply_changed(hidpp->battery.ps);
3141 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003142 return;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003143 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003144
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003145 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3146 ret = wtp_connect(hdev, connected);
3147 if (ret)
3148 return;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003149 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3150 ret = m560_send_config_command(hdev, connected);
3151 if (ret)
3152 return;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003153 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3154 ret = k400_connect(hdev, connected);
3155 if (ret)
3156 return;
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003157 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003158
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003159 /* the device is already connected, we can ask for its name and
3160 * protocol */
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003161 if (!hidpp->protocol_major) {
Hans de Goede090760d2019-03-22 08:41:39 +01003162 ret = hidpp_root_get_protocol_version(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003163 if (ret) {
3164 hid_err(hdev, "Can not get the protocol version.\n");
3165 return;
3166 }
3167 }
3168
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003169 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003170 name = hidpp_get_device_name(hidpp);
Hans de Goede2ddf07f2019-04-20 13:22:07 +02003171 if (name) {
3172 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3173 "%s", name);
3174 kfree(name);
3175 if (!devm_name)
3176 return;
3177
3178 hidpp->name = devm_name;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003179 }
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003180 }
3181
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003182 hidpp_initialize_battery(hidpp);
3183
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003184 /* forward current battery state */
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003185 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3186 hidpp10_enable_battery_reporting(hidpp);
3187 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3188 hidpp10_query_battery_mileage(hidpp);
3189 else
3190 hidpp10_query_battery_status(hidpp);
3191 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003192 hidpp20_query_battery_info(hidpp);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003193 }
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003194 if (hidpp->battery.ps)
3195 power_supply_changed(hidpp->battery.ps);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003196
Harry Cutts4435ff22018-12-05 10:42:27 +10003197 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3198 hi_res_scroll_enable(hidpp);
3199
Benjamin Tissoires29368362017-03-27 16:59:28 +02003200 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3201 /* if the input nodes are already created, we can stop now */
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003202 return;
3203
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003204 input = hidpp_allocate_input(hdev);
3205 if (!input) {
3206 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3207 return;
3208 }
3209
Hans de Goedee54abaf2019-04-20 13:22:09 +02003210 hidpp_populate_input(hidpp, input);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003211
3212 ret = input_register_device(input);
3213 if (ret)
3214 input_free_device(input);
3215
3216 hidpp->delayed_input = input;
3217}
3218
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003219static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3220
3221static struct attribute *sysfs_attrs[] = {
3222 &dev_attr_builtin_power_supply.attr,
3223 NULL
3224};
3225
Arvind Yadav35a33cb2017-08-03 16:59:04 +05303226static const struct attribute_group ps_attribute_group = {
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003227 .attrs = sysfs_attrs
3228};
3229
Hans de Goeded71b18f2019-04-20 13:22:12 +02003230static int hidpp_get_report_length(struct hid_device *hdev, int id)
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003231{
3232 struct hid_report_enum *re;
3233 struct hid_report *report;
3234
Hans de Goeded71b18f2019-04-20 13:22:12 +02003235 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3236 report = re->report_id_hash[id];
3237 if (!report)
3238 return 0;
3239
3240 return report->field[0]->report_count + 1;
3241}
3242
3243static bool hidpp_validate_report(struct hid_device *hdev, int id,
3244 int expected_length, bool optional)
3245{
3246 int report_length;
3247
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003248 if (id >= HID_MAX_IDS || id < 0) {
3249 hid_err(hdev, "invalid HID report id %u\n", id);
3250 return false;
3251 }
3252
Hans de Goeded71b18f2019-04-20 13:22:12 +02003253 report_length = hidpp_get_report_length(hdev, id);
3254 if (!report_length)
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003255 return optional;
3256
Hans de Goeded71b18f2019-04-20 13:22:12 +02003257 if (report_length < expected_length) {
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003258 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3259 return false;
3260 }
3261
3262 return true;
3263}
3264
3265static bool hidpp_validate_device(struct hid_device *hdev)
3266{
3267 return hidpp_validate_report(hdev, REPORT_ID_HIDPP_SHORT,
Hans de Goeded71b18f2019-04-20 13:22:12 +02003268 HIDPP_REPORT_SHORT_LENGTH, false) &&
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003269 hidpp_validate_report(hdev, REPORT_ID_HIDPP_LONG,
Hans de Goeded71b18f2019-04-20 13:22:12 +02003270 HIDPP_REPORT_LONG_LENGTH, true);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003271}
3272
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003273static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3274{
3275 struct hidpp_device *hidpp;
3276 int ret;
3277 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003278 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003279
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003280 ret = hid_parse(hdev);
3281 if (ret) {
3282 hid_err(hdev, "%s:parse failed\n", __func__);
3283 return ret;
3284 }
3285
3286 /*
3287 * Make sure the device is HID++ capable, otherwise treat as generic HID
3288 */
3289 if (!hidpp_validate_device(hdev))
3290 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
3291
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003292 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
3293 GFP_KERNEL);
3294 if (!hidpp)
3295 return -ENOMEM;
3296
3297 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003298 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003299 hid_set_drvdata(hdev, hidpp);
3300
3301 hidpp->quirks = id->driver_data;
3302
Hans de Goeded71b18f2019-04-20 13:22:12 +02003303 hidpp->very_long_report_length =
3304 hidpp_get_report_length(hdev, REPORT_ID_HIDPP_VERY_LONG);
3305 if (hidpp->very_long_report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3306 hidpp->very_long_report_length = HIDPP_REPORT_VERY_LONG_MAX_LENGTH;
3307
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003308 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3309 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3310
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003311 if (disable_raw_mode) {
3312 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003313 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003314 }
3315
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003316 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3317 ret = wtp_allocate(hdev, id);
3318 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003319 return ret;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003320 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3321 ret = m560_allocate(hdev);
3322 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003323 return ret;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003324 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3325 ret = k400_allocate(hdev);
3326 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003327 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003328 }
3329
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003330 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003331 mutex_init(&hidpp->send_mutex);
3332 init_waitqueue_head(&hidpp->wait);
3333
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003334 /* indicates we are handling the battery properties in the kernel */
3335 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3336 if (ret)
3337 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3338 hdev->name);
3339
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003340 /*
3341 * Plain USB connections need to actually call start and open
3342 * on the transport driver to allow incoming data.
3343 */
3344 ret = hid_hw_start(hdev, 0);
3345 if (ret) {
3346 hid_err(hdev, "hw start failed\n");
3347 goto hid_hw_start_fail;
Simon Wood7bfd2922015-11-19 16:42:12 -07003348 }
3349
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003350 ret = hid_hw_open(hdev);
3351 if (ret < 0) {
3352 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3353 __func__, ret);
3354 hid_hw_stop(hdev);
3355 goto hid_hw_open_fail;
3356 }
Simon Wood7bfd2922015-11-19 16:42:12 -07003357
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003358 /* Allow incoming packets */
3359 hid_device_io_start(hdev);
3360
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003361 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3362 hidpp_unifying_init(hidpp);
3363
Hans de Goede090760d2019-03-22 08:41:39 +01003364 connected = hidpp_root_get_protocol_version(hidpp) == 0;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003365 atomic_set(&hidpp->connected, connected);
3366 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003367 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02003368 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003369 hid_err(hdev, "Device not connected");
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003370 goto hid_hw_init_fail;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003371 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003372
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003373 hidpp_overwrite_name(hdev);
3374 }
Benjamin Tissoires33797822014-09-30 13:18:30 -04003375
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003376 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003377 ret = wtp_get_config(hidpp);
3378 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003379 goto hid_hw_init_fail;
Simon Wood7f4b49f2015-11-19 16:42:13 -07003380 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3381 ret = g920_get_config(hidpp);
3382 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003383 goto hid_hw_init_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003384 }
3385
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003386 hidpp_connect_event(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003387
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003388 /* Reset the HID node state */
3389 hid_device_io_stop(hdev);
3390 hid_hw_close(hdev);
3391 hid_hw_stop(hdev);
3392
3393 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3394 connect_mask &= ~HID_CONNECT_HIDINPUT;
3395
3396 /* Now export the actual inputs and hidraw nodes to the world */
3397 ret = hid_hw_start(hdev, connect_mask);
3398 if (ret) {
3399 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3400 goto hid_hw_start_fail;
3401 }
3402
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003403 return ret;
3404
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003405hid_hw_init_fail:
3406 hid_hw_close(hdev);
3407hid_hw_open_fail:
3408 hid_hw_stop(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003409hid_hw_start_fail:
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003410 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003411 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003412 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003413 return ret;
3414}
3415
3416static void hidpp_remove(struct hid_device *hdev)
3417{
3418 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3419
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003420 if (!hidpp)
3421 return hid_hw_stop(hdev);
3422
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003423 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3424
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003425 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)
Edwin Veldsff21a632016-01-11 00:25:15 +01003426 hidpp_ff_deinit(hdev);
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003427
Simon Wood7bfd2922015-11-19 16:42:12 -07003428 hid_hw_stop(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003429 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003430 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003431}
3432
Harry Cutts4435ff22018-12-05 10:42:27 +10003433#define LDJ_DEVICE(product) \
3434 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3435 USB_VENDOR_ID_LOGITECH, (product))
3436
Hans de Goede754a30882019-04-20 13:22:11 +02003437#define L27MHZ_DEVICE(product) \
3438 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
3439 USB_VENDOR_ID_LOGITECH, (product))
3440
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003441static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04003442 { /* wireless touchpad */
Harry Cutts16767222018-12-05 10:42:28 +10003443 LDJ_DEVICE(0x4011),
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04003444 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3445 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003446 { /* wireless touchpad T650 */
Harry Cutts16767222018-12-05 10:42:28 +10003447 LDJ_DEVICE(0x4101),
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003448 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003449 { /* wireless touchpad T651 */
3450 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3451 USB_DEVICE_ID_LOGITECH_T651),
3452 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Harry Cutts4435ff22018-12-05 10:42:27 +10003453 { /* Mouse Logitech Anywhere MX */
3454 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3455 { /* Mouse Logitech Cube */
3456 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3457 { /* Mouse Logitech M335 */
3458 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3459 { /* Mouse Logitech M515 */
3460 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003461 { /* Mouse logitech M560 */
Harry Cutts4435ff22018-12-05 10:42:27 +10003462 LDJ_DEVICE(0x402d),
3463 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
3464 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3465 { /* Mouse Logitech M705 (firmware RQM17) */
3466 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3467 { /* Mouse Logitech M705 (firmware RQM67) */
3468 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3469 { /* Mouse Logitech M720 */
3470 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3471 { /* Mouse Logitech MX Anywhere 2 */
3472 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3473 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3474 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3475 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3476 { /* Mouse Logitech MX Anywhere 2S */
3477 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3478 { /* Mouse Logitech MX Master */
3479 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3480 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3481 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3482 { /* Mouse Logitech MX Master 2S */
3483 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3484 { /* Mouse Logitech Performance MX */
3485 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003486 { /* Keyboard logitech K400 */
Harry Cutts16767222018-12-05 10:42:28 +10003487 LDJ_DEVICE(0x4024),
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003488 .driver_data = HIDPP_QUIRK_CLASS_K400 },
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003489 { /* Solar Keyboard Logitech K750 */
Harry Cutts16767222018-12-05 10:42:28 +10003490 LDJ_DEVICE(0x4002),
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003491 .driver_data = HIDPP_QUIRK_CLASS_K750 },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003492
Harry Cutts16767222018-12-05 10:42:28 +10003493 { LDJ_DEVICE(HID_ANY_ID) },
Simon Wood7bfd2922015-11-19 16:42:12 -07003494
Hans de Goede754a30882019-04-20 13:22:11 +02003495 { L27MHZ_DEVICE(HID_ANY_ID) },
3496
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003497 { /* Logitech G403 Gaming Mouse over USB */
3498 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
3499 { /* Logitech G700 Gaming Mouse over USB */
3500 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC06B) },
3501 { /* Logitech G900 Gaming Mouse over USB */
3502 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
3503 { /* Logitech G920 Wheel over USB */
3504 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
Simon Wood7bfd2922015-11-19 16:42:12 -07003505 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003506 {}
3507};
3508
3509MODULE_DEVICE_TABLE(hid, hidpp_devices);
3510
Harry Cutts4435ff22018-12-05 10:42:27 +10003511static const struct hid_usage_id hidpp_usages[] = {
3512 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
3513 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
3514};
3515
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003516static struct hid_driver hidpp_driver = {
3517 .name = "logitech-hidpp-device",
3518 .id_table = hidpp_devices,
3519 .probe = hidpp_probe,
3520 .remove = hidpp_remove,
3521 .raw_event = hidpp_raw_event,
Harry Cutts4435ff22018-12-05 10:42:27 +10003522 .usage_table = hidpp_usages,
3523 .event = hidpp_event,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003524 .input_configured = hidpp_input_configured,
3525 .input_mapping = hidpp_input_mapping,
Simon Wood0b1804e2015-11-19 16:42:15 -07003526 .input_mapped = hidpp_input_mapped,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003527};
3528
3529module_hid_driver(hidpp_driver);