[RAMEN9610-21500]HID: Fix assumption that devices have inputs
[GitHub/MotorolaMobilityLLC/kernel-slsi.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 struct hidpp_report response;
729 int ret;
730
731 ret = hidpp_send_fap_command_sync(hidpp,
732 HIDPP_PAGE_ROOT_IDX,
733 CMD_ROOT_GET_PROTOCOL_VERSION,
734 NULL, 0, &response);
735
736 if (ret == HIDPP_ERROR_INVALID_SUBID) {
737 hidpp->protocol_major = 1;
738 hidpp->protocol_minor = 0;
739 return 0;
740 }
741
742 /* the device might not be connected */
743 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
744 return -EIO;
745
746 if (ret > 0) {
747 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
748 __func__, ret);
749 return -EPROTO;
750 }
751 if (ret)
752 return ret;
753
754 hidpp->protocol_major = response.fap.params[0];
755 hidpp->protocol_minor = response.fap.params[1];
756
757 return ret;
758 }
759
760 static bool hidpp_is_connected(struct hidpp_device *hidpp)
761 {
762 int ret;
763
764 ret = hidpp_root_get_protocol_version(hidpp);
765 if (!ret)
766 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
767 hidpp->protocol_major, hidpp->protocol_minor);
768 return ret == 0;
769 }
770
771 /* -------------------------------------------------------------------------- */
772 /* 0x0005: GetDeviceNameType */
773 /* -------------------------------------------------------------------------- */
774
775 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
776
777 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
778 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
779 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
780
781 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
782 u8 feature_index, u8 *nameLength)
783 {
784 struct hidpp_report response;
785 int ret;
786
787 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
788 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
789
790 if (ret > 0) {
791 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
792 __func__, ret);
793 return -EPROTO;
794 }
795 if (ret)
796 return ret;
797
798 *nameLength = response.fap.params[0];
799
800 return ret;
801 }
802
803 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
804 u8 feature_index, u8 char_index, char *device_name, int len_buf)
805 {
806 struct hidpp_report response;
807 int ret, i;
808 int count;
809
810 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
811 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
812 &response);
813
814 if (ret > 0) {
815 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
816 __func__, ret);
817 return -EPROTO;
818 }
819 if (ret)
820 return ret;
821
822 switch (response.report_id) {
823 case REPORT_ID_HIDPP_VERY_LONG:
824 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
825 break;
826 case REPORT_ID_HIDPP_LONG:
827 count = HIDPP_REPORT_LONG_LENGTH - 4;
828 break;
829 case REPORT_ID_HIDPP_SHORT:
830 count = HIDPP_REPORT_SHORT_LENGTH - 4;
831 break;
832 default:
833 return -EPROTO;
834 }
835
836 if (len_buf < count)
837 count = len_buf;
838
839 for (i = 0; i < count; i++)
840 device_name[i] = response.fap.params[i];
841
842 return count;
843 }
844
845 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
846 {
847 u8 feature_type;
848 u8 feature_index;
849 u8 __name_length;
850 char *name;
851 unsigned index = 0;
852 int ret;
853
854 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
855 &feature_index, &feature_type);
856 if (ret)
857 return NULL;
858
859 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
860 &__name_length);
861 if (ret)
862 return NULL;
863
864 name = kzalloc(__name_length + 1, GFP_KERNEL);
865 if (!name)
866 return NULL;
867
868 while (index < __name_length) {
869 ret = hidpp_devicenametype_get_device_name(hidpp,
870 feature_index, index, name + index,
871 __name_length - index);
872 if (ret <= 0) {
873 kfree(name);
874 return NULL;
875 }
876 index += ret;
877 }
878
879 /* include the terminating '\0' */
880 hidpp_prefix_name(&name, __name_length + 1);
881
882 return name;
883 }
884
885 /* -------------------------------------------------------------------------- */
886 /* 0x1000: Battery level status */
887 /* -------------------------------------------------------------------------- */
888
889 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
890
891 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
892 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
893
894 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
895
896 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
897 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
898 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
899
900 static int hidpp_map_battery_level(int capacity)
901 {
902 if (capacity < 11)
903 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
904 else if (capacity < 31)
905 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
906 else if (capacity < 81)
907 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
908 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
909 }
910
911 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
912 int *next_capacity,
913 int *level)
914 {
915 int status;
916
917 *capacity = data[0];
918 *next_capacity = data[1];
919 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
920
921 /* When discharging, we can rely on the device reported capacity.
922 * For all other states the device reports 0 (unknown).
923 */
924 switch (data[2]) {
925 case 0: /* discharging (in use) */
926 status = POWER_SUPPLY_STATUS_DISCHARGING;
927 *level = hidpp_map_battery_level(*capacity);
928 break;
929 case 1: /* recharging */
930 status = POWER_SUPPLY_STATUS_CHARGING;
931 break;
932 case 2: /* charge in final stage */
933 status = POWER_SUPPLY_STATUS_CHARGING;
934 break;
935 case 3: /* charge complete */
936 status = POWER_SUPPLY_STATUS_FULL;
937 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
938 *capacity = 100;
939 break;
940 case 4: /* recharging below optimal speed */
941 status = POWER_SUPPLY_STATUS_CHARGING;
942 break;
943 /* 5 = invalid battery type
944 6 = thermal error
945 7 = other charging error */
946 default:
947 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
948 break;
949 }
950
951 return status;
952 }
953
954 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
955 u8 feature_index,
956 int *status,
957 int *capacity,
958 int *next_capacity,
959 int *level)
960 {
961 struct hidpp_report response;
962 int ret;
963 u8 *params = (u8 *)response.fap.params;
964
965 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
966 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
967 NULL, 0, &response);
968 if (ret > 0) {
969 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
970 __func__, ret);
971 return -EPROTO;
972 }
973 if (ret)
974 return ret;
975
976 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
977 next_capacity,
978 level);
979
980 return 0;
981 }
982
983 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
984 u8 feature_index)
985 {
986 struct hidpp_report response;
987 int ret;
988 u8 *params = (u8 *)response.fap.params;
989 unsigned int level_count, flags;
990
991 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
992 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
993 NULL, 0, &response);
994 if (ret > 0) {
995 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
996 __func__, ret);
997 return -EPROTO;
998 }
999 if (ret)
1000 return ret;
1001
1002 level_count = params[0];
1003 flags = params[1];
1004
1005 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1006 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1007 else
1008 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1009
1010 return 0;
1011 }
1012
1013 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1014 {
1015 u8 feature_type;
1016 int ret;
1017 int status, capacity, next_capacity, level;
1018
1019 if (hidpp->battery.feature_index == 0xff) {
1020 ret = hidpp_root_get_feature(hidpp,
1021 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1022 &hidpp->battery.feature_index,
1023 &feature_type);
1024 if (ret)
1025 return ret;
1026 }
1027
1028 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1029 hidpp->battery.feature_index,
1030 &status, &capacity,
1031 &next_capacity, &level);
1032 if (ret)
1033 return ret;
1034
1035 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1036 hidpp->battery.feature_index);
1037 if (ret)
1038 return ret;
1039
1040 hidpp->battery.status = status;
1041 hidpp->battery.capacity = capacity;
1042 hidpp->battery.level = level;
1043 /* the capacity is only available when discharging or full */
1044 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1045 status == POWER_SUPPLY_STATUS_FULL;
1046
1047 return 0;
1048 }
1049
1050 static int hidpp20_battery_event(struct hidpp_device *hidpp,
1051 u8 *data, int size)
1052 {
1053 struct hidpp_report *report = (struct hidpp_report *)data;
1054 int status, capacity, next_capacity, level;
1055 bool changed;
1056
1057 if (report->fap.feature_index != hidpp->battery.feature_index ||
1058 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1059 return 0;
1060
1061 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1062 &capacity,
1063 &next_capacity,
1064 &level);
1065
1066 /* the capacity is only available when discharging or full */
1067 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1068 status == POWER_SUPPLY_STATUS_FULL;
1069
1070 changed = capacity != hidpp->battery.capacity ||
1071 level != hidpp->battery.level ||
1072 status != hidpp->battery.status;
1073
1074 if (changed) {
1075 hidpp->battery.level = level;
1076 hidpp->battery.capacity = capacity;
1077 hidpp->battery.status = status;
1078 if (hidpp->battery.ps)
1079 power_supply_changed(hidpp->battery.ps);
1080 }
1081
1082 return 0;
1083 }
1084
1085 static enum power_supply_property hidpp_battery_props[] = {
1086 POWER_SUPPLY_PROP_ONLINE,
1087 POWER_SUPPLY_PROP_STATUS,
1088 POWER_SUPPLY_PROP_SCOPE,
1089 POWER_SUPPLY_PROP_MODEL_NAME,
1090 POWER_SUPPLY_PROP_MANUFACTURER,
1091 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1092 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1093 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1094 };
1095
1096 static int hidpp_battery_get_property(struct power_supply *psy,
1097 enum power_supply_property psp,
1098 union power_supply_propval *val)
1099 {
1100 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1101 int ret = 0;
1102
1103 switch(psp) {
1104 case POWER_SUPPLY_PROP_STATUS:
1105 val->intval = hidpp->battery.status;
1106 break;
1107 case POWER_SUPPLY_PROP_CAPACITY:
1108 val->intval = hidpp->battery.capacity;
1109 break;
1110 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1111 val->intval = hidpp->battery.level;
1112 break;
1113 case POWER_SUPPLY_PROP_SCOPE:
1114 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1115 break;
1116 case POWER_SUPPLY_PROP_ONLINE:
1117 val->intval = hidpp->battery.online;
1118 break;
1119 case POWER_SUPPLY_PROP_MODEL_NAME:
1120 if (!strncmp(hidpp->name, "Logitech ", 9))
1121 val->strval = hidpp->name + 9;
1122 else
1123 val->strval = hidpp->name;
1124 break;
1125 case POWER_SUPPLY_PROP_MANUFACTURER:
1126 val->strval = "Logitech";
1127 break;
1128 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1129 val->strval = hidpp->hid_dev->uniq;
1130 break;
1131 default:
1132 ret = -EINVAL;
1133 break;
1134 }
1135
1136 return ret;
1137 }
1138
1139 /* -------------------------------------------------------------------------- */
1140 /* 0x4301: Solar Keyboard */
1141 /* -------------------------------------------------------------------------- */
1142
1143 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1144
1145 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1146
1147 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1148 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1149 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1150
1151 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1152 {
1153 struct hidpp_report response;
1154 u8 params[2] = { 1, 1 };
1155 u8 feature_type;
1156 int ret;
1157
1158 if (hidpp->battery.feature_index == 0xff) {
1159 ret = hidpp_root_get_feature(hidpp,
1160 HIDPP_PAGE_SOLAR_KEYBOARD,
1161 &hidpp->battery.solar_feature_index,
1162 &feature_type);
1163 if (ret)
1164 return ret;
1165 }
1166
1167 ret = hidpp_send_fap_command_sync(hidpp,
1168 hidpp->battery.solar_feature_index,
1169 CMD_SOLAR_SET_LIGHT_MEASURE,
1170 params, 2, &response);
1171 if (ret > 0) {
1172 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1173 __func__, ret);
1174 return -EPROTO;
1175 }
1176 if (ret)
1177 return ret;
1178
1179 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1180
1181 return 0;
1182 }
1183
1184 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1185 u8 *data, int size)
1186 {
1187 struct hidpp_report *report = (struct hidpp_report *)data;
1188 int capacity, lux, status;
1189 u8 function;
1190
1191 function = report->fap.funcindex_clientid;
1192
1193
1194 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1195 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1196 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1197 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1198 return 0;
1199
1200 capacity = report->fap.params[0];
1201
1202 switch (function) {
1203 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1204 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1205 if (lux > 200)
1206 status = POWER_SUPPLY_STATUS_CHARGING;
1207 else
1208 status = POWER_SUPPLY_STATUS_DISCHARGING;
1209 break;
1210 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1211 default:
1212 if (capacity < hidpp->battery.capacity)
1213 status = POWER_SUPPLY_STATUS_DISCHARGING;
1214 else
1215 status = POWER_SUPPLY_STATUS_CHARGING;
1216
1217 }
1218
1219 if (capacity == 100)
1220 status = POWER_SUPPLY_STATUS_FULL;
1221
1222 hidpp->battery.online = true;
1223 if (capacity != hidpp->battery.capacity ||
1224 status != hidpp->battery.status) {
1225 hidpp->battery.capacity = capacity;
1226 hidpp->battery.status = status;
1227 if (hidpp->battery.ps)
1228 power_supply_changed(hidpp->battery.ps);
1229 }
1230
1231 return 0;
1232 }
1233
1234 /* -------------------------------------------------------------------------- */
1235 /* 0x6010: Touchpad FW items */
1236 /* -------------------------------------------------------------------------- */
1237
1238 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1239
1240 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1241
1242 struct hidpp_touchpad_fw_items {
1243 uint8_t presence;
1244 uint8_t desired_state;
1245 uint8_t state;
1246 uint8_t persistent;
1247 };
1248
1249 /**
1250 * send a set state command to the device by reading the current items->state
1251 * field. items is then filled with the current state.
1252 */
1253 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1254 u8 feature_index,
1255 struct hidpp_touchpad_fw_items *items)
1256 {
1257 struct hidpp_report response;
1258 int ret;
1259 u8 *params = (u8 *)response.fap.params;
1260
1261 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1262 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1263
1264 if (ret > 0) {
1265 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1266 __func__, ret);
1267 return -EPROTO;
1268 }
1269 if (ret)
1270 return ret;
1271
1272 items->presence = params[0];
1273 items->desired_state = params[1];
1274 items->state = params[2];
1275 items->persistent = params[3];
1276
1277 return 0;
1278 }
1279
1280 /* -------------------------------------------------------------------------- */
1281 /* 0x6100: TouchPadRawXY */
1282 /* -------------------------------------------------------------------------- */
1283
1284 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1285
1286 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1287 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1288
1289 #define EVENT_TOUCHPAD_RAW_XY 0x00
1290
1291 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1292 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1293
1294 struct hidpp_touchpad_raw_info {
1295 u16 x_size;
1296 u16 y_size;
1297 u8 z_range;
1298 u8 area_range;
1299 u8 timestamp_unit;
1300 u8 maxcontacts;
1301 u8 origin;
1302 u16 res;
1303 };
1304
1305 struct hidpp_touchpad_raw_xy_finger {
1306 u8 contact_type;
1307 u8 contact_status;
1308 u16 x;
1309 u16 y;
1310 u8 z;
1311 u8 area;
1312 u8 finger_id;
1313 };
1314
1315 struct hidpp_touchpad_raw_xy {
1316 u16 timestamp;
1317 struct hidpp_touchpad_raw_xy_finger fingers[2];
1318 u8 spurious_flag;
1319 u8 end_of_frame;
1320 u8 finger_count;
1321 u8 button;
1322 };
1323
1324 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1325 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1326 {
1327 struct hidpp_report response;
1328 int ret;
1329 u8 *params = (u8 *)response.fap.params;
1330
1331 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1332 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1333
1334 if (ret > 0) {
1335 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1336 __func__, ret);
1337 return -EPROTO;
1338 }
1339 if (ret)
1340 return ret;
1341
1342 raw_info->x_size = get_unaligned_be16(&params[0]);
1343 raw_info->y_size = get_unaligned_be16(&params[2]);
1344 raw_info->z_range = params[4];
1345 raw_info->area_range = params[5];
1346 raw_info->maxcontacts = params[7];
1347 raw_info->origin = params[8];
1348 /* res is given in unit per inch */
1349 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1350
1351 return ret;
1352 }
1353
1354 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1355 u8 feature_index, bool send_raw_reports,
1356 bool sensor_enhanced_settings)
1357 {
1358 struct hidpp_report response;
1359
1360 /*
1361 * Params:
1362 * bit 0 - enable raw
1363 * bit 1 - 16bit Z, no area
1364 * bit 2 - enhanced sensitivity
1365 * bit 3 - width, height (4 bits each) instead of area
1366 * bit 4 - send raw + gestures (degrades smoothness)
1367 * remaining bits - reserved
1368 */
1369 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1370
1371 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1372 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1373 }
1374
1375 static void hidpp_touchpad_touch_event(u8 *data,
1376 struct hidpp_touchpad_raw_xy_finger *finger)
1377 {
1378 u8 x_m = data[0] << 2;
1379 u8 y_m = data[2] << 2;
1380
1381 finger->x = x_m << 6 | data[1];
1382 finger->y = y_m << 6 | data[3];
1383
1384 finger->contact_type = data[0] >> 6;
1385 finger->contact_status = data[2] >> 6;
1386
1387 finger->z = data[4];
1388 finger->area = data[5];
1389 finger->finger_id = data[6] >> 4;
1390 }
1391
1392 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1393 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1394 {
1395 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1396 raw_xy->end_of_frame = data[8] & 0x01;
1397 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1398 raw_xy->finger_count = data[15] & 0x0f;
1399 raw_xy->button = (data[8] >> 2) & 0x01;
1400
1401 if (raw_xy->finger_count) {
1402 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1403 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1404 }
1405 }
1406
1407 /* -------------------------------------------------------------------------- */
1408 /* 0x8123: Force feedback support */
1409 /* -------------------------------------------------------------------------- */
1410
1411 #define HIDPP_FF_GET_INFO 0x01
1412 #define HIDPP_FF_RESET_ALL 0x11
1413 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1414 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1415 #define HIDPP_FF_DESTROY_EFFECT 0x41
1416 #define HIDPP_FF_GET_APERTURE 0x51
1417 #define HIDPP_FF_SET_APERTURE 0x61
1418 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1419 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1420
1421 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1422 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1423 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1424 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1425
1426 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1427 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1428 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1429 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1430 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1431 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1432 #define HIDPP_FF_EFFECT_SPRING 0x06
1433 #define HIDPP_FF_EFFECT_DAMPER 0x07
1434 #define HIDPP_FF_EFFECT_FRICTION 0x08
1435 #define HIDPP_FF_EFFECT_INERTIA 0x09
1436 #define HIDPP_FF_EFFECT_RAMP 0x0A
1437
1438 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1439
1440 #define HIDPP_FF_EFFECTID_NONE -1
1441 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1442
1443 #define HIDPP_FF_MAX_PARAMS 20
1444 #define HIDPP_FF_RESERVED_SLOTS 1
1445
1446 struct hidpp_ff_private_data {
1447 struct hidpp_device *hidpp;
1448 u8 feature_index;
1449 u8 version;
1450 u16 gain;
1451 s16 range;
1452 u8 slot_autocenter;
1453 u8 num_effects;
1454 int *effect_ids;
1455 struct workqueue_struct *wq;
1456 atomic_t workqueue_size;
1457 };
1458
1459 struct hidpp_ff_work_data {
1460 struct work_struct work;
1461 struct hidpp_ff_private_data *data;
1462 int effect_id;
1463 u8 command;
1464 u8 params[HIDPP_FF_MAX_PARAMS];
1465 u8 size;
1466 };
1467
1468 static const signed short hiddpp_ff_effects[] = {
1469 FF_CONSTANT,
1470 FF_PERIODIC,
1471 FF_SINE,
1472 FF_SQUARE,
1473 FF_SAW_UP,
1474 FF_SAW_DOWN,
1475 FF_TRIANGLE,
1476 FF_SPRING,
1477 FF_DAMPER,
1478 FF_AUTOCENTER,
1479 FF_GAIN,
1480 -1
1481 };
1482
1483 static const signed short hiddpp_ff_effects_v2[] = {
1484 FF_RAMP,
1485 FF_FRICTION,
1486 FF_INERTIA,
1487 -1
1488 };
1489
1490 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1491 HIDPP_FF_EFFECT_SPRING,
1492 HIDPP_FF_EFFECT_FRICTION,
1493 HIDPP_FF_EFFECT_DAMPER,
1494 HIDPP_FF_EFFECT_INERTIA
1495 };
1496
1497 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1498 "spring",
1499 "friction",
1500 "damper",
1501 "inertia"
1502 };
1503
1504
1505 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1506 {
1507 int i;
1508
1509 for (i = 0; i < data->num_effects; i++)
1510 if (data->effect_ids[i] == effect_id)
1511 return i+1;
1512
1513 return 0;
1514 }
1515
1516 static void hidpp_ff_work_handler(struct work_struct *w)
1517 {
1518 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1519 struct hidpp_ff_private_data *data = wd->data;
1520 struct hidpp_report response;
1521 u8 slot;
1522 int ret;
1523
1524 /* add slot number if needed */
1525 switch (wd->effect_id) {
1526 case HIDPP_FF_EFFECTID_AUTOCENTER:
1527 wd->params[0] = data->slot_autocenter;
1528 break;
1529 case HIDPP_FF_EFFECTID_NONE:
1530 /* leave slot as zero */
1531 break;
1532 default:
1533 /* find current slot for effect */
1534 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1535 break;
1536 }
1537
1538 /* send command and wait for reply */
1539 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1540 wd->command, wd->params, wd->size, &response);
1541
1542 if (ret) {
1543 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1544 goto out;
1545 }
1546
1547 /* parse return data */
1548 switch (wd->command) {
1549 case HIDPP_FF_DOWNLOAD_EFFECT:
1550 slot = response.fap.params[0];
1551 if (slot > 0 && slot <= data->num_effects) {
1552 if (wd->effect_id >= 0)
1553 /* regular effect uploaded */
1554 data->effect_ids[slot-1] = wd->effect_id;
1555 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1556 /* autocenter spring uploaded */
1557 data->slot_autocenter = slot;
1558 }
1559 break;
1560 case HIDPP_FF_DESTROY_EFFECT:
1561 if (wd->effect_id >= 0)
1562 /* regular effect destroyed */
1563 data->effect_ids[wd->params[0]-1] = -1;
1564 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1565 /* autocenter spring destoyed */
1566 data->slot_autocenter = 0;
1567 break;
1568 case HIDPP_FF_SET_GLOBAL_GAINS:
1569 data->gain = (wd->params[0] << 8) + wd->params[1];
1570 break;
1571 case HIDPP_FF_SET_APERTURE:
1572 data->range = (wd->params[0] << 8) + wd->params[1];
1573 break;
1574 default:
1575 /* no action needed */
1576 break;
1577 }
1578
1579 out:
1580 atomic_dec(&data->workqueue_size);
1581 kfree(wd);
1582 }
1583
1584 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1585 {
1586 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1587 int s;
1588
1589 if (!wd)
1590 return -ENOMEM;
1591
1592 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1593
1594 wd->data = data;
1595 wd->effect_id = effect_id;
1596 wd->command = command;
1597 wd->size = size;
1598 memcpy(wd->params, params, size);
1599
1600 atomic_inc(&data->workqueue_size);
1601 queue_work(data->wq, &wd->work);
1602
1603 /* warn about excessive queue size */
1604 s = atomic_read(&data->workqueue_size);
1605 if (s >= 20 && s % 20 == 0)
1606 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1607
1608 return 0;
1609 }
1610
1611 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1612 {
1613 struct hidpp_ff_private_data *data = dev->ff->private;
1614 u8 params[20];
1615 u8 size;
1616 int force;
1617
1618 /* set common parameters */
1619 params[2] = effect->replay.length >> 8;
1620 params[3] = effect->replay.length & 255;
1621 params[4] = effect->replay.delay >> 8;
1622 params[5] = effect->replay.delay & 255;
1623
1624 switch (effect->type) {
1625 case FF_CONSTANT:
1626 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1627 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1628 params[6] = force >> 8;
1629 params[7] = force & 255;
1630 params[8] = effect->u.constant.envelope.attack_level >> 7;
1631 params[9] = effect->u.constant.envelope.attack_length >> 8;
1632 params[10] = effect->u.constant.envelope.attack_length & 255;
1633 params[11] = effect->u.constant.envelope.fade_level >> 7;
1634 params[12] = effect->u.constant.envelope.fade_length >> 8;
1635 params[13] = effect->u.constant.envelope.fade_length & 255;
1636 size = 14;
1637 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1638 effect->u.constant.level,
1639 effect->direction, force);
1640 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1641 effect->u.constant.envelope.attack_level,
1642 effect->u.constant.envelope.attack_length,
1643 effect->u.constant.envelope.fade_level,
1644 effect->u.constant.envelope.fade_length);
1645 break;
1646 case FF_PERIODIC:
1647 {
1648 switch (effect->u.periodic.waveform) {
1649 case FF_SINE:
1650 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1651 break;
1652 case FF_SQUARE:
1653 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1654 break;
1655 case FF_SAW_UP:
1656 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1657 break;
1658 case FF_SAW_DOWN:
1659 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1660 break;
1661 case FF_TRIANGLE:
1662 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1663 break;
1664 default:
1665 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1666 return -EINVAL;
1667 }
1668 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1669 params[6] = effect->u.periodic.magnitude >> 8;
1670 params[7] = effect->u.periodic.magnitude & 255;
1671 params[8] = effect->u.periodic.offset >> 8;
1672 params[9] = effect->u.periodic.offset & 255;
1673 params[10] = effect->u.periodic.period >> 8;
1674 params[11] = effect->u.periodic.period & 255;
1675 params[12] = effect->u.periodic.phase >> 8;
1676 params[13] = effect->u.periodic.phase & 255;
1677 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1678 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1679 params[16] = effect->u.periodic.envelope.attack_length & 255;
1680 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1681 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1682 params[19] = effect->u.periodic.envelope.fade_length & 255;
1683 size = 20;
1684 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1685 effect->u.periodic.magnitude, effect->direction,
1686 effect->u.periodic.offset,
1687 effect->u.periodic.period,
1688 effect->u.periodic.phase);
1689 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1690 effect->u.periodic.envelope.attack_level,
1691 effect->u.periodic.envelope.attack_length,
1692 effect->u.periodic.envelope.fade_level,
1693 effect->u.periodic.envelope.fade_length);
1694 break;
1695 }
1696 case FF_RAMP:
1697 params[1] = HIDPP_FF_EFFECT_RAMP;
1698 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1699 params[6] = force >> 8;
1700 params[7] = force & 255;
1701 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1702 params[8] = force >> 8;
1703 params[9] = force & 255;
1704 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1705 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1706 params[12] = effect->u.ramp.envelope.attack_length & 255;
1707 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1708 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1709 params[15] = effect->u.ramp.envelope.fade_length & 255;
1710 size = 16;
1711 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1712 effect->u.ramp.start_level,
1713 effect->u.ramp.end_level,
1714 effect->direction, force);
1715 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1716 effect->u.ramp.envelope.attack_level,
1717 effect->u.ramp.envelope.attack_length,
1718 effect->u.ramp.envelope.fade_level,
1719 effect->u.ramp.envelope.fade_length);
1720 break;
1721 case FF_FRICTION:
1722 case FF_INERTIA:
1723 case FF_SPRING:
1724 case FF_DAMPER:
1725 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1726 params[6] = effect->u.condition[0].left_saturation >> 9;
1727 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1728 params[8] = effect->u.condition[0].left_coeff >> 8;
1729 params[9] = effect->u.condition[0].left_coeff & 255;
1730 params[10] = effect->u.condition[0].deadband >> 9;
1731 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1732 params[12] = effect->u.condition[0].center >> 8;
1733 params[13] = effect->u.condition[0].center & 255;
1734 params[14] = effect->u.condition[0].right_coeff >> 8;
1735 params[15] = effect->u.condition[0].right_coeff & 255;
1736 params[16] = effect->u.condition[0].right_saturation >> 9;
1737 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1738 size = 18;
1739 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1740 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1741 effect->u.condition[0].left_coeff,
1742 effect->u.condition[0].left_saturation,
1743 effect->u.condition[0].right_coeff,
1744 effect->u.condition[0].right_saturation);
1745 dbg_hid(" deadband=%d, center=%d\n",
1746 effect->u.condition[0].deadband,
1747 effect->u.condition[0].center);
1748 break;
1749 default:
1750 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1751 return -EINVAL;
1752 }
1753
1754 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1755 }
1756
1757 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1758 {
1759 struct hidpp_ff_private_data *data = dev->ff->private;
1760 u8 params[2];
1761
1762 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1763
1764 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1765
1766 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1767 }
1768
1769 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1770 {
1771 struct hidpp_ff_private_data *data = dev->ff->private;
1772 u8 slot = 0;
1773
1774 dbg_hid("Erasing effect %d.\n", effect_id);
1775
1776 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1777 }
1778
1779 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1780 {
1781 struct hidpp_ff_private_data *data = dev->ff->private;
1782 u8 params[18];
1783
1784 dbg_hid("Setting autocenter to %d.\n", magnitude);
1785
1786 /* start a standard spring effect */
1787 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1788 /* zero delay and duration */
1789 params[2] = params[3] = params[4] = params[5] = 0;
1790 /* set coeff to 25% of saturation */
1791 params[8] = params[14] = magnitude >> 11;
1792 params[9] = params[15] = (magnitude >> 3) & 255;
1793 params[6] = params[16] = magnitude >> 9;
1794 params[7] = params[17] = (magnitude >> 1) & 255;
1795 /* zero deadband and center */
1796 params[10] = params[11] = params[12] = params[13] = 0;
1797
1798 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1799 }
1800
1801 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1802 {
1803 struct hidpp_ff_private_data *data = dev->ff->private;
1804 u8 params[4];
1805
1806 dbg_hid("Setting gain to %d.\n", gain);
1807
1808 params[0] = gain >> 8;
1809 params[1] = gain & 255;
1810 params[2] = 0; /* no boost */
1811 params[3] = 0;
1812
1813 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1814 }
1815
1816 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1817 {
1818 struct hid_device *hid = to_hid_device(dev);
1819 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1820 struct input_dev *idev = hidinput->input;
1821 struct hidpp_ff_private_data *data = idev->ff->private;
1822
1823 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1824 }
1825
1826 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1827 {
1828 struct hid_device *hid = to_hid_device(dev);
1829 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1830 struct input_dev *idev = hidinput->input;
1831 struct hidpp_ff_private_data *data = idev->ff->private;
1832 u8 params[2];
1833 int range = simple_strtoul(buf, NULL, 10);
1834
1835 range = clamp(range, 180, 900);
1836
1837 params[0] = range >> 8;
1838 params[1] = range & 0x00FF;
1839
1840 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1841
1842 return count;
1843 }
1844
1845 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1846
1847 static void hidpp_ff_destroy(struct ff_device *ff)
1848 {
1849 struct hidpp_ff_private_data *data = ff->private;
1850
1851 kfree(data->effect_ids);
1852 }
1853
1854 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
1855 {
1856 struct hid_device *hid = hidpp->hid_dev;
1857 struct hid_input *hidinput;
1858 struct input_dev *dev;
1859 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1860 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1861 struct ff_device *ff;
1862 struct hidpp_report response;
1863 struct hidpp_ff_private_data *data;
1864 int error, j, num_slots;
1865 u8 version;
1866
1867 if (list_empty(&hid->inputs)) {
1868 hid_err(hid, "no inputs found\n");
1869 return -ENODEV;
1870 }
1871 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1872 dev = hidinput->input;
1873
1874 if (!dev) {
1875 hid_err(hid, "Struct input_dev not set!\n");
1876 return -EINVAL;
1877 }
1878
1879 /* Get firmware release */
1880 version = bcdDevice & 255;
1881
1882 /* Set supported force feedback capabilities */
1883 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1884 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1885 if (version > 1)
1886 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1887 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1888
1889 /* Read number of slots available in device */
1890 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1891 HIDPP_FF_GET_INFO, NULL, 0, &response);
1892 if (error) {
1893 if (error < 0)
1894 return error;
1895 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1896 __func__, error);
1897 return -EPROTO;
1898 }
1899
1900 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1901
1902 error = input_ff_create(dev, num_slots);
1903
1904 if (error) {
1905 hid_err(dev, "Failed to create FF device!\n");
1906 return error;
1907 }
1908
1909 data = kzalloc(sizeof(*data), GFP_KERNEL);
1910 if (!data)
1911 return -ENOMEM;
1912 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1913 if (!data->effect_ids) {
1914 kfree(data);
1915 return -ENOMEM;
1916 }
1917 data->hidpp = hidpp;
1918 data->feature_index = feature_index;
1919 data->version = version;
1920 data->slot_autocenter = 0;
1921 data->num_effects = num_slots;
1922 for (j = 0; j < num_slots; j++)
1923 data->effect_ids[j] = -1;
1924
1925 ff = dev->ff;
1926 ff->private = data;
1927
1928 ff->upload = hidpp_ff_upload_effect;
1929 ff->erase = hidpp_ff_erase_effect;
1930 ff->playback = hidpp_ff_playback;
1931 ff->set_gain = hidpp_ff_set_gain;
1932 ff->set_autocenter = hidpp_ff_set_autocenter;
1933 ff->destroy = hidpp_ff_destroy;
1934
1935
1936 /* reset all forces */
1937 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1938 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1939
1940 /* Read current Range */
1941 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1942 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1943 if (error)
1944 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1945 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1946
1947 /* Create sysfs interface */
1948 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1949 if (error)
1950 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1951
1952 /* Read the current gain values */
1953 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1954 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1955 if (error)
1956 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1957 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1958 /* ignore boost value at response.fap.params[2] */
1959
1960 /* init the hardware command queue */
1961 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1962 atomic_set(&data->workqueue_size, 0);
1963
1964 /* initialize with zero autocenter to get wheel in usable state */
1965 hidpp_ff_set_autocenter(dev, 0);
1966
1967 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1968
1969 return 0;
1970 }
1971
1972 static int hidpp_ff_deinit(struct hid_device *hid)
1973 {
1974 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1975 struct input_dev *dev = hidinput->input;
1976 struct hidpp_ff_private_data *data;
1977
1978 if (!dev) {
1979 hid_err(hid, "Struct input_dev not found!\n");
1980 return -EINVAL;
1981 }
1982
1983 hid_info(hid, "Unloading HID++ force feedback.\n");
1984 data = dev->ff->private;
1985 if (!data) {
1986 hid_err(hid, "Private data not found!\n");
1987 return -EINVAL;
1988 }
1989
1990 destroy_workqueue(data->wq);
1991 device_remove_file(&hid->dev, &dev_attr_range);
1992
1993 return 0;
1994 }
1995
1996
1997 /* ************************************************************************** */
1998 /* */
1999 /* Device Support */
2000 /* */
2001 /* ************************************************************************** */
2002
2003 /* -------------------------------------------------------------------------- */
2004 /* Touchpad HID++ devices */
2005 /* -------------------------------------------------------------------------- */
2006
2007 #define WTP_MANUAL_RESOLUTION 39
2008
2009 struct wtp_data {
2010 struct input_dev *input;
2011 u16 x_size, y_size;
2012 u8 finger_count;
2013 u8 mt_feature_index;
2014 u8 button_feature_index;
2015 u8 maxcontacts;
2016 bool flip_y;
2017 unsigned int resolution;
2018 };
2019
2020 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2021 struct hid_field *field, struct hid_usage *usage,
2022 unsigned long **bit, int *max)
2023 {
2024 return -1;
2025 }
2026
2027 static void wtp_populate_input(struct hidpp_device *hidpp,
2028 struct input_dev *input_dev, bool origin_is_hid_core)
2029 {
2030 struct wtp_data *wd = hidpp->private_data;
2031
2032 __set_bit(EV_ABS, input_dev->evbit);
2033 __set_bit(EV_KEY, input_dev->evbit);
2034 __clear_bit(EV_REL, input_dev->evbit);
2035 __clear_bit(EV_LED, input_dev->evbit);
2036
2037 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2038 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2039 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2040 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2041
2042 /* Max pressure is not given by the devices, pick one */
2043 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2044
2045 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2046
2047 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2048 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2049 else
2050 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2051
2052 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2053 INPUT_MT_DROP_UNUSED);
2054
2055 wd->input = input_dev;
2056 }
2057
2058 static void wtp_touch_event(struct wtp_data *wd,
2059 struct hidpp_touchpad_raw_xy_finger *touch_report)
2060 {
2061 int slot;
2062
2063 if (!touch_report->finger_id || touch_report->contact_type)
2064 /* no actual data */
2065 return;
2066
2067 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2068
2069 input_mt_slot(wd->input, slot);
2070 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2071 touch_report->contact_status);
2072 if (touch_report->contact_status) {
2073 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2074 touch_report->x);
2075 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2076 wd->flip_y ? wd->y_size - touch_report->y :
2077 touch_report->y);
2078 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2079 touch_report->area);
2080 }
2081 }
2082
2083 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2084 struct hidpp_touchpad_raw_xy *raw)
2085 {
2086 struct wtp_data *wd = hidpp->private_data;
2087 int i;
2088
2089 for (i = 0; i < 2; i++)
2090 wtp_touch_event(wd, &(raw->fingers[i]));
2091
2092 if (raw->end_of_frame &&
2093 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2094 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2095
2096 if (raw->end_of_frame || raw->finger_count <= 2) {
2097 input_mt_sync_frame(wd->input);
2098 input_sync(wd->input);
2099 }
2100 }
2101
2102 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2103 {
2104 struct wtp_data *wd = hidpp->private_data;
2105 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2106 (data[7] >> 4) * (data[7] >> 4)) / 2;
2107 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2108 (data[13] >> 4) * (data[13] >> 4)) / 2;
2109 struct hidpp_touchpad_raw_xy raw = {
2110 .timestamp = data[1],
2111 .fingers = {
2112 {
2113 .contact_type = 0,
2114 .contact_status = !!data[7],
2115 .x = get_unaligned_le16(&data[3]),
2116 .y = get_unaligned_le16(&data[5]),
2117 .z = c1_area,
2118 .area = c1_area,
2119 .finger_id = data[2],
2120 }, {
2121 .contact_type = 0,
2122 .contact_status = !!data[13],
2123 .x = get_unaligned_le16(&data[9]),
2124 .y = get_unaligned_le16(&data[11]),
2125 .z = c2_area,
2126 .area = c2_area,
2127 .finger_id = data[8],
2128 }
2129 },
2130 .finger_count = wd->maxcontacts,
2131 .spurious_flag = 0,
2132 .end_of_frame = (data[0] >> 7) == 0,
2133 .button = data[0] & 0x01,
2134 };
2135
2136 wtp_send_raw_xy_event(hidpp, &raw);
2137
2138 return 1;
2139 }
2140
2141 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2142 {
2143 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2144 struct wtp_data *wd = hidpp->private_data;
2145 struct hidpp_report *report = (struct hidpp_report *)data;
2146 struct hidpp_touchpad_raw_xy raw;
2147
2148 if (!wd || !wd->input)
2149 return 1;
2150
2151 switch (data[0]) {
2152 case 0x02:
2153 if (size < 2) {
2154 hid_err(hdev, "Received HID report of bad size (%d)",
2155 size);
2156 return 1;
2157 }
2158 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2159 input_event(wd->input, EV_KEY, BTN_LEFT,
2160 !!(data[1] & 0x01));
2161 input_event(wd->input, EV_KEY, BTN_RIGHT,
2162 !!(data[1] & 0x02));
2163 input_sync(wd->input);
2164 return 0;
2165 } else {
2166 if (size < 21)
2167 return 1;
2168 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2169 }
2170 case REPORT_ID_HIDPP_LONG:
2171 /* size is already checked in hidpp_raw_event. */
2172 if ((report->fap.feature_index != wd->mt_feature_index) ||
2173 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2174 return 1;
2175 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2176
2177 wtp_send_raw_xy_event(hidpp, &raw);
2178 return 0;
2179 }
2180
2181 return 0;
2182 }
2183
2184 static int wtp_get_config(struct hidpp_device *hidpp)
2185 {
2186 struct wtp_data *wd = hidpp->private_data;
2187 struct hidpp_touchpad_raw_info raw_info = {0};
2188 u8 feature_type;
2189 int ret;
2190
2191 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2192 &wd->mt_feature_index, &feature_type);
2193 if (ret)
2194 /* means that the device is not powered up */
2195 return ret;
2196
2197 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2198 &raw_info);
2199 if (ret)
2200 return ret;
2201
2202 wd->x_size = raw_info.x_size;
2203 wd->y_size = raw_info.y_size;
2204 wd->maxcontacts = raw_info.maxcontacts;
2205 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2206 wd->resolution = raw_info.res;
2207 if (!wd->resolution)
2208 wd->resolution = WTP_MANUAL_RESOLUTION;
2209
2210 return 0;
2211 }
2212
2213 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2214 {
2215 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2216 struct wtp_data *wd;
2217
2218 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2219 GFP_KERNEL);
2220 if (!wd)
2221 return -ENOMEM;
2222
2223 hidpp->private_data = wd;
2224
2225 return 0;
2226 };
2227
2228 static int wtp_connect(struct hid_device *hdev, bool connected)
2229 {
2230 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2231 struct wtp_data *wd = hidpp->private_data;
2232 int ret;
2233
2234 if (!wd->x_size) {
2235 ret = wtp_get_config(hidpp);
2236 if (ret) {
2237 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2238 return ret;
2239 }
2240 }
2241
2242 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2243 true, true);
2244 }
2245
2246 /* ------------------------------------------------------------------------- */
2247 /* Logitech M560 devices */
2248 /* ------------------------------------------------------------------------- */
2249
2250 /*
2251 * Logitech M560 protocol overview
2252 *
2253 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2254 * the sides buttons are pressed, it sends some keyboard keys events
2255 * instead of buttons ones.
2256 * To complicate things further, the middle button keys sequence
2257 * is different from the odd press and the even press.
2258 *
2259 * forward button -> Super_R
2260 * backward button -> Super_L+'d' (press only)
2261 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2262 * 2nd time: left-click (press only)
2263 * NB: press-only means that when the button is pressed, the
2264 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2265 * together sequentially; instead when the button is released, no event is
2266 * generated !
2267 *
2268 * With the command
2269 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2270 * the mouse reacts differently:
2271 * - it never sends a keyboard key event
2272 * - for the three mouse button it sends:
2273 * middle button press 11<xx>0a 3500af00...
2274 * side 1 button (forward) press 11<xx>0a 3500b000...
2275 * side 2 button (backward) press 11<xx>0a 3500ae00...
2276 * middle/side1/side2 button release 11<xx>0a 35000000...
2277 */
2278
2279 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2280
2281 struct m560_private_data {
2282 struct input_dev *input;
2283 };
2284
2285 /* how buttons are mapped in the report */
2286 #define M560_MOUSE_BTN_LEFT 0x01
2287 #define M560_MOUSE_BTN_RIGHT 0x02
2288 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2289 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2290
2291 #define M560_SUB_ID 0x0a
2292 #define M560_BUTTON_MODE_REGISTER 0x35
2293
2294 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2295 {
2296 struct hidpp_report response;
2297 struct hidpp_device *hidpp_dev;
2298
2299 hidpp_dev = hid_get_drvdata(hdev);
2300
2301 return hidpp_send_rap_command_sync(
2302 hidpp_dev,
2303 REPORT_ID_HIDPP_SHORT,
2304 M560_SUB_ID,
2305 M560_BUTTON_MODE_REGISTER,
2306 (u8 *)m560_config_parameter,
2307 sizeof(m560_config_parameter),
2308 &response
2309 );
2310 }
2311
2312 static int m560_allocate(struct hid_device *hdev)
2313 {
2314 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2315 struct m560_private_data *d;
2316
2317 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2318 GFP_KERNEL);
2319 if (!d)
2320 return -ENOMEM;
2321
2322 hidpp->private_data = d;
2323
2324 return 0;
2325 };
2326
2327 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2328 {
2329 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2330 struct m560_private_data *mydata = hidpp->private_data;
2331
2332 /* sanity check */
2333 if (!mydata || !mydata->input) {
2334 hid_err(hdev, "error in parameter\n");
2335 return -EINVAL;
2336 }
2337
2338 if (size < 7) {
2339 hid_err(hdev, "error in report\n");
2340 return 0;
2341 }
2342
2343 if (data[0] == REPORT_ID_HIDPP_LONG &&
2344 data[2] == M560_SUB_ID && data[6] == 0x00) {
2345 /*
2346 * m560 mouse report for middle, forward and backward button
2347 *
2348 * data[0] = 0x11
2349 * data[1] = device-id
2350 * data[2] = 0x0a
2351 * data[5] = 0xaf -> middle
2352 * 0xb0 -> forward
2353 * 0xae -> backward
2354 * 0x00 -> release all
2355 * data[6] = 0x00
2356 */
2357
2358 switch (data[5]) {
2359 case 0xaf:
2360 input_report_key(mydata->input, BTN_MIDDLE, 1);
2361 break;
2362 case 0xb0:
2363 input_report_key(mydata->input, BTN_FORWARD, 1);
2364 break;
2365 case 0xae:
2366 input_report_key(mydata->input, BTN_BACK, 1);
2367 break;
2368 case 0x00:
2369 input_report_key(mydata->input, BTN_BACK, 0);
2370 input_report_key(mydata->input, BTN_FORWARD, 0);
2371 input_report_key(mydata->input, BTN_MIDDLE, 0);
2372 break;
2373 default:
2374 hid_err(hdev, "error in report\n");
2375 return 0;
2376 }
2377 input_sync(mydata->input);
2378
2379 } else if (data[0] == 0x02) {
2380 /*
2381 * Logitech M560 mouse report
2382 *
2383 * data[0] = type (0x02)
2384 * data[1..2] = buttons
2385 * data[3..5] = xy
2386 * data[6] = wheel
2387 */
2388
2389 int v;
2390
2391 input_report_key(mydata->input, BTN_LEFT,
2392 !!(data[1] & M560_MOUSE_BTN_LEFT));
2393 input_report_key(mydata->input, BTN_RIGHT,
2394 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2395
2396 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2397 input_report_rel(mydata->input, REL_HWHEEL, -1);
2398 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2399 input_report_rel(mydata->input, REL_HWHEEL, 1);
2400
2401 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2402 input_report_rel(mydata->input, REL_X, v);
2403
2404 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2405 input_report_rel(mydata->input, REL_Y, v);
2406
2407 v = hid_snto32(data[6], 8);
2408 input_report_rel(mydata->input, REL_WHEEL, v);
2409
2410 input_sync(mydata->input);
2411 }
2412
2413 return 1;
2414 }
2415
2416 static void m560_populate_input(struct hidpp_device *hidpp,
2417 struct input_dev *input_dev, bool origin_is_hid_core)
2418 {
2419 struct m560_private_data *mydata = hidpp->private_data;
2420
2421 mydata->input = input_dev;
2422
2423 __set_bit(EV_KEY, mydata->input->evbit);
2424 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2425 __set_bit(BTN_RIGHT, mydata->input->keybit);
2426 __set_bit(BTN_LEFT, mydata->input->keybit);
2427 __set_bit(BTN_BACK, mydata->input->keybit);
2428 __set_bit(BTN_FORWARD, mydata->input->keybit);
2429
2430 __set_bit(EV_REL, mydata->input->evbit);
2431 __set_bit(REL_X, mydata->input->relbit);
2432 __set_bit(REL_Y, mydata->input->relbit);
2433 __set_bit(REL_WHEEL, mydata->input->relbit);
2434 __set_bit(REL_HWHEEL, mydata->input->relbit);
2435 }
2436
2437 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2438 struct hid_field *field, struct hid_usage *usage,
2439 unsigned long **bit, int *max)
2440 {
2441 return -1;
2442 }
2443
2444 /* ------------------------------------------------------------------------- */
2445 /* Logitech K400 devices */
2446 /* ------------------------------------------------------------------------- */
2447
2448 /*
2449 * The Logitech K400 keyboard has an embedded touchpad which is seen
2450 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2451 * tap-to-click but the setting is not remembered accross reset, annoying some
2452 * users.
2453 *
2454 * We can toggle this feature from the host by using the feature 0x6010:
2455 * Touchpad FW items
2456 */
2457
2458 struct k400_private_data {
2459 u8 feature_index;
2460 };
2461
2462 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2463 {
2464 struct k400_private_data *k400 = hidpp->private_data;
2465 struct hidpp_touchpad_fw_items items = {};
2466 int ret;
2467 u8 feature_type;
2468
2469 if (!k400->feature_index) {
2470 ret = hidpp_root_get_feature(hidpp,
2471 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2472 &k400->feature_index, &feature_type);
2473 if (ret)
2474 /* means that the device is not powered up */
2475 return ret;
2476 }
2477
2478 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2479 if (ret)
2480 return ret;
2481
2482 return 0;
2483 }
2484
2485 static int k400_allocate(struct hid_device *hdev)
2486 {
2487 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2488 struct k400_private_data *k400;
2489
2490 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2491 GFP_KERNEL);
2492 if (!k400)
2493 return -ENOMEM;
2494
2495 hidpp->private_data = k400;
2496
2497 return 0;
2498 };
2499
2500 static int k400_connect(struct hid_device *hdev, bool connected)
2501 {
2502 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2503
2504 if (!disable_tap_to_click)
2505 return 0;
2506
2507 return k400_disable_tap_to_click(hidpp);
2508 }
2509
2510 /* ------------------------------------------------------------------------- */
2511 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2512 /* ------------------------------------------------------------------------- */
2513
2514 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2515
2516 static int g920_get_config(struct hidpp_device *hidpp)
2517 {
2518 u8 feature_type;
2519 u8 feature_index;
2520 int ret;
2521
2522 /* Find feature and store for later use */
2523 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2524 &feature_index, &feature_type);
2525 if (ret)
2526 return ret;
2527
2528 ret = hidpp_ff_init(hidpp, feature_index);
2529 if (ret)
2530 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2531 ret);
2532
2533 return 0;
2534 }
2535
2536 /* -------------------------------------------------------------------------- */
2537 /* Generic HID++ devices */
2538 /* -------------------------------------------------------------------------- */
2539
2540 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2541 struct hid_field *field, struct hid_usage *usage,
2542 unsigned long **bit, int *max)
2543 {
2544 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2545
2546 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2547 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2548 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2549 field->application != HID_GD_MOUSE)
2550 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2551
2552 return 0;
2553 }
2554
2555 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2556 struct hid_field *field, struct hid_usage *usage,
2557 unsigned long **bit, int *max)
2558 {
2559 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2560
2561 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2562 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2563 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2564 usage->code == ABS_Y || usage->code == ABS_Z ||
2565 usage->code == ABS_RZ)) {
2566 field->application = HID_GD_MULTIAXIS;
2567 }
2568 }
2569
2570 return 0;
2571 }
2572
2573
2574 static void hidpp_populate_input(struct hidpp_device *hidpp,
2575 struct input_dev *input, bool origin_is_hid_core)
2576 {
2577 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2578 wtp_populate_input(hidpp, input, origin_is_hid_core);
2579 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2580 m560_populate_input(hidpp, input, origin_is_hid_core);
2581 }
2582
2583 static int hidpp_input_configured(struct hid_device *hdev,
2584 struct hid_input *hidinput)
2585 {
2586 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2587 struct input_dev *input = hidinput->input;
2588
2589 hidpp_populate_input(hidpp, input, true);
2590
2591 return 0;
2592 }
2593
2594 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2595 int size)
2596 {
2597 struct hidpp_report *question = hidpp->send_receive_buf;
2598 struct hidpp_report *answer = hidpp->send_receive_buf;
2599 struct hidpp_report *report = (struct hidpp_report *)data;
2600 int ret;
2601
2602 /*
2603 * If the mutex is locked then we have a pending answer from a
2604 * previously sent command.
2605 */
2606 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2607 /*
2608 * Check for a correct hidpp20 answer or the corresponding
2609 * error
2610 */
2611 if (hidpp_match_answer(question, report) ||
2612 hidpp_match_error(question, report)) {
2613 *answer = *report;
2614 hidpp->answer_available = true;
2615 wake_up(&hidpp->wait);
2616 /*
2617 * This was an answer to a command that this driver sent
2618 * We return 1 to hid-core to avoid forwarding the
2619 * command upstream as it has been treated by the driver
2620 */
2621
2622 return 1;
2623 }
2624 }
2625
2626 if (unlikely(hidpp_report_is_connect_event(report))) {
2627 atomic_set(&hidpp->connected,
2628 !(report->rap.params[0] & (1 << 6)));
2629 if (schedule_work(&hidpp->work) == 0)
2630 dbg_hid("%s: connect event already queued\n", __func__);
2631 return 1;
2632 }
2633
2634 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2635 ret = hidpp20_battery_event(hidpp, data, size);
2636 if (ret != 0)
2637 return ret;
2638 ret = hidpp_solar_battery_event(hidpp, data, size);
2639 if (ret != 0)
2640 return ret;
2641 }
2642
2643 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2644 ret = hidpp10_battery_event(hidpp, data, size);
2645 if (ret != 0)
2646 return ret;
2647 }
2648
2649 return 0;
2650 }
2651
2652 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2653 u8 *data, int size)
2654 {
2655 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2656 int ret = 0;
2657
2658 /* Generic HID++ processing. */
2659 switch (data[0]) {
2660 case REPORT_ID_HIDPP_VERY_LONG:
2661 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2662 hid_err(hdev, "received hid++ report of bad size (%d)",
2663 size);
2664 return 1;
2665 }
2666 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2667 break;
2668 case REPORT_ID_HIDPP_LONG:
2669 if (size != HIDPP_REPORT_LONG_LENGTH) {
2670 hid_err(hdev, "received hid++ report of bad size (%d)",
2671 size);
2672 return 1;
2673 }
2674 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2675 break;
2676 case REPORT_ID_HIDPP_SHORT:
2677 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2678 hid_err(hdev, "received hid++ report of bad size (%d)",
2679 size);
2680 return 1;
2681 }
2682 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2683 break;
2684 }
2685
2686 /* If no report is available for further processing, skip calling
2687 * raw_event of subclasses. */
2688 if (ret != 0)
2689 return ret;
2690
2691 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2692 return wtp_raw_event(hdev, data, size);
2693 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2694 return m560_raw_event(hdev, data, size);
2695
2696 return 0;
2697 }
2698
2699 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2700 {
2701 static atomic_t battery_no = ATOMIC_INIT(0);
2702 struct power_supply_config cfg = { .drv_data = hidpp };
2703 struct power_supply_desc *desc = &hidpp->battery.desc;
2704 enum power_supply_property *battery_props;
2705 struct hidpp_battery *battery;
2706 unsigned int num_battery_props;
2707 unsigned long n;
2708 int ret;
2709
2710 if (hidpp->battery.ps)
2711 return 0;
2712
2713 hidpp->battery.feature_index = 0xff;
2714 hidpp->battery.solar_feature_index = 0xff;
2715
2716 if (hidpp->protocol_major >= 2) {
2717 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
2718 ret = hidpp_solar_request_battery_event(hidpp);
2719 else
2720 ret = hidpp20_query_battery_info(hidpp);
2721
2722 if (ret)
2723 return ret;
2724 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2725 } else {
2726 ret = hidpp10_query_battery_status(hidpp);
2727 if (ret) {
2728 ret = hidpp10_query_battery_mileage(hidpp);
2729 if (ret)
2730 return -ENOENT;
2731 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2732 } else {
2733 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
2734 }
2735 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
2736 }
2737
2738 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
2739 hidpp_battery_props,
2740 sizeof(hidpp_battery_props),
2741 GFP_KERNEL);
2742 if (!battery_props)
2743 return -ENOMEM;
2744
2745 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
2746
2747 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2748 battery_props[num_battery_props++] =
2749 POWER_SUPPLY_PROP_CAPACITY;
2750
2751 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
2752 battery_props[num_battery_props++] =
2753 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
2754
2755 battery = &hidpp->battery;
2756
2757 n = atomic_inc_return(&battery_no) - 1;
2758 desc->properties = battery_props;
2759 desc->num_properties = num_battery_props;
2760 desc->get_property = hidpp_battery_get_property;
2761 sprintf(battery->name, "hidpp_battery_%ld", n);
2762 desc->name = battery->name;
2763 desc->type = POWER_SUPPLY_TYPE_BATTERY;
2764 desc->use_for_apm = 0;
2765
2766 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2767 &battery->desc,
2768 &cfg);
2769 if (IS_ERR(battery->ps))
2770 return PTR_ERR(battery->ps);
2771
2772 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2773
2774 return ret;
2775 }
2776
2777 static void hidpp_overwrite_name(struct hid_device *hdev)
2778 {
2779 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2780 char *name;
2781
2782 if (hidpp->protocol_major < 2)
2783 return;
2784
2785 name = hidpp_get_device_name(hidpp);
2786
2787 if (!name) {
2788 hid_err(hdev, "unable to retrieve the name of the device");
2789 } else {
2790 dbg_hid("HID++: Got name: %s\n", name);
2791 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
2792 }
2793
2794 kfree(name);
2795 }
2796
2797 static int hidpp_input_open(struct input_dev *dev)
2798 {
2799 struct hid_device *hid = input_get_drvdata(dev);
2800
2801 return hid_hw_open(hid);
2802 }
2803
2804 static void hidpp_input_close(struct input_dev *dev)
2805 {
2806 struct hid_device *hid = input_get_drvdata(dev);
2807
2808 hid_hw_close(hid);
2809 }
2810
2811 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2812 {
2813 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
2814 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2815
2816 if (!input_dev)
2817 return NULL;
2818
2819 input_set_drvdata(input_dev, hdev);
2820 input_dev->open = hidpp_input_open;
2821 input_dev->close = hidpp_input_close;
2822
2823 input_dev->name = hidpp->name;
2824 input_dev->phys = hdev->phys;
2825 input_dev->uniq = hdev->uniq;
2826 input_dev->id.bustype = hdev->bus;
2827 input_dev->id.vendor = hdev->vendor;
2828 input_dev->id.product = hdev->product;
2829 input_dev->id.version = hdev->version;
2830 input_dev->dev.parent = &hdev->dev;
2831
2832 return input_dev;
2833 }
2834
2835 static void hidpp_connect_event(struct hidpp_device *hidpp)
2836 {
2837 struct hid_device *hdev = hidpp->hid_dev;
2838 int ret = 0;
2839 bool connected = atomic_read(&hidpp->connected);
2840 struct input_dev *input;
2841 char *name, *devm_name;
2842
2843 if (!connected) {
2844 if (hidpp->battery.ps) {
2845 hidpp->battery.online = false;
2846 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
2847 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2848 power_supply_changed(hidpp->battery.ps);
2849 }
2850 return;
2851 }
2852
2853 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2854 ret = wtp_connect(hdev, connected);
2855 if (ret)
2856 return;
2857 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2858 ret = m560_send_config_command(hdev, connected);
2859 if (ret)
2860 return;
2861 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2862 ret = k400_connect(hdev, connected);
2863 if (ret)
2864 return;
2865 }
2866
2867 /* the device is already connected, we can ask for its name and
2868 * protocol */
2869 if (!hidpp->protocol_major) {
2870 ret = !hidpp_is_connected(hidpp);
2871 if (ret) {
2872 hid_err(hdev, "Can not get the protocol version.\n");
2873 return;
2874 }
2875 hid_info(hdev, "HID++ %u.%u device connected.\n",
2876 hidpp->protocol_major, hidpp->protocol_minor);
2877 }
2878
2879 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
2880 name = hidpp_get_device_name(hidpp);
2881 if (!name) {
2882 hid_err(hdev,
2883 "unable to retrieve the name of the device");
2884 return;
2885 }
2886
2887 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2888 kfree(name);
2889 if (!devm_name)
2890 return;
2891
2892 hidpp->name = devm_name;
2893 }
2894
2895 hidpp_initialize_battery(hidpp);
2896
2897 /* forward current battery state */
2898 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2899 hidpp10_enable_battery_reporting(hidpp);
2900 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2901 hidpp10_query_battery_mileage(hidpp);
2902 else
2903 hidpp10_query_battery_status(hidpp);
2904 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2905 hidpp20_query_battery_info(hidpp);
2906 }
2907 if (hidpp->battery.ps)
2908 power_supply_changed(hidpp->battery.ps);
2909
2910 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
2911 /* if the input nodes are already created, we can stop now */
2912 return;
2913
2914 input = hidpp_allocate_input(hdev);
2915 if (!input) {
2916 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2917 return;
2918 }
2919
2920 hidpp_populate_input(hidpp, input, false);
2921
2922 ret = input_register_device(input);
2923 if (ret)
2924 input_free_device(input);
2925
2926 hidpp->delayed_input = input;
2927 }
2928
2929 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
2930
2931 static struct attribute *sysfs_attrs[] = {
2932 &dev_attr_builtin_power_supply.attr,
2933 NULL
2934 };
2935
2936 static const struct attribute_group ps_attribute_group = {
2937 .attrs = sysfs_attrs
2938 };
2939
2940 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2941 {
2942 struct hidpp_device *hidpp;
2943 int ret;
2944 bool connected;
2945 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2946
2947 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2948 GFP_KERNEL);
2949 if (!hidpp)
2950 return -ENOMEM;
2951
2952 hidpp->hid_dev = hdev;
2953 hidpp->name = hdev->name;
2954 hid_set_drvdata(hdev, hidpp);
2955
2956 hidpp->quirks = id->driver_data;
2957
2958 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
2959 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
2960
2961 if (disable_raw_mode) {
2962 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
2963 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
2964 }
2965
2966 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2967 ret = wtp_allocate(hdev, id);
2968 if (ret)
2969 goto allocate_fail;
2970 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2971 ret = m560_allocate(hdev);
2972 if (ret)
2973 goto allocate_fail;
2974 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2975 ret = k400_allocate(hdev);
2976 if (ret)
2977 goto allocate_fail;
2978 }
2979
2980 INIT_WORK(&hidpp->work, delayed_work_cb);
2981 mutex_init(&hidpp->send_mutex);
2982 init_waitqueue_head(&hidpp->wait);
2983
2984 /* indicates we are handling the battery properties in the kernel */
2985 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
2986 if (ret)
2987 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
2988 hdev->name);
2989
2990 ret = hid_parse(hdev);
2991 if (ret) {
2992 hid_err(hdev, "%s:parse failed\n", __func__);
2993 goto hid_parse_fail;
2994 }
2995
2996 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2997 connect_mask &= ~HID_CONNECT_HIDINPUT;
2998
2999 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3000 ret = hid_hw_start(hdev, connect_mask);
3001 if (ret) {
3002 hid_err(hdev, "hw start failed\n");
3003 goto hid_hw_start_fail;
3004 }
3005 ret = hid_hw_open(hdev);
3006 if (ret < 0) {
3007 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3008 __func__, ret);
3009 hid_hw_stop(hdev);
3010 goto hid_hw_start_fail;
3011 }
3012 }
3013
3014
3015 /* Allow incoming packets */
3016 hid_device_io_start(hdev);
3017
3018 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3019 hidpp_unifying_init(hidpp);
3020
3021 connected = hidpp_is_connected(hidpp);
3022 atomic_set(&hidpp->connected, connected);
3023 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
3024 if (!connected) {
3025 ret = -ENODEV;
3026 hid_err(hdev, "Device not connected");
3027 goto hid_hw_open_failed;
3028 }
3029
3030 hid_info(hdev, "HID++ %u.%u device connected.\n",
3031 hidpp->protocol_major, hidpp->protocol_minor);
3032
3033 hidpp_overwrite_name(hdev);
3034 }
3035
3036 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
3037 ret = wtp_get_config(hidpp);
3038 if (ret)
3039 goto hid_hw_open_failed;
3040 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3041 ret = g920_get_config(hidpp);
3042 if (ret)
3043 goto hid_hw_open_failed;
3044 }
3045
3046 /* Block incoming packets */
3047 hid_device_io_stop(hdev);
3048
3049 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3050 ret = hid_hw_start(hdev, connect_mask);
3051 if (ret) {
3052 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3053 goto hid_hw_start_fail;
3054 }
3055 }
3056
3057 /* Allow incoming packets */
3058 hid_device_io_start(hdev);
3059
3060 hidpp_connect_event(hidpp);
3061
3062 return ret;
3063
3064 hid_hw_open_failed:
3065 hid_device_io_stop(hdev);
3066 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3067 hid_hw_close(hdev);
3068 hid_hw_stop(hdev);
3069 }
3070 hid_hw_start_fail:
3071 hid_parse_fail:
3072 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3073 cancel_work_sync(&hidpp->work);
3074 mutex_destroy(&hidpp->send_mutex);
3075 allocate_fail:
3076 hid_set_drvdata(hdev, NULL);
3077 return ret;
3078 }
3079
3080 static void hidpp_remove(struct hid_device *hdev)
3081 {
3082 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3083
3084 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3085
3086 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3087 hidpp_ff_deinit(hdev);
3088 hid_hw_close(hdev);
3089 }
3090 hid_hw_stop(hdev);
3091 cancel_work_sync(&hidpp->work);
3092 mutex_destroy(&hidpp->send_mutex);
3093 }
3094
3095 static const struct hid_device_id hidpp_devices[] = {
3096 { /* wireless touchpad */
3097 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3098 USB_VENDOR_ID_LOGITECH, 0x4011),
3099 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3100 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
3101 { /* wireless touchpad T650 */
3102 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3103 USB_VENDOR_ID_LOGITECH, 0x4101),
3104 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3105 { /* wireless touchpad T651 */
3106 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3107 USB_DEVICE_ID_LOGITECH_T651),
3108 .driver_data = HIDPP_QUIRK_CLASS_WTP },
3109 { /* Mouse logitech M560 */
3110 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3111 USB_VENDOR_ID_LOGITECH, 0x402d),
3112 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
3113 { /* Keyboard logitech K400 */
3114 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3115 USB_VENDOR_ID_LOGITECH, 0x4024),
3116 .driver_data = HIDPP_QUIRK_CLASS_K400 },
3117 { /* Solar Keyboard Logitech K750 */
3118 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3119 USB_VENDOR_ID_LOGITECH, 0x4002),
3120 .driver_data = HIDPP_QUIRK_CLASS_K750 },
3121
3122 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3123 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
3124
3125 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
3126 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
3127 {}
3128 };
3129
3130 MODULE_DEVICE_TABLE(hid, hidpp_devices);
3131
3132 static struct hid_driver hidpp_driver = {
3133 .name = "logitech-hidpp-device",
3134 .id_table = hidpp_devices,
3135 .probe = hidpp_probe,
3136 .remove = hidpp_remove,
3137 .raw_event = hidpp_raw_event,
3138 .input_configured = hidpp_input_configured,
3139 .input_mapping = hidpp_input_mapping,
3140 .input_mapped = hidpp_input_mapped,
3141 };
3142
3143 module_hid_driver(hidpp_driver);