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