blob: 2a0866e6facef123db8530a7c3e8d8e853294254 [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
Simon Wooda5ce8f52015-11-19 16:42:11 -070054#define HIDPP_REPORT_VERY_LONG_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;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700109 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400110};
111
112struct rap {
113 u8 sub_id;
114 u8 reg_address;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700115 u8 params[HIDPP_REPORT_VERY_LONG_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;
165 bool answer_available;
166 u8 protocol_major;
167 u8 protocol_minor;
168
169 void *private_data;
170
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400171 struct work_struct work;
172 struct kfifo delayed_work_fifo;
173 atomic_t connected;
174 struct input_dev *delayed_input;
175
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400176 unsigned long quirks;
Benjamin Tissoires206d7c62017-03-27 16:59:25 +0200177 unsigned long capabilities;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400178
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000179 struct hidpp_battery battery;
Harry Cutts4435ff22018-12-05 10:42:27 +1000180 struct hidpp_scroll_counter vertical_wheel_counter;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000181};
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400182
Peter Wuf677bb12014-12-16 01:50:14 +0100183/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400184#define HIDPP_ERROR 0x8f
185#define HIDPP_ERROR_SUCCESS 0x00
186#define HIDPP_ERROR_INVALID_SUBID 0x01
187#define HIDPP_ERROR_INVALID_ADRESS 0x02
188#define HIDPP_ERROR_INVALID_VALUE 0x03
189#define HIDPP_ERROR_CONNECT_FAIL 0x04
190#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
191#define HIDPP_ERROR_ALREADY_EXISTS 0x06
192#define HIDPP_ERROR_BUSY 0x07
193#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
194#define HIDPP_ERROR_RESOURCE_ERROR 0x09
195#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
196#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
197#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100198/* HID++ 2.0 error codes */
199#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400200
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400201static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
202
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400203static int __hidpp_send_report(struct hid_device *hdev,
204 struct hidpp_report *hidpp_report)
205{
Simon Wood7bfd2922015-11-19 16:42:12 -0700206 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400207 int fields_count, ret;
208
Simon Wood7bfd2922015-11-19 16:42:12 -0700209 hidpp = hid_get_drvdata(hdev);
210
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400211 switch (hidpp_report->report_id) {
212 case REPORT_ID_HIDPP_SHORT:
213 fields_count = HIDPP_REPORT_SHORT_LENGTH;
214 break;
215 case REPORT_ID_HIDPP_LONG:
216 fields_count = HIDPP_REPORT_LONG_LENGTH;
217 break;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700218 case REPORT_ID_HIDPP_VERY_LONG:
219 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
220 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400221 default:
222 return -ENODEV;
223 }
224
225 /*
226 * set the device_index as the receiver, it will be overwritten by
227 * hid_hw_request if needed
228 */
229 hidpp_report->device_index = 0xff;
230
Simon Wood7bfd2922015-11-19 16:42:12 -0700231 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
232 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
233 } else {
234 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
235 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
236 HID_REQ_SET_REPORT);
237 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400238
239 return ret == fields_count ? 0 : -1;
240}
241
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500242/**
243 * hidpp_send_message_sync() returns 0 in case of success, and something else
244 * in case of a failure.
245 * - If ' something else' is positive, that means that an error has been raised
246 * by the protocol itself.
247 * - If ' something else' is negative, that means that we had a classic error
248 * (-ENOMEM, -EPIPE, etc...)
249 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400250static int hidpp_send_message_sync(struct hidpp_device *hidpp,
251 struct hidpp_report *message,
252 struct hidpp_report *response)
253{
254 int ret;
255
256 mutex_lock(&hidpp->send_mutex);
257
258 hidpp->send_receive_buf = response;
259 hidpp->answer_available = false;
260
261 /*
262 * So that we can later validate the answer when it arrives
263 * in hidpp_raw_event
264 */
265 *response = *message;
266
267 ret = __hidpp_send_report(hidpp->hid_dev, message);
268
269 if (ret) {
270 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
271 memset(response, 0, sizeof(struct hidpp_report));
272 goto exit;
273 }
274
275 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
276 5*HZ)) {
277 dbg_hid("%s:timeout waiting for response\n", __func__);
278 memset(response, 0, sizeof(struct hidpp_report));
279 ret = -ETIMEDOUT;
280 }
281
282 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100283 response->rap.sub_id == HIDPP_ERROR) {
284 ret = response->rap.params[1];
285 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
286 goto exit;
287 }
288
Simon Wooda5ce8f52015-11-19 16:42:11 -0700289 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
290 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
291 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400292 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100293 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400294 goto exit;
295 }
296
297exit:
298 mutex_unlock(&hidpp->send_mutex);
299 return ret;
300
301}
302
303static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
304 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
305 struct hidpp_report *response)
306{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300307 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400308 int ret;
309
310 if (param_count > sizeof(message->fap.params))
311 return -EINVAL;
312
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300313 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
314 if (!message)
315 return -ENOMEM;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700316
317 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
318 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
319 else
320 message->report_id = REPORT_ID_HIDPP_LONG;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400321 message->fap.feature_index = feat_index;
322 message->fap.funcindex_clientid = funcindex_clientid;
323 memcpy(&message->fap.params, params, param_count);
324
325 ret = hidpp_send_message_sync(hidpp, message, response);
326 kfree(message);
327 return ret;
328}
329
Benjamin Tissoires33797822014-09-30 13:18:30 -0400330static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
331 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
332 struct hidpp_report *response)
333{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300334 struct hidpp_report *message;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700335 int ret, max_count;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400336
Simon Wooda5ce8f52015-11-19 16:42:11 -0700337 switch (report_id) {
338 case REPORT_ID_HIDPP_SHORT:
339 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
340 break;
341 case REPORT_ID_HIDPP_LONG:
342 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
343 break;
344 case REPORT_ID_HIDPP_VERY_LONG:
345 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
346 break;
347 default:
Benjamin Tissoires33797822014-09-30 13:18:30 -0400348 return -EINVAL;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700349 }
Benjamin Tissoires33797822014-09-30 13:18:30 -0400350
Simon Wooda5ce8f52015-11-19 16:42:11 -0700351 if (param_count > max_count)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400352 return -EINVAL;
353
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300354 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
355 if (!message)
356 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400357 message->report_id = report_id;
358 message->rap.sub_id = sub_id;
359 message->rap.reg_address = reg_address;
360 memcpy(&message->rap.params, params, param_count);
361
362 ret = hidpp_send_message_sync(hidpp_dev, message, response);
363 kfree(message);
364 return ret;
365}
366
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400367static void delayed_work_cb(struct work_struct *work)
368{
369 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
370 work);
371 hidpp_connect_event(hidpp);
372}
373
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400374static inline bool hidpp_match_answer(struct hidpp_report *question,
375 struct hidpp_report *answer)
376{
377 return (answer->fap.feature_index == question->fap.feature_index) &&
378 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
379}
380
381static inline bool hidpp_match_error(struct hidpp_report *question,
382 struct hidpp_report *answer)
383{
Peter Wuf677bb12014-12-16 01:50:14 +0100384 return ((answer->rap.sub_id == HIDPP_ERROR) ||
385 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400386 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
387 (answer->fap.params[0] == question->fap.funcindex_clientid);
388}
389
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400390static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
391{
392 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
393 (report->rap.sub_id == 0x41);
394}
395
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500396/**
397 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
398 */
399static void hidpp_prefix_name(char **name, int name_length)
400{
401#define PREFIX_LENGTH 9 /* "Logitech " */
402
403 int new_length;
404 char *new_name;
405
406 if (name_length > PREFIX_LENGTH &&
407 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
408 /* The prefix has is already in the name */
409 return;
410
411 new_length = PREFIX_LENGTH + name_length;
412 new_name = kzalloc(new_length, GFP_KERNEL);
413 if (!new_name)
414 return;
415
416 snprintf(new_name, new_length, "Logitech %s", *name);
417
418 kfree(*name);
419
420 *name = new_name;
421}
422
Harry Cutts4435ff22018-12-05 10:42:27 +1000423/**
424 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
425 * events given a high-resolution wheel
426 * movement.
427 * @counter: a hid_scroll_counter struct describing the wheel.
428 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
429 * units.
430 *
431 * Given a high-resolution movement, this function converts the movement into
432 * fractions of 120 and emits high-resolution scroll events for the input
433 * device. It also uses the multiplier from &struct hid_scroll_counter to
434 * emit low-resolution scroll events when appropriate for
435 * backwards-compatibility with userspace input libraries.
436 */
437static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter *counter,
438 int hi_res_value)
439{
440 int low_res_value, remainder, direction;
441 unsigned long long now, previous;
442
443 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
444 input_report_rel(counter->dev, REL_WHEEL_HI_RES, hi_res_value);
445
446 remainder = counter->remainder;
447 direction = hi_res_value > 0 ? 1 : -1;
448
449 now = sched_clock();
450 previous = counter->last_time;
451 counter->last_time = now;
452 /*
453 * Reset the remainder after a period of inactivity or when the
454 * direction changes. This prevents the REL_WHEEL emulation point
455 * from sliding for devices that don't always provide the same
456 * number of movements per detent.
457 */
458 if (now - previous > 1000000000 || direction != counter->direction)
459 remainder = 0;
460
461 counter->direction = direction;
462 remainder += hi_res_value;
463
464 /* Some wheels will rest 7/8ths of a detent from the previous detent
465 * after slow movement, so we want the threshold for low-res events to
466 * be in the middle between two detents (e.g. after 4/8ths) as
467 * opposed to on the detents themselves (8/8ths).
468 */
469 if (abs(remainder) >= 60) {
470 /* Add (or subtract) 1 because we want to trigger when the wheel
471 * is half-way to the next detent (i.e. scroll 1 detent after a
472 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
473 * etc.).
474 */
475 low_res_value = remainder / 120;
476 if (low_res_value == 0)
477 low_res_value = (hi_res_value > 0 ? 1 : -1);
478 input_report_rel(counter->dev, REL_WHEEL, low_res_value);
479 remainder -= low_res_value * 120;
480 }
481 counter->remainder = remainder;
482}
483
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400484/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400485/* HIDP++ 1.0 commands */
486/* -------------------------------------------------------------------------- */
487
488#define HIDPP_SET_REGISTER 0x80
489#define HIDPP_GET_REGISTER 0x81
490#define HIDPP_SET_LONG_REGISTER 0x82
491#define HIDPP_GET_LONG_REGISTER 0x83
492
Harry Cutts95c3d002018-12-05 10:42:26 +1000493/**
494 * hidpp10_set_register_bit() - Sets a single bit in a HID++ 1.0 register.
495 * @hidpp_dev: the device to set the register on.
496 * @register_address: the address of the register to modify.
497 * @byte: the byte of the register to modify. Should be less than 3.
498 * Return: 0 if successful, otherwise a negative error code.
499 */
500static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev,
501 u8 register_address, u8 byte, u8 bit)
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200502{
503 struct hidpp_report response;
504 int ret;
505 u8 params[3] = { 0 };
506
507 ret = hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000508 REPORT_ID_HIDPP_SHORT,
509 HIDPP_GET_REGISTER,
510 register_address,
511 NULL, 0, &response);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200512 if (ret)
513 return ret;
514
515 memcpy(params, response.rap.params, 3);
516
Harry Cutts95c3d002018-12-05 10:42:26 +1000517 params[byte] |= BIT(bit);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200518
519 return hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000520 REPORT_ID_HIDPP_SHORT,
521 HIDPP_SET_REGISTER,
522 register_address,
523 params, 3, &response);
524}
525
526
527#define HIDPP_REG_GENERAL 0x00
528
529static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
530{
531 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_GENERAL, 0, 4);
532}
533
534#define HIDPP_REG_FEATURES 0x01
535
536/* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
537static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
538{
539 return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_FEATURES, 0, 6);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200540}
541
542#define HIDPP_REG_BATTERY_STATUS 0x07
543
544static int hidpp10_battery_status_map_level(u8 param)
545{
546 int level;
547
548 switch (param) {
549 case 1 ... 2:
550 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
551 break;
552 case 3 ... 4:
553 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
554 break;
555 case 5 ... 6:
556 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
557 break;
558 case 7:
559 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
560 break;
561 default:
562 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
563 }
564
565 return level;
566}
567
568static int hidpp10_battery_status_map_status(u8 param)
569{
570 int status;
571
572 switch (param) {
573 case 0x00:
574 /* discharging (in use) */
575 status = POWER_SUPPLY_STATUS_DISCHARGING;
576 break;
577 case 0x21: /* (standard) charging */
578 case 0x24: /* fast charging */
579 case 0x25: /* slow charging */
580 status = POWER_SUPPLY_STATUS_CHARGING;
581 break;
582 case 0x26: /* topping charge */
583 case 0x22: /* charge complete */
584 status = POWER_SUPPLY_STATUS_FULL;
585 break;
586 case 0x20: /* unknown */
587 status = POWER_SUPPLY_STATUS_UNKNOWN;
588 break;
589 /*
590 * 0x01...0x1F = reserved (not charging)
591 * 0x23 = charging error
592 * 0x27..0xff = reserved
593 */
594 default:
595 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
596 break;
597 }
598
599 return status;
600}
601
602static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
603{
604 struct hidpp_report response;
605 int ret, status;
606
607 ret = hidpp_send_rap_command_sync(hidpp,
608 REPORT_ID_HIDPP_SHORT,
609 HIDPP_GET_REGISTER,
610 HIDPP_REG_BATTERY_STATUS,
611 NULL, 0, &response);
612 if (ret)
613 return ret;
614
615 hidpp->battery.level =
616 hidpp10_battery_status_map_level(response.rap.params[0]);
617 status = hidpp10_battery_status_map_status(response.rap.params[1]);
618 hidpp->battery.status = status;
619 /* the capacity is only available when discharging or full */
620 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
621 status == POWER_SUPPLY_STATUS_FULL;
622
623 return 0;
624}
625
626#define HIDPP_REG_BATTERY_MILEAGE 0x0D
627
628static int hidpp10_battery_mileage_map_status(u8 param)
629{
630 int status;
631
632 switch (param >> 6) {
633 case 0x00:
634 /* discharging (in use) */
635 status = POWER_SUPPLY_STATUS_DISCHARGING;
636 break;
637 case 0x01: /* charging */
638 status = POWER_SUPPLY_STATUS_CHARGING;
639 break;
640 case 0x02: /* charge complete */
641 status = POWER_SUPPLY_STATUS_FULL;
642 break;
643 /*
644 * 0x03 = charging error
645 */
646 default:
647 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
648 break;
649 }
650
651 return status;
652}
653
654static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
655{
656 struct hidpp_report response;
657 int ret, status;
658
659 ret = hidpp_send_rap_command_sync(hidpp,
660 REPORT_ID_HIDPP_SHORT,
661 HIDPP_GET_REGISTER,
662 HIDPP_REG_BATTERY_MILEAGE,
663 NULL, 0, &response);
664 if (ret)
665 return ret;
666
667 hidpp->battery.capacity = response.rap.params[0];
668 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
669 hidpp->battery.status = status;
670 /* the capacity is only available when discharging or full */
671 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
672 status == POWER_SUPPLY_STATUS_FULL;
673
674 return 0;
675}
676
677static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
678{
679 struct hidpp_report *report = (struct hidpp_report *)data;
680 int status, capacity, level;
681 bool changed;
682
683 if (report->report_id != REPORT_ID_HIDPP_SHORT)
684 return 0;
685
686 switch (report->rap.sub_id) {
687 case HIDPP_REG_BATTERY_STATUS:
688 capacity = hidpp->battery.capacity;
689 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
690 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
691 break;
692 case HIDPP_REG_BATTERY_MILEAGE:
693 capacity = report->rap.params[0];
694 level = hidpp->battery.level;
695 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
696 break;
697 default:
698 return 0;
699 }
700
701 changed = capacity != hidpp->battery.capacity ||
702 level != hidpp->battery.level ||
703 status != hidpp->battery.status;
704
705 /* the capacity is only available when discharging or full */
706 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
707 status == POWER_SUPPLY_STATUS_FULL;
708
709 if (changed) {
710 hidpp->battery.level = level;
711 hidpp->battery.status = status;
712 if (hidpp->battery.ps)
713 power_supply_changed(hidpp->battery.ps);
714 }
715
716 return 0;
717}
718
Benjamin Tissoires33797822014-09-30 13:18:30 -0400719#define HIDPP_REG_PAIRING_INFORMATION 0xB5
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200720#define HIDPP_EXTENDED_PAIRING 0x30
721#define HIDPP_DEVICE_NAME 0x40
Benjamin Tissoires33797822014-09-30 13:18:30 -0400722
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200723static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400724{
725 struct hidpp_report response;
726 int ret;
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200727 u8 params[1] = { HIDPP_DEVICE_NAME };
Benjamin Tissoires33797822014-09-30 13:18:30 -0400728 char *name;
729 int len;
730
731 ret = hidpp_send_rap_command_sync(hidpp_dev,
732 REPORT_ID_HIDPP_SHORT,
733 HIDPP_GET_LONG_REGISTER,
734 HIDPP_REG_PAIRING_INFORMATION,
735 params, 1, &response);
736 if (ret)
737 return NULL;
738
739 len = response.rap.params[1];
740
Peter Wu3a034a72014-12-11 13:51:19 +0100741 if (2 + len > sizeof(response.rap.params))
742 return NULL;
743
Hans de Goede22bf6bd2019-04-20 13:22:06 +0200744 if (len < 4) /* logitech devices are usually at least Xddd */
745 return NULL;
746
Benjamin Tissoires33797822014-09-30 13:18:30 -0400747 name = kzalloc(len + 1, GFP_KERNEL);
748 if (!name)
749 return NULL;
750
751 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500752
753 /* include the terminating '\0' */
754 hidpp_prefix_name(&name, len + 1);
755
Benjamin Tissoires33797822014-09-30 13:18:30 -0400756 return name;
757}
758
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200759static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
760{
761 struct hidpp_report response;
762 int ret;
763 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
764
765 ret = hidpp_send_rap_command_sync(hidpp,
766 REPORT_ID_HIDPP_SHORT,
767 HIDPP_GET_LONG_REGISTER,
768 HIDPP_REG_PAIRING_INFORMATION,
769 params, 1, &response);
770 if (ret)
771 return ret;
772
773 /*
774 * We don't care about LE or BE, we will output it as a string
775 * with %4phD, so we need to keep the order.
776 */
777 *serial = *((u32 *)&response.rap.params[1]);
778 return 0;
779}
780
781static int hidpp_unifying_init(struct hidpp_device *hidpp)
782{
783 struct hid_device *hdev = hidpp->hid_dev;
784 const char *name;
785 u32 serial;
786 int ret;
787
788 ret = hidpp_unifying_get_serial(hidpp, &serial);
789 if (ret)
790 return ret;
791
792 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
793 hdev->product, &serial);
794 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
795
796 name = hidpp_unifying_get_name(hidpp);
797 if (!name)
798 return -EIO;
799
800 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
801 dbg_hid("HID++ Unifying: Got name: %s\n", name);
802
803 kfree(name);
804 return 0;
805}
806
Benjamin Tissoires33797822014-09-30 13:18:30 -0400807/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400808/* 0x0000: Root */
809/* -------------------------------------------------------------------------- */
810
811#define HIDPP_PAGE_ROOT 0x0000
812#define HIDPP_PAGE_ROOT_IDX 0x00
813
814#define CMD_ROOT_GET_FEATURE 0x01
815#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
816
817static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
818 u8 *feature_index, u8 *feature_type)
819{
820 struct hidpp_report response;
821 int ret;
822 u8 params[2] = { feature >> 8, feature & 0x00FF };
823
824 ret = hidpp_send_fap_command_sync(hidpp,
825 HIDPP_PAGE_ROOT_IDX,
826 CMD_ROOT_GET_FEATURE,
827 params, 2, &response);
828 if (ret)
829 return ret;
830
Benjamin Tissoiresa9525b82017-03-27 16:59:32 +0200831 if (response.fap.params[0] == 0)
832 return -ENOENT;
833
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400834 *feature_index = response.fap.params[0];
835 *feature_type = response.fap.params[1];
836
837 return ret;
838}
839
840static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
841{
842 struct hidpp_report response;
843 int ret;
844
845 ret = hidpp_send_fap_command_sync(hidpp,
846 HIDPP_PAGE_ROOT_IDX,
847 CMD_ROOT_GET_PROTOCOL_VERSION,
848 NULL, 0, &response);
849
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500850 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400851 hidpp->protocol_major = 1;
852 hidpp->protocol_minor = 0;
Hans de Goede9576af62019-03-22 08:41:38 +0100853 goto print_version;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400854 }
855
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500856 /* the device might not be connected */
857 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
858 return -EIO;
859
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500860 if (ret > 0) {
861 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
862 __func__, ret);
863 return -EPROTO;
864 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400865 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500866 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400867
868 hidpp->protocol_major = response.fap.params[0];
869 hidpp->protocol_minor = response.fap.params[1];
870
Hans de Goede9576af62019-03-22 08:41:38 +0100871print_version:
872 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
873 hidpp->protocol_major, hidpp->protocol_minor);
874 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400875}
876
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400877/* -------------------------------------------------------------------------- */
878/* 0x0005: GetDeviceNameType */
879/* -------------------------------------------------------------------------- */
880
881#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
882
883#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
884#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
885#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
886
887static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
888 u8 feature_index, u8 *nameLength)
889{
890 struct hidpp_report response;
891 int ret;
892
893 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
894 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
895
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500896 if (ret > 0) {
897 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
898 __func__, ret);
899 return -EPROTO;
900 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400901 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500902 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400903
904 *nameLength = response.fap.params[0];
905
906 return ret;
907}
908
909static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
910 u8 feature_index, u8 char_index, char *device_name, int len_buf)
911{
912 struct hidpp_report response;
913 int ret, i;
914 int count;
915
916 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
917 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
918 &response);
919
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500920 if (ret > 0) {
921 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
922 __func__, ret);
923 return -EPROTO;
924 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400925 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500926 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400927
Simon Wooda5ce8f52015-11-19 16:42:11 -0700928 switch (response.report_id) {
929 case REPORT_ID_HIDPP_VERY_LONG:
930 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
931 break;
932 case REPORT_ID_HIDPP_LONG:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400933 count = HIDPP_REPORT_LONG_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700934 break;
935 case REPORT_ID_HIDPP_SHORT:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400936 count = HIDPP_REPORT_SHORT_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700937 break;
938 default:
939 return -EPROTO;
940 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400941
942 if (len_buf < count)
943 count = len_buf;
944
945 for (i = 0; i < count; i++)
946 device_name[i] = response.fap.params[i];
947
948 return count;
949}
950
Peter Wu02cc0972014-12-11 13:51:17 +0100951static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400952{
953 u8 feature_type;
954 u8 feature_index;
955 u8 __name_length;
956 char *name;
957 unsigned index = 0;
958 int ret;
959
960 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
961 &feature_index, &feature_type);
962 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100963 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400964
965 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
966 &__name_length);
967 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100968 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400969
970 name = kzalloc(__name_length + 1, GFP_KERNEL);
971 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100972 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400973
Peter Wu1430ee72014-12-11 13:51:18 +0100974 while (index < __name_length) {
975 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400976 feature_index, index, name + index,
977 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +0100978 if (ret <= 0) {
979 kfree(name);
980 return NULL;
981 }
982 index += ret;
983 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400984
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500985 /* include the terminating '\0' */
986 hidpp_prefix_name(&name, __name_length + 1);
987
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400988 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400989}
990
991/* -------------------------------------------------------------------------- */
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000992/* 0x1000: Battery level status */
993/* -------------------------------------------------------------------------- */
994
995#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
996
997#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
998#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
999
1000#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1001
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001002#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1003#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1004#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1005
1006static int hidpp_map_battery_level(int capacity)
1007{
1008 if (capacity < 11)
1009 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
Hans de Goede1f87b0c2019-03-22 08:41:40 +01001010 /*
1011 * The spec says this should be < 31 but some devices report 30
1012 * with brand new batteries and Windows reports 30 as "Good".
1013 */
1014 else if (capacity < 30)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001015 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1016 else if (capacity < 81)
1017 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1018 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1019}
1020
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001021static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001022 int *next_capacity,
1023 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001024{
1025 int status;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001026
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001027 *capacity = data[0];
1028 *next_capacity = data[1];
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001029 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001030
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001031 /* When discharging, we can rely on the device reported capacity.
1032 * For all other states the device reports 0 (unknown).
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001033 */
1034 switch (data[2]) {
1035 case 0: /* discharging (in use) */
1036 status = POWER_SUPPLY_STATUS_DISCHARGING;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001037 *level = hidpp_map_battery_level(*capacity);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001038 break;
1039 case 1: /* recharging */
1040 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001041 break;
1042 case 2: /* charge in final stage */
1043 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001044 break;
1045 case 3: /* charge complete */
1046 status = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001047 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001048 *capacity = 100;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001049 break;
1050 case 4: /* recharging below optimal speed */
1051 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001052 break;
1053 /* 5 = invalid battery type
1054 6 = thermal error
1055 7 = other charging error */
1056 default:
1057 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001058 break;
1059 }
1060
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001061 return status;
1062}
1063
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001064static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1065 u8 feature_index,
1066 int *status,
1067 int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001068 int *next_capacity,
1069 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001070{
1071 struct hidpp_report response;
1072 int ret;
1073 u8 *params = (u8 *)response.fap.params;
1074
1075 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1076 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1077 NULL, 0, &response);
1078 if (ret > 0) {
1079 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1080 __func__, ret);
1081 return -EPROTO;
1082 }
1083 if (ret)
1084 return ret;
1085
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001086 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001087 next_capacity,
1088 level);
1089
1090 return 0;
1091}
1092
1093static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1094 u8 feature_index)
1095{
1096 struct hidpp_report response;
1097 int ret;
1098 u8 *params = (u8 *)response.fap.params;
1099 unsigned int level_count, flags;
1100
1101 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1102 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1103 NULL, 0, &response);
1104 if (ret > 0) {
1105 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1106 __func__, ret);
1107 return -EPROTO;
1108 }
1109 if (ret)
1110 return ret;
1111
1112 level_count = params[0];
1113 flags = params[1];
1114
1115 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1116 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1117 else
1118 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001119
1120 return 0;
1121}
1122
1123static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1124{
1125 u8 feature_type;
1126 int ret;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001127 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001128
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001129 if (hidpp->battery.feature_index == 0xff) {
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001130 ret = hidpp_root_get_feature(hidpp,
1131 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1132 &hidpp->battery.feature_index,
1133 &feature_type);
1134 if (ret)
1135 return ret;
1136 }
1137
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001138 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1139 hidpp->battery.feature_index,
1140 &status, &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001141 &next_capacity, &level);
1142 if (ret)
1143 return ret;
1144
1145 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1146 hidpp->battery.feature_index);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001147 if (ret)
1148 return ret;
1149
1150 hidpp->battery.status = status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001151 hidpp->battery.capacity = capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001152 hidpp->battery.level = level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001153 /* the capacity is only available when discharging or full */
1154 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1155 status == POWER_SUPPLY_STATUS_FULL;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001156
1157 return 0;
1158}
1159
1160static int hidpp20_battery_event(struct hidpp_device *hidpp,
1161 u8 *data, int size)
1162{
1163 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001164 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001165 bool changed;
1166
1167 if (report->fap.feature_index != hidpp->battery.feature_index ||
1168 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1169 return 0;
1170
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001171 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1172 &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001173 &next_capacity,
1174 &level);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001175
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001176 /* the capacity is only available when discharging or full */
1177 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1178 status == POWER_SUPPLY_STATUS_FULL;
1179
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001180 changed = capacity != hidpp->battery.capacity ||
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001181 level != hidpp->battery.level ||
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001182 status != hidpp->battery.status;
1183
1184 if (changed) {
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001185 hidpp->battery.level = level;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001186 hidpp->battery.capacity = capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001187 hidpp->battery.status = status;
1188 if (hidpp->battery.ps)
1189 power_supply_changed(hidpp->battery.ps);
1190 }
1191
1192 return 0;
1193}
1194
1195static enum power_supply_property hidpp_battery_props[] = {
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001196 POWER_SUPPLY_PROP_ONLINE,
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001197 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001198 POWER_SUPPLY_PROP_SCOPE,
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001199 POWER_SUPPLY_PROP_MODEL_NAME,
1200 POWER_SUPPLY_PROP_MANUFACTURER,
1201 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001202 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1203 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001204};
1205
1206static int hidpp_battery_get_property(struct power_supply *psy,
1207 enum power_supply_property psp,
1208 union power_supply_propval *val)
1209{
1210 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1211 int ret = 0;
1212
1213 switch(psp) {
1214 case POWER_SUPPLY_PROP_STATUS:
1215 val->intval = hidpp->battery.status;
1216 break;
1217 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001218 val->intval = hidpp->battery.capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001219 break;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001220 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1221 val->intval = hidpp->battery.level;
1222 break;
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001223 case POWER_SUPPLY_PROP_SCOPE:
1224 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1225 break;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001226 case POWER_SUPPLY_PROP_ONLINE:
1227 val->intval = hidpp->battery.online;
1228 break;
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001229 case POWER_SUPPLY_PROP_MODEL_NAME:
1230 if (!strncmp(hidpp->name, "Logitech ", 9))
1231 val->strval = hidpp->name + 9;
1232 else
1233 val->strval = hidpp->name;
1234 break;
1235 case POWER_SUPPLY_PROP_MANUFACTURER:
1236 val->strval = "Logitech";
1237 break;
1238 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1239 val->strval = hidpp->hid_dev->uniq;
1240 break;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001241 default:
1242 ret = -EINVAL;
1243 break;
1244 }
1245
1246 return ret;
1247}
1248
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001249/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10001250/* 0x2120: Hi-resolution scrolling */
1251/* -------------------------------------------------------------------------- */
1252
1253#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1254
1255#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1256
1257static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1258 bool enabled, u8 *multiplier)
1259{
1260 u8 feature_index;
1261 u8 feature_type;
1262 int ret;
1263 u8 params[1];
1264 struct hidpp_report response;
1265
1266 ret = hidpp_root_get_feature(hidpp,
1267 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1268 &feature_index,
1269 &feature_type);
1270 if (ret)
1271 return ret;
1272
1273 params[0] = enabled ? BIT(0) : 0;
1274 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1275 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1276 params, sizeof(params), &response);
1277 if (ret)
1278 return ret;
1279 *multiplier = response.fap.params[1];
1280 return 0;
1281}
1282
1283/* -------------------------------------------------------------------------- */
1284/* 0x2121: HiRes Wheel */
1285/* -------------------------------------------------------------------------- */
1286
1287#define HIDPP_PAGE_HIRES_WHEEL 0x2121
1288
1289#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1290#define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1291
1292static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1293 u8 *multiplier)
1294{
1295 u8 feature_index;
1296 u8 feature_type;
1297 int ret;
1298 struct hidpp_report response;
1299
1300 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1301 &feature_index, &feature_type);
1302 if (ret)
1303 goto return_default;
1304
1305 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1306 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1307 NULL, 0, &response);
1308 if (ret)
1309 goto return_default;
1310
1311 *multiplier = response.fap.params[0];
1312 return 0;
1313return_default:
1314 hid_warn(hidpp->hid_dev,
1315 "Couldn't get wheel multiplier (error %d)\n", ret);
1316 return ret;
1317}
1318
1319static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1320 bool high_resolution, bool use_hidpp)
1321{
1322 u8 feature_index;
1323 u8 feature_type;
1324 int ret;
1325 u8 params[1];
1326 struct hidpp_report response;
1327
1328 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1329 &feature_index, &feature_type);
1330 if (ret)
1331 return ret;
1332
1333 params[0] = (invert ? BIT(2) : 0) |
1334 (high_resolution ? BIT(1) : 0) |
1335 (use_hidpp ? BIT(0) : 0);
1336
1337 return hidpp_send_fap_command_sync(hidpp, feature_index,
1338 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1339 params, sizeof(params), &response);
1340}
1341
1342/* -------------------------------------------------------------------------- */
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001343/* 0x4301: Solar Keyboard */
1344/* -------------------------------------------------------------------------- */
1345
1346#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1347
1348#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1349
1350#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1351#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1352#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1353
1354static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1355{
1356 struct hidpp_report response;
1357 u8 params[2] = { 1, 1 };
1358 u8 feature_type;
1359 int ret;
1360
1361 if (hidpp->battery.feature_index == 0xff) {
1362 ret = hidpp_root_get_feature(hidpp,
1363 HIDPP_PAGE_SOLAR_KEYBOARD,
1364 &hidpp->battery.solar_feature_index,
1365 &feature_type);
1366 if (ret)
1367 return ret;
1368 }
1369
1370 ret = hidpp_send_fap_command_sync(hidpp,
1371 hidpp->battery.solar_feature_index,
1372 CMD_SOLAR_SET_LIGHT_MEASURE,
1373 params, 2, &response);
1374 if (ret > 0) {
1375 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1376 __func__, ret);
1377 return -EPROTO;
1378 }
1379 if (ret)
1380 return ret;
1381
1382 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1383
1384 return 0;
1385}
1386
1387static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1388 u8 *data, int size)
1389{
1390 struct hidpp_report *report = (struct hidpp_report *)data;
1391 int capacity, lux, status;
1392 u8 function;
1393
1394 function = report->fap.funcindex_clientid;
1395
1396
1397 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1398 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1399 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1400 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1401 return 0;
1402
1403 capacity = report->fap.params[0];
1404
1405 switch (function) {
1406 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1407 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1408 if (lux > 200)
1409 status = POWER_SUPPLY_STATUS_CHARGING;
1410 else
1411 status = POWER_SUPPLY_STATUS_DISCHARGING;
1412 break;
1413 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1414 default:
1415 if (capacity < hidpp->battery.capacity)
1416 status = POWER_SUPPLY_STATUS_DISCHARGING;
1417 else
1418 status = POWER_SUPPLY_STATUS_CHARGING;
1419
1420 }
1421
1422 if (capacity == 100)
1423 status = POWER_SUPPLY_STATUS_FULL;
1424
1425 hidpp->battery.online = true;
1426 if (capacity != hidpp->battery.capacity ||
1427 status != hidpp->battery.status) {
1428 hidpp->battery.capacity = capacity;
1429 hidpp->battery.status = status;
1430 if (hidpp->battery.ps)
1431 power_supply_changed(hidpp->battery.ps);
1432 }
1433
1434 return 0;
1435}
1436
1437/* -------------------------------------------------------------------------- */
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04001438/* 0x6010: Touchpad FW items */
1439/* -------------------------------------------------------------------------- */
1440
1441#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1442
1443#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1444
1445struct hidpp_touchpad_fw_items {
1446 uint8_t presence;
1447 uint8_t desired_state;
1448 uint8_t state;
1449 uint8_t persistent;
1450};
1451
1452/**
1453 * send a set state command to the device by reading the current items->state
1454 * field. items is then filled with the current state.
1455 */
1456static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1457 u8 feature_index,
1458 struct hidpp_touchpad_fw_items *items)
1459{
1460 struct hidpp_report response;
1461 int ret;
1462 u8 *params = (u8 *)response.fap.params;
1463
1464 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1465 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1466
1467 if (ret > 0) {
1468 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1469 __func__, ret);
1470 return -EPROTO;
1471 }
1472 if (ret)
1473 return ret;
1474
1475 items->presence = params[0];
1476 items->desired_state = params[1];
1477 items->state = params[2];
1478 items->persistent = params[3];
1479
1480 return 0;
1481}
1482
1483/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001484/* 0x6100: TouchPadRawXY */
1485/* -------------------------------------------------------------------------- */
1486
1487#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1488
1489#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001490#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1491
1492#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001493
1494#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1495#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1496
1497struct hidpp_touchpad_raw_info {
1498 u16 x_size;
1499 u16 y_size;
1500 u8 z_range;
1501 u8 area_range;
1502 u8 timestamp_unit;
1503 u8 maxcontacts;
1504 u8 origin;
1505 u16 res;
1506};
1507
1508struct hidpp_touchpad_raw_xy_finger {
1509 u8 contact_type;
1510 u8 contact_status;
1511 u16 x;
1512 u16 y;
1513 u8 z;
1514 u8 area;
1515 u8 finger_id;
1516};
1517
1518struct hidpp_touchpad_raw_xy {
1519 u16 timestamp;
1520 struct hidpp_touchpad_raw_xy_finger fingers[2];
1521 u8 spurious_flag;
1522 u8 end_of_frame;
1523 u8 finger_count;
1524 u8 button;
1525};
1526
1527static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1528 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1529{
1530 struct hidpp_report response;
1531 int ret;
1532 u8 *params = (u8 *)response.fap.params;
1533
1534 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1535 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1536
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001537 if (ret > 0) {
1538 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1539 __func__, ret);
1540 return -EPROTO;
1541 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001542 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001543 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001544
1545 raw_info->x_size = get_unaligned_be16(&params[0]);
1546 raw_info->y_size = get_unaligned_be16(&params[2]);
1547 raw_info->z_range = params[4];
1548 raw_info->area_range = params[5];
1549 raw_info->maxcontacts = params[7];
1550 raw_info->origin = params[8];
1551 /* res is given in unit per inch */
1552 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1553
1554 return ret;
1555}
1556
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001557static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1558 u8 feature_index, bool send_raw_reports,
1559 bool sensor_enhanced_settings)
1560{
1561 struct hidpp_report response;
1562
1563 /*
1564 * Params:
1565 * bit 0 - enable raw
1566 * bit 1 - 16bit Z, no area
1567 * bit 2 - enhanced sensitivity
1568 * bit 3 - width, height (4 bits each) instead of area
1569 * bit 4 - send raw + gestures (degrades smoothness)
1570 * remaining bits - reserved
1571 */
1572 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1573
1574 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1575 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1576}
1577
1578static void hidpp_touchpad_touch_event(u8 *data,
1579 struct hidpp_touchpad_raw_xy_finger *finger)
1580{
1581 u8 x_m = data[0] << 2;
1582 u8 y_m = data[2] << 2;
1583
1584 finger->x = x_m << 6 | data[1];
1585 finger->y = y_m << 6 | data[3];
1586
1587 finger->contact_type = data[0] >> 6;
1588 finger->contact_status = data[2] >> 6;
1589
1590 finger->z = data[4];
1591 finger->area = data[5];
1592 finger->finger_id = data[6] >> 4;
1593}
1594
1595static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1596 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1597{
1598 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1599 raw_xy->end_of_frame = data[8] & 0x01;
1600 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1601 raw_xy->finger_count = data[15] & 0x0f;
1602 raw_xy->button = (data[8] >> 2) & 0x01;
1603
1604 if (raw_xy->finger_count) {
1605 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1606 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1607 }
1608}
1609
Edwin Veldsff21a632016-01-11 00:25:15 +01001610/* -------------------------------------------------------------------------- */
1611/* 0x8123: Force feedback support */
1612/* -------------------------------------------------------------------------- */
1613
1614#define HIDPP_FF_GET_INFO 0x01
1615#define HIDPP_FF_RESET_ALL 0x11
1616#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1617#define HIDPP_FF_SET_EFFECT_STATE 0x31
1618#define HIDPP_FF_DESTROY_EFFECT 0x41
1619#define HIDPP_FF_GET_APERTURE 0x51
1620#define HIDPP_FF_SET_APERTURE 0x61
1621#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1622#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1623
1624#define HIDPP_FF_EFFECT_STATE_GET 0x00
1625#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1626#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1627#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1628
1629#define HIDPP_FF_EFFECT_CONSTANT 0x00
1630#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1631#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1632#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1633#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1634#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1635#define HIDPP_FF_EFFECT_SPRING 0x06
1636#define HIDPP_FF_EFFECT_DAMPER 0x07
1637#define HIDPP_FF_EFFECT_FRICTION 0x08
1638#define HIDPP_FF_EFFECT_INERTIA 0x09
1639#define HIDPP_FF_EFFECT_RAMP 0x0A
1640
1641#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1642
1643#define HIDPP_FF_EFFECTID_NONE -1
1644#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1645
1646#define HIDPP_FF_MAX_PARAMS 20
1647#define HIDPP_FF_RESERVED_SLOTS 1
1648
1649struct hidpp_ff_private_data {
1650 struct hidpp_device *hidpp;
1651 u8 feature_index;
1652 u8 version;
1653 u16 gain;
1654 s16 range;
1655 u8 slot_autocenter;
1656 u8 num_effects;
1657 int *effect_ids;
1658 struct workqueue_struct *wq;
1659 atomic_t workqueue_size;
1660};
1661
1662struct hidpp_ff_work_data {
1663 struct work_struct work;
1664 struct hidpp_ff_private_data *data;
1665 int effect_id;
1666 u8 command;
1667 u8 params[HIDPP_FF_MAX_PARAMS];
1668 u8 size;
1669};
1670
Peter Huttererfef33602018-12-05 10:42:25 +10001671static const signed short hidpp_ff_effects[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001672 FF_CONSTANT,
1673 FF_PERIODIC,
1674 FF_SINE,
1675 FF_SQUARE,
1676 FF_SAW_UP,
1677 FF_SAW_DOWN,
1678 FF_TRIANGLE,
1679 FF_SPRING,
1680 FF_DAMPER,
1681 FF_AUTOCENTER,
1682 FF_GAIN,
1683 -1
1684};
1685
Peter Huttererfef33602018-12-05 10:42:25 +10001686static const signed short hidpp_ff_effects_v2[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001687 FF_RAMP,
1688 FF_FRICTION,
1689 FF_INERTIA,
1690 -1
1691};
1692
1693static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1694 HIDPP_FF_EFFECT_SPRING,
1695 HIDPP_FF_EFFECT_FRICTION,
1696 HIDPP_FF_EFFECT_DAMPER,
1697 HIDPP_FF_EFFECT_INERTIA
1698};
1699
1700static const char *HIDPP_FF_CONDITION_NAMES[] = {
1701 "spring",
1702 "friction",
1703 "damper",
1704 "inertia"
1705};
1706
1707
1708static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1709{
1710 int i;
1711
1712 for (i = 0; i < data->num_effects; i++)
1713 if (data->effect_ids[i] == effect_id)
1714 return i+1;
1715
1716 return 0;
1717}
1718
1719static void hidpp_ff_work_handler(struct work_struct *w)
1720{
1721 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1722 struct hidpp_ff_private_data *data = wd->data;
1723 struct hidpp_report response;
1724 u8 slot;
1725 int ret;
1726
1727 /* add slot number if needed */
1728 switch (wd->effect_id) {
1729 case HIDPP_FF_EFFECTID_AUTOCENTER:
1730 wd->params[0] = data->slot_autocenter;
1731 break;
1732 case HIDPP_FF_EFFECTID_NONE:
1733 /* leave slot as zero */
1734 break;
1735 default:
1736 /* find current slot for effect */
1737 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1738 break;
1739 }
1740
1741 /* send command and wait for reply */
1742 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1743 wd->command, wd->params, wd->size, &response);
1744
1745 if (ret) {
1746 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1747 goto out;
1748 }
1749
1750 /* parse return data */
1751 switch (wd->command) {
1752 case HIDPP_FF_DOWNLOAD_EFFECT:
1753 slot = response.fap.params[0];
1754 if (slot > 0 && slot <= data->num_effects) {
1755 if (wd->effect_id >= 0)
1756 /* regular effect uploaded */
1757 data->effect_ids[slot-1] = wd->effect_id;
1758 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1759 /* autocenter spring uploaded */
1760 data->slot_autocenter = slot;
1761 }
1762 break;
1763 case HIDPP_FF_DESTROY_EFFECT:
1764 if (wd->effect_id >= 0)
1765 /* regular effect destroyed */
1766 data->effect_ids[wd->params[0]-1] = -1;
1767 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1768 /* autocenter spring destoyed */
1769 data->slot_autocenter = 0;
1770 break;
1771 case HIDPP_FF_SET_GLOBAL_GAINS:
1772 data->gain = (wd->params[0] << 8) + wd->params[1];
1773 break;
1774 case HIDPP_FF_SET_APERTURE:
1775 data->range = (wd->params[0] << 8) + wd->params[1];
1776 break;
1777 default:
1778 /* no action needed */
1779 break;
1780 }
1781
1782out:
1783 atomic_dec(&data->workqueue_size);
1784 kfree(wd);
1785}
1786
1787static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1788{
1789 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1790 int s;
1791
1792 if (!wd)
1793 return -ENOMEM;
1794
1795 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1796
1797 wd->data = data;
1798 wd->effect_id = effect_id;
1799 wd->command = command;
1800 wd->size = size;
1801 memcpy(wd->params, params, size);
1802
1803 atomic_inc(&data->workqueue_size);
1804 queue_work(data->wq, &wd->work);
1805
1806 /* warn about excessive queue size */
1807 s = atomic_read(&data->workqueue_size);
1808 if (s >= 20 && s % 20 == 0)
1809 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1810
1811 return 0;
1812}
1813
1814static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1815{
1816 struct hidpp_ff_private_data *data = dev->ff->private;
1817 u8 params[20];
1818 u8 size;
1819 int force;
1820
1821 /* set common parameters */
1822 params[2] = effect->replay.length >> 8;
1823 params[3] = effect->replay.length & 255;
1824 params[4] = effect->replay.delay >> 8;
1825 params[5] = effect->replay.delay & 255;
1826
1827 switch (effect->type) {
1828 case FF_CONSTANT:
1829 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1830 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1831 params[6] = force >> 8;
1832 params[7] = force & 255;
1833 params[8] = effect->u.constant.envelope.attack_level >> 7;
1834 params[9] = effect->u.constant.envelope.attack_length >> 8;
1835 params[10] = effect->u.constant.envelope.attack_length & 255;
1836 params[11] = effect->u.constant.envelope.fade_level >> 7;
1837 params[12] = effect->u.constant.envelope.fade_length >> 8;
1838 params[13] = effect->u.constant.envelope.fade_length & 255;
1839 size = 14;
1840 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1841 effect->u.constant.level,
1842 effect->direction, force);
1843 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1844 effect->u.constant.envelope.attack_level,
1845 effect->u.constant.envelope.attack_length,
1846 effect->u.constant.envelope.fade_level,
1847 effect->u.constant.envelope.fade_length);
1848 break;
1849 case FF_PERIODIC:
1850 {
1851 switch (effect->u.periodic.waveform) {
1852 case FF_SINE:
1853 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1854 break;
1855 case FF_SQUARE:
1856 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1857 break;
1858 case FF_SAW_UP:
1859 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1860 break;
1861 case FF_SAW_DOWN:
1862 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1863 break;
1864 case FF_TRIANGLE:
1865 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1866 break;
1867 default:
1868 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1869 return -EINVAL;
1870 }
1871 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1872 params[6] = effect->u.periodic.magnitude >> 8;
1873 params[7] = effect->u.periodic.magnitude & 255;
1874 params[8] = effect->u.periodic.offset >> 8;
1875 params[9] = effect->u.periodic.offset & 255;
1876 params[10] = effect->u.periodic.period >> 8;
1877 params[11] = effect->u.periodic.period & 255;
1878 params[12] = effect->u.periodic.phase >> 8;
1879 params[13] = effect->u.periodic.phase & 255;
1880 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1881 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1882 params[16] = effect->u.periodic.envelope.attack_length & 255;
1883 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1884 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1885 params[19] = effect->u.periodic.envelope.fade_length & 255;
1886 size = 20;
1887 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1888 effect->u.periodic.magnitude, effect->direction,
1889 effect->u.periodic.offset,
1890 effect->u.periodic.period,
1891 effect->u.periodic.phase);
1892 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1893 effect->u.periodic.envelope.attack_level,
1894 effect->u.periodic.envelope.attack_length,
1895 effect->u.periodic.envelope.fade_level,
1896 effect->u.periodic.envelope.fade_length);
1897 break;
1898 }
1899 case FF_RAMP:
1900 params[1] = HIDPP_FF_EFFECT_RAMP;
1901 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1902 params[6] = force >> 8;
1903 params[7] = force & 255;
1904 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1905 params[8] = force >> 8;
1906 params[9] = force & 255;
1907 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1908 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1909 params[12] = effect->u.ramp.envelope.attack_length & 255;
1910 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1911 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1912 params[15] = effect->u.ramp.envelope.fade_length & 255;
1913 size = 16;
1914 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1915 effect->u.ramp.start_level,
1916 effect->u.ramp.end_level,
1917 effect->direction, force);
1918 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1919 effect->u.ramp.envelope.attack_level,
1920 effect->u.ramp.envelope.attack_length,
1921 effect->u.ramp.envelope.fade_level,
1922 effect->u.ramp.envelope.fade_length);
1923 break;
1924 case FF_FRICTION:
1925 case FF_INERTIA:
1926 case FF_SPRING:
1927 case FF_DAMPER:
1928 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1929 params[6] = effect->u.condition[0].left_saturation >> 9;
1930 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1931 params[8] = effect->u.condition[0].left_coeff >> 8;
1932 params[9] = effect->u.condition[0].left_coeff & 255;
1933 params[10] = effect->u.condition[0].deadband >> 9;
1934 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1935 params[12] = effect->u.condition[0].center >> 8;
1936 params[13] = effect->u.condition[0].center & 255;
1937 params[14] = effect->u.condition[0].right_coeff >> 8;
1938 params[15] = effect->u.condition[0].right_coeff & 255;
1939 params[16] = effect->u.condition[0].right_saturation >> 9;
1940 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1941 size = 18;
1942 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1943 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1944 effect->u.condition[0].left_coeff,
1945 effect->u.condition[0].left_saturation,
1946 effect->u.condition[0].right_coeff,
1947 effect->u.condition[0].right_saturation);
1948 dbg_hid(" deadband=%d, center=%d\n",
1949 effect->u.condition[0].deadband,
1950 effect->u.condition[0].center);
1951 break;
1952 default:
1953 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1954 return -EINVAL;
1955 }
1956
1957 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1958}
1959
1960static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1961{
1962 struct hidpp_ff_private_data *data = dev->ff->private;
1963 u8 params[2];
1964
1965 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1966
1967 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1968
1969 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1970}
1971
1972static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1973{
1974 struct hidpp_ff_private_data *data = dev->ff->private;
1975 u8 slot = 0;
1976
1977 dbg_hid("Erasing effect %d.\n", effect_id);
1978
1979 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1980}
1981
1982static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1983{
1984 struct hidpp_ff_private_data *data = dev->ff->private;
1985 u8 params[18];
1986
1987 dbg_hid("Setting autocenter to %d.\n", magnitude);
1988
1989 /* start a standard spring effect */
1990 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1991 /* zero delay and duration */
1992 params[2] = params[3] = params[4] = params[5] = 0;
1993 /* set coeff to 25% of saturation */
1994 params[8] = params[14] = magnitude >> 11;
1995 params[9] = params[15] = (magnitude >> 3) & 255;
1996 params[6] = params[16] = magnitude >> 9;
1997 params[7] = params[17] = (magnitude >> 1) & 255;
1998 /* zero deadband and center */
1999 params[10] = params[11] = params[12] = params[13] = 0;
2000
2001 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2002}
2003
2004static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2005{
2006 struct hidpp_ff_private_data *data = dev->ff->private;
2007 u8 params[4];
2008
2009 dbg_hid("Setting gain to %d.\n", gain);
2010
2011 params[0] = gain >> 8;
2012 params[1] = gain & 255;
2013 params[2] = 0; /* no boost */
2014 params[3] = 0;
2015
2016 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2017}
2018
2019static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2020{
2021 struct hid_device *hid = to_hid_device(dev);
2022 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2023 struct input_dev *idev = hidinput->input;
2024 struct hidpp_ff_private_data *data = idev->ff->private;
2025
2026 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2027}
2028
2029static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2030{
2031 struct hid_device *hid = to_hid_device(dev);
2032 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2033 struct input_dev *idev = hidinput->input;
2034 struct hidpp_ff_private_data *data = idev->ff->private;
2035 u8 params[2];
2036 int range = simple_strtoul(buf, NULL, 10);
2037
2038 range = clamp(range, 180, 900);
2039
2040 params[0] = range >> 8;
2041 params[1] = range & 0x00FF;
2042
2043 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2044
2045 return count;
2046}
2047
2048static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2049
2050static void hidpp_ff_destroy(struct ff_device *ff)
2051{
2052 struct hidpp_ff_private_data *data = ff->private;
2053
2054 kfree(data->effect_ids);
2055}
2056
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01002057static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
Edwin Veldsff21a632016-01-11 00:25:15 +01002058{
2059 struct hid_device *hid = hidpp->hid_dev;
2060 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2061 struct input_dev *dev = hidinput->input;
2062 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2063 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2064 struct ff_device *ff;
2065 struct hidpp_report response;
2066 struct hidpp_ff_private_data *data;
2067 int error, j, num_slots;
2068 u8 version;
2069
2070 if (!dev) {
2071 hid_err(hid, "Struct input_dev not set!\n");
2072 return -EINVAL;
2073 }
2074
2075 /* Get firmware release */
2076 version = bcdDevice & 255;
2077
2078 /* Set supported force feedback capabilities */
Peter Huttererfef33602018-12-05 10:42:25 +10002079 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2080 set_bit(hidpp_ff_effects[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002081 if (version > 1)
Peter Huttererfef33602018-12-05 10:42:25 +10002082 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2083 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002084
2085 /* Read number of slots available in device */
2086 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2087 HIDPP_FF_GET_INFO, NULL, 0, &response);
2088 if (error) {
2089 if (error < 0)
2090 return error;
2091 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2092 __func__, error);
2093 return -EPROTO;
2094 }
2095
2096 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2097
2098 error = input_ff_create(dev, num_slots);
2099
2100 if (error) {
2101 hid_err(dev, "Failed to create FF device!\n");
2102 return error;
2103 }
2104
2105 data = kzalloc(sizeof(*data), GFP_KERNEL);
2106 if (!data)
2107 return -ENOMEM;
2108 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2109 if (!data->effect_ids) {
2110 kfree(data);
2111 return -ENOMEM;
2112 }
Kangjie Lu6c44b152019-03-14 00:24:02 -05002113 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2114 if (!data->wq) {
2115 kfree(data->effect_ids);
2116 kfree(data);
2117 return -ENOMEM;
2118 }
2119
Edwin Veldsff21a632016-01-11 00:25:15 +01002120 data->hidpp = hidpp;
2121 data->feature_index = feature_index;
2122 data->version = version;
2123 data->slot_autocenter = 0;
2124 data->num_effects = num_slots;
2125 for (j = 0; j < num_slots; j++)
2126 data->effect_ids[j] = -1;
2127
2128 ff = dev->ff;
2129 ff->private = data;
2130
2131 ff->upload = hidpp_ff_upload_effect;
2132 ff->erase = hidpp_ff_erase_effect;
2133 ff->playback = hidpp_ff_playback;
2134 ff->set_gain = hidpp_ff_set_gain;
2135 ff->set_autocenter = hidpp_ff_set_autocenter;
2136 ff->destroy = hidpp_ff_destroy;
2137
2138
2139 /* reset all forces */
2140 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2141 HIDPP_FF_RESET_ALL, NULL, 0, &response);
2142
2143 /* Read current Range */
2144 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2145 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
2146 if (error)
2147 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
2148 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
2149
2150 /* Create sysfs interface */
2151 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2152 if (error)
2153 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2154
2155 /* Read the current gain values */
2156 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2157 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
2158 if (error)
2159 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
2160 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
2161 /* ignore boost value at response.fap.params[2] */
2162
2163 /* init the hardware command queue */
Edwin Veldsff21a632016-01-11 00:25:15 +01002164 atomic_set(&data->workqueue_size, 0);
2165
2166 /* initialize with zero autocenter to get wheel in usable state */
2167 hidpp_ff_set_autocenter(dev, 0);
2168
Colin Ian Kingdf47b242017-07-14 14:37:12 +01002169 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2170 version);
Edwin Veldsff21a632016-01-11 00:25:15 +01002171
2172 return 0;
2173}
2174
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01002175static int hidpp_ff_deinit(struct hid_device *hid)
Edwin Veldsff21a632016-01-11 00:25:15 +01002176{
2177 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2178 struct input_dev *dev = hidinput->input;
2179 struct hidpp_ff_private_data *data;
2180
2181 if (!dev) {
2182 hid_err(hid, "Struct input_dev not found!\n");
2183 return -EINVAL;
2184 }
2185
2186 hid_info(hid, "Unloading HID++ force feedback.\n");
2187 data = dev->ff->private;
2188 if (!data) {
2189 hid_err(hid, "Private data not found!\n");
2190 return -EINVAL;
2191 }
2192
2193 destroy_workqueue(data->wq);
2194 device_remove_file(&hid->dev, &dev_attr_range);
2195
2196 return 0;
2197}
2198
2199
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002200/* ************************************************************************** */
2201/* */
2202/* Device Support */
2203/* */
2204/* ************************************************************************** */
2205
2206/* -------------------------------------------------------------------------- */
2207/* Touchpad HID++ devices */
2208/* -------------------------------------------------------------------------- */
2209
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002210#define WTP_MANUAL_RESOLUTION 39
2211
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002212struct wtp_data {
2213 struct input_dev *input;
2214 u16 x_size, y_size;
2215 u8 finger_count;
2216 u8 mt_feature_index;
2217 u8 button_feature_index;
2218 u8 maxcontacts;
2219 bool flip_y;
2220 unsigned int resolution;
2221};
2222
2223static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2224 struct hid_field *field, struct hid_usage *usage,
2225 unsigned long **bit, int *max)
2226{
2227 return -1;
2228}
2229
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002230static void wtp_populate_input(struct hidpp_device *hidpp,
2231 struct input_dev *input_dev, bool origin_is_hid_core)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002232{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002233 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002234
2235 __set_bit(EV_ABS, input_dev->evbit);
2236 __set_bit(EV_KEY, input_dev->evbit);
2237 __clear_bit(EV_REL, input_dev->evbit);
2238 __clear_bit(EV_LED, input_dev->evbit);
2239
2240 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2241 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2242 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2243 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2244
2245 /* Max pressure is not given by the devices, pick one */
2246 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2247
2248 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2249
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002250 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2251 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2252 else
2253 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002254
2255 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2256 INPUT_MT_DROP_UNUSED);
2257
2258 wd->input = input_dev;
2259}
2260
2261static void wtp_touch_event(struct wtp_data *wd,
2262 struct hidpp_touchpad_raw_xy_finger *touch_report)
2263{
2264 int slot;
2265
2266 if (!touch_report->finger_id || touch_report->contact_type)
2267 /* no actual data */
2268 return;
2269
2270 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2271
2272 input_mt_slot(wd->input, slot);
2273 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2274 touch_report->contact_status);
2275 if (touch_report->contact_status) {
2276 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2277 touch_report->x);
2278 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2279 wd->flip_y ? wd->y_size - touch_report->y :
2280 touch_report->y);
2281 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2282 touch_report->area);
2283 }
2284}
2285
2286static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2287 struct hidpp_touchpad_raw_xy *raw)
2288{
2289 struct wtp_data *wd = hidpp->private_data;
2290 int i;
2291
2292 for (i = 0; i < 2; i++)
2293 wtp_touch_event(wd, &(raw->fingers[i]));
2294
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002295 if (raw->end_of_frame &&
2296 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002297 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2298
2299 if (raw->end_of_frame || raw->finger_count <= 2) {
2300 input_mt_sync_frame(wd->input);
2301 input_sync(wd->input);
2302 }
2303}
2304
2305static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2306{
2307 struct wtp_data *wd = hidpp->private_data;
2308 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2309 (data[7] >> 4) * (data[7] >> 4)) / 2;
2310 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2311 (data[13] >> 4) * (data[13] >> 4)) / 2;
2312 struct hidpp_touchpad_raw_xy raw = {
2313 .timestamp = data[1],
2314 .fingers = {
2315 {
2316 .contact_type = 0,
2317 .contact_status = !!data[7],
2318 .x = get_unaligned_le16(&data[3]),
2319 .y = get_unaligned_le16(&data[5]),
2320 .z = c1_area,
2321 .area = c1_area,
2322 .finger_id = data[2],
2323 }, {
2324 .contact_type = 0,
2325 .contact_status = !!data[13],
2326 .x = get_unaligned_le16(&data[9]),
2327 .y = get_unaligned_le16(&data[11]),
2328 .z = c2_area,
2329 .area = c2_area,
2330 .finger_id = data[8],
2331 }
2332 },
2333 .finger_count = wd->maxcontacts,
2334 .spurious_flag = 0,
2335 .end_of_frame = (data[0] >> 7) == 0,
2336 .button = data[0] & 0x01,
2337 };
2338
2339 wtp_send_raw_xy_event(hidpp, &raw);
2340
2341 return 1;
2342}
2343
2344static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2345{
2346 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2347 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002348 struct hidpp_report *report = (struct hidpp_report *)data;
2349 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002350
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002351 if (!wd || !wd->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002352 return 1;
2353
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002354 switch (data[0]) {
2355 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +01002356 if (size < 2) {
2357 hid_err(hdev, "Received HID report of bad size (%d)",
2358 size);
2359 return 1;
2360 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002361 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2362 input_event(wd->input, EV_KEY, BTN_LEFT,
2363 !!(data[1] & 0x01));
2364 input_event(wd->input, EV_KEY, BTN_RIGHT,
2365 !!(data[1] & 0x02));
2366 input_sync(wd->input);
Peter Wu8abd8202014-12-16 01:50:16 +01002367 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002368 } else {
2369 if (size < 21)
2370 return 1;
2371 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2372 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002373 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +01002374 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002375 if ((report->fap.feature_index != wd->mt_feature_index) ||
2376 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2377 return 1;
2378 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2379
2380 wtp_send_raw_xy_event(hidpp, &raw);
2381 return 0;
2382 }
2383
2384 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002385}
2386
2387static int wtp_get_config(struct hidpp_device *hidpp)
2388{
2389 struct wtp_data *wd = hidpp->private_data;
2390 struct hidpp_touchpad_raw_info raw_info = {0};
2391 u8 feature_type;
2392 int ret;
2393
2394 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2395 &wd->mt_feature_index, &feature_type);
2396 if (ret)
2397 /* means that the device is not powered up */
2398 return ret;
2399
2400 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2401 &raw_info);
2402 if (ret)
2403 return ret;
2404
2405 wd->x_size = raw_info.x_size;
2406 wd->y_size = raw_info.y_size;
2407 wd->maxcontacts = raw_info.maxcontacts;
2408 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2409 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002410 if (!wd->resolution)
2411 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002412
2413 return 0;
2414}
2415
2416static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2417{
2418 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2419 struct wtp_data *wd;
2420
2421 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2422 GFP_KERNEL);
2423 if (!wd)
2424 return -ENOMEM;
2425
2426 hidpp->private_data = wd;
2427
2428 return 0;
2429};
2430
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002431static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002432{
2433 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2434 struct wtp_data *wd = hidpp->private_data;
2435 int ret;
2436
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002437 if (!wd->x_size) {
2438 ret = wtp_get_config(hidpp);
2439 if (ret) {
2440 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002441 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002442 }
2443 }
2444
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002445 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002446 true, true);
2447}
2448
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002449/* ------------------------------------------------------------------------- */
2450/* Logitech M560 devices */
2451/* ------------------------------------------------------------------------- */
2452
2453/*
2454 * Logitech M560 protocol overview
2455 *
2456 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2457 * the sides buttons are pressed, it sends some keyboard keys events
2458 * instead of buttons ones.
2459 * To complicate things further, the middle button keys sequence
2460 * is different from the odd press and the even press.
2461 *
2462 * forward button -> Super_R
2463 * backward button -> Super_L+'d' (press only)
2464 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2465 * 2nd time: left-click (press only)
2466 * NB: press-only means that when the button is pressed, the
2467 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2468 * together sequentially; instead when the button is released, no event is
2469 * generated !
2470 *
2471 * With the command
2472 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2473 * the mouse reacts differently:
2474 * - it never sends a keyboard key event
2475 * - for the three mouse button it sends:
2476 * middle button press 11<xx>0a 3500af00...
2477 * side 1 button (forward) press 11<xx>0a 3500b000...
2478 * side 2 button (backward) press 11<xx>0a 3500ae00...
2479 * middle/side1/side2 button release 11<xx>0a 35000000...
2480 */
2481
2482static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2483
2484struct m560_private_data {
2485 struct input_dev *input;
2486};
2487
2488/* how buttons are mapped in the report */
2489#define M560_MOUSE_BTN_LEFT 0x01
2490#define M560_MOUSE_BTN_RIGHT 0x02
2491#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2492#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2493
2494#define M560_SUB_ID 0x0a
2495#define M560_BUTTON_MODE_REGISTER 0x35
2496
2497static int m560_send_config_command(struct hid_device *hdev, bool connected)
2498{
2499 struct hidpp_report response;
2500 struct hidpp_device *hidpp_dev;
2501
2502 hidpp_dev = hid_get_drvdata(hdev);
2503
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002504 return hidpp_send_rap_command_sync(
2505 hidpp_dev,
2506 REPORT_ID_HIDPP_SHORT,
2507 M560_SUB_ID,
2508 M560_BUTTON_MODE_REGISTER,
2509 (u8 *)m560_config_parameter,
2510 sizeof(m560_config_parameter),
2511 &response
2512 );
2513}
2514
2515static int m560_allocate(struct hid_device *hdev)
2516{
2517 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2518 struct m560_private_data *d;
2519
2520 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2521 GFP_KERNEL);
2522 if (!d)
2523 return -ENOMEM;
2524
2525 hidpp->private_data = d;
2526
2527 return 0;
2528};
2529
2530static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2531{
2532 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2533 struct m560_private_data *mydata = hidpp->private_data;
2534
2535 /* sanity check */
2536 if (!mydata || !mydata->input) {
2537 hid_err(hdev, "error in parameter\n");
2538 return -EINVAL;
2539 }
2540
2541 if (size < 7) {
2542 hid_err(hdev, "error in report\n");
2543 return 0;
2544 }
2545
2546 if (data[0] == REPORT_ID_HIDPP_LONG &&
2547 data[2] == M560_SUB_ID && data[6] == 0x00) {
2548 /*
2549 * m560 mouse report for middle, forward and backward button
2550 *
2551 * data[0] = 0x11
2552 * data[1] = device-id
2553 * data[2] = 0x0a
2554 * data[5] = 0xaf -> middle
2555 * 0xb0 -> forward
2556 * 0xae -> backward
2557 * 0x00 -> release all
2558 * data[6] = 0x00
2559 */
2560
2561 switch (data[5]) {
2562 case 0xaf:
2563 input_report_key(mydata->input, BTN_MIDDLE, 1);
2564 break;
2565 case 0xb0:
2566 input_report_key(mydata->input, BTN_FORWARD, 1);
2567 break;
2568 case 0xae:
2569 input_report_key(mydata->input, BTN_BACK, 1);
2570 break;
2571 case 0x00:
2572 input_report_key(mydata->input, BTN_BACK, 0);
2573 input_report_key(mydata->input, BTN_FORWARD, 0);
2574 input_report_key(mydata->input, BTN_MIDDLE, 0);
2575 break;
2576 default:
2577 hid_err(hdev, "error in report\n");
2578 return 0;
2579 }
2580 input_sync(mydata->input);
2581
2582 } else if (data[0] == 0x02) {
2583 /*
2584 * Logitech M560 mouse report
2585 *
2586 * data[0] = type (0x02)
2587 * data[1..2] = buttons
2588 * data[3..5] = xy
2589 * data[6] = wheel
2590 */
2591
2592 int v;
2593
2594 input_report_key(mydata->input, BTN_LEFT,
2595 !!(data[1] & M560_MOUSE_BTN_LEFT));
2596 input_report_key(mydata->input, BTN_RIGHT,
2597 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2598
Harry Cutts4435ff22018-12-05 10:42:27 +10002599 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002600 input_report_rel(mydata->input, REL_HWHEEL, -1);
Harry Cutts4435ff22018-12-05 10:42:27 +10002601 input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2602 -120);
2603 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002604 input_report_rel(mydata->input, REL_HWHEEL, 1);
Harry Cutts4435ff22018-12-05 10:42:27 +10002605 input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2606 120);
2607 }
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002608
2609 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2610 input_report_rel(mydata->input, REL_X, v);
2611
2612 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2613 input_report_rel(mydata->input, REL_Y, v);
2614
2615 v = hid_snto32(data[6], 8);
Peter Huttererfd357592019-03-20 08:48:23 +10002616 if (v != 0)
2617 hidpp_scroll_counter_handle_scroll(
2618 &hidpp->vertical_wheel_counter, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002619
2620 input_sync(mydata->input);
2621 }
2622
2623 return 1;
2624}
2625
2626static void m560_populate_input(struct hidpp_device *hidpp,
2627 struct input_dev *input_dev, bool origin_is_hid_core)
2628{
2629 struct m560_private_data *mydata = hidpp->private_data;
2630
2631 mydata->input = input_dev;
2632
2633 __set_bit(EV_KEY, mydata->input->evbit);
2634 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2635 __set_bit(BTN_RIGHT, mydata->input->keybit);
2636 __set_bit(BTN_LEFT, mydata->input->keybit);
2637 __set_bit(BTN_BACK, mydata->input->keybit);
2638 __set_bit(BTN_FORWARD, mydata->input->keybit);
2639
2640 __set_bit(EV_REL, mydata->input->evbit);
2641 __set_bit(REL_X, mydata->input->relbit);
2642 __set_bit(REL_Y, mydata->input->relbit);
2643 __set_bit(REL_WHEEL, mydata->input->relbit);
2644 __set_bit(REL_HWHEEL, mydata->input->relbit);
Harry Cutts4435ff22018-12-05 10:42:27 +10002645 __set_bit(REL_WHEEL_HI_RES, mydata->input->relbit);
2646 __set_bit(REL_HWHEEL_HI_RES, mydata->input->relbit);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002647}
2648
2649static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2650 struct hid_field *field, struct hid_usage *usage,
2651 unsigned long **bit, int *max)
2652{
2653 return -1;
2654}
2655
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002656/* ------------------------------------------------------------------------- */
2657/* Logitech K400 devices */
2658/* ------------------------------------------------------------------------- */
2659
2660/*
2661 * The Logitech K400 keyboard has an embedded touchpad which is seen
2662 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2663 * tap-to-click but the setting is not remembered accross reset, annoying some
2664 * users.
2665 *
2666 * We can toggle this feature from the host by using the feature 0x6010:
2667 * Touchpad FW items
2668 */
2669
2670struct k400_private_data {
2671 u8 feature_index;
2672};
2673
2674static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2675{
2676 struct k400_private_data *k400 = hidpp->private_data;
2677 struct hidpp_touchpad_fw_items items = {};
2678 int ret;
2679 u8 feature_type;
2680
2681 if (!k400->feature_index) {
2682 ret = hidpp_root_get_feature(hidpp,
2683 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2684 &k400->feature_index, &feature_type);
2685 if (ret)
2686 /* means that the device is not powered up */
2687 return ret;
2688 }
2689
2690 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2691 if (ret)
2692 return ret;
2693
2694 return 0;
2695}
2696
2697static int k400_allocate(struct hid_device *hdev)
2698{
2699 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2700 struct k400_private_data *k400;
2701
2702 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2703 GFP_KERNEL);
2704 if (!k400)
2705 return -ENOMEM;
2706
2707 hidpp->private_data = k400;
2708
2709 return 0;
2710};
2711
2712static int k400_connect(struct hid_device *hdev, bool connected)
2713{
2714 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2715
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002716 if (!disable_tap_to_click)
2717 return 0;
2718
2719 return k400_disable_tap_to_click(hidpp);
2720}
2721
Simon Wood7f4b49f2015-11-19 16:42:13 -07002722/* ------------------------------------------------------------------------- */
2723/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2724/* ------------------------------------------------------------------------- */
2725
2726#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2727
Simon Wood7f4b49f2015-11-19 16:42:13 -07002728static int g920_get_config(struct hidpp_device *hidpp)
2729{
Simon Wood7f4b49f2015-11-19 16:42:13 -07002730 u8 feature_type;
2731 u8 feature_index;
2732 int ret;
2733
Simon Wood7f4b49f2015-11-19 16:42:13 -07002734 /* Find feature and store for later use */
2735 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2736 &feature_index, &feature_type);
2737 if (ret)
2738 return ret;
2739
Edwin Veldsff21a632016-01-11 00:25:15 +01002740 ret = hidpp_ff_init(hidpp, feature_index);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002741 if (ret)
Edwin Veldsff21a632016-01-11 00:25:15 +01002742 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2743 ret);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002744
2745 return 0;
2746}
2747
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002748/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10002749/* High-resolution scroll wheels */
2750/* -------------------------------------------------------------------------- */
2751
2752static int hi_res_scroll_enable(struct hidpp_device *hidpp)
2753{
2754 int ret;
2755 u8 multiplier = 1;
2756
2757 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
2758 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
2759 if (ret == 0)
2760 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
2761 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
2762 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
2763 &multiplier);
2764 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
2765 ret = hidpp10_enable_scrolling_acceleration(hidpp);
2766 multiplier = 8;
2767 }
2768 if (ret)
2769 return ret;
2770
2771 if (multiplier == 0)
2772 multiplier = 1;
2773
2774 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
2775 hid_info(hidpp->hid_dev, "multiplier = %d\n", multiplier);
2776 return 0;
2777}
2778
2779/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002780/* Generic HID++ devices */
2781/* -------------------------------------------------------------------------- */
2782
2783static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2784 struct hid_field *field, struct hid_usage *usage,
2785 unsigned long **bit, int *max)
2786{
2787 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2788
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002789 if (!hidpp)
2790 return 0;
2791
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002792 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2793 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002794 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2795 field->application != HID_GD_MOUSE)
2796 return m560_input_mapping(hdev, hi, field, usage, bit, max);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002797
2798 return 0;
2799}
2800
Simon Wood0b1804e2015-11-19 16:42:15 -07002801static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2802 struct hid_field *field, struct hid_usage *usage,
2803 unsigned long **bit, int *max)
2804{
2805 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2806
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002807 if (!hidpp)
2808 return 0;
2809
Simon Wood0b1804e2015-11-19 16:42:15 -07002810 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2811 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2812 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2813 usage->code == ABS_Y || usage->code == ABS_Z ||
2814 usage->code == ABS_RZ)) {
2815 field->application = HID_GD_MULTIAXIS;
2816 }
2817 }
2818
2819 return 0;
2820}
2821
2822
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002823static void hidpp_populate_input(struct hidpp_device *hidpp,
2824 struct input_dev *input, bool origin_is_hid_core)
2825{
2826 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2827 wtp_populate_input(hidpp, input, origin_is_hid_core);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002828 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2829 m560_populate_input(hidpp, input, origin_is_hid_core);
Harry Cutts4435ff22018-12-05 10:42:27 +10002830
2831 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
2832 hidpp->vertical_wheel_counter.dev = input;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002833}
2834
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002835static int hidpp_input_configured(struct hid_device *hdev,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002836 struct hid_input *hidinput)
2837{
2838 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002839 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002840
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002841 if (!hidpp)
2842 return 0;
2843
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002844 hidpp_populate_input(hidpp, input, true);
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002845
2846 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002847}
2848
2849static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2850 int size)
2851{
2852 struct hidpp_report *question = hidpp->send_receive_buf;
2853 struct hidpp_report *answer = hidpp->send_receive_buf;
2854 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002855 int ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002856
2857 /*
2858 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +01002859 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002860 */
2861 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2862 /*
2863 * Check for a correct hidpp20 answer or the corresponding
2864 * error
2865 */
2866 if (hidpp_match_answer(question, report) ||
2867 hidpp_match_error(question, report)) {
2868 *answer = *report;
2869 hidpp->answer_available = true;
2870 wake_up(&hidpp->wait);
2871 /*
2872 * This was an answer to a command that this driver sent
2873 * We return 1 to hid-core to avoid forwarding the
2874 * command upstream as it has been treated by the driver
2875 */
2876
2877 return 1;
2878 }
2879 }
2880
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002881 if (unlikely(hidpp_report_is_connect_event(report))) {
2882 atomic_set(&hidpp->connected,
2883 !(report->rap.params[0] & (1 << 6)));
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002884 if (schedule_work(&hidpp->work) == 0)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002885 dbg_hid("%s: connect event already queued\n", __func__);
2886 return 1;
2887 }
2888
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002889 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2890 ret = hidpp20_battery_event(hidpp, data, size);
2891 if (ret != 0)
2892 return ret;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02002893 ret = hidpp_solar_battery_event(hidpp, data, size);
2894 if (ret != 0)
2895 return ret;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002896 }
2897
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02002898 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2899 ret = hidpp10_battery_event(hidpp, data, size);
2900 if (ret != 0)
2901 return ret;
2902 }
2903
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002904 return 0;
2905}
2906
2907static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2908 u8 *data, int size)
2909{
2910 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01002911 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002912
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002913 if (!hidpp)
2914 return 0;
2915
Peter Wue529fea2014-12-17 00:23:51 +01002916 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002917 switch (data[0]) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07002918 case REPORT_ID_HIDPP_VERY_LONG:
2919 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2920 hid_err(hdev, "received hid++ report of bad size (%d)",
2921 size);
2922 return 1;
2923 }
2924 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2925 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002926 case REPORT_ID_HIDPP_LONG:
2927 if (size != HIDPP_REPORT_LONG_LENGTH) {
2928 hid_err(hdev, "received hid++ report of bad size (%d)",
2929 size);
2930 return 1;
2931 }
Peter Wue529fea2014-12-17 00:23:51 +01002932 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2933 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002934 case REPORT_ID_HIDPP_SHORT:
2935 if (size != HIDPP_REPORT_SHORT_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 }
2943
Peter Wue529fea2014-12-17 00:23:51 +01002944 /* If no report is available for further processing, skip calling
2945 * raw_event of subclasses. */
2946 if (ret != 0)
2947 return ret;
2948
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002949 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2950 return wtp_raw_event(hdev, data, size);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002951 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2952 return m560_raw_event(hdev, data, size);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002953
2954 return 0;
2955}
2956
Harry Cutts4435ff22018-12-05 10:42:27 +10002957static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
2958 struct hid_usage *usage, __s32 value)
2959{
2960 /* This function will only be called for scroll events, due to the
2961 * restriction imposed in hidpp_usages.
2962 */
2963 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02002964 struct hidpp_scroll_counter *counter;
2965
2966 if (!hidpp)
2967 return 0;
2968
2969 counter = &hidpp->vertical_wheel_counter;
Harry Cutts4435ff22018-12-05 10:42:27 +10002970 /* A scroll event may occur before the multiplier has been retrieved or
2971 * the input device set, or high-res scroll enabling may fail. In such
2972 * cases we must return early (falling back to default behaviour) to
2973 * avoid a crash in hidpp_scroll_counter_handle_scroll.
2974 */
2975 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
2976 || counter->dev == NULL || counter->wheel_multiplier == 0)
2977 return 0;
2978
2979 hidpp_scroll_counter_handle_scroll(counter, value);
2980 return 1;
2981}
2982
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002983static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2984{
2985 static atomic_t battery_no = ATOMIC_INIT(0);
2986 struct power_supply_config cfg = { .drv_data = hidpp };
2987 struct power_supply_desc *desc = &hidpp->battery.desc;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002988 enum power_supply_property *battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002989 struct hidpp_battery *battery;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002990 unsigned int num_battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002991 unsigned long n;
2992 int ret;
2993
2994 if (hidpp->battery.ps)
2995 return 0;
2996
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02002997 hidpp->battery.feature_index = 0xff;
2998 hidpp->battery.solar_feature_index = 0xff;
2999
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003000 if (hidpp->protocol_major >= 2) {
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003001 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3002 ret = hidpp_solar_request_battery_event(hidpp);
3003 else
3004 ret = hidpp20_query_battery_info(hidpp);
3005
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003006 if (ret)
3007 return ret;
3008 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3009 } else {
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003010 ret = hidpp10_query_battery_status(hidpp);
3011 if (ret) {
3012 ret = hidpp10_query_battery_mileage(hidpp);
3013 if (ret)
3014 return -ENOENT;
3015 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3016 } else {
3017 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3018 }
3019 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003020 }
3021
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003022 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3023 hidpp_battery_props,
3024 sizeof(hidpp_battery_props),
3025 GFP_KERNEL);
Gustavo A. R. Silva929b60a2017-07-07 00:12:13 -05003026 if (!battery_props)
3027 return -ENOMEM;
3028
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003029 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
3030
3031 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3032 battery_props[num_battery_props++] =
3033 POWER_SUPPLY_PROP_CAPACITY;
3034
3035 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3036 battery_props[num_battery_props++] =
3037 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3038
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003039 battery = &hidpp->battery;
3040
3041 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003042 desc->properties = battery_props;
3043 desc->num_properties = num_battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02003044 desc->get_property = hidpp_battery_get_property;
3045 sprintf(battery->name, "hidpp_battery_%ld", n);
3046 desc->name = battery->name;
3047 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3048 desc->use_for_apm = 0;
3049
3050 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3051 &battery->desc,
3052 &cfg);
3053 if (IS_ERR(battery->ps))
3054 return PTR_ERR(battery->ps);
3055
3056 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3057
3058 return ret;
3059}
3060
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003061static void hidpp_overwrite_name(struct hid_device *hdev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003062{
3063 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3064 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003065
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003066 if (hidpp->protocol_major < 2)
Benjamin Tissoiresb4f8ce02017-03-27 16:59:24 +02003067 return;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003068
3069 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003070
Simon Wood7bfd2922015-11-19 16:42:12 -07003071 if (!name) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003072 hid_err(hdev, "unable to retrieve the name of the device");
Simon Wood7bfd2922015-11-19 16:42:12 -07003073 } else {
3074 dbg_hid("HID++: Got name: %s\n", name);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003075 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
Simon Wood7bfd2922015-11-19 16:42:12 -07003076 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003077
3078 kfree(name);
3079}
3080
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003081static int hidpp_input_open(struct input_dev *dev)
3082{
3083 struct hid_device *hid = input_get_drvdata(dev);
3084
3085 return hid_hw_open(hid);
3086}
3087
3088static void hidpp_input_close(struct input_dev *dev)
3089{
3090 struct hid_device *hid = input_get_drvdata(dev);
3091
3092 hid_hw_close(hid);
3093}
3094
3095static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3096{
3097 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003098 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003099
3100 if (!input_dev)
3101 return NULL;
3102
3103 input_set_drvdata(input_dev, hdev);
3104 input_dev->open = hidpp_input_open;
3105 input_dev->close = hidpp_input_close;
3106
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003107 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003108 input_dev->phys = hdev->phys;
3109 input_dev->uniq = hdev->uniq;
3110 input_dev->id.bustype = hdev->bus;
3111 input_dev->id.vendor = hdev->vendor;
3112 input_dev->id.product = hdev->product;
3113 input_dev->id.version = hdev->version;
3114 input_dev->dev.parent = &hdev->dev;
3115
3116 return input_dev;
3117}
3118
3119static void hidpp_connect_event(struct hidpp_device *hidpp)
3120{
3121 struct hid_device *hdev = hidpp->hid_dev;
3122 int ret = 0;
3123 bool connected = atomic_read(&hidpp->connected);
3124 struct input_dev *input;
3125 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003126
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003127 if (!connected) {
3128 if (hidpp->battery.ps) {
3129 hidpp->battery.online = false;
3130 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003131 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003132 power_supply_changed(hidpp->battery.ps);
3133 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003134 return;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003135 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003136
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003137 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3138 ret = wtp_connect(hdev, connected);
3139 if (ret)
3140 return;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003141 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3142 ret = m560_send_config_command(hdev, connected);
3143 if (ret)
3144 return;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003145 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3146 ret = k400_connect(hdev, connected);
3147 if (ret)
3148 return;
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003149 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003150
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003151 /* the device is already connected, we can ask for its name and
3152 * protocol */
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003153 if (!hidpp->protocol_major) {
Hans de Goede090760d2019-03-22 08:41:39 +01003154 ret = hidpp_root_get_protocol_version(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003155 if (ret) {
3156 hid_err(hdev, "Can not get the protocol version.\n");
3157 return;
3158 }
3159 }
3160
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003161 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003162 name = hidpp_get_device_name(hidpp);
Hans de Goede2ddf07f2019-04-20 13:22:07 +02003163 if (name) {
3164 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3165 "%s", name);
3166 kfree(name);
3167 if (!devm_name)
3168 return;
3169
3170 hidpp->name = devm_name;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003171 }
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003172 }
3173
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003174 hidpp_initialize_battery(hidpp);
3175
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003176 /* forward current battery state */
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003177 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3178 hidpp10_enable_battery_reporting(hidpp);
3179 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3180 hidpp10_query_battery_mileage(hidpp);
3181 else
3182 hidpp10_query_battery_status(hidpp);
3183 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003184 hidpp20_query_battery_info(hidpp);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003185 }
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003186 if (hidpp->battery.ps)
3187 power_supply_changed(hidpp->battery.ps);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003188
Harry Cutts4435ff22018-12-05 10:42:27 +10003189 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3190 hi_res_scroll_enable(hidpp);
3191
Benjamin Tissoires29368362017-03-27 16:59:28 +02003192 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3193 /* if the input nodes are already created, we can stop now */
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003194 return;
3195
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003196 input = hidpp_allocate_input(hdev);
3197 if (!input) {
3198 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3199 return;
3200 }
3201
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003202 hidpp_populate_input(hidpp, input, false);
3203
3204 ret = input_register_device(input);
3205 if (ret)
3206 input_free_device(input);
3207
3208 hidpp->delayed_input = input;
3209}
3210
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003211static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3212
3213static struct attribute *sysfs_attrs[] = {
3214 &dev_attr_builtin_power_supply.attr,
3215 NULL
3216};
3217
Arvind Yadav35a33cb2017-08-03 16:59:04 +05303218static const struct attribute_group ps_attribute_group = {
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003219 .attrs = sysfs_attrs
3220};
3221
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003222static bool hidpp_validate_report(struct hid_device *hdev, int id, int size,
3223 bool optional)
3224{
3225 struct hid_report_enum *re;
3226 struct hid_report *report;
3227
3228 if (id >= HID_MAX_IDS || id < 0) {
3229 hid_err(hdev, "invalid HID report id %u\n", id);
3230 return false;
3231 }
3232
3233 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3234 report = re->report_id_hash[id];
3235
3236 if (!report)
3237 return optional;
3238
3239 if (report->field[0]->report_count < size) {
3240 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3241 return false;
3242 }
3243
3244 return true;
3245}
3246
3247static bool hidpp_validate_device(struct hid_device *hdev)
3248{
3249 return hidpp_validate_report(hdev, REPORT_ID_HIDPP_SHORT,
3250 HIDPP_REPORT_SHORT_LENGTH - 1, false) &&
3251 hidpp_validate_report(hdev, REPORT_ID_HIDPP_LONG,
3252 HIDPP_REPORT_LONG_LENGTH - 1, true) &&
3253 hidpp_validate_report(hdev, REPORT_ID_HIDPP_VERY_LONG,
3254 HIDPP_REPORT_VERY_LONG_LENGTH - 1, true);
3255}
3256
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003257static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3258{
3259 struct hidpp_device *hidpp;
3260 int ret;
3261 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003262 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003263
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003264 ret = hid_parse(hdev);
3265 if (ret) {
3266 hid_err(hdev, "%s:parse failed\n", __func__);
3267 return ret;
3268 }
3269
3270 /*
3271 * Make sure the device is HID++ capable, otherwise treat as generic HID
3272 */
3273 if (!hidpp_validate_device(hdev))
3274 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
3275
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003276 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
3277 GFP_KERNEL);
3278 if (!hidpp)
3279 return -ENOMEM;
3280
3281 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003282 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003283 hid_set_drvdata(hdev, hidpp);
3284
3285 hidpp->quirks = id->driver_data;
3286
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003287 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3288 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3289
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003290 if (disable_raw_mode) {
3291 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003292 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003293 }
3294
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003295 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3296 ret = wtp_allocate(hdev, id);
3297 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003298 return ret;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003299 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3300 ret = m560_allocate(hdev);
3301 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003302 return ret;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003303 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3304 ret = k400_allocate(hdev);
3305 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003306 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003307 }
3308
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003309 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003310 mutex_init(&hidpp->send_mutex);
3311 init_waitqueue_head(&hidpp->wait);
3312
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003313 /* indicates we are handling the battery properties in the kernel */
3314 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3315 if (ret)
3316 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3317 hdev->name);
3318
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003319 /*
3320 * Plain USB connections need to actually call start and open
3321 * on the transport driver to allow incoming data.
3322 */
3323 ret = hid_hw_start(hdev, 0);
3324 if (ret) {
3325 hid_err(hdev, "hw start failed\n");
3326 goto hid_hw_start_fail;
Simon Wood7bfd2922015-11-19 16:42:12 -07003327 }
3328
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003329 ret = hid_hw_open(hdev);
3330 if (ret < 0) {
3331 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3332 __func__, ret);
3333 hid_hw_stop(hdev);
3334 goto hid_hw_open_fail;
3335 }
Simon Wood7bfd2922015-11-19 16:42:12 -07003336
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003337 /* Allow incoming packets */
3338 hid_device_io_start(hdev);
3339
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003340 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3341 hidpp_unifying_init(hidpp);
3342
Hans de Goede090760d2019-03-22 08:41:39 +01003343 connected = hidpp_root_get_protocol_version(hidpp) == 0;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003344 atomic_set(&hidpp->connected, connected);
3345 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003346 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02003347 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003348 hid_err(hdev, "Device not connected");
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003349 goto hid_hw_init_fail;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003350 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003351
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003352 hidpp_overwrite_name(hdev);
3353 }
Benjamin Tissoires33797822014-09-30 13:18:30 -04003354
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003355 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003356 ret = wtp_get_config(hidpp);
3357 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003358 goto hid_hw_init_fail;
Simon Wood7f4b49f2015-11-19 16:42:13 -07003359 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3360 ret = g920_get_config(hidpp);
3361 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003362 goto hid_hw_init_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003363 }
3364
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003365 hidpp_connect_event(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003366
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003367 /* Reset the HID node state */
3368 hid_device_io_stop(hdev);
3369 hid_hw_close(hdev);
3370 hid_hw_stop(hdev);
3371
3372 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3373 connect_mask &= ~HID_CONNECT_HIDINPUT;
3374
3375 /* Now export the actual inputs and hidraw nodes to the world */
3376 ret = hid_hw_start(hdev, connect_mask);
3377 if (ret) {
3378 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3379 goto hid_hw_start_fail;
3380 }
3381
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003382 return ret;
3383
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003384hid_hw_init_fail:
3385 hid_hw_close(hdev);
3386hid_hw_open_fail:
3387 hid_hw_stop(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003388hid_hw_start_fail:
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003389 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003390 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003391 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003392 return ret;
3393}
3394
3395static void hidpp_remove(struct hid_device *hdev)
3396{
3397 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3398
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003399 if (!hidpp)
3400 return hid_hw_stop(hdev);
3401
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003402 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3403
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003404 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)
Edwin Veldsff21a632016-01-11 00:25:15 +01003405 hidpp_ff_deinit(hdev);
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003406
Simon Wood7bfd2922015-11-19 16:42:12 -07003407 hid_hw_stop(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003408 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003409 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003410}
3411
Harry Cutts4435ff22018-12-05 10:42:27 +10003412#define LDJ_DEVICE(product) \
3413 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3414 USB_VENDOR_ID_LOGITECH, (product))
3415
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003416static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04003417 { /* wireless touchpad */
Harry Cutts16767222018-12-05 10:42:28 +10003418 LDJ_DEVICE(0x4011),
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04003419 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3420 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003421 { /* wireless touchpad T650 */
Harry Cutts16767222018-12-05 10:42:28 +10003422 LDJ_DEVICE(0x4101),
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003423 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003424 { /* wireless touchpad T651 */
3425 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3426 USB_DEVICE_ID_LOGITECH_T651),
3427 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Harry Cutts4435ff22018-12-05 10:42:27 +10003428 { /* Mouse Logitech Anywhere MX */
3429 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3430 { /* Mouse Logitech Cube */
3431 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3432 { /* Mouse Logitech M335 */
3433 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3434 { /* Mouse Logitech M515 */
3435 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003436 { /* Mouse logitech M560 */
Harry Cutts4435ff22018-12-05 10:42:27 +10003437 LDJ_DEVICE(0x402d),
3438 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
3439 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3440 { /* Mouse Logitech M705 (firmware RQM17) */
3441 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3442 { /* Mouse Logitech M705 (firmware RQM67) */
3443 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3444 { /* Mouse Logitech M720 */
3445 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3446 { /* Mouse Logitech MX Anywhere 2 */
3447 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3448 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3449 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3450 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3451 { /* Mouse Logitech MX Anywhere 2S */
3452 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3453 { /* Mouse Logitech MX Master */
3454 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3455 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3456 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3457 { /* Mouse Logitech MX Master 2S */
3458 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3459 { /* Mouse Logitech Performance MX */
3460 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003461 { /* Keyboard logitech K400 */
Harry Cutts16767222018-12-05 10:42:28 +10003462 LDJ_DEVICE(0x4024),
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003463 .driver_data = HIDPP_QUIRK_CLASS_K400 },
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003464 { /* Solar Keyboard Logitech K750 */
Harry Cutts16767222018-12-05 10:42:28 +10003465 LDJ_DEVICE(0x4002),
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003466 .driver_data = HIDPP_QUIRK_CLASS_K750 },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003467
Harry Cutts16767222018-12-05 10:42:28 +10003468 { LDJ_DEVICE(HID_ANY_ID) },
Simon Wood7bfd2922015-11-19 16:42:12 -07003469
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003470 { /* Logitech G403 Gaming Mouse over USB */
3471 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
3472 { /* Logitech G700 Gaming Mouse over USB */
3473 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC06B) },
3474 { /* Logitech G900 Gaming Mouse over USB */
3475 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
3476 { /* Logitech G920 Wheel over USB */
3477 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
Simon Wood7bfd2922015-11-19 16:42:12 -07003478 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003479 {}
3480};
3481
3482MODULE_DEVICE_TABLE(hid, hidpp_devices);
3483
Harry Cutts4435ff22018-12-05 10:42:27 +10003484static const struct hid_usage_id hidpp_usages[] = {
3485 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
3486 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
3487};
3488
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003489static struct hid_driver hidpp_driver = {
3490 .name = "logitech-hidpp-device",
3491 .id_table = hidpp_devices,
3492 .probe = hidpp_probe,
3493 .remove = hidpp_remove,
3494 .raw_event = hidpp_raw_event,
Harry Cutts4435ff22018-12-05 10:42:27 +10003495 .usage_table = hidpp_usages,
3496 .event = hidpp_event,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003497 .input_configured = hidpp_input_configured,
3498 .input_mapping = hidpp_input_mapping,
Simon Wood0b1804e2015-11-19 16:42:15 -07003499 .input_mapped = hidpp_input_mapped,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003500};
3501
3502module_hid_driver(hidpp_driver);