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