1
2
3
4
5
6
7
8
9
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/device.h>
14#include <linux/input.h>
15#include <linux/usb.h>
16#include <linux/hid.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
20#include <linux/sched/clock.h>
21#include <linux/kfifo.h>
22#include <linux/input/mt.h>
23#include <linux/workqueue.h>
24#include <linux/atomic.h>
25#include <linux/fixp-arith.h>
26#include <asm/unaligned.h>
27#include "usbhid/usbhid.h"
28#include "hid-ids.h"
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
32MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
33
34static bool disable_raw_mode;
35module_param(disable_raw_mode, bool, 0644);
36MODULE_PARM_DESC(disable_raw_mode,
37 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
38
39static bool disable_tap_to_click;
40module_param(disable_tap_to_click, bool, 0644);
41MODULE_PARM_DESC(disable_tap_to_click,
42 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
43
44#define REPORT_ID_HIDPP_SHORT 0x10
45#define REPORT_ID_HIDPP_LONG 0x11
46#define REPORT_ID_HIDPP_VERY_LONG 0x12
47
48#define HIDPP_REPORT_SHORT_LENGTH 7
49#define HIDPP_REPORT_LONG_LENGTH 20
50#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
51
52#define HIDPP_REPORT_SHORT_SUPPORTED BIT(0)
53#define HIDPP_REPORT_LONG_SUPPORTED BIT(1)
54#define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2)
55
56#define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
57#define HIDPP_SUB_ID_ROLLER 0x05
58#define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
59#define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08
60#define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5)
61
62#define HIDPP_QUIRK_CLASS_WTP BIT(0)
63#define HIDPP_QUIRK_CLASS_M560 BIT(1)
64#define HIDPP_QUIRK_CLASS_K400 BIT(2)
65#define HIDPP_QUIRK_CLASS_G920 BIT(3)
66#define HIDPP_QUIRK_CLASS_K750 BIT(4)
67
68
69
70#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
71#define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
72#define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
73#define HIDPP_QUIRK_UNIFYING BIT(25)
74#define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
75#define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
76#define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
77#define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
78#define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
79#define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
80
81
82#define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
83#define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
84
85
86#define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
87 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
88 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
89
90#define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
91
92#define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
93#define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
94#define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
95#define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
96#define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4)
97#define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5)
98#define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6)
99
100#define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124struct fap {
125 u8 feature_index;
126 u8 funcindex_clientid;
127 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
128};
129
130struct rap {
131 u8 sub_id;
132 u8 reg_address;
133 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
134};
135
136struct hidpp_report {
137 u8 report_id;
138 u8 device_index;
139 union {
140 struct fap fap;
141 struct rap rap;
142 u8 rawbytes[sizeof(struct fap)];
143 };
144} __packed;
145
146struct hidpp_battery {
147 u8 feature_index;
148 u8 solar_feature_index;
149 u8 voltage_feature_index;
150 struct power_supply_desc desc;
151 struct power_supply *ps;
152 char name[64];
153 int status;
154 int capacity;
155 int level;
156 int voltage;
157 int charge_type;
158 bool online;
159 u8 supported_levels_1004;
160};
161
162
163
164
165
166
167
168
169
170
171
172
173struct hidpp_scroll_counter {
174 int wheel_multiplier;
175 int remainder;
176 int direction;
177 unsigned long long last_time;
178};
179
180struct hidpp_device {
181 struct hid_device *hid_dev;
182 struct input_dev *input;
183 struct mutex send_mutex;
184 void *send_receive_buf;
185 char *name;
186 wait_queue_head_t wait;
187 int very_long_report_length;
188 bool answer_available;
189 u8 protocol_major;
190 u8 protocol_minor;
191
192 void *private_data;
193
194 struct work_struct work;
195 struct kfifo delayed_work_fifo;
196 atomic_t connected;
197 struct input_dev *delayed_input;
198
199 unsigned long quirks;
200 unsigned long capabilities;
201 u8 supported_reports;
202
203 struct hidpp_battery battery;
204 struct hidpp_scroll_counter vertical_wheel_counter;
205
206 u8 wireless_feature_index;
207};
208
209
210#define HIDPP_ERROR 0x8f
211#define HIDPP_ERROR_SUCCESS 0x00
212#define HIDPP_ERROR_INVALID_SUBID 0x01
213#define HIDPP_ERROR_INVALID_ADRESS 0x02
214#define HIDPP_ERROR_INVALID_VALUE 0x03
215#define HIDPP_ERROR_CONNECT_FAIL 0x04
216#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
217#define HIDPP_ERROR_ALREADY_EXISTS 0x06
218#define HIDPP_ERROR_BUSY 0x07
219#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
220#define HIDPP_ERROR_RESOURCE_ERROR 0x09
221#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
222#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
223#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
224
225#define HIDPP20_ERROR 0xff
226
227static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
228
229static int __hidpp_send_report(struct hid_device *hdev,
230 struct hidpp_report *hidpp_report)
231{
232 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
233 int fields_count, ret;
234
235 switch (hidpp_report->report_id) {
236 case REPORT_ID_HIDPP_SHORT:
237 fields_count = HIDPP_REPORT_SHORT_LENGTH;
238 break;
239 case REPORT_ID_HIDPP_LONG:
240 fields_count = HIDPP_REPORT_LONG_LENGTH;
241 break;
242 case REPORT_ID_HIDPP_VERY_LONG:
243 fields_count = hidpp->very_long_report_length;
244 break;
245 default:
246 return -ENODEV;
247 }
248
249
250
251
252
253 hidpp_report->device_index = 0xff;
254
255 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
256 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
257 } else {
258 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
259 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
260 HID_REQ_SET_REPORT);
261 }
262
263 return ret == fields_count ? 0 : -1;
264}
265
266
267
268
269
270
271
272
273
274static int hidpp_send_message_sync(struct hidpp_device *hidpp,
275 struct hidpp_report *message,
276 struct hidpp_report *response)
277{
278 int ret;
279
280 mutex_lock(&hidpp->send_mutex);
281
282 hidpp->send_receive_buf = response;
283 hidpp->answer_available = false;
284
285
286
287
288
289 *response = *message;
290
291 ret = __hidpp_send_report(hidpp->hid_dev, message);
292
293 if (ret) {
294 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
295 memset(response, 0, sizeof(struct hidpp_report));
296 goto exit;
297 }
298
299 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
300 5*HZ)) {
301 dbg_hid("%s:timeout waiting for response\n", __func__);
302 memset(response, 0, sizeof(struct hidpp_report));
303 ret = -ETIMEDOUT;
304 }
305
306 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
307 response->rap.sub_id == HIDPP_ERROR) {
308 ret = response->rap.params[1];
309 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
310 goto exit;
311 }
312
313 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
314 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
315 response->fap.feature_index == HIDPP20_ERROR) {
316 ret = response->fap.params[1];
317 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
318 goto exit;
319 }
320
321exit:
322 mutex_unlock(&hidpp->send_mutex);
323 return ret;
324
325}
326
327static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
328 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
329 struct hidpp_report *response)
330{
331 struct hidpp_report *message;
332 int ret;
333
334 if (param_count > sizeof(message->fap.params))
335 return -EINVAL;
336
337 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
338 if (!message)
339 return -ENOMEM;
340
341 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
342 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
343 else
344 message->report_id = REPORT_ID_HIDPP_LONG;
345 message->fap.feature_index = feat_index;
346 message->fap.funcindex_clientid = funcindex_clientid;
347 memcpy(&message->fap.params, params, param_count);
348
349 ret = hidpp_send_message_sync(hidpp, message, response);
350 kfree(message);
351 return ret;
352}
353
354static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
355 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
356 struct hidpp_report *response)
357{
358 struct hidpp_report *message;
359 int ret, max_count;
360
361
362 if (report_id == REPORT_ID_HIDPP_SHORT &&
363 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
364 report_id = REPORT_ID_HIDPP_LONG;
365
366 switch (report_id) {
367 case REPORT_ID_HIDPP_SHORT:
368 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
369 break;
370 case REPORT_ID_HIDPP_LONG:
371 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
372 break;
373 case REPORT_ID_HIDPP_VERY_LONG:
374 max_count = hidpp_dev->very_long_report_length - 4;
375 break;
376 default:
377 return -EINVAL;
378 }
379
380 if (param_count > max_count)
381 return -EINVAL;
382
383 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
384 if (!message)
385 return -ENOMEM;
386 message->report_id = report_id;
387 message->rap.sub_id = sub_id;
388 message->rap.reg_address = reg_address;
389 memcpy(&message->rap.params, params, param_count);
390
391 ret = hidpp_send_message_sync(hidpp_dev, message, response);
392 kfree(message);
393 return ret;
394}
395
396static void delayed_work_cb(struct work_struct *work)
397{
398 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
399 work);
400 hidpp_connect_event(hidpp);
401}
402
403static inline bool hidpp_match_answer(struct hidpp_report *question,
404 struct hidpp_report *answer)
405{
406 return (answer->fap.feature_index == question->fap.feature_index) &&
407 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
408}
409
410static inline bool hidpp_match_error(struct hidpp_report *question,
411 struct hidpp_report *answer)
412{
413 return ((answer->rap.sub_id == HIDPP_ERROR) ||
414 (answer->fap.feature_index == HIDPP20_ERROR)) &&
415 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
416 (answer->fap.params[0] == question->fap.funcindex_clientid);
417}
418
419static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp,
420 struct hidpp_report *report)
421{
422 return (hidpp->wireless_feature_index &&
423 (report->fap.feature_index == hidpp->wireless_feature_index)) ||
424 ((report->report_id == REPORT_ID_HIDPP_SHORT) &&
425 (report->rap.sub_id == 0x41));
426}
427
428
429
430
431static void hidpp_prefix_name(char **name, int name_length)
432{
433#define PREFIX_LENGTH 9
434
435 int new_length;
436 char *new_name;
437
438 if (name_length > PREFIX_LENGTH &&
439 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
440
441 return;
442
443 new_length = PREFIX_LENGTH + name_length;
444 new_name = kzalloc(new_length, GFP_KERNEL);
445 if (!new_name)
446 return;
447
448 snprintf(new_name, new_length, "Logitech %s", *name);
449
450 kfree(*name);
451
452 *name = new_name;
453}
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
471 struct hidpp_scroll_counter *counter,
472 int hi_res_value)
473{
474 int low_res_value, remainder, direction;
475 unsigned long long now, previous;
476
477 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
478 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
479
480 remainder = counter->remainder;
481 direction = hi_res_value > 0 ? 1 : -1;
482
483 now = sched_clock();
484 previous = counter->last_time;
485 counter->last_time = now;
486
487
488
489
490
491
492 if (now - previous > 1000000000 || direction != counter->direction)
493 remainder = 0;
494
495 counter->direction = direction;
496 remainder += hi_res_value;
497
498
499
500
501
502
503 if (abs(remainder) >= 60) {
504
505
506
507
508
509 low_res_value = remainder / 120;
510 if (low_res_value == 0)
511 low_res_value = (hi_res_value > 0 ? 1 : -1);
512 input_report_rel(input_dev, REL_WHEEL, low_res_value);
513 remainder -= low_res_value * 120;
514 }
515 counter->remainder = remainder;
516}
517
518
519
520
521
522#define HIDPP_SET_REGISTER 0x80
523#define HIDPP_GET_REGISTER 0x81
524#define HIDPP_SET_LONG_REGISTER 0x82
525#define HIDPP_GET_LONG_REGISTER 0x83
526
527
528
529
530
531
532
533
534
535
536static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
537 u8 register_address, u8 byte, u8 mask, u8 value)
538{
539 struct hidpp_report response;
540 int ret;
541 u8 params[3] = { 0 };
542
543 ret = hidpp_send_rap_command_sync(hidpp_dev,
544 REPORT_ID_HIDPP_SHORT,
545 HIDPP_GET_REGISTER,
546 register_address,
547 NULL, 0, &response);
548 if (ret)
549 return ret;
550
551 memcpy(params, response.rap.params, 3);
552
553 params[byte] &= ~mask;
554 params[byte] |= value & mask;
555
556 return hidpp_send_rap_command_sync(hidpp_dev,
557 REPORT_ID_HIDPP_SHORT,
558 HIDPP_SET_REGISTER,
559 register_address,
560 params, 3, &response);
561}
562
563#define HIDPP_REG_ENABLE_REPORTS 0x00
564#define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
565#define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
566#define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
567#define HIDPP_ENABLE_BAT_REPORT BIT(4)
568#define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
569
570static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
571{
572 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
573 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
574}
575
576#define HIDPP_REG_FEATURES 0x01
577#define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
578#define HIDPP_ENABLE_FAST_SCROLL BIT(6)
579
580
581static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
582{
583 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
584 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
585}
586
587#define HIDPP_REG_BATTERY_STATUS 0x07
588
589static int hidpp10_battery_status_map_level(u8 param)
590{
591 int level;
592
593 switch (param) {
594 case 1 ... 2:
595 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
596 break;
597 case 3 ... 4:
598 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
599 break;
600 case 5 ... 6:
601 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
602 break;
603 case 7:
604 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
605 break;
606 default:
607 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
608 }
609
610 return level;
611}
612
613static int hidpp10_battery_status_map_status(u8 param)
614{
615 int status;
616
617 switch (param) {
618 case 0x00:
619
620 status = POWER_SUPPLY_STATUS_DISCHARGING;
621 break;
622 case 0x21:
623 case 0x24:
624 case 0x25:
625 status = POWER_SUPPLY_STATUS_CHARGING;
626 break;
627 case 0x26:
628 case 0x22:
629 status = POWER_SUPPLY_STATUS_FULL;
630 break;
631 case 0x20:
632 status = POWER_SUPPLY_STATUS_UNKNOWN;
633 break;
634
635
636
637
638
639 default:
640 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
641 break;
642 }
643
644 return status;
645}
646
647static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
648{
649 struct hidpp_report response;
650 int ret, status;
651
652 ret = hidpp_send_rap_command_sync(hidpp,
653 REPORT_ID_HIDPP_SHORT,
654 HIDPP_GET_REGISTER,
655 HIDPP_REG_BATTERY_STATUS,
656 NULL, 0, &response);
657 if (ret)
658 return ret;
659
660 hidpp->battery.level =
661 hidpp10_battery_status_map_level(response.rap.params[0]);
662 status = hidpp10_battery_status_map_status(response.rap.params[1]);
663 hidpp->battery.status = status;
664
665 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
666 status == POWER_SUPPLY_STATUS_FULL;
667
668 return 0;
669}
670
671#define HIDPP_REG_BATTERY_MILEAGE 0x0D
672
673static int hidpp10_battery_mileage_map_status(u8 param)
674{
675 int status;
676
677 switch (param >> 6) {
678 case 0x00:
679
680 status = POWER_SUPPLY_STATUS_DISCHARGING;
681 break;
682 case 0x01:
683 status = POWER_SUPPLY_STATUS_CHARGING;
684 break;
685 case 0x02:
686 status = POWER_SUPPLY_STATUS_FULL;
687 break;
688
689
690
691 default:
692 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
693 break;
694 }
695
696 return status;
697}
698
699static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
700{
701 struct hidpp_report response;
702 int ret, status;
703
704 ret = hidpp_send_rap_command_sync(hidpp,
705 REPORT_ID_HIDPP_SHORT,
706 HIDPP_GET_REGISTER,
707 HIDPP_REG_BATTERY_MILEAGE,
708 NULL, 0, &response);
709 if (ret)
710 return ret;
711
712 hidpp->battery.capacity = response.rap.params[0];
713 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
714 hidpp->battery.status = status;
715
716 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
717 status == POWER_SUPPLY_STATUS_FULL;
718
719 return 0;
720}
721
722static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
723{
724 struct hidpp_report *report = (struct hidpp_report *)data;
725 int status, capacity, level;
726 bool changed;
727
728 if (report->report_id != REPORT_ID_HIDPP_SHORT)
729 return 0;
730
731 switch (report->rap.sub_id) {
732 case HIDPP_REG_BATTERY_STATUS:
733 capacity = hidpp->battery.capacity;
734 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
735 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
736 break;
737 case HIDPP_REG_BATTERY_MILEAGE:
738 capacity = report->rap.params[0];
739 level = hidpp->battery.level;
740 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
741 break;
742 default:
743 return 0;
744 }
745
746 changed = capacity != hidpp->battery.capacity ||
747 level != hidpp->battery.level ||
748 status != hidpp->battery.status;
749
750
751 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
752 status == POWER_SUPPLY_STATUS_FULL;
753
754 if (changed) {
755 hidpp->battery.level = level;
756 hidpp->battery.status = status;
757 if (hidpp->battery.ps)
758 power_supply_changed(hidpp->battery.ps);
759 }
760
761 return 0;
762}
763
764#define HIDPP_REG_PAIRING_INFORMATION 0xB5
765#define HIDPP_EXTENDED_PAIRING 0x30
766#define HIDPP_DEVICE_NAME 0x40
767
768static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
769{
770 struct hidpp_report response;
771 int ret;
772 u8 params[1] = { HIDPP_DEVICE_NAME };
773 char *name;
774 int len;
775
776 ret = hidpp_send_rap_command_sync(hidpp_dev,
777 REPORT_ID_HIDPP_SHORT,
778 HIDPP_GET_LONG_REGISTER,
779 HIDPP_REG_PAIRING_INFORMATION,
780 params, 1, &response);
781 if (ret)
782 return NULL;
783
784 len = response.rap.params[1];
785
786 if (2 + len > sizeof(response.rap.params))
787 return NULL;
788
789 if (len < 4)
790 return NULL;
791
792 name = kzalloc(len + 1, GFP_KERNEL);
793 if (!name)
794 return NULL;
795
796 memcpy(name, &response.rap.params[2], len);
797
798
799 hidpp_prefix_name(&name, len + 1);
800
801 return name;
802}
803
804static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
805{
806 struct hidpp_report response;
807 int ret;
808 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
809
810 ret = hidpp_send_rap_command_sync(hidpp,
811 REPORT_ID_HIDPP_SHORT,
812 HIDPP_GET_LONG_REGISTER,
813 HIDPP_REG_PAIRING_INFORMATION,
814 params, 1, &response);
815 if (ret)
816 return ret;
817
818
819
820
821
822 *serial = *((u32 *)&response.rap.params[1]);
823 return 0;
824}
825
826static int hidpp_unifying_init(struct hidpp_device *hidpp)
827{
828 struct hid_device *hdev = hidpp->hid_dev;
829 const char *name;
830 u32 serial;
831 int ret;
832
833 ret = hidpp_unifying_get_serial(hidpp, &serial);
834 if (ret)
835 return ret;
836
837 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
838 hdev->product, &serial);
839 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
840
841 name = hidpp_unifying_get_name(hidpp);
842 if (!name)
843 return -EIO;
844
845 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
846 dbg_hid("HID++ Unifying: Got name: %s\n", name);
847
848 kfree(name);
849 return 0;
850}
851
852
853
854
855
856#define HIDPP_PAGE_ROOT 0x0000
857#define HIDPP_PAGE_ROOT_IDX 0x00
858
859#define CMD_ROOT_GET_FEATURE 0x01
860#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
861
862static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
863 u8 *feature_index, u8 *feature_type)
864{
865 struct hidpp_report response;
866 int ret;
867 u8 params[2] = { feature >> 8, feature & 0x00FF };
868
869 ret = hidpp_send_fap_command_sync(hidpp,
870 HIDPP_PAGE_ROOT_IDX,
871 CMD_ROOT_GET_FEATURE,
872 params, 2, &response);
873 if (ret)
874 return ret;
875
876 if (response.fap.params[0] == 0)
877 return -ENOENT;
878
879 *feature_index = response.fap.params[0];
880 *feature_type = response.fap.params[1];
881
882 return ret;
883}
884
885static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
886{
887 const u8 ping_byte = 0x5a;
888 u8 ping_data[3] = { 0, 0, ping_byte };
889 struct hidpp_report response;
890 int ret;
891
892 ret = hidpp_send_rap_command_sync(hidpp,
893 REPORT_ID_HIDPP_SHORT,
894 HIDPP_PAGE_ROOT_IDX,
895 CMD_ROOT_GET_PROTOCOL_VERSION,
896 ping_data, sizeof(ping_data), &response);
897
898 if (ret == HIDPP_ERROR_INVALID_SUBID) {
899 hidpp->protocol_major = 1;
900 hidpp->protocol_minor = 0;
901 goto print_version;
902 }
903
904
905 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
906 return -EIO;
907
908 if (ret > 0) {
909 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
910 __func__, ret);
911 return -EPROTO;
912 }
913 if (ret)
914 return ret;
915
916 if (response.rap.params[2] != ping_byte) {
917 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
918 __func__, response.rap.params[2], ping_byte);
919 return -EPROTO;
920 }
921
922 hidpp->protocol_major = response.rap.params[0];
923 hidpp->protocol_minor = response.rap.params[1];
924
925print_version:
926 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
927 hidpp->protocol_major, hidpp->protocol_minor);
928 return 0;
929}
930
931
932
933
934
935#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
936
937#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
938#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
939#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
940
941static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
942 u8 feature_index, u8 *nameLength)
943{
944 struct hidpp_report response;
945 int ret;
946
947 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
948 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
949
950 if (ret > 0) {
951 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
952 __func__, ret);
953 return -EPROTO;
954 }
955 if (ret)
956 return ret;
957
958 *nameLength = response.fap.params[0];
959
960 return ret;
961}
962
963static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
964 u8 feature_index, u8 char_index, char *device_name, int len_buf)
965{
966 struct hidpp_report response;
967 int ret, i;
968 int count;
969
970 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
971 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
972 &response);
973
974 if (ret > 0) {
975 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
976 __func__, ret);
977 return -EPROTO;
978 }
979 if (ret)
980 return ret;
981
982 switch (response.report_id) {
983 case REPORT_ID_HIDPP_VERY_LONG:
984 count = hidpp->very_long_report_length - 4;
985 break;
986 case REPORT_ID_HIDPP_LONG:
987 count = HIDPP_REPORT_LONG_LENGTH - 4;
988 break;
989 case REPORT_ID_HIDPP_SHORT:
990 count = HIDPP_REPORT_SHORT_LENGTH - 4;
991 break;
992 default:
993 return -EPROTO;
994 }
995
996 if (len_buf < count)
997 count = len_buf;
998
999 for (i = 0; i < count; i++)
1000 device_name[i] = response.fap.params[i];
1001
1002 return count;
1003}
1004
1005static char *hidpp_get_device_name(struct hidpp_device *hidpp)
1006{
1007 u8 feature_type;
1008 u8 feature_index;
1009 u8 __name_length;
1010 char *name;
1011 unsigned index = 0;
1012 int ret;
1013
1014 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
1015 &feature_index, &feature_type);
1016 if (ret)
1017 return NULL;
1018
1019 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
1020 &__name_length);
1021 if (ret)
1022 return NULL;
1023
1024 name = kzalloc(__name_length + 1, GFP_KERNEL);
1025 if (!name)
1026 return NULL;
1027
1028 while (index < __name_length) {
1029 ret = hidpp_devicenametype_get_device_name(hidpp,
1030 feature_index, index, name + index,
1031 __name_length - index);
1032 if (ret <= 0) {
1033 kfree(name);
1034 return NULL;
1035 }
1036 index += ret;
1037 }
1038
1039
1040 hidpp_prefix_name(&name, __name_length + 1);
1041
1042 return name;
1043}
1044
1045
1046
1047
1048
1049#define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1050
1051#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1052#define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1053
1054#define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1055
1056#define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1057#define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1058#define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1059
1060static int hidpp_map_battery_level(int capacity)
1061{
1062 if (capacity < 11)
1063 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1064
1065
1066
1067
1068 else if (capacity < 30)
1069 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1070 else if (capacity < 81)
1071 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1072 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1073}
1074
1075static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1076 int *next_capacity,
1077 int *level)
1078{
1079 int status;
1080
1081 *capacity = data[0];
1082 *next_capacity = data[1];
1083 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1084
1085
1086
1087
1088 switch (data[2]) {
1089 case 0:
1090 status = POWER_SUPPLY_STATUS_DISCHARGING;
1091 *level = hidpp_map_battery_level(*capacity);
1092 break;
1093 case 1:
1094 status = POWER_SUPPLY_STATUS_CHARGING;
1095 break;
1096 case 2:
1097 status = POWER_SUPPLY_STATUS_CHARGING;
1098 break;
1099 case 3:
1100 status = POWER_SUPPLY_STATUS_FULL;
1101 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1102 *capacity = 100;
1103 break;
1104 case 4:
1105 status = POWER_SUPPLY_STATUS_CHARGING;
1106 break;
1107
1108
1109
1110 default:
1111 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1112 break;
1113 }
1114
1115 return status;
1116}
1117
1118static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1119 u8 feature_index,
1120 int *status,
1121 int *capacity,
1122 int *next_capacity,
1123 int *level)
1124{
1125 struct hidpp_report response;
1126 int ret;
1127 u8 *params = (u8 *)response.fap.params;
1128
1129 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1130 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1131 NULL, 0, &response);
1132
1133 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1134 return -EIO;
1135 if (ret > 0) {
1136 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1137 __func__, ret);
1138 return -EPROTO;
1139 }
1140 if (ret)
1141 return ret;
1142
1143 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1144 next_capacity,
1145 level);
1146
1147 return 0;
1148}
1149
1150static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1151 u8 feature_index)
1152{
1153 struct hidpp_report response;
1154 int ret;
1155 u8 *params = (u8 *)response.fap.params;
1156 unsigned int level_count, flags;
1157
1158 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1159 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1160 NULL, 0, &response);
1161 if (ret > 0) {
1162 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1163 __func__, ret);
1164 return -EPROTO;
1165 }
1166 if (ret)
1167 return ret;
1168
1169 level_count = params[0];
1170 flags = params[1];
1171
1172 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1173 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1174 else
1175 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1176
1177 return 0;
1178}
1179
1180static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp)
1181{
1182 u8 feature_type;
1183 int ret;
1184 int status, capacity, next_capacity, level;
1185
1186 if (hidpp->battery.feature_index == 0xff) {
1187 ret = hidpp_root_get_feature(hidpp,
1188 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1189 &hidpp->battery.feature_index,
1190 &feature_type);
1191 if (ret)
1192 return ret;
1193 }
1194
1195 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1196 hidpp->battery.feature_index,
1197 &status, &capacity,
1198 &next_capacity, &level);
1199 if (ret)
1200 return ret;
1201
1202 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1203 hidpp->battery.feature_index);
1204 if (ret)
1205 return ret;
1206
1207 hidpp->battery.status = status;
1208 hidpp->battery.capacity = capacity;
1209 hidpp->battery.level = level;
1210
1211 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1212 status == POWER_SUPPLY_STATUS_FULL;
1213
1214 return 0;
1215}
1216
1217static int hidpp20_battery_event_1000(struct hidpp_device *hidpp,
1218 u8 *data, int size)
1219{
1220 struct hidpp_report *report = (struct hidpp_report *)data;
1221 int status, capacity, next_capacity, level;
1222 bool changed;
1223
1224 if (report->fap.feature_index != hidpp->battery.feature_index ||
1225 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1226 return 0;
1227
1228 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1229 &capacity,
1230 &next_capacity,
1231 &level);
1232
1233
1234 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1235 status == POWER_SUPPLY_STATUS_FULL;
1236
1237 changed = capacity != hidpp->battery.capacity ||
1238 level != hidpp->battery.level ||
1239 status != hidpp->battery.status;
1240
1241 if (changed) {
1242 hidpp->battery.level = level;
1243 hidpp->battery.capacity = capacity;
1244 hidpp->battery.status = status;
1245 if (hidpp->battery.ps)
1246 power_supply_changed(hidpp->battery.ps);
1247 }
1248
1249 return 0;
1250}
1251
1252
1253
1254
1255
1256#define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001
1257
1258#define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00
1259
1260#define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00
1261
1262static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage,
1263 int *level, int *charge_type)
1264{
1265 int status;
1266
1267 long flags = (long) data[2];
1268 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1269
1270 if (flags & 0x80)
1271 switch (flags & 0x07) {
1272 case 0:
1273 status = POWER_SUPPLY_STATUS_CHARGING;
1274 break;
1275 case 1:
1276 status = POWER_SUPPLY_STATUS_FULL;
1277 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1278 break;
1279 case 2:
1280 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1281 break;
1282 default:
1283 status = POWER_SUPPLY_STATUS_UNKNOWN;
1284 break;
1285 }
1286 else
1287 status = POWER_SUPPLY_STATUS_DISCHARGING;
1288
1289 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
1290 if (test_bit(3, &flags)) {
1291 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1292 }
1293 if (test_bit(4, &flags)) {
1294 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1295 }
1296 if (test_bit(5, &flags)) {
1297 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1298 }
1299
1300 *voltage = get_unaligned_be16(data);
1301
1302 return status;
1303}
1304
1305static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp,
1306 u8 feature_index,
1307 int *status, int *voltage,
1308 int *level, int *charge_type)
1309{
1310 struct hidpp_report response;
1311 int ret;
1312 u8 *params = (u8 *)response.fap.params;
1313
1314 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1315 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE,
1316 NULL, 0, &response);
1317
1318 if (ret > 0) {
1319 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1320 __func__, ret);
1321 return -EPROTO;
1322 }
1323 if (ret)
1324 return ret;
1325
1326 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE;
1327
1328 *status = hidpp20_battery_map_status_voltage(params, voltage,
1329 level, charge_type);
1330
1331 return 0;
1332}
1333
1334static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp)
1335{
1336 u8 feature_type;
1337 int ret;
1338 int status, voltage, level, charge_type;
1339
1340 if (hidpp->battery.voltage_feature_index == 0xff) {
1341 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE,
1342 &hidpp->battery.voltage_feature_index,
1343 &feature_type);
1344 if (ret)
1345 return ret;
1346 }
1347
1348 ret = hidpp20_battery_get_battery_voltage(hidpp,
1349 hidpp->battery.voltage_feature_index,
1350 &status, &voltage, &level, &charge_type);
1351
1352 if (ret)
1353 return ret;
1354
1355 hidpp->battery.status = status;
1356 hidpp->battery.voltage = voltage;
1357 hidpp->battery.level = level;
1358 hidpp->battery.charge_type = charge_type;
1359 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1360
1361 return 0;
1362}
1363
1364static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp,
1365 u8 *data, int size)
1366{
1367 struct hidpp_report *report = (struct hidpp_report *)data;
1368 int status, voltage, level, charge_type;
1369
1370 if (report->fap.feature_index != hidpp->battery.voltage_feature_index ||
1371 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST)
1372 return 0;
1373
1374 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage,
1375 &level, &charge_type);
1376
1377 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING;
1378
1379 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) {
1380 hidpp->battery.voltage = voltage;
1381 hidpp->battery.status = status;
1382 hidpp->battery.level = level;
1383 hidpp->battery.charge_type = charge_type;
1384 if (hidpp->battery.ps)
1385 power_supply_changed(hidpp->battery.ps);
1386 }
1387 return 0;
1388}
1389
1390
1391
1392
1393
1394#define HIDPP_PAGE_UNIFIED_BATTERY 0x1004
1395
1396#define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00
1397#define CMD_UNIFIED_BATTERY_GET_STATUS 0x10
1398
1399#define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00
1400
1401#define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0)
1402#define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1)
1403#define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2)
1404#define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3)
1405
1406#define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0)
1407#define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1)
1408
1409static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp,
1410 u8 feature_index)
1411{
1412 struct hidpp_report response;
1413 int ret;
1414 u8 *params = (u8 *)response.fap.params;
1415
1416 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS ||
1417 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) {
1418
1419 return 0;
1420 }
1421
1422 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1423 CMD_UNIFIED_BATTERY_GET_CAPABILITIES,
1424 NULL, 0, &response);
1425
1426 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1427 return -EIO;
1428 if (ret > 0) {
1429 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1430 __func__, ret);
1431 return -EPROTO;
1432 }
1433 if (ret)
1434 return ret;
1435
1436
1437
1438
1439
1440
1441
1442
1443 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) {
1444 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE;
1445 hidpp->battery.supported_levels_1004 = 0;
1446 } else {
1447 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1448 hidpp->battery.supported_levels_1004 = params[0];
1449 }
1450
1451 return 0;
1452}
1453
1454static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp,
1455 u8 charging_status,
1456 u8 external_power_status)
1457{
1458 int status;
1459
1460 switch (charging_status) {
1461 case 0:
1462 status = POWER_SUPPLY_STATUS_DISCHARGING;
1463 break;
1464 case 1:
1465 case 2:
1466 status = POWER_SUPPLY_STATUS_CHARGING;
1467 break;
1468 case 3:
1469 status = POWER_SUPPLY_STATUS_FULL;
1470 break;
1471 case 4:
1472 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1473 hid_info(hidpp->hid_dev, "%s: charging error",
1474 hidpp->name);
1475 break;
1476 default:
1477 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1478 break;
1479 }
1480
1481 return status;
1482}
1483
1484static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp,
1485 u8 battery_level)
1486{
1487
1488 battery_level &= hidpp->battery.supported_levels_1004;
1489
1490 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL)
1491 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1492 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD)
1493 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1494 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW)
1495 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1496 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL)
1497 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1498
1499 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1500}
1501
1502static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp,
1503 u8 feature_index,
1504 u8 *state_of_charge,
1505 int *status,
1506 int *level)
1507{
1508 struct hidpp_report response;
1509 int ret;
1510 u8 *params = (u8 *)response.fap.params;
1511
1512 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1513 CMD_UNIFIED_BATTERY_GET_STATUS,
1514 NULL, 0, &response);
1515
1516 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1517 return -EIO;
1518 if (ret > 0) {
1519 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1520 __func__, ret);
1521 return -EPROTO;
1522 }
1523 if (ret)
1524 return ret;
1525
1526 *state_of_charge = params[0];
1527 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1528 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1529
1530 return 0;
1531}
1532
1533static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp)
1534{
1535 u8 feature_type;
1536 int ret;
1537 u8 state_of_charge;
1538 int status, level;
1539
1540 if (hidpp->battery.feature_index == 0xff) {
1541 ret = hidpp_root_get_feature(hidpp,
1542 HIDPP_PAGE_UNIFIED_BATTERY,
1543 &hidpp->battery.feature_index,
1544 &feature_type);
1545 if (ret)
1546 return ret;
1547 }
1548
1549 ret = hidpp20_unifiedbattery_get_capabilities(hidpp,
1550 hidpp->battery.feature_index);
1551 if (ret)
1552 return ret;
1553
1554 ret = hidpp20_unifiedbattery_get_status(hidpp,
1555 hidpp->battery.feature_index,
1556 &state_of_charge,
1557 &status,
1558 &level);
1559 if (ret)
1560 return ret;
1561
1562 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY;
1563 hidpp->battery.capacity = state_of_charge;
1564 hidpp->battery.status = status;
1565 hidpp->battery.level = level;
1566 hidpp->battery.online = true;
1567
1568 return 0;
1569}
1570
1571static int hidpp20_battery_event_1004(struct hidpp_device *hidpp,
1572 u8 *data, int size)
1573{
1574 struct hidpp_report *report = (struct hidpp_report *)data;
1575 u8 *params = (u8 *)report->fap.params;
1576 int state_of_charge, status, level;
1577 bool changed;
1578
1579 if (report->fap.feature_index != hidpp->battery.feature_index ||
1580 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT)
1581 return 0;
1582
1583 state_of_charge = params[0];
1584 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]);
1585 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]);
1586
1587 changed = status != hidpp->battery.status ||
1588 (state_of_charge != hidpp->battery.capacity &&
1589 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) ||
1590 (level != hidpp->battery.level &&
1591 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS);
1592
1593 if (changed) {
1594 hidpp->battery.capacity = state_of_charge;
1595 hidpp->battery.status = status;
1596 hidpp->battery.level = level;
1597 if (hidpp->battery.ps)
1598 power_supply_changed(hidpp->battery.ps);
1599 }
1600
1601 return 0;
1602}
1603
1604
1605
1606
1607
1608static enum power_supply_property hidpp_battery_props[] = {
1609 POWER_SUPPLY_PROP_ONLINE,
1610 POWER_SUPPLY_PROP_STATUS,
1611 POWER_SUPPLY_PROP_SCOPE,
1612 POWER_SUPPLY_PROP_MODEL_NAME,
1613 POWER_SUPPLY_PROP_MANUFACTURER,
1614 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1615 0,
1616 0,
1617 0,
1618};
1619
1620static int hidpp_battery_get_property(struct power_supply *psy,
1621 enum power_supply_property psp,
1622 union power_supply_propval *val)
1623{
1624 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1625 int ret = 0;
1626
1627 switch(psp) {
1628 case POWER_SUPPLY_PROP_STATUS:
1629 val->intval = hidpp->battery.status;
1630 break;
1631 case POWER_SUPPLY_PROP_CAPACITY:
1632 val->intval = hidpp->battery.capacity;
1633 break;
1634 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1635 val->intval = hidpp->battery.level;
1636 break;
1637 case POWER_SUPPLY_PROP_SCOPE:
1638 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1639 break;
1640 case POWER_SUPPLY_PROP_ONLINE:
1641 val->intval = hidpp->battery.online;
1642 break;
1643 case POWER_SUPPLY_PROP_MODEL_NAME:
1644 if (!strncmp(hidpp->name, "Logitech ", 9))
1645 val->strval = hidpp->name + 9;
1646 else
1647 val->strval = hidpp->name;
1648 break;
1649 case POWER_SUPPLY_PROP_MANUFACTURER:
1650 val->strval = "Logitech";
1651 break;
1652 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1653 val->strval = hidpp->hid_dev->uniq;
1654 break;
1655 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1656
1657 val->intval = hidpp->battery.voltage * 1000;
1658 break;
1659 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1660 val->intval = hidpp->battery.charge_type;
1661 break;
1662 default:
1663 ret = -EINVAL;
1664 break;
1665 }
1666
1667 return ret;
1668}
1669
1670
1671
1672
1673#define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b
1674
1675static int hidpp_set_wireless_feature_index(struct hidpp_device *hidpp)
1676{
1677 u8 feature_type;
1678 int ret;
1679
1680 ret = hidpp_root_get_feature(hidpp,
1681 HIDPP_PAGE_WIRELESS_DEVICE_STATUS,
1682 &hidpp->wireless_feature_index,
1683 &feature_type);
1684
1685 return ret;
1686}
1687
1688
1689
1690
1691
1692#define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1693
1694#define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1695
1696static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1697 bool enabled, u8 *multiplier)
1698{
1699 u8 feature_index;
1700 u8 feature_type;
1701 int ret;
1702 u8 params[1];
1703 struct hidpp_report response;
1704
1705 ret = hidpp_root_get_feature(hidpp,
1706 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1707 &feature_index,
1708 &feature_type);
1709 if (ret)
1710 return ret;
1711
1712 params[0] = enabled ? BIT(0) : 0;
1713 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1714 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1715 params, sizeof(params), &response);
1716 if (ret)
1717 return ret;
1718 *multiplier = response.fap.params[1];
1719 return 0;
1720}
1721
1722
1723
1724
1725
1726#define HIDPP_PAGE_HIRES_WHEEL 0x2121
1727
1728#define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1729#define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1730
1731static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1732 u8 *multiplier)
1733{
1734 u8 feature_index;
1735 u8 feature_type;
1736 int ret;
1737 struct hidpp_report response;
1738
1739 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1740 &feature_index, &feature_type);
1741 if (ret)
1742 goto return_default;
1743
1744 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1745 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1746 NULL, 0, &response);
1747 if (ret)
1748 goto return_default;
1749
1750 *multiplier = response.fap.params[0];
1751 return 0;
1752return_default:
1753 hid_warn(hidpp->hid_dev,
1754 "Couldn't get wheel multiplier (error %d)\n", ret);
1755 return ret;
1756}
1757
1758static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1759 bool high_resolution, bool use_hidpp)
1760{
1761 u8 feature_index;
1762 u8 feature_type;
1763 int ret;
1764 u8 params[1];
1765 struct hidpp_report response;
1766
1767 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1768 &feature_index, &feature_type);
1769 if (ret)
1770 return ret;
1771
1772 params[0] = (invert ? BIT(2) : 0) |
1773 (high_resolution ? BIT(1) : 0) |
1774 (use_hidpp ? BIT(0) : 0);
1775
1776 return hidpp_send_fap_command_sync(hidpp, feature_index,
1777 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1778 params, sizeof(params), &response);
1779}
1780
1781
1782
1783
1784
1785#define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1786
1787#define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1788
1789#define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1790#define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1791#define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1792
1793static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1794{
1795 struct hidpp_report response;
1796 u8 params[2] = { 1, 1 };
1797 u8 feature_type;
1798 int ret;
1799
1800 if (hidpp->battery.feature_index == 0xff) {
1801 ret = hidpp_root_get_feature(hidpp,
1802 HIDPP_PAGE_SOLAR_KEYBOARD,
1803 &hidpp->battery.solar_feature_index,
1804 &feature_type);
1805 if (ret)
1806 return ret;
1807 }
1808
1809 ret = hidpp_send_fap_command_sync(hidpp,
1810 hidpp->battery.solar_feature_index,
1811 CMD_SOLAR_SET_LIGHT_MEASURE,
1812 params, 2, &response);
1813 if (ret > 0) {
1814 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1815 __func__, ret);
1816 return -EPROTO;
1817 }
1818 if (ret)
1819 return ret;
1820
1821 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1822
1823 return 0;
1824}
1825
1826static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1827 u8 *data, int size)
1828{
1829 struct hidpp_report *report = (struct hidpp_report *)data;
1830 int capacity, lux, status;
1831 u8 function;
1832
1833 function = report->fap.funcindex_clientid;
1834
1835
1836 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1837 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1838 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1839 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1840 return 0;
1841
1842 capacity = report->fap.params[0];
1843
1844 switch (function) {
1845 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1846 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1847 if (lux > 200)
1848 status = POWER_SUPPLY_STATUS_CHARGING;
1849 else
1850 status = POWER_SUPPLY_STATUS_DISCHARGING;
1851 break;
1852 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1853 default:
1854 if (capacity < hidpp->battery.capacity)
1855 status = POWER_SUPPLY_STATUS_DISCHARGING;
1856 else
1857 status = POWER_SUPPLY_STATUS_CHARGING;
1858
1859 }
1860
1861 if (capacity == 100)
1862 status = POWER_SUPPLY_STATUS_FULL;
1863
1864 hidpp->battery.online = true;
1865 if (capacity != hidpp->battery.capacity ||
1866 status != hidpp->battery.status) {
1867 hidpp->battery.capacity = capacity;
1868 hidpp->battery.status = status;
1869 if (hidpp->battery.ps)
1870 power_supply_changed(hidpp->battery.ps);
1871 }
1872
1873 return 0;
1874}
1875
1876
1877
1878
1879
1880#define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1881
1882#define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1883
1884struct hidpp_touchpad_fw_items {
1885 uint8_t presence;
1886 uint8_t desired_state;
1887 uint8_t state;
1888 uint8_t persistent;
1889};
1890
1891
1892
1893
1894
1895static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1896 u8 feature_index,
1897 struct hidpp_touchpad_fw_items *items)
1898{
1899 struct hidpp_report response;
1900 int ret;
1901 u8 *params = (u8 *)response.fap.params;
1902
1903 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1904 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1905
1906 if (ret > 0) {
1907 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1908 __func__, ret);
1909 return -EPROTO;
1910 }
1911 if (ret)
1912 return ret;
1913
1914 items->presence = params[0];
1915 items->desired_state = params[1];
1916 items->state = params[2];
1917 items->persistent = params[3];
1918
1919 return 0;
1920}
1921
1922
1923
1924
1925
1926#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1927
1928#define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1929#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1930
1931#define EVENT_TOUCHPAD_RAW_XY 0x00
1932
1933#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1934#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1935
1936struct hidpp_touchpad_raw_info {
1937 u16 x_size;
1938 u16 y_size;
1939 u8 z_range;
1940 u8 area_range;
1941 u8 timestamp_unit;
1942 u8 maxcontacts;
1943 u8 origin;
1944 u16 res;
1945};
1946
1947struct hidpp_touchpad_raw_xy_finger {
1948 u8 contact_type;
1949 u8 contact_status;
1950 u16 x;
1951 u16 y;
1952 u8 z;
1953 u8 area;
1954 u8 finger_id;
1955};
1956
1957struct hidpp_touchpad_raw_xy {
1958 u16 timestamp;
1959 struct hidpp_touchpad_raw_xy_finger fingers[2];
1960 u8 spurious_flag;
1961 u8 end_of_frame;
1962 u8 finger_count;
1963 u8 button;
1964};
1965
1966static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1967 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1968{
1969 struct hidpp_report response;
1970 int ret;
1971 u8 *params = (u8 *)response.fap.params;
1972
1973 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1974 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1975
1976 if (ret > 0) {
1977 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1978 __func__, ret);
1979 return -EPROTO;
1980 }
1981 if (ret)
1982 return ret;
1983
1984 raw_info->x_size = get_unaligned_be16(¶ms[0]);
1985 raw_info->y_size = get_unaligned_be16(¶ms[2]);
1986 raw_info->z_range = params[4];
1987 raw_info->area_range = params[5];
1988 raw_info->maxcontacts = params[7];
1989 raw_info->origin = params[8];
1990
1991 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51;
1992
1993 return ret;
1994}
1995
1996static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1997 u8 feature_index, bool send_raw_reports,
1998 bool sensor_enhanced_settings)
1999{
2000 struct hidpp_report response;
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
2012
2013 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
2014 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response);
2015}
2016
2017static void hidpp_touchpad_touch_event(u8 *data,
2018 struct hidpp_touchpad_raw_xy_finger *finger)
2019{
2020 u8 x_m = data[0] << 2;
2021 u8 y_m = data[2] << 2;
2022
2023 finger->x = x_m << 6 | data[1];
2024 finger->y = y_m << 6 | data[3];
2025
2026 finger->contact_type = data[0] >> 6;
2027 finger->contact_status = data[2] >> 6;
2028
2029 finger->z = data[4];
2030 finger->area = data[5];
2031 finger->finger_id = data[6] >> 4;
2032}
2033
2034static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
2035 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
2036{
2037 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
2038 raw_xy->end_of_frame = data[8] & 0x01;
2039 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
2040 raw_xy->finger_count = data[15] & 0x0f;
2041 raw_xy->button = (data[8] >> 2) & 0x01;
2042
2043 if (raw_xy->finger_count) {
2044 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
2045 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
2046 }
2047}
2048
2049
2050
2051
2052
2053#define HIDPP_FF_GET_INFO 0x01
2054#define HIDPP_FF_RESET_ALL 0x11
2055#define HIDPP_FF_DOWNLOAD_EFFECT 0x21
2056#define HIDPP_FF_SET_EFFECT_STATE 0x31
2057#define HIDPP_FF_DESTROY_EFFECT 0x41
2058#define HIDPP_FF_GET_APERTURE 0x51
2059#define HIDPP_FF_SET_APERTURE 0x61
2060#define HIDPP_FF_GET_GLOBAL_GAINS 0x71
2061#define HIDPP_FF_SET_GLOBAL_GAINS 0x81
2062
2063#define HIDPP_FF_EFFECT_STATE_GET 0x00
2064#define HIDPP_FF_EFFECT_STATE_STOP 0x01
2065#define HIDPP_FF_EFFECT_STATE_PLAY 0x02
2066#define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
2067
2068#define HIDPP_FF_EFFECT_CONSTANT 0x00
2069#define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
2070#define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
2071#define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
2072#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
2073#define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
2074#define HIDPP_FF_EFFECT_SPRING 0x06
2075#define HIDPP_FF_EFFECT_DAMPER 0x07
2076#define HIDPP_FF_EFFECT_FRICTION 0x08
2077#define HIDPP_FF_EFFECT_INERTIA 0x09
2078#define HIDPP_FF_EFFECT_RAMP 0x0A
2079
2080#define HIDPP_FF_EFFECT_AUTOSTART 0x80
2081
2082#define HIDPP_FF_EFFECTID_NONE -1
2083#define HIDPP_FF_EFFECTID_AUTOCENTER -2
2084#define HIDPP_AUTOCENTER_PARAMS_LENGTH 18
2085
2086#define HIDPP_FF_MAX_PARAMS 20
2087#define HIDPP_FF_RESERVED_SLOTS 1
2088
2089struct hidpp_ff_private_data {
2090 struct hidpp_device *hidpp;
2091 u8 feature_index;
2092 u8 version;
2093 u16 gain;
2094 s16 range;
2095 u8 slot_autocenter;
2096 u8 num_effects;
2097 int *effect_ids;
2098 struct workqueue_struct *wq;
2099 atomic_t workqueue_size;
2100};
2101
2102struct hidpp_ff_work_data {
2103 struct work_struct work;
2104 struct hidpp_ff_private_data *data;
2105 int effect_id;
2106 u8 command;
2107 u8 params[HIDPP_FF_MAX_PARAMS];
2108 u8 size;
2109};
2110
2111static const signed short hidpp_ff_effects[] = {
2112 FF_CONSTANT,
2113 FF_PERIODIC,
2114 FF_SINE,
2115 FF_SQUARE,
2116 FF_SAW_UP,
2117 FF_SAW_DOWN,
2118 FF_TRIANGLE,
2119 FF_SPRING,
2120 FF_DAMPER,
2121 FF_AUTOCENTER,
2122 FF_GAIN,
2123 -1
2124};
2125
2126static const signed short hidpp_ff_effects_v2[] = {
2127 FF_RAMP,
2128 FF_FRICTION,
2129 FF_INERTIA,
2130 -1
2131};
2132
2133static const u8 HIDPP_FF_CONDITION_CMDS[] = {
2134 HIDPP_FF_EFFECT_SPRING,
2135 HIDPP_FF_EFFECT_FRICTION,
2136 HIDPP_FF_EFFECT_DAMPER,
2137 HIDPP_FF_EFFECT_INERTIA
2138};
2139
2140static const char *HIDPP_FF_CONDITION_NAMES[] = {
2141 "spring",
2142 "friction",
2143 "damper",
2144 "inertia"
2145};
2146
2147
2148static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
2149{
2150 int i;
2151
2152 for (i = 0; i < data->num_effects; i++)
2153 if (data->effect_ids[i] == effect_id)
2154 return i+1;
2155
2156 return 0;
2157}
2158
2159static void hidpp_ff_work_handler(struct work_struct *w)
2160{
2161 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
2162 struct hidpp_ff_private_data *data = wd->data;
2163 struct hidpp_report response;
2164 u8 slot;
2165 int ret;
2166
2167
2168 switch (wd->effect_id) {
2169 case HIDPP_FF_EFFECTID_AUTOCENTER:
2170 wd->params[0] = data->slot_autocenter;
2171 break;
2172 case HIDPP_FF_EFFECTID_NONE:
2173
2174 break;
2175 default:
2176
2177 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
2178 break;
2179 }
2180
2181
2182 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
2183 wd->command, wd->params, wd->size, &response);
2184
2185 if (ret) {
2186 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
2187 goto out;
2188 }
2189
2190
2191 switch (wd->command) {
2192 case HIDPP_FF_DOWNLOAD_EFFECT:
2193 slot = response.fap.params[0];
2194 if (slot > 0 && slot <= data->num_effects) {
2195 if (wd->effect_id >= 0)
2196
2197 data->effect_ids[slot-1] = wd->effect_id;
2198 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2199
2200 data->slot_autocenter = slot;
2201 }
2202 break;
2203 case HIDPP_FF_DESTROY_EFFECT:
2204 if (wd->effect_id >= 0)
2205
2206 data->effect_ids[wd->params[0]-1] = -1;
2207 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
2208
2209 data->slot_autocenter = 0;
2210 break;
2211 case HIDPP_FF_SET_GLOBAL_GAINS:
2212 data->gain = (wd->params[0] << 8) + wd->params[1];
2213 break;
2214 case HIDPP_FF_SET_APERTURE:
2215 data->range = (wd->params[0] << 8) + wd->params[1];
2216 break;
2217 default:
2218
2219 break;
2220 }
2221
2222out:
2223 atomic_dec(&data->workqueue_size);
2224 kfree(wd);
2225}
2226
2227static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
2228{
2229 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
2230 int s;
2231
2232 if (!wd)
2233 return -ENOMEM;
2234
2235 INIT_WORK(&wd->work, hidpp_ff_work_handler);
2236
2237 wd->data = data;
2238 wd->effect_id = effect_id;
2239 wd->command = command;
2240 wd->size = size;
2241 memcpy(wd->params, params, size);
2242
2243 atomic_inc(&data->workqueue_size);
2244 queue_work(data->wq, &wd->work);
2245
2246
2247 s = atomic_read(&data->workqueue_size);
2248 if (s >= 20 && s % 20 == 0)
2249 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
2250
2251 return 0;
2252}
2253
2254static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
2255{
2256 struct hidpp_ff_private_data *data = dev->ff->private;
2257 u8 params[20];
2258 u8 size;
2259 int force;
2260
2261
2262 params[2] = effect->replay.length >> 8;
2263 params[3] = effect->replay.length & 255;
2264 params[4] = effect->replay.delay >> 8;
2265 params[5] = effect->replay.delay & 255;
2266
2267 switch (effect->type) {
2268 case FF_CONSTANT:
2269 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2270 params[1] = HIDPP_FF_EFFECT_CONSTANT;
2271 params[6] = force >> 8;
2272 params[7] = force & 255;
2273 params[8] = effect->u.constant.envelope.attack_level >> 7;
2274 params[9] = effect->u.constant.envelope.attack_length >> 8;
2275 params[10] = effect->u.constant.envelope.attack_length & 255;
2276 params[11] = effect->u.constant.envelope.fade_level >> 7;
2277 params[12] = effect->u.constant.envelope.fade_length >> 8;
2278 params[13] = effect->u.constant.envelope.fade_length & 255;
2279 size = 14;
2280 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
2281 effect->u.constant.level,
2282 effect->direction, force);
2283 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2284 effect->u.constant.envelope.attack_level,
2285 effect->u.constant.envelope.attack_length,
2286 effect->u.constant.envelope.fade_level,
2287 effect->u.constant.envelope.fade_length);
2288 break;
2289 case FF_PERIODIC:
2290 {
2291 switch (effect->u.periodic.waveform) {
2292 case FF_SINE:
2293 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
2294 break;
2295 case FF_SQUARE:
2296 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
2297 break;
2298 case FF_SAW_UP:
2299 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
2300 break;
2301 case FF_SAW_DOWN:
2302 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
2303 break;
2304 case FF_TRIANGLE:
2305 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
2306 break;
2307 default:
2308 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
2309 return -EINVAL;
2310 }
2311 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2312 params[6] = effect->u.periodic.magnitude >> 8;
2313 params[7] = effect->u.periodic.magnitude & 255;
2314 params[8] = effect->u.periodic.offset >> 8;
2315 params[9] = effect->u.periodic.offset & 255;
2316 params[10] = effect->u.periodic.period >> 8;
2317 params[11] = effect->u.periodic.period & 255;
2318 params[12] = effect->u.periodic.phase >> 8;
2319 params[13] = effect->u.periodic.phase & 255;
2320 params[14] = effect->u.periodic.envelope.attack_level >> 7;
2321 params[15] = effect->u.periodic.envelope.attack_length >> 8;
2322 params[16] = effect->u.periodic.envelope.attack_length & 255;
2323 params[17] = effect->u.periodic.envelope.fade_level >> 7;
2324 params[18] = effect->u.periodic.envelope.fade_length >> 8;
2325 params[19] = effect->u.periodic.envelope.fade_length & 255;
2326 size = 20;
2327 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
2328 effect->u.periodic.magnitude, effect->direction,
2329 effect->u.periodic.offset,
2330 effect->u.periodic.period,
2331 effect->u.periodic.phase);
2332 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2333 effect->u.periodic.envelope.attack_level,
2334 effect->u.periodic.envelope.attack_length,
2335 effect->u.periodic.envelope.fade_level,
2336 effect->u.periodic.envelope.fade_length);
2337 break;
2338 }
2339 case FF_RAMP:
2340 params[1] = HIDPP_FF_EFFECT_RAMP;
2341 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2342 params[6] = force >> 8;
2343 params[7] = force & 255;
2344 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
2345 params[8] = force >> 8;
2346 params[9] = force & 255;
2347 params[10] = effect->u.ramp.envelope.attack_level >> 7;
2348 params[11] = effect->u.ramp.envelope.attack_length >> 8;
2349 params[12] = effect->u.ramp.envelope.attack_length & 255;
2350 params[13] = effect->u.ramp.envelope.fade_level >> 7;
2351 params[14] = effect->u.ramp.envelope.fade_length >> 8;
2352 params[15] = effect->u.ramp.envelope.fade_length & 255;
2353 size = 16;
2354 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
2355 effect->u.ramp.start_level,
2356 effect->u.ramp.end_level,
2357 effect->direction, force);
2358 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
2359 effect->u.ramp.envelope.attack_level,
2360 effect->u.ramp.envelope.attack_length,
2361 effect->u.ramp.envelope.fade_level,
2362 effect->u.ramp.envelope.fade_length);
2363 break;
2364 case FF_FRICTION:
2365 case FF_INERTIA:
2366 case FF_SPRING:
2367 case FF_DAMPER:
2368 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
2369 params[6] = effect->u.condition[0].left_saturation >> 9;
2370 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
2371 params[8] = effect->u.condition[0].left_coeff >> 8;
2372 params[9] = effect->u.condition[0].left_coeff & 255;
2373 params[10] = effect->u.condition[0].deadband >> 9;
2374 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
2375 params[12] = effect->u.condition[0].center >> 8;
2376 params[13] = effect->u.condition[0].center & 255;
2377 params[14] = effect->u.condition[0].right_coeff >> 8;
2378 params[15] = effect->u.condition[0].right_coeff & 255;
2379 params[16] = effect->u.condition[0].right_saturation >> 9;
2380 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
2381 size = 18;
2382 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
2383 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
2384 effect->u.condition[0].left_coeff,
2385 effect->u.condition[0].left_saturation,
2386 effect->u.condition[0].right_coeff,
2387 effect->u.condition[0].right_saturation);
2388 dbg_hid(" deadband=%d, center=%d\n",
2389 effect->u.condition[0].deadband,
2390 effect->u.condition[0].center);
2391 break;
2392 default:
2393 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
2394 return -EINVAL;
2395 }
2396
2397 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
2398}
2399
2400static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
2401{
2402 struct hidpp_ff_private_data *data = dev->ff->private;
2403 u8 params[2];
2404
2405 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
2406
2407 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
2408
2409 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2410}
2411
2412static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2413{
2414 struct hidpp_ff_private_data *data = dev->ff->private;
2415 u8 slot = 0;
2416
2417 dbg_hid("Erasing effect %d.\n", effect_id);
2418
2419 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2420}
2421
2422static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2423{
2424 struct hidpp_ff_private_data *data = dev->ff->private;
2425 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH];
2426
2427 dbg_hid("Setting autocenter to %d.\n", magnitude);
2428
2429
2430 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2431
2432 params[2] = params[3] = params[4] = params[5] = 0;
2433
2434 params[8] = params[14] = magnitude >> 11;
2435 params[9] = params[15] = (magnitude >> 3) & 255;
2436 params[6] = params[16] = magnitude >> 9;
2437 params[7] = params[17] = (magnitude >> 1) & 255;
2438
2439 params[10] = params[11] = params[12] = params[13] = 0;
2440
2441 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2442}
2443
2444static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2445{
2446 struct hidpp_ff_private_data *data = dev->ff->private;
2447 u8 params[4];
2448
2449 dbg_hid("Setting gain to %d.\n", gain);
2450
2451 params[0] = gain >> 8;
2452 params[1] = gain & 255;
2453 params[2] = 0;
2454 params[3] = 0;
2455
2456 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2457}
2458
2459static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2460{
2461 struct hid_device *hid = to_hid_device(dev);
2462 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2463 struct input_dev *idev = hidinput->input;
2464 struct hidpp_ff_private_data *data = idev->ff->private;
2465
2466 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2467}
2468
2469static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2470{
2471 struct hid_device *hid = to_hid_device(dev);
2472 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2473 struct input_dev *idev = hidinput->input;
2474 struct hidpp_ff_private_data *data = idev->ff->private;
2475 u8 params[2];
2476 int range = simple_strtoul(buf, NULL, 10);
2477
2478 range = clamp(range, 180, 900);
2479
2480 params[0] = range >> 8;
2481 params[1] = range & 0x00FF;
2482
2483 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2484
2485 return count;
2486}
2487
2488static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2489
2490static void hidpp_ff_destroy(struct ff_device *ff)
2491{
2492 struct hidpp_ff_private_data *data = ff->private;
2493 struct hid_device *hid = data->hidpp->hid_dev;
2494
2495 hid_info(hid, "Unloading HID++ force feedback.\n");
2496
2497 device_remove_file(&hid->dev, &dev_attr_range);
2498 destroy_workqueue(data->wq);
2499 kfree(data->effect_ids);
2500}
2501
2502static int hidpp_ff_init(struct hidpp_device *hidpp,
2503 struct hidpp_ff_private_data *data)
2504{
2505 struct hid_device *hid = hidpp->hid_dev;
2506 struct hid_input *hidinput;
2507 struct input_dev *dev;
2508 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2509 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2510 struct ff_device *ff;
2511 int error, j, num_slots = data->num_effects;
2512 u8 version;
2513
2514 if (list_empty(&hid->inputs)) {
2515 hid_err(hid, "no inputs found\n");
2516 return -ENODEV;
2517 }
2518 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2519 dev = hidinput->input;
2520
2521 if (!dev) {
2522 hid_err(hid, "Struct input_dev not set!\n");
2523 return -EINVAL;
2524 }
2525
2526
2527 version = bcdDevice & 255;
2528
2529
2530 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2531 set_bit(hidpp_ff_effects[j], dev->ffbit);
2532 if (version > 1)
2533 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2534 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2535
2536 error = input_ff_create(dev, num_slots);
2537
2538 if (error) {
2539 hid_err(dev, "Failed to create FF device!\n");
2540 return error;
2541 }
2542
2543
2544
2545
2546 data = kmemdup(data, sizeof(*data), GFP_KERNEL);
2547 if (!data)
2548 return -ENOMEM;
2549 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2550 if (!data->effect_ids) {
2551 kfree(data);
2552 return -ENOMEM;
2553 }
2554 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2555 if (!data->wq) {
2556 kfree(data->effect_ids);
2557 kfree(data);
2558 return -ENOMEM;
2559 }
2560
2561 data->hidpp = hidpp;
2562 data->version = version;
2563 for (j = 0; j < num_slots; j++)
2564 data->effect_ids[j] = -1;
2565
2566 ff = dev->ff;
2567 ff->private = data;
2568
2569 ff->upload = hidpp_ff_upload_effect;
2570 ff->erase = hidpp_ff_erase_effect;
2571 ff->playback = hidpp_ff_playback;
2572 ff->set_gain = hidpp_ff_set_gain;
2573 ff->set_autocenter = hidpp_ff_set_autocenter;
2574 ff->destroy = hidpp_ff_destroy;
2575
2576
2577 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2578 if (error)
2579 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2580
2581
2582 atomic_set(&data->workqueue_size, 0);
2583
2584 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2585 version);
2586
2587 return 0;
2588}
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600#define WTP_MANUAL_RESOLUTION 39
2601
2602struct wtp_data {
2603 u16 x_size, y_size;
2604 u8 finger_count;
2605 u8 mt_feature_index;
2606 u8 button_feature_index;
2607 u8 maxcontacts;
2608 bool flip_y;
2609 unsigned int resolution;
2610};
2611
2612static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2613 struct hid_field *field, struct hid_usage *usage,
2614 unsigned long **bit, int *max)
2615{
2616 return -1;
2617}
2618
2619static void wtp_populate_input(struct hidpp_device *hidpp,
2620 struct input_dev *input_dev)
2621{
2622 struct wtp_data *wd = hidpp->private_data;
2623
2624 __set_bit(EV_ABS, input_dev->evbit);
2625 __set_bit(EV_KEY, input_dev->evbit);
2626 __clear_bit(EV_REL, input_dev->evbit);
2627 __clear_bit(EV_LED, input_dev->evbit);
2628
2629 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2630 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2631 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2632 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2633
2634
2635 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2636
2637 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2638
2639 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2640 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2641 else
2642 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2643
2644 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2645 INPUT_MT_DROP_UNUSED);
2646}
2647
2648static void wtp_touch_event(struct hidpp_device *hidpp,
2649 struct hidpp_touchpad_raw_xy_finger *touch_report)
2650{
2651 struct wtp_data *wd = hidpp->private_data;
2652 int slot;
2653
2654 if (!touch_report->finger_id || touch_report->contact_type)
2655
2656 return;
2657
2658 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2659
2660 input_mt_slot(hidpp->input, slot);
2661 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2662 touch_report->contact_status);
2663 if (touch_report->contact_status) {
2664 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2665 touch_report->x);
2666 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2667 wd->flip_y ? wd->y_size - touch_report->y :
2668 touch_report->y);
2669 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2670 touch_report->area);
2671 }
2672}
2673
2674static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2675 struct hidpp_touchpad_raw_xy *raw)
2676{
2677 int i;
2678
2679 for (i = 0; i < 2; i++)
2680 wtp_touch_event(hidpp, &(raw->fingers[i]));
2681
2682 if (raw->end_of_frame &&
2683 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2684 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2685
2686 if (raw->end_of_frame || raw->finger_count <= 2) {
2687 input_mt_sync_frame(hidpp->input);
2688 input_sync(hidpp->input);
2689 }
2690}
2691
2692static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2693{
2694 struct wtp_data *wd = hidpp->private_data;
2695 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2696 (data[7] >> 4) * (data[7] >> 4)) / 2;
2697 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2698 (data[13] >> 4) * (data[13] >> 4)) / 2;
2699 struct hidpp_touchpad_raw_xy raw = {
2700 .timestamp = data[1],
2701 .fingers = {
2702 {
2703 .contact_type = 0,
2704 .contact_status = !!data[7],
2705 .x = get_unaligned_le16(&data[3]),
2706 .y = get_unaligned_le16(&data[5]),
2707 .z = c1_area,
2708 .area = c1_area,
2709 .finger_id = data[2],
2710 }, {
2711 .contact_type = 0,
2712 .contact_status = !!data[13],
2713 .x = get_unaligned_le16(&data[9]),
2714 .y = get_unaligned_le16(&data[11]),
2715 .z = c2_area,
2716 .area = c2_area,
2717 .finger_id = data[8],
2718 }
2719 },
2720 .finger_count = wd->maxcontacts,
2721 .spurious_flag = 0,
2722 .end_of_frame = (data[0] >> 7) == 0,
2723 .button = data[0] & 0x01,
2724 };
2725
2726 wtp_send_raw_xy_event(hidpp, &raw);
2727
2728 return 1;
2729}
2730
2731static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2732{
2733 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2734 struct wtp_data *wd = hidpp->private_data;
2735 struct hidpp_report *report = (struct hidpp_report *)data;
2736 struct hidpp_touchpad_raw_xy raw;
2737
2738 if (!wd || !hidpp->input)
2739 return 1;
2740
2741 switch (data[0]) {
2742 case 0x02:
2743 if (size < 2) {
2744 hid_err(hdev, "Received HID report of bad size (%d)",
2745 size);
2746 return 1;
2747 }
2748 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2749 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2750 !!(data[1] & 0x01));
2751 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2752 !!(data[1] & 0x02));
2753 input_sync(hidpp->input);
2754 return 0;
2755 } else {
2756 if (size < 21)
2757 return 1;
2758 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2759 }
2760 case REPORT_ID_HIDPP_LONG:
2761
2762 if ((report->fap.feature_index != wd->mt_feature_index) ||
2763 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2764 return 1;
2765 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2766
2767 wtp_send_raw_xy_event(hidpp, &raw);
2768 return 0;
2769 }
2770
2771 return 0;
2772}
2773
2774static int wtp_get_config(struct hidpp_device *hidpp)
2775{
2776 struct wtp_data *wd = hidpp->private_data;
2777 struct hidpp_touchpad_raw_info raw_info = {0};
2778 u8 feature_type;
2779 int ret;
2780
2781 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2782 &wd->mt_feature_index, &feature_type);
2783 if (ret)
2784
2785 return ret;
2786
2787 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2788 &raw_info);
2789 if (ret)
2790 return ret;
2791
2792 wd->x_size = raw_info.x_size;
2793 wd->y_size = raw_info.y_size;
2794 wd->maxcontacts = raw_info.maxcontacts;
2795 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2796 wd->resolution = raw_info.res;
2797 if (!wd->resolution)
2798 wd->resolution = WTP_MANUAL_RESOLUTION;
2799
2800 return 0;
2801}
2802
2803static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2804{
2805 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2806 struct wtp_data *wd;
2807
2808 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2809 GFP_KERNEL);
2810 if (!wd)
2811 return -ENOMEM;
2812
2813 hidpp->private_data = wd;
2814
2815 return 0;
2816};
2817
2818static int wtp_connect(struct hid_device *hdev, bool connected)
2819{
2820 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2821 struct wtp_data *wd = hidpp->private_data;
2822 int ret;
2823
2824 if (!wd->x_size) {
2825 ret = wtp_get_config(hidpp);
2826 if (ret) {
2827 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2828 return ret;
2829 }
2830 }
2831
2832 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2833 true, true);
2834}
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2870
2871
2872#define M560_MOUSE_BTN_LEFT 0x01
2873#define M560_MOUSE_BTN_RIGHT 0x02
2874#define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2875#define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2876
2877#define M560_SUB_ID 0x0a
2878#define M560_BUTTON_MODE_REGISTER 0x35
2879
2880static int m560_send_config_command(struct hid_device *hdev, bool connected)
2881{
2882 struct hidpp_report response;
2883 struct hidpp_device *hidpp_dev;
2884
2885 hidpp_dev = hid_get_drvdata(hdev);
2886
2887 return hidpp_send_rap_command_sync(
2888 hidpp_dev,
2889 REPORT_ID_HIDPP_SHORT,
2890 M560_SUB_ID,
2891 M560_BUTTON_MODE_REGISTER,
2892 (u8 *)m560_config_parameter,
2893 sizeof(m560_config_parameter),
2894 &response
2895 );
2896}
2897
2898static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2899{
2900 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2901
2902
2903 if (!hidpp->input) {
2904 hid_err(hdev, "error in parameter\n");
2905 return -EINVAL;
2906 }
2907
2908 if (size < 7) {
2909 hid_err(hdev, "error in report\n");
2910 return 0;
2911 }
2912
2913 if (data[0] == REPORT_ID_HIDPP_LONG &&
2914 data[2] == M560_SUB_ID && data[6] == 0x00) {
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928 switch (data[5]) {
2929 case 0xaf:
2930 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2931 break;
2932 case 0xb0:
2933 input_report_key(hidpp->input, BTN_FORWARD, 1);
2934 break;
2935 case 0xae:
2936 input_report_key(hidpp->input, BTN_BACK, 1);
2937 break;
2938 case 0x00:
2939 input_report_key(hidpp->input, BTN_BACK, 0);
2940 input_report_key(hidpp->input, BTN_FORWARD, 0);
2941 input_report_key(hidpp->input, BTN_MIDDLE, 0);
2942 break;
2943 default:
2944 hid_err(hdev, "error in report\n");
2945 return 0;
2946 }
2947 input_sync(hidpp->input);
2948
2949 } else if (data[0] == 0x02) {
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959 int v;
2960
2961 input_report_key(hidpp->input, BTN_LEFT,
2962 !!(data[1] & M560_MOUSE_BTN_LEFT));
2963 input_report_key(hidpp->input, BTN_RIGHT,
2964 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2965
2966 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
2967 input_report_rel(hidpp->input, REL_HWHEEL, -1);
2968 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2969 -120);
2970 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
2971 input_report_rel(hidpp->input, REL_HWHEEL, 1);
2972 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2973 120);
2974 }
2975
2976 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2977 input_report_rel(hidpp->input, REL_X, v);
2978
2979 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2980 input_report_rel(hidpp->input, REL_Y, v);
2981
2982 v = hid_snto32(data[6], 8);
2983 if (v != 0)
2984 hidpp_scroll_counter_handle_scroll(hidpp->input,
2985 &hidpp->vertical_wheel_counter, v);
2986
2987 input_sync(hidpp->input);
2988 }
2989
2990 return 1;
2991}
2992
2993static void m560_populate_input(struct hidpp_device *hidpp,
2994 struct input_dev *input_dev)
2995{
2996 __set_bit(EV_KEY, input_dev->evbit);
2997 __set_bit(BTN_MIDDLE, input_dev->keybit);
2998 __set_bit(BTN_RIGHT, input_dev->keybit);
2999 __set_bit(BTN_LEFT, input_dev->keybit);
3000 __set_bit(BTN_BACK, input_dev->keybit);
3001 __set_bit(BTN_FORWARD, input_dev->keybit);
3002
3003 __set_bit(EV_REL, input_dev->evbit);
3004 __set_bit(REL_X, input_dev->relbit);
3005 __set_bit(REL_Y, input_dev->relbit);
3006 __set_bit(REL_WHEEL, input_dev->relbit);
3007 __set_bit(REL_HWHEEL, input_dev->relbit);
3008 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3009 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3010}
3011
3012static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3013 struct hid_field *field, struct hid_usage *usage,
3014 unsigned long **bit, int *max)
3015{
3016 return -1;
3017}
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033struct k400_private_data {
3034 u8 feature_index;
3035};
3036
3037static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
3038{
3039 struct k400_private_data *k400 = hidpp->private_data;
3040 struct hidpp_touchpad_fw_items items = {};
3041 int ret;
3042 u8 feature_type;
3043
3044 if (!k400->feature_index) {
3045 ret = hidpp_root_get_feature(hidpp,
3046 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
3047 &k400->feature_index, &feature_type);
3048 if (ret)
3049
3050 return ret;
3051 }
3052
3053 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
3054 if (ret)
3055 return ret;
3056
3057 return 0;
3058}
3059
3060static int k400_allocate(struct hid_device *hdev)
3061{
3062 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3063 struct k400_private_data *k400;
3064
3065 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
3066 GFP_KERNEL);
3067 if (!k400)
3068 return -ENOMEM;
3069
3070 hidpp->private_data = k400;
3071
3072 return 0;
3073};
3074
3075static int k400_connect(struct hid_device *hdev, bool connected)
3076{
3077 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3078
3079 if (!disable_tap_to_click)
3080 return 0;
3081
3082 return k400_disable_tap_to_click(hidpp);
3083}
3084
3085
3086
3087
3088
3089#define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
3090
3091static int g920_ff_set_autocenter(struct hidpp_device *hidpp,
3092 struct hidpp_ff_private_data *data)
3093{
3094 struct hidpp_report response;
3095 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = {
3096 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART,
3097 };
3098 int ret;
3099
3100
3101
3102 dbg_hid("Setting autocenter to 0.\n");
3103 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3104 HIDPP_FF_DOWNLOAD_EFFECT,
3105 params, ARRAY_SIZE(params),
3106 &response);
3107 if (ret)
3108 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n");
3109 else
3110 data->slot_autocenter = response.fap.params[0];
3111
3112 return ret;
3113}
3114
3115static int g920_get_config(struct hidpp_device *hidpp,
3116 struct hidpp_ff_private_data *data)
3117{
3118 struct hidpp_report response;
3119 u8 feature_type;
3120 int ret;
3121
3122 memset(data, 0, sizeof(*data));
3123
3124
3125 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
3126 &data->feature_index, &feature_type);
3127 if (ret)
3128 return ret;
3129
3130
3131 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3132 HIDPP_FF_GET_INFO,
3133 NULL, 0,
3134 &response);
3135 if (ret) {
3136 if (ret < 0)
3137 return ret;
3138 hid_err(hidpp->hid_dev,
3139 "%s: received protocol error 0x%02x\n", __func__, ret);
3140 return -EPROTO;
3141 }
3142
3143 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
3144
3145
3146 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3147 HIDPP_FF_RESET_ALL,
3148 NULL, 0,
3149 &response);
3150 if (ret)
3151 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n");
3152
3153 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3154 HIDPP_FF_GET_APERTURE,
3155 NULL, 0,
3156 &response);
3157 if (ret) {
3158 hid_warn(hidpp->hid_dev,
3159 "Failed to read range from device!\n");
3160 }
3161 data->range = ret ?
3162 900 : get_unaligned_be16(&response.fap.params[0]);
3163
3164
3165 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index,
3166 HIDPP_FF_GET_GLOBAL_GAINS,
3167 NULL, 0,
3168 &response);
3169 if (ret)
3170 hid_warn(hidpp->hid_dev,
3171 "Failed to read gain values from device!\n");
3172 data->gain = ret ?
3173 0xffff : get_unaligned_be16(&response.fap.params[0]);
3174
3175
3176
3177 return g920_ff_set_autocenter(hidpp, data);
3178}
3179
3180
3181
3182
3183#define DINOVO_MINI_PRODUCT_ID 0xb30c
3184
3185static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3186 struct hid_field *field, struct hid_usage *usage,
3187 unsigned long **bit, int *max)
3188{
3189 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
3190 return 0;
3191
3192 switch (usage->hid & HID_USAGE) {
3193 case 0x00d: lg_map_key_clear(KEY_MEDIA); break;
3194 default:
3195 return 0;
3196 }
3197 return 1;
3198}
3199
3200
3201
3202
3203static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
3204{
3205 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3206 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
3207 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
3208}
3209
3210static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
3211 u8 *data, int size)
3212{
3213 s8 value, hvalue;
3214
3215 if (!hidpp->input)
3216 return -EINVAL;
3217
3218 if (size < 7)
3219 return 0;
3220
3221 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
3222 return 0;
3223
3224 value = data[3];
3225 hvalue = data[4];
3226
3227 input_report_rel(hidpp->input, REL_WHEEL, value);
3228 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
3229 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
3230 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
3231 input_sync(hidpp->input);
3232
3233 return 1;
3234}
3235
3236static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
3237 struct input_dev *input_dev)
3238{
3239 __set_bit(EV_REL, input_dev->evbit);
3240 __set_bit(REL_WHEEL, input_dev->relbit);
3241 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
3242 __set_bit(REL_HWHEEL, input_dev->relbit);
3243 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
3244}
3245
3246
3247
3248
3249static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
3250{
3251 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3252 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
3253 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
3254}
3255
3256static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
3257 u8 *data, int size)
3258{
3259 int i;
3260
3261 if (!hidpp->input)
3262 return -EINVAL;
3263
3264 if (size < 7)
3265 return 0;
3266
3267 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3268 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
3269 return 0;
3270
3271
3272
3273
3274
3275
3276
3277 for (i = 0; i < 8; i++)
3278 input_report_key(hidpp->input, BTN_MOUSE + i,
3279 (data[3] & (1 << i)));
3280
3281
3282 for (i = 0; i < 8; i++)
3283 input_report_key(hidpp->input, BTN_MISC + i,
3284 (data[4] & (1 << i)));
3285
3286 input_sync(hidpp->input);
3287 return 1;
3288}
3289
3290static void hidpp10_extra_mouse_buttons_populate_input(
3291 struct hidpp_device *hidpp, struct input_dev *input_dev)
3292{
3293
3294 __set_bit(BTN_0, input_dev->keybit);
3295 __set_bit(BTN_1, input_dev->keybit);
3296 __set_bit(BTN_2, input_dev->keybit);
3297 __set_bit(BTN_3, input_dev->keybit);
3298 __set_bit(BTN_4, input_dev->keybit);
3299 __set_bit(BTN_5, input_dev->keybit);
3300 __set_bit(BTN_6, input_dev->keybit);
3301 __set_bit(BTN_7, input_dev->keybit);
3302}
3303
3304
3305
3306
3307
3308
3309static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
3310 u8 *_rdesc, unsigned int *rsize)
3311{
3312
3313 static const char consumer_rdesc_start[] = {
3314 0x05, 0x0C,
3315 0x09, 0x01,
3316 0xA1, 0x01,
3317 0x85, 0x03,
3318 0x75, 0x10,
3319 0x95, 0x02,
3320 0x15, 0x01,
3321 0x26, 0x00
3322 };
3323 char *consumer_rdesc, *rdesc = (char *)_rdesc;
3324 unsigned int size;
3325
3326 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
3327 size = *rsize - (consumer_rdesc - rdesc);
3328 if (consumer_rdesc && size >= 25) {
3329 consumer_rdesc[15] = 0x7f;
3330 consumer_rdesc[16] = 0x10;
3331 consumer_rdesc[20] = 0x7f;
3332 consumer_rdesc[21] = 0x10;
3333 }
3334 return _rdesc;
3335}
3336
3337static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
3338{
3339 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
3340 HIDPP_ENABLE_CONSUMER_REPORT,
3341 HIDPP_ENABLE_CONSUMER_REPORT);
3342}
3343
3344static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
3345 u8 *data, int size)
3346{
3347 u8 consumer_report[5];
3348
3349 if (size < 7)
3350 return 0;
3351
3352 if (data[0] != REPORT_ID_HIDPP_SHORT ||
3353 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
3354 return 0;
3355
3356
3357
3358
3359
3360 consumer_report[0] = 0x03;
3361 memcpy(&consumer_report[1], &data[3], 4);
3362
3363 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
3364 consumer_report, 5, 1);
3365
3366 return 1;
3367}
3368
3369
3370
3371
3372
3373static int hi_res_scroll_enable(struct hidpp_device *hidpp)
3374{
3375 int ret;
3376 u8 multiplier = 1;
3377
3378 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
3379 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
3380 if (ret == 0)
3381 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
3382 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
3383 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
3384 &multiplier);
3385 } else {
3386 ret = hidpp10_enable_scrolling_acceleration(hidpp);
3387 multiplier = 8;
3388 }
3389 if (ret)
3390 return ret;
3391
3392 if (multiplier == 0)
3393 multiplier = 1;
3394
3395 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
3396 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier);
3397 return 0;
3398}
3399
3400
3401
3402
3403
3404static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
3405 unsigned int *rsize)
3406{
3407 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3408
3409 if (!hidpp)
3410 return rdesc;
3411
3412
3413 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
3414 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
3415 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
3416
3417 return rdesc;
3418}
3419
3420static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
3421 struct hid_field *field, struct hid_usage *usage,
3422 unsigned long **bit, int *max)
3423{
3424 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3425
3426 if (!hidpp)
3427 return 0;
3428
3429 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3430 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
3431 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
3432 field->application != HID_GD_MOUSE)
3433 return m560_input_mapping(hdev, hi, field, usage, bit, max);
3434
3435 if (hdev->product == DINOVO_MINI_PRODUCT_ID)
3436 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max);
3437
3438 return 0;
3439}
3440
3441static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
3442 struct hid_field *field, struct hid_usage *usage,
3443 unsigned long **bit, int *max)
3444{
3445 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3446
3447 if (!hidpp)
3448 return 0;
3449
3450
3451 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3452 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3453 usage->code == ABS_Y || usage->code == ABS_Z ||
3454 usage->code == ABS_RZ)) {
3455 field->application = HID_GD_MULTIAXIS;
3456 }
3457 }
3458
3459 return 0;
3460}
3461
3462
3463static void hidpp_populate_input(struct hidpp_device *hidpp,
3464 struct input_dev *input)
3465{
3466 hidpp->input = input;
3467
3468 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3469 wtp_populate_input(hidpp, input);
3470 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3471 m560_populate_input(hidpp, input);
3472
3473 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3474 hidpp10_wheel_populate_input(hidpp, input);
3475
3476 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3477 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3478}
3479
3480static int hidpp_input_configured(struct hid_device *hdev,
3481 struct hid_input *hidinput)
3482{
3483 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3484 struct input_dev *input = hidinput->input;
3485
3486 if (!hidpp)
3487 return 0;
3488
3489 hidpp_populate_input(hidpp, input);
3490
3491 return 0;
3492}
3493
3494static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3495 int size)
3496{
3497 struct hidpp_report *question = hidpp->send_receive_buf;
3498 struct hidpp_report *answer = hidpp->send_receive_buf;
3499 struct hidpp_report *report = (struct hidpp_report *)data;
3500 int ret;
3501
3502
3503
3504
3505
3506 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3507
3508
3509
3510
3511 if (hidpp_match_answer(question, report) ||
3512 hidpp_match_error(question, report)) {
3513 *answer = *report;
3514 hidpp->answer_available = true;
3515 wake_up(&hidpp->wait);
3516
3517
3518
3519
3520
3521
3522 return 1;
3523 }
3524 }
3525
3526 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) {
3527 atomic_set(&hidpp->connected,
3528 !(report->rap.params[0] & (1 << 6)));
3529 if (schedule_work(&hidpp->work) == 0)
3530 dbg_hid("%s: connect event already queued\n", __func__);
3531 return 1;
3532 }
3533
3534 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3535 data[0] == REPORT_ID_HIDPP_SHORT &&
3536 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT &&
3537 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) {
3538 dev_err_ratelimited(&hidpp->hid_dev->dev,
3539 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n");
3540 dev_err_ratelimited(&hidpp->hid_dev->dev,
3541 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n");
3542 }
3543
3544 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3545 ret = hidpp20_battery_event_1000(hidpp, data, size);
3546 if (ret != 0)
3547 return ret;
3548 ret = hidpp20_battery_event_1004(hidpp, data, size);
3549 if (ret != 0)
3550 return ret;
3551 ret = hidpp_solar_battery_event(hidpp, data, size);
3552 if (ret != 0)
3553 return ret;
3554 ret = hidpp20_battery_voltage_event(hidpp, data, size);
3555 if (ret != 0)
3556 return ret;
3557 }
3558
3559 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3560 ret = hidpp10_battery_event(hidpp, data, size);
3561 if (ret != 0)
3562 return ret;
3563 }
3564
3565 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3566 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3567 if (ret != 0)
3568 return ret;
3569 }
3570
3571 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3572 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3573 if (ret != 0)
3574 return ret;
3575 }
3576
3577 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3578 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3579 if (ret != 0)
3580 return ret;
3581 }
3582
3583 return 0;
3584}
3585
3586static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3587 u8 *data, int size)
3588{
3589 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3590 int ret = 0;
3591
3592 if (!hidpp)
3593 return 0;
3594
3595
3596 switch (data[0]) {
3597 case REPORT_ID_HIDPP_VERY_LONG:
3598 if (size != hidpp->very_long_report_length) {
3599 hid_err(hdev, "received hid++ report of bad size (%d)",
3600 size);
3601 return 1;
3602 }
3603 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3604 break;
3605 case REPORT_ID_HIDPP_LONG:
3606 if (size != HIDPP_REPORT_LONG_LENGTH) {
3607 hid_err(hdev, "received hid++ report of bad size (%d)",
3608 size);
3609 return 1;
3610 }
3611 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3612 break;
3613 case REPORT_ID_HIDPP_SHORT:
3614 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3615 hid_err(hdev, "received hid++ report of bad size (%d)",
3616 size);
3617 return 1;
3618 }
3619 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3620 break;
3621 }
3622
3623
3624
3625 if (ret != 0)
3626 return ret;
3627
3628 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3629 return wtp_raw_event(hdev, data, size);
3630 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3631 return m560_raw_event(hdev, data, size);
3632
3633 return 0;
3634}
3635
3636static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3637 struct hid_usage *usage, __s32 value)
3638{
3639
3640
3641
3642 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3643 struct hidpp_scroll_counter *counter;
3644
3645 if (!hidpp)
3646 return 0;
3647
3648 counter = &hidpp->vertical_wheel_counter;
3649
3650
3651
3652
3653
3654 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
3655 || hidpp->input == NULL || counter->wheel_multiplier == 0)
3656 return 0;
3657
3658 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3659 return 1;
3660}
3661
3662static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3663{
3664 static atomic_t battery_no = ATOMIC_INIT(0);
3665 struct power_supply_config cfg = { .drv_data = hidpp };
3666 struct power_supply_desc *desc = &hidpp->battery.desc;
3667 enum power_supply_property *battery_props;
3668 struct hidpp_battery *battery;
3669 unsigned int num_battery_props;
3670 unsigned long n;
3671 int ret;
3672
3673 if (hidpp->battery.ps)
3674 return 0;
3675
3676 hidpp->battery.feature_index = 0xff;
3677 hidpp->battery.solar_feature_index = 0xff;
3678 hidpp->battery.voltage_feature_index = 0xff;
3679
3680 if (hidpp->protocol_major >= 2) {
3681 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3682 ret = hidpp_solar_request_battery_event(hidpp);
3683 else {
3684
3685
3686
3687 ret = hidpp20_query_battery_info_1000(hidpp);
3688 if (ret)
3689 ret = hidpp20_query_battery_info_1004(hidpp);
3690 if (ret)
3691 ret = hidpp20_query_battery_voltage_info(hidpp);
3692 }
3693
3694 if (ret)
3695 return ret;
3696 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3697 } else {
3698 ret = hidpp10_query_battery_status(hidpp);
3699 if (ret) {
3700 ret = hidpp10_query_battery_mileage(hidpp);
3701 if (ret)
3702 return -ENOENT;
3703 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3704 } else {
3705 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3706 }
3707 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3708 }
3709
3710 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3711 hidpp_battery_props,
3712 sizeof(hidpp_battery_props),
3713 GFP_KERNEL);
3714 if (!battery_props)
3715 return -ENOMEM;
3716
3717 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3;
3718
3719 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE ||
3720 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE)
3721 battery_props[num_battery_props++] =
3722 POWER_SUPPLY_PROP_CAPACITY;
3723
3724 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3725 battery_props[num_battery_props++] =
3726 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3727
3728 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3729 battery_props[num_battery_props++] =
3730 POWER_SUPPLY_PROP_VOLTAGE_NOW;
3731
3732 battery = &hidpp->battery;
3733
3734 n = atomic_inc_return(&battery_no) - 1;
3735 desc->properties = battery_props;
3736 desc->num_properties = num_battery_props;
3737 desc->get_property = hidpp_battery_get_property;
3738 sprintf(battery->name, "hidpp_battery_%ld", n);
3739 desc->name = battery->name;
3740 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3741 desc->use_for_apm = 0;
3742
3743 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3744 &battery->desc,
3745 &cfg);
3746 if (IS_ERR(battery->ps))
3747 return PTR_ERR(battery->ps);
3748
3749 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3750
3751 return ret;
3752}
3753
3754static void hidpp_overwrite_name(struct hid_device *hdev)
3755{
3756 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3757 char *name;
3758
3759 if (hidpp->protocol_major < 2)
3760 return;
3761
3762 name = hidpp_get_device_name(hidpp);
3763
3764 if (!name) {
3765 hid_err(hdev, "unable to retrieve the name of the device");
3766 } else {
3767 dbg_hid("HID++: Got name: %s\n", name);
3768 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3769 }
3770
3771 kfree(name);
3772}
3773
3774static int hidpp_input_open(struct input_dev *dev)
3775{
3776 struct hid_device *hid = input_get_drvdata(dev);
3777
3778 return hid_hw_open(hid);
3779}
3780
3781static void hidpp_input_close(struct input_dev *dev)
3782{
3783 struct hid_device *hid = input_get_drvdata(dev);
3784
3785 hid_hw_close(hid);
3786}
3787
3788static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3789{
3790 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3791 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3792
3793 if (!input_dev)
3794 return NULL;
3795
3796 input_set_drvdata(input_dev, hdev);
3797 input_dev->open = hidpp_input_open;
3798 input_dev->close = hidpp_input_close;
3799
3800 input_dev->name = hidpp->name;
3801 input_dev->phys = hdev->phys;
3802 input_dev->uniq = hdev->uniq;
3803 input_dev->id.bustype = hdev->bus;
3804 input_dev->id.vendor = hdev->vendor;
3805 input_dev->id.product = hdev->product;
3806 input_dev->id.version = hdev->version;
3807 input_dev->dev.parent = &hdev->dev;
3808
3809 return input_dev;
3810}
3811
3812static void hidpp_connect_event(struct hidpp_device *hidpp)
3813{
3814 struct hid_device *hdev = hidpp->hid_dev;
3815 int ret = 0;
3816 bool connected = atomic_read(&hidpp->connected);
3817 struct input_dev *input;
3818 char *name, *devm_name;
3819
3820 if (!connected) {
3821 if (hidpp->battery.ps) {
3822 hidpp->battery.online = false;
3823 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3824 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3825 power_supply_changed(hidpp->battery.ps);
3826 }
3827 return;
3828 }
3829
3830 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3831 ret = wtp_connect(hdev, connected);
3832 if (ret)
3833 return;
3834 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3835 ret = m560_send_config_command(hdev, connected);
3836 if (ret)
3837 return;
3838 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3839 ret = k400_connect(hdev, connected);
3840 if (ret)
3841 return;
3842 }
3843
3844 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3845 ret = hidpp10_wheel_connect(hidpp);
3846 if (ret)
3847 return;
3848 }
3849
3850 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3851 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3852 if (ret)
3853 return;
3854 }
3855
3856 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3857 ret = hidpp10_consumer_keys_connect(hidpp);
3858 if (ret)
3859 return;
3860 }
3861
3862
3863
3864 if (!hidpp->protocol_major) {
3865 ret = hidpp_root_get_protocol_version(hidpp);
3866 if (ret) {
3867 hid_err(hdev, "Can not get the protocol version.\n");
3868 return;
3869 }
3870 }
3871
3872 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3873 name = hidpp_get_device_name(hidpp);
3874 if (name) {
3875 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3876 "%s", name);
3877 kfree(name);
3878 if (!devm_name)
3879 return;
3880
3881 hidpp->name = devm_name;
3882 }
3883 }
3884
3885 hidpp_initialize_battery(hidpp);
3886
3887
3888 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3889 hidpp10_enable_battery_reporting(hidpp);
3890 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3891 hidpp10_query_battery_mileage(hidpp);
3892 else
3893 hidpp10_query_battery_status(hidpp);
3894 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3895 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE)
3896 hidpp20_query_battery_voltage_info(hidpp);
3897 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY)
3898 hidpp20_query_battery_info_1004(hidpp);
3899 else
3900 hidpp20_query_battery_info_1000(hidpp);
3901 }
3902 if (hidpp->battery.ps)
3903 power_supply_changed(hidpp->battery.ps);
3904
3905 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3906 hi_res_scroll_enable(hidpp);
3907
3908 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3909
3910 return;
3911
3912 input = hidpp_allocate_input(hdev);
3913 if (!input) {
3914 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3915 return;
3916 }
3917
3918 hidpp_populate_input(hidpp, input);
3919
3920 ret = input_register_device(input);
3921 if (ret)
3922 input_free_device(input);
3923
3924 hidpp->delayed_input = input;
3925}
3926
3927static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3928
3929static struct attribute *sysfs_attrs[] = {
3930 &dev_attr_builtin_power_supply.attr,
3931 NULL
3932};
3933
3934static const struct attribute_group ps_attribute_group = {
3935 .attrs = sysfs_attrs
3936};
3937
3938static int hidpp_get_report_length(struct hid_device *hdev, int id)
3939{
3940 struct hid_report_enum *re;
3941 struct hid_report *report;
3942
3943 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3944 report = re->report_id_hash[id];
3945 if (!report)
3946 return 0;
3947
3948 return report->field[0]->report_count + 1;
3949}
3950
3951static u8 hidpp_validate_device(struct hid_device *hdev)
3952{
3953 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3954 int id, report_length;
3955 u8 supported_reports = 0;
3956
3957 id = REPORT_ID_HIDPP_SHORT;
3958 report_length = hidpp_get_report_length(hdev, id);
3959 if (report_length) {
3960 if (report_length < HIDPP_REPORT_SHORT_LENGTH)
3961 goto bad_device;
3962
3963 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED;
3964 }
3965
3966 id = REPORT_ID_HIDPP_LONG;
3967 report_length = hidpp_get_report_length(hdev, id);
3968 if (report_length) {
3969 if (report_length < HIDPP_REPORT_LONG_LENGTH)
3970 goto bad_device;
3971
3972 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED;
3973 }
3974
3975 id = REPORT_ID_HIDPP_VERY_LONG;
3976 report_length = hidpp_get_report_length(hdev, id);
3977 if (report_length) {
3978 if (report_length < HIDPP_REPORT_LONG_LENGTH ||
3979 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3980 goto bad_device;
3981
3982 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED;
3983 hidpp->very_long_report_length = report_length;
3984 }
3985
3986 return supported_reports;
3987
3988bad_device:
3989 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3990 return false;
3991}
3992
3993static bool hidpp_application_equals(struct hid_device *hdev,
3994 unsigned int application)
3995{
3996 struct list_head *report_list;
3997 struct hid_report *report;
3998
3999 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
4000 report = list_first_entry_or_null(report_list, struct hid_report, list);
4001 return report && report->application == application;
4002}
4003
4004static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
4005{
4006 struct hidpp_device *hidpp;
4007 int ret;
4008 bool connected;
4009 unsigned int connect_mask = HID_CONNECT_DEFAULT;
4010 struct hidpp_ff_private_data data;
4011
4012
4013 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
4014 if (!hidpp)
4015 return -ENOMEM;
4016
4017 hidpp->hid_dev = hdev;
4018 hidpp->name = hdev->name;
4019 hidpp->quirks = id->driver_data;
4020 hid_set_drvdata(hdev, hidpp);
4021
4022 ret = hid_parse(hdev);
4023 if (ret) {
4024 hid_err(hdev, "%s:parse failed\n", __func__);
4025 return ret;
4026 }
4027
4028
4029
4030
4031 hidpp->supported_reports = hidpp_validate_device(hdev);
4032
4033 if (!hidpp->supported_reports) {
4034 hid_set_drvdata(hdev, NULL);
4035 devm_kfree(&hdev->dev, hidpp);
4036 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4037 }
4038
4039 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
4040 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
4041
4042 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4043 hidpp_application_equals(hdev, HID_GD_MOUSE))
4044 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
4045 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
4046
4047 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
4048 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
4049 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
4050
4051 if (disable_raw_mode) {
4052 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
4053 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
4054 }
4055
4056 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
4057 ret = wtp_allocate(hdev, id);
4058 if (ret)
4059 return ret;
4060 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
4061 ret = k400_allocate(hdev);
4062 if (ret)
4063 return ret;
4064 }
4065
4066 INIT_WORK(&hidpp->work, delayed_work_cb);
4067 mutex_init(&hidpp->send_mutex);
4068 init_waitqueue_head(&hidpp->wait);
4069
4070
4071 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
4072 if (ret)
4073 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
4074 hdev->name);
4075
4076
4077
4078
4079
4080 ret = hid_hw_start(hdev, 0);
4081 if (ret) {
4082 hid_err(hdev, "hw start failed\n");
4083 goto hid_hw_start_fail;
4084 }
4085
4086 ret = hid_hw_open(hdev);
4087 if (ret < 0) {
4088 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
4089 __func__, ret);
4090 goto hid_hw_open_fail;
4091 }
4092
4093
4094 hid_device_io_start(hdev);
4095
4096 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
4097 hidpp_unifying_init(hidpp);
4098
4099 connected = hidpp_root_get_protocol_version(hidpp) == 0;
4100 atomic_set(&hidpp->connected, connected);
4101 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
4102 if (!connected) {
4103 ret = -ENODEV;
4104 hid_err(hdev, "Device not connected");
4105 goto hid_hw_init_fail;
4106 }
4107
4108 hidpp_overwrite_name(hdev);
4109 }
4110
4111 if (connected && hidpp->protocol_major >= 2) {
4112 ret = hidpp_set_wireless_feature_index(hidpp);
4113 if (ret == -ENOENT)
4114 hidpp->wireless_feature_index = 0;
4115 else if (ret)
4116 goto hid_hw_init_fail;
4117 }
4118
4119 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
4120 ret = wtp_get_config(hidpp);
4121 if (ret)
4122 goto hid_hw_init_fail;
4123 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
4124 ret = g920_get_config(hidpp, &data);
4125 if (ret)
4126 goto hid_hw_init_fail;
4127 }
4128
4129 hidpp_connect_event(hidpp);
4130
4131
4132 hid_device_io_stop(hdev);
4133 hid_hw_close(hdev);
4134 hid_hw_stop(hdev);
4135
4136 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
4137 connect_mask &= ~HID_CONNECT_HIDINPUT;
4138
4139
4140 ret = hid_hw_start(hdev, connect_mask);
4141 if (ret) {
4142 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
4143 goto hid_hw_start_fail;
4144 }
4145
4146 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
4147 ret = hidpp_ff_init(hidpp, &data);
4148 if (ret)
4149 hid_warn(hidpp->hid_dev,
4150 "Unable to initialize force feedback support, errno %d\n",
4151 ret);
4152 }
4153
4154 return ret;
4155
4156hid_hw_init_fail:
4157 hid_hw_close(hdev);
4158hid_hw_open_fail:
4159 hid_hw_stop(hdev);
4160hid_hw_start_fail:
4161 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4162 cancel_work_sync(&hidpp->work);
4163 mutex_destroy(&hidpp->send_mutex);
4164 return ret;
4165}
4166
4167static void hidpp_remove(struct hid_device *hdev)
4168{
4169 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
4170
4171 if (!hidpp)
4172 return hid_hw_stop(hdev);
4173
4174 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
4175
4176 hid_hw_stop(hdev);
4177 cancel_work_sync(&hidpp->work);
4178 mutex_destroy(&hidpp->send_mutex);
4179}
4180
4181#define LDJ_DEVICE(product) \
4182 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
4183 USB_VENDOR_ID_LOGITECH, (product))
4184
4185#define L27MHZ_DEVICE(product) \
4186 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
4187 USB_VENDOR_ID_LOGITECH, (product))
4188
4189static const struct hid_device_id hidpp_devices[] = {
4190 {
4191 LDJ_DEVICE(0x4011),
4192 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
4193 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
4194 {
4195 LDJ_DEVICE(0x4101),
4196 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
4197 {
4198 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
4199 USB_DEVICE_ID_LOGITECH_T651),
4200 .driver_data = HIDPP_QUIRK_CLASS_WTP },
4201 {
4202 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4203 {
4204 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4205 {
4206 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4207 {
4208 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4209 {
4210 LDJ_DEVICE(0x402d),
4211 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
4212 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
4213 {
4214 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4215 {
4216 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4217 {
4218 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4219 {
4220 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4221 { LDJ_DEVICE(0x4072), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4222 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4223 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4224 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4225 {
4226 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4227 {
4228 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4229 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4230 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4231 {
4232 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4233 {
4234 LDJ_DEVICE(0x4082), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4235 {
4236 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
4237 {
4238 LDJ_DEVICE(0x4024),
4239 .driver_data = HIDPP_QUIRK_CLASS_K400 },
4240 {
4241 LDJ_DEVICE(0x4002),
4242 .driver_data = HIDPP_QUIRK_CLASS_K750 },
4243 {
4244 LDJ_DEVICE(0xb305),
4245 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4246 {
4247 LDJ_DEVICE(0xb309),
4248 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4249 {
4250 LDJ_DEVICE(0xb30b),
4251 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4252
4253 { LDJ_DEVICE(HID_ANY_ID) },
4254
4255 {
4256 L27MHZ_DEVICE(0x0049),
4257 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4258 {
4259 L27MHZ_DEVICE(0x0057),
4260 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4261 {
4262 L27MHZ_DEVICE(0x005c),
4263 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
4264 {
4265 L27MHZ_DEVICE(0x00fe),
4266 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
4267
4268 { L27MHZ_DEVICE(HID_ANY_ID) },
4269
4270 {
4271 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
4272 {
4273 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) },
4274 {
4275 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) },
4276 {
4277 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
4278 {
4279 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) },
4280 {
4281 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) },
4282 {
4283 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
4284 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
4285 {
4286 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) },
4287
4288 {
4289 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
4290 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4291 {
4292 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309),
4293 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4294 {
4295 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b),
4296 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
4297 {
4298 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) },
4299 {
4300 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012),
4301 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4302 {
4303 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) },
4304 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e),
4305 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4306 {
4307 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023),
4308 .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
4309 {}
4310};
4311
4312MODULE_DEVICE_TABLE(hid, hidpp_devices);
4313
4314static const struct hid_usage_id hidpp_usages[] = {
4315 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
4316 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
4317};
4318
4319static struct hid_driver hidpp_driver = {
4320 .name = "logitech-hidpp-device",
4321 .id_table = hidpp_devices,
4322 .report_fixup = hidpp_report_fixup,
4323 .probe = hidpp_probe,
4324 .remove = hidpp_remove,
4325 .raw_event = hidpp_raw_event,
4326 .usage_table = hidpp_usages,
4327 .event = hidpp_event,
4328 .input_configured = hidpp_input_configured,
4329 .input_mapping = hidpp_input_mapping,
4330 .input_mapped = hidpp_input_mapped,
4331};
4332
4333module_hid_driver(hidpp_driver);
4334