]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hid/hid-logitech-hidpp.c
HID: logitech-hidpp: battery: provide CAPACITY_LEVEL
[mirror_ubuntu-artful-kernel.git] / drivers / hid / hid-logitech-hidpp.c
CommitLineData
2f31c525
BT
1/*
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>
ff21a635
EV
18#include <linux/input.h>
19#include <linux/usb.h>
2f31c525
BT
20#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>
ff21a635
EV
26#include <linux/workqueue.h>
27#include <linux/atomic.h>
28#include <linux/fixp-arith.h>
2f31c525 29#include <asm/unaligned.h>
ff21a635 30#include "usbhid/usbhid.h"
2f31c525
BT
31#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
9188dbae
BT
37static 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
90cdd986
BT
42static 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
2f31c525
BT
47#define REPORT_ID_HIDPP_SHORT 0x10
48#define REPORT_ID_HIDPP_LONG 0x11
a5ce8f5b 49#define REPORT_ID_HIDPP_VERY_LONG 0x12
2f31c525
BT
50
51#define HIDPP_REPORT_SHORT_LENGTH 7
52#define HIDPP_REPORT_LONG_LENGTH 20
a5ce8f5b 53#define HIDPP_REPORT_VERY_LONG_LENGTH 64
2f31c525
BT
54
55#define HIDPP_QUIRK_CLASS_WTP BIT(0)
8a09b4fa 56#define HIDPP_QUIRK_CLASS_M560 BIT(1)
90cdd986 57#define HIDPP_QUIRK_CLASS_K400 BIT(2)
7bfd2927 58#define HIDPP_QUIRK_CLASS_G920 BIT(3)
2f31c525 59
8a09b4fa 60/* bits 2..20 are reserved for classes */
6bd4e65d 61/* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
57ac86cf 62#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
580a7e82 63#define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
7bfd2927 64#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
843c624e 65#define HIDPP_QUIRK_UNIFYING BIT(25)
580a7e82 66
6bd4e65d 67#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
c39e3d5f 68
206d7c68
BT
69#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
70#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
5b036ea1
BT
71#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
72#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
206d7c68 73
2f31c525
BT
74/*
75 * There are two hidpp protocols in use, the first version hidpp10 is known
76 * as register access protocol or RAP, the second version hidpp20 is known as
77 * feature access protocol or FAP
78 *
79 * Most older devices (including the Unifying usb receiver) use the RAP protocol
80 * where as most newer devices use the FAP protocol. Both protocols are
81 * compatible with the underlying transport, which could be usb, Unifiying, or
82 * bluetooth. The message lengths are defined by the hid vendor specific report
83 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
84 * the HIDPP_LONG report type (total message length 20 bytes)
85 *
86 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
87 * messages. The Unifying receiver itself responds to RAP messages (device index
88 * is 0xFF for the receiver), and all messages (short or long) with a device
89 * index between 1 and 6 are passed untouched to the corresponding paired
90 * Unifying device.
91 *
92 * The paired device can be RAP or FAP, it will receive the message untouched
93 * from the Unifiying receiver.
94 */
95
96struct fap {
97 u8 feature_index;
98 u8 funcindex_clientid;
a5ce8f5b 99 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
2f31c525
BT
100};
101
102struct rap {
103 u8 sub_id;
104 u8 reg_address;
a5ce8f5b 105 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
2f31c525
BT
106};
107
108struct hidpp_report {
109 u8 report_id;
110 u8 device_index;
111 union {
112 struct fap fap;
113 struct rap rap;
114 u8 rawbytes[sizeof(struct fap)];
115 };
116} __packed;
117
5a2b190c
PH
118struct hidpp_battery {
119 u8 feature_index;
120 struct power_supply_desc desc;
121 struct power_supply *ps;
122 char name[64];
123 int status;
14f437a1 124 int capacity;
5b036ea1 125 int level;
284f8d75 126 bool online;
5a2b190c
PH
127};
128
2f31c525
BT
129struct hidpp_device {
130 struct hid_device *hid_dev;
131 struct mutex send_mutex;
132 void *send_receive_buf;
005b3f57 133 char *name; /* will never be NULL and should not be freed */
2f31c525
BT
134 wait_queue_head_t wait;
135 bool answer_available;
136 u8 protocol_major;
137 u8 protocol_minor;
138
139 void *private_data;
140
c39e3d5f
BT
141 struct work_struct work;
142 struct kfifo delayed_work_fifo;
143 atomic_t connected;
144 struct input_dev *delayed_input;
145
2f31c525 146 unsigned long quirks;
206d7c68 147 unsigned long capabilities;
2f31c525 148
5a2b190c
PH
149 struct hidpp_battery battery;
150};
2f31c525 151
f677bb15 152/* HID++ 1.0 error codes */
2f31c525
BT
153#define HIDPP_ERROR 0x8f
154#define HIDPP_ERROR_SUCCESS 0x00
155#define HIDPP_ERROR_INVALID_SUBID 0x01
156#define HIDPP_ERROR_INVALID_ADRESS 0x02
157#define HIDPP_ERROR_INVALID_VALUE 0x03
158#define HIDPP_ERROR_CONNECT_FAIL 0x04
159#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
160#define HIDPP_ERROR_ALREADY_EXISTS 0x06
161#define HIDPP_ERROR_BUSY 0x07
162#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
163#define HIDPP_ERROR_RESOURCE_ERROR 0x09
164#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
165#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
166#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
f677bb15
PW
167/* HID++ 2.0 error codes */
168#define HIDPP20_ERROR 0xff
2f31c525 169
c39e3d5f
BT
170static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
171
2f31c525
BT
172static int __hidpp_send_report(struct hid_device *hdev,
173 struct hidpp_report *hidpp_report)
174{
7bfd2927 175 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2f31c525
BT
176 int fields_count, ret;
177
7bfd2927
SW
178 hidpp = hid_get_drvdata(hdev);
179
2f31c525
BT
180 switch (hidpp_report->report_id) {
181 case REPORT_ID_HIDPP_SHORT:
182 fields_count = HIDPP_REPORT_SHORT_LENGTH;
183 break;
184 case REPORT_ID_HIDPP_LONG:
185 fields_count = HIDPP_REPORT_LONG_LENGTH;
186 break;
a5ce8f5b
SW
187 case REPORT_ID_HIDPP_VERY_LONG:
188 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
189 break;
2f31c525
BT
190 default:
191 return -ENODEV;
192 }
193
194 /*
195 * set the device_index as the receiver, it will be overwritten by
196 * hid_hw_request if needed
197 */
198 hidpp_report->device_index = 0xff;
199
7bfd2927
SW
200 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
201 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
202 } else {
203 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
204 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
205 HID_REQ_SET_REPORT);
206 }
2f31c525
BT
207
208 return ret == fields_count ? 0 : -1;
209}
210
8c9952b2
BT
211/**
212 * hidpp_send_message_sync() returns 0 in case of success, and something else
213 * in case of a failure.
214 * - If ' something else' is positive, that means that an error has been raised
215 * by the protocol itself.
216 * - If ' something else' is negative, that means that we had a classic error
217 * (-ENOMEM, -EPIPE, etc...)
218 */
2f31c525
BT
219static int hidpp_send_message_sync(struct hidpp_device *hidpp,
220 struct hidpp_report *message,
221 struct hidpp_report *response)
222{
223 int ret;
224
225 mutex_lock(&hidpp->send_mutex);
226
227 hidpp->send_receive_buf = response;
228 hidpp->answer_available = false;
229
230 /*
231 * So that we can later validate the answer when it arrives
232 * in hidpp_raw_event
233 */
234 *response = *message;
235
236 ret = __hidpp_send_report(hidpp->hid_dev, message);
237
238 if (ret) {
239 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
240 memset(response, 0, sizeof(struct hidpp_report));
241 goto exit;
242 }
243
244 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
245 5*HZ)) {
246 dbg_hid("%s:timeout waiting for response\n", __func__);
247 memset(response, 0, sizeof(struct hidpp_report));
248 ret = -ETIMEDOUT;
249 }
250
251 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
f677bb15
PW
252 response->rap.sub_id == HIDPP_ERROR) {
253 ret = response->rap.params[1];
254 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
255 goto exit;
256 }
257
a5ce8f5b
SW
258 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
259 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
260 response->fap.feature_index == HIDPP20_ERROR) {
2f31c525 261 ret = response->fap.params[1];
f677bb15 262 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
2f31c525
BT
263 goto exit;
264 }
265
266exit:
267 mutex_unlock(&hidpp->send_mutex);
268 return ret;
269
270}
271
272static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
273 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
274 struct hidpp_report *response)
275{
3e7830ce 276 struct hidpp_report *message;
2f31c525
BT
277 int ret;
278
279 if (param_count > sizeof(message->fap.params))
280 return -EINVAL;
281
3e7830ce
DC
282 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
283 if (!message)
284 return -ENOMEM;
a5ce8f5b
SW
285
286 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
287 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
288 else
289 message->report_id = REPORT_ID_HIDPP_LONG;
2f31c525
BT
290 message->fap.feature_index = feat_index;
291 message->fap.funcindex_clientid = funcindex_clientid;
292 memcpy(&message->fap.params, params, param_count);
293
294 ret = hidpp_send_message_sync(hidpp, message, response);
295 kfree(message);
296 return ret;
297}
298
33797820
BT
299static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
300 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
301 struct hidpp_report *response)
302{
3e7830ce 303 struct hidpp_report *message;
a5ce8f5b 304 int ret, max_count;
33797820 305
a5ce8f5b
SW
306 switch (report_id) {
307 case REPORT_ID_HIDPP_SHORT:
308 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
309 break;
310 case REPORT_ID_HIDPP_LONG:
311 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
312 break;
313 case REPORT_ID_HIDPP_VERY_LONG:
314 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
315 break;
316 default:
33797820 317 return -EINVAL;
a5ce8f5b 318 }
33797820 319
a5ce8f5b 320 if (param_count > max_count)
33797820
BT
321 return -EINVAL;
322
3e7830ce
DC
323 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
324 if (!message)
325 return -ENOMEM;
33797820
BT
326 message->report_id = report_id;
327 message->rap.sub_id = sub_id;
328 message->rap.reg_address = reg_address;
329 memcpy(&message->rap.params, params, param_count);
330
331 ret = hidpp_send_message_sync(hidpp_dev, message, response);
332 kfree(message);
333 return ret;
334}
335
c39e3d5f
BT
336static void delayed_work_cb(struct work_struct *work)
337{
338 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
339 work);
340 hidpp_connect_event(hidpp);
341}
342
2f31c525
BT
343static inline bool hidpp_match_answer(struct hidpp_report *question,
344 struct hidpp_report *answer)
345{
346 return (answer->fap.feature_index == question->fap.feature_index) &&
347 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
348}
349
350static inline bool hidpp_match_error(struct hidpp_report *question,
351 struct hidpp_report *answer)
352{
f677bb15
PW
353 return ((answer->rap.sub_id == HIDPP_ERROR) ||
354 (answer->fap.feature_index == HIDPP20_ERROR)) &&
2f31c525
BT
355 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
356 (answer->fap.params[0] == question->fap.funcindex_clientid);
357}
358
c39e3d5f
BT
359static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
360{
361 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
362 (report->rap.sub_id == 0x41);
363}
364
a0e625f8
BT
365/**
366 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
367 */
368static void hidpp_prefix_name(char **name, int name_length)
369{
370#define PREFIX_LENGTH 9 /* "Logitech " */
371
372 int new_length;
373 char *new_name;
374
375 if (name_length > PREFIX_LENGTH &&
376 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
377 /* The prefix has is already in the name */
378 return;
379
380 new_length = PREFIX_LENGTH + name_length;
381 new_name = kzalloc(new_length, GFP_KERNEL);
382 if (!new_name)
383 return;
384
385 snprintf(new_name, new_length, "Logitech %s", *name);
386
387 kfree(*name);
388
389 *name = new_name;
390}
391
33797820
BT
392/* -------------------------------------------------------------------------- */
393/* HIDP++ 1.0 commands */
394/* -------------------------------------------------------------------------- */
395
396#define HIDPP_SET_REGISTER 0x80
397#define HIDPP_GET_REGISTER 0x81
398#define HIDPP_SET_LONG_REGISTER 0x82
399#define HIDPP_GET_LONG_REGISTER 0x83
400
401#define HIDPP_REG_PAIRING_INFORMATION 0xB5
843c624e
BT
402#define HIDPP_EXTENDED_PAIRING 0x30
403#define HIDPP_DEVICE_NAME 0x40
33797820 404
843c624e 405static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
33797820
BT
406{
407 struct hidpp_report response;
408 int ret;
843c624e 409 u8 params[1] = { HIDPP_DEVICE_NAME };
33797820
BT
410 char *name;
411 int len;
412
413 ret = hidpp_send_rap_command_sync(hidpp_dev,
414 REPORT_ID_HIDPP_SHORT,
415 HIDPP_GET_LONG_REGISTER,
416 HIDPP_REG_PAIRING_INFORMATION,
417 params, 1, &response);
418 if (ret)
419 return NULL;
420
421 len = response.rap.params[1];
422
3a034a7a
PW
423 if (2 + len > sizeof(response.rap.params))
424 return NULL;
425
33797820
BT
426 name = kzalloc(len + 1, GFP_KERNEL);
427 if (!name)
428 return NULL;
429
430 memcpy(name, &response.rap.params[2], len);
a0e625f8
BT
431
432 /* include the terminating '\0' */
433 hidpp_prefix_name(&name, len + 1);
434
33797820
BT
435 return name;
436}
437
843c624e
BT
438static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
439{
440 struct hidpp_report response;
441 int ret;
442 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
443
444 ret = hidpp_send_rap_command_sync(hidpp,
445 REPORT_ID_HIDPP_SHORT,
446 HIDPP_GET_LONG_REGISTER,
447 HIDPP_REG_PAIRING_INFORMATION,
448 params, 1, &response);
449 if (ret)
450 return ret;
451
452 /*
453 * We don't care about LE or BE, we will output it as a string
454 * with %4phD, so we need to keep the order.
455 */
456 *serial = *((u32 *)&response.rap.params[1]);
457 return 0;
458}
459
460static int hidpp_unifying_init(struct hidpp_device *hidpp)
461{
462 struct hid_device *hdev = hidpp->hid_dev;
463 const char *name;
464 u32 serial;
465 int ret;
466
467 ret = hidpp_unifying_get_serial(hidpp, &serial);
468 if (ret)
469 return ret;
470
471 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
472 hdev->product, &serial);
473 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
474
475 name = hidpp_unifying_get_name(hidpp);
476 if (!name)
477 return -EIO;
478
479 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
480 dbg_hid("HID++ Unifying: Got name: %s\n", name);
481
482 kfree(name);
483 return 0;
484}
485
2f31c525
BT
486/* -------------------------------------------------------------------------- */
487/* 0x0000: Root */
488/* -------------------------------------------------------------------------- */
489
490#define HIDPP_PAGE_ROOT 0x0000
491#define HIDPP_PAGE_ROOT_IDX 0x00
492
493#define CMD_ROOT_GET_FEATURE 0x01
494#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
495
496static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
497 u8 *feature_index, u8 *feature_type)
498{
499 struct hidpp_report response;
500 int ret;
501 u8 params[2] = { feature >> 8, feature & 0x00FF };
502
503 ret = hidpp_send_fap_command_sync(hidpp,
504 HIDPP_PAGE_ROOT_IDX,
505 CMD_ROOT_GET_FEATURE,
506 params, 2, &response);
507 if (ret)
508 return ret;
509
a9525b80
BT
510 if (response.fap.params[0] == 0)
511 return -ENOENT;
512
2f31c525
BT
513 *feature_index = response.fap.params[0];
514 *feature_type = response.fap.params[1];
515
516 return ret;
517}
518
519static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
520{
521 struct hidpp_report response;
522 int ret;
523
524 ret = hidpp_send_fap_command_sync(hidpp,
525 HIDPP_PAGE_ROOT_IDX,
526 CMD_ROOT_GET_PROTOCOL_VERSION,
527 NULL, 0, &response);
528
552f12eb 529 if (ret == HIDPP_ERROR_INVALID_SUBID) {
2f31c525
BT
530 hidpp->protocol_major = 1;
531 hidpp->protocol_minor = 0;
532 return 0;
533 }
534
552f12eb
BT
535 /* the device might not be connected */
536 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
537 return -EIO;
538
8c9952b2
BT
539 if (ret > 0) {
540 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
541 __func__, ret);
542 return -EPROTO;
543 }
2f31c525 544 if (ret)
8c9952b2 545 return ret;
2f31c525
BT
546
547 hidpp->protocol_major = response.fap.params[0];
548 hidpp->protocol_minor = response.fap.params[1];
549
550 return ret;
551}
552
553static bool hidpp_is_connected(struct hidpp_device *hidpp)
554{
555 int ret;
556
557 ret = hidpp_root_get_protocol_version(hidpp);
558 if (!ret)
559 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
560 hidpp->protocol_major, hidpp->protocol_minor);
561 return ret == 0;
562}
563
564/* -------------------------------------------------------------------------- */
565/* 0x0005: GetDeviceNameType */
566/* -------------------------------------------------------------------------- */
567
568#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
569
570#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
571#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
572#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
573
574static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
575 u8 feature_index, u8 *nameLength)
576{
577 struct hidpp_report response;
578 int ret;
579
580 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
581 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
582
8c9952b2
BT
583 if (ret > 0) {
584 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
585 __func__, ret);
586 return -EPROTO;
587 }
2f31c525 588 if (ret)
8c9952b2 589 return ret;
2f31c525
BT
590
591 *nameLength = response.fap.params[0];
592
593 return ret;
594}
595
596static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
597 u8 feature_index, u8 char_index, char *device_name, int len_buf)
598{
599 struct hidpp_report response;
600 int ret, i;
601 int count;
602
603 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
604 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
605 &response);
606
8c9952b2
BT
607 if (ret > 0) {
608 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
609 __func__, ret);
610 return -EPROTO;
611 }
2f31c525 612 if (ret)
8c9952b2 613 return ret;
2f31c525 614
a5ce8f5b
SW
615 switch (response.report_id) {
616 case REPORT_ID_HIDPP_VERY_LONG:
617 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
618 break;
619 case REPORT_ID_HIDPP_LONG:
2f31c525 620 count = HIDPP_REPORT_LONG_LENGTH - 4;
a5ce8f5b
SW
621 break;
622 case REPORT_ID_HIDPP_SHORT:
2f31c525 623 count = HIDPP_REPORT_SHORT_LENGTH - 4;
a5ce8f5b
SW
624 break;
625 default:
626 return -EPROTO;
627 }
2f31c525
BT
628
629 if (len_buf < count)
630 count = len_buf;
631
632 for (i = 0; i < count; i++)
633 device_name[i] = response.fap.params[i];
634
635 return count;
636}
637
02cc097e 638static char *hidpp_get_device_name(struct hidpp_device *hidpp)
2f31c525
BT
639{
640 u8 feature_type;
641 u8 feature_index;
642 u8 __name_length;
643 char *name;
644 unsigned index = 0;
645 int ret;
646
647 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
648 &feature_index, &feature_type);
649 if (ret)
02cc097e 650 return NULL;
2f31c525
BT
651
652 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
653 &__name_length);
654 if (ret)
02cc097e 655 return NULL;
2f31c525
BT
656
657 name = kzalloc(__name_length + 1, GFP_KERNEL);
658 if (!name)
02cc097e 659 return NULL;
2f31c525 660
1430ee73
PW
661 while (index < __name_length) {
662 ret = hidpp_devicenametype_get_device_name(hidpp,
2f31c525
BT
663 feature_index, index, name + index,
664 __name_length - index);
1430ee73
PW
665 if (ret <= 0) {
666 kfree(name);
667 return NULL;
668 }
669 index += ret;
670 }
2f31c525 671
a0e625f8
BT
672 /* include the terminating '\0' */
673 hidpp_prefix_name(&name, __name_length + 1);
674
2f31c525 675 return name;
2f31c525
BT
676}
677
5a2b190c
PH
678/* -------------------------------------------------------------------------- */
679/* 0x1000: Battery level status */
680/* -------------------------------------------------------------------------- */
681
682#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
683
684#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
685#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
686
687#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
688
5b036ea1
BT
689#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
690#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
691#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
692
693static int hidpp_map_battery_level(int capacity)
694{
695 if (capacity < 11)
696 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
697 else if (capacity < 31)
698 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
699 else if (capacity < 81)
700 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
701 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
702}
703
14f437a1 704static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
5b036ea1
BT
705 int *next_capacity,
706 int *level)
5a2b190c
PH
707{
708 int status;
5a2b190c 709
14f437a1
BT
710 *capacity = data[0];
711 *next_capacity = data[1];
5b036ea1 712 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
5a2b190c 713
14f437a1
BT
714 /* When discharging, we can rely on the device reported capacity.
715 * For all other states the device reports 0 (unknown).
5a2b190c
PH
716 */
717 switch (data[2]) {
718 case 0: /* discharging (in use) */
719 status = POWER_SUPPLY_STATUS_DISCHARGING;
5b036ea1 720 *level = hidpp_map_battery_level(*capacity);
5a2b190c
PH
721 break;
722 case 1: /* recharging */
723 status = POWER_SUPPLY_STATUS_CHARGING;
5a2b190c
PH
724 break;
725 case 2: /* charge in final stage */
726 status = POWER_SUPPLY_STATUS_CHARGING;
5a2b190c
PH
727 break;
728 case 3: /* charge complete */
729 status = POWER_SUPPLY_STATUS_FULL;
5b036ea1 730 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
14f437a1 731 *capacity = 100;
5a2b190c
PH
732 break;
733 case 4: /* recharging below optimal speed */
734 status = POWER_SUPPLY_STATUS_CHARGING;
5a2b190c
PH
735 break;
736 /* 5 = invalid battery type
737 6 = thermal error
738 7 = other charging error */
739 default:
740 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
5a2b190c
PH
741 break;
742 }
743
5a2b190c
PH
744 return status;
745}
746
14f437a1
BT
747static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
748 u8 feature_index,
749 int *status,
750 int *capacity,
5b036ea1
BT
751 int *next_capacity,
752 int *level)
5a2b190c
PH
753{
754 struct hidpp_report response;
755 int ret;
756 u8 *params = (u8 *)response.fap.params;
757
758 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
759 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
760 NULL, 0, &response);
761 if (ret > 0) {
762 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
763 __func__, ret);
764 return -EPROTO;
765 }
766 if (ret)
767 return ret;
768
14f437a1 769 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
5b036ea1
BT
770 next_capacity,
771 level);
772
773 return 0;
774}
775
776static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
777 u8 feature_index)
778{
779 struct hidpp_report response;
780 int ret;
781 u8 *params = (u8 *)response.fap.params;
782 unsigned int level_count, flags;
783
784 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
785 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
786 NULL, 0, &response);
787 if (ret > 0) {
788 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
789 __func__, ret);
790 return -EPROTO;
791 }
792 if (ret)
793 return ret;
794
795 level_count = params[0];
796 flags = params[1];
797
798 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
799 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
800 else
801 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
5a2b190c
PH
802
803 return 0;
804}
805
806static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
807{
808 u8 feature_type;
809 int ret;
5b036ea1 810 int status, capacity, next_capacity, level;
5a2b190c
PH
811
812 if (hidpp->battery.feature_index == 0) {
813 ret = hidpp_root_get_feature(hidpp,
814 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
815 &hidpp->battery.feature_index,
816 &feature_type);
817 if (ret)
818 return ret;
819 }
820
14f437a1
BT
821 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
822 hidpp->battery.feature_index,
823 &status, &capacity,
5b036ea1
BT
824 &next_capacity, &level);
825 if (ret)
826 return ret;
827
828 ret = hidpp20_batterylevel_get_battery_info(hidpp,
829 hidpp->battery.feature_index);
5a2b190c
PH
830 if (ret)
831 return ret;
832
833 hidpp->battery.status = status;
14f437a1 834 hidpp->battery.capacity = capacity;
5b036ea1 835 hidpp->battery.level = level;
284f8d75
BT
836 /* the capacity is only available when discharging or full */
837 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
838 status == POWER_SUPPLY_STATUS_FULL;
5a2b190c
PH
839
840 return 0;
841}
842
843static int hidpp20_battery_event(struct hidpp_device *hidpp,
844 u8 *data, int size)
845{
846 struct hidpp_report *report = (struct hidpp_report *)data;
5b036ea1 847 int status, capacity, next_capacity, level;
5a2b190c
PH
848 bool changed;
849
850 if (report->fap.feature_index != hidpp->battery.feature_index ||
851 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
852 return 0;
853
14f437a1
BT
854 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
855 &capacity,
5b036ea1
BT
856 &next_capacity,
857 &level);
5a2b190c 858
284f8d75
BT
859 /* the capacity is only available when discharging or full */
860 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
861 status == POWER_SUPPLY_STATUS_FULL;
862
14f437a1 863 changed = capacity != hidpp->battery.capacity ||
5b036ea1 864 level != hidpp->battery.level ||
5a2b190c
PH
865 status != hidpp->battery.status;
866
867 if (changed) {
5b036ea1 868 hidpp->battery.level = level;
14f437a1 869 hidpp->battery.capacity = capacity;
5a2b190c
PH
870 hidpp->battery.status = status;
871 if (hidpp->battery.ps)
872 power_supply_changed(hidpp->battery.ps);
873 }
874
875 return 0;
876}
877
878static enum power_supply_property hidpp_battery_props[] = {
284f8d75 879 POWER_SUPPLY_PROP_ONLINE,
5a2b190c 880 POWER_SUPPLY_PROP_STATUS,
3861e6ca 881 POWER_SUPPLY_PROP_SCOPE,
32043d0f
BT
882 POWER_SUPPLY_PROP_MODEL_NAME,
883 POWER_SUPPLY_PROP_MANUFACTURER,
884 POWER_SUPPLY_PROP_SERIAL_NUMBER,
5b036ea1
BT
885 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
886 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
5a2b190c
PH
887};
888
889static int hidpp_battery_get_property(struct power_supply *psy,
890 enum power_supply_property psp,
891 union power_supply_propval *val)
892{
893 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
894 int ret = 0;
895
896 switch(psp) {
897 case POWER_SUPPLY_PROP_STATUS:
898 val->intval = hidpp->battery.status;
899 break;
900 case POWER_SUPPLY_PROP_CAPACITY:
14f437a1 901 val->intval = hidpp->battery.capacity;
5a2b190c 902 break;
5b036ea1
BT
903 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
904 val->intval = hidpp->battery.level;
905 break;
3861e6ca
BN
906 case POWER_SUPPLY_PROP_SCOPE:
907 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
908 break;
284f8d75
BT
909 case POWER_SUPPLY_PROP_ONLINE:
910 val->intval = hidpp->battery.online;
911 break;
32043d0f
BT
912 case POWER_SUPPLY_PROP_MODEL_NAME:
913 if (!strncmp(hidpp->name, "Logitech ", 9))
914 val->strval = hidpp->name + 9;
915 else
916 val->strval = hidpp->name;
917 break;
918 case POWER_SUPPLY_PROP_MANUFACTURER:
919 val->strval = "Logitech";
920 break;
921 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
922 val->strval = hidpp->hid_dev->uniq;
923 break;
5a2b190c
PH
924 default:
925 ret = -EINVAL;
926 break;
927 }
928
929 return ret;
930}
931
90cdd986
BT
932/* -------------------------------------------------------------------------- */
933/* 0x6010: Touchpad FW items */
934/* -------------------------------------------------------------------------- */
935
936#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
937
938#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
939
940struct hidpp_touchpad_fw_items {
941 uint8_t presence;
942 uint8_t desired_state;
943 uint8_t state;
944 uint8_t persistent;
945};
946
947/**
948 * send a set state command to the device by reading the current items->state
949 * field. items is then filled with the current state.
950 */
951static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
952 u8 feature_index,
953 struct hidpp_touchpad_fw_items *items)
954{
955 struct hidpp_report response;
956 int ret;
957 u8 *params = (u8 *)response.fap.params;
958
959 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
960 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
961
962 if (ret > 0) {
963 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
964 __func__, ret);
965 return -EPROTO;
966 }
967 if (ret)
968 return ret;
969
970 items->presence = params[0];
971 items->desired_state = params[1];
972 items->state = params[2];
973 items->persistent = params[3];
974
975 return 0;
976}
977
2f31c525
BT
978/* -------------------------------------------------------------------------- */
979/* 0x6100: TouchPadRawXY */
980/* -------------------------------------------------------------------------- */
981
982#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
983
984#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
586bdc4e
BT
985#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
986
987#define EVENT_TOUCHPAD_RAW_XY 0x00
2f31c525
BT
988
989#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
990#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
991
992struct hidpp_touchpad_raw_info {
993 u16 x_size;
994 u16 y_size;
995 u8 z_range;
996 u8 area_range;
997 u8 timestamp_unit;
998 u8 maxcontacts;
999 u8 origin;
1000 u16 res;
1001};
1002
1003struct hidpp_touchpad_raw_xy_finger {
1004 u8 contact_type;
1005 u8 contact_status;
1006 u16 x;
1007 u16 y;
1008 u8 z;
1009 u8 area;
1010 u8 finger_id;
1011};
1012
1013struct hidpp_touchpad_raw_xy {
1014 u16 timestamp;
1015 struct hidpp_touchpad_raw_xy_finger fingers[2];
1016 u8 spurious_flag;
1017 u8 end_of_frame;
1018 u8 finger_count;
1019 u8 button;
1020};
1021
1022static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1023 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1024{
1025 struct hidpp_report response;
1026 int ret;
1027 u8 *params = (u8 *)response.fap.params;
1028
1029 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1030 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1031
8c9952b2
BT
1032 if (ret > 0) {
1033 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1034 __func__, ret);
1035 return -EPROTO;
1036 }
2f31c525 1037 if (ret)
8c9952b2 1038 return ret;
2f31c525
BT
1039
1040 raw_info->x_size = get_unaligned_be16(&params[0]);
1041 raw_info->y_size = get_unaligned_be16(&params[2]);
1042 raw_info->z_range = params[4];
1043 raw_info->area_range = params[5];
1044 raw_info->maxcontacts = params[7];
1045 raw_info->origin = params[8];
1046 /* res is given in unit per inch */
1047 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1048
1049 return ret;
1050}
1051
586bdc4e
BT
1052static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1053 u8 feature_index, bool send_raw_reports,
1054 bool sensor_enhanced_settings)
1055{
1056 struct hidpp_report response;
1057
1058 /*
1059 * Params:
1060 * bit 0 - enable raw
1061 * bit 1 - 16bit Z, no area
1062 * bit 2 - enhanced sensitivity
1063 * bit 3 - width, height (4 bits each) instead of area
1064 * bit 4 - send raw + gestures (degrades smoothness)
1065 * remaining bits - reserved
1066 */
1067 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1068
1069 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1070 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1071}
1072
1073static void hidpp_touchpad_touch_event(u8 *data,
1074 struct hidpp_touchpad_raw_xy_finger *finger)
1075{
1076 u8 x_m = data[0] << 2;
1077 u8 y_m = data[2] << 2;
1078
1079 finger->x = x_m << 6 | data[1];
1080 finger->y = y_m << 6 | data[3];
1081
1082 finger->contact_type = data[0] >> 6;
1083 finger->contact_status = data[2] >> 6;
1084
1085 finger->z = data[4];
1086 finger->area = data[5];
1087 finger->finger_id = data[6] >> 4;
1088}
1089
1090static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1091 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1092{
1093 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1094 raw_xy->end_of_frame = data[8] & 0x01;
1095 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1096 raw_xy->finger_count = data[15] & 0x0f;
1097 raw_xy->button = (data[8] >> 2) & 0x01;
1098
1099 if (raw_xy->finger_count) {
1100 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1101 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1102 }
1103}
1104
ff21a635
EV
1105/* -------------------------------------------------------------------------- */
1106/* 0x8123: Force feedback support */
1107/* -------------------------------------------------------------------------- */
1108
1109#define HIDPP_FF_GET_INFO 0x01
1110#define HIDPP_FF_RESET_ALL 0x11
1111#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1112#define HIDPP_FF_SET_EFFECT_STATE 0x31
1113#define HIDPP_FF_DESTROY_EFFECT 0x41
1114#define HIDPP_FF_GET_APERTURE 0x51
1115#define HIDPP_FF_SET_APERTURE 0x61
1116#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1117#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1118
1119#define HIDPP_FF_EFFECT_STATE_GET 0x00
1120#define HIDPP_FF_EFFECT_STATE_STOP 0x01
1121#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1122#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1123
1124#define HIDPP_FF_EFFECT_CONSTANT 0x00
1125#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1126#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1127#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1128#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1129#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1130#define HIDPP_FF_EFFECT_SPRING 0x06
1131#define HIDPP_FF_EFFECT_DAMPER 0x07
1132#define HIDPP_FF_EFFECT_FRICTION 0x08
1133#define HIDPP_FF_EFFECT_INERTIA 0x09
1134#define HIDPP_FF_EFFECT_RAMP 0x0A
1135
1136#define HIDPP_FF_EFFECT_AUTOSTART 0x80
1137
1138#define HIDPP_FF_EFFECTID_NONE -1
1139#define HIDPP_FF_EFFECTID_AUTOCENTER -2
1140
1141#define HIDPP_FF_MAX_PARAMS 20
1142#define HIDPP_FF_RESERVED_SLOTS 1
1143
1144struct hidpp_ff_private_data {
1145 struct hidpp_device *hidpp;
1146 u8 feature_index;
1147 u8 version;
1148 u16 gain;
1149 s16 range;
1150 u8 slot_autocenter;
1151 u8 num_effects;
1152 int *effect_ids;
1153 struct workqueue_struct *wq;
1154 atomic_t workqueue_size;
1155};
1156
1157struct hidpp_ff_work_data {
1158 struct work_struct work;
1159 struct hidpp_ff_private_data *data;
1160 int effect_id;
1161 u8 command;
1162 u8 params[HIDPP_FF_MAX_PARAMS];
1163 u8 size;
1164};
1165
1166static const signed short hiddpp_ff_effects[] = {
1167 FF_CONSTANT,
1168 FF_PERIODIC,
1169 FF_SINE,
1170 FF_SQUARE,
1171 FF_SAW_UP,
1172 FF_SAW_DOWN,
1173 FF_TRIANGLE,
1174 FF_SPRING,
1175 FF_DAMPER,
1176 FF_AUTOCENTER,
1177 FF_GAIN,
1178 -1
1179};
1180
1181static const signed short hiddpp_ff_effects_v2[] = {
1182 FF_RAMP,
1183 FF_FRICTION,
1184 FF_INERTIA,
1185 -1
1186};
1187
1188static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1189 HIDPP_FF_EFFECT_SPRING,
1190 HIDPP_FF_EFFECT_FRICTION,
1191 HIDPP_FF_EFFECT_DAMPER,
1192 HIDPP_FF_EFFECT_INERTIA
1193};
1194
1195static const char *HIDPP_FF_CONDITION_NAMES[] = {
1196 "spring",
1197 "friction",
1198 "damper",
1199 "inertia"
1200};
1201
1202
1203static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1204{
1205 int i;
1206
1207 for (i = 0; i < data->num_effects; i++)
1208 if (data->effect_ids[i] == effect_id)
1209 return i+1;
1210
1211 return 0;
1212}
1213
1214static void hidpp_ff_work_handler(struct work_struct *w)
1215{
1216 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1217 struct hidpp_ff_private_data *data = wd->data;
1218 struct hidpp_report response;
1219 u8 slot;
1220 int ret;
1221
1222 /* add slot number if needed */
1223 switch (wd->effect_id) {
1224 case HIDPP_FF_EFFECTID_AUTOCENTER:
1225 wd->params[0] = data->slot_autocenter;
1226 break;
1227 case HIDPP_FF_EFFECTID_NONE:
1228 /* leave slot as zero */
1229 break;
1230 default:
1231 /* find current slot for effect */
1232 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1233 break;
1234 }
1235
1236 /* send command and wait for reply */
1237 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1238 wd->command, wd->params, wd->size, &response);
1239
1240 if (ret) {
1241 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1242 goto out;
1243 }
1244
1245 /* parse return data */
1246 switch (wd->command) {
1247 case HIDPP_FF_DOWNLOAD_EFFECT:
1248 slot = response.fap.params[0];
1249 if (slot > 0 && slot <= data->num_effects) {
1250 if (wd->effect_id >= 0)
1251 /* regular effect uploaded */
1252 data->effect_ids[slot-1] = wd->effect_id;
1253 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1254 /* autocenter spring uploaded */
1255 data->slot_autocenter = slot;
1256 }
1257 break;
1258 case HIDPP_FF_DESTROY_EFFECT:
1259 if (wd->effect_id >= 0)
1260 /* regular effect destroyed */
1261 data->effect_ids[wd->params[0]-1] = -1;
1262 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1263 /* autocenter spring destoyed */
1264 data->slot_autocenter = 0;
1265 break;
1266 case HIDPP_FF_SET_GLOBAL_GAINS:
1267 data->gain = (wd->params[0] << 8) + wd->params[1];
1268 break;
1269 case HIDPP_FF_SET_APERTURE:
1270 data->range = (wd->params[0] << 8) + wd->params[1];
1271 break;
1272 default:
1273 /* no action needed */
1274 break;
1275 }
1276
1277out:
1278 atomic_dec(&data->workqueue_size);
1279 kfree(wd);
1280}
1281
1282static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1283{
1284 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1285 int s;
1286
1287 if (!wd)
1288 return -ENOMEM;
1289
1290 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1291
1292 wd->data = data;
1293 wd->effect_id = effect_id;
1294 wd->command = command;
1295 wd->size = size;
1296 memcpy(wd->params, params, size);
1297
1298 atomic_inc(&data->workqueue_size);
1299 queue_work(data->wq, &wd->work);
1300
1301 /* warn about excessive queue size */
1302 s = atomic_read(&data->workqueue_size);
1303 if (s >= 20 && s % 20 == 0)
1304 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1305
1306 return 0;
1307}
1308
1309static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1310{
1311 struct hidpp_ff_private_data *data = dev->ff->private;
1312 u8 params[20];
1313 u8 size;
1314 int force;
1315
1316 /* set common parameters */
1317 params[2] = effect->replay.length >> 8;
1318 params[3] = effect->replay.length & 255;
1319 params[4] = effect->replay.delay >> 8;
1320 params[5] = effect->replay.delay & 255;
1321
1322 switch (effect->type) {
1323 case FF_CONSTANT:
1324 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1325 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1326 params[6] = force >> 8;
1327 params[7] = force & 255;
1328 params[8] = effect->u.constant.envelope.attack_level >> 7;
1329 params[9] = effect->u.constant.envelope.attack_length >> 8;
1330 params[10] = effect->u.constant.envelope.attack_length & 255;
1331 params[11] = effect->u.constant.envelope.fade_level >> 7;
1332 params[12] = effect->u.constant.envelope.fade_length >> 8;
1333 params[13] = effect->u.constant.envelope.fade_length & 255;
1334 size = 14;
1335 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1336 effect->u.constant.level,
1337 effect->direction, force);
1338 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1339 effect->u.constant.envelope.attack_level,
1340 effect->u.constant.envelope.attack_length,
1341 effect->u.constant.envelope.fade_level,
1342 effect->u.constant.envelope.fade_length);
1343 break;
1344 case FF_PERIODIC:
1345 {
1346 switch (effect->u.periodic.waveform) {
1347 case FF_SINE:
1348 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1349 break;
1350 case FF_SQUARE:
1351 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1352 break;
1353 case FF_SAW_UP:
1354 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1355 break;
1356 case FF_SAW_DOWN:
1357 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1358 break;
1359 case FF_TRIANGLE:
1360 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1361 break;
1362 default:
1363 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1364 return -EINVAL;
1365 }
1366 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1367 params[6] = effect->u.periodic.magnitude >> 8;
1368 params[7] = effect->u.periodic.magnitude & 255;
1369 params[8] = effect->u.periodic.offset >> 8;
1370 params[9] = effect->u.periodic.offset & 255;
1371 params[10] = effect->u.periodic.period >> 8;
1372 params[11] = effect->u.periodic.period & 255;
1373 params[12] = effect->u.periodic.phase >> 8;
1374 params[13] = effect->u.periodic.phase & 255;
1375 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1376 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1377 params[16] = effect->u.periodic.envelope.attack_length & 255;
1378 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1379 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1380 params[19] = effect->u.periodic.envelope.fade_length & 255;
1381 size = 20;
1382 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1383 effect->u.periodic.magnitude, effect->direction,
1384 effect->u.periodic.offset,
1385 effect->u.periodic.period,
1386 effect->u.periodic.phase);
1387 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1388 effect->u.periodic.envelope.attack_level,
1389 effect->u.periodic.envelope.attack_length,
1390 effect->u.periodic.envelope.fade_level,
1391 effect->u.periodic.envelope.fade_length);
1392 break;
1393 }
1394 case FF_RAMP:
1395 params[1] = HIDPP_FF_EFFECT_RAMP;
1396 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1397 params[6] = force >> 8;
1398 params[7] = force & 255;
1399 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1400 params[8] = force >> 8;
1401 params[9] = force & 255;
1402 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1403 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1404 params[12] = effect->u.ramp.envelope.attack_length & 255;
1405 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1406 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1407 params[15] = effect->u.ramp.envelope.fade_length & 255;
1408 size = 16;
1409 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1410 effect->u.ramp.start_level,
1411 effect->u.ramp.end_level,
1412 effect->direction, force);
1413 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1414 effect->u.ramp.envelope.attack_level,
1415 effect->u.ramp.envelope.attack_length,
1416 effect->u.ramp.envelope.fade_level,
1417 effect->u.ramp.envelope.fade_length);
1418 break;
1419 case FF_FRICTION:
1420 case FF_INERTIA:
1421 case FF_SPRING:
1422 case FF_DAMPER:
1423 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1424 params[6] = effect->u.condition[0].left_saturation >> 9;
1425 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1426 params[8] = effect->u.condition[0].left_coeff >> 8;
1427 params[9] = effect->u.condition[0].left_coeff & 255;
1428 params[10] = effect->u.condition[0].deadband >> 9;
1429 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1430 params[12] = effect->u.condition[0].center >> 8;
1431 params[13] = effect->u.condition[0].center & 255;
1432 params[14] = effect->u.condition[0].right_coeff >> 8;
1433 params[15] = effect->u.condition[0].right_coeff & 255;
1434 params[16] = effect->u.condition[0].right_saturation >> 9;
1435 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1436 size = 18;
1437 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1438 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1439 effect->u.condition[0].left_coeff,
1440 effect->u.condition[0].left_saturation,
1441 effect->u.condition[0].right_coeff,
1442 effect->u.condition[0].right_saturation);
1443 dbg_hid(" deadband=%d, center=%d\n",
1444 effect->u.condition[0].deadband,
1445 effect->u.condition[0].center);
1446 break;
1447 default:
1448 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1449 return -EINVAL;
1450 }
1451
1452 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1453}
1454
1455static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1456{
1457 struct hidpp_ff_private_data *data = dev->ff->private;
1458 u8 params[2];
1459
1460 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1461
1462 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1463
1464 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1465}
1466
1467static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1468{
1469 struct hidpp_ff_private_data *data = dev->ff->private;
1470 u8 slot = 0;
1471
1472 dbg_hid("Erasing effect %d.\n", effect_id);
1473
1474 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1475}
1476
1477static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1478{
1479 struct hidpp_ff_private_data *data = dev->ff->private;
1480 u8 params[18];
1481
1482 dbg_hid("Setting autocenter to %d.\n", magnitude);
1483
1484 /* start a standard spring effect */
1485 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1486 /* zero delay and duration */
1487 params[2] = params[3] = params[4] = params[5] = 0;
1488 /* set coeff to 25% of saturation */
1489 params[8] = params[14] = magnitude >> 11;
1490 params[9] = params[15] = (magnitude >> 3) & 255;
1491 params[6] = params[16] = magnitude >> 9;
1492 params[7] = params[17] = (magnitude >> 1) & 255;
1493 /* zero deadband and center */
1494 params[10] = params[11] = params[12] = params[13] = 0;
1495
1496 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1497}
1498
1499static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1500{
1501 struct hidpp_ff_private_data *data = dev->ff->private;
1502 u8 params[4];
1503
1504 dbg_hid("Setting gain to %d.\n", gain);
1505
1506 params[0] = gain >> 8;
1507 params[1] = gain & 255;
1508 params[2] = 0; /* no boost */
1509 params[3] = 0;
1510
1511 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1512}
1513
1514static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1515{
1516 struct hid_device *hid = to_hid_device(dev);
1517 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1518 struct input_dev *idev = hidinput->input;
1519 struct hidpp_ff_private_data *data = idev->ff->private;
1520
1521 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1522}
1523
1524static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1525{
1526 struct hid_device *hid = to_hid_device(dev);
1527 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1528 struct input_dev *idev = hidinput->input;
1529 struct hidpp_ff_private_data *data = idev->ff->private;
1530 u8 params[2];
1531 int range = simple_strtoul(buf, NULL, 10);
1532
1533 range = clamp(range, 180, 900);
1534
1535 params[0] = range >> 8;
1536 params[1] = range & 0x00FF;
1537
1538 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1539
1540 return count;
1541}
1542
1543static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1544
1545static void hidpp_ff_destroy(struct ff_device *ff)
1546{
1547 struct hidpp_ff_private_data *data = ff->private;
1548
1549 kfree(data->effect_ids);
1550}
1551
af2e628d 1552static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
ff21a635
EV
1553{
1554 struct hid_device *hid = hidpp->hid_dev;
1555 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1556 struct input_dev *dev = hidinput->input;
1557 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1558 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1559 struct ff_device *ff;
1560 struct hidpp_report response;
1561 struct hidpp_ff_private_data *data;
1562 int error, j, num_slots;
1563 u8 version;
1564
1565 if (!dev) {
1566 hid_err(hid, "Struct input_dev not set!\n");
1567 return -EINVAL;
1568 }
1569
1570 /* Get firmware release */
1571 version = bcdDevice & 255;
1572
1573 /* Set supported force feedback capabilities */
1574 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1575 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1576 if (version > 1)
1577 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1578 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1579
1580 /* Read number of slots available in device */
1581 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1582 HIDPP_FF_GET_INFO, NULL, 0, &response);
1583 if (error) {
1584 if (error < 0)
1585 return error;
1586 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1587 __func__, error);
1588 return -EPROTO;
1589 }
1590
1591 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1592
1593 error = input_ff_create(dev, num_slots);
1594
1595 if (error) {
1596 hid_err(dev, "Failed to create FF device!\n");
1597 return error;
1598 }
1599
1600 data = kzalloc(sizeof(*data), GFP_KERNEL);
1601 if (!data)
1602 return -ENOMEM;
1603 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1604 if (!data->effect_ids) {
1605 kfree(data);
1606 return -ENOMEM;
1607 }
1608 data->hidpp = hidpp;
1609 data->feature_index = feature_index;
1610 data->version = version;
1611 data->slot_autocenter = 0;
1612 data->num_effects = num_slots;
1613 for (j = 0; j < num_slots; j++)
1614 data->effect_ids[j] = -1;
1615
1616 ff = dev->ff;
1617 ff->private = data;
1618
1619 ff->upload = hidpp_ff_upload_effect;
1620 ff->erase = hidpp_ff_erase_effect;
1621 ff->playback = hidpp_ff_playback;
1622 ff->set_gain = hidpp_ff_set_gain;
1623 ff->set_autocenter = hidpp_ff_set_autocenter;
1624 ff->destroy = hidpp_ff_destroy;
1625
1626
1627 /* reset all forces */
1628 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1629 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1630
1631 /* Read current Range */
1632 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1633 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1634 if (error)
1635 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1636 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1637
1638 /* Create sysfs interface */
1639 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1640 if (error)
1641 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1642
1643 /* Read the current gain values */
1644 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1645 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1646 if (error)
1647 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1648 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1649 /* ignore boost value at response.fap.params[2] */
1650
1651 /* init the hardware command queue */
1652 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1653 atomic_set(&data->workqueue_size, 0);
1654
1655 /* initialize with zero autocenter to get wheel in usable state */
1656 hidpp_ff_set_autocenter(dev, 0);
1657
1658 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1659
1660 return 0;
1661}
1662
af2e628d 1663static int hidpp_ff_deinit(struct hid_device *hid)
ff21a635
EV
1664{
1665 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1666 struct input_dev *dev = hidinput->input;
1667 struct hidpp_ff_private_data *data;
1668
1669 if (!dev) {
1670 hid_err(hid, "Struct input_dev not found!\n");
1671 return -EINVAL;
1672 }
1673
1674 hid_info(hid, "Unloading HID++ force feedback.\n");
1675 data = dev->ff->private;
1676 if (!data) {
1677 hid_err(hid, "Private data not found!\n");
1678 return -EINVAL;
1679 }
1680
1681 destroy_workqueue(data->wq);
1682 device_remove_file(&hid->dev, &dev_attr_range);
1683
1684 return 0;
1685}
1686
1687
2f31c525
BT
1688/* ************************************************************************** */
1689/* */
1690/* Device Support */
1691/* */
1692/* ************************************************************************** */
1693
1694/* -------------------------------------------------------------------------- */
1695/* Touchpad HID++ devices */
1696/* -------------------------------------------------------------------------- */
1697
57ac86cf
BT
1698#define WTP_MANUAL_RESOLUTION 39
1699
2f31c525
BT
1700struct wtp_data {
1701 struct input_dev *input;
1702 u16 x_size, y_size;
1703 u8 finger_count;
1704 u8 mt_feature_index;
1705 u8 button_feature_index;
1706 u8 maxcontacts;
1707 bool flip_y;
1708 unsigned int resolution;
1709};
1710
1711static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1712 struct hid_field *field, struct hid_usage *usage,
1713 unsigned long **bit, int *max)
1714{
1715 return -1;
1716}
1717
c39e3d5f
BT
1718static void wtp_populate_input(struct hidpp_device *hidpp,
1719 struct input_dev *input_dev, bool origin_is_hid_core)
2f31c525 1720{
2f31c525 1721 struct wtp_data *wd = hidpp->private_data;
2f31c525
BT
1722
1723 __set_bit(EV_ABS, input_dev->evbit);
1724 __set_bit(EV_KEY, input_dev->evbit);
1725 __clear_bit(EV_REL, input_dev->evbit);
1726 __clear_bit(EV_LED, input_dev->evbit);
1727
1728 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
1729 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
1730 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
1731 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
1732
1733 /* Max pressure is not given by the devices, pick one */
1734 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
1735
1736 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
1737
57ac86cf
BT
1738 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
1739 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
1740 else
1741 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2f31c525
BT
1742
1743 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
1744 INPUT_MT_DROP_UNUSED);
1745
1746 wd->input = input_dev;
1747}
1748
1749static void wtp_touch_event(struct wtp_data *wd,
1750 struct hidpp_touchpad_raw_xy_finger *touch_report)
1751{
1752 int slot;
1753
1754 if (!touch_report->finger_id || touch_report->contact_type)
1755 /* no actual data */
1756 return;
1757
1758 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
1759
1760 input_mt_slot(wd->input, slot);
1761 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
1762 touch_report->contact_status);
1763 if (touch_report->contact_status) {
1764 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
1765 touch_report->x);
1766 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
1767 wd->flip_y ? wd->y_size - touch_report->y :
1768 touch_report->y);
1769 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
1770 touch_report->area);
1771 }
1772}
1773
1774static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
1775 struct hidpp_touchpad_raw_xy *raw)
1776{
1777 struct wtp_data *wd = hidpp->private_data;
1778 int i;
1779
1780 for (i = 0; i < 2; i++)
1781 wtp_touch_event(wd, &(raw->fingers[i]));
1782
57ac86cf
BT
1783 if (raw->end_of_frame &&
1784 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2f31c525
BT
1785 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
1786
1787 if (raw->end_of_frame || raw->finger_count <= 2) {
1788 input_mt_sync_frame(wd->input);
1789 input_sync(wd->input);
1790 }
1791}
1792
1793static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
1794{
1795 struct wtp_data *wd = hidpp->private_data;
1796 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
1797 (data[7] >> 4) * (data[7] >> 4)) / 2;
1798 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
1799 (data[13] >> 4) * (data[13] >> 4)) / 2;
1800 struct hidpp_touchpad_raw_xy raw = {
1801 .timestamp = data[1],
1802 .fingers = {
1803 {
1804 .contact_type = 0,
1805 .contact_status = !!data[7],
1806 .x = get_unaligned_le16(&data[3]),
1807 .y = get_unaligned_le16(&data[5]),
1808 .z = c1_area,
1809 .area = c1_area,
1810 .finger_id = data[2],
1811 }, {
1812 .contact_type = 0,
1813 .contact_status = !!data[13],
1814 .x = get_unaligned_le16(&data[9]),
1815 .y = get_unaligned_le16(&data[11]),
1816 .z = c2_area,
1817 .area = c2_area,
1818 .finger_id = data[8],
1819 }
1820 },
1821 .finger_count = wd->maxcontacts,
1822 .spurious_flag = 0,
1823 .end_of_frame = (data[0] >> 7) == 0,
1824 .button = data[0] & 0x01,
1825 };
1826
1827 wtp_send_raw_xy_event(hidpp, &raw);
1828
1829 return 1;
1830}
1831
1832static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
1833{
1834 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1835 struct wtp_data *wd = hidpp->private_data;
586bdc4e
BT
1836 struct hidpp_report *report = (struct hidpp_report *)data;
1837 struct hidpp_touchpad_raw_xy raw;
2f31c525 1838
586bdc4e 1839 if (!wd || !wd->input)
2f31c525
BT
1840 return 1;
1841
586bdc4e
BT
1842 switch (data[0]) {
1843 case 0x02:
0b3f6569
PW
1844 if (size < 2) {
1845 hid_err(hdev, "Received HID report of bad size (%d)",
1846 size);
1847 return 1;
1848 }
57ac86cf
BT
1849 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
1850 input_event(wd->input, EV_KEY, BTN_LEFT,
1851 !!(data[1] & 0x01));
1852 input_event(wd->input, EV_KEY, BTN_RIGHT,
1853 !!(data[1] & 0x02));
1854 input_sync(wd->input);
8abd8205 1855 return 0;
57ac86cf
BT
1856 } else {
1857 if (size < 21)
1858 return 1;
1859 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
1860 }
586bdc4e 1861 case REPORT_ID_HIDPP_LONG:
0b3f6569 1862 /* size is already checked in hidpp_raw_event. */
586bdc4e
BT
1863 if ((report->fap.feature_index != wd->mt_feature_index) ||
1864 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
1865 return 1;
1866 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
1867
1868 wtp_send_raw_xy_event(hidpp, &raw);
1869 return 0;
1870 }
1871
1872 return 0;
2f31c525
BT
1873}
1874
1875static int wtp_get_config(struct hidpp_device *hidpp)
1876{
1877 struct wtp_data *wd = hidpp->private_data;
1878 struct hidpp_touchpad_raw_info raw_info = {0};
1879 u8 feature_type;
1880 int ret;
1881
1882 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
1883 &wd->mt_feature_index, &feature_type);
1884 if (ret)
1885 /* means that the device is not powered up */
1886 return ret;
1887
1888 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
1889 &raw_info);
1890 if (ret)
1891 return ret;
1892
1893 wd->x_size = raw_info.x_size;
1894 wd->y_size = raw_info.y_size;
1895 wd->maxcontacts = raw_info.maxcontacts;
1896 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
1897 wd->resolution = raw_info.res;
57ac86cf
BT
1898 if (!wd->resolution)
1899 wd->resolution = WTP_MANUAL_RESOLUTION;
2f31c525
BT
1900
1901 return 0;
1902}
1903
1904static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
1905{
1906 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1907 struct wtp_data *wd;
1908
1909 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
1910 GFP_KERNEL);
1911 if (!wd)
1912 return -ENOMEM;
1913
1914 hidpp->private_data = wd;
1915
1916 return 0;
1917};
1918
bf159447 1919static int wtp_connect(struct hid_device *hdev, bool connected)
586bdc4e
BT
1920{
1921 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1922 struct wtp_data *wd = hidpp->private_data;
1923 int ret;
1924
586bdc4e
BT
1925 if (!wd->x_size) {
1926 ret = wtp_get_config(hidpp);
1927 if (ret) {
1928 hid_err(hdev, "Can not get wtp config: %d\n", ret);
bf159447 1929 return ret;
586bdc4e
BT
1930 }
1931 }
1932
bf159447 1933 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
586bdc4e
BT
1934 true, true);
1935}
1936
8a09b4fa
GB
1937/* ------------------------------------------------------------------------- */
1938/* Logitech M560 devices */
1939/* ------------------------------------------------------------------------- */
1940
1941/*
1942 * Logitech M560 protocol overview
1943 *
1944 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1945 * the sides buttons are pressed, it sends some keyboard keys events
1946 * instead of buttons ones.
1947 * To complicate things further, the middle button keys sequence
1948 * is different from the odd press and the even press.
1949 *
1950 * forward button -> Super_R
1951 * backward button -> Super_L+'d' (press only)
1952 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1953 * 2nd time: left-click (press only)
1954 * NB: press-only means that when the button is pressed, the
1955 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1956 * together sequentially; instead when the button is released, no event is
1957 * generated !
1958 *
1959 * With the command
1960 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1961 * the mouse reacts differently:
1962 * - it never sends a keyboard key event
1963 * - for the three mouse button it sends:
1964 * middle button press 11<xx>0a 3500af00...
1965 * side 1 button (forward) press 11<xx>0a 3500b000...
1966 * side 2 button (backward) press 11<xx>0a 3500ae00...
1967 * middle/side1/side2 button release 11<xx>0a 35000000...
1968 */
1969
1970static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1971
1972struct m560_private_data {
1973 struct input_dev *input;
1974};
1975
1976/* how buttons are mapped in the report */
1977#define M560_MOUSE_BTN_LEFT 0x01
1978#define M560_MOUSE_BTN_RIGHT 0x02
1979#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1980#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1981
1982#define M560_SUB_ID 0x0a
1983#define M560_BUTTON_MODE_REGISTER 0x35
1984
1985static int m560_send_config_command(struct hid_device *hdev, bool connected)
1986{
1987 struct hidpp_report response;
1988 struct hidpp_device *hidpp_dev;
1989
1990 hidpp_dev = hid_get_drvdata(hdev);
1991
8a09b4fa
GB
1992 return hidpp_send_rap_command_sync(
1993 hidpp_dev,
1994 REPORT_ID_HIDPP_SHORT,
1995 M560_SUB_ID,
1996 M560_BUTTON_MODE_REGISTER,
1997 (u8 *)m560_config_parameter,
1998 sizeof(m560_config_parameter),
1999 &response
2000 );
2001}
2002
2003static int m560_allocate(struct hid_device *hdev)
2004{
2005 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2006 struct m560_private_data *d;
2007
2008 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2009 GFP_KERNEL);
2010 if (!d)
2011 return -ENOMEM;
2012
2013 hidpp->private_data = d;
2014
2015 return 0;
2016};
2017
2018static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2019{
2020 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2021 struct m560_private_data *mydata = hidpp->private_data;
2022
2023 /* sanity check */
2024 if (!mydata || !mydata->input) {
2025 hid_err(hdev, "error in parameter\n");
2026 return -EINVAL;
2027 }
2028
2029 if (size < 7) {
2030 hid_err(hdev, "error in report\n");
2031 return 0;
2032 }
2033
2034 if (data[0] == REPORT_ID_HIDPP_LONG &&
2035 data[2] == M560_SUB_ID && data[6] == 0x00) {
2036 /*
2037 * m560 mouse report for middle, forward and backward button
2038 *
2039 * data[0] = 0x11
2040 * data[1] = device-id
2041 * data[2] = 0x0a
2042 * data[5] = 0xaf -> middle
2043 * 0xb0 -> forward
2044 * 0xae -> backward
2045 * 0x00 -> release all
2046 * data[6] = 0x00
2047 */
2048
2049 switch (data[5]) {
2050 case 0xaf:
2051 input_report_key(mydata->input, BTN_MIDDLE, 1);
2052 break;
2053 case 0xb0:
2054 input_report_key(mydata->input, BTN_FORWARD, 1);
2055 break;
2056 case 0xae:
2057 input_report_key(mydata->input, BTN_BACK, 1);
2058 break;
2059 case 0x00:
2060 input_report_key(mydata->input, BTN_BACK, 0);
2061 input_report_key(mydata->input, BTN_FORWARD, 0);
2062 input_report_key(mydata->input, BTN_MIDDLE, 0);
2063 break;
2064 default:
2065 hid_err(hdev, "error in report\n");
2066 return 0;
2067 }
2068 input_sync(mydata->input);
2069
2070 } else if (data[0] == 0x02) {
2071 /*
2072 * Logitech M560 mouse report
2073 *
2074 * data[0] = type (0x02)
2075 * data[1..2] = buttons
2076 * data[3..5] = xy
2077 * data[6] = wheel
2078 */
2079
2080 int v;
2081
2082 input_report_key(mydata->input, BTN_LEFT,
2083 !!(data[1] & M560_MOUSE_BTN_LEFT));
2084 input_report_key(mydata->input, BTN_RIGHT,
2085 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2086
2087 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2088 input_report_rel(mydata->input, REL_HWHEEL, -1);
2089 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2090 input_report_rel(mydata->input, REL_HWHEEL, 1);
2091
2092 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2093 input_report_rel(mydata->input, REL_X, v);
2094
2095 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2096 input_report_rel(mydata->input, REL_Y, v);
2097
2098 v = hid_snto32(data[6], 8);
2099 input_report_rel(mydata->input, REL_WHEEL, v);
2100
2101 input_sync(mydata->input);
2102 }
2103
2104 return 1;
2105}
2106
2107static void m560_populate_input(struct hidpp_device *hidpp,
2108 struct input_dev *input_dev, bool origin_is_hid_core)
2109{
2110 struct m560_private_data *mydata = hidpp->private_data;
2111
2112 mydata->input = input_dev;
2113
2114 __set_bit(EV_KEY, mydata->input->evbit);
2115 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2116 __set_bit(BTN_RIGHT, mydata->input->keybit);
2117 __set_bit(BTN_LEFT, mydata->input->keybit);
2118 __set_bit(BTN_BACK, mydata->input->keybit);
2119 __set_bit(BTN_FORWARD, mydata->input->keybit);
2120
2121 __set_bit(EV_REL, mydata->input->evbit);
2122 __set_bit(REL_X, mydata->input->relbit);
2123 __set_bit(REL_Y, mydata->input->relbit);
2124 __set_bit(REL_WHEEL, mydata->input->relbit);
2125 __set_bit(REL_HWHEEL, mydata->input->relbit);
2126}
2127
2128static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2129 struct hid_field *field, struct hid_usage *usage,
2130 unsigned long **bit, int *max)
2131{
2132 return -1;
2133}
2134
90cdd986
BT
2135/* ------------------------------------------------------------------------- */
2136/* Logitech K400 devices */
2137/* ------------------------------------------------------------------------- */
2138
2139/*
2140 * The Logitech K400 keyboard has an embedded touchpad which is seen
2141 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2142 * tap-to-click but the setting is not remembered accross reset, annoying some
2143 * users.
2144 *
2145 * We can toggle this feature from the host by using the feature 0x6010:
2146 * Touchpad FW items
2147 */
2148
2149struct k400_private_data {
2150 u8 feature_index;
2151};
2152
2153static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2154{
2155 struct k400_private_data *k400 = hidpp->private_data;
2156 struct hidpp_touchpad_fw_items items = {};
2157 int ret;
2158 u8 feature_type;
2159
2160 if (!k400->feature_index) {
2161 ret = hidpp_root_get_feature(hidpp,
2162 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2163 &k400->feature_index, &feature_type);
2164 if (ret)
2165 /* means that the device is not powered up */
2166 return ret;
2167 }
2168
2169 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2170 if (ret)
2171 return ret;
2172
2173 return 0;
2174}
2175
2176static int k400_allocate(struct hid_device *hdev)
2177{
2178 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2179 struct k400_private_data *k400;
2180
2181 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2182 GFP_KERNEL);
2183 if (!k400)
2184 return -ENOMEM;
2185
2186 hidpp->private_data = k400;
2187
2188 return 0;
2189};
2190
2191static int k400_connect(struct hid_device *hdev, bool connected)
2192{
2193 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2194
90cdd986
BT
2195 if (!disable_tap_to_click)
2196 return 0;
2197
2198 return k400_disable_tap_to_click(hidpp);
2199}
2200
7f4b49fe
SW
2201/* ------------------------------------------------------------------------- */
2202/* Logitech G920 Driving Force Racing Wheel for Xbox One */
2203/* ------------------------------------------------------------------------- */
2204
2205#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2206
7f4b49fe
SW
2207static int g920_get_config(struct hidpp_device *hidpp)
2208{
7f4b49fe
SW
2209 u8 feature_type;
2210 u8 feature_index;
2211 int ret;
2212
7f4b49fe
SW
2213 /* Find feature and store for later use */
2214 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2215 &feature_index, &feature_type);
2216 if (ret)
2217 return ret;
2218
ff21a635 2219 ret = hidpp_ff_init(hidpp, feature_index);
7f4b49fe 2220 if (ret)
ff21a635
EV
2221 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2222 ret);
7f4b49fe
SW
2223
2224 return 0;
2225}
2226
2f31c525
BT
2227/* -------------------------------------------------------------------------- */
2228/* Generic HID++ devices */
2229/* -------------------------------------------------------------------------- */
2230
2231static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2232 struct hid_field *field, struct hid_usage *usage,
2233 unsigned long **bit, int *max)
2234{
2235 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2236
2237 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2238 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
8a09b4fa
GB
2239 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2240 field->application != HID_GD_MOUSE)
2241 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2f31c525
BT
2242
2243 return 0;
2244}
2245
0b1804e3
SW
2246static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2247 struct hid_field *field, struct hid_usage *usage,
2248 unsigned long **bit, int *max)
2249{
2250 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2251
2252 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2253 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2254 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2255 usage->code == ABS_Y || usage->code == ABS_Z ||
2256 usage->code == ABS_RZ)) {
2257 field->application = HID_GD_MULTIAXIS;
2258 }
2259 }
2260
2261 return 0;
2262}
2263
2264
c39e3d5f
BT
2265static void hidpp_populate_input(struct hidpp_device *hidpp,
2266 struct input_dev *input, bool origin_is_hid_core)
2267{
2268 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2269 wtp_populate_input(hidpp, input, origin_is_hid_core);
8a09b4fa
GB
2270 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2271 m560_populate_input(hidpp, input, origin_is_hid_core);
c39e3d5f
BT
2272}
2273
b2c68a2f 2274static int hidpp_input_configured(struct hid_device *hdev,
2f31c525
BT
2275 struct hid_input *hidinput)
2276{
2277 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
c39e3d5f 2278 struct input_dev *input = hidinput->input;
2f31c525 2279
c39e3d5f 2280 hidpp_populate_input(hidpp, input, true);
b2c68a2f
DT
2281
2282 return 0;
2f31c525
BT
2283}
2284
2285static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2286 int size)
2287{
2288 struct hidpp_report *question = hidpp->send_receive_buf;
2289 struct hidpp_report *answer = hidpp->send_receive_buf;
2290 struct hidpp_report *report = (struct hidpp_report *)data;
eb626c57 2291 int ret;
2f31c525
BT
2292
2293 /*
2294 * If the mutex is locked then we have a pending answer from a
e529fea9 2295 * previously sent command.
2f31c525
BT
2296 */
2297 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2298 /*
2299 * Check for a correct hidpp20 answer or the corresponding
2300 * error
2301 */
2302 if (hidpp_match_answer(question, report) ||
2303 hidpp_match_error(question, report)) {
2304 *answer = *report;
2305 hidpp->answer_available = true;
2306 wake_up(&hidpp->wait);
2307 /*
2308 * This was an answer to a command that this driver sent
2309 * We return 1 to hid-core to avoid forwarding the
2310 * command upstream as it has been treated by the driver
2311 */
2312
2313 return 1;
2314 }
2315 }
2316
c39e3d5f
BT
2317 if (unlikely(hidpp_report_is_connect_event(report))) {
2318 atomic_set(&hidpp->connected,
2319 !(report->rap.params[0] & (1 << 6)));
6bd4e65d 2320 if (schedule_work(&hidpp->work) == 0)
c39e3d5f
BT
2321 dbg_hid("%s: connect event already queued\n", __func__);
2322 return 1;
2323 }
2324
eb626c57
BT
2325 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2326 ret = hidpp20_battery_event(hidpp, data, size);
2327 if (ret != 0)
2328 return ret;
2329 }
2330
2f31c525
BT
2331 return 0;
2332}
2333
2334static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2335 u8 *data, int size)
2336{
2337 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
e529fea9 2338 int ret = 0;
2f31c525 2339
e529fea9 2340 /* Generic HID++ processing. */
2f31c525 2341 switch (data[0]) {
a5ce8f5b
SW
2342 case REPORT_ID_HIDPP_VERY_LONG:
2343 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2344 hid_err(hdev, "received hid++ report of bad size (%d)",
2345 size);
2346 return 1;
2347 }
2348 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2349 break;
2f31c525
BT
2350 case REPORT_ID_HIDPP_LONG:
2351 if (size != HIDPP_REPORT_LONG_LENGTH) {
2352 hid_err(hdev, "received hid++ report of bad size (%d)",
2353 size);
2354 return 1;
2355 }
e529fea9
PW
2356 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2357 break;
2f31c525
BT
2358 case REPORT_ID_HIDPP_SHORT:
2359 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2360 hid_err(hdev, "received hid++ report of bad size (%d)",
2361 size);
2362 return 1;
2363 }
e529fea9
PW
2364 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2365 break;
2f31c525
BT
2366 }
2367
e529fea9
PW
2368 /* If no report is available for further processing, skip calling
2369 * raw_event of subclasses. */
2370 if (ret != 0)
2371 return ret;
2372
2f31c525
BT
2373 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2374 return wtp_raw_event(hdev, data, size);
8a09b4fa
GB
2375 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2376 return m560_raw_event(hdev, data, size);
2f31c525
BT
2377
2378 return 0;
2379}
2380
a52ec107
BT
2381static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2382{
2383 static atomic_t battery_no = ATOMIC_INIT(0);
2384 struct power_supply_config cfg = { .drv_data = hidpp };
2385 struct power_supply_desc *desc = &hidpp->battery.desc;
5b036ea1 2386 enum power_supply_property *battery_props;
a52ec107 2387 struct hidpp_battery *battery;
5b036ea1 2388 unsigned int num_battery_props;
a52ec107
BT
2389 unsigned long n;
2390 int ret;
2391
2392 if (hidpp->battery.ps)
2393 return 0;
2394
2395 if (hidpp->protocol_major >= 2) {
2396 ret = hidpp20_query_battery_info(hidpp);
2397 if (ret)
2398 return ret;
2399 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2400 } else {
2401 return -ENOENT;
2402 }
2403
5b036ea1
BT
2404 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
2405 hidpp_battery_props,
2406 sizeof(hidpp_battery_props),
2407 GFP_KERNEL);
2408 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
2409
2410 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2411 battery_props[num_battery_props++] =
2412 POWER_SUPPLY_PROP_CAPACITY;
2413
2414 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
2415 battery_props[num_battery_props++] =
2416 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
2417
a52ec107
BT
2418 battery = &hidpp->battery;
2419
2420 n = atomic_inc_return(&battery_no) - 1;
5b036ea1
BT
2421 desc->properties = battery_props;
2422 desc->num_properties = num_battery_props;
a52ec107
BT
2423 desc->get_property = hidpp_battery_get_property;
2424 sprintf(battery->name, "hidpp_battery_%ld", n);
2425 desc->name = battery->name;
2426 desc->type = POWER_SUPPLY_TYPE_BATTERY;
2427 desc->use_for_apm = 0;
2428
2429 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2430 &battery->desc,
2431 &cfg);
2432 if (IS_ERR(battery->ps))
2433 return PTR_ERR(battery->ps);
2434
2435 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2436
2437 return ret;
2438}
2439
843c624e 2440static void hidpp_overwrite_name(struct hid_device *hdev)
2f31c525
BT
2441{
2442 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2443 char *name;
2f31c525 2444
843c624e 2445 if (hidpp->protocol_major < 2)
b4f8ce07 2446 return;
843c624e
BT
2447
2448 name = hidpp_get_device_name(hidpp);
2f31c525 2449
7bfd2927 2450 if (!name) {
2f31c525 2451 hid_err(hdev, "unable to retrieve the name of the device");
7bfd2927
SW
2452 } else {
2453 dbg_hid("HID++: Got name: %s\n", name);
2f31c525 2454 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
7bfd2927 2455 }
2f31c525
BT
2456
2457 kfree(name);
2458}
2459
c39e3d5f
BT
2460static int hidpp_input_open(struct input_dev *dev)
2461{
2462 struct hid_device *hid = input_get_drvdata(dev);
2463
2464 return hid_hw_open(hid);
2465}
2466
2467static void hidpp_input_close(struct input_dev *dev)
2468{
2469 struct hid_device *hid = input_get_drvdata(dev);
2470
2471 hid_hw_close(hid);
2472}
2473
2474static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2475{
2476 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
005b3f57 2477 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
c39e3d5f
BT
2478
2479 if (!input_dev)
2480 return NULL;
2481
2482 input_set_drvdata(input_dev, hdev);
2483 input_dev->open = hidpp_input_open;
2484 input_dev->close = hidpp_input_close;
2485
005b3f57 2486 input_dev->name = hidpp->name;
c39e3d5f
BT
2487 input_dev->phys = hdev->phys;
2488 input_dev->uniq = hdev->uniq;
2489 input_dev->id.bustype = hdev->bus;
2490 input_dev->id.vendor = hdev->vendor;
2491 input_dev->id.product = hdev->product;
2492 input_dev->id.version = hdev->version;
2493 input_dev->dev.parent = &hdev->dev;
2494
2495 return input_dev;
2496}
2497
2498static void hidpp_connect_event(struct hidpp_device *hidpp)
2499{
2500 struct hid_device *hdev = hidpp->hid_dev;
2501 int ret = 0;
2502 bool connected = atomic_read(&hidpp->connected);
2503 struct input_dev *input;
2504 char *name, *devm_name;
c39e3d5f 2505
284f8d75
BT
2506 if (!connected) {
2507 if (hidpp->battery.ps) {
2508 hidpp->battery.online = false;
2509 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
5b036ea1 2510 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
284f8d75
BT
2511 power_supply_changed(hidpp->battery.ps);
2512 }
2936836f 2513 return;
284f8d75 2514 }
2936836f 2515
bf159447
BT
2516 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2517 ret = wtp_connect(hdev, connected);
2518 if (ret)
2519 return;
8a09b4fa
GB
2520 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2521 ret = m560_send_config_command(hdev, connected);
2522 if (ret)
2523 return;
90cdd986
BT
2524 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2525 ret = k400_connect(hdev, connected);
2526 if (ret)
2527 return;
bf159447 2528 }
586bdc4e 2529
580a7e82
BT
2530 /* the device is already connected, we can ask for its name and
2531 * protocol */
c39e3d5f
BT
2532 if (!hidpp->protocol_major) {
2533 ret = !hidpp_is_connected(hidpp);
2534 if (ret) {
2535 hid_err(hdev, "Can not get the protocol version.\n");
2536 return;
2537 }
580a7e82
BT
2538 hid_info(hdev, "HID++ %u.%u device connected.\n",
2539 hidpp->protocol_major, hidpp->protocol_minor);
c39e3d5f
BT
2540 }
2541
187f2bba 2542 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
005b3f57
BT
2543 name = hidpp_get_device_name(hidpp);
2544 if (!name) {
2545 hid_err(hdev,
2546 "unable to retrieve the name of the device");
2547 return;
2548 }
2549
2550 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2551 kfree(name);
2552 if (!devm_name)
2553 return;
2554
2555 hidpp->name = devm_name;
2556 }
2557
187f2bba
BT
2558 hidpp_initialize_battery(hidpp);
2559
9b9c519f
BT
2560 /* forward current battery state */
2561 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2562 hidpp20_query_battery_info(hidpp);
2563 if (hidpp->battery.ps)
2564 power_supply_changed(hidpp->battery.ps);
2565 }
2566
2936836f
BT
2567 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
2568 /* if the input nodes are already created, we can stop now */
187f2bba
BT
2569 return;
2570
c39e3d5f
BT
2571 input = hidpp_allocate_input(hdev);
2572 if (!input) {
2573 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2574 return;
2575 }
2576
c39e3d5f
BT
2577 hidpp_populate_input(hidpp, input, false);
2578
2579 ret = input_register_device(input);
2580 if (ret)
2581 input_free_device(input);
2582
2583 hidpp->delayed_input = input;
2584}
2585
2f31c525
BT
2586static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2587{
2588 struct hidpp_device *hidpp;
2589 int ret;
2590 bool connected;
c39e3d5f 2591 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2f31c525
BT
2592
2593 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2594 GFP_KERNEL);
2595 if (!hidpp)
2596 return -ENOMEM;
2597
2598 hidpp->hid_dev = hdev;
005b3f57 2599 hidpp->name = hdev->name;
2f31c525
BT
2600 hid_set_drvdata(hdev, hidpp);
2601
2602 hidpp->quirks = id->driver_data;
2603
843c624e
BT
2604 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
2605 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
2606
9188dbae
BT
2607 if (disable_raw_mode) {
2608 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
580a7e82 2609 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
9188dbae
BT
2610 }
2611
2f31c525
BT
2612 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2613 ret = wtp_allocate(hdev, id);
2614 if (ret)
8a09b4fa
GB
2615 goto allocate_fail;
2616 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2617 ret = m560_allocate(hdev);
2618 if (ret)
2619 goto allocate_fail;
90cdd986
BT
2620 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2621 ret = k400_allocate(hdev);
2622 if (ret)
2623 goto allocate_fail;
2f31c525
BT
2624 }
2625
c39e3d5f 2626 INIT_WORK(&hidpp->work, delayed_work_cb);
2f31c525
BT
2627 mutex_init(&hidpp->send_mutex);
2628 init_waitqueue_head(&hidpp->wait);
2629
2630 ret = hid_parse(hdev);
2631 if (ret) {
2632 hid_err(hdev, "%s:parse failed\n", __func__);
2633 goto hid_parse_fail;
2634 }
2635
7bfd2927
SW
2636 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2637 connect_mask &= ~HID_CONNECT_HIDINPUT;
2638
2639 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2640 ret = hid_hw_start(hdev, connect_mask);
2641 if (ret) {
2642 hid_err(hdev, "hw start failed\n");
2643 goto hid_hw_start_fail;
2644 }
2645 ret = hid_hw_open(hdev);
2646 if (ret < 0) {
2647 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
2648 __func__, ret);
2649 hid_hw_stop(hdev);
2650 goto hid_hw_start_fail;
2651 }
2652 }
2653
2654
2f31c525
BT
2655 /* Allow incoming packets */
2656 hid_device_io_start(hdev);
2657
843c624e
BT
2658 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
2659 hidpp_unifying_init(hidpp);
2660
2f31c525 2661 connected = hidpp_is_connected(hidpp);
843c624e
BT
2662 atomic_set(&hidpp->connected, connected);
2663 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
ab94e562 2664 if (!connected) {
b832da56 2665 ret = -ENODEV;
ab94e562 2666 hid_err(hdev, "Device not connected");
7bfd2927 2667 goto hid_hw_open_failed;
ab94e562 2668 }
2f31c525 2669
ab94e562
BT
2670 hid_info(hdev, "HID++ %u.%u device connected.\n",
2671 hidpp->protocol_major, hidpp->protocol_minor);
2f31c525 2672
843c624e
BT
2673 hidpp_overwrite_name(hdev);
2674 }
33797820 2675
c39e3d5f 2676 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
2f31c525
BT
2677 ret = wtp_get_config(hidpp);
2678 if (ret)
7bfd2927 2679 goto hid_hw_open_failed;
7f4b49fe
SW
2680 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2681 ret = g920_get_config(hidpp);
2682 if (ret)
2683 goto hid_hw_open_failed;
2f31c525
BT
2684 }
2685
2686 /* Block incoming packets */
2687 hid_device_io_stop(hdev);
2688
7bfd2927
SW
2689 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2690 ret = hid_hw_start(hdev, connect_mask);
2691 if (ret) {
2692 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
2693 goto hid_hw_start_fail;
2694 }
2f31c525
BT
2695 }
2696
6bd4e65d
BT
2697 /* Allow incoming packets */
2698 hid_device_io_start(hdev);
c39e3d5f 2699
6bd4e65d 2700 hidpp_connect_event(hidpp);
c39e3d5f 2701
2f31c525
BT
2702 return ret;
2703
7bfd2927
SW
2704hid_hw_open_failed:
2705 hid_device_io_stop(hdev);
2706 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2707 hid_hw_close(hdev);
2708 hid_hw_stop(hdev);
2709 }
2f31c525
BT
2710hid_hw_start_fail:
2711hid_parse_fail:
c39e3d5f 2712 cancel_work_sync(&hidpp->work);
2f31c525 2713 mutex_destroy(&hidpp->send_mutex);
8a09b4fa 2714allocate_fail:
2f31c525
BT
2715 hid_set_drvdata(hdev, NULL);
2716 return ret;
2717}
2718
2719static void hidpp_remove(struct hid_device *hdev)
2720{
2721 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2722
7f4b49fe 2723 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
ff21a635 2724 hidpp_ff_deinit(hdev);
7bfd2927 2725 hid_hw_close(hdev);
7f4b49fe 2726 }
7bfd2927 2727 hid_hw_stop(hdev);
c39e3d5f 2728 cancel_work_sync(&hidpp->work);
2f31c525 2729 mutex_destroy(&hidpp->send_mutex);
2f31c525
BT
2730}
2731
2732static const struct hid_device_id hidpp_devices[] = {
57ac86cf
BT
2733 { /* wireless touchpad */
2734 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2735 USB_VENDOR_ID_LOGITECH, 0x4011),
2736 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
2737 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
586bdc4e
BT
2738 { /* wireless touchpad T650 */
2739 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2740 USB_VENDOR_ID_LOGITECH, 0x4101),
2741 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
2f31c525
BT
2742 { /* wireless touchpad T651 */
2743 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
2744 USB_DEVICE_ID_LOGITECH_T651),
2745 .driver_data = HIDPP_QUIRK_CLASS_WTP },
8a09b4fa 2746 { /* Mouse logitech M560 */
3a61e975 2747 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
8a09b4fa
GB
2748 USB_VENDOR_ID_LOGITECH, 0x402d),
2749 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
90cdd986
BT
2750 { /* Keyboard logitech K400 */
2751 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2752 USB_VENDOR_ID_LOGITECH, 0x4024),
6bd4e65d 2753 .driver_data = HIDPP_QUIRK_CLASS_K400 },
ab94e562
BT
2754
2755 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2756 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
7bfd2927
SW
2757
2758 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
2759 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
2f31c525
BT
2760 {}
2761};
2762
2763MODULE_DEVICE_TABLE(hid, hidpp_devices);
2764
2765static struct hid_driver hidpp_driver = {
2766 .name = "logitech-hidpp-device",
2767 .id_table = hidpp_devices,
2768 .probe = hidpp_probe,
2769 .remove = hidpp_remove,
2770 .raw_event = hidpp_raw_event,
2771 .input_configured = hidpp_input_configured,
2772 .input_mapping = hidpp_input_mapping,
0b1804e3 2773 .input_mapped = hidpp_input_mapped,
2f31c525
BT
2774};
2775
2776module_hid_driver(hidpp_driver);