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