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