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