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