blob: 4aaf2378897f21759615898549e9e04d8eb287da [file] [log] [blame]
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001/*
2 * HIDPP protocol for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/device.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010018#include <linux/input.h>
19#include <linux/usb.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040020#include <linux/hid.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/sched.h>
24#include <linux/kfifo.h>
25#include <linux/input/mt.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010026#include <linux/workqueue.h>
27#include <linux/atomic.h>
28#include <linux/fixp-arith.h>
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040029#include <asm/unaligned.h>
Edwin Veldsff21a632016-01-11 00:25:15 +010030#include "usbhid/usbhid.h"
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040031#include "hid-ids.h"
32
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
35MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
36
Benjamin Tissoires9188dba2015-03-26 12:41:57 -040037static bool disable_raw_mode;
38module_param(disable_raw_mode, bool, 0644);
39MODULE_PARM_DESC(disable_raw_mode,
40 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
41
Benjamin Tissoires90cdd982015-09-03 09:08:30 -040042static bool disable_tap_to_click;
43module_param(disable_tap_to_click, bool, 0644);
44MODULE_PARM_DESC(disable_tap_to_click,
45 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
46
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040047#define REPORT_ID_HIDPP_SHORT 0x10
48#define REPORT_ID_HIDPP_LONG 0x11
Simon Wooda5ce8f52015-11-19 16:42:11 -070049#define REPORT_ID_HIDPP_VERY_LONG 0x12
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040050
51#define HIDPP_REPORT_SHORT_LENGTH 7
52#define HIDPP_REPORT_LONG_LENGTH 20
Simon Wooda5ce8f52015-11-19 16:42:11 -070053#define HIDPP_REPORT_VERY_LONG_LENGTH 64
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040054
55#define HIDPP_QUIRK_CLASS_WTP BIT(0)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +020056#define HIDPP_QUIRK_CLASS_M560 BIT(1)
Benjamin Tissoires90cdd982015-09-03 09:08:30 -040057#define HIDPP_QUIRK_CLASS_K400 BIT(2)
Simon Wood7bfd2922015-11-19 16:42:12 -070058#define HIDPP_QUIRK_CLASS_G920 BIT(3)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040059
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +020060/* bits 2..20 are reserved for classes */
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100061/* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -040062#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040063#define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
Simon Wood7bfd2922015-11-19 16:42:12 -070064#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
Peter Hutterer5a2b1902016-06-29 19:28:01 +100065#define HIDPP_QUIRK_HIDPP20_BATTERY BIT(25)
66#define HIDPP_QUIRK_HIDPP10_BATTERY BIT(26)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040067
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100068#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040069
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040070/*
71 * There are two hidpp protocols in use, the first version hidpp10 is known
72 * as register access protocol or RAP, the second version hidpp20 is known as
73 * feature access protocol or FAP
74 *
75 * Most older devices (including the Unifying usb receiver) use the RAP protocol
76 * where as most newer devices use the FAP protocol. Both protocols are
77 * compatible with the underlying transport, which could be usb, Unifiying, or
78 * bluetooth. The message lengths are defined by the hid vendor specific report
79 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
80 * the HIDPP_LONG report type (total message length 20 bytes)
81 *
82 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
83 * messages. The Unifying receiver itself responds to RAP messages (device index
84 * is 0xFF for the receiver), and all messages (short or long) with a device
85 * index between 1 and 6 are passed untouched to the corresponding paired
86 * Unifying device.
87 *
88 * The paired device can be RAP or FAP, it will receive the message untouched
89 * from the Unifiying receiver.
90 */
91
92struct fap {
93 u8 feature_index;
94 u8 funcindex_clientid;
Simon Wooda5ce8f52015-11-19 16:42:11 -070095 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040096};
97
98struct rap {
99 u8 sub_id;
100 u8 reg_address;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700101 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400102};
103
104struct hidpp_report {
105 u8 report_id;
106 u8 device_index;
107 union {
108 struct fap fap;
109 struct rap rap;
110 u8 rawbytes[sizeof(struct fap)];
111 };
112} __packed;
113
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000114struct hidpp_battery {
115 u8 feature_index;
116 struct power_supply_desc desc;
117 struct power_supply *ps;
118 char name[64];
119 int status;
120 int level;
121};
122
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400123struct hidpp_device {
124 struct hid_device *hid_dev;
125 struct mutex send_mutex;
126 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -0500127 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400128 wait_queue_head_t wait;
129 bool answer_available;
130 u8 protocol_major;
131 u8 protocol_minor;
132
133 void *private_data;
134
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400135 struct work_struct work;
136 struct kfifo delayed_work_fifo;
137 atomic_t connected;
138 struct input_dev *delayed_input;
139
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400140 unsigned long quirks;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400141
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000142 struct hidpp_battery battery;
143};
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400144
Peter Wuf677bb12014-12-16 01:50:14 +0100145/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400146#define HIDPP_ERROR 0x8f
147#define HIDPP_ERROR_SUCCESS 0x00
148#define HIDPP_ERROR_INVALID_SUBID 0x01
149#define HIDPP_ERROR_INVALID_ADRESS 0x02
150#define HIDPP_ERROR_INVALID_VALUE 0x03
151#define HIDPP_ERROR_CONNECT_FAIL 0x04
152#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
153#define HIDPP_ERROR_ALREADY_EXISTS 0x06
154#define HIDPP_ERROR_BUSY 0x07
155#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
156#define HIDPP_ERROR_RESOURCE_ERROR 0x09
157#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
158#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
159#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100160/* HID++ 2.0 error codes */
161#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400162
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400163static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
164
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400165static int __hidpp_send_report(struct hid_device *hdev,
166 struct hidpp_report *hidpp_report)
167{
Simon Wood7bfd2922015-11-19 16:42:12 -0700168 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400169 int fields_count, ret;
170
Simon Wood7bfd2922015-11-19 16:42:12 -0700171 hidpp = hid_get_drvdata(hdev);
172
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400173 switch (hidpp_report->report_id) {
174 case REPORT_ID_HIDPP_SHORT:
175 fields_count = HIDPP_REPORT_SHORT_LENGTH;
176 break;
177 case REPORT_ID_HIDPP_LONG:
178 fields_count = HIDPP_REPORT_LONG_LENGTH;
179 break;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700180 case REPORT_ID_HIDPP_VERY_LONG:
181 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
182 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400183 default:
184 return -ENODEV;
185 }
186
187 /*
188 * set the device_index as the receiver, it will be overwritten by
189 * hid_hw_request if needed
190 */
191 hidpp_report->device_index = 0xff;
192
Simon Wood7bfd2922015-11-19 16:42:12 -0700193 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
194 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
195 } else {
196 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
197 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
198 HID_REQ_SET_REPORT);
199 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400200
201 return ret == fields_count ? 0 : -1;
202}
203
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500204/**
205 * hidpp_send_message_sync() returns 0 in case of success, and something else
206 * in case of a failure.
207 * - If ' something else' is positive, that means that an error has been raised
208 * by the protocol itself.
209 * - If ' something else' is negative, that means that we had a classic error
210 * (-ENOMEM, -EPIPE, etc...)
211 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400212static int hidpp_send_message_sync(struct hidpp_device *hidpp,
213 struct hidpp_report *message,
214 struct hidpp_report *response)
215{
216 int ret;
217
218 mutex_lock(&hidpp->send_mutex);
219
220 hidpp->send_receive_buf = response;
221 hidpp->answer_available = false;
222
223 /*
224 * So that we can later validate the answer when it arrives
225 * in hidpp_raw_event
226 */
227 *response = *message;
228
229 ret = __hidpp_send_report(hidpp->hid_dev, message);
230
231 if (ret) {
232 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
233 memset(response, 0, sizeof(struct hidpp_report));
234 goto exit;
235 }
236
237 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
238 5*HZ)) {
239 dbg_hid("%s:timeout waiting for response\n", __func__);
240 memset(response, 0, sizeof(struct hidpp_report));
241 ret = -ETIMEDOUT;
242 }
243
244 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100245 response->rap.sub_id == HIDPP_ERROR) {
246 ret = response->rap.params[1];
247 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
248 goto exit;
249 }
250
Simon Wooda5ce8f52015-11-19 16:42:11 -0700251 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
252 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
253 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400254 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100255 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400256 goto exit;
257 }
258
259exit:
260 mutex_unlock(&hidpp->send_mutex);
261 return ret;
262
263}
264
265static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
266 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
267 struct hidpp_report *response)
268{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300269 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400270 int ret;
271
272 if (param_count > sizeof(message->fap.params))
273 return -EINVAL;
274
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300275 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
276 if (!message)
277 return -ENOMEM;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700278
279 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
280 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
281 else
282 message->report_id = REPORT_ID_HIDPP_LONG;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400283 message->fap.feature_index = feat_index;
284 message->fap.funcindex_clientid = funcindex_clientid;
285 memcpy(&message->fap.params, params, param_count);
286
287 ret = hidpp_send_message_sync(hidpp, message, response);
288 kfree(message);
289 return ret;
290}
291
Benjamin Tissoires33797822014-09-30 13:18:30 -0400292static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
293 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
294 struct hidpp_report *response)
295{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300296 struct hidpp_report *message;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700297 int ret, max_count;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400298
Simon Wooda5ce8f52015-11-19 16:42:11 -0700299 switch (report_id) {
300 case REPORT_ID_HIDPP_SHORT:
301 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
302 break;
303 case REPORT_ID_HIDPP_LONG:
304 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
305 break;
306 case REPORT_ID_HIDPP_VERY_LONG:
307 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
308 break;
309 default:
Benjamin Tissoires33797822014-09-30 13:18:30 -0400310 return -EINVAL;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700311 }
Benjamin Tissoires33797822014-09-30 13:18:30 -0400312
Simon Wooda5ce8f52015-11-19 16:42:11 -0700313 if (param_count > max_count)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400314 return -EINVAL;
315
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300316 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
317 if (!message)
318 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400319 message->report_id = report_id;
320 message->rap.sub_id = sub_id;
321 message->rap.reg_address = reg_address;
322 memcpy(&message->rap.params, params, param_count);
323
324 ret = hidpp_send_message_sync(hidpp_dev, message, response);
325 kfree(message);
326 return ret;
327}
328
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400329static void delayed_work_cb(struct work_struct *work)
330{
331 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
332 work);
333 hidpp_connect_event(hidpp);
334}
335
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400336static inline bool hidpp_match_answer(struct hidpp_report *question,
337 struct hidpp_report *answer)
338{
339 return (answer->fap.feature_index == question->fap.feature_index) &&
340 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
341}
342
343static inline bool hidpp_match_error(struct hidpp_report *question,
344 struct hidpp_report *answer)
345{
Peter Wuf677bb12014-12-16 01:50:14 +0100346 return ((answer->rap.sub_id == HIDPP_ERROR) ||
347 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400348 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
349 (answer->fap.params[0] == question->fap.funcindex_clientid);
350}
351
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400352static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
353{
354 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
355 (report->rap.sub_id == 0x41);
356}
357
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500358/**
359 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
360 */
361static void hidpp_prefix_name(char **name, int name_length)
362{
363#define PREFIX_LENGTH 9 /* "Logitech " */
364
365 int new_length;
366 char *new_name;
367
368 if (name_length > PREFIX_LENGTH &&
369 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
370 /* The prefix has is already in the name */
371 return;
372
373 new_length = PREFIX_LENGTH + name_length;
374 new_name = kzalloc(new_length, GFP_KERNEL);
375 if (!new_name)
376 return;
377
378 snprintf(new_name, new_length, "Logitech %s", *name);
379
380 kfree(*name);
381
382 *name = new_name;
383}
384
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400385/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400386/* HIDP++ 1.0 commands */
387/* -------------------------------------------------------------------------- */
388
389#define HIDPP_SET_REGISTER 0x80
390#define HIDPP_GET_REGISTER 0x81
391#define HIDPP_SET_LONG_REGISTER 0x82
392#define HIDPP_GET_LONG_REGISTER 0x83
393
394#define HIDPP_REG_PAIRING_INFORMATION 0xB5
395#define DEVICE_NAME 0x40
396
397static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
398{
399 struct hidpp_report response;
400 int ret;
401 /* hid-logitech-dj is in charge of setting the right device index */
402 u8 params[1] = { DEVICE_NAME };
403 char *name;
404 int len;
405
406 ret = hidpp_send_rap_command_sync(hidpp_dev,
407 REPORT_ID_HIDPP_SHORT,
408 HIDPP_GET_LONG_REGISTER,
409 HIDPP_REG_PAIRING_INFORMATION,
410 params, 1, &response);
411 if (ret)
412 return NULL;
413
414 len = response.rap.params[1];
415
Peter Wu3a034a72014-12-11 13:51:19 +0100416 if (2 + len > sizeof(response.rap.params))
417 return NULL;
418
Benjamin Tissoires33797822014-09-30 13:18:30 -0400419 name = kzalloc(len + 1, GFP_KERNEL);
420 if (!name)
421 return NULL;
422
423 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500424
425 /* include the terminating '\0' */
426 hidpp_prefix_name(&name, len + 1);
427
Benjamin Tissoires33797822014-09-30 13:18:30 -0400428 return name;
429}
430
431/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400432/* 0x0000: Root */
433/* -------------------------------------------------------------------------- */
434
435#define HIDPP_PAGE_ROOT 0x0000
436#define HIDPP_PAGE_ROOT_IDX 0x00
437
438#define CMD_ROOT_GET_FEATURE 0x01
439#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
440
441static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
442 u8 *feature_index, u8 *feature_type)
443{
444 struct hidpp_report response;
445 int ret;
446 u8 params[2] = { feature >> 8, feature & 0x00FF };
447
448 ret = hidpp_send_fap_command_sync(hidpp,
449 HIDPP_PAGE_ROOT_IDX,
450 CMD_ROOT_GET_FEATURE,
451 params, 2, &response);
452 if (ret)
453 return ret;
454
455 *feature_index = response.fap.params[0];
456 *feature_type = response.fap.params[1];
457
458 return ret;
459}
460
461static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
462{
463 struct hidpp_report response;
464 int ret;
465
466 ret = hidpp_send_fap_command_sync(hidpp,
467 HIDPP_PAGE_ROOT_IDX,
468 CMD_ROOT_GET_PROTOCOL_VERSION,
469 NULL, 0, &response);
470
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500471 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400472 hidpp->protocol_major = 1;
473 hidpp->protocol_minor = 0;
474 return 0;
475 }
476
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500477 /* the device might not be connected */
478 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
479 return -EIO;
480
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500481 if (ret > 0) {
482 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
483 __func__, ret);
484 return -EPROTO;
485 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400486 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500487 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400488
489 hidpp->protocol_major = response.fap.params[0];
490 hidpp->protocol_minor = response.fap.params[1];
491
492 return ret;
493}
494
495static bool hidpp_is_connected(struct hidpp_device *hidpp)
496{
497 int ret;
498
499 ret = hidpp_root_get_protocol_version(hidpp);
500 if (!ret)
501 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
502 hidpp->protocol_major, hidpp->protocol_minor);
503 return ret == 0;
504}
505
506/* -------------------------------------------------------------------------- */
507/* 0x0005: GetDeviceNameType */
508/* -------------------------------------------------------------------------- */
509
510#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
511
512#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
513#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
514#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
515
516static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
517 u8 feature_index, u8 *nameLength)
518{
519 struct hidpp_report response;
520 int ret;
521
522 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
523 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
524
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500525 if (ret > 0) {
526 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
527 __func__, ret);
528 return -EPROTO;
529 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400530 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500531 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400532
533 *nameLength = response.fap.params[0];
534
535 return ret;
536}
537
538static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
539 u8 feature_index, u8 char_index, char *device_name, int len_buf)
540{
541 struct hidpp_report response;
542 int ret, i;
543 int count;
544
545 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
546 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
547 &response);
548
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500549 if (ret > 0) {
550 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
551 __func__, ret);
552 return -EPROTO;
553 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400554 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500555 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400556
Simon Wooda5ce8f52015-11-19 16:42:11 -0700557 switch (response.report_id) {
558 case REPORT_ID_HIDPP_VERY_LONG:
559 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
560 break;
561 case REPORT_ID_HIDPP_LONG:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400562 count = HIDPP_REPORT_LONG_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700563 break;
564 case REPORT_ID_HIDPP_SHORT:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400565 count = HIDPP_REPORT_SHORT_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700566 break;
567 default:
568 return -EPROTO;
569 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400570
571 if (len_buf < count)
572 count = len_buf;
573
574 for (i = 0; i < count; i++)
575 device_name[i] = response.fap.params[i];
576
577 return count;
578}
579
Peter Wu02cc0972014-12-11 13:51:17 +0100580static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400581{
582 u8 feature_type;
583 u8 feature_index;
584 u8 __name_length;
585 char *name;
586 unsigned index = 0;
587 int ret;
588
589 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
590 &feature_index, &feature_type);
591 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100592 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400593
594 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
595 &__name_length);
596 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100597 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400598
599 name = kzalloc(__name_length + 1, GFP_KERNEL);
600 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100601 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400602
Peter Wu1430ee72014-12-11 13:51:18 +0100603 while (index < __name_length) {
604 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400605 feature_index, index, name + index,
606 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +0100607 if (ret <= 0) {
608 kfree(name);
609 return NULL;
610 }
611 index += ret;
612 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400613
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500614 /* include the terminating '\0' */
615 hidpp_prefix_name(&name, __name_length + 1);
616
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400617 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400618}
619
620/* -------------------------------------------------------------------------- */
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000621/* 0x1000: Battery level status */
622/* -------------------------------------------------------------------------- */
623
624#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
625
626#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
627#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
628
629#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
630
631static int hidpp20_batterylevel_map_status_level(u8 data[3], int *level,
632 int *next_level)
633{
634 int status;
635 int level_override;
636
637 *level = data[0];
638 *next_level = data[1];
639
640 /* When discharging, we can rely on the device reported level.
641 * For all other states the device reports level 0 (unknown). Make up
642 * a number instead
643 */
644 switch (data[2]) {
645 case 0: /* discharging (in use) */
646 status = POWER_SUPPLY_STATUS_DISCHARGING;
647 level_override = 0;
648 break;
649 case 1: /* recharging */
650 status = POWER_SUPPLY_STATUS_CHARGING;
651 level_override = 80;
652 break;
653 case 2: /* charge in final stage */
654 status = POWER_SUPPLY_STATUS_CHARGING;
655 level_override = 90;
656 break;
657 case 3: /* charge complete */
658 status = POWER_SUPPLY_STATUS_FULL;
659 level_override = 100;
660 break;
661 case 4: /* recharging below optimal speed */
662 status = POWER_SUPPLY_STATUS_CHARGING;
663 level_override = 50;
664 break;
665 /* 5 = invalid battery type
666 6 = thermal error
667 7 = other charging error */
668 default:
669 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
670 level_override = 0;
671 break;
672 }
673
674 if (level_override != 0 && *level == 0)
675 *level = level_override;
676
677 return status;
678}
679
680static int hidpp20_batterylevel_get_battery_level(struct hidpp_device *hidpp,
681 u8 feature_index,
682 int *status,
683 int *level,
684 int *next_level)
685{
686 struct hidpp_report response;
687 int ret;
688 u8 *params = (u8 *)response.fap.params;
689
690 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
691 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
692 NULL, 0, &response);
693 if (ret > 0) {
694 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
695 __func__, ret);
696 return -EPROTO;
697 }
698 if (ret)
699 return ret;
700
701 *status = hidpp20_batterylevel_map_status_level(params, level,
702 next_level);
703
704 return 0;
705}
706
707static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
708{
709 u8 feature_type;
710 int ret;
711 int status, level, next_level;
712
713 if (hidpp->battery.feature_index == 0) {
714 ret = hidpp_root_get_feature(hidpp,
715 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
716 &hidpp->battery.feature_index,
717 &feature_type);
718 if (ret)
719 return ret;
720 }
721
722 ret = hidpp20_batterylevel_get_battery_level(hidpp,
723 hidpp->battery.feature_index,
724 &status, &level, &next_level);
725 if (ret)
726 return ret;
727
728 hidpp->battery.status = status;
729 hidpp->battery.level = level;
730
731 return 0;
732}
733
734static int hidpp20_battery_event(struct hidpp_device *hidpp,
735 u8 *data, int size)
736{
737 struct hidpp_report *report = (struct hidpp_report *)data;
738 int status, level, next_level;
739 bool changed;
740
741 if (report->fap.feature_index != hidpp->battery.feature_index ||
742 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
743 return 0;
744
745 status = hidpp20_batterylevel_map_status_level(report->fap.params,
746 &level, &next_level);
747
748 changed = level != hidpp->battery.level ||
749 status != hidpp->battery.status;
750
751 if (changed) {
752 hidpp->battery.level = level;
753 hidpp->battery.status = status;
754 if (hidpp->battery.ps)
755 power_supply_changed(hidpp->battery.ps);
756 }
757
758 return 0;
759}
760
761static enum power_supply_property hidpp_battery_props[] = {
762 POWER_SUPPLY_PROP_STATUS,
763 POWER_SUPPLY_PROP_CAPACITY,
Bastien Nocera3861e6c2017-03-27 16:59:22 +0200764 POWER_SUPPLY_PROP_SCOPE,
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000765};
766
767static int hidpp_battery_get_property(struct power_supply *psy,
768 enum power_supply_property psp,
769 union power_supply_propval *val)
770{
771 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
772 int ret = 0;
773
774 switch(psp) {
775 case POWER_SUPPLY_PROP_STATUS:
776 val->intval = hidpp->battery.status;
777 break;
778 case POWER_SUPPLY_PROP_CAPACITY:
779 val->intval = hidpp->battery.level;
780 break;
Bastien Nocera3861e6c2017-03-27 16:59:22 +0200781 case POWER_SUPPLY_PROP_SCOPE:
782 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
783 break;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000784 default:
785 ret = -EINVAL;
786 break;
787 }
788
789 return ret;
790}
791
792static int hidpp20_initialize_battery(struct hidpp_device *hidpp)
793{
794 static atomic_t battery_no = ATOMIC_INIT(0);
795 struct power_supply_config cfg = { .drv_data = hidpp };
796 struct power_supply_desc *desc = &hidpp->battery.desc;
797 struct hidpp_battery *battery;
798 unsigned long n;
799 int ret;
800
801 ret = hidpp20_query_battery_info(hidpp);
802 if (ret)
803 return ret;
804
805 battery = &hidpp->battery;
806
807 n = atomic_inc_return(&battery_no) - 1;
808 desc->properties = hidpp_battery_props;
809 desc->num_properties = ARRAY_SIZE(hidpp_battery_props);
810 desc->get_property = hidpp_battery_get_property;
811 sprintf(battery->name, "hidpp_battery_%ld", n);
812 desc->name = battery->name;
813 desc->type = POWER_SUPPLY_TYPE_BATTERY;
814 desc->use_for_apm = 0;
815
816 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
817 &battery->desc,
818 &cfg);
819 if (IS_ERR(battery->ps))
820 return PTR_ERR(battery->ps);
821
822 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
823
824 return 0;
825}
826
827static int hidpp_initialize_battery(struct hidpp_device *hidpp)
828{
829 int ret;
830
831 if (hidpp->protocol_major >= 2) {
832 ret = hidpp20_initialize_battery(hidpp);
833 if (ret == 0)
834 hidpp->quirks |= HIDPP_QUIRK_HIDPP20_BATTERY;
835 }
836
837 return ret;
838}
839
840/* -------------------------------------------------------------------------- */
Benjamin Tissoires90cdd982015-09-03 09:08:30 -0400841/* 0x6010: Touchpad FW items */
842/* -------------------------------------------------------------------------- */
843
844#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
845
846#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
847
848struct hidpp_touchpad_fw_items {
849 uint8_t presence;
850 uint8_t desired_state;
851 uint8_t state;
852 uint8_t persistent;
853};
854
855/**
856 * send a set state command to the device by reading the current items->state
857 * field. items is then filled with the current state.
858 */
859static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
860 u8 feature_index,
861 struct hidpp_touchpad_fw_items *items)
862{
863 struct hidpp_report response;
864 int ret;
865 u8 *params = (u8 *)response.fap.params;
866
867 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
868 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
869
870 if (ret > 0) {
871 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
872 __func__, ret);
873 return -EPROTO;
874 }
875 if (ret)
876 return ret;
877
878 items->presence = params[0];
879 items->desired_state = params[1];
880 items->state = params[2];
881 items->persistent = params[3];
882
883 return 0;
884}
885
886/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400887/* 0x6100: TouchPadRawXY */
888/* -------------------------------------------------------------------------- */
889
890#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
891
892#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400893#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
894
895#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400896
897#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
898#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
899
900struct hidpp_touchpad_raw_info {
901 u16 x_size;
902 u16 y_size;
903 u8 z_range;
904 u8 area_range;
905 u8 timestamp_unit;
906 u8 maxcontacts;
907 u8 origin;
908 u16 res;
909};
910
911struct hidpp_touchpad_raw_xy_finger {
912 u8 contact_type;
913 u8 contact_status;
914 u16 x;
915 u16 y;
916 u8 z;
917 u8 area;
918 u8 finger_id;
919};
920
921struct hidpp_touchpad_raw_xy {
922 u16 timestamp;
923 struct hidpp_touchpad_raw_xy_finger fingers[2];
924 u8 spurious_flag;
925 u8 end_of_frame;
926 u8 finger_count;
927 u8 button;
928};
929
930static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
931 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
932{
933 struct hidpp_report response;
934 int ret;
935 u8 *params = (u8 *)response.fap.params;
936
937 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
938 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
939
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500940 if (ret > 0) {
941 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
942 __func__, ret);
943 return -EPROTO;
944 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400945 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500946 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400947
948 raw_info->x_size = get_unaligned_be16(&params[0]);
949 raw_info->y_size = get_unaligned_be16(&params[2]);
950 raw_info->z_range = params[4];
951 raw_info->area_range = params[5];
952 raw_info->maxcontacts = params[7];
953 raw_info->origin = params[8];
954 /* res is given in unit per inch */
955 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
956
957 return ret;
958}
959
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400960static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
961 u8 feature_index, bool send_raw_reports,
962 bool sensor_enhanced_settings)
963{
964 struct hidpp_report response;
965
966 /*
967 * Params:
968 * bit 0 - enable raw
969 * bit 1 - 16bit Z, no area
970 * bit 2 - enhanced sensitivity
971 * bit 3 - width, height (4 bits each) instead of area
972 * bit 4 - send raw + gestures (degrades smoothness)
973 * remaining bits - reserved
974 */
975 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
976
977 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
978 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
979}
980
981static void hidpp_touchpad_touch_event(u8 *data,
982 struct hidpp_touchpad_raw_xy_finger *finger)
983{
984 u8 x_m = data[0] << 2;
985 u8 y_m = data[2] << 2;
986
987 finger->x = x_m << 6 | data[1];
988 finger->y = y_m << 6 | data[3];
989
990 finger->contact_type = data[0] >> 6;
991 finger->contact_status = data[2] >> 6;
992
993 finger->z = data[4];
994 finger->area = data[5];
995 finger->finger_id = data[6] >> 4;
996}
997
998static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
999 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1000{
1001 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1002 raw_xy->end_of_frame = data[8] & 0x01;
1003 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1004 raw_xy->finger_count = data[15] & 0x0f;
1005 raw_xy->button = (data[8] >> 2) & 0x01;
1006
1007 if (raw_xy->finger_count) {
1008 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1009 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1010 }
1011}
1012
Edwin Veldsff21a632016-01-11 00:25:15 +01001013/* -------------------------------------------------------------------------- */
1014/* 0x8123: Force feedback support */
1015/* -------------------------------------------------------------------------- */
1016
1017#define HIDPP_FF_GET_INFO 0x01
1018#define HIDPP_FF_RESET_ALL 0x11
1019#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1020#define HIDPP_FF_SET_EFFECT_STATE 0x31
1021#define HIDPP_FF_DESTROY_EFFECT 0x41
1022#define HIDPP_FF_GET_APERTURE 0x51
1023#define HIDPP_FF_SET_APERTURE 0x61
1024#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1025#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1026
1027#define HIDPP_FF_EFFECT_STATE_GET 0x00
1028#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1029#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1030#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1031
1032#define HIDPP_FF_EFFECT_CONSTANT 0x00
1033#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1034#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1035#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1036#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1037#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1038#define HIDPP_FF_EFFECT_SPRING 0x06
1039#define HIDPP_FF_EFFECT_DAMPER 0x07
1040#define HIDPP_FF_EFFECT_FRICTION 0x08
1041#define HIDPP_FF_EFFECT_INERTIA 0x09
1042#define HIDPP_FF_EFFECT_RAMP 0x0A
1043
1044#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1045
1046#define HIDPP_FF_EFFECTID_NONE -1
1047#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1048
1049#define HIDPP_FF_MAX_PARAMS 20
1050#define HIDPP_FF_RESERVED_SLOTS 1
1051
1052struct hidpp_ff_private_data {
1053 struct hidpp_device *hidpp;
1054 u8 feature_index;
1055 u8 version;
1056 u16 gain;
1057 s16 range;
1058 u8 slot_autocenter;
1059 u8 num_effects;
1060 int *effect_ids;
1061 struct workqueue_struct *wq;
1062 atomic_t workqueue_size;
1063};
1064
1065struct hidpp_ff_work_data {
1066 struct work_struct work;
1067 struct hidpp_ff_private_data *data;
1068 int effect_id;
1069 u8 command;
1070 u8 params[HIDPP_FF_MAX_PARAMS];
1071 u8 size;
1072};
1073
1074static const signed short hiddpp_ff_effects[] = {
1075 FF_CONSTANT,
1076 FF_PERIODIC,
1077 FF_SINE,
1078 FF_SQUARE,
1079 FF_SAW_UP,
1080 FF_SAW_DOWN,
1081 FF_TRIANGLE,
1082 FF_SPRING,
1083 FF_DAMPER,
1084 FF_AUTOCENTER,
1085 FF_GAIN,
1086 -1
1087};
1088
1089static const signed short hiddpp_ff_effects_v2[] = {
1090 FF_RAMP,
1091 FF_FRICTION,
1092 FF_INERTIA,
1093 -1
1094};
1095
1096static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1097 HIDPP_FF_EFFECT_SPRING,
1098 HIDPP_FF_EFFECT_FRICTION,
1099 HIDPP_FF_EFFECT_DAMPER,
1100 HIDPP_FF_EFFECT_INERTIA
1101};
1102
1103static const char *HIDPP_FF_CONDITION_NAMES[] = {
1104 "spring",
1105 "friction",
1106 "damper",
1107 "inertia"
1108};
1109
1110
1111static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1112{
1113 int i;
1114
1115 for (i = 0; i < data->num_effects; i++)
1116 if (data->effect_ids[i] == effect_id)
1117 return i+1;
1118
1119 return 0;
1120}
1121
1122static void hidpp_ff_work_handler(struct work_struct *w)
1123{
1124 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1125 struct hidpp_ff_private_data *data = wd->data;
1126 struct hidpp_report response;
1127 u8 slot;
1128 int ret;
1129
1130 /* add slot number if needed */
1131 switch (wd->effect_id) {
1132 case HIDPP_FF_EFFECTID_AUTOCENTER:
1133 wd->params[0] = data->slot_autocenter;
1134 break;
1135 case HIDPP_FF_EFFECTID_NONE:
1136 /* leave slot as zero */
1137 break;
1138 default:
1139 /* find current slot for effect */
1140 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1141 break;
1142 }
1143
1144 /* send command and wait for reply */
1145 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1146 wd->command, wd->params, wd->size, &response);
1147
1148 if (ret) {
1149 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1150 goto out;
1151 }
1152
1153 /* parse return data */
1154 switch (wd->command) {
1155 case HIDPP_FF_DOWNLOAD_EFFECT:
1156 slot = response.fap.params[0];
1157 if (slot > 0 && slot <= data->num_effects) {
1158 if (wd->effect_id >= 0)
1159 /* regular effect uploaded */
1160 data->effect_ids[slot-1] = wd->effect_id;
1161 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1162 /* autocenter spring uploaded */
1163 data->slot_autocenter = slot;
1164 }
1165 break;
1166 case HIDPP_FF_DESTROY_EFFECT:
1167 if (wd->effect_id >= 0)
1168 /* regular effect destroyed */
1169 data->effect_ids[wd->params[0]-1] = -1;
1170 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1171 /* autocenter spring destoyed */
1172 data->slot_autocenter = 0;
1173 break;
1174 case HIDPP_FF_SET_GLOBAL_GAINS:
1175 data->gain = (wd->params[0] << 8) + wd->params[1];
1176 break;
1177 case HIDPP_FF_SET_APERTURE:
1178 data->range = (wd->params[0] << 8) + wd->params[1];
1179 break;
1180 default:
1181 /* no action needed */
1182 break;
1183 }
1184
1185out:
1186 atomic_dec(&data->workqueue_size);
1187 kfree(wd);
1188}
1189
1190static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1191{
1192 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1193 int s;
1194
1195 if (!wd)
1196 return -ENOMEM;
1197
1198 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1199
1200 wd->data = data;
1201 wd->effect_id = effect_id;
1202 wd->command = command;
1203 wd->size = size;
1204 memcpy(wd->params, params, size);
1205
1206 atomic_inc(&data->workqueue_size);
1207 queue_work(data->wq, &wd->work);
1208
1209 /* warn about excessive queue size */
1210 s = atomic_read(&data->workqueue_size);
1211 if (s >= 20 && s % 20 == 0)
1212 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1213
1214 return 0;
1215}
1216
1217static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1218{
1219 struct hidpp_ff_private_data *data = dev->ff->private;
1220 u8 params[20];
1221 u8 size;
1222 int force;
1223
1224 /* set common parameters */
1225 params[2] = effect->replay.length >> 8;
1226 params[3] = effect->replay.length & 255;
1227 params[4] = effect->replay.delay >> 8;
1228 params[5] = effect->replay.delay & 255;
1229
1230 switch (effect->type) {
1231 case FF_CONSTANT:
1232 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1233 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1234 params[6] = force >> 8;
1235 params[7] = force & 255;
1236 params[8] = effect->u.constant.envelope.attack_level >> 7;
1237 params[9] = effect->u.constant.envelope.attack_length >> 8;
1238 params[10] = effect->u.constant.envelope.attack_length & 255;
1239 params[11] = effect->u.constant.envelope.fade_level >> 7;
1240 params[12] = effect->u.constant.envelope.fade_length >> 8;
1241 params[13] = effect->u.constant.envelope.fade_length & 255;
1242 size = 14;
1243 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1244 effect->u.constant.level,
1245 effect->direction, force);
1246 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1247 effect->u.constant.envelope.attack_level,
1248 effect->u.constant.envelope.attack_length,
1249 effect->u.constant.envelope.fade_level,
1250 effect->u.constant.envelope.fade_length);
1251 break;
1252 case FF_PERIODIC:
1253 {
1254 switch (effect->u.periodic.waveform) {
1255 case FF_SINE:
1256 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1257 break;
1258 case FF_SQUARE:
1259 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1260 break;
1261 case FF_SAW_UP:
1262 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1263 break;
1264 case FF_SAW_DOWN:
1265 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1266 break;
1267 case FF_TRIANGLE:
1268 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1269 break;
1270 default:
1271 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1272 return -EINVAL;
1273 }
1274 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1275 params[6] = effect->u.periodic.magnitude >> 8;
1276 params[7] = effect->u.periodic.magnitude & 255;
1277 params[8] = effect->u.periodic.offset >> 8;
1278 params[9] = effect->u.periodic.offset & 255;
1279 params[10] = effect->u.periodic.period >> 8;
1280 params[11] = effect->u.periodic.period & 255;
1281 params[12] = effect->u.periodic.phase >> 8;
1282 params[13] = effect->u.periodic.phase & 255;
1283 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1284 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1285 params[16] = effect->u.periodic.envelope.attack_length & 255;
1286 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1287 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1288 params[19] = effect->u.periodic.envelope.fade_length & 255;
1289 size = 20;
1290 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1291 effect->u.periodic.magnitude, effect->direction,
1292 effect->u.periodic.offset,
1293 effect->u.periodic.period,
1294 effect->u.periodic.phase);
1295 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1296 effect->u.periodic.envelope.attack_level,
1297 effect->u.periodic.envelope.attack_length,
1298 effect->u.periodic.envelope.fade_level,
1299 effect->u.periodic.envelope.fade_length);
1300 break;
1301 }
1302 case FF_RAMP:
1303 params[1] = HIDPP_FF_EFFECT_RAMP;
1304 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1305 params[6] = force >> 8;
1306 params[7] = force & 255;
1307 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1308 params[8] = force >> 8;
1309 params[9] = force & 255;
1310 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1311 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1312 params[12] = effect->u.ramp.envelope.attack_length & 255;
1313 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1314 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1315 params[15] = effect->u.ramp.envelope.fade_length & 255;
1316 size = 16;
1317 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1318 effect->u.ramp.start_level,
1319 effect->u.ramp.end_level,
1320 effect->direction, force);
1321 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1322 effect->u.ramp.envelope.attack_level,
1323 effect->u.ramp.envelope.attack_length,
1324 effect->u.ramp.envelope.fade_level,
1325 effect->u.ramp.envelope.fade_length);
1326 break;
1327 case FF_FRICTION:
1328 case FF_INERTIA:
1329 case FF_SPRING:
1330 case FF_DAMPER:
1331 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1332 params[6] = effect->u.condition[0].left_saturation >> 9;
1333 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1334 params[8] = effect->u.condition[0].left_coeff >> 8;
1335 params[9] = effect->u.condition[0].left_coeff & 255;
1336 params[10] = effect->u.condition[0].deadband >> 9;
1337 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1338 params[12] = effect->u.condition[0].center >> 8;
1339 params[13] = effect->u.condition[0].center & 255;
1340 params[14] = effect->u.condition[0].right_coeff >> 8;
1341 params[15] = effect->u.condition[0].right_coeff & 255;
1342 params[16] = effect->u.condition[0].right_saturation >> 9;
1343 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1344 size = 18;
1345 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1346 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1347 effect->u.condition[0].left_coeff,
1348 effect->u.condition[0].left_saturation,
1349 effect->u.condition[0].right_coeff,
1350 effect->u.condition[0].right_saturation);
1351 dbg_hid(" deadband=%d, center=%d\n",
1352 effect->u.condition[0].deadband,
1353 effect->u.condition[0].center);
1354 break;
1355 default:
1356 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1357 return -EINVAL;
1358 }
1359
1360 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1361}
1362
1363static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1364{
1365 struct hidpp_ff_private_data *data = dev->ff->private;
1366 u8 params[2];
1367
1368 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1369
1370 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1371
1372 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1373}
1374
1375static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1376{
1377 struct hidpp_ff_private_data *data = dev->ff->private;
1378 u8 slot = 0;
1379
1380 dbg_hid("Erasing effect %d.\n", effect_id);
1381
1382 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1383}
1384
1385static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1386{
1387 struct hidpp_ff_private_data *data = dev->ff->private;
1388 u8 params[18];
1389
1390 dbg_hid("Setting autocenter to %d.\n", magnitude);
1391
1392 /* start a standard spring effect */
1393 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1394 /* zero delay and duration */
1395 params[2] = params[3] = params[4] = params[5] = 0;
1396 /* set coeff to 25% of saturation */
1397 params[8] = params[14] = magnitude >> 11;
1398 params[9] = params[15] = (magnitude >> 3) & 255;
1399 params[6] = params[16] = magnitude >> 9;
1400 params[7] = params[17] = (magnitude >> 1) & 255;
1401 /* zero deadband and center */
1402 params[10] = params[11] = params[12] = params[13] = 0;
1403
1404 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1405}
1406
1407static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1408{
1409 struct hidpp_ff_private_data *data = dev->ff->private;
1410 u8 params[4];
1411
1412 dbg_hid("Setting gain to %d.\n", gain);
1413
1414 params[0] = gain >> 8;
1415 params[1] = gain & 255;
1416 params[2] = 0; /* no boost */
1417 params[3] = 0;
1418
1419 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1420}
1421
1422static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1423{
1424 struct hid_device *hid = to_hid_device(dev);
1425 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1426 struct input_dev *idev = hidinput->input;
1427 struct hidpp_ff_private_data *data = idev->ff->private;
1428
1429 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1430}
1431
1432static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1433{
1434 struct hid_device *hid = to_hid_device(dev);
1435 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1436 struct input_dev *idev = hidinput->input;
1437 struct hidpp_ff_private_data *data = idev->ff->private;
1438 u8 params[2];
1439 int range = simple_strtoul(buf, NULL, 10);
1440
1441 range = clamp(range, 180, 900);
1442
1443 params[0] = range >> 8;
1444 params[1] = range & 0x00FF;
1445
1446 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1447
1448 return count;
1449}
1450
1451static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1452
1453static void hidpp_ff_destroy(struct ff_device *ff)
1454{
1455 struct hidpp_ff_private_data *data = ff->private;
1456
1457 kfree(data->effect_ids);
1458}
1459
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01001460static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
Edwin Veldsff21a632016-01-11 00:25:15 +01001461{
1462 struct hid_device *hid = hidpp->hid_dev;
1463 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1464 struct input_dev *dev = hidinput->input;
1465 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1466 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1467 struct ff_device *ff;
1468 struct hidpp_report response;
1469 struct hidpp_ff_private_data *data;
1470 int error, j, num_slots;
1471 u8 version;
1472
1473 if (!dev) {
1474 hid_err(hid, "Struct input_dev not set!\n");
1475 return -EINVAL;
1476 }
1477
1478 /* Get firmware release */
1479 version = bcdDevice & 255;
1480
1481 /* Set supported force feedback capabilities */
1482 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1483 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1484 if (version > 1)
1485 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1486 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1487
1488 /* Read number of slots available in device */
1489 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1490 HIDPP_FF_GET_INFO, NULL, 0, &response);
1491 if (error) {
1492 if (error < 0)
1493 return error;
1494 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1495 __func__, error);
1496 return -EPROTO;
1497 }
1498
1499 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1500
1501 error = input_ff_create(dev, num_slots);
1502
1503 if (error) {
1504 hid_err(dev, "Failed to create FF device!\n");
1505 return error;
1506 }
1507
1508 data = kzalloc(sizeof(*data), GFP_KERNEL);
1509 if (!data)
1510 return -ENOMEM;
1511 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1512 if (!data->effect_ids) {
1513 kfree(data);
1514 return -ENOMEM;
1515 }
1516 data->hidpp = hidpp;
1517 data->feature_index = feature_index;
1518 data->version = version;
1519 data->slot_autocenter = 0;
1520 data->num_effects = num_slots;
1521 for (j = 0; j < num_slots; j++)
1522 data->effect_ids[j] = -1;
1523
1524 ff = dev->ff;
1525 ff->private = data;
1526
1527 ff->upload = hidpp_ff_upload_effect;
1528 ff->erase = hidpp_ff_erase_effect;
1529 ff->playback = hidpp_ff_playback;
1530 ff->set_gain = hidpp_ff_set_gain;
1531 ff->set_autocenter = hidpp_ff_set_autocenter;
1532 ff->destroy = hidpp_ff_destroy;
1533
1534
1535 /* reset all forces */
1536 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1537 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1538
1539 /* Read current Range */
1540 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1541 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1542 if (error)
1543 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1544 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1545
1546 /* Create sysfs interface */
1547 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1548 if (error)
1549 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1550
1551 /* Read the current gain values */
1552 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1553 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1554 if (error)
1555 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1556 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1557 /* ignore boost value at response.fap.params[2] */
1558
1559 /* init the hardware command queue */
1560 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1561 atomic_set(&data->workqueue_size, 0);
1562
1563 /* initialize with zero autocenter to get wheel in usable state */
1564 hidpp_ff_set_autocenter(dev, 0);
1565
1566 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1567
1568 return 0;
1569}
1570
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01001571static int hidpp_ff_deinit(struct hid_device *hid)
Edwin Veldsff21a632016-01-11 00:25:15 +01001572{
1573 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1574 struct input_dev *dev = hidinput->input;
1575 struct hidpp_ff_private_data *data;
1576
1577 if (!dev) {
1578 hid_err(hid, "Struct input_dev not found!\n");
1579 return -EINVAL;
1580 }
1581
1582 hid_info(hid, "Unloading HID++ force feedback.\n");
1583 data = dev->ff->private;
1584 if (!data) {
1585 hid_err(hid, "Private data not found!\n");
1586 return -EINVAL;
1587 }
1588
1589 destroy_workqueue(data->wq);
1590 device_remove_file(&hid->dev, &dev_attr_range);
1591
1592 return 0;
1593}
1594
1595
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001596/* ************************************************************************** */
1597/* */
1598/* Device Support */
1599/* */
1600/* ************************************************************************** */
1601
1602/* -------------------------------------------------------------------------- */
1603/* Touchpad HID++ devices */
1604/* -------------------------------------------------------------------------- */
1605
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001606#define WTP_MANUAL_RESOLUTION 39
1607
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001608struct wtp_data {
1609 struct input_dev *input;
1610 u16 x_size, y_size;
1611 u8 finger_count;
1612 u8 mt_feature_index;
1613 u8 button_feature_index;
1614 u8 maxcontacts;
1615 bool flip_y;
1616 unsigned int resolution;
1617};
1618
1619static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1620 struct hid_field *field, struct hid_usage *usage,
1621 unsigned long **bit, int *max)
1622{
1623 return -1;
1624}
1625
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001626static void wtp_populate_input(struct hidpp_device *hidpp,
1627 struct input_dev *input_dev, bool origin_is_hid_core)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001628{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001629 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001630
1631 __set_bit(EV_ABS, input_dev->evbit);
1632 __set_bit(EV_KEY, input_dev->evbit);
1633 __clear_bit(EV_REL, input_dev->evbit);
1634 __clear_bit(EV_LED, input_dev->evbit);
1635
1636 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
1637 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
1638 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
1639 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
1640
1641 /* Max pressure is not given by the devices, pick one */
1642 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
1643
1644 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
1645
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001646 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
1647 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
1648 else
1649 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001650
1651 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
1652 INPUT_MT_DROP_UNUSED);
1653
1654 wd->input = input_dev;
1655}
1656
1657static void wtp_touch_event(struct wtp_data *wd,
1658 struct hidpp_touchpad_raw_xy_finger *touch_report)
1659{
1660 int slot;
1661
1662 if (!touch_report->finger_id || touch_report->contact_type)
1663 /* no actual data */
1664 return;
1665
1666 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
1667
1668 input_mt_slot(wd->input, slot);
1669 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
1670 touch_report->contact_status);
1671 if (touch_report->contact_status) {
1672 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
1673 touch_report->x);
1674 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
1675 wd->flip_y ? wd->y_size - touch_report->y :
1676 touch_report->y);
1677 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
1678 touch_report->area);
1679 }
1680}
1681
1682static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
1683 struct hidpp_touchpad_raw_xy *raw)
1684{
1685 struct wtp_data *wd = hidpp->private_data;
1686 int i;
1687
1688 for (i = 0; i < 2; i++)
1689 wtp_touch_event(wd, &(raw->fingers[i]));
1690
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001691 if (raw->end_of_frame &&
1692 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001693 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
1694
1695 if (raw->end_of_frame || raw->finger_count <= 2) {
1696 input_mt_sync_frame(wd->input);
1697 input_sync(wd->input);
1698 }
1699}
1700
1701static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
1702{
1703 struct wtp_data *wd = hidpp->private_data;
1704 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
1705 (data[7] >> 4) * (data[7] >> 4)) / 2;
1706 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
1707 (data[13] >> 4) * (data[13] >> 4)) / 2;
1708 struct hidpp_touchpad_raw_xy raw = {
1709 .timestamp = data[1],
1710 .fingers = {
1711 {
1712 .contact_type = 0,
1713 .contact_status = !!data[7],
1714 .x = get_unaligned_le16(&data[3]),
1715 .y = get_unaligned_le16(&data[5]),
1716 .z = c1_area,
1717 .area = c1_area,
1718 .finger_id = data[2],
1719 }, {
1720 .contact_type = 0,
1721 .contact_status = !!data[13],
1722 .x = get_unaligned_le16(&data[9]),
1723 .y = get_unaligned_le16(&data[11]),
1724 .z = c2_area,
1725 .area = c2_area,
1726 .finger_id = data[8],
1727 }
1728 },
1729 .finger_count = wd->maxcontacts,
1730 .spurious_flag = 0,
1731 .end_of_frame = (data[0] >> 7) == 0,
1732 .button = data[0] & 0x01,
1733 };
1734
1735 wtp_send_raw_xy_event(hidpp, &raw);
1736
1737 return 1;
1738}
1739
1740static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
1741{
1742 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1743 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001744 struct hidpp_report *report = (struct hidpp_report *)data;
1745 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001746
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001747 if (!wd || !wd->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001748 return 1;
1749
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001750 switch (data[0]) {
1751 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +01001752 if (size < 2) {
1753 hid_err(hdev, "Received HID report of bad size (%d)",
1754 size);
1755 return 1;
1756 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001757 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
1758 input_event(wd->input, EV_KEY, BTN_LEFT,
1759 !!(data[1] & 0x01));
1760 input_event(wd->input, EV_KEY, BTN_RIGHT,
1761 !!(data[1] & 0x02));
1762 input_sync(wd->input);
Peter Wu8abd8202014-12-16 01:50:16 +01001763 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001764 } else {
1765 if (size < 21)
1766 return 1;
1767 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
1768 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001769 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +01001770 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001771 if ((report->fap.feature_index != wd->mt_feature_index) ||
1772 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
1773 return 1;
1774 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
1775
1776 wtp_send_raw_xy_event(hidpp, &raw);
1777 return 0;
1778 }
1779
1780 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001781}
1782
1783static int wtp_get_config(struct hidpp_device *hidpp)
1784{
1785 struct wtp_data *wd = hidpp->private_data;
1786 struct hidpp_touchpad_raw_info raw_info = {0};
1787 u8 feature_type;
1788 int ret;
1789
1790 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
1791 &wd->mt_feature_index, &feature_type);
1792 if (ret)
1793 /* means that the device is not powered up */
1794 return ret;
1795
1796 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
1797 &raw_info);
1798 if (ret)
1799 return ret;
1800
1801 wd->x_size = raw_info.x_size;
1802 wd->y_size = raw_info.y_size;
1803 wd->maxcontacts = raw_info.maxcontacts;
1804 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
1805 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001806 if (!wd->resolution)
1807 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001808
1809 return 0;
1810}
1811
1812static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
1813{
1814 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1815 struct wtp_data *wd;
1816
1817 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
1818 GFP_KERNEL);
1819 if (!wd)
1820 return -ENOMEM;
1821
1822 hidpp->private_data = wd;
1823
1824 return 0;
1825};
1826
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001827static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001828{
1829 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1830 struct wtp_data *wd = hidpp->private_data;
1831 int ret;
1832
1833 if (!connected)
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001834 return 0;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001835
1836 if (!wd->x_size) {
1837 ret = wtp_get_config(hidpp);
1838 if (ret) {
1839 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001840 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001841 }
1842 }
1843
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001844 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001845 true, true);
1846}
1847
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02001848/* ------------------------------------------------------------------------- */
1849/* Logitech M560 devices */
1850/* ------------------------------------------------------------------------- */
1851
1852/*
1853 * Logitech M560 protocol overview
1854 *
1855 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1856 * the sides buttons are pressed, it sends some keyboard keys events
1857 * instead of buttons ones.
1858 * To complicate things further, the middle button keys sequence
1859 * is different from the odd press and the even press.
1860 *
1861 * forward button -> Super_R
1862 * backward button -> Super_L+'d' (press only)
1863 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1864 * 2nd time: left-click (press only)
1865 * NB: press-only means that when the button is pressed, the
1866 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1867 * together sequentially; instead when the button is released, no event is
1868 * generated !
1869 *
1870 * With the command
1871 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1872 * the mouse reacts differently:
1873 * - it never sends a keyboard key event
1874 * - for the three mouse button it sends:
1875 * middle button press 11<xx>0a 3500af00...
1876 * side 1 button (forward) press 11<xx>0a 3500b000...
1877 * side 2 button (backward) press 11<xx>0a 3500ae00...
1878 * middle/side1/side2 button release 11<xx>0a 35000000...
1879 */
1880
1881static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1882
1883struct m560_private_data {
1884 struct input_dev *input;
1885};
1886
1887/* how buttons are mapped in the report */
1888#define M560_MOUSE_BTN_LEFT 0x01
1889#define M560_MOUSE_BTN_RIGHT 0x02
1890#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1891#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1892
1893#define M560_SUB_ID 0x0a
1894#define M560_BUTTON_MODE_REGISTER 0x35
1895
1896static int m560_send_config_command(struct hid_device *hdev, bool connected)
1897{
1898 struct hidpp_report response;
1899 struct hidpp_device *hidpp_dev;
1900
1901 hidpp_dev = hid_get_drvdata(hdev);
1902
1903 if (!connected)
1904 return -ENODEV;
1905
1906 return hidpp_send_rap_command_sync(
1907 hidpp_dev,
1908 REPORT_ID_HIDPP_SHORT,
1909 M560_SUB_ID,
1910 M560_BUTTON_MODE_REGISTER,
1911 (u8 *)m560_config_parameter,
1912 sizeof(m560_config_parameter),
1913 &response
1914 );
1915}
1916
1917static int m560_allocate(struct hid_device *hdev)
1918{
1919 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1920 struct m560_private_data *d;
1921
1922 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1923 GFP_KERNEL);
1924 if (!d)
1925 return -ENOMEM;
1926
1927 hidpp->private_data = d;
1928
1929 return 0;
1930};
1931
1932static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1933{
1934 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1935 struct m560_private_data *mydata = hidpp->private_data;
1936
1937 /* sanity check */
1938 if (!mydata || !mydata->input) {
1939 hid_err(hdev, "error in parameter\n");
1940 return -EINVAL;
1941 }
1942
1943 if (size < 7) {
1944 hid_err(hdev, "error in report\n");
1945 return 0;
1946 }
1947
1948 if (data[0] == REPORT_ID_HIDPP_LONG &&
1949 data[2] == M560_SUB_ID && data[6] == 0x00) {
1950 /*
1951 * m560 mouse report for middle, forward and backward button
1952 *
1953 * data[0] = 0x11
1954 * data[1] = device-id
1955 * data[2] = 0x0a
1956 * data[5] = 0xaf -> middle
1957 * 0xb0 -> forward
1958 * 0xae -> backward
1959 * 0x00 -> release all
1960 * data[6] = 0x00
1961 */
1962
1963 switch (data[5]) {
1964 case 0xaf:
1965 input_report_key(mydata->input, BTN_MIDDLE, 1);
1966 break;
1967 case 0xb0:
1968 input_report_key(mydata->input, BTN_FORWARD, 1);
1969 break;
1970 case 0xae:
1971 input_report_key(mydata->input, BTN_BACK, 1);
1972 break;
1973 case 0x00:
1974 input_report_key(mydata->input, BTN_BACK, 0);
1975 input_report_key(mydata->input, BTN_FORWARD, 0);
1976 input_report_key(mydata->input, BTN_MIDDLE, 0);
1977 break;
1978 default:
1979 hid_err(hdev, "error in report\n");
1980 return 0;
1981 }
1982 input_sync(mydata->input);
1983
1984 } else if (data[0] == 0x02) {
1985 /*
1986 * Logitech M560 mouse report
1987 *
1988 * data[0] = type (0x02)
1989 * data[1..2] = buttons
1990 * data[3..5] = xy
1991 * data[6] = wheel
1992 */
1993
1994 int v;
1995
1996 input_report_key(mydata->input, BTN_LEFT,
1997 !!(data[1] & M560_MOUSE_BTN_LEFT));
1998 input_report_key(mydata->input, BTN_RIGHT,
1999 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2000
2001 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2002 input_report_rel(mydata->input, REL_HWHEEL, -1);
2003 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2004 input_report_rel(mydata->input, REL_HWHEEL, 1);
2005
2006 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2007 input_report_rel(mydata->input, REL_X, v);
2008
2009 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2010 input_report_rel(mydata->input, REL_Y, v);
2011
2012 v = hid_snto32(data[6], 8);
2013 input_report_rel(mydata->input, REL_WHEEL, v);
2014
2015 input_sync(mydata->input);
2016 }
2017
2018 return 1;
2019}
2020
2021static void m560_populate_input(struct hidpp_device *hidpp,
2022 struct input_dev *input_dev, bool origin_is_hid_core)
2023{
2024 struct m560_private_data *mydata = hidpp->private_data;
2025
2026 mydata->input = input_dev;
2027
2028 __set_bit(EV_KEY, mydata->input->evbit);
2029 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2030 __set_bit(BTN_RIGHT, mydata->input->keybit);
2031 __set_bit(BTN_LEFT, mydata->input->keybit);
2032 __set_bit(BTN_BACK, mydata->input->keybit);
2033 __set_bit(BTN_FORWARD, mydata->input->keybit);
2034
2035 __set_bit(EV_REL, mydata->input->evbit);
2036 __set_bit(REL_X, mydata->input->relbit);
2037 __set_bit(REL_Y, mydata->input->relbit);
2038 __set_bit(REL_WHEEL, mydata->input->relbit);
2039 __set_bit(REL_HWHEEL, mydata->input->relbit);
2040}
2041
2042static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2043 struct hid_field *field, struct hid_usage *usage,
2044 unsigned long **bit, int *max)
2045{
2046 return -1;
2047}
2048
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002049/* ------------------------------------------------------------------------- */
2050/* Logitech K400 devices */
2051/* ------------------------------------------------------------------------- */
2052
2053/*
2054 * The Logitech K400 keyboard has an embedded touchpad which is seen
2055 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2056 * tap-to-click but the setting is not remembered accross reset, annoying some
2057 * users.
2058 *
2059 * We can toggle this feature from the host by using the feature 0x6010:
2060 * Touchpad FW items
2061 */
2062
2063struct k400_private_data {
2064 u8 feature_index;
2065};
2066
2067static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2068{
2069 struct k400_private_data *k400 = hidpp->private_data;
2070 struct hidpp_touchpad_fw_items items = {};
2071 int ret;
2072 u8 feature_type;
2073
2074 if (!k400->feature_index) {
2075 ret = hidpp_root_get_feature(hidpp,
2076 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2077 &k400->feature_index, &feature_type);
2078 if (ret)
2079 /* means that the device is not powered up */
2080 return ret;
2081 }
2082
2083 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2084 if (ret)
2085 return ret;
2086
2087 return 0;
2088}
2089
2090static int k400_allocate(struct hid_device *hdev)
2091{
2092 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2093 struct k400_private_data *k400;
2094
2095 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2096 GFP_KERNEL);
2097 if (!k400)
2098 return -ENOMEM;
2099
2100 hidpp->private_data = k400;
2101
2102 return 0;
2103};
2104
2105static int k400_connect(struct hid_device *hdev, bool connected)
2106{
2107 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2108
2109 if (!connected)
2110 return 0;
2111
2112 if (!disable_tap_to_click)
2113 return 0;
2114
2115 return k400_disable_tap_to_click(hidpp);
2116}
2117
Simon Wood7f4b49f2015-11-19 16:42:13 -07002118/* ------------------------------------------------------------------------- */
2119/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2120/* ------------------------------------------------------------------------- */
2121
2122#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2123
Simon Wood7f4b49f2015-11-19 16:42:13 -07002124static int g920_get_config(struct hidpp_device *hidpp)
2125{
Simon Wood7f4b49f2015-11-19 16:42:13 -07002126 u8 feature_type;
2127 u8 feature_index;
2128 int ret;
2129
Simon Wood7f4b49f2015-11-19 16:42:13 -07002130 /* Find feature and store for later use */
2131 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2132 &feature_index, &feature_type);
2133 if (ret)
2134 return ret;
2135
Edwin Veldsff21a632016-01-11 00:25:15 +01002136 ret = hidpp_ff_init(hidpp, feature_index);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002137 if (ret)
Edwin Veldsff21a632016-01-11 00:25:15 +01002138 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2139 ret);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002140
2141 return 0;
2142}
2143
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002144/* -------------------------------------------------------------------------- */
2145/* Generic HID++ devices */
2146/* -------------------------------------------------------------------------- */
2147
2148static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2149 struct hid_field *field, struct hid_usage *usage,
2150 unsigned long **bit, int *max)
2151{
2152 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2153
2154 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2155 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002156 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2157 field->application != HID_GD_MOUSE)
2158 return m560_input_mapping(hdev, hi, field, usage, bit, max);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002159
2160 return 0;
2161}
2162
Simon Wood0b1804e2015-11-19 16:42:15 -07002163static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2164 struct hid_field *field, struct hid_usage *usage,
2165 unsigned long **bit, int *max)
2166{
2167 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2168
2169 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2170 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2171 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2172 usage->code == ABS_Y || usage->code == ABS_Z ||
2173 usage->code == ABS_RZ)) {
2174 field->application = HID_GD_MULTIAXIS;
2175 }
2176 }
2177
2178 return 0;
2179}
2180
2181
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002182static void hidpp_populate_input(struct hidpp_device *hidpp,
2183 struct input_dev *input, bool origin_is_hid_core)
2184{
2185 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2186 wtp_populate_input(hidpp, input, origin_is_hid_core);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002187 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2188 m560_populate_input(hidpp, input, origin_is_hid_core);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002189}
2190
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002191static int hidpp_input_configured(struct hid_device *hdev,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002192 struct hid_input *hidinput)
2193{
2194 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002195 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002196
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002197 hidpp_populate_input(hidpp, input, true);
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002198
2199 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002200}
2201
2202static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2203 int size)
2204{
2205 struct hidpp_report *question = hidpp->send_receive_buf;
2206 struct hidpp_report *answer = hidpp->send_receive_buf;
2207 struct hidpp_report *report = (struct hidpp_report *)data;
2208
2209 /*
2210 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +01002211 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002212 */
2213 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2214 /*
2215 * Check for a correct hidpp20 answer or the corresponding
2216 * error
2217 */
2218 if (hidpp_match_answer(question, report) ||
2219 hidpp_match_error(question, report)) {
2220 *answer = *report;
2221 hidpp->answer_available = true;
2222 wake_up(&hidpp->wait);
2223 /*
2224 * This was an answer to a command that this driver sent
2225 * We return 1 to hid-core to avoid forwarding the
2226 * command upstream as it has been treated by the driver
2227 */
2228
2229 return 1;
2230 }
2231 }
2232
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002233 if (unlikely(hidpp_report_is_connect_event(report))) {
2234 atomic_set(&hidpp->connected,
2235 !(report->rap.params[0] & (1 << 6)));
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002236 if (schedule_work(&hidpp->work) == 0)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002237 dbg_hid("%s: connect event already queued\n", __func__);
2238 return 1;
2239 }
2240
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002241 return 0;
2242}
2243
2244static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2245 u8 *data, int size)
2246{
2247 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01002248 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002249
Peter Wue529fea2014-12-17 00:23:51 +01002250 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002251 switch (data[0]) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07002252 case REPORT_ID_HIDPP_VERY_LONG:
2253 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2254 hid_err(hdev, "received hid++ report of bad size (%d)",
2255 size);
2256 return 1;
2257 }
2258 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2259 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002260 case REPORT_ID_HIDPP_LONG:
2261 if (size != HIDPP_REPORT_LONG_LENGTH) {
2262 hid_err(hdev, "received hid++ report of bad size (%d)",
2263 size);
2264 return 1;
2265 }
Peter Wue529fea2014-12-17 00:23:51 +01002266 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2267 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002268 case REPORT_ID_HIDPP_SHORT:
2269 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2270 hid_err(hdev, "received hid++ report of bad size (%d)",
2271 size);
2272 return 1;
2273 }
Peter Wue529fea2014-12-17 00:23:51 +01002274 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2275 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002276 }
2277
Peter Wue529fea2014-12-17 00:23:51 +01002278 /* If no report is available for further processing, skip calling
2279 * raw_event of subclasses. */
2280 if (ret != 0)
2281 return ret;
2282
Peter Hutterer5a2b1902016-06-29 19:28:01 +10002283 if (hidpp->quirks & HIDPP_QUIRK_HIDPP20_BATTERY) {
2284 ret = hidpp20_battery_event(hidpp, data, size);
2285 if (ret != 0)
2286 return ret;
2287 }
2288
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002289 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2290 return wtp_raw_event(hdev, data, size);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002291 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2292 return m560_raw_event(hdev, data, size);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002293
2294 return 0;
2295}
2296
Benjamin Tissoires33797822014-09-30 13:18:30 -04002297static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002298{
2299 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2300 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002301
Benjamin Tissoires33797822014-09-30 13:18:30 -04002302 if (use_unifying)
2303 /*
2304 * the device is connected through an Unifying receiver, and
2305 * might not be already connected.
2306 * Ask the receiver for its name.
2307 */
2308 name = hidpp_get_unifying_name(hidpp);
2309 else
Peter Wu02cc0972014-12-11 13:51:17 +01002310 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002311
Simon Wood7bfd2922015-11-19 16:42:12 -07002312 if (!name) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002313 hid_err(hdev, "unable to retrieve the name of the device");
Simon Wood7bfd2922015-11-19 16:42:12 -07002314 } else {
2315 dbg_hid("HID++: Got name: %s\n", name);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002316 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
Simon Wood7bfd2922015-11-19 16:42:12 -07002317 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002318
2319 kfree(name);
2320}
2321
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002322static int hidpp_input_open(struct input_dev *dev)
2323{
2324 struct hid_device *hid = input_get_drvdata(dev);
2325
2326 return hid_hw_open(hid);
2327}
2328
2329static void hidpp_input_close(struct input_dev *dev)
2330{
2331 struct hid_device *hid = input_get_drvdata(dev);
2332
2333 hid_hw_close(hid);
2334}
2335
2336static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2337{
2338 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002339 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002340
2341 if (!input_dev)
2342 return NULL;
2343
2344 input_set_drvdata(input_dev, hdev);
2345 input_dev->open = hidpp_input_open;
2346 input_dev->close = hidpp_input_close;
2347
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002348 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002349 input_dev->phys = hdev->phys;
2350 input_dev->uniq = hdev->uniq;
2351 input_dev->id.bustype = hdev->bus;
2352 input_dev->id.vendor = hdev->vendor;
2353 input_dev->id.product = hdev->product;
2354 input_dev->id.version = hdev->version;
2355 input_dev->dev.parent = &hdev->dev;
2356
2357 return input_dev;
2358}
2359
2360static void hidpp_connect_event(struct hidpp_device *hidpp)
2361{
2362 struct hid_device *hdev = hidpp->hid_dev;
2363 int ret = 0;
2364 bool connected = atomic_read(&hidpp->connected);
2365 struct input_dev *input;
2366 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002367
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002368 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2369 ret = wtp_connect(hdev, connected);
2370 if (ret)
2371 return;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002372 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2373 ret = m560_send_config_command(hdev, connected);
2374 if (ret)
2375 return;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002376 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2377 ret = k400_connect(hdev, connected);
2378 if (ret)
2379 return;
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002380 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002381
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002382 if (!connected || hidpp->delayed_input)
2383 return;
2384
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002385 /* the device is already connected, we can ask for its name and
2386 * protocol */
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002387 if (!hidpp->protocol_major) {
2388 ret = !hidpp_is_connected(hidpp);
2389 if (ret) {
2390 hid_err(hdev, "Can not get the protocol version.\n");
2391 return;
2392 }
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002393 hid_info(hdev, "HID++ %u.%u device connected.\n",
2394 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002395 }
2396
Peter Hutterer5a2b1902016-06-29 19:28:01 +10002397 hidpp_initialize_battery(hidpp);
2398
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002399 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
2400 /* if HID created the input nodes for us, we can stop now */
2401 return;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002402
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002403 if (!hidpp->name || hidpp->name == hdev->name) {
2404 name = hidpp_get_device_name(hidpp);
2405 if (!name) {
2406 hid_err(hdev,
2407 "unable to retrieve the name of the device");
2408 return;
2409 }
2410
2411 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2412 kfree(name);
2413 if (!devm_name)
2414 return;
2415
2416 hidpp->name = devm_name;
2417 }
2418
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002419 input = hidpp_allocate_input(hdev);
2420 if (!input) {
2421 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2422 return;
2423 }
2424
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002425 hidpp_populate_input(hidpp, input, false);
2426
2427 ret = input_register_device(input);
2428 if (ret)
2429 input_free_device(input);
2430
2431 hidpp->delayed_input = input;
2432}
2433
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002434static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2435{
2436 struct hidpp_device *hidpp;
2437 int ret;
2438 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002439 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002440
2441 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2442 GFP_KERNEL);
2443 if (!hidpp)
2444 return -ENOMEM;
2445
2446 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002447 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002448 hid_set_drvdata(hdev, hidpp);
2449
2450 hidpp->quirks = id->driver_data;
2451
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04002452 if (disable_raw_mode) {
2453 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002454 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04002455 }
2456
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002457 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2458 ret = wtp_allocate(hdev, id);
2459 if (ret)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002460 goto allocate_fail;
2461 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2462 ret = m560_allocate(hdev);
2463 if (ret)
2464 goto allocate_fail;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002465 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2466 ret = k400_allocate(hdev);
2467 if (ret)
2468 goto allocate_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002469 }
2470
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002471 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002472 mutex_init(&hidpp->send_mutex);
2473 init_waitqueue_head(&hidpp->wait);
2474
2475 ret = hid_parse(hdev);
2476 if (ret) {
2477 hid_err(hdev, "%s:parse failed\n", __func__);
2478 goto hid_parse_fail;
2479 }
2480
Simon Wood7bfd2922015-11-19 16:42:12 -07002481 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2482 connect_mask &= ~HID_CONNECT_HIDINPUT;
2483
2484 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2485 ret = hid_hw_start(hdev, connect_mask);
2486 if (ret) {
2487 hid_err(hdev, "hw start failed\n");
2488 goto hid_hw_start_fail;
2489 }
2490 ret = hid_hw_open(hdev);
2491 if (ret < 0) {
2492 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
2493 __func__, ret);
2494 hid_hw_stop(hdev);
2495 goto hid_hw_start_fail;
2496 }
2497 }
2498
2499
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002500 /* Allow incoming packets */
2501 hid_device_io_start(hdev);
2502
2503 connected = hidpp_is_connected(hidpp);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002504 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
2505 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02002506 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002507 hid_err(hdev, "Device not connected");
Simon Wood7bfd2922015-11-19 16:42:12 -07002508 goto hid_hw_open_failed;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002509 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002510
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002511 hid_info(hdev, "HID++ %u.%u device connected.\n",
2512 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002513 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002514
Benjamin Tissoires33797822014-09-30 13:18:30 -04002515 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002516 atomic_set(&hidpp->connected, connected);
Benjamin Tissoires33797822014-09-30 13:18:30 -04002517
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002518 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002519 ret = wtp_get_config(hidpp);
2520 if (ret)
Simon Wood7bfd2922015-11-19 16:42:12 -07002521 goto hid_hw_open_failed;
Simon Wood7f4b49f2015-11-19 16:42:13 -07002522 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2523 ret = g920_get_config(hidpp);
2524 if (ret)
2525 goto hid_hw_open_failed;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002526 }
2527
2528 /* Block incoming packets */
2529 hid_device_io_stop(hdev);
2530
Simon Wood7bfd2922015-11-19 16:42:12 -07002531 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2532 ret = hid_hw_start(hdev, connect_mask);
2533 if (ret) {
2534 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
2535 goto hid_hw_start_fail;
2536 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002537 }
2538
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002539 /* Allow incoming packets */
2540 hid_device_io_start(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002541
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002542 hidpp_connect_event(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002543
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002544 return ret;
2545
Simon Wood7bfd2922015-11-19 16:42:12 -07002546hid_hw_open_failed:
2547 hid_device_io_stop(hdev);
2548 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2549 hid_hw_close(hdev);
2550 hid_hw_stop(hdev);
2551 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002552hid_hw_start_fail:
2553hid_parse_fail:
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002554 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002555 mutex_destroy(&hidpp->send_mutex);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002556allocate_fail:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002557 hid_set_drvdata(hdev, NULL);
2558 return ret;
2559}
2560
2561static void hidpp_remove(struct hid_device *hdev)
2562{
2563 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2564
Simon Wood7f4b49f2015-11-19 16:42:13 -07002565 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
Edwin Veldsff21a632016-01-11 00:25:15 +01002566 hidpp_ff_deinit(hdev);
Simon Wood7bfd2922015-11-19 16:42:12 -07002567 hid_hw_close(hdev);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002568 }
Simon Wood7bfd2922015-11-19 16:42:12 -07002569 hid_hw_stop(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002570 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002571 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002572}
2573
2574static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002575 { /* wireless touchpad */
2576 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2577 USB_VENDOR_ID_LOGITECH, 0x4011),
2578 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
2579 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002580 { /* wireless touchpad T650 */
2581 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2582 USB_VENDOR_ID_LOGITECH, 0x4101),
2583 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002584 { /* wireless touchpad T651 */
2585 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
2586 USB_DEVICE_ID_LOGITECH_T651),
2587 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002588 { /* Mouse logitech M560 */
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04002589 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002590 USB_VENDOR_ID_LOGITECH, 0x402d),
2591 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002592 { /* Keyboard logitech K400 */
2593 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2594 USB_VENDOR_ID_LOGITECH, 0x4024),
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002595 .driver_data = HIDPP_QUIRK_CLASS_K400 },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002596
2597 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2598 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
Simon Wood7bfd2922015-11-19 16:42:12 -07002599
2600 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
2601 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002602 {}
2603};
2604
2605MODULE_DEVICE_TABLE(hid, hidpp_devices);
2606
2607static struct hid_driver hidpp_driver = {
2608 .name = "logitech-hidpp-device",
2609 .id_table = hidpp_devices,
2610 .probe = hidpp_probe,
2611 .remove = hidpp_remove,
2612 .raw_event = hidpp_raw_event,
2613 .input_configured = hidpp_input_configured,
2614 .input_mapping = hidpp_input_mapping,
Simon Wood0b1804e2015-11-19 16:42:15 -07002615 .input_mapped = hidpp_input_mapped,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002616};
2617
2618module_hid_driver(hidpp_driver);