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