blob: bb40e5d3a73e4b5241f96f3194e1ccf11d12dd42 [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 Tissoires696ecef2017-03-27 16:59:37 +020059#define HIDPP_QUIRK_CLASS_K750 BIT(4)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040060
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +020061/* bits 2..20 are reserved for classes */
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100062/* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -040063#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040064#define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
Simon Wood7bfd2922015-11-19 16:42:12 -070065#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
Benjamin Tissoires843c6242017-03-27 16:59:26 +020066#define HIDPP_QUIRK_UNIFYING BIT(25)
Benjamin Tissoires580a7e82015-09-03 09:08:29 -040067
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +100068#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -040069
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020070#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
71#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +020072#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
73#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
Benjamin Tissoires206d7c62017-03-27 16:59:25 +020074
Benjamin Tissoires2f31c522014-09-30 13:18:27 -040075/*
76 * There are two hidpp protocols in use, the first version hidpp10 is known
77 * as register access protocol or RAP, the second version hidpp20 is known as
78 * feature access protocol or FAP
79 *
80 * Most older devices (including the Unifying usb receiver) use the RAP protocol
81 * where as most newer devices use the FAP protocol. Both protocols are
82 * compatible with the underlying transport, which could be usb, Unifiying, or
83 * bluetooth. The message lengths are defined by the hid vendor specific report
84 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
85 * the HIDPP_LONG report type (total message length 20 bytes)
86 *
87 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
88 * messages. The Unifying receiver itself responds to RAP messages (device index
89 * is 0xFF for the receiver), and all messages (short or long) with a device
90 * index between 1 and 6 are passed untouched to the corresponding paired
91 * Unifying device.
92 *
93 * The paired device can be RAP or FAP, it will receive the message untouched
94 * from the Unifiying receiver.
95 */
96
97struct fap {
98 u8 feature_index;
99 u8 funcindex_clientid;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700100 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400101};
102
103struct rap {
104 u8 sub_id;
105 u8 reg_address;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700106 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400107};
108
109struct hidpp_report {
110 u8 report_id;
111 u8 device_index;
112 union {
113 struct fap fap;
114 struct rap rap;
115 u8 rawbytes[sizeof(struct fap)];
116 };
117} __packed;
118
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000119struct hidpp_battery {
120 u8 feature_index;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +0200121 u8 solar_feature_index;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000122 struct power_supply_desc desc;
123 struct power_supply *ps;
124 char name[64];
125 int status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200126 int capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200127 int level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200128 bool online;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000129};
130
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400131struct hidpp_device {
132 struct hid_device *hid_dev;
133 struct mutex send_mutex;
134 void *send_receive_buf;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -0500135 char *name; /* will never be NULL and should not be freed */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400136 wait_queue_head_t wait;
137 bool answer_available;
138 u8 protocol_major;
139 u8 protocol_minor;
140
141 void *private_data;
142
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400143 struct work_struct work;
144 struct kfifo delayed_work_fifo;
145 atomic_t connected;
146 struct input_dev *delayed_input;
147
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400148 unsigned long quirks;
Benjamin Tissoires206d7c62017-03-27 16:59:25 +0200149 unsigned long capabilities;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400150
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000151 struct hidpp_battery battery;
152};
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400153
Peter Wuf677bb12014-12-16 01:50:14 +0100154/* HID++ 1.0 error codes */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400155#define HIDPP_ERROR 0x8f
156#define HIDPP_ERROR_SUCCESS 0x00
157#define HIDPP_ERROR_INVALID_SUBID 0x01
158#define HIDPP_ERROR_INVALID_ADRESS 0x02
159#define HIDPP_ERROR_INVALID_VALUE 0x03
160#define HIDPP_ERROR_CONNECT_FAIL 0x04
161#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
162#define HIDPP_ERROR_ALREADY_EXISTS 0x06
163#define HIDPP_ERROR_BUSY 0x07
164#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
165#define HIDPP_ERROR_RESOURCE_ERROR 0x09
166#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
167#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
168#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
Peter Wuf677bb12014-12-16 01:50:14 +0100169/* HID++ 2.0 error codes */
170#define HIDPP20_ERROR 0xff
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400171
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400172static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
173
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400174static int __hidpp_send_report(struct hid_device *hdev,
175 struct hidpp_report *hidpp_report)
176{
Simon Wood7bfd2922015-11-19 16:42:12 -0700177 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400178 int fields_count, ret;
179
Simon Wood7bfd2922015-11-19 16:42:12 -0700180 hidpp = hid_get_drvdata(hdev);
181
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400182 switch (hidpp_report->report_id) {
183 case REPORT_ID_HIDPP_SHORT:
184 fields_count = HIDPP_REPORT_SHORT_LENGTH;
185 break;
186 case REPORT_ID_HIDPP_LONG:
187 fields_count = HIDPP_REPORT_LONG_LENGTH;
188 break;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700189 case REPORT_ID_HIDPP_VERY_LONG:
190 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
191 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400192 default:
193 return -ENODEV;
194 }
195
196 /*
197 * set the device_index as the receiver, it will be overwritten by
198 * hid_hw_request if needed
199 */
200 hidpp_report->device_index = 0xff;
201
Simon Wood7bfd2922015-11-19 16:42:12 -0700202 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
203 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
204 } else {
205 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
206 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
207 HID_REQ_SET_REPORT);
208 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400209
210 return ret == fields_count ? 0 : -1;
211}
212
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500213/**
214 * hidpp_send_message_sync() returns 0 in case of success, and something else
215 * in case of a failure.
216 * - If ' something else' is positive, that means that an error has been raised
217 * by the protocol itself.
218 * - If ' something else' is negative, that means that we had a classic error
219 * (-ENOMEM, -EPIPE, etc...)
220 */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400221static int hidpp_send_message_sync(struct hidpp_device *hidpp,
222 struct hidpp_report *message,
223 struct hidpp_report *response)
224{
225 int ret;
226
227 mutex_lock(&hidpp->send_mutex);
228
229 hidpp->send_receive_buf = response;
230 hidpp->answer_available = false;
231
232 /*
233 * So that we can later validate the answer when it arrives
234 * in hidpp_raw_event
235 */
236 *response = *message;
237
238 ret = __hidpp_send_report(hidpp->hid_dev, message);
239
240 if (ret) {
241 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
242 memset(response, 0, sizeof(struct hidpp_report));
243 goto exit;
244 }
245
246 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
247 5*HZ)) {
248 dbg_hid("%s:timeout waiting for response\n", __func__);
249 memset(response, 0, sizeof(struct hidpp_report));
250 ret = -ETIMEDOUT;
251 }
252
253 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
Peter Wuf677bb12014-12-16 01:50:14 +0100254 response->rap.sub_id == HIDPP_ERROR) {
255 ret = response->rap.params[1];
256 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
257 goto exit;
258 }
259
Simon Wooda5ce8f52015-11-19 16:42:11 -0700260 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
261 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
262 response->fap.feature_index == HIDPP20_ERROR) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400263 ret = response->fap.params[1];
Peter Wuf677bb12014-12-16 01:50:14 +0100264 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400265 goto exit;
266 }
267
268exit:
269 mutex_unlock(&hidpp->send_mutex);
270 return ret;
271
272}
273
274static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
275 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
276 struct hidpp_report *response)
277{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300278 struct hidpp_report *message;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400279 int ret;
280
281 if (param_count > sizeof(message->fap.params))
282 return -EINVAL;
283
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300284 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
285 if (!message)
286 return -ENOMEM;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700287
288 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
289 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
290 else
291 message->report_id = REPORT_ID_HIDPP_LONG;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400292 message->fap.feature_index = feat_index;
293 message->fap.funcindex_clientid = funcindex_clientid;
294 memcpy(&message->fap.params, params, param_count);
295
296 ret = hidpp_send_message_sync(hidpp, message, response);
297 kfree(message);
298 return ret;
299}
300
Benjamin Tissoires33797822014-09-30 13:18:30 -0400301static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
302 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
303 struct hidpp_report *response)
304{
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300305 struct hidpp_report *message;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700306 int ret, max_count;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400307
Simon Wooda5ce8f52015-11-19 16:42:11 -0700308 switch (report_id) {
309 case REPORT_ID_HIDPP_SHORT:
310 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
311 break;
312 case REPORT_ID_HIDPP_LONG:
313 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
314 break;
315 case REPORT_ID_HIDPP_VERY_LONG:
316 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
317 break;
318 default:
Benjamin Tissoires33797822014-09-30 13:18:30 -0400319 return -EINVAL;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700320 }
Benjamin Tissoires33797822014-09-30 13:18:30 -0400321
Simon Wooda5ce8f52015-11-19 16:42:11 -0700322 if (param_count > max_count)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400323 return -EINVAL;
324
Dan Carpenter3e7830c2014-10-31 12:14:39 +0300325 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
326 if (!message)
327 return -ENOMEM;
Benjamin Tissoires33797822014-09-30 13:18:30 -0400328 message->report_id = report_id;
329 message->rap.sub_id = sub_id;
330 message->rap.reg_address = reg_address;
331 memcpy(&message->rap.params, params, param_count);
332
333 ret = hidpp_send_message_sync(hidpp_dev, message, response);
334 kfree(message);
335 return ret;
336}
337
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400338static void delayed_work_cb(struct work_struct *work)
339{
340 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
341 work);
342 hidpp_connect_event(hidpp);
343}
344
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400345static inline bool hidpp_match_answer(struct hidpp_report *question,
346 struct hidpp_report *answer)
347{
348 return (answer->fap.feature_index == question->fap.feature_index) &&
349 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
350}
351
352static inline bool hidpp_match_error(struct hidpp_report *question,
353 struct hidpp_report *answer)
354{
Peter Wuf677bb12014-12-16 01:50:14 +0100355 return ((answer->rap.sub_id == HIDPP_ERROR) ||
356 (answer->fap.feature_index == HIDPP20_ERROR)) &&
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400357 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
358 (answer->fap.params[0] == question->fap.funcindex_clientid);
359}
360
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -0400361static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
362{
363 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
364 (report->rap.sub_id == 0x41);
365}
366
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500367/**
368 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
369 */
370static void hidpp_prefix_name(char **name, int name_length)
371{
372#define PREFIX_LENGTH 9 /* "Logitech " */
373
374 int new_length;
375 char *new_name;
376
377 if (name_length > PREFIX_LENGTH &&
378 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
379 /* The prefix has is already in the name */
380 return;
381
382 new_length = PREFIX_LENGTH + name_length;
383 new_name = kzalloc(new_length, GFP_KERNEL);
384 if (!new_name)
385 return;
386
387 snprintf(new_name, new_length, "Logitech %s", *name);
388
389 kfree(*name);
390
391 *name = new_name;
392}
393
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400394/* -------------------------------------------------------------------------- */
Benjamin Tissoires33797822014-09-30 13:18:30 -0400395/* HIDP++ 1.0 commands */
396/* -------------------------------------------------------------------------- */
397
398#define HIDPP_SET_REGISTER 0x80
399#define HIDPP_GET_REGISTER 0x81
400#define HIDPP_SET_LONG_REGISTER 0x82
401#define HIDPP_GET_LONG_REGISTER 0x83
402
403#define HIDPP_REG_PAIRING_INFORMATION 0xB5
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200404#define HIDPP_EXTENDED_PAIRING 0x30
405#define HIDPP_DEVICE_NAME 0x40
Benjamin Tissoires33797822014-09-30 13:18:30 -0400406
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200407static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
Benjamin Tissoires33797822014-09-30 13:18:30 -0400408{
409 struct hidpp_report response;
410 int ret;
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200411 u8 params[1] = { HIDPP_DEVICE_NAME };
Benjamin Tissoires33797822014-09-30 13:18:30 -0400412 char *name;
413 int len;
414
415 ret = hidpp_send_rap_command_sync(hidpp_dev,
416 REPORT_ID_HIDPP_SHORT,
417 HIDPP_GET_LONG_REGISTER,
418 HIDPP_REG_PAIRING_INFORMATION,
419 params, 1, &response);
420 if (ret)
421 return NULL;
422
423 len = response.rap.params[1];
424
Peter Wu3a034a72014-12-11 13:51:19 +0100425 if (2 + len > sizeof(response.rap.params))
426 return NULL;
427
Benjamin Tissoires33797822014-09-30 13:18:30 -0400428 name = kzalloc(len + 1, GFP_KERNEL);
429 if (!name)
430 return NULL;
431
432 memcpy(name, &response.rap.params[2], len);
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500433
434 /* include the terminating '\0' */
435 hidpp_prefix_name(&name, len + 1);
436
Benjamin Tissoires33797822014-09-30 13:18:30 -0400437 return name;
438}
439
Benjamin Tissoires843c6242017-03-27 16:59:26 +0200440static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
441{
442 struct hidpp_report response;
443 int ret;
444 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
445
446 ret = hidpp_send_rap_command_sync(hidpp,
447 REPORT_ID_HIDPP_SHORT,
448 HIDPP_GET_LONG_REGISTER,
449 HIDPP_REG_PAIRING_INFORMATION,
450 params, 1, &response);
451 if (ret)
452 return ret;
453
454 /*
455 * We don't care about LE or BE, we will output it as a string
456 * with %4phD, so we need to keep the order.
457 */
458 *serial = *((u32 *)&response.rap.params[1]);
459 return 0;
460}
461
462static int hidpp_unifying_init(struct hidpp_device *hidpp)
463{
464 struct hid_device *hdev = hidpp->hid_dev;
465 const char *name;
466 u32 serial;
467 int ret;
468
469 ret = hidpp_unifying_get_serial(hidpp, &serial);
470 if (ret)
471 return ret;
472
473 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
474 hdev->product, &serial);
475 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
476
477 name = hidpp_unifying_get_name(hidpp);
478 if (!name)
479 return -EIO;
480
481 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
482 dbg_hid("HID++ Unifying: Got name: %s\n", name);
483
484 kfree(name);
485 return 0;
486}
487
Benjamin Tissoires33797822014-09-30 13:18:30 -0400488/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400489/* 0x0000: Root */
490/* -------------------------------------------------------------------------- */
491
492#define HIDPP_PAGE_ROOT 0x0000
493#define HIDPP_PAGE_ROOT_IDX 0x00
494
495#define CMD_ROOT_GET_FEATURE 0x01
496#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
497
498static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
499 u8 *feature_index, u8 *feature_type)
500{
501 struct hidpp_report response;
502 int ret;
503 u8 params[2] = { feature >> 8, feature & 0x00FF };
504
505 ret = hidpp_send_fap_command_sync(hidpp,
506 HIDPP_PAGE_ROOT_IDX,
507 CMD_ROOT_GET_FEATURE,
508 params, 2, &response);
509 if (ret)
510 return ret;
511
Benjamin Tissoiresa9525b82017-03-27 16:59:32 +0200512 if (response.fap.params[0] == 0)
513 return -ENOENT;
514
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400515 *feature_index = response.fap.params[0];
516 *feature_type = response.fap.params[1];
517
518 return ret;
519}
520
521static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
522{
523 struct hidpp_report response;
524 int ret;
525
526 ret = hidpp_send_fap_command_sync(hidpp,
527 HIDPP_PAGE_ROOT_IDX,
528 CMD_ROOT_GET_PROTOCOL_VERSION,
529 NULL, 0, &response);
530
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500531 if (ret == HIDPP_ERROR_INVALID_SUBID) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400532 hidpp->protocol_major = 1;
533 hidpp->protocol_minor = 0;
534 return 0;
535 }
536
Benjamin Tissoires552f12e2014-11-03 16:09:59 -0500537 /* the device might not be connected */
538 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
539 return -EIO;
540
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500541 if (ret > 0) {
542 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
543 __func__, ret);
544 return -EPROTO;
545 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400546 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500547 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400548
549 hidpp->protocol_major = response.fap.params[0];
550 hidpp->protocol_minor = response.fap.params[1];
551
552 return ret;
553}
554
555static bool hidpp_is_connected(struct hidpp_device *hidpp)
556{
557 int ret;
558
559 ret = hidpp_root_get_protocol_version(hidpp);
560 if (!ret)
561 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
562 hidpp->protocol_major, hidpp->protocol_minor);
563 return ret == 0;
564}
565
566/* -------------------------------------------------------------------------- */
567/* 0x0005: GetDeviceNameType */
568/* -------------------------------------------------------------------------- */
569
570#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
571
572#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
573#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
574#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
575
576static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
577 u8 feature_index, u8 *nameLength)
578{
579 struct hidpp_report response;
580 int ret;
581
582 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
583 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
584
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500585 if (ret > 0) {
586 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
587 __func__, ret);
588 return -EPROTO;
589 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400590 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500591 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400592
593 *nameLength = response.fap.params[0];
594
595 return ret;
596}
597
598static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
599 u8 feature_index, u8 char_index, char *device_name, int len_buf)
600{
601 struct hidpp_report response;
602 int ret, i;
603 int count;
604
605 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
606 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
607 &response);
608
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500609 if (ret > 0) {
610 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
611 __func__, ret);
612 return -EPROTO;
613 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400614 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -0500615 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400616
Simon Wooda5ce8f52015-11-19 16:42:11 -0700617 switch (response.report_id) {
618 case REPORT_ID_HIDPP_VERY_LONG:
619 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
620 break;
621 case REPORT_ID_HIDPP_LONG:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400622 count = HIDPP_REPORT_LONG_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700623 break;
624 case REPORT_ID_HIDPP_SHORT:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400625 count = HIDPP_REPORT_SHORT_LENGTH - 4;
Simon Wooda5ce8f52015-11-19 16:42:11 -0700626 break;
627 default:
628 return -EPROTO;
629 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400630
631 if (len_buf < count)
632 count = len_buf;
633
634 for (i = 0; i < count; i++)
635 device_name[i] = response.fap.params[i];
636
637 return count;
638}
639
Peter Wu02cc0972014-12-11 13:51:17 +0100640static char *hidpp_get_device_name(struct hidpp_device *hidpp)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400641{
642 u8 feature_type;
643 u8 feature_index;
644 u8 __name_length;
645 char *name;
646 unsigned index = 0;
647 int ret;
648
649 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
650 &feature_index, &feature_type);
651 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100652 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400653
654 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
655 &__name_length);
656 if (ret)
Peter Wu02cc0972014-12-11 13:51:17 +0100657 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400658
659 name = kzalloc(__name_length + 1, GFP_KERNEL);
660 if (!name)
Peter Wu02cc0972014-12-11 13:51:17 +0100661 return NULL;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400662
Peter Wu1430ee72014-12-11 13:51:18 +0100663 while (index < __name_length) {
664 ret = hidpp_devicenametype_get_device_name(hidpp,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400665 feature_index, index, name + index,
666 __name_length - index);
Peter Wu1430ee72014-12-11 13:51:18 +0100667 if (ret <= 0) {
668 kfree(name);
669 return NULL;
670 }
671 index += ret;
672 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400673
Benjamin Tissoiresa0e625f2014-12-11 17:39:59 -0500674 /* include the terminating '\0' */
675 hidpp_prefix_name(&name, __name_length + 1);
676
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400677 return name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -0400678}
679
680/* -------------------------------------------------------------------------- */
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000681/* 0x1000: Battery level status */
682/* -------------------------------------------------------------------------- */
683
684#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
685
686#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
687#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
688
689#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
690
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200691#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
692#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
693#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
694
695static int hidpp_map_battery_level(int capacity)
696{
697 if (capacity < 11)
698 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
699 else if (capacity < 31)
700 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
701 else if (capacity < 81)
702 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
703 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
704}
705
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200706static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200707 int *next_capacity,
708 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000709{
710 int status;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000711
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200712 *capacity = data[0];
713 *next_capacity = data[1];
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200714 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000715
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200716 /* When discharging, we can rely on the device reported capacity.
717 * For all other states the device reports 0 (unknown).
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000718 */
719 switch (data[2]) {
720 case 0: /* discharging (in use) */
721 status = POWER_SUPPLY_STATUS_DISCHARGING;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200722 *level = hidpp_map_battery_level(*capacity);
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000723 break;
724 case 1: /* recharging */
725 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000726 break;
727 case 2: /* charge in final stage */
728 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000729 break;
730 case 3: /* charge complete */
731 status = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200732 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200733 *capacity = 100;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000734 break;
735 case 4: /* recharging below optimal speed */
736 status = POWER_SUPPLY_STATUS_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000737 break;
738 /* 5 = invalid battery type
739 6 = thermal error
740 7 = other charging error */
741 default:
742 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000743 break;
744 }
745
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000746 return status;
747}
748
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200749static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
750 u8 feature_index,
751 int *status,
752 int *capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200753 int *next_capacity,
754 int *level)
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000755{
756 struct hidpp_report response;
757 int ret;
758 u8 *params = (u8 *)response.fap.params;
759
760 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
761 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
762 NULL, 0, &response);
763 if (ret > 0) {
764 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
765 __func__, ret);
766 return -EPROTO;
767 }
768 if (ret)
769 return ret;
770
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200771 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200772 next_capacity,
773 level);
774
775 return 0;
776}
777
778static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
779 u8 feature_index)
780{
781 struct hidpp_report response;
782 int ret;
783 u8 *params = (u8 *)response.fap.params;
784 unsigned int level_count, flags;
785
786 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
787 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
788 NULL, 0, &response);
789 if (ret > 0) {
790 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
791 __func__, ret);
792 return -EPROTO;
793 }
794 if (ret)
795 return ret;
796
797 level_count = params[0];
798 flags = params[1];
799
800 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
801 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
802 else
803 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000804
805 return 0;
806}
807
808static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
809{
810 u8 feature_type;
811 int ret;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200812 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000813
Benjamin Tissoires696ecef2017-03-27 16:59:37 +0200814 if (hidpp->battery.feature_index == 0xff) {
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000815 ret = hidpp_root_get_feature(hidpp,
816 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
817 &hidpp->battery.feature_index,
818 &feature_type);
819 if (ret)
820 return ret;
821 }
822
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200823 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
824 hidpp->battery.feature_index,
825 &status, &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200826 &next_capacity, &level);
827 if (ret)
828 return ret;
829
830 ret = hidpp20_batterylevel_get_battery_info(hidpp,
831 hidpp->battery.feature_index);
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000832 if (ret)
833 return ret;
834
835 hidpp->battery.status = status;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200836 hidpp->battery.capacity = capacity;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200837 hidpp->battery.level = level;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200838 /* the capacity is only available when discharging or full */
839 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
840 status == POWER_SUPPLY_STATUS_FULL;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000841
842 return 0;
843}
844
845static int hidpp20_battery_event(struct hidpp_device *hidpp,
846 u8 *data, int size)
847{
848 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200849 int status, capacity, next_capacity, level;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000850 bool changed;
851
852 if (report->fap.feature_index != hidpp->battery.feature_index ||
853 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
854 return 0;
855
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200856 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
857 &capacity,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200858 &next_capacity,
859 &level);
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000860
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200861 /* the capacity is only available when discharging or full */
862 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
863 status == POWER_SUPPLY_STATUS_FULL;
864
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200865 changed = capacity != hidpp->battery.capacity ||
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200866 level != hidpp->battery.level ||
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000867 status != hidpp->battery.status;
868
869 if (changed) {
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200870 hidpp->battery.level = level;
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200871 hidpp->battery.capacity = capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000872 hidpp->battery.status = status;
873 if (hidpp->battery.ps)
874 power_supply_changed(hidpp->battery.ps);
875 }
876
877 return 0;
878}
879
880static enum power_supply_property hidpp_battery_props[] = {
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200881 POWER_SUPPLY_PROP_ONLINE,
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000882 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera3861e6c2017-03-27 16:59:22 +0200883 POWER_SUPPLY_PROP_SCOPE,
Benjamin Tissoires32043d02017-03-27 16:59:30 +0200884 POWER_SUPPLY_PROP_MODEL_NAME,
885 POWER_SUPPLY_PROP_MANUFACTURER,
886 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200887 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
888 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000889};
890
891static int hidpp_battery_get_property(struct power_supply *psy,
892 enum power_supply_property psp,
893 union power_supply_propval *val)
894{
895 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
896 int ret = 0;
897
898 switch(psp) {
899 case POWER_SUPPLY_PROP_STATUS:
900 val->intval = hidpp->battery.status;
901 break;
902 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires14f437a2017-03-27 16:59:35 +0200903 val->intval = hidpp->battery.capacity;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000904 break;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +0200905 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
906 val->intval = hidpp->battery.level;
907 break;
Bastien Nocera3861e6c2017-03-27 16:59:22 +0200908 case POWER_SUPPLY_PROP_SCOPE:
909 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
910 break;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +0200911 case POWER_SUPPLY_PROP_ONLINE:
912 val->intval = hidpp->battery.online;
913 break;
Benjamin Tissoires32043d02017-03-27 16:59:30 +0200914 case POWER_SUPPLY_PROP_MODEL_NAME:
915 if (!strncmp(hidpp->name, "Logitech ", 9))
916 val->strval = hidpp->name + 9;
917 else
918 val->strval = hidpp->name;
919 break;
920 case POWER_SUPPLY_PROP_MANUFACTURER:
921 val->strval = "Logitech";
922 break;
923 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
924 val->strval = hidpp->hid_dev->uniq;
925 break;
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000926 default:
927 ret = -EINVAL;
928 break;
929 }
930
931 return ret;
932}
933
Peter Hutterer5a2b1902016-06-29 19:28:01 +1000934/* -------------------------------------------------------------------------- */
Benjamin Tissoires696ecef2017-03-27 16:59:37 +0200935/* 0x4301: Solar Keyboard */
936/* -------------------------------------------------------------------------- */
937
938#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
939
940#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
941
942#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
943#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
944#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
945
946static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
947{
948 struct hidpp_report response;
949 u8 params[2] = { 1, 1 };
950 u8 feature_type;
951 int ret;
952
953 if (hidpp->battery.feature_index == 0xff) {
954 ret = hidpp_root_get_feature(hidpp,
955 HIDPP_PAGE_SOLAR_KEYBOARD,
956 &hidpp->battery.solar_feature_index,
957 &feature_type);
958 if (ret)
959 return ret;
960 }
961
962 ret = hidpp_send_fap_command_sync(hidpp,
963 hidpp->battery.solar_feature_index,
964 CMD_SOLAR_SET_LIGHT_MEASURE,
965 params, 2, &response);
966 if (ret > 0) {
967 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
968 __func__, ret);
969 return -EPROTO;
970 }
971 if (ret)
972 return ret;
973
974 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
975
976 return 0;
977}
978
979static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
980 u8 *data, int size)
981{
982 struct hidpp_report *report = (struct hidpp_report *)data;
983 int capacity, lux, status;
984 u8 function;
985
986 function = report->fap.funcindex_clientid;
987
988
989 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
990 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
991 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
992 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
993 return 0;
994
995 capacity = report->fap.params[0];
996
997 switch (function) {
998 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
999 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1000 if (lux > 200)
1001 status = POWER_SUPPLY_STATUS_CHARGING;
1002 else
1003 status = POWER_SUPPLY_STATUS_DISCHARGING;
1004 break;
1005 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1006 default:
1007 if (capacity < hidpp->battery.capacity)
1008 status = POWER_SUPPLY_STATUS_DISCHARGING;
1009 else
1010 status = POWER_SUPPLY_STATUS_CHARGING;
1011
1012 }
1013
1014 if (capacity == 100)
1015 status = POWER_SUPPLY_STATUS_FULL;
1016
1017 hidpp->battery.online = true;
1018 if (capacity != hidpp->battery.capacity ||
1019 status != hidpp->battery.status) {
1020 hidpp->battery.capacity = capacity;
1021 hidpp->battery.status = status;
1022 if (hidpp->battery.ps)
1023 power_supply_changed(hidpp->battery.ps);
1024 }
1025
1026 return 0;
1027}
1028
1029/* -------------------------------------------------------------------------- */
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04001030/* 0x6010: Touchpad FW items */
1031/* -------------------------------------------------------------------------- */
1032
1033#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1034
1035#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1036
1037struct hidpp_touchpad_fw_items {
1038 uint8_t presence;
1039 uint8_t desired_state;
1040 uint8_t state;
1041 uint8_t persistent;
1042};
1043
1044/**
1045 * send a set state command to the device by reading the current items->state
1046 * field. items is then filled with the current state.
1047 */
1048static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1049 u8 feature_index,
1050 struct hidpp_touchpad_fw_items *items)
1051{
1052 struct hidpp_report response;
1053 int ret;
1054 u8 *params = (u8 *)response.fap.params;
1055
1056 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1057 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1058
1059 if (ret > 0) {
1060 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1061 __func__, ret);
1062 return -EPROTO;
1063 }
1064 if (ret)
1065 return ret;
1066
1067 items->presence = params[0];
1068 items->desired_state = params[1];
1069 items->state = params[2];
1070 items->persistent = params[3];
1071
1072 return 0;
1073}
1074
1075/* -------------------------------------------------------------------------- */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001076/* 0x6100: TouchPadRawXY */
1077/* -------------------------------------------------------------------------- */
1078
1079#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1080
1081#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001082#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1083
1084#define EVENT_TOUCHPAD_RAW_XY 0x00
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001085
1086#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1087#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1088
1089struct hidpp_touchpad_raw_info {
1090 u16 x_size;
1091 u16 y_size;
1092 u8 z_range;
1093 u8 area_range;
1094 u8 timestamp_unit;
1095 u8 maxcontacts;
1096 u8 origin;
1097 u16 res;
1098};
1099
1100struct hidpp_touchpad_raw_xy_finger {
1101 u8 contact_type;
1102 u8 contact_status;
1103 u16 x;
1104 u16 y;
1105 u8 z;
1106 u8 area;
1107 u8 finger_id;
1108};
1109
1110struct hidpp_touchpad_raw_xy {
1111 u16 timestamp;
1112 struct hidpp_touchpad_raw_xy_finger fingers[2];
1113 u8 spurious_flag;
1114 u8 end_of_frame;
1115 u8 finger_count;
1116 u8 button;
1117};
1118
1119static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1120 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1121{
1122 struct hidpp_report response;
1123 int ret;
1124 u8 *params = (u8 *)response.fap.params;
1125
1126 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1127 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1128
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001129 if (ret > 0) {
1130 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1131 __func__, ret);
1132 return -EPROTO;
1133 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001134 if (ret)
Benjamin Tissoires8c9952b2014-11-03 16:09:58 -05001135 return ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001136
1137 raw_info->x_size = get_unaligned_be16(&params[0]);
1138 raw_info->y_size = get_unaligned_be16(&params[2]);
1139 raw_info->z_range = params[4];
1140 raw_info->area_range = params[5];
1141 raw_info->maxcontacts = params[7];
1142 raw_info->origin = params[8];
1143 /* res is given in unit per inch */
1144 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1145
1146 return ret;
1147}
1148
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001149static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1150 u8 feature_index, bool send_raw_reports,
1151 bool sensor_enhanced_settings)
1152{
1153 struct hidpp_report response;
1154
1155 /*
1156 * Params:
1157 * bit 0 - enable raw
1158 * bit 1 - 16bit Z, no area
1159 * bit 2 - enhanced sensitivity
1160 * bit 3 - width, height (4 bits each) instead of area
1161 * bit 4 - send raw + gestures (degrades smoothness)
1162 * remaining bits - reserved
1163 */
1164 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1165
1166 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1167 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1168}
1169
1170static void hidpp_touchpad_touch_event(u8 *data,
1171 struct hidpp_touchpad_raw_xy_finger *finger)
1172{
1173 u8 x_m = data[0] << 2;
1174 u8 y_m = data[2] << 2;
1175
1176 finger->x = x_m << 6 | data[1];
1177 finger->y = y_m << 6 | data[3];
1178
1179 finger->contact_type = data[0] >> 6;
1180 finger->contact_status = data[2] >> 6;
1181
1182 finger->z = data[4];
1183 finger->area = data[5];
1184 finger->finger_id = data[6] >> 4;
1185}
1186
1187static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1188 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1189{
1190 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1191 raw_xy->end_of_frame = data[8] & 0x01;
1192 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1193 raw_xy->finger_count = data[15] & 0x0f;
1194 raw_xy->button = (data[8] >> 2) & 0x01;
1195
1196 if (raw_xy->finger_count) {
1197 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1198 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1199 }
1200}
1201
Edwin Veldsff21a632016-01-11 00:25:15 +01001202/* -------------------------------------------------------------------------- */
1203/* 0x8123: Force feedback support */
1204/* -------------------------------------------------------------------------- */
1205
1206#define HIDPP_FF_GET_INFO 0x01
1207#define HIDPP_FF_RESET_ALL 0x11
1208#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1209#define HIDPP_FF_SET_EFFECT_STATE 0x31
1210#define HIDPP_FF_DESTROY_EFFECT 0x41
1211#define HIDPP_FF_GET_APERTURE 0x51
1212#define HIDPP_FF_SET_APERTURE 0x61
1213#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1214#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1215
1216#define HIDPP_FF_EFFECT_STATE_GET 0x00
1217#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1218#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1219#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1220
1221#define HIDPP_FF_EFFECT_CONSTANT 0x00
1222#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1223#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1224#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1225#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1226#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1227#define HIDPP_FF_EFFECT_SPRING 0x06
1228#define HIDPP_FF_EFFECT_DAMPER 0x07
1229#define HIDPP_FF_EFFECT_FRICTION 0x08
1230#define HIDPP_FF_EFFECT_INERTIA 0x09
1231#define HIDPP_FF_EFFECT_RAMP 0x0A
1232
1233#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1234
1235#define HIDPP_FF_EFFECTID_NONE -1
1236#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1237
1238#define HIDPP_FF_MAX_PARAMS 20
1239#define HIDPP_FF_RESERVED_SLOTS 1
1240
1241struct hidpp_ff_private_data {
1242 struct hidpp_device *hidpp;
1243 u8 feature_index;
1244 u8 version;
1245 u16 gain;
1246 s16 range;
1247 u8 slot_autocenter;
1248 u8 num_effects;
1249 int *effect_ids;
1250 struct workqueue_struct *wq;
1251 atomic_t workqueue_size;
1252};
1253
1254struct hidpp_ff_work_data {
1255 struct work_struct work;
1256 struct hidpp_ff_private_data *data;
1257 int effect_id;
1258 u8 command;
1259 u8 params[HIDPP_FF_MAX_PARAMS];
1260 u8 size;
1261};
1262
1263static const signed short hiddpp_ff_effects[] = {
1264 FF_CONSTANT,
1265 FF_PERIODIC,
1266 FF_SINE,
1267 FF_SQUARE,
1268 FF_SAW_UP,
1269 FF_SAW_DOWN,
1270 FF_TRIANGLE,
1271 FF_SPRING,
1272 FF_DAMPER,
1273 FF_AUTOCENTER,
1274 FF_GAIN,
1275 -1
1276};
1277
1278static const signed short hiddpp_ff_effects_v2[] = {
1279 FF_RAMP,
1280 FF_FRICTION,
1281 FF_INERTIA,
1282 -1
1283};
1284
1285static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1286 HIDPP_FF_EFFECT_SPRING,
1287 HIDPP_FF_EFFECT_FRICTION,
1288 HIDPP_FF_EFFECT_DAMPER,
1289 HIDPP_FF_EFFECT_INERTIA
1290};
1291
1292static const char *HIDPP_FF_CONDITION_NAMES[] = {
1293 "spring",
1294 "friction",
1295 "damper",
1296 "inertia"
1297};
1298
1299
1300static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1301{
1302 int i;
1303
1304 for (i = 0; i < data->num_effects; i++)
1305 if (data->effect_ids[i] == effect_id)
1306 return i+1;
1307
1308 return 0;
1309}
1310
1311static void hidpp_ff_work_handler(struct work_struct *w)
1312{
1313 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1314 struct hidpp_ff_private_data *data = wd->data;
1315 struct hidpp_report response;
1316 u8 slot;
1317 int ret;
1318
1319 /* add slot number if needed */
1320 switch (wd->effect_id) {
1321 case HIDPP_FF_EFFECTID_AUTOCENTER:
1322 wd->params[0] = data->slot_autocenter;
1323 break;
1324 case HIDPP_FF_EFFECTID_NONE:
1325 /* leave slot as zero */
1326 break;
1327 default:
1328 /* find current slot for effect */
1329 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1330 break;
1331 }
1332
1333 /* send command and wait for reply */
1334 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1335 wd->command, wd->params, wd->size, &response);
1336
1337 if (ret) {
1338 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1339 goto out;
1340 }
1341
1342 /* parse return data */
1343 switch (wd->command) {
1344 case HIDPP_FF_DOWNLOAD_EFFECT:
1345 slot = response.fap.params[0];
1346 if (slot > 0 && slot <= data->num_effects) {
1347 if (wd->effect_id >= 0)
1348 /* regular effect uploaded */
1349 data->effect_ids[slot-1] = wd->effect_id;
1350 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1351 /* autocenter spring uploaded */
1352 data->slot_autocenter = slot;
1353 }
1354 break;
1355 case HIDPP_FF_DESTROY_EFFECT:
1356 if (wd->effect_id >= 0)
1357 /* regular effect destroyed */
1358 data->effect_ids[wd->params[0]-1] = -1;
1359 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1360 /* autocenter spring destoyed */
1361 data->slot_autocenter = 0;
1362 break;
1363 case HIDPP_FF_SET_GLOBAL_GAINS:
1364 data->gain = (wd->params[0] << 8) + wd->params[1];
1365 break;
1366 case HIDPP_FF_SET_APERTURE:
1367 data->range = (wd->params[0] << 8) + wd->params[1];
1368 break;
1369 default:
1370 /* no action needed */
1371 break;
1372 }
1373
1374out:
1375 atomic_dec(&data->workqueue_size);
1376 kfree(wd);
1377}
1378
1379static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1380{
1381 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1382 int s;
1383
1384 if (!wd)
1385 return -ENOMEM;
1386
1387 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1388
1389 wd->data = data;
1390 wd->effect_id = effect_id;
1391 wd->command = command;
1392 wd->size = size;
1393 memcpy(wd->params, params, size);
1394
1395 atomic_inc(&data->workqueue_size);
1396 queue_work(data->wq, &wd->work);
1397
1398 /* warn about excessive queue size */
1399 s = atomic_read(&data->workqueue_size);
1400 if (s >= 20 && s % 20 == 0)
1401 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1402
1403 return 0;
1404}
1405
1406static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1407{
1408 struct hidpp_ff_private_data *data = dev->ff->private;
1409 u8 params[20];
1410 u8 size;
1411 int force;
1412
1413 /* set common parameters */
1414 params[2] = effect->replay.length >> 8;
1415 params[3] = effect->replay.length & 255;
1416 params[4] = effect->replay.delay >> 8;
1417 params[5] = effect->replay.delay & 255;
1418
1419 switch (effect->type) {
1420 case FF_CONSTANT:
1421 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1422 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1423 params[6] = force >> 8;
1424 params[7] = force & 255;
1425 params[8] = effect->u.constant.envelope.attack_level >> 7;
1426 params[9] = effect->u.constant.envelope.attack_length >> 8;
1427 params[10] = effect->u.constant.envelope.attack_length & 255;
1428 params[11] = effect->u.constant.envelope.fade_level >> 7;
1429 params[12] = effect->u.constant.envelope.fade_length >> 8;
1430 params[13] = effect->u.constant.envelope.fade_length & 255;
1431 size = 14;
1432 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1433 effect->u.constant.level,
1434 effect->direction, force);
1435 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1436 effect->u.constant.envelope.attack_level,
1437 effect->u.constant.envelope.attack_length,
1438 effect->u.constant.envelope.fade_level,
1439 effect->u.constant.envelope.fade_length);
1440 break;
1441 case FF_PERIODIC:
1442 {
1443 switch (effect->u.periodic.waveform) {
1444 case FF_SINE:
1445 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1446 break;
1447 case FF_SQUARE:
1448 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1449 break;
1450 case FF_SAW_UP:
1451 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1452 break;
1453 case FF_SAW_DOWN:
1454 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1455 break;
1456 case FF_TRIANGLE:
1457 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1458 break;
1459 default:
1460 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1461 return -EINVAL;
1462 }
1463 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1464 params[6] = effect->u.periodic.magnitude >> 8;
1465 params[7] = effect->u.periodic.magnitude & 255;
1466 params[8] = effect->u.periodic.offset >> 8;
1467 params[9] = effect->u.periodic.offset & 255;
1468 params[10] = effect->u.periodic.period >> 8;
1469 params[11] = effect->u.periodic.period & 255;
1470 params[12] = effect->u.periodic.phase >> 8;
1471 params[13] = effect->u.periodic.phase & 255;
1472 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1473 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1474 params[16] = effect->u.periodic.envelope.attack_length & 255;
1475 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1476 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1477 params[19] = effect->u.periodic.envelope.fade_length & 255;
1478 size = 20;
1479 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1480 effect->u.periodic.magnitude, effect->direction,
1481 effect->u.periodic.offset,
1482 effect->u.periodic.period,
1483 effect->u.periodic.phase);
1484 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1485 effect->u.periodic.envelope.attack_level,
1486 effect->u.periodic.envelope.attack_length,
1487 effect->u.periodic.envelope.fade_level,
1488 effect->u.periodic.envelope.fade_length);
1489 break;
1490 }
1491 case FF_RAMP:
1492 params[1] = HIDPP_FF_EFFECT_RAMP;
1493 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1494 params[6] = force >> 8;
1495 params[7] = force & 255;
1496 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1497 params[8] = force >> 8;
1498 params[9] = force & 255;
1499 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1500 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1501 params[12] = effect->u.ramp.envelope.attack_length & 255;
1502 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1503 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1504 params[15] = effect->u.ramp.envelope.fade_length & 255;
1505 size = 16;
1506 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1507 effect->u.ramp.start_level,
1508 effect->u.ramp.end_level,
1509 effect->direction, force);
1510 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1511 effect->u.ramp.envelope.attack_level,
1512 effect->u.ramp.envelope.attack_length,
1513 effect->u.ramp.envelope.fade_level,
1514 effect->u.ramp.envelope.fade_length);
1515 break;
1516 case FF_FRICTION:
1517 case FF_INERTIA:
1518 case FF_SPRING:
1519 case FF_DAMPER:
1520 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1521 params[6] = effect->u.condition[0].left_saturation >> 9;
1522 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1523 params[8] = effect->u.condition[0].left_coeff >> 8;
1524 params[9] = effect->u.condition[0].left_coeff & 255;
1525 params[10] = effect->u.condition[0].deadband >> 9;
1526 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1527 params[12] = effect->u.condition[0].center >> 8;
1528 params[13] = effect->u.condition[0].center & 255;
1529 params[14] = effect->u.condition[0].right_coeff >> 8;
1530 params[15] = effect->u.condition[0].right_coeff & 255;
1531 params[16] = effect->u.condition[0].right_saturation >> 9;
1532 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1533 size = 18;
1534 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1535 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1536 effect->u.condition[0].left_coeff,
1537 effect->u.condition[0].left_saturation,
1538 effect->u.condition[0].right_coeff,
1539 effect->u.condition[0].right_saturation);
1540 dbg_hid(" deadband=%d, center=%d\n",
1541 effect->u.condition[0].deadband,
1542 effect->u.condition[0].center);
1543 break;
1544 default:
1545 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1546 return -EINVAL;
1547 }
1548
1549 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1550}
1551
1552static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1553{
1554 struct hidpp_ff_private_data *data = dev->ff->private;
1555 u8 params[2];
1556
1557 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1558
1559 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1560
1561 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1562}
1563
1564static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1565{
1566 struct hidpp_ff_private_data *data = dev->ff->private;
1567 u8 slot = 0;
1568
1569 dbg_hid("Erasing effect %d.\n", effect_id);
1570
1571 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1572}
1573
1574static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1575{
1576 struct hidpp_ff_private_data *data = dev->ff->private;
1577 u8 params[18];
1578
1579 dbg_hid("Setting autocenter to %d.\n", magnitude);
1580
1581 /* start a standard spring effect */
1582 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1583 /* zero delay and duration */
1584 params[2] = params[3] = params[4] = params[5] = 0;
1585 /* set coeff to 25% of saturation */
1586 params[8] = params[14] = magnitude >> 11;
1587 params[9] = params[15] = (magnitude >> 3) & 255;
1588 params[6] = params[16] = magnitude >> 9;
1589 params[7] = params[17] = (magnitude >> 1) & 255;
1590 /* zero deadband and center */
1591 params[10] = params[11] = params[12] = params[13] = 0;
1592
1593 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1594}
1595
1596static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1597{
1598 struct hidpp_ff_private_data *data = dev->ff->private;
1599 u8 params[4];
1600
1601 dbg_hid("Setting gain to %d.\n", gain);
1602
1603 params[0] = gain >> 8;
1604 params[1] = gain & 255;
1605 params[2] = 0; /* no boost */
1606 params[3] = 0;
1607
1608 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1609}
1610
1611static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1612{
1613 struct hid_device *hid = to_hid_device(dev);
1614 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1615 struct input_dev *idev = hidinput->input;
1616 struct hidpp_ff_private_data *data = idev->ff->private;
1617
1618 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1619}
1620
1621static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1622{
1623 struct hid_device *hid = to_hid_device(dev);
1624 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1625 struct input_dev *idev = hidinput->input;
1626 struct hidpp_ff_private_data *data = idev->ff->private;
1627 u8 params[2];
1628 int range = simple_strtoul(buf, NULL, 10);
1629
1630 range = clamp(range, 180, 900);
1631
1632 params[0] = range >> 8;
1633 params[1] = range & 0x00FF;
1634
1635 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1636
1637 return count;
1638}
1639
1640static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1641
1642static void hidpp_ff_destroy(struct ff_device *ff)
1643{
1644 struct hidpp_ff_private_data *data = ff->private;
1645
1646 kfree(data->effect_ids);
1647}
1648
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01001649static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
Edwin Veldsff21a632016-01-11 00:25:15 +01001650{
1651 struct hid_device *hid = hidpp->hid_dev;
1652 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1653 struct input_dev *dev = hidinput->input;
1654 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1655 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1656 struct ff_device *ff;
1657 struct hidpp_report response;
1658 struct hidpp_ff_private_data *data;
1659 int error, j, num_slots;
1660 u8 version;
1661
1662 if (!dev) {
1663 hid_err(hid, "Struct input_dev not set!\n");
1664 return -EINVAL;
1665 }
1666
1667 /* Get firmware release */
1668 version = bcdDevice & 255;
1669
1670 /* Set supported force feedback capabilities */
1671 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1672 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1673 if (version > 1)
1674 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1675 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1676
1677 /* Read number of slots available in device */
1678 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1679 HIDPP_FF_GET_INFO, NULL, 0, &response);
1680 if (error) {
1681 if (error < 0)
1682 return error;
1683 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1684 __func__, error);
1685 return -EPROTO;
1686 }
1687
1688 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1689
1690 error = input_ff_create(dev, num_slots);
1691
1692 if (error) {
1693 hid_err(dev, "Failed to create FF device!\n");
1694 return error;
1695 }
1696
1697 data = kzalloc(sizeof(*data), GFP_KERNEL);
1698 if (!data)
1699 return -ENOMEM;
1700 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1701 if (!data->effect_ids) {
1702 kfree(data);
1703 return -ENOMEM;
1704 }
1705 data->hidpp = hidpp;
1706 data->feature_index = feature_index;
1707 data->version = version;
1708 data->slot_autocenter = 0;
1709 data->num_effects = num_slots;
1710 for (j = 0; j < num_slots; j++)
1711 data->effect_ids[j] = -1;
1712
1713 ff = dev->ff;
1714 ff->private = data;
1715
1716 ff->upload = hidpp_ff_upload_effect;
1717 ff->erase = hidpp_ff_erase_effect;
1718 ff->playback = hidpp_ff_playback;
1719 ff->set_gain = hidpp_ff_set_gain;
1720 ff->set_autocenter = hidpp_ff_set_autocenter;
1721 ff->destroy = hidpp_ff_destroy;
1722
1723
1724 /* reset all forces */
1725 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1726 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1727
1728 /* Read current Range */
1729 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1730 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1731 if (error)
1732 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1733 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1734
1735 /* Create sysfs interface */
1736 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1737 if (error)
1738 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1739
1740 /* Read the current gain values */
1741 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1742 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1743 if (error)
1744 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1745 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1746 /* ignore boost value at response.fap.params[2] */
1747
1748 /* init the hardware command queue */
1749 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1750 atomic_set(&data->workqueue_size, 0);
1751
1752 /* initialize with zero autocenter to get wheel in usable state */
1753 hidpp_ff_set_autocenter(dev, 0);
1754
1755 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1756
1757 return 0;
1758}
1759
Jiri Kosinaaf2e6282016-01-28 14:28:39 +01001760static int hidpp_ff_deinit(struct hid_device *hid)
Edwin Veldsff21a632016-01-11 00:25:15 +01001761{
1762 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1763 struct input_dev *dev = hidinput->input;
1764 struct hidpp_ff_private_data *data;
1765
1766 if (!dev) {
1767 hid_err(hid, "Struct input_dev not found!\n");
1768 return -EINVAL;
1769 }
1770
1771 hid_info(hid, "Unloading HID++ force feedback.\n");
1772 data = dev->ff->private;
1773 if (!data) {
1774 hid_err(hid, "Private data not found!\n");
1775 return -EINVAL;
1776 }
1777
1778 destroy_workqueue(data->wq);
1779 device_remove_file(&hid->dev, &dev_attr_range);
1780
1781 return 0;
1782}
1783
1784
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001785/* ************************************************************************** */
1786/* */
1787/* Device Support */
1788/* */
1789/* ************************************************************************** */
1790
1791/* -------------------------------------------------------------------------- */
1792/* Touchpad HID++ devices */
1793/* -------------------------------------------------------------------------- */
1794
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001795#define WTP_MANUAL_RESOLUTION 39
1796
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001797struct wtp_data {
1798 struct input_dev *input;
1799 u16 x_size, y_size;
1800 u8 finger_count;
1801 u8 mt_feature_index;
1802 u8 button_feature_index;
1803 u8 maxcontacts;
1804 bool flip_y;
1805 unsigned int resolution;
1806};
1807
1808static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1809 struct hid_field *field, struct hid_usage *usage,
1810 unsigned long **bit, int *max)
1811{
1812 return -1;
1813}
1814
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04001815static void wtp_populate_input(struct hidpp_device *hidpp,
1816 struct input_dev *input_dev, bool origin_is_hid_core)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001817{
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001818 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001819
1820 __set_bit(EV_ABS, input_dev->evbit);
1821 __set_bit(EV_KEY, input_dev->evbit);
1822 __clear_bit(EV_REL, input_dev->evbit);
1823 __clear_bit(EV_LED, input_dev->evbit);
1824
1825 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
1826 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
1827 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
1828 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
1829
1830 /* Max pressure is not given by the devices, pick one */
1831 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
1832
1833 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
1834
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001835 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
1836 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
1837 else
1838 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001839
1840 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
1841 INPUT_MT_DROP_UNUSED);
1842
1843 wd->input = input_dev;
1844}
1845
1846static void wtp_touch_event(struct wtp_data *wd,
1847 struct hidpp_touchpad_raw_xy_finger *touch_report)
1848{
1849 int slot;
1850
1851 if (!touch_report->finger_id || touch_report->contact_type)
1852 /* no actual data */
1853 return;
1854
1855 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
1856
1857 input_mt_slot(wd->input, slot);
1858 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
1859 touch_report->contact_status);
1860 if (touch_report->contact_status) {
1861 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
1862 touch_report->x);
1863 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
1864 wd->flip_y ? wd->y_size - touch_report->y :
1865 touch_report->y);
1866 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
1867 touch_report->area);
1868 }
1869}
1870
1871static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
1872 struct hidpp_touchpad_raw_xy *raw)
1873{
1874 struct wtp_data *wd = hidpp->private_data;
1875 int i;
1876
1877 for (i = 0; i < 2; i++)
1878 wtp_touch_event(wd, &(raw->fingers[i]));
1879
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001880 if (raw->end_of_frame &&
1881 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001882 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
1883
1884 if (raw->end_of_frame || raw->finger_count <= 2) {
1885 input_mt_sync_frame(wd->input);
1886 input_sync(wd->input);
1887 }
1888}
1889
1890static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
1891{
1892 struct wtp_data *wd = hidpp->private_data;
1893 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
1894 (data[7] >> 4) * (data[7] >> 4)) / 2;
1895 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
1896 (data[13] >> 4) * (data[13] >> 4)) / 2;
1897 struct hidpp_touchpad_raw_xy raw = {
1898 .timestamp = data[1],
1899 .fingers = {
1900 {
1901 .contact_type = 0,
1902 .contact_status = !!data[7],
1903 .x = get_unaligned_le16(&data[3]),
1904 .y = get_unaligned_le16(&data[5]),
1905 .z = c1_area,
1906 .area = c1_area,
1907 .finger_id = data[2],
1908 }, {
1909 .contact_type = 0,
1910 .contact_status = !!data[13],
1911 .x = get_unaligned_le16(&data[9]),
1912 .y = get_unaligned_le16(&data[11]),
1913 .z = c2_area,
1914 .area = c2_area,
1915 .finger_id = data[8],
1916 }
1917 },
1918 .finger_count = wd->maxcontacts,
1919 .spurious_flag = 0,
1920 .end_of_frame = (data[0] >> 7) == 0,
1921 .button = data[0] & 0x01,
1922 };
1923
1924 wtp_send_raw_xy_event(hidpp, &raw);
1925
1926 return 1;
1927}
1928
1929static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
1930{
1931 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1932 struct wtp_data *wd = hidpp->private_data;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001933 struct hidpp_report *report = (struct hidpp_report *)data;
1934 struct hidpp_touchpad_raw_xy raw;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001935
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001936 if (!wd || !wd->input)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001937 return 1;
1938
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001939 switch (data[0]) {
1940 case 0x02:
Peter Wu0b3f6562014-12-16 16:55:22 +01001941 if (size < 2) {
1942 hid_err(hdev, "Received HID report of bad size (%d)",
1943 size);
1944 return 1;
1945 }
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001946 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
1947 input_event(wd->input, EV_KEY, BTN_LEFT,
1948 !!(data[1] & 0x01));
1949 input_event(wd->input, EV_KEY, BTN_RIGHT,
1950 !!(data[1] & 0x02));
1951 input_sync(wd->input);
Peter Wu8abd8202014-12-16 01:50:16 +01001952 return 0;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001953 } else {
1954 if (size < 21)
1955 return 1;
1956 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
1957 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001958 case REPORT_ID_HIDPP_LONG:
Peter Wu0b3f6562014-12-16 16:55:22 +01001959 /* size is already checked in hidpp_raw_event. */
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04001960 if ((report->fap.feature_index != wd->mt_feature_index) ||
1961 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
1962 return 1;
1963 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
1964
1965 wtp_send_raw_xy_event(hidpp, &raw);
1966 return 0;
1967 }
1968
1969 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001970}
1971
1972static int wtp_get_config(struct hidpp_device *hidpp)
1973{
1974 struct wtp_data *wd = hidpp->private_data;
1975 struct hidpp_touchpad_raw_info raw_info = {0};
1976 u8 feature_type;
1977 int ret;
1978
1979 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
1980 &wd->mt_feature_index, &feature_type);
1981 if (ret)
1982 /* means that the device is not powered up */
1983 return ret;
1984
1985 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
1986 &raw_info);
1987 if (ret)
1988 return ret;
1989
1990 wd->x_size = raw_info.x_size;
1991 wd->y_size = raw_info.y_size;
1992 wd->maxcontacts = raw_info.maxcontacts;
1993 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
1994 wd->resolution = raw_info.res;
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04001995 if (!wd->resolution)
1996 wd->resolution = WTP_MANUAL_RESOLUTION;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04001997
1998 return 0;
1999}
2000
2001static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2002{
2003 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2004 struct wtp_data *wd;
2005
2006 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2007 GFP_KERNEL);
2008 if (!wd)
2009 return -ENOMEM;
2010
2011 hidpp->private_data = wd;
2012
2013 return 0;
2014};
2015
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002016static int wtp_connect(struct hid_device *hdev, bool connected)
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002017{
2018 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2019 struct wtp_data *wd = hidpp->private_data;
2020 int ret;
2021
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002022 if (!wd->x_size) {
2023 ret = wtp_get_config(hidpp);
2024 if (ret) {
2025 hid_err(hdev, "Can not get wtp config: %d\n", ret);
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002026 return ret;
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002027 }
2028 }
2029
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002030 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002031 true, true);
2032}
2033
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002034/* ------------------------------------------------------------------------- */
2035/* Logitech M560 devices */
2036/* ------------------------------------------------------------------------- */
2037
2038/*
2039 * Logitech M560 protocol overview
2040 *
2041 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2042 * the sides buttons are pressed, it sends some keyboard keys events
2043 * instead of buttons ones.
2044 * To complicate things further, the middle button keys sequence
2045 * is different from the odd press and the even press.
2046 *
2047 * forward button -> Super_R
2048 * backward button -> Super_L+'d' (press only)
2049 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2050 * 2nd time: left-click (press only)
2051 * NB: press-only means that when the button is pressed, the
2052 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2053 * together sequentially; instead when the button is released, no event is
2054 * generated !
2055 *
2056 * With the command
2057 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2058 * the mouse reacts differently:
2059 * - it never sends a keyboard key event
2060 * - for the three mouse button it sends:
2061 * middle button press 11<xx>0a 3500af00...
2062 * side 1 button (forward) press 11<xx>0a 3500b000...
2063 * side 2 button (backward) press 11<xx>0a 3500ae00...
2064 * middle/side1/side2 button release 11<xx>0a 35000000...
2065 */
2066
2067static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2068
2069struct m560_private_data {
2070 struct input_dev *input;
2071};
2072
2073/* how buttons are mapped in the report */
2074#define M560_MOUSE_BTN_LEFT 0x01
2075#define M560_MOUSE_BTN_RIGHT 0x02
2076#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2077#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2078
2079#define M560_SUB_ID 0x0a
2080#define M560_BUTTON_MODE_REGISTER 0x35
2081
2082static int m560_send_config_command(struct hid_device *hdev, bool connected)
2083{
2084 struct hidpp_report response;
2085 struct hidpp_device *hidpp_dev;
2086
2087 hidpp_dev = hid_get_drvdata(hdev);
2088
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002089 return hidpp_send_rap_command_sync(
2090 hidpp_dev,
2091 REPORT_ID_HIDPP_SHORT,
2092 M560_SUB_ID,
2093 M560_BUTTON_MODE_REGISTER,
2094 (u8 *)m560_config_parameter,
2095 sizeof(m560_config_parameter),
2096 &response
2097 );
2098}
2099
2100static int m560_allocate(struct hid_device *hdev)
2101{
2102 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2103 struct m560_private_data *d;
2104
2105 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2106 GFP_KERNEL);
2107 if (!d)
2108 return -ENOMEM;
2109
2110 hidpp->private_data = d;
2111
2112 return 0;
2113};
2114
2115static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2116{
2117 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2118 struct m560_private_data *mydata = hidpp->private_data;
2119
2120 /* sanity check */
2121 if (!mydata || !mydata->input) {
2122 hid_err(hdev, "error in parameter\n");
2123 return -EINVAL;
2124 }
2125
2126 if (size < 7) {
2127 hid_err(hdev, "error in report\n");
2128 return 0;
2129 }
2130
2131 if (data[0] == REPORT_ID_HIDPP_LONG &&
2132 data[2] == M560_SUB_ID && data[6] == 0x00) {
2133 /*
2134 * m560 mouse report for middle, forward and backward button
2135 *
2136 * data[0] = 0x11
2137 * data[1] = device-id
2138 * data[2] = 0x0a
2139 * data[5] = 0xaf -> middle
2140 * 0xb0 -> forward
2141 * 0xae -> backward
2142 * 0x00 -> release all
2143 * data[6] = 0x00
2144 */
2145
2146 switch (data[5]) {
2147 case 0xaf:
2148 input_report_key(mydata->input, BTN_MIDDLE, 1);
2149 break;
2150 case 0xb0:
2151 input_report_key(mydata->input, BTN_FORWARD, 1);
2152 break;
2153 case 0xae:
2154 input_report_key(mydata->input, BTN_BACK, 1);
2155 break;
2156 case 0x00:
2157 input_report_key(mydata->input, BTN_BACK, 0);
2158 input_report_key(mydata->input, BTN_FORWARD, 0);
2159 input_report_key(mydata->input, BTN_MIDDLE, 0);
2160 break;
2161 default:
2162 hid_err(hdev, "error in report\n");
2163 return 0;
2164 }
2165 input_sync(mydata->input);
2166
2167 } else if (data[0] == 0x02) {
2168 /*
2169 * Logitech M560 mouse report
2170 *
2171 * data[0] = type (0x02)
2172 * data[1..2] = buttons
2173 * data[3..5] = xy
2174 * data[6] = wheel
2175 */
2176
2177 int v;
2178
2179 input_report_key(mydata->input, BTN_LEFT,
2180 !!(data[1] & M560_MOUSE_BTN_LEFT));
2181 input_report_key(mydata->input, BTN_RIGHT,
2182 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2183
2184 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2185 input_report_rel(mydata->input, REL_HWHEEL, -1);
2186 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2187 input_report_rel(mydata->input, REL_HWHEEL, 1);
2188
2189 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2190 input_report_rel(mydata->input, REL_X, v);
2191
2192 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2193 input_report_rel(mydata->input, REL_Y, v);
2194
2195 v = hid_snto32(data[6], 8);
2196 input_report_rel(mydata->input, REL_WHEEL, v);
2197
2198 input_sync(mydata->input);
2199 }
2200
2201 return 1;
2202}
2203
2204static void m560_populate_input(struct hidpp_device *hidpp,
2205 struct input_dev *input_dev, bool origin_is_hid_core)
2206{
2207 struct m560_private_data *mydata = hidpp->private_data;
2208
2209 mydata->input = input_dev;
2210
2211 __set_bit(EV_KEY, mydata->input->evbit);
2212 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2213 __set_bit(BTN_RIGHT, mydata->input->keybit);
2214 __set_bit(BTN_LEFT, mydata->input->keybit);
2215 __set_bit(BTN_BACK, mydata->input->keybit);
2216 __set_bit(BTN_FORWARD, mydata->input->keybit);
2217
2218 __set_bit(EV_REL, mydata->input->evbit);
2219 __set_bit(REL_X, mydata->input->relbit);
2220 __set_bit(REL_Y, mydata->input->relbit);
2221 __set_bit(REL_WHEEL, mydata->input->relbit);
2222 __set_bit(REL_HWHEEL, mydata->input->relbit);
2223}
2224
2225static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2226 struct hid_field *field, struct hid_usage *usage,
2227 unsigned long **bit, int *max)
2228{
2229 return -1;
2230}
2231
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002232/* ------------------------------------------------------------------------- */
2233/* Logitech K400 devices */
2234/* ------------------------------------------------------------------------- */
2235
2236/*
2237 * The Logitech K400 keyboard has an embedded touchpad which is seen
2238 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2239 * tap-to-click but the setting is not remembered accross reset, annoying some
2240 * users.
2241 *
2242 * We can toggle this feature from the host by using the feature 0x6010:
2243 * Touchpad FW items
2244 */
2245
2246struct k400_private_data {
2247 u8 feature_index;
2248};
2249
2250static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2251{
2252 struct k400_private_data *k400 = hidpp->private_data;
2253 struct hidpp_touchpad_fw_items items = {};
2254 int ret;
2255 u8 feature_type;
2256
2257 if (!k400->feature_index) {
2258 ret = hidpp_root_get_feature(hidpp,
2259 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2260 &k400->feature_index, &feature_type);
2261 if (ret)
2262 /* means that the device is not powered up */
2263 return ret;
2264 }
2265
2266 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2267 if (ret)
2268 return ret;
2269
2270 return 0;
2271}
2272
2273static int k400_allocate(struct hid_device *hdev)
2274{
2275 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2276 struct k400_private_data *k400;
2277
2278 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2279 GFP_KERNEL);
2280 if (!k400)
2281 return -ENOMEM;
2282
2283 hidpp->private_data = k400;
2284
2285 return 0;
2286};
2287
2288static int k400_connect(struct hid_device *hdev, bool connected)
2289{
2290 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2291
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002292 if (!disable_tap_to_click)
2293 return 0;
2294
2295 return k400_disable_tap_to_click(hidpp);
2296}
2297
Simon Wood7f4b49f2015-11-19 16:42:13 -07002298/* ------------------------------------------------------------------------- */
2299/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2300/* ------------------------------------------------------------------------- */
2301
2302#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2303
Simon Wood7f4b49f2015-11-19 16:42:13 -07002304static int g920_get_config(struct hidpp_device *hidpp)
2305{
Simon Wood7f4b49f2015-11-19 16:42:13 -07002306 u8 feature_type;
2307 u8 feature_index;
2308 int ret;
2309
Simon Wood7f4b49f2015-11-19 16:42:13 -07002310 /* Find feature and store for later use */
2311 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2312 &feature_index, &feature_type);
2313 if (ret)
2314 return ret;
2315
Edwin Veldsff21a632016-01-11 00:25:15 +01002316 ret = hidpp_ff_init(hidpp, feature_index);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002317 if (ret)
Edwin Veldsff21a632016-01-11 00:25:15 +01002318 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2319 ret);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002320
2321 return 0;
2322}
2323
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002324/* -------------------------------------------------------------------------- */
2325/* Generic HID++ devices */
2326/* -------------------------------------------------------------------------- */
2327
2328static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2329 struct hid_field *field, struct hid_usage *usage,
2330 unsigned long **bit, int *max)
2331{
2332 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2333
2334 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2335 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002336 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2337 field->application != HID_GD_MOUSE)
2338 return m560_input_mapping(hdev, hi, field, usage, bit, max);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002339
2340 return 0;
2341}
2342
Simon Wood0b1804e2015-11-19 16:42:15 -07002343static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2344 struct hid_field *field, struct hid_usage *usage,
2345 unsigned long **bit, int *max)
2346{
2347 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2348
2349 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2350 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2351 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2352 usage->code == ABS_Y || usage->code == ABS_Z ||
2353 usage->code == ABS_RZ)) {
2354 field->application = HID_GD_MULTIAXIS;
2355 }
2356 }
2357
2358 return 0;
2359}
2360
2361
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002362static void hidpp_populate_input(struct hidpp_device *hidpp,
2363 struct input_dev *input, bool origin_is_hid_core)
2364{
2365 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2366 wtp_populate_input(hidpp, input, origin_is_hid_core);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002367 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2368 m560_populate_input(hidpp, input, origin_is_hid_core);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002369}
2370
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002371static int hidpp_input_configured(struct hid_device *hdev,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002372 struct hid_input *hidinput)
2373{
2374 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002375 struct input_dev *input = hidinput->input;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002376
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002377 hidpp_populate_input(hidpp, input, true);
Dmitry Torokhovb2c68a22015-09-29 15:52:59 -07002378
2379 return 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002380}
2381
2382static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2383 int size)
2384{
2385 struct hidpp_report *question = hidpp->send_receive_buf;
2386 struct hidpp_report *answer = hidpp->send_receive_buf;
2387 struct hidpp_report *report = (struct hidpp_report *)data;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002388 int ret;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002389
2390 /*
2391 * If the mutex is locked then we have a pending answer from a
Peter Wue529fea2014-12-17 00:23:51 +01002392 * previously sent command.
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002393 */
2394 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2395 /*
2396 * Check for a correct hidpp20 answer or the corresponding
2397 * error
2398 */
2399 if (hidpp_match_answer(question, report) ||
2400 hidpp_match_error(question, report)) {
2401 *answer = *report;
2402 hidpp->answer_available = true;
2403 wake_up(&hidpp->wait);
2404 /*
2405 * This was an answer to a command that this driver sent
2406 * We return 1 to hid-core to avoid forwarding the
2407 * command upstream as it has been treated by the driver
2408 */
2409
2410 return 1;
2411 }
2412 }
2413
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002414 if (unlikely(hidpp_report_is_connect_event(report))) {
2415 atomic_set(&hidpp->connected,
2416 !(report->rap.params[0] & (1 << 6)));
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002417 if (schedule_work(&hidpp->work) == 0)
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002418 dbg_hid("%s: connect event already queued\n", __func__);
2419 return 1;
2420 }
2421
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002422 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2423 ret = hidpp20_battery_event(hidpp, data, size);
2424 if (ret != 0)
2425 return ret;
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02002426 ret = hidpp_solar_battery_event(hidpp, data, size);
2427 if (ret != 0)
2428 return ret;
Benjamin Tissoireseb626c52017-03-27 16:59:29 +02002429 }
2430
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002431 return 0;
2432}
2433
2434static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2435 u8 *data, int size)
2436{
2437 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Peter Wue529fea2014-12-17 00:23:51 +01002438 int ret = 0;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002439
Peter Wue529fea2014-12-17 00:23:51 +01002440 /* Generic HID++ processing. */
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002441 switch (data[0]) {
Simon Wooda5ce8f52015-11-19 16:42:11 -07002442 case REPORT_ID_HIDPP_VERY_LONG:
2443 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2444 hid_err(hdev, "received hid++ report of bad size (%d)",
2445 size);
2446 return 1;
2447 }
2448 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2449 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002450 case REPORT_ID_HIDPP_LONG:
2451 if (size != HIDPP_REPORT_LONG_LENGTH) {
2452 hid_err(hdev, "received hid++ report of bad size (%d)",
2453 size);
2454 return 1;
2455 }
Peter Wue529fea2014-12-17 00:23:51 +01002456 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2457 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002458 case REPORT_ID_HIDPP_SHORT:
2459 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2460 hid_err(hdev, "received hid++ report of bad size (%d)",
2461 size);
2462 return 1;
2463 }
Peter Wue529fea2014-12-17 00:23:51 +01002464 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2465 break;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002466 }
2467
Peter Wue529fea2014-12-17 00:23:51 +01002468 /* If no report is available for further processing, skip calling
2469 * raw_event of subclasses. */
2470 if (ret != 0)
2471 return ret;
2472
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002473 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2474 return wtp_raw_event(hdev, data, size);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002475 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2476 return m560_raw_event(hdev, data, size);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002477
2478 return 0;
2479}
2480
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002481static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2482{
2483 static atomic_t battery_no = ATOMIC_INIT(0);
2484 struct power_supply_config cfg = { .drv_data = hidpp };
2485 struct power_supply_desc *desc = &hidpp->battery.desc;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002486 enum power_supply_property *battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002487 struct hidpp_battery *battery;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002488 unsigned int num_battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002489 unsigned long n;
2490 int ret;
2491
2492 if (hidpp->battery.ps)
2493 return 0;
2494
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02002495 hidpp->battery.feature_index = 0xff;
2496 hidpp->battery.solar_feature_index = 0xff;
2497
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002498 if (hidpp->protocol_major >= 2) {
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02002499 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
2500 ret = hidpp_solar_request_battery_event(hidpp);
2501 else
2502 ret = hidpp20_query_battery_info(hidpp);
2503
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002504 if (ret)
2505 return ret;
2506 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2507 } else {
2508 return -ENOENT;
2509 }
2510
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002511 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
2512 hidpp_battery_props,
2513 sizeof(hidpp_battery_props),
2514 GFP_KERNEL);
2515 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
2516
2517 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2518 battery_props[num_battery_props++] =
2519 POWER_SUPPLY_PROP_CAPACITY;
2520
2521 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
2522 battery_props[num_battery_props++] =
2523 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
2524
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002525 battery = &hidpp->battery;
2526
2527 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002528 desc->properties = battery_props;
2529 desc->num_properties = num_battery_props;
Benjamin Tissoiresa52ec102017-03-27 16:59:31 +02002530 desc->get_property = hidpp_battery_get_property;
2531 sprintf(battery->name, "hidpp_battery_%ld", n);
2532 desc->name = battery->name;
2533 desc->type = POWER_SUPPLY_TYPE_BATTERY;
2534 desc->use_for_apm = 0;
2535
2536 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2537 &battery->desc,
2538 &cfg);
2539 if (IS_ERR(battery->ps))
2540 return PTR_ERR(battery->ps);
2541
2542 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2543
2544 return ret;
2545}
2546
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002547static void hidpp_overwrite_name(struct hid_device *hdev)
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002548{
2549 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2550 char *name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002551
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002552 if (hidpp->protocol_major < 2)
Benjamin Tissoiresb4f8ce02017-03-27 16:59:24 +02002553 return;
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002554
2555 name = hidpp_get_device_name(hidpp);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002556
Simon Wood7bfd2922015-11-19 16:42:12 -07002557 if (!name) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002558 hid_err(hdev, "unable to retrieve the name of the device");
Simon Wood7bfd2922015-11-19 16:42:12 -07002559 } else {
2560 dbg_hid("HID++: Got name: %s\n", name);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002561 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
Simon Wood7bfd2922015-11-19 16:42:12 -07002562 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002563
2564 kfree(name);
2565}
2566
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002567static int hidpp_input_open(struct input_dev *dev)
2568{
2569 struct hid_device *hid = input_get_drvdata(dev);
2570
2571 return hid_hw_open(hid);
2572}
2573
2574static void hidpp_input_close(struct input_dev *dev)
2575{
2576 struct hid_device *hid = input_get_drvdata(dev);
2577
2578 hid_hw_close(hid);
2579}
2580
2581static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2582{
2583 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002584 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002585
2586 if (!input_dev)
2587 return NULL;
2588
2589 input_set_drvdata(input_dev, hdev);
2590 input_dev->open = hidpp_input_open;
2591 input_dev->close = hidpp_input_close;
2592
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002593 input_dev->name = hidpp->name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002594 input_dev->phys = hdev->phys;
2595 input_dev->uniq = hdev->uniq;
2596 input_dev->id.bustype = hdev->bus;
2597 input_dev->id.vendor = hdev->vendor;
2598 input_dev->id.product = hdev->product;
2599 input_dev->id.version = hdev->version;
2600 input_dev->dev.parent = &hdev->dev;
2601
2602 return input_dev;
2603}
2604
2605static void hidpp_connect_event(struct hidpp_device *hidpp)
2606{
2607 struct hid_device *hdev = hidpp->hid_dev;
2608 int ret = 0;
2609 bool connected = atomic_read(&hidpp->connected);
2610 struct input_dev *input;
2611 char *name, *devm_name;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002612
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02002613 if (!connected) {
2614 if (hidpp->battery.ps) {
2615 hidpp->battery.online = false;
2616 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
Benjamin Tissoires5b036ea2017-03-27 16:59:36 +02002617 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02002618 power_supply_changed(hidpp->battery.ps);
2619 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02002620 return;
Benjamin Tissoires284f8d72017-03-27 16:59:34 +02002621 }
Benjamin Tissoires29368362017-03-27 16:59:28 +02002622
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002623 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2624 ret = wtp_connect(hdev, connected);
2625 if (ret)
2626 return;
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002627 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2628 ret = m560_send_config_command(hdev, connected);
2629 if (ret)
2630 return;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002631 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2632 ret = k400_connect(hdev, connected);
2633 if (ret)
2634 return;
Benjamin Tissoiresbf159442014-12-16 17:06:01 -05002635 }
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002636
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002637 /* the device is already connected, we can ask for its name and
2638 * protocol */
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002639 if (!hidpp->protocol_major) {
2640 ret = !hidpp_is_connected(hidpp);
2641 if (ret) {
2642 hid_err(hdev, "Can not get the protocol version.\n");
2643 return;
2644 }
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002645 hid_info(hdev, "HID++ %u.%u device connected.\n",
2646 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002647 }
2648
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02002649 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002650 name = hidpp_get_device_name(hidpp);
2651 if (!name) {
2652 hid_err(hdev,
2653 "unable to retrieve the name of the device");
2654 return;
2655 }
2656
2657 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2658 kfree(name);
2659 if (!devm_name)
2660 return;
2661
2662 hidpp->name = devm_name;
2663 }
2664
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02002665 hidpp_initialize_battery(hidpp);
2666
Benjamin Tissoires9b9c5192017-03-27 16:59:33 +02002667 /* forward current battery state */
2668 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2669 hidpp20_query_battery_info(hidpp);
2670 if (hidpp->battery.ps)
2671 power_supply_changed(hidpp->battery.ps);
2672 }
2673
Benjamin Tissoires29368362017-03-27 16:59:28 +02002674 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
2675 /* if the input nodes are already created, we can stop now */
Benjamin Tissoires187f2bb2017-03-27 16:59:27 +02002676 return;
2677
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002678 input = hidpp_allocate_input(hdev);
2679 if (!input) {
2680 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2681 return;
2682 }
2683
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002684 hidpp_populate_input(hidpp, input, false);
2685
2686 ret = input_register_device(input);
2687 if (ret)
2688 input_free_device(input);
2689
2690 hidpp->delayed_input = input;
2691}
2692
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002693static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2694{
2695 struct hidpp_device *hidpp;
2696 int ret;
2697 bool connected;
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002698 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002699
2700 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2701 GFP_KERNEL);
2702 if (!hidpp)
2703 return -ENOMEM;
2704
2705 hidpp->hid_dev = hdev;
Benjamin Tissoires005b3f52015-01-08 14:37:12 -05002706 hidpp->name = hdev->name;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002707 hid_set_drvdata(hdev, hidpp);
2708
2709 hidpp->quirks = id->driver_data;
2710
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002711 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
2712 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
2713
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04002714 if (disable_raw_mode) {
2715 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
Benjamin Tissoires580a7e82015-09-03 09:08:29 -04002716 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
Benjamin Tissoires9188dba2015-03-26 12:41:57 -04002717 }
2718
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002719 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2720 ret = wtp_allocate(hdev, id);
2721 if (ret)
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002722 goto allocate_fail;
2723 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2724 ret = m560_allocate(hdev);
2725 if (ret)
2726 goto allocate_fail;
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002727 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2728 ret = k400_allocate(hdev);
2729 if (ret)
2730 goto allocate_fail;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002731 }
2732
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002733 INIT_WORK(&hidpp->work, delayed_work_cb);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002734 mutex_init(&hidpp->send_mutex);
2735 init_waitqueue_head(&hidpp->wait);
2736
2737 ret = hid_parse(hdev);
2738 if (ret) {
2739 hid_err(hdev, "%s:parse failed\n", __func__);
2740 goto hid_parse_fail;
2741 }
2742
Simon Wood7bfd2922015-11-19 16:42:12 -07002743 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2744 connect_mask &= ~HID_CONNECT_HIDINPUT;
2745
2746 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2747 ret = hid_hw_start(hdev, connect_mask);
2748 if (ret) {
2749 hid_err(hdev, "hw start failed\n");
2750 goto hid_hw_start_fail;
2751 }
2752 ret = hid_hw_open(hdev);
2753 if (ret < 0) {
2754 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
2755 __func__, ret);
2756 hid_hw_stop(hdev);
2757 goto hid_hw_start_fail;
2758 }
2759 }
2760
2761
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002762 /* Allow incoming packets */
2763 hid_device_io_start(hdev);
2764
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002765 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
2766 hidpp_unifying_init(hidpp);
2767
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002768 connected = hidpp_is_connected(hidpp);
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002769 atomic_set(&hidpp->connected, connected);
2770 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002771 if (!connected) {
Julia Lawallb832da52015-04-05 14:06:29 +02002772 ret = -ENODEV;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002773 hid_err(hdev, "Device not connected");
Simon Wood7bfd2922015-11-19 16:42:12 -07002774 goto hid_hw_open_failed;
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002775 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002776
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002777 hid_info(hdev, "HID++ %u.%u device connected.\n",
2778 hidpp->protocol_major, hidpp->protocol_minor);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002779
Benjamin Tissoires843c6242017-03-27 16:59:26 +02002780 hidpp_overwrite_name(hdev);
2781 }
Benjamin Tissoires33797822014-09-30 13:18:30 -04002782
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002783 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002784 ret = wtp_get_config(hidpp);
2785 if (ret)
Simon Wood7bfd2922015-11-19 16:42:12 -07002786 goto hid_hw_open_failed;
Simon Wood7f4b49f2015-11-19 16:42:13 -07002787 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2788 ret = g920_get_config(hidpp);
2789 if (ret)
2790 goto hid_hw_open_failed;
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002791 }
2792
2793 /* Block incoming packets */
2794 hid_device_io_stop(hdev);
2795
Simon Wood7bfd2922015-11-19 16:42:12 -07002796 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2797 ret = hid_hw_start(hdev, connect_mask);
2798 if (ret) {
2799 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
2800 goto hid_hw_start_fail;
2801 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002802 }
2803
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002804 /* Allow incoming packets */
2805 hid_device_io_start(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002806
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002807 hidpp_connect_event(hidpp);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002808
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002809 return ret;
2810
Simon Wood7bfd2922015-11-19 16:42:12 -07002811hid_hw_open_failed:
2812 hid_device_io_stop(hdev);
2813 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2814 hid_hw_close(hdev);
2815 hid_hw_stop(hdev);
2816 }
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002817hid_hw_start_fail:
2818hid_parse_fail:
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002819 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002820 mutex_destroy(&hidpp->send_mutex);
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002821allocate_fail:
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002822 hid_set_drvdata(hdev, NULL);
2823 return ret;
2824}
2825
2826static void hidpp_remove(struct hid_device *hdev)
2827{
2828 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2829
Simon Wood7f4b49f2015-11-19 16:42:13 -07002830 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
Edwin Veldsff21a632016-01-11 00:25:15 +01002831 hidpp_ff_deinit(hdev);
Simon Wood7bfd2922015-11-19 16:42:12 -07002832 hid_hw_close(hdev);
Simon Wood7f4b49f2015-11-19 16:42:13 -07002833 }
Simon Wood7bfd2922015-11-19 16:42:12 -07002834 hid_hw_stop(hdev);
Benjamin Tissoiresc39e3d52014-09-30 13:18:32 -04002835 cancel_work_sync(&hidpp->work);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002836 mutex_destroy(&hidpp->send_mutex);
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002837}
2838
2839static const struct hid_device_id hidpp_devices[] = {
Benjamin Tissoires57ac86c2014-09-30 13:18:34 -04002840 { /* wireless touchpad */
2841 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2842 USB_VENDOR_ID_LOGITECH, 0x4011),
2843 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
2844 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
Benjamin Tissoires586bdc42014-09-30 13:18:33 -04002845 { /* wireless touchpad T650 */
2846 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2847 USB_VENDOR_ID_LOGITECH, 0x4101),
2848 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002849 { /* wireless touchpad T651 */
2850 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
2851 USB_DEVICE_ID_LOGITECH_T651),
2852 .driver_data = HIDPP_QUIRK_CLASS_WTP },
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002853 { /* Mouse logitech M560 */
Benjamin Tissoires3a61e972014-09-30 13:18:35 -04002854 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
Goffredo Baroncelli8a09b4f2015-05-30 11:00:27 +02002855 USB_VENDOR_ID_LOGITECH, 0x402d),
2856 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
Benjamin Tissoires90cdd982015-09-03 09:08:30 -04002857 { /* Keyboard logitech K400 */
2858 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2859 USB_VENDOR_ID_LOGITECH, 0x4024),
Benjamin Tissoires6bd4e652016-06-29 19:28:02 +10002860 .driver_data = HIDPP_QUIRK_CLASS_K400 },
Benjamin Tissoires696ecef2017-03-27 16:59:37 +02002861 { /* Solar Keyboard Logitech K750 */
2862 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2863 USB_VENDOR_ID_LOGITECH, 0x4002),
2864 .driver_data = HIDPP_QUIRK_CLASS_K750 },
Benjamin Tissoiresab94e562014-09-30 13:18:28 -04002865
2866 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2867 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
Simon Wood7bfd2922015-11-19 16:42:12 -07002868
2869 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
2870 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002871 {}
2872};
2873
2874MODULE_DEVICE_TABLE(hid, hidpp_devices);
2875
2876static struct hid_driver hidpp_driver = {
2877 .name = "logitech-hidpp-device",
2878 .id_table = hidpp_devices,
2879 .probe = hidpp_probe,
2880 .remove = hidpp_remove,
2881 .raw_event = hidpp_raw_event,
2882 .input_configured = hidpp_input_configured,
2883 .input_mapping = hidpp_input_mapping,
Simon Wood0b1804e2015-11-19 16:42:15 -07002884 .input_mapped = hidpp_input_mapped,
Benjamin Tissoires2f31c522014-09-30 13:18:27 -04002885};
2886
2887module_hid_driver(hidpp_driver);