blob: db15cfc622c49dca1b2e2d300dee6a0badb4d42f [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)
Benjamin Tissoires843c6242017-03-27 16:59:26 +020065#define HIDPP_QUIRK_UNIFYING BIT(25)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040066
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100067#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040068
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020069#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
70#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
71
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040072/*
73 * There are two hidpp protocols in use, the first version hidpp10 is known
74 * as register access protocol or RAP, the second version hidpp20 is known as
75 * feature access protocol or FAP
76 *
77 * Most older devices (including the Unifying usb receiver) use the RAP protocol
78 * where as most newer devices use the FAP protocol. Both protocols are
79 * compatible with the underlying transport, which could be usb, Unifiying, or
80 * bluetooth. The message lengths are defined by the hid vendor specific report
81 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
82 * the HIDPP_LONG report type (total message length 20 bytes)
83 *
84 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
85 * messages. The Unifying receiver itself responds to RAP messages (device index
86 * is 0xFF for the receiver), and all messages (short or long) with a device
87 * index between 1 and 6 are passed untouched to the corresponding paired
88 * Unifying device.
89 *
90 * The paired device can be RAP or FAP, it will receive the message untouched
91 * from the Unifiying receiver.
92 */
93
94struct fap {
95 u8 feature_index;
96 u8 funcindex_clientid;
Simon Wooda5ce8f52015-11-19 16:42:11 -070097 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040098};
99
100struct rap {
101 u8 sub_id;
102 u8 reg_address;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700103 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400104};
105
106struct hidpp_report {
107 u8 report_id;
108 u8 device_index;
109 union {
110 struct fap fap;
111 struct rap rap;
112 u8 rawbytes[sizeof(struct fap)];
113 };
114} __packed;
115
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000116struct hidpp_battery {
117 u8 feature_index;
118 struct power_supply_desc desc;
119 struct power_supply *ps;
120 char name[64];
121 int status;
122 int level;
123};
124
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400125struct hidpp_device {
126 struct hid_device *hid_dev;
127 struct mutex send_mutex;
128 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -0500129 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400130 wait_queue_head_t wait;
131 bool answer_available;
132 u8 protocol_major;
133 u8 protocol_minor;
134
135 void *private_data;
136
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400137 struct work_struct work;
138 struct kfifo delayed_work_fifo;
139 atomic_t connected;
140 struct input_dev *delayed_input;
141
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400142 unsigned long quirks;
Benjamin Tissoires206d7c62017-03-27 16:59:25 +0200143 unsigned long capabilities;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400144
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000145 struct hidpp_battery battery;
146};
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400147
Peter Wuf677bb12014-12-16 01:50:14 +0100148/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400149#define HIDPP_ERROR 0x8f
150#define HIDPP_ERROR_SUCCESS 0x00
151#define HIDPP_ERROR_INVALID_SUBID 0x01
152#define HIDPP_ERROR_INVALID_ADRESS 0x02
153#define HIDPP_ERROR_INVALID_VALUE 0x03
154#define HIDPP_ERROR_CONNECT_FAIL 0x04
155#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
156#define HIDPP_ERROR_ALREADY_EXISTS 0x06
157#define HIDPP_ERROR_BUSY 0x07
158#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
159#define HIDPP_ERROR_RESOURCE_ERROR 0x09
160#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
161#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
162#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100163/* HID++ 2.0 error codes */
164#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400165
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400166static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
167
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400168static int __hidpp_send_report(struct hid_device *hdev,
169 struct hidpp_report *hidpp_report)
170{
Simon Wood7bfd2922015-11-19 16:42:12 -0700171 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400172 int fields_count, ret;
173
Simon Wood7bfd2922015-11-19 16:42:12 -0700174 hidpp = hid_get_drvdata(hdev);
175
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400176 switch (hidpp_report->report_id) {
177 case REPORT_ID_HIDPP_SHORT:
178 fields_count = HIDPP_REPORT_SHORT_LENGTH;
179 break;
180 case REPORT_ID_HIDPP_LONG:
181 fields_count = HIDPP_REPORT_LONG_LENGTH;
182 break;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700183 case REPORT_ID_HIDPP_VERY_LONG:
184 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
185 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400186 default:
187 return -ENODEV;
188 }
189
190 /*
191 * set the device_index as the receiver, it will be overwritten by
192 * hid_hw_request if needed
193 */
194 hidpp_report->device_index = 0xff;
195
Simon Wood7bfd2922015-11-19 16:42:12 -0700196 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
197 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
198 } else {
199 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
200 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
201 HID_REQ_SET_REPORT);
202 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400203
204 return ret == fields_count ? 0 : -1;
205}
206
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500207/**
208 * hidpp_send_message_sync() returns 0 in case of success, and something else
209 * in case of a failure.
210 * - If ' something else' is positive, that means that an error has been raised
211 * by the protocol itself.
212 * - If ' something else' is negative, that means that we had a classic error
213 * (-ENOMEM, -EPIPE, etc...)
214 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400215static int hidpp_send_message_sync(struct hidpp_device *hidpp,
216 struct hidpp_report *message,
217 struct hidpp_report *response)
218{
219 int ret;
220
221 mutex_lock(&hidpp->send_mutex);
222
223 hidpp->send_receive_buf = response;
224 hidpp->answer_available = false;
225
226 /*
227 * So that we can later validate the answer when it arrives
228 * in hidpp_raw_event
229 */
230 *response = *message;
231
232 ret = __hidpp_send_report(hidpp->hid_dev, message);
233
234 if (ret) {
235 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
236 memset(response, 0, sizeof(struct hidpp_report));
237 goto exit;
238 }
239
240 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
241 5*HZ)) {
242 dbg_hid("%s:timeout waiting for response\n", __func__);
243 memset(response, 0, sizeof(struct hidpp_report));
244 ret = -ETIMEDOUT;
245 }
246
247 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100248 response->rap.sub_id == HIDPP_ERROR) {
249 ret = response->rap.params[1];
250 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
251 goto exit;
252 }
253
Simon Wooda5ce8f52015-11-19 16:42:11 -0700254 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
255 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
256 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400257 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100258 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400259 goto exit;
260 }
261
262exit:
263 mutex_unlock(&hidpp->send_mutex);
264 return ret;
265
266}
267
268static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
269 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
270 struct hidpp_report *response)
271{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300272 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400273 int ret;
274
275 if (param_count > sizeof(message->fap.params))
276 return -EINVAL;
277
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300278 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
279 if (!message)
280 return -ENOMEM;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700281
282 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
283 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
284 else
285 message->report_id = REPORT_ID_HIDPP_LONG;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400286 message->fap.feature_index = feat_index;
287 message->fap.funcindex_clientid = funcindex_clientid;
288 memcpy(&message->fap.params, params, param_count);
289
290 ret = hidpp_send_message_sync(hidpp, message, response);
291 kfree(message);
292 return ret;
293}
294
Benjamin Tissoires33797822014-09-30 13:18:30 -0400295static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
296 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
297 struct hidpp_report *response)
298{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300299 struct hidpp_report *message;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700300 int ret, max_count;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400301
Simon Wooda5ce8f52015-11-19 16:42:11 -0700302 switch (report_id) {
303 case REPORT_ID_HIDPP_SHORT:
304 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
305 break;
306 case REPORT_ID_HIDPP_LONG:
307 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
308 break;
309 case REPORT_ID_HIDPP_VERY_LONG:
310 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
311 break;
312 default:
Benjamin Tissoires33797822014-09-30 13:18:30 -0400313 return -EINVAL;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700314 }
Benjamin Tissoires33797822014-09-30 13:18:30 -0400315
Simon Wooda5ce8f52015-11-19 16:42:11 -0700316 if (param_count > max_count)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400317 return -EINVAL;
318
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300319 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
320 if (!message)
321 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400322 message->report_id = report_id;
323 message->rap.sub_id = sub_id;
324 message->rap.reg_address = reg_address;
325 memcpy(&message->rap.params, params, param_count);
326
327 ret = hidpp_send_message_sync(hidpp_dev, message, response);
328 kfree(message);
329 return ret;
330}
331
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400332static void delayed_work_cb(struct work_struct *work)
333{
334 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
335 work);
336 hidpp_connect_event(hidpp);
337}
338
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400339static inline bool hidpp_match_answer(struct hidpp_report *question,
340 struct hidpp_report *answer)
341{
342 return (answer->fap.feature_index == question->fap.feature_index) &&
343 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
344}
345
346static inline bool hidpp_match_error(struct hidpp_report *question,
347 struct hidpp_report *answer)
348{
Peter Wuf677bb12014-12-16 01:50:14 +0100349 return ((answer->rap.sub_id == HIDPP_ERROR) ||
350 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400351 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
352 (answer->fap.params[0] == question->fap.funcindex_clientid);
353}
354
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400355static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
356{
357 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
358 (report->rap.sub_id == 0x41);
359}
360
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500361/**
362 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
363 */
364static void hidpp_prefix_name(char **name, int name_length)
365{
366#define PREFIX_LENGTH 9 /* "Logitech " */
367
368 int new_length;
369 char *new_name;
370
371 if (name_length > PREFIX_LENGTH &&
372 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
373 /* The prefix has is already in the name */
374 return;
375
376 new_length = PREFIX_LENGTH + name_length;
377 new_name = kzalloc(new_length, GFP_KERNEL);
378 if (!new_name)
379 return;
380
381 snprintf(new_name, new_length, "Logitech %s", *name);
382
383 kfree(*name);
384
385 *name = new_name;
386}
387
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400388/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400389/* HIDP++ 1.0 commands */
390/* -------------------------------------------------------------------------- */
391
392#define HIDPP_SET_REGISTER 0x80
393#define HIDPP_GET_REGISTER 0x81
394#define HIDPP_SET_LONG_REGISTER 0x82
395#define HIDPP_GET_LONG_REGISTER 0x83
396
397#define HIDPP_REG_PAIRING_INFORMATION 0xB5
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200398#define HIDPP_EXTENDED_PAIRING 0x30
399#define HIDPP_DEVICE_NAME 0x40
Benjamin Tissoires33797822014-09-30 13:18:30 -0400400
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200401static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400402{
403 struct hidpp_report response;
404 int ret;
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200405 u8 params[1] = { HIDPP_DEVICE_NAME };
Benjamin Tissoires33797822014-09-30 13:18:30 -0400406 char *name;
407 int len;
408
409 ret = hidpp_send_rap_command_sync(hidpp_dev,
410 REPORT_ID_HIDPP_SHORT,
411 HIDPP_GET_LONG_REGISTER,
412 HIDPP_REG_PAIRING_INFORMATION,
413 params, 1, &response);
414 if (ret)
415 return NULL;
416
417 len = response.rap.params[1];
418
Peter Wu3a034a72014-12-11 13:51:19 +0100419 if (2 + len > sizeof(response.rap.params))
420 return NULL;
421
Benjamin Tissoires33797822014-09-30 13:18:30 -0400422 name = kzalloc(len + 1, GFP_KERNEL);
423 if (!name)
424 return NULL;
425
426 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500427
428 /* include the terminating '\0' */
429 hidpp_prefix_name(&name, len + 1);
430
Benjamin Tissoires33797822014-09-30 13:18:30 -0400431 return name;
432}
433
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200434static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
435{
436 struct hidpp_report response;
437 int ret;
438 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
439
440 ret = hidpp_send_rap_command_sync(hidpp,
441 REPORT_ID_HIDPP_SHORT,
442 HIDPP_GET_LONG_REGISTER,
443 HIDPP_REG_PAIRING_INFORMATION,
444 params, 1, &response);
445 if (ret)
446 return ret;
447
448 /*
449 * We don't care about LE or BE, we will output it as a string
450 * with %4phD, so we need to keep the order.
451 */
452 *serial = *((u32 *)&response.rap.params[1]);
453 return 0;
454}
455
456static int hidpp_unifying_init(struct hidpp_device *hidpp)
457{
458 struct hid_device *hdev = hidpp->hid_dev;
459 const char *name;
460 u32 serial;
461 int ret;
462
463 ret = hidpp_unifying_get_serial(hidpp, &serial);
464 if (ret)
465 return ret;
466
467 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
468 hdev->product, &serial);
469 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
470
471 name = hidpp_unifying_get_name(hidpp);
472 if (!name)
473 return -EIO;
474
475 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
476 dbg_hid("HID++ Unifying: Got name: %s\n", name);
477
478 kfree(name);
479 return 0;
480}
481
Benjamin Tissoires33797822014-09-30 13:18:30 -0400482/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400483/* 0x0000: Root */
484/* -------------------------------------------------------------------------- */
485
486#define HIDPP_PAGE_ROOT 0x0000
487#define HIDPP_PAGE_ROOT_IDX 0x00
488
489#define CMD_ROOT_GET_FEATURE 0x01
490#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
491
492static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
493 u8 *feature_index, u8 *feature_type)
494{
495 struct hidpp_report response;
496 int ret;
497 u8 params[2] = { feature >> 8, feature & 0x00FF };
498
499 ret = hidpp_send_fap_command_sync(hidpp,
500 HIDPP_PAGE_ROOT_IDX,
501 CMD_ROOT_GET_FEATURE,
502 params, 2, &response);
503 if (ret)
504 return ret;
505
506 *feature_index = response.fap.params[0];
507 *feature_type = response.fap.params[1];
508
509 return ret;
510}
511
512static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
513{
514 struct hidpp_report response;
515 int ret;
516
517 ret = hidpp_send_fap_command_sync(hidpp,
518 HIDPP_PAGE_ROOT_IDX,
519 CMD_ROOT_GET_PROTOCOL_VERSION,
520 NULL, 0, &response);
521
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500522 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400523 hidpp->protocol_major = 1;
524 hidpp->protocol_minor = 0;
525 return 0;
526 }
527
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500528 /* the device might not be connected */
529 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
530 return -EIO;
531
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500532 if (ret > 0) {
533 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
534 __func__, ret);
535 return -EPROTO;
536 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400537 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500538 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400539
540 hidpp->protocol_major = response.fap.params[0];
541 hidpp->protocol_minor = response.fap.params[1];
542
543 return ret;
544}
545
546static bool hidpp_is_connected(struct hidpp_device *hidpp)
547{
548 int ret;
549
550 ret = hidpp_root_get_protocol_version(hidpp);
551 if (!ret)
552 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
553 hidpp->protocol_major, hidpp->protocol_minor);
554 return ret == 0;
555}
556
557/* -------------------------------------------------------------------------- */
558/* 0x0005: GetDeviceNameType */
559/* -------------------------------------------------------------------------- */
560
561#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
562
563#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
564#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
565#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
566
567static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
568 u8 feature_index, u8 *nameLength)
569{
570 struct hidpp_report response;
571 int ret;
572
573 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
574 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
575
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500576 if (ret > 0) {
577 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
578 __func__, ret);
579 return -EPROTO;
580 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400581 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500582 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400583
584 *nameLength = response.fap.params[0];
585
586 return ret;
587}
588
589static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
590 u8 feature_index, u8 char_index, char *device_name, int len_buf)
591{
592 struct hidpp_report response;
593 int ret, i;
594 int count;
595
596 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
597 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
598 &response);
599
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500600 if (ret > 0) {
601 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
602 __func__, ret);
603 return -EPROTO;
604 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400605 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500606 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400607
Simon Wooda5ce8f52015-11-19 16:42:11 -0700608 switch (response.report_id) {
609 case REPORT_ID_HIDPP_VERY_LONG:
610 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
611 break;
612 case REPORT_ID_HIDPP_LONG:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400613 count = HIDPP_REPORT_LONG_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700614 break;
615 case REPORT_ID_HIDPP_SHORT:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400616 count = HIDPP_REPORT_SHORT_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700617 break;
618 default:
619 return -EPROTO;
620 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400621
622 if (len_buf < count)
623 count = len_buf;
624
625 for (i = 0; i < count; i++)
626 device_name[i] = response.fap.params[i];
627
628 return count;
629}
630
Peter Wu02cc0972014-12-11 13:51:17 +0100631static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400632{
633 u8 feature_type;
634 u8 feature_index;
635 u8 __name_length;
636 char *name;
637 unsigned index = 0;
638 int ret;
639
640 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
641 &feature_index, &feature_type);
642 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100643 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400644
645 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
646 &__name_length);
647 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100648 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400649
650 name = kzalloc(__name_length + 1, GFP_KERNEL);
651 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100652 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400653
Peter Wu1430ee72014-12-11 13:51:18 +0100654 while (index < __name_length) {
655 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400656 feature_index, index, name + index,
657 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +0100658 if (ret <= 0) {
659 kfree(name);
660 return NULL;
661 }
662 index += ret;
663 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400664
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500665 /* include the terminating '\0' */
666 hidpp_prefix_name(&name, __name_length + 1);
667
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400668 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400669}
670
671/* -------------------------------------------------------------------------- */
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000672/* 0x1000: Battery level status */
673/* -------------------------------------------------------------------------- */
674
675#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
676
677#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
678#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
679
680#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
681
682static int hidpp20_batterylevel_map_status_level(u8 data[3], int *level,
683 int *next_level)
684{
685 int status;
686 int level_override;
687
688 *level = data[0];
689 *next_level = data[1];
690
691 /* When discharging, we can rely on the device reported level.
692 * For all other states the device reports level 0 (unknown). Make up
693 * a number instead
694 */
695 switch (data[2]) {
696 case 0: /* discharging (in use) */
697 status = POWER_SUPPLY_STATUS_DISCHARGING;
698 level_override = 0;
699 break;
700 case 1: /* recharging */
701 status = POWER_SUPPLY_STATUS_CHARGING;
702 level_override = 80;
703 break;
704 case 2: /* charge in final stage */
705 status = POWER_SUPPLY_STATUS_CHARGING;
706 level_override = 90;
707 break;
708 case 3: /* charge complete */
709 status = POWER_SUPPLY_STATUS_FULL;
710 level_override = 100;
711 break;
712 case 4: /* recharging below optimal speed */
713 status = POWER_SUPPLY_STATUS_CHARGING;
714 level_override = 50;
715 break;
716 /* 5 = invalid battery type
717 6 = thermal error
718 7 = other charging error */
719 default:
720 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
721 level_override = 0;
722 break;
723 }
724
725 if (level_override != 0 && *level == 0)
726 *level = level_override;
727
728 return status;
729}
730
731static int hidpp20_batterylevel_get_battery_level(struct hidpp_device *hidpp,
732 u8 feature_index,
733 int *status,
734 int *level,
735 int *next_level)
736{
737 struct hidpp_report response;
738 int ret;
739 u8 *params = (u8 *)response.fap.params;
740
741 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
742 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
743 NULL, 0, &response);
744 if (ret > 0) {
745 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
746 __func__, ret);
747 return -EPROTO;
748 }
749 if (ret)
750 return ret;
751
752 *status = hidpp20_batterylevel_map_status_level(params, level,
753 next_level);
754
755 return 0;
756}
757
758static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
759{
760 u8 feature_type;
761 int ret;
762 int status, level, next_level;
763
764 if (hidpp->battery.feature_index == 0) {
765 ret = hidpp_root_get_feature(hidpp,
766 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
767 &hidpp->battery.feature_index,
768 &feature_type);
769 if (ret)
770 return ret;
771 }
772
773 ret = hidpp20_batterylevel_get_battery_level(hidpp,
774 hidpp->battery.feature_index,
775 &status, &level, &next_level);
776 if (ret)
777 return ret;
778
779 hidpp->battery.status = status;
780 hidpp->battery.level = level;
781
782 return 0;
783}
784
785static int hidpp20_battery_event(struct hidpp_device *hidpp,
786 u8 *data, int size)
787{
788 struct hidpp_report *report = (struct hidpp_report *)data;
789 int status, level, next_level;
790 bool changed;
791
792 if (report->fap.feature_index != hidpp->battery.feature_index ||
793 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
794 return 0;
795
796 status = hidpp20_batterylevel_map_status_level(report->fap.params,
797 &level, &next_level);
798
799 changed = level != hidpp->battery.level ||
800 status != hidpp->battery.status;
801
802 if (changed) {
803 hidpp->battery.level = level;
804 hidpp->battery.status = status;
805 if (hidpp->battery.ps)
806 power_supply_changed(hidpp->battery.ps);
807 }
808
809 return 0;
810}
811
812static enum power_supply_property hidpp_battery_props[] = {
813 POWER_SUPPLY_PROP_STATUS,
814 POWER_SUPPLY_PROP_CAPACITY,
Bastien Nocera3861e6c2017-03-27 16:59:22 +0200815 POWER_SUPPLY_PROP_SCOPE,
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000816};
817
818static int hidpp_battery_get_property(struct power_supply *psy,
819 enum power_supply_property psp,
820 union power_supply_propval *val)
821{
822 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
823 int ret = 0;
824
825 switch(psp) {
826 case POWER_SUPPLY_PROP_STATUS:
827 val->intval = hidpp->battery.status;
828 break;
829 case POWER_SUPPLY_PROP_CAPACITY:
830 val->intval = hidpp->battery.level;
831 break;
Bastien Nocera3861e6c2017-03-27 16:59:22 +0200832 case POWER_SUPPLY_PROP_SCOPE:
833 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
834 break;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000835 default:
836 ret = -EINVAL;
837 break;
838 }
839
840 return ret;
841}
842
843static int hidpp20_initialize_battery(struct hidpp_device *hidpp)
844{
845 static atomic_t battery_no = ATOMIC_INIT(0);
846 struct power_supply_config cfg = { .drv_data = hidpp };
847 struct power_supply_desc *desc = &hidpp->battery.desc;
848 struct hidpp_battery *battery;
849 unsigned long n;
850 int ret;
851
852 ret = hidpp20_query_battery_info(hidpp);
853 if (ret)
854 return ret;
855
856 battery = &hidpp->battery;
857
858 n = atomic_inc_return(&battery_no) - 1;
859 desc->properties = hidpp_battery_props;
860 desc->num_properties = ARRAY_SIZE(hidpp_battery_props);
861 desc->get_property = hidpp_battery_get_property;
862 sprintf(battery->name, "hidpp_battery_%ld", n);
863 desc->name = battery->name;
864 desc->type = POWER_SUPPLY_TYPE_BATTERY;
865 desc->use_for_apm = 0;
866
867 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
868 &battery->desc,
869 &cfg);
870 if (IS_ERR(battery->ps))
871 return PTR_ERR(battery->ps);
872
873 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
874
875 return 0;
876}
877
878static int hidpp_initialize_battery(struct hidpp_device *hidpp)
879{
880 int ret;
881
Benjamin Tissoires680de742017-03-27 16:59:23 +0200882 if (hidpp->battery.ps)
883 return 0;
884
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000885 if (hidpp->protocol_major >= 2) {
886 ret = hidpp20_initialize_battery(hidpp);
887 if (ret == 0)
Benjamin Tissoires206d7c62017-03-27 16:59:25 +0200888 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000889 }
890
891 return ret;
892}
893
894/* -------------------------------------------------------------------------- */
Benjamin Tissoires90cdd982015-09-03 09:08:30 -0400895/* 0x6010: Touchpad FW items */
896/* -------------------------------------------------------------------------- */
897
898#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
899
900#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
901
902struct hidpp_touchpad_fw_items {
903 uint8_t presence;
904 uint8_t desired_state;
905 uint8_t state;
906 uint8_t persistent;
907};
908
909/**
910 * send a set state command to the device by reading the current items->state
911 * field. items is then filled with the current state.
912 */
913static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
914 u8 feature_index,
915 struct hidpp_touchpad_fw_items *items)
916{
917 struct hidpp_report response;
918 int ret;
919 u8 *params = (u8 *)response.fap.params;
920
921 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
922 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
923
924 if (ret > 0) {
925 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
926 __func__, ret);
927 return -EPROTO;
928 }
929 if (ret)
930 return ret;
931
932 items->presence = params[0];
933 items->desired_state = params[1];
934 items->state = params[2];
935 items->persistent = params[3];
936
937 return 0;
938}
939
940/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400941/* 0x6100: TouchPadRawXY */
942/* -------------------------------------------------------------------------- */
943
944#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
945
946#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -0400947#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
948
949#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400950
951#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
952#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
953
954struct hidpp_touchpad_raw_info {
955 u16 x_size;
956 u16 y_size;
957 u8 z_range;
958 u8 area_range;
959 u8 timestamp_unit;
960 u8 maxcontacts;
961 u8 origin;
962 u16 res;
963};
964
965struct hidpp_touchpad_raw_xy_finger {
966 u8 contact_type;
967 u8 contact_status;
968 u16 x;
969 u16 y;
970 u8 z;
971 u8 area;
972 u8 finger_id;
973};
974
975struct hidpp_touchpad_raw_xy {
976 u16 timestamp;
977 struct hidpp_touchpad_raw_xy_finger fingers[2];
978 u8 spurious_flag;
979 u8 end_of_frame;
980 u8 finger_count;
981 u8 button;
982};
983
984static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
985 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
986{
987 struct hidpp_report response;
988 int ret;
989 u8 *params = (u8 *)response.fap.params;
990
991 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
992 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
993
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500994 if (ret > 0) {
995 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
996 __func__, ret);
997 return -EPROTO;
998 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400999 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001000 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001001
1002 raw_info->x_size = get_unaligned_be16(&params[0]);
1003 raw_info->y_size = get_unaligned_be16(&params[2]);
1004 raw_info->z_range = params[4];
1005 raw_info->area_range = params[5];
1006 raw_info->maxcontacts = params[7];
1007 raw_info->origin = params[8];
1008 /* res is given in unit per inch */
1009 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1010
1011 return ret;
1012}
1013
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001014static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1015 u8 feature_index, bool send_raw_reports,
1016 bool sensor_enhanced_settings)
1017{
1018 struct hidpp_report response;
1019
1020 /*
1021 * Params:
1022 * bit 0 - enable raw
1023 * bit 1 - 16bit Z, no area
1024 * bit 2 - enhanced sensitivity
1025 * bit 3 - width, height (4 bits each) instead of area
1026 * bit 4 - send raw + gestures (degrades smoothness)
1027 * remaining bits - reserved
1028 */
1029 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1030
1031 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1032 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1033}
1034
1035static void hidpp_touchpad_touch_event(u8 *data,
1036 struct hidpp_touchpad_raw_xy_finger *finger)
1037{
1038 u8 x_m = data[0] << 2;
1039 u8 y_m = data[2] << 2;
1040
1041 finger->x = x_m << 6 | data[1];
1042 finger->y = y_m << 6 | data[3];
1043
1044 finger->contact_type = data[0] >> 6;
1045 finger->contact_status = data[2] >> 6;
1046
1047 finger->z = data[4];
1048 finger->area = data[5];
1049 finger->finger_id = data[6] >> 4;
1050}
1051
1052static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1053 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1054{
1055 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1056 raw_xy->end_of_frame = data[8] & 0x01;
1057 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1058 raw_xy->finger_count = data[15] & 0x0f;
1059 raw_xy->button = (data[8] >> 2) & 0x01;
1060
1061 if (raw_xy->finger_count) {
1062 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1063 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1064 }
1065}
1066
Edwin Veldsff21a632016-01-11 00:25:15 +01001067/* -------------------------------------------------------------------------- */
1068/* 0x8123: Force feedback support */
1069/* -------------------------------------------------------------------------- */
1070
1071#define HIDPP_FF_GET_INFO 0x01
1072#define HIDPP_FF_RESET_ALL 0x11
1073#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1074#define HIDPP_FF_SET_EFFECT_STATE 0x31
1075#define HIDPP_FF_DESTROY_EFFECT 0x41
1076#define HIDPP_FF_GET_APERTURE 0x51
1077#define HIDPP_FF_SET_APERTURE 0x61
1078#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1079#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1080
1081#define HIDPP_FF_EFFECT_STATE_GET 0x00
1082#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1083#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1084#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1085
1086#define HIDPP_FF_EFFECT_CONSTANT 0x00
1087#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1088#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1089#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1090#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1091#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1092#define HIDPP_FF_EFFECT_SPRING 0x06
1093#define HIDPP_FF_EFFECT_DAMPER 0x07
1094#define HIDPP_FF_EFFECT_FRICTION 0x08
1095#define HIDPP_FF_EFFECT_INERTIA 0x09
1096#define HIDPP_FF_EFFECT_RAMP 0x0A
1097
1098#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1099
1100#define HIDPP_FF_EFFECTID_NONE -1
1101#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1102
1103#define HIDPP_FF_MAX_PARAMS 20
1104#define HIDPP_FF_RESERVED_SLOTS 1
1105
1106struct hidpp_ff_private_data {
1107 struct hidpp_device *hidpp;
1108 u8 feature_index;
1109 u8 version;
1110 u16 gain;
1111 s16 range;
1112 u8 slot_autocenter;
1113 u8 num_effects;
1114 int *effect_ids;
1115 struct workqueue_struct *wq;
1116 atomic_t workqueue_size;
1117};
1118
1119struct hidpp_ff_work_data {
1120 struct work_struct work;
1121 struct hidpp_ff_private_data *data;
1122 int effect_id;
1123 u8 command;
1124 u8 params[HIDPP_FF_MAX_PARAMS];
1125 u8 size;
1126};
1127
1128static const signed short hiddpp_ff_effects[] = {
1129 FF_CONSTANT,
1130 FF_PERIODIC,
1131 FF_SINE,
1132 FF_SQUARE,
1133 FF_SAW_UP,
1134 FF_SAW_DOWN,
1135 FF_TRIANGLE,
1136 FF_SPRING,
1137 FF_DAMPER,
1138 FF_AUTOCENTER,
1139 FF_GAIN,
1140 -1
1141};
1142
1143static const signed short hiddpp_ff_effects_v2[] = {
1144 FF_RAMP,
1145 FF_FRICTION,
1146 FF_INERTIA,
1147 -1
1148};
1149
1150static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1151 HIDPP_FF_EFFECT_SPRING,
1152 HIDPP_FF_EFFECT_FRICTION,
1153 HIDPP_FF_EFFECT_DAMPER,
1154 HIDPP_FF_EFFECT_INERTIA
1155};
1156
1157static const char *HIDPP_FF_CONDITION_NAMES[] = {
1158 "spring",
1159 "friction",
1160 "damper",
1161 "inertia"
1162};
1163
1164
1165static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1166{
1167 int i;
1168
1169 for (i = 0; i < data->num_effects; i++)
1170 if (data->effect_ids[i] == effect_id)
1171 return i+1;
1172
1173 return 0;
1174}
1175
1176static void hidpp_ff_work_handler(struct work_struct *w)
1177{
1178 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1179 struct hidpp_ff_private_data *data = wd->data;
1180 struct hidpp_report response;
1181 u8 slot;
1182 int ret;
1183
1184 /* add slot number if needed */
1185 switch (wd->effect_id) {
1186 case HIDPP_FF_EFFECTID_AUTOCENTER:
1187 wd->params[0] = data->slot_autocenter;
1188 break;
1189 case HIDPP_FF_EFFECTID_NONE:
1190 /* leave slot as zero */
1191 break;
1192 default:
1193 /* find current slot for effect */
1194 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1195 break;
1196 }
1197
1198 /* send command and wait for reply */
1199 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1200 wd->command, wd->params, wd->size, &response);
1201
1202 if (ret) {
1203 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1204 goto out;
1205 }
1206
1207 /* parse return data */
1208 switch (wd->command) {
1209 case HIDPP_FF_DOWNLOAD_EFFECT:
1210 slot = response.fap.params[0];
1211 if (slot > 0 && slot <= data->num_effects) {
1212 if (wd->effect_id >= 0)
1213 /* regular effect uploaded */
1214 data->effect_ids[slot-1] = wd->effect_id;
1215 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1216 /* autocenter spring uploaded */
1217 data->slot_autocenter = slot;
1218 }
1219 break;
1220 case HIDPP_FF_DESTROY_EFFECT:
1221 if (wd->effect_id >= 0)
1222 /* regular effect destroyed */
1223 data->effect_ids[wd->params[0]-1] = -1;
1224 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1225 /* autocenter spring destoyed */
1226 data->slot_autocenter = 0;
1227 break;
1228 case HIDPP_FF_SET_GLOBAL_GAINS:
1229 data->gain = (wd->params[0] << 8) + wd->params[1];
1230 break;
1231 case HIDPP_FF_SET_APERTURE:
1232 data->range = (wd->params[0] << 8) + wd->params[1];
1233 break;
1234 default:
1235 /* no action needed */
1236 break;
1237 }
1238
1239out:
1240 atomic_dec(&data->workqueue_size);
1241 kfree(wd);
1242}
1243
1244static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1245{
1246 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1247 int s;
1248
1249 if (!wd)
1250 return -ENOMEM;
1251
1252 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1253
1254 wd->data = data;
1255 wd->effect_id = effect_id;
1256 wd->command = command;
1257 wd->size = size;
1258 memcpy(wd->params, params, size);
1259
1260 atomic_inc(&data->workqueue_size);
1261 queue_work(data->wq, &wd->work);
1262
1263 /* warn about excessive queue size */
1264 s = atomic_read(&data->workqueue_size);
1265 if (s >= 20 && s % 20 == 0)
1266 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1267
1268 return 0;
1269}
1270
1271static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1272{
1273 struct hidpp_ff_private_data *data = dev->ff->private;
1274 u8 params[20];
1275 u8 size;
1276 int force;
1277
1278 /* set common parameters */
1279 params[2] = effect->replay.length >> 8;
1280 params[3] = effect->replay.length & 255;
1281 params[4] = effect->replay.delay >> 8;
1282 params[5] = effect->replay.delay & 255;
1283
1284 switch (effect->type) {
1285 case FF_CONSTANT:
1286 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1287 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1288 params[6] = force >> 8;
1289 params[7] = force & 255;
1290 params[8] = effect->u.constant.envelope.attack_level >> 7;
1291 params[9] = effect->u.constant.envelope.attack_length >> 8;
1292 params[10] = effect->u.constant.envelope.attack_length & 255;
1293 params[11] = effect->u.constant.envelope.fade_level >> 7;
1294 params[12] = effect->u.constant.envelope.fade_length >> 8;
1295 params[13] = effect->u.constant.envelope.fade_length & 255;
1296 size = 14;
1297 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1298 effect->u.constant.level,
1299 effect->direction, force);
1300 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1301 effect->u.constant.envelope.attack_level,
1302 effect->u.constant.envelope.attack_length,
1303 effect->u.constant.envelope.fade_level,
1304 effect->u.constant.envelope.fade_length);
1305 break;
1306 case FF_PERIODIC:
1307 {
1308 switch (effect->u.periodic.waveform) {
1309 case FF_SINE:
1310 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1311 break;
1312 case FF_SQUARE:
1313 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1314 break;
1315 case FF_SAW_UP:
1316 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1317 break;
1318 case FF_SAW_DOWN:
1319 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1320 break;
1321 case FF_TRIANGLE:
1322 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1323 break;
1324 default:
1325 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1326 return -EINVAL;
1327 }
1328 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1329 params[6] = effect->u.periodic.magnitude >> 8;
1330 params[7] = effect->u.periodic.magnitude & 255;
1331 params[8] = effect->u.periodic.offset >> 8;
1332 params[9] = effect->u.periodic.offset & 255;
1333 params[10] = effect->u.periodic.period >> 8;
1334 params[11] = effect->u.periodic.period & 255;
1335 params[12] = effect->u.periodic.phase >> 8;
1336 params[13] = effect->u.periodic.phase & 255;
1337 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1338 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1339 params[16] = effect->u.periodic.envelope.attack_length & 255;
1340 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1341 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1342 params[19] = effect->u.periodic.envelope.fade_length & 255;
1343 size = 20;
1344 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1345 effect->u.periodic.magnitude, effect->direction,
1346 effect->u.periodic.offset,
1347 effect->u.periodic.period,
1348 effect->u.periodic.phase);
1349 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1350 effect->u.periodic.envelope.attack_level,
1351 effect->u.periodic.envelope.attack_length,
1352 effect->u.periodic.envelope.fade_level,
1353 effect->u.periodic.envelope.fade_length);
1354 break;
1355 }
1356 case FF_RAMP:
1357 params[1] = HIDPP_FF_EFFECT_RAMP;
1358 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1359 params[6] = force >> 8;
1360 params[7] = force & 255;
1361 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1362 params[8] = force >> 8;
1363 params[9] = force & 255;
1364 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1365 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1366 params[12] = effect->u.ramp.envelope.attack_length & 255;
1367 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1368 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1369 params[15] = effect->u.ramp.envelope.fade_length & 255;
1370 size = 16;
1371 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1372 effect->u.ramp.start_level,
1373 effect->u.ramp.end_level,
1374 effect->direction, force);
1375 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1376 effect->u.ramp.envelope.attack_level,
1377 effect->u.ramp.envelope.attack_length,
1378 effect->u.ramp.envelope.fade_level,
1379 effect->u.ramp.envelope.fade_length);
1380 break;
1381 case FF_FRICTION:
1382 case FF_INERTIA:
1383 case FF_SPRING:
1384 case FF_DAMPER:
1385 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1386 params[6] = effect->u.condition[0].left_saturation >> 9;
1387 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1388 params[8] = effect->u.condition[0].left_coeff >> 8;
1389 params[9] = effect->u.condition[0].left_coeff & 255;
1390 params[10] = effect->u.condition[0].deadband >> 9;
1391 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1392 params[12] = effect->u.condition[0].center >> 8;
1393 params[13] = effect->u.condition[0].center & 255;
1394 params[14] = effect->u.condition[0].right_coeff >> 8;
1395 params[15] = effect->u.condition[0].right_coeff & 255;
1396 params[16] = effect->u.condition[0].right_saturation >> 9;
1397 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1398 size = 18;
1399 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1400 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1401 effect->u.condition[0].left_coeff,
1402 effect->u.condition[0].left_saturation,
1403 effect->u.condition[0].right_coeff,
1404 effect->u.condition[0].right_saturation);
1405 dbg_hid(" deadband=%d, center=%d\n",
1406 effect->u.condition[0].deadband,
1407 effect->u.condition[0].center);
1408 break;
1409 default:
1410 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1411 return -EINVAL;
1412 }
1413
1414 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1415}
1416
1417static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1418{
1419 struct hidpp_ff_private_data *data = dev->ff->private;
1420 u8 params[2];
1421
1422 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1423
1424 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1425
1426 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1427}
1428
1429static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1430{
1431 struct hidpp_ff_private_data *data = dev->ff->private;
1432 u8 slot = 0;
1433
1434 dbg_hid("Erasing effect %d.\n", effect_id);
1435
1436 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1437}
1438
1439static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1440{
1441 struct hidpp_ff_private_data *data = dev->ff->private;
1442 u8 params[18];
1443
1444 dbg_hid("Setting autocenter to %d.\n", magnitude);
1445
1446 /* start a standard spring effect */
1447 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1448 /* zero delay and duration */
1449 params[2] = params[3] = params[4] = params[5] = 0;
1450 /* set coeff to 25% of saturation */
1451 params[8] = params[14] = magnitude >> 11;
1452 params[9] = params[15] = (magnitude >> 3) & 255;
1453 params[6] = params[16] = magnitude >> 9;
1454 params[7] = params[17] = (magnitude >> 1) & 255;
1455 /* zero deadband and center */
1456 params[10] = params[11] = params[12] = params[13] = 0;
1457
1458 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1459}
1460
1461static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1462{
1463 struct hidpp_ff_private_data *data = dev->ff->private;
1464 u8 params[4];
1465
1466 dbg_hid("Setting gain to %d.\n", gain);
1467
1468 params[0] = gain >> 8;
1469 params[1] = gain & 255;
1470 params[2] = 0; /* no boost */
1471 params[3] = 0;
1472
1473 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1474}
1475
1476static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1477{
1478 struct hid_device *hid = to_hid_device(dev);
1479 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1480 struct input_dev *idev = hidinput->input;
1481 struct hidpp_ff_private_data *data = idev->ff->private;
1482
1483 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1484}
1485
1486static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1487{
1488 struct hid_device *hid = to_hid_device(dev);
1489 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1490 struct input_dev *idev = hidinput->input;
1491 struct hidpp_ff_private_data *data = idev->ff->private;
1492 u8 params[2];
1493 int range = simple_strtoul(buf, NULL, 10);
1494
1495 range = clamp(range, 180, 900);
1496
1497 params[0] = range >> 8;
1498 params[1] = range & 0x00FF;
1499
1500 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1501
1502 return count;
1503}
1504
1505static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1506
1507static void hidpp_ff_destroy(struct ff_device *ff)
1508{
1509 struct hidpp_ff_private_data *data = ff->private;
1510
1511 kfree(data->effect_ids);
1512}
1513
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01001514static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
Edwin Veldsff21a632016-01-11 00:25:15 +01001515{
1516 struct hid_device *hid = hidpp->hid_dev;
1517 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1518 struct input_dev *dev = hidinput->input;
1519 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1520 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1521 struct ff_device *ff;
1522 struct hidpp_report response;
1523 struct hidpp_ff_private_data *data;
1524 int error, j, num_slots;
1525 u8 version;
1526
1527 if (!dev) {
1528 hid_err(hid, "Struct input_dev not set!\n");
1529 return -EINVAL;
1530 }
1531
1532 /* Get firmware release */
1533 version = bcdDevice & 255;
1534
1535 /* Set supported force feedback capabilities */
1536 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1537 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1538 if (version > 1)
1539 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1540 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1541
1542 /* Read number of slots available in device */
1543 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1544 HIDPP_FF_GET_INFO, NULL, 0, &response);
1545 if (error) {
1546 if (error < 0)
1547 return error;
1548 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1549 __func__, error);
1550 return -EPROTO;
1551 }
1552
1553 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1554
1555 error = input_ff_create(dev, num_slots);
1556
1557 if (error) {
1558 hid_err(dev, "Failed to create FF device!\n");
1559 return error;
1560 }
1561
1562 data = kzalloc(sizeof(*data), GFP_KERNEL);
1563 if (!data)
1564 return -ENOMEM;
1565 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1566 if (!data->effect_ids) {
1567 kfree(data);
1568 return -ENOMEM;
1569 }
1570 data->hidpp = hidpp;
1571 data->feature_index = feature_index;
1572 data->version = version;
1573 data->slot_autocenter = 0;
1574 data->num_effects = num_slots;
1575 for (j = 0; j < num_slots; j++)
1576 data->effect_ids[j] = -1;
1577
1578 ff = dev->ff;
1579 ff->private = data;
1580
1581 ff->upload = hidpp_ff_upload_effect;
1582 ff->erase = hidpp_ff_erase_effect;
1583 ff->playback = hidpp_ff_playback;
1584 ff->set_gain = hidpp_ff_set_gain;
1585 ff->set_autocenter = hidpp_ff_set_autocenter;
1586 ff->destroy = hidpp_ff_destroy;
1587
1588
1589 /* reset all forces */
1590 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1591 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1592
1593 /* Read current Range */
1594 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1595 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1596 if (error)
1597 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1598 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1599
1600 /* Create sysfs interface */
1601 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1602 if (error)
1603 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1604
1605 /* Read the current gain values */
1606 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1607 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1608 if (error)
1609 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1610 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1611 /* ignore boost value at response.fap.params[2] */
1612
1613 /* init the hardware command queue */
1614 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1615 atomic_set(&data->workqueue_size, 0);
1616
1617 /* initialize with zero autocenter to get wheel in usable state */
1618 hidpp_ff_set_autocenter(dev, 0);
1619
1620 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1621
1622 return 0;
1623}
1624
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01001625static int hidpp_ff_deinit(struct hid_device *hid)
Edwin Veldsff21a632016-01-11 00:25:15 +01001626{
1627 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1628 struct input_dev *dev = hidinput->input;
1629 struct hidpp_ff_private_data *data;
1630
1631 if (!dev) {
1632 hid_err(hid, "Struct input_dev not found!\n");
1633 return -EINVAL;
1634 }
1635
1636 hid_info(hid, "Unloading HID++ force feedback.\n");
1637 data = dev->ff->private;
1638 if (!data) {
1639 hid_err(hid, "Private data not found!\n");
1640 return -EINVAL;
1641 }
1642
1643 destroy_workqueue(data->wq);
1644 device_remove_file(&hid->dev, &dev_attr_range);
1645
1646 return 0;
1647}
1648
1649
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001650/* ************************************************************************** */
1651/* */
1652/* Device Support */
1653/* */
1654/* ************************************************************************** */
1655
1656/* -------------------------------------------------------------------------- */
1657/* Touchpad HID++ devices */
1658/* -------------------------------------------------------------------------- */
1659
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001660#define WTP_MANUAL_RESOLUTION 39
1661
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001662struct wtp_data {
1663 struct input_dev *input;
1664 u16 x_size, y_size;
1665 u8 finger_count;
1666 u8 mt_feature_index;
1667 u8 button_feature_index;
1668 u8 maxcontacts;
1669 bool flip_y;
1670 unsigned int resolution;
1671};
1672
1673static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1674 struct hid_field *field, struct hid_usage *usage,
1675 unsigned long **bit, int *max)
1676{
1677 return -1;
1678}
1679
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001680static void wtp_populate_input(struct hidpp_device *hidpp,
1681 struct input_dev *input_dev, bool origin_is_hid_core)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001682{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001683 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001684
1685 __set_bit(EV_ABS, input_dev->evbit);
1686 __set_bit(EV_KEY, input_dev->evbit);
1687 __clear_bit(EV_REL, input_dev->evbit);
1688 __clear_bit(EV_LED, input_dev->evbit);
1689
1690 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
1691 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
1692 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
1693 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
1694
1695 /* Max pressure is not given by the devices, pick one */
1696 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
1697
1698 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
1699
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001700 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
1701 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
1702 else
1703 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001704
1705 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
1706 INPUT_MT_DROP_UNUSED);
1707
1708 wd->input = input_dev;
1709}
1710
1711static void wtp_touch_event(struct wtp_data *wd,
1712 struct hidpp_touchpad_raw_xy_finger *touch_report)
1713{
1714 int slot;
1715
1716 if (!touch_report->finger_id || touch_report->contact_type)
1717 /* no actual data */
1718 return;
1719
1720 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
1721
1722 input_mt_slot(wd->input, slot);
1723 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
1724 touch_report->contact_status);
1725 if (touch_report->contact_status) {
1726 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
1727 touch_report->x);
1728 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
1729 wd->flip_y ? wd->y_size - touch_report->y :
1730 touch_report->y);
1731 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
1732 touch_report->area);
1733 }
1734}
1735
1736static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
1737 struct hidpp_touchpad_raw_xy *raw)
1738{
1739 struct wtp_data *wd = hidpp->private_data;
1740 int i;
1741
1742 for (i = 0; i < 2; i++)
1743 wtp_touch_event(wd, &(raw->fingers[i]));
1744
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001745 if (raw->end_of_frame &&
1746 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001747 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
1748
1749 if (raw->end_of_frame || raw->finger_count <= 2) {
1750 input_mt_sync_frame(wd->input);
1751 input_sync(wd->input);
1752 }
1753}
1754
1755static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
1756{
1757 struct wtp_data *wd = hidpp->private_data;
1758 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
1759 (data[7] >> 4) * (data[7] >> 4)) / 2;
1760 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
1761 (data[13] >> 4) * (data[13] >> 4)) / 2;
1762 struct hidpp_touchpad_raw_xy raw = {
1763 .timestamp = data[1],
1764 .fingers = {
1765 {
1766 .contact_type = 0,
1767 .contact_status = !!data[7],
1768 .x = get_unaligned_le16(&data[3]),
1769 .y = get_unaligned_le16(&data[5]),
1770 .z = c1_area,
1771 .area = c1_area,
1772 .finger_id = data[2],
1773 }, {
1774 .contact_type = 0,
1775 .contact_status = !!data[13],
1776 .x = get_unaligned_le16(&data[9]),
1777 .y = get_unaligned_le16(&data[11]),
1778 .z = c2_area,
1779 .area = c2_area,
1780 .finger_id = data[8],
1781 }
1782 },
1783 .finger_count = wd->maxcontacts,
1784 .spurious_flag = 0,
1785 .end_of_frame = (data[0] >> 7) == 0,
1786 .button = data[0] & 0x01,
1787 };
1788
1789 wtp_send_raw_xy_event(hidpp, &raw);
1790
1791 return 1;
1792}
1793
1794static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
1795{
1796 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1797 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001798 struct hidpp_report *report = (struct hidpp_report *)data;
1799 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001800
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001801 if (!wd || !wd->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001802 return 1;
1803
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001804 switch (data[0]) {
1805 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +01001806 if (size < 2) {
1807 hid_err(hdev, "Received HID report of bad size (%d)",
1808 size);
1809 return 1;
1810 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001811 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
1812 input_event(wd->input, EV_KEY, BTN_LEFT,
1813 !!(data[1] & 0x01));
1814 input_event(wd->input, EV_KEY, BTN_RIGHT,
1815 !!(data[1] & 0x02));
1816 input_sync(wd->input);
Peter Wu8abd8202014-12-16 01:50:16 +01001817 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001818 } else {
1819 if (size < 21)
1820 return 1;
1821 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
1822 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001823 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +01001824 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001825 if ((report->fap.feature_index != wd->mt_feature_index) ||
1826 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
1827 return 1;
1828 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
1829
1830 wtp_send_raw_xy_event(hidpp, &raw);
1831 return 0;
1832 }
1833
1834 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001835}
1836
1837static int wtp_get_config(struct hidpp_device *hidpp)
1838{
1839 struct wtp_data *wd = hidpp->private_data;
1840 struct hidpp_touchpad_raw_info raw_info = {0};
1841 u8 feature_type;
1842 int ret;
1843
1844 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
1845 &wd->mt_feature_index, &feature_type);
1846 if (ret)
1847 /* means that the device is not powered up */
1848 return ret;
1849
1850 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
1851 &raw_info);
1852 if (ret)
1853 return ret;
1854
1855 wd->x_size = raw_info.x_size;
1856 wd->y_size = raw_info.y_size;
1857 wd->maxcontacts = raw_info.maxcontacts;
1858 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
1859 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001860 if (!wd->resolution)
1861 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001862
1863 return 0;
1864}
1865
1866static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
1867{
1868 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1869 struct wtp_data *wd;
1870
1871 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
1872 GFP_KERNEL);
1873 if (!wd)
1874 return -ENOMEM;
1875
1876 hidpp->private_data = wd;
1877
1878 return 0;
1879};
1880
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001881static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001882{
1883 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1884 struct wtp_data *wd = hidpp->private_data;
1885 int ret;
1886
1887 if (!connected)
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001888 return 0;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001889
1890 if (!wd->x_size) {
1891 ret = wtp_get_config(hidpp);
1892 if (ret) {
1893 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001894 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001895 }
1896 }
1897
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05001898 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001899 true, true);
1900}
1901
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02001902/* ------------------------------------------------------------------------- */
1903/* Logitech M560 devices */
1904/* ------------------------------------------------------------------------- */
1905
1906/*
1907 * Logitech M560 protocol overview
1908 *
1909 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1910 * the sides buttons are pressed, it sends some keyboard keys events
1911 * instead of buttons ones.
1912 * To complicate things further, the middle button keys sequence
1913 * is different from the odd press and the even press.
1914 *
1915 * forward button -> Super_R
1916 * backward button -> Super_L+'d' (press only)
1917 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1918 * 2nd time: left-click (press only)
1919 * NB: press-only means that when the button is pressed, the
1920 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1921 * together sequentially; instead when the button is released, no event is
1922 * generated !
1923 *
1924 * With the command
1925 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1926 * the mouse reacts differently:
1927 * - it never sends a keyboard key event
1928 * - for the three mouse button it sends:
1929 * middle button press 11<xx>0a 3500af00...
1930 * side 1 button (forward) press 11<xx>0a 3500b000...
1931 * side 2 button (backward) press 11<xx>0a 3500ae00...
1932 * middle/side1/side2 button release 11<xx>0a 35000000...
1933 */
1934
1935static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1936
1937struct m560_private_data {
1938 struct input_dev *input;
1939};
1940
1941/* how buttons are mapped in the report */
1942#define M560_MOUSE_BTN_LEFT 0x01
1943#define M560_MOUSE_BTN_RIGHT 0x02
1944#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1945#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1946
1947#define M560_SUB_ID 0x0a
1948#define M560_BUTTON_MODE_REGISTER 0x35
1949
1950static int m560_send_config_command(struct hid_device *hdev, bool connected)
1951{
1952 struct hidpp_report response;
1953 struct hidpp_device *hidpp_dev;
1954
1955 hidpp_dev = hid_get_drvdata(hdev);
1956
1957 if (!connected)
1958 return -ENODEV;
1959
1960 return hidpp_send_rap_command_sync(
1961 hidpp_dev,
1962 REPORT_ID_HIDPP_SHORT,
1963 M560_SUB_ID,
1964 M560_BUTTON_MODE_REGISTER,
1965 (u8 *)m560_config_parameter,
1966 sizeof(m560_config_parameter),
1967 &response
1968 );
1969}
1970
1971static int m560_allocate(struct hid_device *hdev)
1972{
1973 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1974 struct m560_private_data *d;
1975
1976 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1977 GFP_KERNEL);
1978 if (!d)
1979 return -ENOMEM;
1980
1981 hidpp->private_data = d;
1982
1983 return 0;
1984};
1985
1986static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1987{
1988 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1989 struct m560_private_data *mydata = hidpp->private_data;
1990
1991 /* sanity check */
1992 if (!mydata || !mydata->input) {
1993 hid_err(hdev, "error in parameter\n");
1994 return -EINVAL;
1995 }
1996
1997 if (size < 7) {
1998 hid_err(hdev, "error in report\n");
1999 return 0;
2000 }
2001
2002 if (data[0] == REPORT_ID_HIDPP_LONG &&
2003 data[2] == M560_SUB_ID && data[6] == 0x00) {
2004 /*
2005 * m560 mouse report for middle, forward and backward button
2006 *
2007 * data[0] = 0x11
2008 * data[1] = device-id
2009 * data[2] = 0x0a
2010 * data[5] = 0xaf -> middle
2011 * 0xb0 -> forward
2012 * 0xae -> backward
2013 * 0x00 -> release all
2014 * data[6] = 0x00
2015 */
2016
2017 switch (data[5]) {
2018 case 0xaf:
2019 input_report_key(mydata->input, BTN_MIDDLE, 1);
2020 break;
2021 case 0xb0:
2022 input_report_key(mydata->input, BTN_FORWARD, 1);
2023 break;
2024 case 0xae:
2025 input_report_key(mydata->input, BTN_BACK, 1);
2026 break;
2027 case 0x00:
2028 input_report_key(mydata->input, BTN_BACK, 0);
2029 input_report_key(mydata->input, BTN_FORWARD, 0);
2030 input_report_key(mydata->input, BTN_MIDDLE, 0);
2031 break;
2032 default:
2033 hid_err(hdev, "error in report\n");
2034 return 0;
2035 }
2036 input_sync(mydata->input);
2037
2038 } else if (data[0] == 0x02) {
2039 /*
2040 * Logitech M560 mouse report
2041 *
2042 * data[0] = type (0x02)
2043 * data[1..2] = buttons
2044 * data[3..5] = xy
2045 * data[6] = wheel
2046 */
2047
2048 int v;
2049
2050 input_report_key(mydata->input, BTN_LEFT,
2051 !!(data[1] & M560_MOUSE_BTN_LEFT));
2052 input_report_key(mydata->input, BTN_RIGHT,
2053 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2054
2055 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2056 input_report_rel(mydata->input, REL_HWHEEL, -1);
2057 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2058 input_report_rel(mydata->input, REL_HWHEEL, 1);
2059
2060 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2061 input_report_rel(mydata->input, REL_X, v);
2062
2063 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2064 input_report_rel(mydata->input, REL_Y, v);
2065
2066 v = hid_snto32(data[6], 8);
2067 input_report_rel(mydata->input, REL_WHEEL, v);
2068
2069 input_sync(mydata->input);
2070 }
2071
2072 return 1;
2073}
2074
2075static void m560_populate_input(struct hidpp_device *hidpp,
2076 struct input_dev *input_dev, bool origin_is_hid_core)
2077{
2078 struct m560_private_data *mydata = hidpp->private_data;
2079
2080 mydata->input = input_dev;
2081
2082 __set_bit(EV_KEY, mydata->input->evbit);
2083 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2084 __set_bit(BTN_RIGHT, mydata->input->keybit);
2085 __set_bit(BTN_LEFT, mydata->input->keybit);
2086 __set_bit(BTN_BACK, mydata->input->keybit);
2087 __set_bit(BTN_FORWARD, mydata->input->keybit);
2088
2089 __set_bit(EV_REL, mydata->input->evbit);
2090 __set_bit(REL_X, mydata->input->relbit);
2091 __set_bit(REL_Y, mydata->input->relbit);
2092 __set_bit(REL_WHEEL, mydata->input->relbit);
2093 __set_bit(REL_HWHEEL, mydata->input->relbit);
2094}
2095
2096static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2097 struct hid_field *field, struct hid_usage *usage,
2098 unsigned long **bit, int *max)
2099{
2100 return -1;
2101}
2102
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002103/* ------------------------------------------------------------------------- */
2104/* Logitech K400 devices */
2105/* ------------------------------------------------------------------------- */
2106
2107/*
2108 * The Logitech K400 keyboard has an embedded touchpad which is seen
2109 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2110 * tap-to-click but the setting is not remembered accross reset, annoying some
2111 * users.
2112 *
2113 * We can toggle this feature from the host by using the feature 0x6010:
2114 * Touchpad FW items
2115 */
2116
2117struct k400_private_data {
2118 u8 feature_index;
2119};
2120
2121static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2122{
2123 struct k400_private_data *k400 = hidpp->private_data;
2124 struct hidpp_touchpad_fw_items items = {};
2125 int ret;
2126 u8 feature_type;
2127
2128 if (!k400->feature_index) {
2129 ret = hidpp_root_get_feature(hidpp,
2130 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2131 &k400->feature_index, &feature_type);
2132 if (ret)
2133 /* means that the device is not powered up */
2134 return ret;
2135 }
2136
2137 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2138 if (ret)
2139 return ret;
2140
2141 return 0;
2142}
2143
2144static int k400_allocate(struct hid_device *hdev)
2145{
2146 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2147 struct k400_private_data *k400;
2148
2149 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2150 GFP_KERNEL);
2151 if (!k400)
2152 return -ENOMEM;
2153
2154 hidpp->private_data = k400;
2155
2156 return 0;
2157};
2158
2159static int k400_connect(struct hid_device *hdev, bool connected)
2160{
2161 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2162
2163 if (!connected)
2164 return 0;
2165
2166 if (!disable_tap_to_click)
2167 return 0;
2168
2169 return k400_disable_tap_to_click(hidpp);
2170}
2171
Simon Wood7f4b49f2015-11-19 16:42:13 -07002172/* ------------------------------------------------------------------------- */
2173/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2174/* ------------------------------------------------------------------------- */
2175
2176#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2177
Simon Wood7f4b49f2015-11-19 16:42:13 -07002178static int g920_get_config(struct hidpp_device *hidpp)
2179{
Simon Wood7f4b49f2015-11-19 16:42:13 -07002180 u8 feature_type;
2181 u8 feature_index;
2182 int ret;
2183
Simon Wood7f4b49f2015-11-19 16:42:13 -07002184 /* Find feature and store for later use */
2185 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2186 &feature_index, &feature_type);
2187 if (ret)
2188 return ret;
2189
Edwin Veldsff21a632016-01-11 00:25:15 +01002190 ret = hidpp_ff_init(hidpp, feature_index);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002191 if (ret)
Edwin Veldsff21a632016-01-11 00:25:15 +01002192 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2193 ret);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002194
2195 return 0;
2196}
2197
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002198/* -------------------------------------------------------------------------- */
2199/* Generic HID++ devices */
2200/* -------------------------------------------------------------------------- */
2201
2202static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2203 struct hid_field *field, struct hid_usage *usage,
2204 unsigned long **bit, int *max)
2205{
2206 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2207
2208 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2209 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002210 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2211 field->application != HID_GD_MOUSE)
2212 return m560_input_mapping(hdev, hi, field, usage, bit, max);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002213
2214 return 0;
2215}
2216
Simon Wood0b1804e2015-11-19 16:42:15 -07002217static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2218 struct hid_field *field, struct hid_usage *usage,
2219 unsigned long **bit, int *max)
2220{
2221 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2222
2223 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2224 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2225 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2226 usage->code == ABS_Y || usage->code == ABS_Z ||
2227 usage->code == ABS_RZ)) {
2228 field->application = HID_GD_MULTIAXIS;
2229 }
2230 }
2231
2232 return 0;
2233}
2234
2235
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002236static void hidpp_populate_input(struct hidpp_device *hidpp,
2237 struct input_dev *input, bool origin_is_hid_core)
2238{
2239 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2240 wtp_populate_input(hidpp, input, origin_is_hid_core);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002241 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2242 m560_populate_input(hidpp, input, origin_is_hid_core);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002243}
2244
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002245static int hidpp_input_configured(struct hid_device *hdev,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002246 struct hid_input *hidinput)
2247{
2248 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002249 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002250
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002251 hidpp_populate_input(hidpp, input, true);
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002252
2253 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002254}
2255
2256static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2257 int size)
2258{
2259 struct hidpp_report *question = hidpp->send_receive_buf;
2260 struct hidpp_report *answer = hidpp->send_receive_buf;
2261 struct hidpp_report *report = (struct hidpp_report *)data;
2262
2263 /*
2264 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +01002265 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002266 */
2267 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2268 /*
2269 * Check for a correct hidpp20 answer or the corresponding
2270 * error
2271 */
2272 if (hidpp_match_answer(question, report) ||
2273 hidpp_match_error(question, report)) {
2274 *answer = *report;
2275 hidpp->answer_available = true;
2276 wake_up(&hidpp->wait);
2277 /*
2278 * This was an answer to a command that this driver sent
2279 * We return 1 to hid-core to avoid forwarding the
2280 * command upstream as it has been treated by the driver
2281 */
2282
2283 return 1;
2284 }
2285 }
2286
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002287 if (unlikely(hidpp_report_is_connect_event(report))) {
2288 atomic_set(&hidpp->connected,
2289 !(report->rap.params[0] & (1 << 6)));
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002290 if (schedule_work(&hidpp->work) == 0)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002291 dbg_hid("%s: connect event already queued\n", __func__);
2292 return 1;
2293 }
2294
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002295 return 0;
2296}
2297
2298static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2299 u8 *data, int size)
2300{
2301 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01002302 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002303
Peter Wue529fea2014-12-17 00:23:51 +01002304 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002305 switch (data[0]) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07002306 case REPORT_ID_HIDPP_VERY_LONG:
2307 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2308 hid_err(hdev, "received hid++ report of bad size (%d)",
2309 size);
2310 return 1;
2311 }
2312 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2313 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002314 case REPORT_ID_HIDPP_LONG:
2315 if (size != HIDPP_REPORT_LONG_LENGTH) {
2316 hid_err(hdev, "received hid++ report of bad size (%d)",
2317 size);
2318 return 1;
2319 }
Peter Wue529fea2014-12-17 00:23:51 +01002320 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2321 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002322 case REPORT_ID_HIDPP_SHORT:
2323 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2324 hid_err(hdev, "received hid++ report of bad size (%d)",
2325 size);
2326 return 1;
2327 }
Peter Wue529fea2014-12-17 00:23:51 +01002328 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2329 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002330 }
2331
Peter Wue529fea2014-12-17 00:23:51 +01002332 /* If no report is available for further processing, skip calling
2333 * raw_event of subclasses. */
2334 if (ret != 0)
2335 return ret;
2336
Benjamin Tissoires206d7c62017-03-27 16:59:25 +02002337 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
Peter Hutterer5a2b1902016-06-29 19:28:01 +10002338 ret = hidpp20_battery_event(hidpp, data, size);
2339 if (ret != 0)
2340 return ret;
2341 }
2342
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002343 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2344 return wtp_raw_event(hdev, data, size);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002345 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2346 return m560_raw_event(hdev, data, size);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002347
2348 return 0;
2349}
2350
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002351static void hidpp_overwrite_name(struct hid_device *hdev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002352{
2353 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2354 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002355
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002356 if (hidpp->protocol_major < 2)
Benjamin Tissoiresb4f8ce02017-03-27 16:59:24 +02002357 return;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002358
2359 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002360
Simon Wood7bfd2922015-11-19 16:42:12 -07002361 if (!name) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002362 hid_err(hdev, "unable to retrieve the name of the device");
Simon Wood7bfd2922015-11-19 16:42:12 -07002363 } else {
2364 dbg_hid("HID++: Got name: %s\n", name);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002365 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
Simon Wood7bfd2922015-11-19 16:42:12 -07002366 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002367
2368 kfree(name);
2369}
2370
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002371static int hidpp_input_open(struct input_dev *dev)
2372{
2373 struct hid_device *hid = input_get_drvdata(dev);
2374
2375 return hid_hw_open(hid);
2376}
2377
2378static void hidpp_input_close(struct input_dev *dev)
2379{
2380 struct hid_device *hid = input_get_drvdata(dev);
2381
2382 hid_hw_close(hid);
2383}
2384
2385static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2386{
2387 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002388 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002389
2390 if (!input_dev)
2391 return NULL;
2392
2393 input_set_drvdata(input_dev, hdev);
2394 input_dev->open = hidpp_input_open;
2395 input_dev->close = hidpp_input_close;
2396
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002397 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002398 input_dev->phys = hdev->phys;
2399 input_dev->uniq = hdev->uniq;
2400 input_dev->id.bustype = hdev->bus;
2401 input_dev->id.vendor = hdev->vendor;
2402 input_dev->id.product = hdev->product;
2403 input_dev->id.version = hdev->version;
2404 input_dev->dev.parent = &hdev->dev;
2405
2406 return input_dev;
2407}
2408
2409static void hidpp_connect_event(struct hidpp_device *hidpp)
2410{
2411 struct hid_device *hdev = hidpp->hid_dev;
2412 int ret = 0;
2413 bool connected = atomic_read(&hidpp->connected);
2414 struct input_dev *input;
2415 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002416
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002417 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2418 ret = wtp_connect(hdev, connected);
2419 if (ret)
2420 return;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002421 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2422 ret = m560_send_config_command(hdev, connected);
2423 if (ret)
2424 return;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002425 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2426 ret = k400_connect(hdev, connected);
2427 if (ret)
2428 return;
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002429 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002430
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002431 if (!connected || hidpp->delayed_input)
2432 return;
2433
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002434 /* the device is already connected, we can ask for its name and
2435 * protocol */
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002436 if (!hidpp->protocol_major) {
2437 ret = !hidpp_is_connected(hidpp);
2438 if (ret) {
2439 hid_err(hdev, "Can not get the protocol version.\n");
2440 return;
2441 }
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002442 hid_info(hdev, "HID++ %u.%u device connected.\n",
2443 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002444 }
2445
Peter Hutterer5a2b1902016-06-29 19:28:01 +10002446 hidpp_initialize_battery(hidpp);
2447
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002448 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
2449 /* if HID created the input nodes for us, we can stop now */
2450 return;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002451
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002452 if (!hidpp->name || hidpp->name == hdev->name) {
2453 name = hidpp_get_device_name(hidpp);
2454 if (!name) {
2455 hid_err(hdev,
2456 "unable to retrieve the name of the device");
2457 return;
2458 }
2459
2460 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2461 kfree(name);
2462 if (!devm_name)
2463 return;
2464
2465 hidpp->name = devm_name;
2466 }
2467
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002468 input = hidpp_allocate_input(hdev);
2469 if (!input) {
2470 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2471 return;
2472 }
2473
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002474 hidpp_populate_input(hidpp, input, false);
2475
2476 ret = input_register_device(input);
2477 if (ret)
2478 input_free_device(input);
2479
2480 hidpp->delayed_input = input;
2481}
2482
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002483static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2484{
2485 struct hidpp_device *hidpp;
2486 int ret;
2487 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002488 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002489
2490 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2491 GFP_KERNEL);
2492 if (!hidpp)
2493 return -ENOMEM;
2494
2495 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002496 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002497 hid_set_drvdata(hdev, hidpp);
2498
2499 hidpp->quirks = id->driver_data;
2500
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002501 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
2502 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
2503
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04002504 if (disable_raw_mode) {
2505 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002506 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04002507 }
2508
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002509 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2510 ret = wtp_allocate(hdev, id);
2511 if (ret)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002512 goto allocate_fail;
2513 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2514 ret = m560_allocate(hdev);
2515 if (ret)
2516 goto allocate_fail;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002517 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2518 ret = k400_allocate(hdev);
2519 if (ret)
2520 goto allocate_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002521 }
2522
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002523 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002524 mutex_init(&hidpp->send_mutex);
2525 init_waitqueue_head(&hidpp->wait);
2526
2527 ret = hid_parse(hdev);
2528 if (ret) {
2529 hid_err(hdev, "%s:parse failed\n", __func__);
2530 goto hid_parse_fail;
2531 }
2532
Simon Wood7bfd2922015-11-19 16:42:12 -07002533 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2534 connect_mask &= ~HID_CONNECT_HIDINPUT;
2535
2536 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2537 ret = hid_hw_start(hdev, connect_mask);
2538 if (ret) {
2539 hid_err(hdev, "hw start failed\n");
2540 goto hid_hw_start_fail;
2541 }
2542 ret = hid_hw_open(hdev);
2543 if (ret < 0) {
2544 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
2545 __func__, ret);
2546 hid_hw_stop(hdev);
2547 goto hid_hw_start_fail;
2548 }
2549 }
2550
2551
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002552 /* Allow incoming packets */
2553 hid_device_io_start(hdev);
2554
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002555 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
2556 hidpp_unifying_init(hidpp);
2557
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002558 connected = hidpp_is_connected(hidpp);
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002559 atomic_set(&hidpp->connected, connected);
2560 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002561 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02002562 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002563 hid_err(hdev, "Device not connected");
Simon Wood7bfd2922015-11-19 16:42:12 -07002564 goto hid_hw_open_failed;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002565 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002566
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002567 hid_info(hdev, "HID++ %u.%u device connected.\n",
2568 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002569
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002570 hidpp_overwrite_name(hdev);
2571 }
Benjamin Tissoires33797822014-09-30 13:18:30 -04002572
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002573 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002574 ret = wtp_get_config(hidpp);
2575 if (ret)
Simon Wood7bfd2922015-11-19 16:42:12 -07002576 goto hid_hw_open_failed;
Simon Wood7f4b49f2015-11-19 16:42:13 -07002577 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2578 ret = g920_get_config(hidpp);
2579 if (ret)
2580 goto hid_hw_open_failed;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002581 }
2582
2583 /* Block incoming packets */
2584 hid_device_io_stop(hdev);
2585
Simon Wood7bfd2922015-11-19 16:42:12 -07002586 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2587 ret = hid_hw_start(hdev, connect_mask);
2588 if (ret) {
2589 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
2590 goto hid_hw_start_fail;
2591 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002592 }
2593
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002594 /* Allow incoming packets */
2595 hid_device_io_start(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002596
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002597 hidpp_connect_event(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002598
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002599 return ret;
2600
Simon Wood7bfd2922015-11-19 16:42:12 -07002601hid_hw_open_failed:
2602 hid_device_io_stop(hdev);
2603 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2604 hid_hw_close(hdev);
2605 hid_hw_stop(hdev);
2606 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002607hid_hw_start_fail:
2608hid_parse_fail:
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002609 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002610 mutex_destroy(&hidpp->send_mutex);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002611allocate_fail:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002612 hid_set_drvdata(hdev, NULL);
2613 return ret;
2614}
2615
2616static void hidpp_remove(struct hid_device *hdev)
2617{
2618 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2619
Simon Wood7f4b49f2015-11-19 16:42:13 -07002620 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
Edwin Veldsff21a632016-01-11 00:25:15 +01002621 hidpp_ff_deinit(hdev);
Simon Wood7bfd2922015-11-19 16:42:12 -07002622 hid_hw_close(hdev);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002623 }
Simon Wood7bfd2922015-11-19 16:42:12 -07002624 hid_hw_stop(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002625 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002626 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002627}
2628
2629static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002630 { /* wireless touchpad */
2631 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2632 USB_VENDOR_ID_LOGITECH, 0x4011),
2633 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
2634 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002635 { /* wireless touchpad T650 */
2636 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2637 USB_VENDOR_ID_LOGITECH, 0x4101),
2638 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002639 { /* wireless touchpad T651 */
2640 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
2641 USB_DEVICE_ID_LOGITECH_T651),
2642 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002643 { /* Mouse logitech M560 */
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04002644 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002645 USB_VENDOR_ID_LOGITECH, 0x402d),
2646 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002647 { /* Keyboard logitech K400 */
2648 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2649 USB_VENDOR_ID_LOGITECH, 0x4024),
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002650 .driver_data = HIDPP_QUIRK_CLASS_K400 },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002651
2652 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2653 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
Simon Wood7bfd2922015-11-19 16:42:12 -07002654
2655 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
2656 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002657 {}
2658};
2659
2660MODULE_DEVICE_TABLE(hid, hidpp_devices);
2661
2662static struct hid_driver hidpp_driver = {
2663 .name = "logitech-hidpp-device",
2664 .id_table = hidpp_devices,
2665 .probe = hidpp_probe,
2666 .remove = hidpp_remove,
2667 .raw_event = hidpp_raw_event,
2668 .input_configured = hidpp_input_configured,
2669 .input_mapping = hidpp_input_mapping,
Simon Wood0b1804e2015-11-19 16:42:15 -07002670 .input_mapped = hidpp_input_mapped,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002671};
2672
2673module_hid_driver(hidpp_driver);