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