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