blob: 2e32a21bbcbfc576199fb9b59543fc140c0a4df7 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002/*
Filipe Laínsc08ce252020-01-12 23:50:09 +00003 * HIDPP protocol for Logitech receivers
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004 *
5 * Copyright (c) 2011 Logitech (c)
6 * Copyright (c) 2012-2013 Google (c)
7 * Copyright (c) 2013-2014 Red Hat Inc.
8 */
9
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/device.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010014#include <linux/input.h>
15#include <linux/usb.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040016#include <linux/hid.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
Harry Cutts4435ff22018-12-05 10:42:27 +100020#include <linux/sched/clock.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040021#include <linux/kfifo.h>
22#include <linux/input/mt.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010023#include <linux/workqueue.h>
24#include <linux/atomic.h>
25#include <linux/fixp-arith.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040026#include <asm/unaligned.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010027#include "usbhid/usbhid.h"
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040028#include "hid-ids.h"
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
33
Benjamin Tissoires9188dba2015-03-26 12:41:57 -040034static bool disable_raw_mode;
35module_param(disable_raw_mode, bool, 0644);
36MODULE_PARM_DESC(disable_raw_mode,
37 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
38
Benjamin Tissoires90cdd982015-09-03 09:08:30 -040039static bool disable_tap_to_click;
40module_param(disable_tap_to_click, bool, 0644);
41MODULE_PARM_DESC(disable_tap_to_click,
42 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
43
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040044#define REPORT_ID_HIDPP_SHORT 0x10
45#define REPORT_ID_HIDPP_LONG 0x11
Simon Wooda5ce8f52015-11-19 16:42:11 -070046#define REPORT_ID_HIDPP_VERY_LONG 0x12
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040047
48#define HIDPP_REPORT_SHORT_LENGTH 7
49#define HIDPP_REPORT_LONG_LENGTH 20
Hans de Goeded71b18f2019-04-20 13:22:12 +020050#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040051
Mazin Rezkc2a93272019-10-27 17:44:06 +000052#define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
53#define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
54#define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
55
Hans de Goede42bc4f32019-04-20 13:22:17 +020056#define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
Hans de Goede4a79bcc2019-04-20 13:22:15 +020057#define HIDPP_SUB_ID_ROLLER 0x05
58#define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
59
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040060#define HIDPP_QUIRK_CLASS_WTP BIT(0)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +020061#define HIDPP_QUIRK_CLASS_M560 BIT(1)
Benjamin Tissoires90cdd982015-09-03 09:08:30 -040062#define HIDPP_QUIRK_CLASS_K400 BIT(2)
Simon Wood7bfd2922015-11-19 16:42:12 -070063#define HIDPP_QUIRK_CLASS_G920 BIT(3)
Benjamin Tissoires696ecef2017-03-27 16:59:37 +020064#define HIDPP_QUIRK_CLASS_K750 BIT(4)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040065
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +020066/* bits 2..20 are reserved for classes */
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100067/* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -040068#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040069#define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
Simon Wood7bfd2922015-11-19 16:42:12 -070070#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
Benjamin Tissoires843c6242017-03-27 16:59:26 +020071#define HIDPP_QUIRK_UNIFYING BIT(25)
Harry Cutts4435ff22018-12-05 10:42:27 +100072#define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
73#define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
74#define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
Hans de Goede4a79bcc2019-04-20 13:22:15 +020075#define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
Hans de Goede7457bc12019-04-20 13:22:16 +020076#define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
Hans de Goede42bc4f32019-04-20 13:22:17 +020077#define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
Hans de Goede4a79bcc2019-04-20 13:22:15 +020078
79/* These are just aliases for now */
80#define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
81#define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
Harry Cutts4435ff22018-12-05 10:42:27 +100082
83/* Convenience constant to check for any high-res support. */
84#define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
85 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
86 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040087
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100088#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040089
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020090#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
91#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +020092#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
93#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
Pedro Vanzellabe281362019-10-26 19:25:06 -030094#define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020095
Hans de Goedeb4c00e72020-11-14 22:20:56 +010096#define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
97
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040098/*
99 * There are two hidpp protocols in use, the first version hidpp10 is known
100 * as register access protocol or RAP, the second version hidpp20 is known as
101 * feature access protocol or FAP
102 *
103 * Most older devices (including the Unifying usb receiver) use the RAP protocol
104 * where as most newer devices use the FAP protocol. Both protocols are
105 * compatible with the underlying transport, which could be usb, Unifiying, or
106 * bluetooth. The message lengths are defined by the hid vendor specific report
107 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
108 * the HIDPP_LONG report type (total message length 20 bytes)
109 *
110 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
111 * messages. The Unifying receiver itself responds to RAP messages (device index
112 * is 0xFF for the receiver), and all messages (short or long) with a device
113 * index between 1 and 6 are passed untouched to the corresponding paired
114 * Unifying device.
115 *
116 * The paired device can be RAP or FAP, it will receive the message untouched
117 * from the Unifiying receiver.
118 */
119
120struct fap {
121 u8 feature_index;
122 u8 funcindex_clientid;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200123 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400124};
125
126struct rap {
127 u8 sub_id;
128 u8 reg_address;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200129 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400130};
131
132struct hidpp_report {
133 u8 report_id;
134 u8 device_index;
135 union {
136 struct fap fap;
137 struct rap rap;
138 u8 rawbytes[sizeof(struct fap)];
139 };
140} __packed;
141
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000142struct hidpp_battery {
143 u8 feature_index;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +0200144 u8 solar_feature_index;
Pedro Vanzellabe281362019-10-26 19:25:06 -0300145 u8 voltage_feature_index;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000146 struct power_supply_desc desc;
147 struct power_supply *ps;
148 char name[64];
149 int status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200150 int capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200151 int level;
Pedro Vanzellabe281362019-10-26 19:25:06 -0300152 int voltage;
153 int charge_type;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200154 bool online;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000155};
156
Harry Cutts4435ff22018-12-05 10:42:27 +1000157/**
158 * struct hidpp_scroll_counter - Utility class for processing high-resolution
159 * scroll events.
160 * @dev: the input device for which events should be reported.
161 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
162 * @remainder: counts the number of high-resolution units moved since the last
163 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
164 * only be used by class methods.
165 * @direction: direction of last movement (1 or -1)
166 * @last_time: last event time, used to reset remainder after inactivity
167 */
168struct hidpp_scroll_counter {
Harry Cutts4435ff22018-12-05 10:42:27 +1000169 int wheel_multiplier;
170 int remainder;
171 int direction;
172 unsigned long long last_time;
173};
174
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400175struct hidpp_device {
176 struct hid_device *hid_dev;
Hans de Goede06104302019-04-20 13:22:13 +0200177 struct input_dev *input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400178 struct mutex send_mutex;
179 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -0500180 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400181 wait_queue_head_t wait;
Hans de Goeded71b18f2019-04-20 13:22:12 +0200182 int very_long_report_length;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400183 bool answer_available;
184 u8 protocol_major;
185 u8 protocol_minor;
186
187 void *private_data;
188
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400189 struct work_struct work;
190 struct kfifo delayed_work_fifo;
191 atomic_t connected;
192 struct input_dev *delayed_input;
193
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400194 unsigned long quirks;
Benjamin Tissoires206d7c62017-03-27 16:59:25 +0200195 unsigned long capabilities;
Mazin Rezkc2a93272019-10-27 17:44:06 +0000196 u8 supported_reports;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400197
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000198 struct hidpp_battery battery;
Harry Cutts4435ff22018-12-05 10:42:27 +1000199 struct hidpp_scroll_counter vertical_wheel_counter;
Mazin Rezk0da0a63b2019-10-27 17:44:13 +0000200
201 u8 wireless_feature_index;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000202};
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400203
Peter Wuf677bb12014-12-16 01:50:14 +0100204/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400205#define HIDPP_ERROR 0x8f
206#define HIDPP_ERROR_SUCCESS 0x00
207#define HIDPP_ERROR_INVALID_SUBID 0x01
208#define HIDPP_ERROR_INVALID_ADRESS 0x02
209#define HIDPP_ERROR_INVALID_VALUE 0x03
210#define HIDPP_ERROR_CONNECT_FAIL 0x04
211#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
212#define HIDPP_ERROR_ALREADY_EXISTS 0x06
213#define HIDPP_ERROR_BUSY 0x07
214#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
215#define HIDPP_ERROR_RESOURCE_ERROR 0x09
216#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
217#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
218#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100219/* HID++ 2.0 error codes */
220#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400221
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400222static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
223
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400224static int __hidpp_send_report(struct hid_device *hdev,
225 struct hidpp_report *hidpp_report)
226{
Simon Wood7bfd2922015-11-19 16:42:12 -0700227 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400228 int fields_count, ret;
229
230 switch (hidpp_report->report_id) {
231 case REPORT_ID_HIDPP_SHORT:
232 fields_count = HIDPP_REPORT_SHORT_LENGTH;
233 break;
234 case REPORT_ID_HIDPP_LONG:
235 fields_count = HIDPP_REPORT_LONG_LENGTH;
236 break;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700237 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200238 fields_count = hidpp->very_long_report_length;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700239 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400240 default:
241 return -ENODEV;
242 }
243
244 /*
245 * set the device_index as the receiver, it will be overwritten by
246 * hid_hw_request if needed
247 */
248 hidpp_report->device_index = 0xff;
249
Simon Wood7bfd2922015-11-19 16:42:12 -0700250 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
251 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
252 } else {
253 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
254 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
255 HID_REQ_SET_REPORT);
256 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400257
258 return ret == fields_count ? 0 : -1;
259}
260
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500261/**
262 * hidpp_send_message_sync() returns 0 in case of success, and something else
263 * in case of a failure.
264 * - If ' something else' is positive, that means that an error has been raised
265 * by the protocol itself.
266 * - If ' something else' is negative, that means that we had a classic error
267 * (-ENOMEM, -EPIPE, etc...)
268 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400269static int hidpp_send_message_sync(struct hidpp_device *hidpp,
270 struct hidpp_report *message,
271 struct hidpp_report *response)
272{
273 int ret;
274
275 mutex_lock(&hidpp->send_mutex);
276
277 hidpp->send_receive_buf = response;
278 hidpp->answer_available = false;
279
280 /*
281 * So that we can later validate the answer when it arrives
282 * in hidpp_raw_event
283 */
284 *response = *message;
285
286 ret = __hidpp_send_report(hidpp->hid_dev, message);
287
288 if (ret) {
289 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
290 memset(response, 0, sizeof(struct hidpp_report));
291 goto exit;
292 }
293
294 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
295 5*HZ)) {
296 dbg_hid("%s:timeout waiting for response\n", __func__);
297 memset(response, 0, sizeof(struct hidpp_report));
298 ret = -ETIMEDOUT;
299 }
300
301 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100302 response->rap.sub_id == HIDPP_ERROR) {
303 ret = response->rap.params[1];
304 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
305 goto exit;
306 }
307
Simon Wooda5ce8f52015-11-19 16:42:11 -0700308 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
309 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
310 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400311 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100312 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400313 goto exit;
314 }
315
316exit:
317 mutex_unlock(&hidpp->send_mutex);
318 return ret;
319
320}
321
322static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
323 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
324 struct hidpp_report *response)
325{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300326 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400327 int ret;
328
329 if (param_count > sizeof(message->fap.params))
330 return -EINVAL;
331
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300332 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
333 if (!message)
334 return -ENOMEM;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700335
336 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
337 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
338 else
339 message->report_id = REPORT_ID_HIDPP_LONG;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400340 message->fap.feature_index = feat_index;
341 message->fap.funcindex_clientid = funcindex_clientid;
342 memcpy(&message->fap.params, params, param_count);
343
344 ret = hidpp_send_message_sync(hidpp, message, response);
345 kfree(message);
346 return ret;
347}
348
Benjamin Tissoires33797822014-09-30 13:18:30 -0400349static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
350 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
351 struct hidpp_report *response)
352{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300353 struct hidpp_report *message;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700354 int ret, max_count;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400355
Mazin Rezkc2a93272019-10-27 17:44:06 +0000356 /* Send as long report if short reports are not supported. */
357 if (report_id == REPORT_ID_HIDPP_SHORT &&
358 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
359 report_id = REPORT_ID_HIDPP_LONG;
360
Simon Wooda5ce8f52015-11-19 16:42:11 -0700361 switch (report_id) {
362 case REPORT_ID_HIDPP_SHORT:
363 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
364 break;
365 case REPORT_ID_HIDPP_LONG:
366 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
367 break;
368 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +0200369 max_count = hidpp_dev->very_long_report_length - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700370 break;
371 default:
Benjamin Tissoires33797822014-09-30 13:18:30 -0400372 return -EINVAL;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700373 }
Benjamin Tissoires33797822014-09-30 13:18:30 -0400374
Simon Wooda5ce8f52015-11-19 16:42:11 -0700375 if (param_count > max_count)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400376 return -EINVAL;
377
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300378 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
379 if (!message)
380 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400381 message->report_id = report_id;
382 message->rap.sub_id = sub_id;
383 message->rap.reg_address = reg_address;
384 memcpy(&message->rap.params, params, param_count);
385
386 ret = hidpp_send_message_sync(hidpp_dev, message, response);
387 kfree(message);
388 return ret;
389}
390
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400391static void delayed_work_cb(struct work_struct *work)
392{
393 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
394 work);
395 hidpp_connect_event(hidpp);
396}
397
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400398static inline bool hidpp_match_answer(struct hidpp_report *question,
399 struct hidpp_report *answer)
400{
401 return (answer->fap.feature_index == question->fap.feature_index) &&
402 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
403}
404
405static inline bool hidpp_match_error(struct hidpp_report *question,
406 struct hidpp_report *answer)
407{
Peter Wuf677bb12014-12-16 01:50:14 +0100408 return ((answer->rap.sub_id == HIDPP_ERROR) ||
409 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400410 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
411 (answer->fap.params[0] == question->fap.funcindex_clientid);
412}
413
Mazin Rezk0da0a63b2019-10-27 17:44:13 +0000414static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
415 struct hidpp_report *report)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400416{
Mazin Rezk0da0a63b2019-10-27 17:44:13 +0000417 return (hidpp->wireless_feature_index &&
418 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
419 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
420 (report->rap.sub_id == 0x41));
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400421}
422
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500423/**
424 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
425 */
426static void hidpp_prefix_name(char **name, int name_length)
427{
428#define PREFIX_LENGTH 9 /* "Logitech " */
429
430 int new_length;
431 char *new_name;
432
433 if (name_length > PREFIX_LENGTH &&
434 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
435 /* The prefix has is already in the name */
436 return;
437
438 new_length = PREFIX_LENGTH + name_length;
439 new_name = kzalloc(new_length, GFP_KERNEL);
440 if (!new_name)
441 return;
442
443 snprintf(new_name, new_length, "Logitech %s", *name);
444
445 kfree(*name);
446
447 *name = new_name;
448}
449
Harry Cutts4435ff22018-12-05 10:42:27 +1000450/**
451 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
452 * events given a high-resolution wheel
453 * movement.
454 * @counter: a hid_scroll_counter struct describing the wheel.
455 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
456 * units.
457 *
458 * Given a high-resolution movement, this function converts the movement into
459 * fractions of 120 and emits high-resolution scroll events for the input
460 * device. It also uses the multiplier from &struct hid_scroll_counter to
461 * emit low-resolution scroll events when appropriate for
462 * backwards-compatibility with userspace input libraries.
463 */
Hans de Goede06104302019-04-20 13:22:13 +0200464static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
465 struct hidpp_scroll_counter *counter,
Harry Cutts4435ff22018-12-05 10:42:27 +1000466 int hi_res_value)
467{
468 int low_res_value, remainder, direction;
469 unsigned long long now, previous;
470
471 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
Hans de Goede06104302019-04-20 13:22:13 +0200472 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
Harry Cutts4435ff22018-12-05 10:42:27 +1000473
474 remainder = counter->remainder;
475 direction = hi_res_value > 0 ? 1 : -1;
476
477 now = sched_clock();
478 previous = counter->last_time;
479 counter->last_time = now;
480 /*
481 * Reset the remainder after a period of inactivity or when the
482 * direction changes. This prevents the REL_WHEEL emulation point
483 * from sliding for devices that don't always provide the same
484 * number of movements per detent.
485 */
486 if (now - previous > 1000000000 || direction != counter->direction)
487 remainder = 0;
488
489 counter->direction = direction;
490 remainder += hi_res_value;
491
492 /* Some wheels will rest 7/8ths of a detent from the previous detent
493 * after slow movement, so we want the threshold for low-res events to
494 * be in the middle between two detents (e.g. after 4/8ths) as
495 * opposed to on the detents themselves (8/8ths).
496 */
497 if (abs(remainder) >= 60) {
498 /* Add (or subtract) 1 because we want to trigger when the wheel
499 * is half-way to the next detent (i.e. scroll 1 detent after a
500 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
501 * etc.).
502 */
503 low_res_value = remainder / 120;
504 if (low_res_value == 0)
505 low_res_value = (hi_res_value > 0 ? 1 : -1);
Hans de Goede06104302019-04-20 13:22:13 +0200506 input_report_rel(input_dev, REL_WHEEL, low_res_value);
Harry Cutts4435ff22018-12-05 10:42:27 +1000507 remainder -= low_res_value * 120;
508 }
509 counter->remainder = remainder;
510}
511
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400512/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400513/* HIDP++ 1.0 commands */
514/* -------------------------------------------------------------------------- */
515
516#define HIDPP_SET_REGISTER 0x80
517#define HIDPP_GET_REGISTER 0x81
518#define HIDPP_SET_LONG_REGISTER 0x82
519#define HIDPP_GET_LONG_REGISTER 0x83
520
Harry Cutts95c3d002018-12-05 10:42:26 +1000521/**
Hans de Goede35839f72019-04-20 13:22:14 +0200522 * hidpp10_set_register - Modify a HID++ 1.0 register.
Harry Cutts95c3d002018-12-05 10:42:26 +1000523 * @hidpp_dev: the device to set the register on.
524 * @register_address: the address of the register to modify.
525 * @byte: the byte of the register to modify. Should be less than 3.
Hans de Goede35839f72019-04-20 13:22:14 +0200526 * @mask: mask of the bits to modify
527 * @value: new values for the bits in mask
Harry Cutts95c3d002018-12-05 10:42:26 +1000528 * Return: 0 if successful, otherwise a negative error code.
529 */
Hans de Goede35839f72019-04-20 13:22:14 +0200530static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
531 u8 register_address, u8 byte, u8 mask, u8 value)
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200532{
533 struct hidpp_report response;
534 int ret;
535 u8 params[3] = { 0 };
536
537 ret = hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000538 REPORT_ID_HIDPP_SHORT,
539 HIDPP_GET_REGISTER,
540 register_address,
541 NULL, 0, &response);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200542 if (ret)
543 return ret;
544
545 memcpy(params, response.rap.params, 3);
546
Hans de Goede35839f72019-04-20 13:22:14 +0200547 params[byte] &= ~mask;
548 params[byte] |= value & mask;
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200549
550 return hidpp_send_rap_command_sync(hidpp_dev,
Harry Cutts95c3d002018-12-05 10:42:26 +1000551 REPORT_ID_HIDPP_SHORT,
552 HIDPP_SET_REGISTER,
553 register_address,
554 params, 3, &response);
555}
556
Hans de Goede35839f72019-04-20 13:22:14 +0200557#define HIDPP_REG_ENABLE_REPORTS 0x00
558#define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
559#define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
560#define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
561#define HIDPP_ENABLE_BAT_REPORT BIT(4)
562#define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
Harry Cutts95c3d002018-12-05 10:42:26 +1000563
564static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
565{
Hans de Goede35839f72019-04-20 13:22:14 +0200566 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
567 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
Harry Cutts95c3d002018-12-05 10:42:26 +1000568}
569
570#define HIDPP_REG_FEATURES 0x01
Hans de Goede35839f72019-04-20 13:22:14 +0200571#define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
572#define HIDPP_ENABLE_FAST_SCROLL BIT(6)
Harry Cutts95c3d002018-12-05 10:42:26 +1000573
574/* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
575static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
576{
Hans de Goede35839f72019-04-20 13:22:14 +0200577 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
578 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +0200579}
580
581#define HIDPP_REG_BATTERY_STATUS 0x07
582
583static int hidpp10_battery_status_map_level(u8 param)
584{
585 int level;
586
587 switch (param) {
588 case 1 ... 2:
589 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
590 break;
591 case 3 ... 4:
592 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
593 break;
594 case 5 ... 6:
595 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
596 break;
597 case 7:
598 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
599 break;
600 default:
601 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
602 }
603
604 return level;
605}
606
607static int hidpp10_battery_status_map_status(u8 param)
608{
609 int status;
610
611 switch (param) {
612 case 0x00:
613 /* discharging (in use) */
614 status = POWER_SUPPLY_STATUS_DISCHARGING;
615 break;
616 case 0x21: /* (standard) charging */
617 case 0x24: /* fast charging */
618 case 0x25: /* slow charging */
619 status = POWER_SUPPLY_STATUS_CHARGING;
620 break;
621 case 0x26: /* topping charge */
622 case 0x22: /* charge complete */
623 status = POWER_SUPPLY_STATUS_FULL;
624 break;
625 case 0x20: /* unknown */
626 status = POWER_SUPPLY_STATUS_UNKNOWN;
627 break;
628 /*
629 * 0x01...0x1F = reserved (not charging)
630 * 0x23 = charging error
631 * 0x27..0xff = reserved
632 */
633 default:
634 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
635 break;
636 }
637
638 return status;
639}
640
641static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
642{
643 struct hidpp_report response;
644 int ret, status;
645
646 ret = hidpp_send_rap_command_sync(hidpp,
647 REPORT_ID_HIDPP_SHORT,
648 HIDPP_GET_REGISTER,
649 HIDPP_REG_BATTERY_STATUS,
650 NULL, 0, &response);
651 if (ret)
652 return ret;
653
654 hidpp->battery.level =
655 hidpp10_battery_status_map_level(response.rap.params[0]);
656 status = hidpp10_battery_status_map_status(response.rap.params[1]);
657 hidpp->battery.status = status;
658 /* the capacity is only available when discharging or full */
659 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
660 status == POWER_SUPPLY_STATUS_FULL;
661
662 return 0;
663}
664
665#define HIDPP_REG_BATTERY_MILEAGE 0x0D
666
667static int hidpp10_battery_mileage_map_status(u8 param)
668{
669 int status;
670
671 switch (param >> 6) {
672 case 0x00:
673 /* discharging (in use) */
674 status = POWER_SUPPLY_STATUS_DISCHARGING;
675 break;
676 case 0x01: /* charging */
677 status = POWER_SUPPLY_STATUS_CHARGING;
678 break;
679 case 0x02: /* charge complete */
680 status = POWER_SUPPLY_STATUS_FULL;
681 break;
682 /*
683 * 0x03 = charging error
684 */
685 default:
686 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
687 break;
688 }
689
690 return status;
691}
692
693static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
694{
695 struct hidpp_report response;
696 int ret, status;
697
698 ret = hidpp_send_rap_command_sync(hidpp,
699 REPORT_ID_HIDPP_SHORT,
700 HIDPP_GET_REGISTER,
701 HIDPP_REG_BATTERY_MILEAGE,
702 NULL, 0, &response);
703 if (ret)
704 return ret;
705
706 hidpp->battery.capacity = response.rap.params[0];
707 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
708 hidpp->battery.status = status;
709 /* the capacity is only available when discharging or full */
710 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
711 status == POWER_SUPPLY_STATUS_FULL;
712
713 return 0;
714}
715
716static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
717{
718 struct hidpp_report *report = (struct hidpp_report *)data;
719 int status, capacity, level;
720 bool changed;
721
722 if (report->report_id != REPORT_ID_HIDPP_SHORT)
723 return 0;
724
725 switch (report->rap.sub_id) {
726 case HIDPP_REG_BATTERY_STATUS:
727 capacity = hidpp->battery.capacity;
728 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
729 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
730 break;
731 case HIDPP_REG_BATTERY_MILEAGE:
732 capacity = report->rap.params[0];
733 level = hidpp->battery.level;
734 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
735 break;
736 default:
737 return 0;
738 }
739
740 changed = capacity != hidpp->battery.capacity ||
741 level != hidpp->battery.level ||
742 status != hidpp->battery.status;
743
744 /* the capacity is only available when discharging or full */
745 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
746 status == POWER_SUPPLY_STATUS_FULL;
747
748 if (changed) {
749 hidpp->battery.level = level;
750 hidpp->battery.status = status;
751 if (hidpp->battery.ps)
752 power_supply_changed(hidpp->battery.ps);
753 }
754
755 return 0;
756}
757
Benjamin Tissoires33797822014-09-30 13:18:30 -0400758#define HIDPP_REG_PAIRING_INFORMATION 0xB5
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200759#define HIDPP_EXTENDED_PAIRING 0x30
760#define HIDPP_DEVICE_NAME 0x40
Benjamin Tissoires33797822014-09-30 13:18:30 -0400761
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200762static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400763{
764 struct hidpp_report response;
765 int ret;
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200766 u8 params[1] = { HIDPP_DEVICE_NAME };
Benjamin Tissoires33797822014-09-30 13:18:30 -0400767 char *name;
768 int len;
769
770 ret = hidpp_send_rap_command_sync(hidpp_dev,
771 REPORT_ID_HIDPP_SHORT,
772 HIDPP_GET_LONG_REGISTER,
773 HIDPP_REG_PAIRING_INFORMATION,
774 params, 1, &response);
775 if (ret)
776 return NULL;
777
778 len = response.rap.params[1];
779
Peter Wu3a034a7a2014-12-11 13:51:19 +0100780 if (2 + len > sizeof(response.rap.params))
781 return NULL;
782
Hans de Goede22bf6bd2019-04-20 13:22:06 +0200783 if (len < 4) /* logitech devices are usually at least Xddd */
784 return NULL;
785
Benjamin Tissoires33797822014-09-30 13:18:30 -0400786 name = kzalloc(len + 1, GFP_KERNEL);
787 if (!name)
788 return NULL;
789
790 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500791
792 /* include the terminating '\0' */
793 hidpp_prefix_name(&name, len + 1);
794
Benjamin Tissoires33797822014-09-30 13:18:30 -0400795 return name;
796}
797
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200798static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
799{
800 struct hidpp_report response;
801 int ret;
802 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
803
804 ret = hidpp_send_rap_command_sync(hidpp,
805 REPORT_ID_HIDPP_SHORT,
806 HIDPP_GET_LONG_REGISTER,
807 HIDPP_REG_PAIRING_INFORMATION,
808 params, 1, &response);
809 if (ret)
810 return ret;
811
812 /*
813 * We don't care about LE or BE, we will output it as a string
814 * with %4phD, so we need to keep the order.
815 */
816 *serial = *((u32 *)&response.rap.params[1]);
817 return 0;
818}
819
820static int hidpp_unifying_init(struct hidpp_device *hidpp)
821{
822 struct hid_device *hdev = hidpp->hid_dev;
823 const char *name;
824 u32 serial;
825 int ret;
826
827 ret = hidpp_unifying_get_serial(hidpp, &serial);
828 if (ret)
829 return ret;
830
Bastien Nocera0898a1d2023-03-02 14:01:17 +0100831 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200832 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
833
834 name = hidpp_unifying_get_name(hidpp);
835 if (!name)
836 return -EIO;
837
838 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
839 dbg_hid("HID++ Unifying: Got name: %s\n", name);
840
841 kfree(name);
842 return 0;
843}
844
Benjamin Tissoires33797822014-09-30 13:18:30 -0400845/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400846/* 0x0000: Root */
847/* -------------------------------------------------------------------------- */
848
849#define HIDPP_PAGE_ROOT 0x0000
850#define HIDPP_PAGE_ROOT_IDX 0x00
851
852#define CMD_ROOT_GET_FEATURE 0x01
853#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
854
855static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
856 u8 *feature_index, u8 *feature_type)
857{
858 struct hidpp_report response;
859 int ret;
860 u8 params[2] = { feature >> 8, feature & 0x00FF };
861
862 ret = hidpp_send_fap_command_sync(hidpp,
863 HIDPP_PAGE_ROOT_IDX,
864 CMD_ROOT_GET_FEATURE,
865 params, 2, &response);
866 if (ret)
867 return ret;
868
Benjamin Tissoiresa9525b82017-03-27 16:59:32 +0200869 if (response.fap.params[0] == 0)
870 return -ENOENT;
871
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400872 *feature_index = response.fap.params[0];
873 *feature_type = response.fap.params[1];
874
875 return ret;
876}
877
878static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
879{
Hans de Goede09637752019-04-20 13:22:10 +0200880 const u8 ping_byte = 0x5a;
881 u8 ping_data[3] = { 0, 0, ping_byte };
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400882 struct hidpp_report response;
883 int ret;
884
Hans de Goede09637752019-04-20 13:22:10 +0200885 ret = hidpp_send_rap_command_sync(hidpp,
886 REPORT_ID_HIDPP_SHORT,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400887 HIDPP_PAGE_ROOT_IDX,
888 CMD_ROOT_GET_PROTOCOL_VERSION,
Hans de Goede09637752019-04-20 13:22:10 +0200889 ping_data, sizeof(ping_data), &response);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400890
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500891 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400892 hidpp->protocol_major = 1;
893 hidpp->protocol_minor = 0;
Hans de Goede9576af62019-03-22 08:41:38 +0100894 goto print_version;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400895 }
896
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500897 /* the device might not be connected */
898 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
899 return -EIO;
900
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500901 if (ret > 0) {
902 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
903 __func__, ret);
904 return -EPROTO;
905 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400906 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500907 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400908
Hans de Goede09637752019-04-20 13:22:10 +0200909 if (response.rap.params[2] != ping_byte) {
910 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
911 __func__, response.rap.params[2], ping_byte);
912 return -EPROTO;
913 }
914
915 hidpp->protocol_major = response.rap.params[0];
916 hidpp->protocol_minor = response.rap.params[1];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400917
Hans de Goede9576af62019-03-22 08:41:38 +0100918print_version:
919 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
920 hidpp->protocol_major, hidpp->protocol_minor);
921 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400922}
923
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400924/* -------------------------------------------------------------------------- */
Bastien Nocera958534d2023-03-02 14:01:16 +0100925/* 0x0003: Device Information */
926/* -------------------------------------------------------------------------- */
927
928#define HIDPP_PAGE_DEVICE_INFORMATION 0x0003
929
930#define CMD_GET_DEVICE_INFO 0x00
931
932static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
933{
934 struct hidpp_report response;
935 u8 feature_type;
936 u8 feature_index;
937 int ret;
938
939 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
940 &feature_index,
941 &feature_type);
942 if (ret)
943 return ret;
944
945 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
946 CMD_GET_DEVICE_INFO,
947 NULL, 0, &response);
948 if (ret)
949 return ret;
950
951 /* See hidpp_unifying_get_serial() */
952 *serial = *((u32 *)&response.rap.params[1]);
953 return 0;
954}
955
956static int hidpp_serial_init(struct hidpp_device *hidpp)
957{
958 struct hid_device *hdev = hidpp->hid_dev;
959 u32 serial;
960 int ret;
961
962 ret = hidpp_get_serial(hidpp, &serial);
963 if (ret)
964 return ret;
965
Bastien Nocera0898a1d2023-03-02 14:01:17 +0100966 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
Bastien Nocera958534d2023-03-02 14:01:16 +0100967 dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
968
969 return 0;
970}
971
972/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400973/* 0x0005: GetDeviceNameType */
974/* -------------------------------------------------------------------------- */
975
976#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
977
978#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
979#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
980#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
981
982static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
983 u8 feature_index, u8 *nameLength)
984{
985 struct hidpp_report response;
986 int ret;
987
988 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
989 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
990
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500991 if (ret > 0) {
992 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
993 __func__, ret);
994 return -EPROTO;
995 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400996 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500997 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400998
999 *nameLength = response.fap.params[0];
1000
1001 return ret;
1002}
1003
1004static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
1005 u8 feature_index, u8 char_index, char *device_name, int len_buf)
1006{
1007 struct hidpp_report response;
1008 int ret, i;
1009 int count;
1010
1011 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1012 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
1013 &response);
1014
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001015 if (ret > 0) {
1016 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1017 __func__, ret);
1018 return -EPROTO;
1019 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001020 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001021 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001022
Simon Wooda5ce8f52015-11-19 16:42:11 -07001023 switch (response.report_id) {
1024 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +02001025 count = hidpp->very_long_report_length - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -07001026 break;
1027 case REPORT_ID_HIDPP_LONG:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001028 count = HIDPP_REPORT_LONG_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -07001029 break;
1030 case REPORT_ID_HIDPP_SHORT:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001031 count = HIDPP_REPORT_SHORT_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -07001032 break;
1033 default:
1034 return -EPROTO;
1035 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001036
1037 if (len_buf < count)
1038 count = len_buf;
1039
1040 for (i = 0; i < count; i++)
1041 device_name[i] = response.fap.params[i];
1042
1043 return count;
1044}
1045
Peter Wu02cc0972014-12-11 13:51:17 +01001046static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001047{
1048 u8 feature_type;
1049 u8 feature_index;
1050 u8 __name_length;
1051 char *name;
1052 unsigned index = 0;
1053 int ret;
1054
1055 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1056 &feature_index, &feature_type);
1057 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +01001058 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001059
1060 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1061 &__name_length);
1062 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +01001063 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001064
1065 name = kzalloc(__name_length + 1, GFP_KERNEL);
1066 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +01001067 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001068
Peter Wu1430ee72014-12-11 13:51:18 +01001069 while (index < __name_length) {
1070 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001071 feature_index, index, name + index,
1072 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +01001073 if (ret <= 0) {
1074 kfree(name);
1075 return NULL;
1076 }
1077 index += ret;
1078 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001079
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -05001080 /* include the terminating '\0' */
1081 hidpp_prefix_name(&name, __name_length + 1);
1082
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001083 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001084}
1085
1086/* -------------------------------------------------------------------------- */
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001087/* 0x1000: Battery level status */
1088/* -------------------------------------------------------------------------- */
1089
1090#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1091
1092#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1093#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1094
1095#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1096
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001097#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1098#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1099#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1100
1101static int hidpp_map_battery_level(int capacity)
1102{
1103 if (capacity < 11)
1104 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
Hans de Goede1f87b0c2019-03-22 08:41:40 +01001105 /*
1106 * The spec says this should be < 31 but some devices report 30
1107 * with brand new batteries and Windows reports 30 as "Good".
1108 */
1109 else if (capacity < 30)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001110 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1111 else if (capacity < 81)
1112 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1113 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1114}
1115
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001116static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001117 int *next_capacity,
1118 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001119{
1120 int status;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001121
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001122 *capacity = data[0];
1123 *next_capacity = data[1];
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001124 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001125
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001126 /* When discharging, we can rely on the device reported capacity.
1127 * For all other states the device reports 0 (unknown).
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001128 */
1129 switch (data[2]) {
1130 case 0: /* discharging (in use) */
1131 status = POWER_SUPPLY_STATUS_DISCHARGING;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001132 *level = hidpp_map_battery_level(*capacity);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001133 break;
1134 case 1: /* recharging */
1135 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001136 break;
1137 case 2: /* charge in final stage */
1138 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001139 break;
1140 case 3: /* charge complete */
1141 status = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001142 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001143 *capacity = 100;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001144 break;
1145 case 4: /* recharging below optimal speed */
1146 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001147 break;
1148 /* 5 = invalid battery type
1149 6 = thermal error
1150 7 = other charging error */
1151 default:
1152 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001153 break;
1154 }
1155
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001156 return status;
1157}
1158
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001159static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1160 u8 feature_index,
1161 int *status,
1162 int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001163 int *next_capacity,
1164 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001165{
1166 struct hidpp_report response;
1167 int ret;
1168 u8 *params = (u8 *)response.fap.params;
1169
1170 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1171 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1172 NULL, 0, &response);
Hans de Goede61005d62019-11-14 15:30:46 +01001173 /* Ignore these intermittent errors */
1174 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1175 return -EIO;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001176 if (ret > 0) {
1177 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1178 __func__, ret);
1179 return -EPROTO;
1180 }
1181 if (ret)
1182 return ret;
1183
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001184 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001185 next_capacity,
1186 level);
1187
1188 return 0;
1189}
1190
1191static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1192 u8 feature_index)
1193{
1194 struct hidpp_report response;
1195 int ret;
1196 u8 *params = (u8 *)response.fap.params;
1197 unsigned int level_count, flags;
1198
1199 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1200 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1201 NULL, 0, &response);
1202 if (ret > 0) {
1203 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1204 __func__, ret);
1205 return -EPROTO;
1206 }
1207 if (ret)
1208 return ret;
1209
1210 level_count = params[0];
1211 flags = params[1];
1212
1213 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1214 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1215 else
1216 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001217
1218 return 0;
1219}
1220
1221static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1222{
1223 u8 feature_type;
1224 int ret;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001225 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001226
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001227 if (hidpp->battery.feature_index == 0xff) {
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001228 ret = hidpp_root_get_feature(hidpp,
1229 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1230 &hidpp->battery.feature_index,
1231 &feature_type);
1232 if (ret)
1233 return ret;
1234 }
1235
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001236 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1237 hidpp->battery.feature_index,
1238 &status, &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001239 &next_capacity, &level);
1240 if (ret)
1241 return ret;
1242
1243 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1244 hidpp->battery.feature_index);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001245 if (ret)
1246 return ret;
1247
1248 hidpp->battery.status = status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001249 hidpp->battery.capacity = capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001250 hidpp->battery.level = level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001251 /* the capacity is only available when discharging or full */
1252 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1253 status == POWER_SUPPLY_STATUS_FULL;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001254
1255 return 0;
1256}
1257
1258static int hidpp20_battery_event(struct hidpp_device *hidpp,
1259 u8 *data, int size)
1260{
1261 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001262 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001263 bool changed;
1264
1265 if (report->fap.feature_index != hidpp->battery.feature_index ||
1266 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1267 return 0;
1268
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001269 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1270 &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001271 &next_capacity,
1272 &level);
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001273
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001274 /* the capacity is only available when discharging or full */
1275 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1276 status == POWER_SUPPLY_STATUS_FULL;
1277
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001278 changed = capacity != hidpp->battery.capacity ||
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001279 level != hidpp->battery.level ||
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001280 status != hidpp->battery.status;
1281
1282 if (changed) {
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001283 hidpp->battery.level = level;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001284 hidpp->battery.capacity = capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001285 hidpp->battery.status = status;
1286 if (hidpp->battery.ps)
1287 power_supply_changed(hidpp->battery.ps);
1288 }
1289
1290 return 0;
1291}
1292
Pedro Vanzellabe281362019-10-26 19:25:06 -03001293/* -------------------------------------------------------------------------- */
1294/* 0x1001: Battery voltage */
1295/* -------------------------------------------------------------------------- */
1296
1297#define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1298
1299#define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1300
1301#define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1302
1303static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1304 int *level, int *charge_type)
1305{
1306 int status;
1307
Filipe Laíns4ab2bb32020-01-11 19:24:19 +00001308 long flags = (long) data[2];
Tom Rix39b92722021-05-07 12:18:19 -07001309 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Pedro Vanzellabe281362019-10-26 19:25:06 -03001310
Filipe Laíns4ab2bb32020-01-11 19:24:19 +00001311 if (flags & 0x80)
1312 switch (flags & 0x07) {
1313 case 0:
1314 status = POWER_SUPPLY_STATUS_CHARGING;
1315 break;
1316 case 1:
1317 status = POWER_SUPPLY_STATUS_FULL;
1318 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1319 break;
1320 case 2:
1321 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1322 break;
1323 default:
1324 status = POWER_SUPPLY_STATUS_UNKNOWN;
1325 break;
1326 }
1327 else
Pedro Vanzellabe281362019-10-26 19:25:06 -03001328 status = POWER_SUPPLY_STATUS_DISCHARGING;
Pedro Vanzellabe281362019-10-26 19:25:06 -03001329
1330 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
Filipe Laíns4ab2bb32020-01-11 19:24:19 +00001331 if (test_bit(3, &flags)) {
Pedro Vanzellabe281362019-10-26 19:25:06 -03001332 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1333 }
Filipe Laíns4ab2bb32020-01-11 19:24:19 +00001334 if (test_bit(4, &flags)) {
Pedro Vanzellabe281362019-10-26 19:25:06 -03001335 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1336 }
Filipe Laíns4ab2bb32020-01-11 19:24:19 +00001337 if (test_bit(5, &flags)) {
Pedro Vanzellabe281362019-10-26 19:25:06 -03001338 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1339 }
1340
1341 *voltage = get_unaligned_be16(data);
1342
1343 return status;
1344}
1345
1346static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1347 u8 feature_index,
1348 int *status, int *voltage,
1349 int *level, int *charge_type)
1350{
1351 struct hidpp_report response;
1352 int ret;
1353 u8 *params = (u8 *)response.fap.params;
1354
1355 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1356 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1357 NULL, 0, &response);
1358
1359 if (ret > 0) {
1360 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1361 __func__, ret);
1362 return -EPROTO;
1363 }
1364 if (ret)
1365 return ret;
1366
1367 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1368
1369 *status = hidpp20_battery_map_status_voltage(params, voltage,
1370 level, charge_type);
1371
1372 return 0;
1373}
1374
1375static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1376{
1377 u8 feature_type;
1378 int ret;
1379 int status, voltage, level, charge_type;
1380
1381 if (hidpp->battery.voltage_feature_index == 0xff) {
1382 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1383 &hidpp->battery.voltage_feature_index,
1384 &feature_type);
1385 if (ret)
1386 return ret;
1387 }
1388
1389 ret = hidpp20_battery_get_battery_voltage(hidpp,
1390 hidpp->battery.voltage_feature_index,
1391 &status, &voltage, &level, &charge_type);
1392
1393 if (ret)
1394 return ret;
1395
1396 hidpp->battery.status = status;
1397 hidpp->battery.voltage = voltage;
1398 hidpp->battery.level = level;
1399 hidpp->battery.charge_type = charge_type;
1400 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1401
1402 return 0;
1403}
1404
1405static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1406 u8 *data, int size)
1407{
1408 struct hidpp_report *report = (struct hidpp_report *)data;
1409 int status, voltage, level, charge_type;
1410
1411 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1412 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1413 return 0;
1414
1415 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1416 &level, &charge_type);
1417
1418 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1419
1420 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1421 hidpp->battery.voltage = voltage;
1422 hidpp->battery.status = status;
1423 hidpp->battery.level = level;
1424 hidpp->battery.charge_type = charge_type;
1425 if (hidpp->battery.ps)
1426 power_supply_changed(hidpp->battery.ps);
1427 }
1428 return 0;
1429}
1430
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001431static enum power_supply_property hidpp_battery_props[] = {
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001432 POWER_SUPPLY_PROP_ONLINE,
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001433 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001434 POWER_SUPPLY_PROP_SCOPE,
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001435 POWER_SUPPLY_PROP_MODEL_NAME,
1436 POWER_SUPPLY_PROP_MANUFACTURER,
1437 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001438 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1439 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
Pedro Vanzellabe281362019-10-26 19:25:06 -03001440 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001441};
1442
1443static int hidpp_battery_get_property(struct power_supply *psy,
1444 enum power_supply_property psp,
1445 union power_supply_propval *val)
1446{
1447 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1448 int ret = 0;
1449
1450 switch(psp) {
1451 case POWER_SUPPLY_PROP_STATUS:
1452 val->intval = hidpp->battery.status;
1453 break;
1454 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires14f437a2017-03-27 16:59:35 +02001455 val->intval = hidpp->battery.capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001456 break;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02001457 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1458 val->intval = hidpp->battery.level;
1459 break;
Bastien Nocera3861e6c2017-03-27 16:59:22 +02001460 case POWER_SUPPLY_PROP_SCOPE:
1461 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1462 break;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02001463 case POWER_SUPPLY_PROP_ONLINE:
1464 val->intval = hidpp->battery.online;
1465 break;
Benjamin Tissoires32043d02017-03-27 16:59:30 +02001466 case POWER_SUPPLY_PROP_MODEL_NAME:
1467 if (!strncmp(hidpp->name, "Logitech ", 9))
1468 val->strval = hidpp->name + 9;
1469 else
1470 val->strval = hidpp->name;
1471 break;
1472 case POWER_SUPPLY_PROP_MANUFACTURER:
1473 val->strval = "Logitech";
1474 break;
1475 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1476 val->strval = hidpp->hid_dev->uniq;
1477 break;
Pedro Vanzellabe281362019-10-26 19:25:06 -03001478 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1479 /* hardware reports voltage in in mV. sysfs expects uV */
1480 val->intval = hidpp->battery.voltage * 1000;
1481 break;
1482 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1483 val->intval = hidpp->battery.charge_type;
1484 break;
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001485 default:
1486 ret = -EINVAL;
1487 break;
1488 }
1489
1490 return ret;
1491}
1492
Peter Hutterer5a2b1902016-06-29 19:28:01 +10001493/* -------------------------------------------------------------------------- */
Mazin Rezk0da0a63b2019-10-27 17:44:13 +00001494/* 0x1d4b: Wireless device status */
1495/* -------------------------------------------------------------------------- */
1496#define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1497
1498static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1499{
1500 u8 feature_type;
1501 int ret;
1502
1503 ret = hidpp_root_get_feature(hidpp,
1504 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1505 &hidpp->wireless_feature_index,
1506 &feature_type);
1507
1508 return ret;
1509}
1510
1511/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10001512/* 0x2120: Hi-resolution scrolling */
1513/* -------------------------------------------------------------------------- */
1514
1515#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1516
1517#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1518
1519static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1520 bool enabled, u8 *multiplier)
1521{
1522 u8 feature_index;
1523 u8 feature_type;
1524 int ret;
1525 u8 params[1];
1526 struct hidpp_report response;
1527
1528 ret = hidpp_root_get_feature(hidpp,
1529 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1530 &feature_index,
1531 &feature_type);
1532 if (ret)
1533 return ret;
1534
1535 params[0] = enabled ? BIT(0) : 0;
1536 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1537 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1538 params, sizeof(params), &response);
1539 if (ret)
1540 return ret;
1541 *multiplier = response.fap.params[1];
1542 return 0;
1543}
1544
1545/* -------------------------------------------------------------------------- */
1546/* 0x2121: HiRes Wheel */
1547/* -------------------------------------------------------------------------- */
1548
1549#define HIDPP_PAGE_HIRES_WHEEL 0x2121
1550
1551#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1552#define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1553
1554static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1555 u8 *multiplier)
1556{
1557 u8 feature_index;
1558 u8 feature_type;
1559 int ret;
1560 struct hidpp_report response;
1561
1562 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1563 &feature_index, &feature_type);
1564 if (ret)
1565 goto return_default;
1566
1567 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1568 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1569 NULL, 0, &response);
1570 if (ret)
1571 goto return_default;
1572
1573 *multiplier = response.fap.params[0];
1574 return 0;
1575return_default:
1576 hid_warn(hidpp->hid_dev,
1577 "Couldn't get wheel multiplier (error %d)\n", ret);
1578 return ret;
1579}
1580
1581static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1582 bool high_resolution, bool use_hidpp)
1583{
1584 u8 feature_index;
1585 u8 feature_type;
1586 int ret;
1587 u8 params[1];
1588 struct hidpp_report response;
1589
1590 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1591 &feature_index, &feature_type);
1592 if (ret)
1593 return ret;
1594
1595 params[0] = (invert ? BIT(2) : 0) |
1596 (high_resolution ? BIT(1) : 0) |
1597 (use_hidpp ? BIT(0) : 0);
1598
1599 return hidpp_send_fap_command_sync(hidpp, feature_index,
1600 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1601 params, sizeof(params), &response);
1602}
1603
1604/* -------------------------------------------------------------------------- */
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02001605/* 0x4301: Solar Keyboard */
1606/* -------------------------------------------------------------------------- */
1607
1608#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1609
1610#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1611
1612#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1613#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1614#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1615
1616static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1617{
1618 struct hidpp_report response;
1619 u8 params[2] = { 1, 1 };
1620 u8 feature_type;
1621 int ret;
1622
1623 if (hidpp->battery.feature_index == 0xff) {
1624 ret = hidpp_root_get_feature(hidpp,
1625 HIDPP_PAGE_SOLAR_KEYBOARD,
1626 &hidpp->battery.solar_feature_index,
1627 &feature_type);
1628 if (ret)
1629 return ret;
1630 }
1631
1632 ret = hidpp_send_fap_command_sync(hidpp,
1633 hidpp->battery.solar_feature_index,
1634 CMD_SOLAR_SET_LIGHT_MEASURE,
1635 params, 2, &response);
1636 if (ret > 0) {
1637 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1638 __func__, ret);
1639 return -EPROTO;
1640 }
1641 if (ret)
1642 return ret;
1643
1644 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1645
1646 return 0;
1647}
1648
1649static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1650 u8 *data, int size)
1651{
1652 struct hidpp_report *report = (struct hidpp_report *)data;
1653 int capacity, lux, status;
1654 u8 function;
1655
1656 function = report->fap.funcindex_clientid;
1657
1658
1659 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1660 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1661 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1662 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1663 return 0;
1664
1665 capacity = report->fap.params[0];
1666
1667 switch (function) {
1668 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1669 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1670 if (lux > 200)
1671 status = POWER_SUPPLY_STATUS_CHARGING;
1672 else
1673 status = POWER_SUPPLY_STATUS_DISCHARGING;
1674 break;
1675 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1676 default:
1677 if (capacity < hidpp->battery.capacity)
1678 status = POWER_SUPPLY_STATUS_DISCHARGING;
1679 else
1680 status = POWER_SUPPLY_STATUS_CHARGING;
1681
1682 }
1683
1684 if (capacity == 100)
1685 status = POWER_SUPPLY_STATUS_FULL;
1686
1687 hidpp->battery.online = true;
1688 if (capacity != hidpp->battery.capacity ||
1689 status != hidpp->battery.status) {
1690 hidpp->battery.capacity = capacity;
1691 hidpp->battery.status = status;
1692 if (hidpp->battery.ps)
1693 power_supply_changed(hidpp->battery.ps);
1694 }
1695
1696 return 0;
1697}
1698
1699/* -------------------------------------------------------------------------- */
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04001700/* 0x6010: Touchpad FW items */
1701/* -------------------------------------------------------------------------- */
1702
1703#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1704
1705#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1706
1707struct hidpp_touchpad_fw_items {
1708 uint8_t presence;
1709 uint8_t desired_state;
1710 uint8_t state;
1711 uint8_t persistent;
1712};
1713
1714/**
1715 * send a set state command to the device by reading the current items->state
1716 * field. items is then filled with the current state.
1717 */
1718static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1719 u8 feature_index,
1720 struct hidpp_touchpad_fw_items *items)
1721{
1722 struct hidpp_report response;
1723 int ret;
1724 u8 *params = (u8 *)response.fap.params;
1725
1726 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1727 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1728
1729 if (ret > 0) {
1730 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1731 __func__, ret);
1732 return -EPROTO;
1733 }
1734 if (ret)
1735 return ret;
1736
1737 items->presence = params[0];
1738 items->desired_state = params[1];
1739 items->state = params[2];
1740 items->persistent = params[3];
1741
1742 return 0;
1743}
1744
1745/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001746/* 0x6100: TouchPadRawXY */
1747/* -------------------------------------------------------------------------- */
1748
1749#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1750
1751#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001752#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1753
1754#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001755
1756#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1757#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1758
1759struct hidpp_touchpad_raw_info {
1760 u16 x_size;
1761 u16 y_size;
1762 u8 z_range;
1763 u8 area_range;
1764 u8 timestamp_unit;
1765 u8 maxcontacts;
1766 u8 origin;
1767 u16 res;
1768};
1769
1770struct hidpp_touchpad_raw_xy_finger {
1771 u8 contact_type;
1772 u8 contact_status;
1773 u16 x;
1774 u16 y;
1775 u8 z;
1776 u8 area;
1777 u8 finger_id;
1778};
1779
1780struct hidpp_touchpad_raw_xy {
1781 u16 timestamp;
1782 struct hidpp_touchpad_raw_xy_finger fingers[2];
1783 u8 spurious_flag;
1784 u8 end_of_frame;
1785 u8 finger_count;
1786 u8 button;
1787};
1788
1789static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1790 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1791{
1792 struct hidpp_report response;
1793 int ret;
1794 u8 *params = (u8 *)response.fap.params;
1795
1796 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1797 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1798
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001799 if (ret > 0) {
1800 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1801 __func__, ret);
1802 return -EPROTO;
1803 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001804 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001805 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001806
1807 raw_info->x_size = get_unaligned_be16(&params[0]);
1808 raw_info->y_size = get_unaligned_be16(&params[2]);
1809 raw_info->z_range = params[4];
1810 raw_info->area_range = params[5];
1811 raw_info->maxcontacts = params[7];
1812 raw_info->origin = params[8];
1813 /* res is given in unit per inch */
1814 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1815
1816 return ret;
1817}
1818
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001819static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1820 u8 feature_index, bool send_raw_reports,
1821 bool sensor_enhanced_settings)
1822{
1823 struct hidpp_report response;
1824
1825 /*
1826 * Params:
1827 * bit 0 - enable raw
1828 * bit 1 - 16bit Z, no area
1829 * bit 2 - enhanced sensitivity
1830 * bit 3 - width, height (4 bits each) instead of area
1831 * bit 4 - send raw + gestures (degrades smoothness)
1832 * remaining bits - reserved
1833 */
1834 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1835
1836 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1837 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1838}
1839
1840static void hidpp_touchpad_touch_event(u8 *data,
1841 struct hidpp_touchpad_raw_xy_finger *finger)
1842{
1843 u8 x_m = data[0] << 2;
1844 u8 y_m = data[2] << 2;
1845
1846 finger->x = x_m << 6 | data[1];
1847 finger->y = y_m << 6 | data[3];
1848
1849 finger->contact_type = data[0] >> 6;
1850 finger->contact_status = data[2] >> 6;
1851
1852 finger->z = data[4];
1853 finger->area = data[5];
1854 finger->finger_id = data[6] >> 4;
1855}
1856
1857static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1858 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1859{
1860 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1861 raw_xy->end_of_frame = data[8] & 0x01;
1862 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1863 raw_xy->finger_count = data[15] & 0x0f;
1864 raw_xy->button = (data[8] >> 2) & 0x01;
1865
1866 if (raw_xy->finger_count) {
1867 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1868 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1869 }
1870}
1871
Edwin Veldsff21a632016-01-11 00:25:15 +01001872/* -------------------------------------------------------------------------- */
1873/* 0x8123: Force feedback support */
1874/* -------------------------------------------------------------------------- */
1875
1876#define HIDPP_FF_GET_INFO 0x01
1877#define HIDPP_FF_RESET_ALL 0x11
1878#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1879#define HIDPP_FF_SET_EFFECT_STATE 0x31
1880#define HIDPP_FF_DESTROY_EFFECT 0x41
1881#define HIDPP_FF_GET_APERTURE 0x51
1882#define HIDPP_FF_SET_APERTURE 0x61
1883#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1884#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1885
1886#define HIDPP_FF_EFFECT_STATE_GET 0x00
1887#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1888#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1889#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1890
1891#define HIDPP_FF_EFFECT_CONSTANT 0x00
1892#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1893#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1894#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1895#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1896#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1897#define HIDPP_FF_EFFECT_SPRING 0x06
1898#define HIDPP_FF_EFFECT_DAMPER 0x07
1899#define HIDPP_FF_EFFECT_FRICTION 0x08
1900#define HIDPP_FF_EFFECT_INERTIA 0x09
1901#define HIDPP_FF_EFFECT_RAMP 0x0A
1902
1903#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1904
1905#define HIDPP_FF_EFFECTID_NONE -1
1906#define HIDPP_FF_EFFECTID_AUTOCENTER -2
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07001907#define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
Edwin Veldsff21a632016-01-11 00:25:15 +01001908
1909#define HIDPP_FF_MAX_PARAMS 20
1910#define HIDPP_FF_RESERVED_SLOTS 1
1911
1912struct hidpp_ff_private_data {
1913 struct hidpp_device *hidpp;
1914 u8 feature_index;
1915 u8 version;
1916 u16 gain;
1917 s16 range;
1918 u8 slot_autocenter;
1919 u8 num_effects;
1920 int *effect_ids;
1921 struct workqueue_struct *wq;
1922 atomic_t workqueue_size;
1923};
1924
1925struct hidpp_ff_work_data {
1926 struct work_struct work;
1927 struct hidpp_ff_private_data *data;
1928 int effect_id;
1929 u8 command;
1930 u8 params[HIDPP_FF_MAX_PARAMS];
1931 u8 size;
1932};
1933
Peter Huttererfef33602018-12-05 10:42:25 +10001934static const signed short hidpp_ff_effects[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001935 FF_CONSTANT,
1936 FF_PERIODIC,
1937 FF_SINE,
1938 FF_SQUARE,
1939 FF_SAW_UP,
1940 FF_SAW_DOWN,
1941 FF_TRIANGLE,
1942 FF_SPRING,
1943 FF_DAMPER,
1944 FF_AUTOCENTER,
1945 FF_GAIN,
1946 -1
1947};
1948
Peter Huttererfef33602018-12-05 10:42:25 +10001949static const signed short hidpp_ff_effects_v2[] = {
Edwin Veldsff21a632016-01-11 00:25:15 +01001950 FF_RAMP,
1951 FF_FRICTION,
1952 FF_INERTIA,
1953 -1
1954};
1955
1956static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1957 HIDPP_FF_EFFECT_SPRING,
1958 HIDPP_FF_EFFECT_FRICTION,
1959 HIDPP_FF_EFFECT_DAMPER,
1960 HIDPP_FF_EFFECT_INERTIA
1961};
1962
1963static const char *HIDPP_FF_CONDITION_NAMES[] = {
1964 "spring",
1965 "friction",
1966 "damper",
1967 "inertia"
1968};
1969
1970
1971static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1972{
1973 int i;
1974
1975 for (i = 0; i < data->num_effects; i++)
1976 if (data->effect_ids[i] == effect_id)
1977 return i+1;
1978
1979 return 0;
1980}
1981
1982static void hidpp_ff_work_handler(struct work_struct *w)
1983{
1984 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1985 struct hidpp_ff_private_data *data = wd->data;
1986 struct hidpp_report response;
1987 u8 slot;
1988 int ret;
1989
1990 /* add slot number if needed */
1991 switch (wd->effect_id) {
1992 case HIDPP_FF_EFFECTID_AUTOCENTER:
1993 wd->params[0] = data->slot_autocenter;
1994 break;
1995 case HIDPP_FF_EFFECTID_NONE:
1996 /* leave slot as zero */
1997 break;
1998 default:
1999 /* find current slot for effect */
2000 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2001 break;
2002 }
2003
2004 /* send command and wait for reply */
2005 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2006 wd->command, wd->params, wd->size, &response);
2007
2008 if (ret) {
2009 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2010 goto out;
2011 }
2012
2013 /* parse return data */
2014 switch (wd->command) {
2015 case HIDPP_FF_DOWNLOAD_EFFECT:
2016 slot = response.fap.params[0];
2017 if (slot > 0 && slot <= data->num_effects) {
2018 if (wd->effect_id >= 0)
2019 /* regular effect uploaded */
2020 data->effect_ids[slot-1] = wd->effect_id;
2021 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2022 /* autocenter spring uploaded */
2023 data->slot_autocenter = slot;
2024 }
2025 break;
2026 case HIDPP_FF_DESTROY_EFFECT:
2027 if (wd->effect_id >= 0)
2028 /* regular effect destroyed */
2029 data->effect_ids[wd->params[0]-1] = -1;
2030 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2031 /* autocenter spring destoyed */
2032 data->slot_autocenter = 0;
2033 break;
2034 case HIDPP_FF_SET_GLOBAL_GAINS:
2035 data->gain = (wd->params[0] << 8) + wd->params[1];
2036 break;
2037 case HIDPP_FF_SET_APERTURE:
2038 data->range = (wd->params[0] << 8) + wd->params[1];
2039 break;
2040 default:
2041 /* no action needed */
2042 break;
2043 }
2044
2045out:
2046 atomic_dec(&data->workqueue_size);
2047 kfree(wd);
2048}
2049
2050static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2051{
2052 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2053 int s;
2054
2055 if (!wd)
2056 return -ENOMEM;
2057
2058 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2059
2060 wd->data = data;
2061 wd->effect_id = effect_id;
2062 wd->command = command;
2063 wd->size = size;
2064 memcpy(wd->params, params, size);
2065
2066 atomic_inc(&data->workqueue_size);
2067 queue_work(data->wq, &wd->work);
2068
2069 /* warn about excessive queue size */
2070 s = atomic_read(&data->workqueue_size);
2071 if (s >= 20 && s % 20 == 0)
2072 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2073
2074 return 0;
2075}
2076
2077static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2078{
2079 struct hidpp_ff_private_data *data = dev->ff->private;
2080 u8 params[20];
2081 u8 size;
2082 int force;
2083
2084 /* set common parameters */
2085 params[2] = effect->replay.length >> 8;
2086 params[3] = effect->replay.length & 255;
2087 params[4] = effect->replay.delay >> 8;
2088 params[5] = effect->replay.delay & 255;
2089
2090 switch (effect->type) {
2091 case FF_CONSTANT:
2092 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2093 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2094 params[6] = force >> 8;
2095 params[7] = force & 255;
2096 params[8] = effect->u.constant.envelope.attack_level >> 7;
2097 params[9] = effect->u.constant.envelope.attack_length >> 8;
2098 params[10] = effect->u.constant.envelope.attack_length & 255;
2099 params[11] = effect->u.constant.envelope.fade_level >> 7;
2100 params[12] = effect->u.constant.envelope.fade_length >> 8;
2101 params[13] = effect->u.constant.envelope.fade_length & 255;
2102 size = 14;
2103 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2104 effect->u.constant.level,
2105 effect->direction, force);
2106 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2107 effect->u.constant.envelope.attack_level,
2108 effect->u.constant.envelope.attack_length,
2109 effect->u.constant.envelope.fade_level,
2110 effect->u.constant.envelope.fade_length);
2111 break;
2112 case FF_PERIODIC:
2113 {
2114 switch (effect->u.periodic.waveform) {
2115 case FF_SINE:
2116 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2117 break;
2118 case FF_SQUARE:
2119 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2120 break;
2121 case FF_SAW_UP:
2122 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2123 break;
2124 case FF_SAW_DOWN:
2125 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2126 break;
2127 case FF_TRIANGLE:
2128 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2129 break;
2130 default:
2131 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2132 return -EINVAL;
2133 }
2134 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2135 params[6] = effect->u.periodic.magnitude >> 8;
2136 params[7] = effect->u.periodic.magnitude & 255;
2137 params[8] = effect->u.periodic.offset >> 8;
2138 params[9] = effect->u.periodic.offset & 255;
2139 params[10] = effect->u.periodic.period >> 8;
2140 params[11] = effect->u.periodic.period & 255;
2141 params[12] = effect->u.periodic.phase >> 8;
2142 params[13] = effect->u.periodic.phase & 255;
2143 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2144 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2145 params[16] = effect->u.periodic.envelope.attack_length & 255;
2146 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2147 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2148 params[19] = effect->u.periodic.envelope.fade_length & 255;
2149 size = 20;
2150 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2151 effect->u.periodic.magnitude, effect->direction,
2152 effect->u.periodic.offset,
2153 effect->u.periodic.period,
2154 effect->u.periodic.phase);
2155 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2156 effect->u.periodic.envelope.attack_level,
2157 effect->u.periodic.envelope.attack_length,
2158 effect->u.periodic.envelope.fade_level,
2159 effect->u.periodic.envelope.fade_length);
2160 break;
2161 }
2162 case FF_RAMP:
2163 params[1] = HIDPP_FF_EFFECT_RAMP;
2164 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2165 params[6] = force >> 8;
2166 params[7] = force & 255;
2167 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2168 params[8] = force >> 8;
2169 params[9] = force & 255;
2170 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2171 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2172 params[12] = effect->u.ramp.envelope.attack_length & 255;
2173 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2174 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2175 params[15] = effect->u.ramp.envelope.fade_length & 255;
2176 size = 16;
2177 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2178 effect->u.ramp.start_level,
2179 effect->u.ramp.end_level,
2180 effect->direction, force);
2181 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2182 effect->u.ramp.envelope.attack_level,
2183 effect->u.ramp.envelope.attack_length,
2184 effect->u.ramp.envelope.fade_level,
2185 effect->u.ramp.envelope.fade_length);
2186 break;
2187 case FF_FRICTION:
2188 case FF_INERTIA:
2189 case FF_SPRING:
2190 case FF_DAMPER:
2191 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2192 params[6] = effect->u.condition[0].left_saturation >> 9;
2193 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2194 params[8] = effect->u.condition[0].left_coeff >> 8;
2195 params[9] = effect->u.condition[0].left_coeff & 255;
2196 params[10] = effect->u.condition[0].deadband >> 9;
2197 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2198 params[12] = effect->u.condition[0].center >> 8;
2199 params[13] = effect->u.condition[0].center & 255;
2200 params[14] = effect->u.condition[0].right_coeff >> 8;
2201 params[15] = effect->u.condition[0].right_coeff & 255;
2202 params[16] = effect->u.condition[0].right_saturation >> 9;
2203 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2204 size = 18;
2205 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2206 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2207 effect->u.condition[0].left_coeff,
2208 effect->u.condition[0].left_saturation,
2209 effect->u.condition[0].right_coeff,
2210 effect->u.condition[0].right_saturation);
2211 dbg_hid(" deadband=%d, center=%d\n",
2212 effect->u.condition[0].deadband,
2213 effect->u.condition[0].center);
2214 break;
2215 default:
2216 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2217 return -EINVAL;
2218 }
2219
2220 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2221}
2222
2223static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2224{
2225 struct hidpp_ff_private_data *data = dev->ff->private;
2226 u8 params[2];
2227
2228 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2229
2230 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2231
2232 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2233}
2234
2235static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2236{
2237 struct hidpp_ff_private_data *data = dev->ff->private;
2238 u8 slot = 0;
2239
2240 dbg_hid("Erasing effect %d.\n", effect_id);
2241
2242 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2243}
2244
2245static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2246{
2247 struct hidpp_ff_private_data *data = dev->ff->private;
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002248 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
Edwin Veldsff21a632016-01-11 00:25:15 +01002249
2250 dbg_hid("Setting autocenter to %d.\n", magnitude);
2251
2252 /* start a standard spring effect */
2253 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2254 /* zero delay and duration */
2255 params[2] = params[3] = params[4] = params[5] = 0;
2256 /* set coeff to 25% of saturation */
2257 params[8] = params[14] = magnitude >> 11;
2258 params[9] = params[15] = (magnitude >> 3) & 255;
2259 params[6] = params[16] = magnitude >> 9;
2260 params[7] = params[17] = (magnitude >> 1) & 255;
2261 /* zero deadband and center */
2262 params[10] = params[11] = params[12] = params[13] = 0;
2263
2264 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2265}
2266
2267static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2268{
2269 struct hidpp_ff_private_data *data = dev->ff->private;
2270 u8 params[4];
2271
2272 dbg_hid("Setting gain to %d.\n", gain);
2273
2274 params[0] = gain >> 8;
2275 params[1] = gain & 255;
2276 params[2] = 0; /* no boost */
2277 params[3] = 0;
2278
2279 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2280}
2281
2282static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2283{
2284 struct hid_device *hid = to_hid_device(dev);
2285 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2286 struct input_dev *idev = hidinput->input;
2287 struct hidpp_ff_private_data *data = idev->ff->private;
2288
2289 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2290}
2291
2292static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2293{
2294 struct hid_device *hid = to_hid_device(dev);
2295 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2296 struct input_dev *idev = hidinput->input;
2297 struct hidpp_ff_private_data *data = idev->ff->private;
2298 u8 params[2];
2299 int range = simple_strtoul(buf, NULL, 10);
2300
2301 range = clamp(range, 180, 900);
2302
2303 params[0] = range >> 8;
2304 params[1] = range & 0x00FF;
2305
2306 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2307
2308 return count;
2309}
2310
2311static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2312
2313static void hidpp_ff_destroy(struct ff_device *ff)
2314{
2315 struct hidpp_ff_private_data *data = ff->private;
Andrey Smirnov08c453f2019-10-17 21:45:17 -07002316 struct hid_device *hid = data->hidpp->hid_dev;
Edwin Veldsff21a632016-01-11 00:25:15 +01002317
Andrey Smirnov08c453f2019-10-17 21:45:17 -07002318 hid_info(hid, "Unloading HID++ force feedback.\n");
2319
2320 device_remove_file(&hid->dev, &dev_attr_range);
2321 destroy_workqueue(data->wq);
Edwin Veldsff21a632016-01-11 00:25:15 +01002322 kfree(data->effect_ids);
2323}
2324
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002325static int hidpp_ff_init(struct hidpp_device *hidpp,
2326 struct hidpp_ff_private_data *data)
Edwin Veldsff21a632016-01-11 00:25:15 +01002327{
2328 struct hid_device *hid = hidpp->hid_dev;
Alan Sternd9d4b1e2019-10-03 14:53:59 -04002329 struct hid_input *hidinput;
2330 struct input_dev *dev;
Edwin Veldsff21a632016-01-11 00:25:15 +01002331 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2332 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2333 struct ff_device *ff;
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002334 int error, j, num_slots = data->num_effects;
Edwin Veldsff21a632016-01-11 00:25:15 +01002335 u8 version;
2336
Alan Sternd9d4b1e2019-10-03 14:53:59 -04002337 if (list_empty(&hid->inputs)) {
2338 hid_err(hid, "no inputs found\n");
2339 return -ENODEV;
2340 }
2341 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2342 dev = hidinput->input;
2343
Edwin Veldsff21a632016-01-11 00:25:15 +01002344 if (!dev) {
2345 hid_err(hid, "Struct input_dev not set!\n");
2346 return -EINVAL;
2347 }
2348
2349 /* Get firmware release */
2350 version = bcdDevice & 255;
2351
2352 /* Set supported force feedback capabilities */
Peter Huttererfef33602018-12-05 10:42:25 +10002353 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2354 set_bit(hidpp_ff_effects[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002355 if (version > 1)
Peter Huttererfef33602018-12-05 10:42:25 +10002356 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2357 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
Edwin Veldsff21a632016-01-11 00:25:15 +01002358
Edwin Veldsff21a632016-01-11 00:25:15 +01002359 error = input_ff_create(dev, num_slots);
2360
2361 if (error) {
2362 hid_err(dev, "Failed to create FF device!\n");
2363 return error;
2364 }
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002365 /*
2366 * Create a copy of passed data, so we can transfer memory
2367 * ownership to FF core
2368 */
2369 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
Edwin Veldsff21a632016-01-11 00:25:15 +01002370 if (!data)
2371 return -ENOMEM;
2372 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2373 if (!data->effect_ids) {
2374 kfree(data);
2375 return -ENOMEM;
2376 }
Kangjie Lu6c44b152019-03-14 00:24:02 -05002377 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2378 if (!data->wq) {
2379 kfree(data->effect_ids);
2380 kfree(data);
2381 return -ENOMEM;
2382 }
2383
Edwin Veldsff21a632016-01-11 00:25:15 +01002384 data->hidpp = hidpp;
Edwin Veldsff21a632016-01-11 00:25:15 +01002385 data->version = version;
Edwin Veldsff21a632016-01-11 00:25:15 +01002386 for (j = 0; j < num_slots; j++)
2387 data->effect_ids[j] = -1;
2388
2389 ff = dev->ff;
2390 ff->private = data;
2391
2392 ff->upload = hidpp_ff_upload_effect;
2393 ff->erase = hidpp_ff_erase_effect;
2394 ff->playback = hidpp_ff_playback;
2395 ff->set_gain = hidpp_ff_set_gain;
2396 ff->set_autocenter = hidpp_ff_set_autocenter;
2397 ff->destroy = hidpp_ff_destroy;
2398
Edwin Veldsff21a632016-01-11 00:25:15 +01002399 /* Create sysfs interface */
2400 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2401 if (error)
2402 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2403
Edwin Veldsff21a632016-01-11 00:25:15 +01002404 /* init the hardware command queue */
Edwin Veldsff21a632016-01-11 00:25:15 +01002405 atomic_set(&data->workqueue_size, 0);
2406
Colin Ian Kingdf47b242017-07-14 14:37:12 +01002407 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2408 version);
Edwin Veldsff21a632016-01-11 00:25:15 +01002409
2410 return 0;
2411}
2412
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002413/* ************************************************************************** */
2414/* */
2415/* Device Support */
2416/* */
2417/* ************************************************************************** */
2418
2419/* -------------------------------------------------------------------------- */
2420/* Touchpad HID++ devices */
2421/* -------------------------------------------------------------------------- */
2422
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002423#define WTP_MANUAL_RESOLUTION 39
2424
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002425struct wtp_data {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002426 u16 x_size, y_size;
2427 u8 finger_count;
2428 u8 mt_feature_index;
2429 u8 button_feature_index;
2430 u8 maxcontacts;
2431 bool flip_y;
2432 unsigned int resolution;
2433};
2434
2435static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2436 struct hid_field *field, struct hid_usage *usage,
2437 unsigned long **bit, int *max)
2438{
2439 return -1;
2440}
2441
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002442static void wtp_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02002443 struct input_dev *input_dev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002444{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002445 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002446
2447 __set_bit(EV_ABS, input_dev->evbit);
2448 __set_bit(EV_KEY, input_dev->evbit);
2449 __clear_bit(EV_REL, input_dev->evbit);
2450 __clear_bit(EV_LED, input_dev->evbit);
2451
2452 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2453 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2454 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2455 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2456
2457 /* Max pressure is not given by the devices, pick one */
2458 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2459
2460 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2461
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002462 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2463 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2464 else
2465 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002466
2467 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2468 INPUT_MT_DROP_UNUSED);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002469}
2470
Hans de Goede06104302019-04-20 13:22:13 +02002471static void wtp_touch_event(struct hidpp_device *hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002472 struct hidpp_touchpad_raw_xy_finger *touch_report)
2473{
Hans de Goede06104302019-04-20 13:22:13 +02002474 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002475 int slot;
2476
2477 if (!touch_report->finger_id || touch_report->contact_type)
2478 /* no actual data */
2479 return;
2480
Hans de Goede06104302019-04-20 13:22:13 +02002481 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002482
Hans de Goede06104302019-04-20 13:22:13 +02002483 input_mt_slot(hidpp->input, slot);
2484 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002485 touch_report->contact_status);
2486 if (touch_report->contact_status) {
Hans de Goede06104302019-04-20 13:22:13 +02002487 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002488 touch_report->x);
Hans de Goede06104302019-04-20 13:22:13 +02002489 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002490 wd->flip_y ? wd->y_size - touch_report->y :
2491 touch_report->y);
Hans de Goede06104302019-04-20 13:22:13 +02002492 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002493 touch_report->area);
2494 }
2495}
2496
2497static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2498 struct hidpp_touchpad_raw_xy *raw)
2499{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002500 int i;
2501
2502 for (i = 0; i < 2; i++)
Hans de Goede06104302019-04-20 13:22:13 +02002503 wtp_touch_event(hidpp, &(raw->fingers[i]));
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002504
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002505 if (raw->end_of_frame &&
2506 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Hans de Goede06104302019-04-20 13:22:13 +02002507 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002508
2509 if (raw->end_of_frame || raw->finger_count <= 2) {
Hans de Goede06104302019-04-20 13:22:13 +02002510 input_mt_sync_frame(hidpp->input);
2511 input_sync(hidpp->input);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002512 }
2513}
2514
2515static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2516{
2517 struct wtp_data *wd = hidpp->private_data;
2518 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2519 (data[7] >> 4) * (data[7] >> 4)) / 2;
2520 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2521 (data[13] >> 4) * (data[13] >> 4)) / 2;
2522 struct hidpp_touchpad_raw_xy raw = {
2523 .timestamp = data[1],
2524 .fingers = {
2525 {
2526 .contact_type = 0,
2527 .contact_status = !!data[7],
2528 .x = get_unaligned_le16(&data[3]),
2529 .y = get_unaligned_le16(&data[5]),
2530 .z = c1_area,
2531 .area = c1_area,
2532 .finger_id = data[2],
2533 }, {
2534 .contact_type = 0,
2535 .contact_status = !!data[13],
2536 .x = get_unaligned_le16(&data[9]),
2537 .y = get_unaligned_le16(&data[11]),
2538 .z = c2_area,
2539 .area = c2_area,
2540 .finger_id = data[8],
2541 }
2542 },
2543 .finger_count = wd->maxcontacts,
2544 .spurious_flag = 0,
2545 .end_of_frame = (data[0] >> 7) == 0,
2546 .button = data[0] & 0x01,
2547 };
2548
2549 wtp_send_raw_xy_event(hidpp, &raw);
2550
2551 return 1;
2552}
2553
2554static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2555{
2556 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2557 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002558 struct hidpp_report *report = (struct hidpp_report *)data;
2559 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002560
Hans de Goede06104302019-04-20 13:22:13 +02002561 if (!wd || !hidpp->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002562 return 1;
2563
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002564 switch (data[0]) {
2565 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +01002566 if (size < 2) {
2567 hid_err(hdev, "Received HID report of bad size (%d)",
2568 size);
2569 return 1;
2570 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002571 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
Hans de Goede06104302019-04-20 13:22:13 +02002572 input_event(hidpp->input, EV_KEY, BTN_LEFT,
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002573 !!(data[1] & 0x01));
Hans de Goede06104302019-04-20 13:22:13 +02002574 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002575 !!(data[1] & 0x02));
Hans de Goede06104302019-04-20 13:22:13 +02002576 input_sync(hidpp->input);
Peter Wu8abd8202014-12-16 01:50:16 +01002577 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002578 } else {
2579 if (size < 21)
2580 return 1;
2581 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2582 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002583 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +01002584 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002585 if ((report->fap.feature_index != wd->mt_feature_index) ||
2586 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2587 return 1;
2588 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2589
2590 wtp_send_raw_xy_event(hidpp, &raw);
2591 return 0;
2592 }
2593
2594 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002595}
2596
2597static int wtp_get_config(struct hidpp_device *hidpp)
2598{
2599 struct wtp_data *wd = hidpp->private_data;
2600 struct hidpp_touchpad_raw_info raw_info = {0};
2601 u8 feature_type;
2602 int ret;
2603
2604 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2605 &wd->mt_feature_index, &feature_type);
2606 if (ret)
2607 /* means that the device is not powered up */
2608 return ret;
2609
2610 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2611 &raw_info);
2612 if (ret)
2613 return ret;
2614
2615 wd->x_size = raw_info.x_size;
2616 wd->y_size = raw_info.y_size;
2617 wd->maxcontacts = raw_info.maxcontacts;
2618 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2619 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002620 if (!wd->resolution)
2621 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002622
2623 return 0;
2624}
2625
2626static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2627{
2628 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2629 struct wtp_data *wd;
2630
2631 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2632 GFP_KERNEL);
2633 if (!wd)
2634 return -ENOMEM;
2635
2636 hidpp->private_data = wd;
2637
2638 return 0;
2639};
2640
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002641static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002642{
2643 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2644 struct wtp_data *wd = hidpp->private_data;
2645 int ret;
2646
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002647 if (!wd->x_size) {
2648 ret = wtp_get_config(hidpp);
2649 if (ret) {
2650 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002651 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002652 }
2653 }
2654
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002655 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002656 true, true);
2657}
2658
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002659/* ------------------------------------------------------------------------- */
2660/* Logitech M560 devices */
2661/* ------------------------------------------------------------------------- */
2662
2663/*
2664 * Logitech M560 protocol overview
2665 *
2666 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2667 * the sides buttons are pressed, it sends some keyboard keys events
2668 * instead of buttons ones.
2669 * To complicate things further, the middle button keys sequence
2670 * is different from the odd press and the even press.
2671 *
2672 * forward button -> Super_R
2673 * backward button -> Super_L+'d' (press only)
2674 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2675 * 2nd time: left-click (press only)
2676 * NB: press-only means that when the button is pressed, the
2677 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2678 * together sequentially; instead when the button is released, no event is
2679 * generated !
2680 *
2681 * With the command
2682 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2683 * the mouse reacts differently:
2684 * - it never sends a keyboard key event
2685 * - for the three mouse button it sends:
2686 * middle button press 11<xx>0a 3500af00...
2687 * side 1 button (forward) press 11<xx>0a 3500b000...
2688 * side 2 button (backward) press 11<xx>0a 3500ae00...
2689 * middle/side1/side2 button release 11<xx>0a 35000000...
2690 */
2691
2692static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2693
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002694/* how buttons are mapped in the report */
2695#define M560_MOUSE_BTN_LEFT 0x01
2696#define M560_MOUSE_BTN_RIGHT 0x02
2697#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2698#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2699
2700#define M560_SUB_ID 0x0a
2701#define M560_BUTTON_MODE_REGISTER 0x35
2702
2703static int m560_send_config_command(struct hid_device *hdev, bool connected)
2704{
2705 struct hidpp_report response;
2706 struct hidpp_device *hidpp_dev;
2707
2708 hidpp_dev = hid_get_drvdata(hdev);
2709
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002710 return hidpp_send_rap_command_sync(
2711 hidpp_dev,
2712 REPORT_ID_HIDPP_SHORT,
2713 M560_SUB_ID,
2714 M560_BUTTON_MODE_REGISTER,
2715 (u8 *)m560_config_parameter,
2716 sizeof(m560_config_parameter),
2717 &response
2718 );
2719}
2720
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002721static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2722{
2723 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002724
2725 /* sanity check */
Hans de Goede06104302019-04-20 13:22:13 +02002726 if (!hidpp->input) {
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002727 hid_err(hdev, "error in parameter\n");
2728 return -EINVAL;
2729 }
2730
2731 if (size < 7) {
2732 hid_err(hdev, "error in report\n");
2733 return 0;
2734 }
2735
2736 if (data[0] == REPORT_ID_HIDPP_LONG &&
2737 data[2] == M560_SUB_ID && data[6] == 0x00) {
2738 /*
2739 * m560 mouse report for middle, forward and backward button
2740 *
2741 * data[0] = 0x11
2742 * data[1] = device-id
2743 * data[2] = 0x0a
2744 * data[5] = 0xaf -> middle
2745 * 0xb0 -> forward
2746 * 0xae -> backward
2747 * 0x00 -> release all
2748 * data[6] = 0x00
2749 */
2750
2751 switch (data[5]) {
2752 case 0xaf:
Hans de Goede06104302019-04-20 13:22:13 +02002753 input_report_key(hidpp->input, BTN_MIDDLE, 1);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002754 break;
2755 case 0xb0:
Hans de Goede06104302019-04-20 13:22:13 +02002756 input_report_key(hidpp->input, BTN_FORWARD, 1);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002757 break;
2758 case 0xae:
Hans de Goede06104302019-04-20 13:22:13 +02002759 input_report_key(hidpp->input, BTN_BACK, 1);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002760 break;
2761 case 0x00:
Hans de Goede06104302019-04-20 13:22:13 +02002762 input_report_key(hidpp->input, BTN_BACK, 0);
2763 input_report_key(hidpp->input, BTN_FORWARD, 0);
2764 input_report_key(hidpp->input, BTN_MIDDLE, 0);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002765 break;
2766 default:
2767 hid_err(hdev, "error in report\n");
2768 return 0;
2769 }
Hans de Goede06104302019-04-20 13:22:13 +02002770 input_sync(hidpp->input);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002771
2772 } else if (data[0] == 0x02) {
2773 /*
2774 * Logitech M560 mouse report
2775 *
2776 * data[0] = type (0x02)
2777 * data[1..2] = buttons
2778 * data[3..5] = xy
2779 * data[6] = wheel
2780 */
2781
2782 int v;
2783
Hans de Goede06104302019-04-20 13:22:13 +02002784 input_report_key(hidpp->input, BTN_LEFT,
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002785 !!(data[1] & M560_MOUSE_BTN_LEFT));
Hans de Goede06104302019-04-20 13:22:13 +02002786 input_report_key(hidpp->input, BTN_RIGHT,
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002787 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2788
Harry Cutts4435ff22018-12-05 10:42:27 +10002789 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
Hans de Goede06104302019-04-20 13:22:13 +02002790 input_report_rel(hidpp->input, REL_HWHEEL, -1);
2791 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
Harry Cutts4435ff22018-12-05 10:42:27 +10002792 -120);
2793 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
Hans de Goede06104302019-04-20 13:22:13 +02002794 input_report_rel(hidpp->input, REL_HWHEEL, 1);
2795 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
Harry Cutts4435ff22018-12-05 10:42:27 +10002796 120);
2797 }
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002798
2799 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
Hans de Goede06104302019-04-20 13:22:13 +02002800 input_report_rel(hidpp->input, REL_X, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002801
2802 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
Hans de Goede06104302019-04-20 13:22:13 +02002803 input_report_rel(hidpp->input, REL_Y, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002804
2805 v = hid_snto32(data[6], 8);
Peter Huttererfd35759c2019-03-20 08:48:23 +10002806 if (v != 0)
Hans de Goede06104302019-04-20 13:22:13 +02002807 hidpp_scroll_counter_handle_scroll(hidpp->input,
Peter Huttererfd35759c2019-03-20 08:48:23 +10002808 &hidpp->vertical_wheel_counter, v);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002809
Hans de Goede06104302019-04-20 13:22:13 +02002810 input_sync(hidpp->input);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002811 }
2812
2813 return 1;
2814}
2815
2816static void m560_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02002817 struct input_dev *input_dev)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002818{
Hans de Goede06104302019-04-20 13:22:13 +02002819 __set_bit(EV_KEY, input_dev->evbit);
2820 __set_bit(BTN_MIDDLE, input_dev->keybit);
2821 __set_bit(BTN_RIGHT, input_dev->keybit);
2822 __set_bit(BTN_LEFT, input_dev->keybit);
2823 __set_bit(BTN_BACK, input_dev->keybit);
2824 __set_bit(BTN_FORWARD, input_dev->keybit);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002825
Hans de Goede06104302019-04-20 13:22:13 +02002826 __set_bit(EV_REL, input_dev->evbit);
2827 __set_bit(REL_X, input_dev->relbit);
2828 __set_bit(REL_Y, input_dev->relbit);
2829 __set_bit(REL_WHEEL, input_dev->relbit);
2830 __set_bit(REL_HWHEEL, input_dev->relbit);
2831 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2832 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002833}
2834
2835static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2836 struct hid_field *field, struct hid_usage *usage,
2837 unsigned long **bit, int *max)
2838{
2839 return -1;
2840}
2841
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002842/* ------------------------------------------------------------------------- */
2843/* Logitech K400 devices */
2844/* ------------------------------------------------------------------------- */
2845
2846/*
2847 * The Logitech K400 keyboard has an embedded touchpad which is seen
2848 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2849 * tap-to-click but the setting is not remembered accross reset, annoying some
2850 * users.
2851 *
2852 * We can toggle this feature from the host by using the feature 0x6010:
2853 * Touchpad FW items
2854 */
2855
2856struct k400_private_data {
2857 u8 feature_index;
2858};
2859
2860static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2861{
2862 struct k400_private_data *k400 = hidpp->private_data;
2863 struct hidpp_touchpad_fw_items items = {};
2864 int ret;
2865 u8 feature_type;
2866
2867 if (!k400->feature_index) {
2868 ret = hidpp_root_get_feature(hidpp,
2869 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2870 &k400->feature_index, &feature_type);
2871 if (ret)
2872 /* means that the device is not powered up */
2873 return ret;
2874 }
2875
2876 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2877 if (ret)
2878 return ret;
2879
2880 return 0;
2881}
2882
2883static int k400_allocate(struct hid_device *hdev)
2884{
2885 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2886 struct k400_private_data *k400;
2887
2888 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2889 GFP_KERNEL);
2890 if (!k400)
2891 return -ENOMEM;
2892
2893 hidpp->private_data = k400;
2894
2895 return 0;
2896};
2897
2898static int k400_connect(struct hid_device *hdev, bool connected)
2899{
2900 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2901
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002902 if (!disable_tap_to_click)
2903 return 0;
2904
2905 return k400_disable_tap_to_click(hidpp);
2906}
2907
Simon Wood7f4b49f2015-11-19 16:42:13 -07002908/* ------------------------------------------------------------------------- */
2909/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2910/* ------------------------------------------------------------------------- */
2911
2912#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2913
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002914static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
2915 struct hidpp_ff_private_data *data)
Simon Wood7f4b49f2015-11-19 16:42:13 -07002916{
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002917 struct hidpp_report response;
2918 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
2919 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
2920 };
Simon Wood7f4b49f2015-11-19 16:42:13 -07002921 int ret;
2922
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002923 /* initialize with zero autocenter to get wheel in usable state */
2924
2925 dbg_hid("Setting autocenter to 0.\n");
2926 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2927 HIDPP_FF_DOWNLOAD_EFFECT,
2928 params, ARRAY_SIZE(params),
2929 &response);
2930 if (ret)
2931 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
2932 else
2933 data->slot_autocenter = response.fap.params[0];
2934
2935 return ret;
2936}
2937
2938static int g920_get_config(struct hidpp_device *hidpp,
2939 struct hidpp_ff_private_data *data)
2940{
2941 struct hidpp_report response;
2942 u8 feature_type;
2943 int ret;
2944
2945 memset(data, 0, sizeof(*data));
2946
Simon Wood7f4b49f2015-11-19 16:42:13 -07002947 /* Find feature and store for later use */
2948 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002949 &data->feature_index, &feature_type);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002950 if (ret)
2951 return ret;
2952
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002953 /* Read number of slots available in device */
2954 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2955 HIDPP_FF_GET_INFO,
2956 NULL, 0,
2957 &response);
2958 if (ret) {
2959 if (ret < 0)
2960 return ret;
2961 hid_err(hidpp->hid_dev,
2962 "%s: received protocol error 0x%02x\n", __func__, ret);
2963 return -EPROTO;
2964 }
Simon Wood7f4b49f2015-11-19 16:42:13 -07002965
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07002966 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2967
2968 /* reset all forces */
2969 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2970 HIDPP_FF_RESET_ALL,
2971 NULL, 0,
2972 &response);
2973 if (ret)
2974 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
2975
2976 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2977 HIDPP_FF_GET_APERTURE,
2978 NULL, 0,
2979 &response);
2980 if (ret) {
2981 hid_warn(hidpp->hid_dev,
2982 "Failed to read range from device!\n");
2983 }
2984 data->range = ret ?
2985 900 : get_unaligned_be16(&response.fap.params[0]);
2986
2987 /* Read the current gain values */
2988 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
2989 HIDPP_FF_GET_GLOBAL_GAINS,
2990 NULL, 0,
2991 &response);
2992 if (ret)
2993 hid_warn(hidpp->hid_dev,
2994 "Failed to read gain values from device!\n");
2995 data->gain = ret ?
2996 0xffff : get_unaligned_be16(&response.fap.params[0]);
2997
2998 /* ignore boost value at response.fap.params[2] */
2999
3000 return g920_ff_set_autocenter(hidpp, data);
Simon Wood7f4b49f2015-11-19 16:42:13 -07003001}
3002
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003003/* -------------------------------------------------------------------------- */
Hans de Goedeb4c00e72020-11-14 22:20:56 +01003004/* Logitech Dinovo Mini keyboard with builtin touchpad */
3005/* -------------------------------------------------------------------------- */
3006#define DINOVO_MINI_PRODUCT_ID 0xb30c
3007
3008static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3009 struct hid_field *field, struct hid_usage *usage,
3010 unsigned long **bit, int *max)
3011{
3012 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3013 return 0;
3014
3015 switch (usage->hid & HID_USAGE) {
3016 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3017 default:
3018 return 0;
3019 }
3020 return 1;
3021}
3022
3023/* -------------------------------------------------------------------------- */
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003024/* HID++1.0 devices which use HID++ reports for their wheels */
3025/* -------------------------------------------------------------------------- */
3026static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3027{
3028 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3029 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3030 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3031}
3032
3033static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3034 u8 *data, int size)
3035{
3036 s8 value, hvalue;
3037
3038 if (!hidpp->input)
3039 return -EINVAL;
3040
3041 if (size < 7)
3042 return 0;
3043
3044 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3045 return 0;
3046
3047 value = data[3];
3048 hvalue = data[4];
3049
3050 input_report_rel(hidpp->input, REL_WHEEL, value);
3051 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3052 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3053 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3054 input_sync(hidpp->input);
3055
3056 return 1;
3057}
3058
3059static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3060 struct input_dev *input_dev)
3061{
3062 __set_bit(EV_REL, input_dev->evbit);
3063 __set_bit(REL_WHEEL, input_dev->relbit);
3064 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3065 __set_bit(REL_HWHEEL, input_dev->relbit);
3066 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3067}
3068
3069/* -------------------------------------------------------------------------- */
Hans de Goede7457bc12019-04-20 13:22:16 +02003070/* HID++1.0 mice which use HID++ reports for extra mouse buttons */
3071/* -------------------------------------------------------------------------- */
3072static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3073{
3074 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3075 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3076 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3077}
3078
3079static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3080 u8 *data, int size)
3081{
3082 int i;
3083
3084 if (!hidpp->input)
3085 return -EINVAL;
3086
3087 if (size < 7)
3088 return 0;
3089
3090 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3091 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3092 return 0;
3093
3094 /*
3095 * Buttons are either delivered through the regular mouse report *or*
3096 * through the extra buttons report. At least for button 6 how it is
3097 * delivered differs per receiver firmware version. Even receivers with
3098 * the same usb-id show different behavior, so we handle both cases.
3099 */
3100 for (i = 0; i < 8; i++)
3101 input_report_key(hidpp->input, BTN_MOUSE + i,
3102 (data[3] & (1 << i)));
3103
3104 /* Some mice report events on button 9+, use BTN_MISC */
3105 for (i = 0; i < 8; i++)
3106 input_report_key(hidpp->input, BTN_MISC + i,
3107 (data[4] & (1 << i)));
3108
3109 input_sync(hidpp->input);
3110 return 1;
3111}
3112
3113static void hidpp10_extra_mouse_buttons_populate_input(
3114 struct hidpp_device *hidpp, struct input_dev *input_dev)
3115{
3116 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
3117 __set_bit(BTN_0, input_dev->keybit);
3118 __set_bit(BTN_1, input_dev->keybit);
3119 __set_bit(BTN_2, input_dev->keybit);
3120 __set_bit(BTN_3, input_dev->keybit);
3121 __set_bit(BTN_4, input_dev->keybit);
3122 __set_bit(BTN_5, input_dev->keybit);
3123 __set_bit(BTN_6, input_dev->keybit);
3124 __set_bit(BTN_7, input_dev->keybit);
3125}
3126
3127/* -------------------------------------------------------------------------- */
Hans de Goede42bc4f32019-04-20 13:22:17 +02003128/* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
3129/* -------------------------------------------------------------------------- */
3130
3131/* Find the consumer-page input report desc and change Maximums to 0x107f */
3132static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3133 u8 *_rdesc, unsigned int *rsize)
3134{
3135 /* Note 0 terminated so we can use strnstr to search for this. */
Colin Ian Kinga96a8a52019-05-10 14:17:22 +01003136 static const char consumer_rdesc_start[] = {
Hans de Goede42bc4f32019-04-20 13:22:17 +02003137 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
3138 0x09, 0x01, /* USAGE (Consumer Control) */
3139 0xA1, 0x01, /* COLLECTION (Application) */
3140 0x85, 0x03, /* REPORT_ID = 3 */
3141 0x75, 0x10, /* REPORT_SIZE (16) */
3142 0x95, 0x02, /* REPORT_COUNT (2) */
3143 0x15, 0x01, /* LOGICAL_MIN (1) */
3144 0x26, 0x00 /* LOGICAL_MAX (... */
3145 };
3146 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3147 unsigned int size;
3148
3149 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3150 size = *rsize - (consumer_rdesc - rdesc);
3151 if (consumer_rdesc && size >= 25) {
3152 consumer_rdesc[15] = 0x7f;
3153 consumer_rdesc[16] = 0x10;
3154 consumer_rdesc[20] = 0x7f;
3155 consumer_rdesc[21] = 0x10;
3156 }
3157 return _rdesc;
3158}
3159
3160static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3161{
3162 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3163 HIDPP_ENABLE_CONSUMER_REPORT,
3164 HIDPP_ENABLE_CONSUMER_REPORT);
3165}
3166
3167static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3168 u8 *data, int size)
3169{
3170 u8 consumer_report[5];
3171
3172 if (size < 7)
3173 return 0;
3174
3175 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3176 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3177 return 0;
3178
3179 /*
3180 * Build a normal consumer report (3) out of the data, this detour
3181 * is necessary to get some keyboards to report their 0x10xx usages.
3182 */
3183 consumer_report[0] = 0x03;
3184 memcpy(&consumer_report[1], &data[3], 4);
3185 /* We are called from atomic context */
3186 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3187 consumer_report, 5, 1);
3188
3189 return 1;
3190}
3191
3192/* -------------------------------------------------------------------------- */
Harry Cutts4435ff22018-12-05 10:42:27 +10003193/* High-resolution scroll wheels */
3194/* -------------------------------------------------------------------------- */
3195
3196static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3197{
3198 int ret;
3199 u8 multiplier = 1;
3200
3201 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
3202 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3203 if (ret == 0)
3204 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3205 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
3206 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3207 &multiplier);
3208 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
3209 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3210 multiplier = 8;
3211 }
3212 if (ret)
3213 return ret;
3214
3215 if (multiplier == 0)
3216 multiplier = 1;
3217
3218 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
Maciej S. Szmigieroe13762a2020-07-05 19:34:57 +02003219 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
Harry Cutts4435ff22018-12-05 10:42:27 +10003220 return 0;
3221}
3222
3223/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003224/* Generic HID++ devices */
3225/* -------------------------------------------------------------------------- */
3226
Hans de Goede42bc4f32019-04-20 13:22:17 +02003227static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3228 unsigned int *rsize)
3229{
3230 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3231
3232 if (!hidpp)
3233 return rdesc;
3234
3235 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
3236 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3237 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3238 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3239
3240 return rdesc;
3241}
3242
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003243static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3244 struct hid_field *field, struct hid_usage *usage,
3245 unsigned long **bit, int *max)
3246{
3247 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3248
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003249 if (!hidpp)
3250 return 0;
3251
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003252 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3253 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003254 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3255 field->application != HID_GD_MOUSE)
3256 return m560_input_mapping(hdev, hi, field, usage, bit, max);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003257
Hans de Goedeb4c00e72020-11-14 22:20:56 +01003258 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3259 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3260
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003261 return 0;
3262}
3263
Simon Wood0b1804e2015-11-19 16:42:15 -07003264static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3265 struct hid_field *field, struct hid_usage *usage,
3266 unsigned long **bit, int *max)
3267{
3268 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3269
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003270 if (!hidpp)
3271 return 0;
3272
Simon Wood0b1804e2015-11-19 16:42:15 -07003273 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3274 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3275 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3276 usage->code == ABS_Y || usage->code == ABS_Z ||
3277 usage->code == ABS_RZ)) {
3278 field->application = HID_GD_MULTIAXIS;
3279 }
3280 }
3281
3282 return 0;
3283}
3284
3285
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003286static void hidpp_populate_input(struct hidpp_device *hidpp,
Hans de Goedee54abaf2019-04-20 13:22:09 +02003287 struct input_dev *input)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003288{
Hans de Goede06104302019-04-20 13:22:13 +02003289 hidpp->input = input;
3290
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003291 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
Hans de Goedee54abaf2019-04-20 13:22:09 +02003292 wtp_populate_input(hidpp, input);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003293 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
Hans de Goedee54abaf2019-04-20 13:22:09 +02003294 m560_populate_input(hidpp, input);
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003295
3296 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3297 hidpp10_wheel_populate_input(hidpp, input);
Hans de Goede7457bc12019-04-20 13:22:16 +02003298
3299 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3300 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003301}
3302
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07003303static int hidpp_input_configured(struct hid_device *hdev,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003304 struct hid_input *hidinput)
3305{
3306 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003307 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003308
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003309 if (!hidpp)
3310 return 0;
3311
Hans de Goedee54abaf2019-04-20 13:22:09 +02003312 hidpp_populate_input(hidpp, input);
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07003313
3314 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003315}
3316
3317static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3318 int size)
3319{
3320 struct hidpp_report *question = hidpp->send_receive_buf;
3321 struct hidpp_report *answer = hidpp->send_receive_buf;
3322 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02003323 int ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003324
3325 /*
3326 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +01003327 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003328 */
3329 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3330 /*
3331 * Check for a correct hidpp20 answer or the corresponding
3332 * error
3333 */
3334 if (hidpp_match_answer(question, report) ||
3335 hidpp_match_error(question, report)) {
3336 *answer = *report;
3337 hidpp->answer_available = true;
3338 wake_up(&hidpp->wait);
3339 /*
3340 * This was an answer to a command that this driver sent
3341 * We return 1 to hid-core to avoid forwarding the
3342 * command upstream as it has been treated by the driver
3343 */
3344
3345 return 1;
3346 }
3347 }
3348
Mazin Rezk0da0a63b2019-10-27 17:44:13 +00003349 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003350 atomic_set(&hidpp->connected,
3351 !(report->rap.params[0] & (1 << 6)));
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003352 if (schedule_work(&hidpp->work) == 0)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003353 dbg_hid("%s: connect event already queued\n", __func__);
3354 return 1;
3355 }
3356
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02003357 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3358 ret = hidpp20_battery_event(hidpp, data, size);
3359 if (ret != 0)
3360 return ret;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003361 ret = hidpp_solar_battery_event(hidpp, data, size);
3362 if (ret != 0)
3363 return ret;
Pedro Vanzellabe281362019-10-26 19:25:06 -03003364 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3365 if (ret != 0)
3366 return ret;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02003367 }
3368
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003369 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3370 ret = hidpp10_battery_event(hidpp, data, size);
3371 if (ret != 0)
3372 return ret;
3373 }
3374
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003375 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3376 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3377 if (ret != 0)
3378 return ret;
3379 }
3380
Hans de Goede7457bc12019-04-20 13:22:16 +02003381 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3382 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3383 if (ret != 0)
3384 return ret;
3385 }
3386
Hans de Goede42bc4f32019-04-20 13:22:17 +02003387 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3388 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3389 if (ret != 0)
3390 return ret;
3391 }
3392
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003393 return 0;
3394}
3395
3396static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3397 u8 *data, int size)
3398{
3399 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01003400 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003401
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003402 if (!hidpp)
3403 return 0;
3404
Peter Wue529fea2014-12-17 00:23:51 +01003405 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003406 switch (data[0]) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07003407 case REPORT_ID_HIDPP_VERY_LONG:
Hans de Goeded71b18f2019-04-20 13:22:12 +02003408 if (size != hidpp->very_long_report_length) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07003409 hid_err(hdev, "received hid++ report of bad size (%d)",
3410 size);
3411 return 1;
3412 }
3413 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3414 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003415 case REPORT_ID_HIDPP_LONG:
3416 if (size != HIDPP_REPORT_LONG_LENGTH) {
3417 hid_err(hdev, "received hid++ report of bad size (%d)",
3418 size);
3419 return 1;
3420 }
Peter Wue529fea2014-12-17 00:23:51 +01003421 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3422 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003423 case REPORT_ID_HIDPP_SHORT:
3424 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3425 hid_err(hdev, "received hid++ report of bad size (%d)",
3426 size);
3427 return 1;
3428 }
Peter Wue529fea2014-12-17 00:23:51 +01003429 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3430 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003431 }
3432
Peter Wue529fea2014-12-17 00:23:51 +01003433 /* If no report is available for further processing, skip calling
3434 * raw_event of subclasses. */
3435 if (ret != 0)
3436 return ret;
3437
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003438 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3439 return wtp_raw_event(hdev, data, size);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003440 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3441 return m560_raw_event(hdev, data, size);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003442
3443 return 0;
3444}
3445
Harry Cutts4435ff22018-12-05 10:42:27 +10003446static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3447 struct hid_usage *usage, __s32 value)
3448{
3449 /* This function will only be called for scroll events, due to the
3450 * restriction imposed in hidpp_usages.
3451 */
3452 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003453 struct hidpp_scroll_counter *counter;
3454
3455 if (!hidpp)
3456 return 0;
3457
3458 counter = &hidpp->vertical_wheel_counter;
Harry Cutts4435ff22018-12-05 10:42:27 +10003459 /* A scroll event may occur before the multiplier has been retrieved or
3460 * the input device set, or high-res scroll enabling may fail. In such
3461 * cases we must return early (falling back to default behaviour) to
3462 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3463 */
3464 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
Hans de Goede06104302019-04-20 13:22:13 +02003465 || hidpp->input == NULL || counter->wheel_multiplier == 0)
Harry Cutts4435ff22018-12-05 10:42:27 +10003466 return 0;
3467
Hans de Goede06104302019-04-20 13:22:13 +02003468 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
Harry Cutts4435ff22018-12-05 10:42:27 +10003469 return 1;
3470}
3471
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003472static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3473{
3474 static atomic_t battery_no = ATOMIC_INIT(0);
3475 struct power_supply_config cfg = { .drv_data = hidpp };
3476 struct power_supply_desc *desc = &hidpp->battery.desc;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003477 enum power_supply_property *battery_props;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003478 struct hidpp_battery *battery;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003479 unsigned int num_battery_props;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003480 unsigned long n;
3481 int ret;
3482
3483 if (hidpp->battery.ps)
3484 return 0;
3485
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003486 hidpp->battery.feature_index = 0xff;
3487 hidpp->battery.solar_feature_index = 0xff;
Pedro Vanzellabe281362019-10-26 19:25:06 -03003488 hidpp->battery.voltage_feature_index = 0xff;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003489
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003490 if (hidpp->protocol_major >= 2) {
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003491 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3492 ret = hidpp_solar_request_battery_event(hidpp);
Pedro Vanzellabe281362019-10-26 19:25:06 -03003493 else {
3494 ret = hidpp20_query_battery_voltage_info(hidpp);
3495 if (ret)
3496 ret = hidpp20_query_battery_info(hidpp);
3497 }
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02003498
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003499 if (ret)
3500 return ret;
3501 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3502 } else {
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003503 ret = hidpp10_query_battery_status(hidpp);
3504 if (ret) {
3505 ret = hidpp10_query_battery_mileage(hidpp);
3506 if (ret)
3507 return -ENOENT;
3508 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3509 } else {
3510 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3511 }
3512 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003513 }
3514
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003515 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3516 hidpp_battery_props,
3517 sizeof(hidpp_battery_props),
3518 GFP_KERNEL);
Gustavo A. R. Silva929b60a2017-07-07 00:12:13 -05003519 if (!battery_props)
3520 return -ENOMEM;
3521
Pedro Vanzellabe281362019-10-26 19:25:06 -03003522 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003523
3524 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3525 battery_props[num_battery_props++] =
3526 POWER_SUPPLY_PROP_CAPACITY;
3527
3528 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3529 battery_props[num_battery_props++] =
3530 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3531
Pedro Vanzellabe281362019-10-26 19:25:06 -03003532 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3533 battery_props[num_battery_props++] =
3534 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3535
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003536 battery = &hidpp->battery;
3537
3538 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003539 desc->properties = battery_props;
3540 desc->num_properties = num_battery_props;
Benjamin Tissoiresa52ec1072017-03-27 16:59:31 +02003541 desc->get_property = hidpp_battery_get_property;
3542 sprintf(battery->name, "hidpp_battery_%ld", n);
3543 desc->name = battery->name;
3544 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3545 desc->use_for_apm = 0;
3546
3547 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3548 &battery->desc,
3549 &cfg);
3550 if (IS_ERR(battery->ps))
3551 return PTR_ERR(battery->ps);
3552
3553 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3554
3555 return ret;
3556}
3557
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003558static void hidpp_overwrite_name(struct hid_device *hdev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003559{
3560 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3561 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003562
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003563 if (hidpp->protocol_major < 2)
Benjamin Tissoiresb4f8ce02017-03-27 16:59:24 +02003564 return;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003565
3566 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003567
Simon Wood7bfd2922015-11-19 16:42:12 -07003568 if (!name) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003569 hid_err(hdev, "unable to retrieve the name of the device");
Simon Wood7bfd2922015-11-19 16:42:12 -07003570 } else {
3571 dbg_hid("HID++: Got name: %s\n", name);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003572 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
Simon Wood7bfd2922015-11-19 16:42:12 -07003573 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003574
3575 kfree(name);
3576}
3577
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003578static int hidpp_input_open(struct input_dev *dev)
3579{
3580 struct hid_device *hid = input_get_drvdata(dev);
3581
3582 return hid_hw_open(hid);
3583}
3584
3585static void hidpp_input_close(struct input_dev *dev)
3586{
3587 struct hid_device *hid = input_get_drvdata(dev);
3588
3589 hid_hw_close(hid);
3590}
3591
3592static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3593{
3594 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003595 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003596
3597 if (!input_dev)
3598 return NULL;
3599
3600 input_set_drvdata(input_dev, hdev);
3601 input_dev->open = hidpp_input_open;
3602 input_dev->close = hidpp_input_close;
3603
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003604 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003605 input_dev->phys = hdev->phys;
3606 input_dev->uniq = hdev->uniq;
3607 input_dev->id.bustype = hdev->bus;
3608 input_dev->id.vendor = hdev->vendor;
3609 input_dev->id.product = hdev->product;
3610 input_dev->id.version = hdev->version;
3611 input_dev->dev.parent = &hdev->dev;
3612
3613 return input_dev;
3614}
3615
3616static void hidpp_connect_event(struct hidpp_device *hidpp)
3617{
3618 struct hid_device *hdev = hidpp->hid_dev;
3619 int ret = 0;
3620 bool connected = atomic_read(&hidpp->connected);
3621 struct input_dev *input;
3622 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003623
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003624 if (!connected) {
3625 if (hidpp->battery.ps) {
3626 hidpp->battery.online = false;
3627 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02003628 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003629 power_supply_changed(hidpp->battery.ps);
3630 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003631 return;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02003632 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02003633
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003634 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3635 ret = wtp_connect(hdev, connected);
3636 if (ret)
3637 return;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02003638 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3639 ret = m560_send_config_command(hdev, connected);
3640 if (ret)
3641 return;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003642 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3643 ret = k400_connect(hdev, connected);
3644 if (ret)
3645 return;
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05003646 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04003647
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003648 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3649 ret = hidpp10_wheel_connect(hidpp);
3650 if (ret)
3651 return;
3652 }
3653
Hans de Goede7457bc12019-04-20 13:22:16 +02003654 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3655 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3656 if (ret)
3657 return;
3658 }
3659
Hans de Goede42bc4f32019-04-20 13:22:17 +02003660 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3661 ret = hidpp10_consumer_keys_connect(hidpp);
3662 if (ret)
3663 return;
3664 }
3665
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003666 /* the device is already connected, we can ask for its name and
3667 * protocol */
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003668 if (!hidpp->protocol_major) {
Hans de Goede090760d2019-03-22 08:41:39 +01003669 ret = hidpp_root_get_protocol_version(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003670 if (ret) {
3671 hid_err(hdev, "Can not get the protocol version.\n");
3672 return;
3673 }
3674 }
3675
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003676 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003677 name = hidpp_get_device_name(hidpp);
Hans de Goede2ddf07f2019-04-20 13:22:07 +02003678 if (name) {
3679 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3680 "%s", name);
3681 kfree(name);
3682 if (!devm_name)
3683 return;
3684
3685 hidpp->name = devm_name;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003686 }
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05003687 }
3688
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003689 hidpp_initialize_battery(hidpp);
3690
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003691 /* forward current battery state */
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003692 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3693 hidpp10_enable_battery_reporting(hidpp);
3694 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3695 hidpp10_query_battery_mileage(hidpp);
3696 else
3697 hidpp10_query_battery_status(hidpp);
3698 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
Pedro Vanzellabe281362019-10-26 19:25:06 -03003699 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3700 hidpp20_query_battery_voltage_info(hidpp);
3701 else
3702 hidpp20_query_battery_info(hidpp);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003703 }
Benjamin Tissoires7f7ce2a2017-03-27 16:59:38 +02003704 if (hidpp->battery.ps)
3705 power_supply_changed(hidpp->battery.ps);
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02003706
Harry Cutts4435ff22018-12-05 10:42:27 +10003707 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3708 hi_res_scroll_enable(hidpp);
3709
Benjamin Tissoires29368362017-03-27 16:59:28 +02003710 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3711 /* if the input nodes are already created, we can stop now */
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02003712 return;
3713
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003714 input = hidpp_allocate_input(hdev);
3715 if (!input) {
3716 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3717 return;
3718 }
3719
Hans de Goedee54abaf2019-04-20 13:22:09 +02003720 hidpp_populate_input(hidpp, input);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003721
3722 ret = input_register_device(input);
3723 if (ret)
3724 input_free_device(input);
3725
3726 hidpp->delayed_input = input;
3727}
3728
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003729static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3730
3731static struct attribute *sysfs_attrs[] = {
3732 &dev_attr_builtin_power_supply.attr,
3733 NULL
3734};
3735
Arvind Yadav35a33cb2017-08-03 16:59:04 +05303736static const struct attribute_group ps_attribute_group = {
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003737 .attrs = sysfs_attrs
3738};
3739
Hans de Goeded71b18f2019-04-20 13:22:12 +02003740static int hidpp_get_report_length(struct hid_device *hdev, int id)
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003741{
3742 struct hid_report_enum *re;
3743 struct hid_report *report;
3744
Hans de Goeded71b18f2019-04-20 13:22:12 +02003745 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3746 report = re->report_id_hash[id];
3747 if (!report)
3748 return 0;
3749
3750 return report->field[0]->report_count + 1;
3751}
3752
Mazin Rezkc2a93272019-10-27 17:44:06 +00003753static u8 hidpp_validate_device(struct hid_device *hdev)
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003754{
Andrey Smirnov905d7542019-10-17 21:45:16 -07003755 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Mazin Rezkc2a93272019-10-27 17:44:06 +00003756 int id, report_length;
3757 u8 supported_reports = 0;
Andrey Smirnov905d7542019-10-17 21:45:16 -07003758
3759 id = REPORT_ID_HIDPP_SHORT;
3760 report_length = hidpp_get_report_length(hdev, id);
3761 if (report_length) {
3762 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
3763 goto bad_device;
3764
Mazin Rezkc2a93272019-10-27 17:44:06 +00003765 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
Andrey Smirnov905d7542019-10-17 21:45:16 -07003766 }
3767
3768 id = REPORT_ID_HIDPP_LONG;
3769 report_length = hidpp_get_report_length(hdev, id);
3770 if (report_length) {
3771 if (report_length < HIDPP_REPORT_LONG_LENGTH)
3772 goto bad_device;
3773
Mazin Rezkc2a93272019-10-27 17:44:06 +00003774 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
Andrey Smirnov905d7542019-10-17 21:45:16 -07003775 }
3776
3777 id = REPORT_ID_HIDPP_VERY_LONG;
3778 report_length = hidpp_get_report_length(hdev, id);
3779 if (report_length) {
3780 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
3781 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3782 goto bad_device;
3783
Mazin Rezkc2a93272019-10-27 17:44:06 +00003784 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
Andrey Smirnov905d7542019-10-17 21:45:16 -07003785 hidpp->very_long_report_length = report_length;
3786 }
3787
3788 return supported_reports;
3789
3790bad_device:
3791 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3792 return false;
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003793}
3794
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003795static bool hidpp_application_equals(struct hid_device *hdev,
3796 unsigned int application)
3797{
3798 struct list_head *report_list;
3799 struct hid_report *report;
3800
3801 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
3802 report = list_first_entry_or_null(report_list, struct hid_report, list);
3803 return report && report->application == application;
3804}
3805
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003806static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3807{
3808 struct hidpp_device *hidpp;
3809 int ret;
3810 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003811 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07003812 struct hidpp_ff_private_data data;
Bastien Nocera7df5da82023-01-25 13:17:22 +01003813 bool will_restart = false;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003814
Hans de Goede42bc4f32019-04-20 13:22:17 +02003815 /* report_fixup needs drvdata to be set before we call hid_parse */
3816 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
3817 if (!hidpp)
3818 return -ENOMEM;
3819
3820 hidpp->hid_dev = hdev;
3821 hidpp->name = hdev->name;
3822 hidpp->quirks = id->driver_data;
3823 hid_set_drvdata(hdev, hidpp);
3824
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003825 ret = hid_parse(hdev);
3826 if (ret) {
3827 hid_err(hdev, "%s:parse failed\n", __func__);
3828 return ret;
3829 }
3830
3831 /*
3832 * Make sure the device is HID++ capable, otherwise treat as generic HID
3833 */
Mazin Rezkc2a93272019-10-27 17:44:06 +00003834 hidpp->supported_reports = hidpp_validate_device(hdev);
3835
3836 if (!hidpp->supported_reports) {
Hans de Goede42bc4f32019-04-20 13:22:17 +02003837 hid_set_drvdata(hdev, NULL);
3838 devm_kfree(&hdev->dev, hidpp);
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003839 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Hans de Goede42bc4f32019-04-20 13:22:17 +02003840 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003841
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003842 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3843 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3844
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003845 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3846 hidpp_application_equals(hdev, HID_GD_MOUSE))
Hans de Goede7457bc12019-04-20 13:22:16 +02003847 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
3848 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
Hans de Goede4a79bcc2019-04-20 13:22:15 +02003849
Hans de Goede42bc4f32019-04-20 13:22:17 +02003850 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3851 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
3852 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
3853
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003854 if (disable_raw_mode) {
3855 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04003856 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04003857 }
3858
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003859 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3860 ret = wtp_allocate(hdev, id);
3861 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003862 return ret;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04003863 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3864 ret = k400_allocate(hdev);
3865 if (ret)
Benjamin Tissoires43cd97a2019-04-20 13:21:42 +02003866 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003867 }
3868
Bastien Nocera7df5da82023-01-25 13:17:22 +01003869 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT ||
3870 hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3871 will_restart = true;
3872
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003873 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003874 mutex_init(&hidpp->send_mutex);
3875 init_waitqueue_head(&hidpp->wait);
3876
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003877 /* indicates we are handling the battery properties in the kernel */
3878 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3879 if (ret)
3880 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3881 hdev->name);
3882
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003883 /*
3884 * Plain USB connections need to actually call start and open
3885 * on the transport driver to allow incoming data.
3886 */
Bastien Nocera7df5da82023-01-25 13:17:22 +01003887 ret = hid_hw_start(hdev, will_restart ? 0 : connect_mask);
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003888 if (ret) {
3889 hid_err(hdev, "hw start failed\n");
3890 goto hid_hw_start_fail;
Simon Wood7bfd2922015-11-19 16:42:12 -07003891 }
3892
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003893 ret = hid_hw_open(hdev);
3894 if (ret < 0) {
3895 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3896 __func__, ret);
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003897 goto hid_hw_open_fail;
3898 }
Simon Wood7bfd2922015-11-19 16:42:12 -07003899
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003900 /* Allow incoming packets */
3901 hid_device_io_start(hdev);
3902
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003903 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3904 hidpp_unifying_init(hidpp);
Bastien Nocera958534d2023-03-02 14:01:16 +01003905 else if (hid_is_usb(hidpp->hid_dev))
3906 hidpp_serial_init(hidpp);
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003907
Hans de Goede090760d2019-03-22 08:41:39 +01003908 connected = hidpp_root_get_protocol_version(hidpp) == 0;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003909 atomic_set(&hidpp->connected, connected);
3910 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003911 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02003912 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003913 hid_err(hdev, "Device not connected");
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003914 goto hid_hw_init_fail;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04003915 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003916
Benjamin Tissoires843c6242017-03-27 16:59:26 +02003917 hidpp_overwrite_name(hdev);
3918 }
Benjamin Tissoires33797822014-09-30 13:18:30 -04003919
Mazin Rezk0da0a63b2019-10-27 17:44:13 +00003920 if (connected && hidpp->protocol_major >= 2) {
3921 ret = hidpp_set_wireless_feature_index(hidpp);
3922 if (ret == -ENOENT)
3923 hidpp->wireless_feature_index = 0;
3924 else if (ret)
3925 goto hid_hw_init_fail;
Bastien Nocera7df5da82023-01-25 13:17:22 +01003926 ret = 0;
Mazin Rezk0da0a63b2019-10-27 17:44:13 +00003927 }
3928
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003929 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003930 ret = wtp_get_config(hidpp);
3931 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003932 goto hid_hw_init_fail;
Simon Wood7f4b49f2015-11-19 16:42:13 -07003933 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07003934 ret = g920_get_config(hidpp, &data);
Simon Wood7f4b49f2015-11-19 16:42:13 -07003935 if (ret)
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003936 goto hid_hw_init_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003937 }
3938
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10003939 hidpp_connect_event(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003940
Bastien Nocera7df5da82023-01-25 13:17:22 +01003941 if (will_restart) {
3942 /* Reset the HID node state */
3943 hid_device_io_stop(hdev);
3944 hid_hw_close(hdev);
3945 hid_hw_stop(hdev);
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003946
Bastien Nocera7df5da82023-01-25 13:17:22 +01003947 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3948 connect_mask &= ~HID_CONNECT_HIDINPUT;
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003949
Bastien Nocera7df5da82023-01-25 13:17:22 +01003950 /* Now export the actual inputs and hidraw nodes to the world */
3951 ret = hid_hw_start(hdev, connect_mask);
3952 if (ret) {
3953 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3954 goto hid_hw_start_fail;
3955 }
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003956 }
3957
Andrey Smirnovabdd3d02019-10-17 21:45:15 -07003958 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3959 ret = hidpp_ff_init(hidpp, &data);
3960 if (ret)
3961 hid_warn(hidpp->hid_dev,
3962 "Unable to initialize force feedback support, errno %d\n",
3963 ret);
3964 }
3965
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003966 return ret;
3967
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02003968hid_hw_init_fail:
3969 hid_hw_close(hdev);
3970hid_hw_open_fail:
3971 hid_hw_stop(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003972hid_hw_start_fail:
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003973 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003974 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003975 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003976 return ret;
3977}
3978
3979static void hidpp_remove(struct hid_device *hdev)
3980{
3981 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3982
Benjamin Tissoiresfe3ee1e2019-04-20 13:22:04 +02003983 if (!hidpp)
3984 return hid_hw_stop(hdev);
3985
Benjamin Tissoiresa4bf61532017-03-27 16:59:39 +02003986 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3987
Simon Wood7bfd2922015-11-19 16:42:12 -07003988 hid_hw_stop(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04003989 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003990 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04003991}
3992
Harry Cutts4435ff22018-12-05 10:42:27 +10003993#define LDJ_DEVICE(product) \
3994 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3995 USB_VENDOR_ID_LOGITECH, (product))
3996
Hans de Goede754a30882019-04-20 13:22:11 +02003997#define L27MHZ_DEVICE(product) \
3998 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
3999 USB_VENDOR_ID_LOGITECH, (product))
4000
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004001static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04004002 { /* wireless touchpad */
Harry Cutts16767222018-12-05 10:42:28 +10004003 LDJ_DEVICE(0x4011),
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04004004 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4005 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04004006 { /* wireless touchpad T650 */
Harry Cutts16767222018-12-05 10:42:28 +10004007 LDJ_DEVICE(0x4101),
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04004008 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004009 { /* wireless touchpad T651 */
4010 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4011 USB_DEVICE_ID_LOGITECH_T651),
4012 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Harry Cutts4435ff22018-12-05 10:42:27 +10004013 { /* Mouse Logitech Anywhere MX */
4014 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4015 { /* Mouse Logitech Cube */
4016 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4017 { /* Mouse Logitech M335 */
4018 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4019 { /* Mouse Logitech M515 */
4020 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02004021 { /* Mouse logitech M560 */
Harry Cutts4435ff22018-12-05 10:42:27 +10004022 LDJ_DEVICE(0x402d),
4023 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
4024 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4025 { /* Mouse Logitech M705 (firmware RQM17) */
4026 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4027 { /* Mouse Logitech M705 (firmware RQM67) */
4028 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4029 { /* Mouse Logitech M720 */
4030 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4031 { /* Mouse Logitech MX Anywhere 2 */
4032 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
Harry Cuttsb59f38d2020-10-21 06:56:12 -07004033 { LDJ_DEVICE(0x4072), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
Harry Cutts4435ff22018-12-05 10:42:27 +10004034 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4035 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4036 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4037 { /* Mouse Logitech MX Anywhere 2S */
4038 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4039 { /* Mouse Logitech MX Master */
4040 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4041 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4042 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4043 { /* Mouse Logitech MX Master 2S */
4044 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
Adrian Freund04bd68172019-10-25 22:59:29 +02004045 { /* Mouse Logitech MX Master 3 */
4046 LDJ_DEVICE(0x4082), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
Harry Cutts4435ff22018-12-05 10:42:27 +10004047 { /* Mouse Logitech Performance MX */
4048 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04004049 { /* Keyboard logitech K400 */
Harry Cutts16767222018-12-05 10:42:28 +10004050 LDJ_DEVICE(0x4024),
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10004051 .driver_data = HIDPP_QUIRK_CLASS_K400 },
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02004052 { /* Solar Keyboard Logitech K750 */
Harry Cutts16767222018-12-05 10:42:28 +10004053 LDJ_DEVICE(0x4002),
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02004054 .driver_data = HIDPP_QUIRK_CLASS_K750 },
Hans de Goede42bc4f32019-04-20 13:22:17 +02004055 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
4056 LDJ_DEVICE(0xb305),
4057 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Hans de Goedec27168a2020-11-02 14:36:57 +01004058 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */
4059 LDJ_DEVICE(0xb309),
4060 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Hans de Goedeced2c0c2019-04-28 21:25:52 +02004061 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */
4062 LDJ_DEVICE(0xb30b),
4063 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04004064
Harry Cutts16767222018-12-05 10:42:28 +10004065 { LDJ_DEVICE(HID_ANY_ID) },
Simon Wood7bfd2922015-11-19 16:42:12 -07004066
Hans de Goede4a79bcc2019-04-20 13:22:15 +02004067 { /* Keyboard LX501 (Y-RR53) */
4068 L27MHZ_DEVICE(0x0049),
4069 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4070 { /* Keyboard MX3000 (Y-RAM74) */
4071 L27MHZ_DEVICE(0x0057),
4072 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4073 { /* Keyboard MX3200 (Y-RAV80) */
4074 L27MHZ_DEVICE(0x005c),
4075 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
Hans de Goede24098772019-05-12 12:36:02 +02004076 { /* S510 Media Remote */
4077 L27MHZ_DEVICE(0x00fe),
4078 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
Hans de Goede4a79bcc2019-04-20 13:22:15 +02004079
Hans de Goede754a30882019-04-20 13:22:11 +02004080 { L27MHZ_DEVICE(HID_ANY_ID) },
4081
Filipe Laíns27fc32f2019-07-16 08:37:26 +01004082 { /* Logitech G403 Wireless Gaming Mouse over USB */
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02004083 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
Filipe Laíns27fc32f2019-07-16 08:37:26 +01004084 { /* Logitech G703 Gaming Mouse over USB */
4085 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4086 { /* Logitech G703 Hero Gaming Mouse over USB */
4087 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02004088 { /* Logitech G900 Gaming Mouse over USB */
4089 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
Filipe Laíns27fc32f2019-07-16 08:37:26 +01004090 { /* Logitech G903 Gaming Mouse over USB */
4091 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4092 { /* Logitech G903 Hero Gaming Mouse over USB */
4093 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
Benjamin Tissoires91cf9a92019-04-20 13:22:05 +02004094 { /* Logitech G920 Wheel over USB */
4095 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
Simon Wood7bfd2922015-11-19 16:42:12 -07004096 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
Filipe Laíns27fc32f2019-07-16 08:37:26 +01004097 { /* Logitech G Pro Gaming Mouse over USB */
4098 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
Hans de Goede42bc4f32019-04-20 13:22:17 +02004099
4100 { /* MX5000 keyboard over Bluetooth */
4101 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4102 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Hans de Goedec27168a2020-11-02 14:36:57 +01004103 { /* Dinovo Edge keyboard over Bluetooth */
4104 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4105 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Hans de Goedeced2c0c2019-04-28 21:25:52 +02004106 { /* MX5500 keyboard over Bluetooth */
4107 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4108 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
Mazin Rezkc2a93272019-10-27 17:44:06 +00004109 { /* MX Master mouse over Bluetooth */
4110 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012),
4111 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
Nicholas Miell0a3be222021-01-10 22:09:25 -08004112 { /* MX Ergo trackball over Bluetooth */
4113 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
Mazin Rezkc2a93272019-10-27 17:44:06 +00004114 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e),
4115 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
Adrian Freund04bd68172019-10-25 22:59:29 +02004116 { /* MX Master 3 mouse over Bluetooth */
4117 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023),
4118 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004119 {}
4120};
4121
4122MODULE_DEVICE_TABLE(hid, hidpp_devices);
4123
Harry Cutts4435ff22018-12-05 10:42:27 +10004124static const struct hid_usage_id hidpp_usages[] = {
4125 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4126 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4127};
4128
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004129static struct hid_driver hidpp_driver = {
4130 .name = "logitech-hidpp-device",
4131 .id_table = hidpp_devices,
Hans de Goede42bc4f32019-04-20 13:22:17 +02004132 .report_fixup = hidpp_report_fixup,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004133 .probe = hidpp_probe,
4134 .remove = hidpp_remove,
4135 .raw_event = hidpp_raw_event,
Harry Cutts4435ff22018-12-05 10:42:27 +10004136 .usage_table = hidpp_usages,
4137 .event = hidpp_event,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004138 .input_configured = hidpp_input_configured,
4139 .input_mapping = hidpp_input_mapping,
Simon Wood0b1804e2015-11-19 16:42:15 -07004140 .input_mapped = hidpp_input_mapped,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04004141};
4142
4143module_hid_driver(hidpp_driver);