GNU Linux-libre 4.19.304-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), "%4phD", &serial);
679         dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
680
681         name = hidpp_unifying_get_name(hidpp);
682         if (!name)
683                 return -EIO;
684
685         snprintf(hdev->name, sizeof(hdev->name), "%s", name);
686         dbg_hid("HID++ Unifying: Got name: %s\n", name);
687
688         kfree(name);
689         return 0;
690 }
691
692 /* -------------------------------------------------------------------------- */
693 /* 0x0000: Root                                                               */
694 /* -------------------------------------------------------------------------- */
695
696 #define HIDPP_PAGE_ROOT                                 0x0000
697 #define HIDPP_PAGE_ROOT_IDX                             0x00
698
699 #define CMD_ROOT_GET_FEATURE                            0x01
700 #define CMD_ROOT_GET_PROTOCOL_VERSION                   0x11
701
702 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
703         u8 *feature_index, u8 *feature_type)
704 {
705         struct hidpp_report response;
706         int ret;
707         u8 params[2] = { feature >> 8, feature & 0x00FF };
708
709         ret = hidpp_send_fap_command_sync(hidpp,
710                         HIDPP_PAGE_ROOT_IDX,
711                         CMD_ROOT_GET_FEATURE,
712                         params, 2, &response);
713         if (ret)
714                 return ret;
715
716         if (response.fap.params[0] == 0)
717                 return -ENOENT;
718
719         *feature_index = response.fap.params[0];
720         *feature_type = response.fap.params[1];
721
722         return ret;
723 }
724
725 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
726 {
727         const u8 ping_byte = 0x5a;
728         u8 ping_data[3] = { 0, 0, ping_byte };
729         struct hidpp_report response;
730         int ret;
731
732         ret = hidpp_send_rap_command_sync(hidpp,
733                         REPORT_ID_HIDPP_SHORT,
734                         HIDPP_PAGE_ROOT_IDX,
735                         CMD_ROOT_GET_PROTOCOL_VERSION,
736                         ping_data, sizeof(ping_data), &response);
737
738         if (ret == HIDPP_ERROR_INVALID_SUBID) {
739                 hidpp->protocol_major = 1;
740                 hidpp->protocol_minor = 0;
741                 return 0;
742         }
743
744         /* the device might not be connected */
745         if (ret == HIDPP_ERROR_RESOURCE_ERROR)
746                 return -EIO;
747
748         if (ret > 0) {
749                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
750                         __func__, ret);
751                 return -EPROTO;
752         }
753         if (ret)
754                 return ret;
755
756         if (response.rap.params[2] != ping_byte) {
757                 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
758                         __func__, response.rap.params[2], ping_byte);
759                 return -EPROTO;
760         }
761
762         hidpp->protocol_major = response.rap.params[0];
763         hidpp->protocol_minor = response.rap.params[1];
764
765         return ret;
766 }
767
768 static bool hidpp_is_connected(struct hidpp_device *hidpp)
769 {
770         int ret;
771
772         ret = hidpp_root_get_protocol_version(hidpp);
773         if (!ret)
774                 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
775                         hidpp->protocol_major, hidpp->protocol_minor);
776         return ret == 0;
777 }
778
779 /* -------------------------------------------------------------------------- */
780 /* 0x0003: Device Information                                                 */
781 /* -------------------------------------------------------------------------- */
782
783 #define HIDPP_PAGE_DEVICE_INFORMATION                   0x0003
784
785 #define CMD_GET_DEVICE_INFO                             0x00
786
787 static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial)
788 {
789         struct hidpp_report response;
790         u8 feature_type;
791         u8 feature_index;
792         int ret;
793
794         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION,
795                                      &feature_index,
796                                      &feature_type);
797         if (ret)
798                 return ret;
799
800         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
801                                           CMD_GET_DEVICE_INFO,
802                                           NULL, 0, &response);
803         if (ret)
804                 return ret;
805
806         /* See hidpp_unifying_get_serial() */
807         *serial = *((u32 *)&response.rap.params[1]);
808         return 0;
809 }
810
811 static int hidpp_serial_init(struct hidpp_device *hidpp)
812 {
813         struct hid_device *hdev = hidpp->hid_dev;
814         u32 serial;
815         int ret;
816
817         ret = hidpp_get_serial(hidpp, &serial);
818         if (ret)
819                 return ret;
820
821         snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial);
822         dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq);
823
824         return 0;
825 }
826
827 /* -------------------------------------------------------------------------- */
828 /* 0x0005: GetDeviceNameType                                                  */
829 /* -------------------------------------------------------------------------- */
830
831 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE                 0x0005
832
833 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT              0x01
834 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME        0x11
835 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE               0x21
836
837 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
838         u8 feature_index, u8 *nameLength)
839 {
840         struct hidpp_report response;
841         int ret;
842
843         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
844                 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
845
846         if (ret > 0) {
847                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
848                         __func__, ret);
849                 return -EPROTO;
850         }
851         if (ret)
852                 return ret;
853
854         *nameLength = response.fap.params[0];
855
856         return ret;
857 }
858
859 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
860         u8 feature_index, u8 char_index, char *device_name, int len_buf)
861 {
862         struct hidpp_report response;
863         int ret, i;
864         int count;
865
866         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
867                 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
868                 &response);
869
870         if (ret > 0) {
871                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
872                         __func__, ret);
873                 return -EPROTO;
874         }
875         if (ret)
876                 return ret;
877
878         switch (response.report_id) {
879         case REPORT_ID_HIDPP_VERY_LONG:
880                 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
881                 break;
882         case REPORT_ID_HIDPP_LONG:
883                 count = HIDPP_REPORT_LONG_LENGTH - 4;
884                 break;
885         case REPORT_ID_HIDPP_SHORT:
886                 count = HIDPP_REPORT_SHORT_LENGTH - 4;
887                 break;
888         default:
889                 return -EPROTO;
890         }
891
892         if (len_buf < count)
893                 count = len_buf;
894
895         for (i = 0; i < count; i++)
896                 device_name[i] = response.fap.params[i];
897
898         return count;
899 }
900
901 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
902 {
903         u8 feature_type;
904         u8 feature_index;
905         u8 __name_length;
906         char *name;
907         unsigned index = 0;
908         int ret;
909
910         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
911                 &feature_index, &feature_type);
912         if (ret)
913                 return NULL;
914
915         ret = hidpp_devicenametype_get_count(hidpp, feature_index,
916                 &__name_length);
917         if (ret)
918                 return NULL;
919
920         name = kzalloc(__name_length + 1, GFP_KERNEL);
921         if (!name)
922                 return NULL;
923
924         while (index < __name_length) {
925                 ret = hidpp_devicenametype_get_device_name(hidpp,
926                         feature_index, index, name + index,
927                         __name_length - index);
928                 if (ret <= 0) {
929                         kfree(name);
930                         return NULL;
931                 }
932                 index += ret;
933         }
934
935         /* include the terminating '\0' */
936         hidpp_prefix_name(&name, __name_length + 1);
937
938         return name;
939 }
940
941 /* -------------------------------------------------------------------------- */
942 /* 0x1000: Battery level status                                               */
943 /* -------------------------------------------------------------------------- */
944
945 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS                         0x1000
946
947 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS       0x00
948 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY         0x10
949
950 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST                    0x00
951
952 #define FLAG_BATTERY_LEVEL_DISABLE_OSD                          BIT(0)
953 #define FLAG_BATTERY_LEVEL_MILEAGE                              BIT(1)
954 #define FLAG_BATTERY_LEVEL_RECHARGEABLE                         BIT(2)
955
956 static int hidpp_map_battery_level(int capacity)
957 {
958         if (capacity < 11)
959                 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
960         /*
961          * The spec says this should be < 31 but some devices report 30
962          * with brand new batteries and Windows reports 30 as "Good".
963          */
964         else if (capacity < 30)
965                 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
966         else if (capacity < 81)
967                 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
968         return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
969 }
970
971 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
972                                                     int *next_capacity,
973                                                     int *level)
974 {
975         int status;
976
977         *capacity = data[0];
978         *next_capacity = data[1];
979         *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
980
981         /* When discharging, we can rely on the device reported capacity.
982          * For all other states the device reports 0 (unknown).
983          */
984         switch (data[2]) {
985                 case 0: /* discharging (in use) */
986                         status = POWER_SUPPLY_STATUS_DISCHARGING;
987                         *level = hidpp_map_battery_level(*capacity);
988                         break;
989                 case 1: /* recharging */
990                         status = POWER_SUPPLY_STATUS_CHARGING;
991                         break;
992                 case 2: /* charge in final stage */
993                         status = POWER_SUPPLY_STATUS_CHARGING;
994                         break;
995                 case 3: /* charge complete */
996                         status = POWER_SUPPLY_STATUS_FULL;
997                         *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
998                         *capacity = 100;
999                         break;
1000                 case 4: /* recharging below optimal speed */
1001                         status = POWER_SUPPLY_STATUS_CHARGING;
1002                         break;
1003                 /* 5 = invalid battery type
1004                    6 = thermal error
1005                    7 = other charging error */
1006                 default:
1007                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1008                         break;
1009         }
1010
1011         return status;
1012 }
1013
1014 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1015                                                      u8 feature_index,
1016                                                      int *status,
1017                                                      int *capacity,
1018                                                      int *next_capacity,
1019                                                      int *level)
1020 {
1021         struct hidpp_report response;
1022         int ret;
1023         u8 *params = (u8 *)response.fap.params;
1024
1025         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1026                                           CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1027                                           NULL, 0, &response);
1028         /* Ignore these intermittent errors */
1029         if (ret == HIDPP_ERROR_RESOURCE_ERROR)
1030                 return -EIO;
1031         if (ret > 0) {
1032                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1033                         __func__, ret);
1034                 return -EPROTO;
1035         }
1036         if (ret)
1037                 return ret;
1038
1039         *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1040                                                            next_capacity,
1041                                                            level);
1042
1043         return 0;
1044 }
1045
1046 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1047                                                   u8 feature_index)
1048 {
1049         struct hidpp_report response;
1050         int ret;
1051         u8 *params = (u8 *)response.fap.params;
1052         unsigned int level_count, flags;
1053
1054         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1055                                           CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1056                                           NULL, 0, &response);
1057         if (ret > 0) {
1058                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1059                         __func__, ret);
1060                 return -EPROTO;
1061         }
1062         if (ret)
1063                 return ret;
1064
1065         level_count = params[0];
1066         flags = params[1];
1067
1068         if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1069                 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1070         else
1071                 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1072
1073         return 0;
1074 }
1075
1076 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1077 {
1078         u8 feature_type;
1079         int ret;
1080         int status, capacity, next_capacity, level;
1081
1082         if (hidpp->battery.feature_index == 0xff) {
1083                 ret = hidpp_root_get_feature(hidpp,
1084                                              HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1085                                              &hidpp->battery.feature_index,
1086                                              &feature_type);
1087                 if (ret)
1088                         return ret;
1089         }
1090
1091         ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1092                                                 hidpp->battery.feature_index,
1093                                                 &status, &capacity,
1094                                                 &next_capacity, &level);
1095         if (ret)
1096                 return ret;
1097
1098         ret = hidpp20_batterylevel_get_battery_info(hidpp,
1099                                                 hidpp->battery.feature_index);
1100         if (ret)
1101                 return ret;
1102
1103         hidpp->battery.status = status;
1104         hidpp->battery.capacity = capacity;
1105         hidpp->battery.level = level;
1106         /* the capacity is only available when discharging or full */
1107         hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1108                                 status == POWER_SUPPLY_STATUS_FULL;
1109
1110         return 0;
1111 }
1112
1113 static int hidpp20_battery_event(struct hidpp_device *hidpp,
1114                                  u8 *data, int size)
1115 {
1116         struct hidpp_report *report = (struct hidpp_report *)data;
1117         int status, capacity, next_capacity, level;
1118         bool changed;
1119
1120         if (report->fap.feature_index != hidpp->battery.feature_index ||
1121             report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1122                 return 0;
1123
1124         status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1125                                                           &capacity,
1126                                                           &next_capacity,
1127                                                           &level);
1128
1129         /* the capacity is only available when discharging or full */
1130         hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1131                                 status == POWER_SUPPLY_STATUS_FULL;
1132
1133         changed = capacity != hidpp->battery.capacity ||
1134                   level != hidpp->battery.level ||
1135                   status != hidpp->battery.status;
1136
1137         if (changed) {
1138                 hidpp->battery.level = level;
1139                 hidpp->battery.capacity = capacity;
1140                 hidpp->battery.status = status;
1141                 if (hidpp->battery.ps)
1142                         power_supply_changed(hidpp->battery.ps);
1143         }
1144
1145         return 0;
1146 }
1147
1148 static enum power_supply_property hidpp_battery_props[] = {
1149         POWER_SUPPLY_PROP_ONLINE,
1150         POWER_SUPPLY_PROP_STATUS,
1151         POWER_SUPPLY_PROP_SCOPE,
1152         POWER_SUPPLY_PROP_MODEL_NAME,
1153         POWER_SUPPLY_PROP_MANUFACTURER,
1154         POWER_SUPPLY_PROP_SERIAL_NUMBER,
1155         0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1156         0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1157 };
1158
1159 static int hidpp_battery_get_property(struct power_supply *psy,
1160                                       enum power_supply_property psp,
1161                                       union power_supply_propval *val)
1162 {
1163         struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1164         int ret = 0;
1165
1166         switch(psp) {
1167                 case POWER_SUPPLY_PROP_STATUS:
1168                         val->intval = hidpp->battery.status;
1169                         break;
1170                 case POWER_SUPPLY_PROP_CAPACITY:
1171                         val->intval = hidpp->battery.capacity;
1172                         break;
1173                 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1174                         val->intval = hidpp->battery.level;
1175                         break;
1176                 case POWER_SUPPLY_PROP_SCOPE:
1177                         val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1178                         break;
1179                 case POWER_SUPPLY_PROP_ONLINE:
1180                         val->intval = hidpp->battery.online;
1181                         break;
1182                 case POWER_SUPPLY_PROP_MODEL_NAME:
1183                         if (!strncmp(hidpp->name, "Logitech ", 9))
1184                                 val->strval = hidpp->name + 9;
1185                         else
1186                                 val->strval = hidpp->name;
1187                         break;
1188                 case POWER_SUPPLY_PROP_MANUFACTURER:
1189                         val->strval = "Logitech";
1190                         break;
1191                 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1192                         val->strval = hidpp->hid_dev->uniq;
1193                         break;
1194                 default:
1195                         ret = -EINVAL;
1196                         break;
1197         }
1198
1199         return ret;
1200 }
1201
1202 /* -------------------------------------------------------------------------- */
1203 /* 0x4301: Solar Keyboard                                                     */
1204 /* -------------------------------------------------------------------------- */
1205
1206 #define HIDPP_PAGE_SOLAR_KEYBOARD                       0x4301
1207
1208 #define CMD_SOLAR_SET_LIGHT_MEASURE                     0x00
1209
1210 #define EVENT_SOLAR_BATTERY_BROADCAST                   0x00
1211 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE               0x10
1212 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON                  0x20
1213
1214 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1215 {
1216         struct hidpp_report response;
1217         u8 params[2] = { 1, 1 };
1218         u8 feature_type;
1219         int ret;
1220
1221         if (hidpp->battery.feature_index == 0xff) {
1222                 ret = hidpp_root_get_feature(hidpp,
1223                                              HIDPP_PAGE_SOLAR_KEYBOARD,
1224                                              &hidpp->battery.solar_feature_index,
1225                                              &feature_type);
1226                 if (ret)
1227                         return ret;
1228         }
1229
1230         ret = hidpp_send_fap_command_sync(hidpp,
1231                                           hidpp->battery.solar_feature_index,
1232                                           CMD_SOLAR_SET_LIGHT_MEASURE,
1233                                           params, 2, &response);
1234         if (ret > 0) {
1235                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1236                         __func__, ret);
1237                 return -EPROTO;
1238         }
1239         if (ret)
1240                 return ret;
1241
1242         hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1243
1244         return 0;
1245 }
1246
1247 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1248                                      u8 *data, int size)
1249 {
1250         struct hidpp_report *report = (struct hidpp_report *)data;
1251         int capacity, lux, status;
1252         u8 function;
1253
1254         function = report->fap.funcindex_clientid;
1255
1256
1257         if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1258             !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1259               function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1260               function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1261                 return 0;
1262
1263         capacity = report->fap.params[0];
1264
1265         switch (function) {
1266         case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1267                 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1268                 if (lux > 200)
1269                         status = POWER_SUPPLY_STATUS_CHARGING;
1270                 else
1271                         status = POWER_SUPPLY_STATUS_DISCHARGING;
1272                 break;
1273         case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1274         default:
1275                 if (capacity < hidpp->battery.capacity)
1276                         status = POWER_SUPPLY_STATUS_DISCHARGING;
1277                 else
1278                         status = POWER_SUPPLY_STATUS_CHARGING;
1279
1280         }
1281
1282         if (capacity == 100)
1283                 status = POWER_SUPPLY_STATUS_FULL;
1284
1285         hidpp->battery.online = true;
1286         if (capacity != hidpp->battery.capacity ||
1287             status != hidpp->battery.status) {
1288                 hidpp->battery.capacity = capacity;
1289                 hidpp->battery.status = status;
1290                 if (hidpp->battery.ps)
1291                         power_supply_changed(hidpp->battery.ps);
1292         }
1293
1294         return 0;
1295 }
1296
1297 /* -------------------------------------------------------------------------- */
1298 /* 0x6010: Touchpad FW items                                                  */
1299 /* -------------------------------------------------------------------------- */
1300
1301 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS                    0x6010
1302
1303 #define CMD_TOUCHPAD_FW_ITEMS_SET                       0x10
1304
1305 struct hidpp_touchpad_fw_items {
1306         uint8_t presence;
1307         uint8_t desired_state;
1308         uint8_t state;
1309         uint8_t persistent;
1310 };
1311
1312 /**
1313  * send a set state command to the device by reading the current items->state
1314  * field. items is then filled with the current state.
1315  */
1316 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1317                                        u8 feature_index,
1318                                        struct hidpp_touchpad_fw_items *items)
1319 {
1320         struct hidpp_report response;
1321         int ret;
1322         u8 *params = (u8 *)response.fap.params;
1323
1324         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1325                 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1326
1327         if (ret > 0) {
1328                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1329                         __func__, ret);
1330                 return -EPROTO;
1331         }
1332         if (ret)
1333                 return ret;
1334
1335         items->presence = params[0];
1336         items->desired_state = params[1];
1337         items->state = params[2];
1338         items->persistent = params[3];
1339
1340         return 0;
1341 }
1342
1343 /* -------------------------------------------------------------------------- */
1344 /* 0x6100: TouchPadRawXY                                                      */
1345 /* -------------------------------------------------------------------------- */
1346
1347 #define HIDPP_PAGE_TOUCHPAD_RAW_XY                      0x6100
1348
1349 #define CMD_TOUCHPAD_GET_RAW_INFO                       0x01
1350 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE               0x21
1351
1352 #define EVENT_TOUCHPAD_RAW_XY                           0x00
1353
1354 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT               0x01
1355 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT               0x03
1356
1357 struct hidpp_touchpad_raw_info {
1358         u16 x_size;
1359         u16 y_size;
1360         u8 z_range;
1361         u8 area_range;
1362         u8 timestamp_unit;
1363         u8 maxcontacts;
1364         u8 origin;
1365         u16 res;
1366 };
1367
1368 struct hidpp_touchpad_raw_xy_finger {
1369         u8 contact_type;
1370         u8 contact_status;
1371         u16 x;
1372         u16 y;
1373         u8 z;
1374         u8 area;
1375         u8 finger_id;
1376 };
1377
1378 struct hidpp_touchpad_raw_xy {
1379         u16 timestamp;
1380         struct hidpp_touchpad_raw_xy_finger fingers[2];
1381         u8 spurious_flag;
1382         u8 end_of_frame;
1383         u8 finger_count;
1384         u8 button;
1385 };
1386
1387 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1388         u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1389 {
1390         struct hidpp_report response;
1391         int ret;
1392         u8 *params = (u8 *)response.fap.params;
1393
1394         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1395                 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1396
1397         if (ret > 0) {
1398                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1399                         __func__, ret);
1400                 return -EPROTO;
1401         }
1402         if (ret)
1403                 return ret;
1404
1405         raw_info->x_size = get_unaligned_be16(&params[0]);
1406         raw_info->y_size = get_unaligned_be16(&params[2]);
1407         raw_info->z_range = params[4];
1408         raw_info->area_range = params[5];
1409         raw_info->maxcontacts = params[7];
1410         raw_info->origin = params[8];
1411         /* res is given in unit per inch */
1412         raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1413
1414         return ret;
1415 }
1416
1417 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1418                 u8 feature_index, bool send_raw_reports,
1419                 bool sensor_enhanced_settings)
1420 {
1421         struct hidpp_report response;
1422
1423         /*
1424          * Params:
1425          *   bit 0 - enable raw
1426          *   bit 1 - 16bit Z, no area
1427          *   bit 2 - enhanced sensitivity
1428          *   bit 3 - width, height (4 bits each) instead of area
1429          *   bit 4 - send raw + gestures (degrades smoothness)
1430          *   remaining bits - reserved
1431          */
1432         u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1433
1434         return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1435                 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1436 }
1437
1438 static void hidpp_touchpad_touch_event(u8 *data,
1439         struct hidpp_touchpad_raw_xy_finger *finger)
1440 {
1441         u8 x_m = data[0] << 2;
1442         u8 y_m = data[2] << 2;
1443
1444         finger->x = x_m << 6 | data[1];
1445         finger->y = y_m << 6 | data[3];
1446
1447         finger->contact_type = data[0] >> 6;
1448         finger->contact_status = data[2] >> 6;
1449
1450         finger->z = data[4];
1451         finger->area = data[5];
1452         finger->finger_id = data[6] >> 4;
1453 }
1454
1455 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1456                 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1457 {
1458         memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1459         raw_xy->end_of_frame = data[8] & 0x01;
1460         raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1461         raw_xy->finger_count = data[15] & 0x0f;
1462         raw_xy->button = (data[8] >> 2) & 0x01;
1463
1464         if (raw_xy->finger_count) {
1465                 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1466                 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1467         }
1468 }
1469
1470 /* -------------------------------------------------------------------------- */
1471 /* 0x8123: Force feedback support                                             */
1472 /* -------------------------------------------------------------------------- */
1473
1474 #define HIDPP_FF_GET_INFO               0x01
1475 #define HIDPP_FF_RESET_ALL              0x11
1476 #define HIDPP_FF_DOWNLOAD_EFFECT        0x21
1477 #define HIDPP_FF_SET_EFFECT_STATE       0x31
1478 #define HIDPP_FF_DESTROY_EFFECT         0x41
1479 #define HIDPP_FF_GET_APERTURE           0x51
1480 #define HIDPP_FF_SET_APERTURE           0x61
1481 #define HIDPP_FF_GET_GLOBAL_GAINS       0x71
1482 #define HIDPP_FF_SET_GLOBAL_GAINS       0x81
1483
1484 #define HIDPP_FF_EFFECT_STATE_GET       0x00
1485 #define HIDPP_FF_EFFECT_STATE_STOP      0x01
1486 #define HIDPP_FF_EFFECT_STATE_PLAY      0x02
1487 #define HIDPP_FF_EFFECT_STATE_PAUSE     0x03
1488
1489 #define HIDPP_FF_EFFECT_CONSTANT        0x00
1490 #define HIDPP_FF_EFFECT_PERIODIC_SINE           0x01
1491 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE         0x02
1492 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE       0x03
1493 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP     0x04
1494 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN   0x05
1495 #define HIDPP_FF_EFFECT_SPRING          0x06
1496 #define HIDPP_FF_EFFECT_DAMPER          0x07
1497 #define HIDPP_FF_EFFECT_FRICTION        0x08
1498 #define HIDPP_FF_EFFECT_INERTIA         0x09
1499 #define HIDPP_FF_EFFECT_RAMP            0x0A
1500
1501 #define HIDPP_FF_EFFECT_AUTOSTART       0x80
1502
1503 #define HIDPP_FF_EFFECTID_NONE          -1
1504 #define HIDPP_FF_EFFECTID_AUTOCENTER    -2
1505
1506 #define HIDPP_FF_MAX_PARAMS     20
1507 #define HIDPP_FF_RESERVED_SLOTS 1
1508
1509 struct hidpp_ff_private_data {
1510         struct hidpp_device *hidpp;
1511         u8 feature_index;
1512         u8 version;
1513         u16 gain;
1514         s16 range;
1515         u8 slot_autocenter;
1516         u8 num_effects;
1517         int *effect_ids;
1518         struct workqueue_struct *wq;
1519         atomic_t workqueue_size;
1520 };
1521
1522 struct hidpp_ff_work_data {
1523         struct work_struct work;
1524         struct hidpp_ff_private_data *data;
1525         int effect_id;
1526         u8 command;
1527         u8 params[HIDPP_FF_MAX_PARAMS];
1528         u8 size;
1529 };
1530
1531 static const signed short hiddpp_ff_effects[] = {
1532         FF_CONSTANT,
1533         FF_PERIODIC,
1534         FF_SINE,
1535         FF_SQUARE,
1536         FF_SAW_UP,
1537         FF_SAW_DOWN,
1538         FF_TRIANGLE,
1539         FF_SPRING,
1540         FF_DAMPER,
1541         FF_AUTOCENTER,
1542         FF_GAIN,
1543         -1
1544 };
1545
1546 static const signed short hiddpp_ff_effects_v2[] = {
1547         FF_RAMP,
1548         FF_FRICTION,
1549         FF_INERTIA,
1550         -1
1551 };
1552
1553 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1554         HIDPP_FF_EFFECT_SPRING,
1555         HIDPP_FF_EFFECT_FRICTION,
1556         HIDPP_FF_EFFECT_DAMPER,
1557         HIDPP_FF_EFFECT_INERTIA
1558 };
1559
1560 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1561         "spring",
1562         "friction",
1563         "damper",
1564         "inertia"
1565 };
1566
1567
1568 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1569 {
1570         int i;
1571
1572         for (i = 0; i < data->num_effects; i++)
1573                 if (data->effect_ids[i] == effect_id)
1574                         return i+1;
1575
1576         return 0;
1577 }
1578
1579 static void hidpp_ff_work_handler(struct work_struct *w)
1580 {
1581         struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1582         struct hidpp_ff_private_data *data = wd->data;
1583         struct hidpp_report response;
1584         u8 slot;
1585         int ret;
1586
1587         /* add slot number if needed */
1588         switch (wd->effect_id) {
1589         case HIDPP_FF_EFFECTID_AUTOCENTER:
1590                 wd->params[0] = data->slot_autocenter;
1591                 break;
1592         case HIDPP_FF_EFFECTID_NONE:
1593                 /* leave slot as zero */
1594                 break;
1595         default:
1596                 /* find current slot for effect */
1597                 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1598                 break;
1599         }
1600
1601         /* send command and wait for reply */
1602         ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1603                 wd->command, wd->params, wd->size, &response);
1604
1605         if (ret) {
1606                 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1607                 goto out;
1608         }
1609
1610         /* parse return data */
1611         switch (wd->command) {
1612         case HIDPP_FF_DOWNLOAD_EFFECT:
1613                 slot = response.fap.params[0];
1614                 if (slot > 0 && slot <= data->num_effects) {
1615                         if (wd->effect_id >= 0)
1616                                 /* regular effect uploaded */
1617                                 data->effect_ids[slot-1] = wd->effect_id;
1618                         else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1619                                 /* autocenter spring uploaded */
1620                                 data->slot_autocenter = slot;
1621                 }
1622                 break;
1623         case HIDPP_FF_DESTROY_EFFECT:
1624                 if (wd->effect_id >= 0)
1625                         /* regular effect destroyed */
1626                         data->effect_ids[wd->params[0]-1] = -1;
1627                 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1628                         /* autocenter spring destoyed */
1629                         data->slot_autocenter = 0;
1630                 break;
1631         case HIDPP_FF_SET_GLOBAL_GAINS:
1632                 data->gain = (wd->params[0] << 8) + wd->params[1];
1633                 break;
1634         case HIDPP_FF_SET_APERTURE:
1635                 data->range = (wd->params[0] << 8) + wd->params[1];
1636                 break;
1637         default:
1638                 /* no action needed */
1639                 break;
1640         }
1641
1642 out:
1643         atomic_dec(&data->workqueue_size);
1644         kfree(wd);
1645 }
1646
1647 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1648 {
1649         struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1650         int s;
1651
1652         if (!wd)
1653                 return -ENOMEM;
1654
1655         INIT_WORK(&wd->work, hidpp_ff_work_handler);
1656
1657         wd->data = data;
1658         wd->effect_id = effect_id;
1659         wd->command = command;
1660         wd->size = size;
1661         memcpy(wd->params, params, size);
1662
1663         atomic_inc(&data->workqueue_size);
1664         queue_work(data->wq, &wd->work);
1665
1666         /* warn about excessive queue size */
1667         s = atomic_read(&data->workqueue_size);
1668         if (s >= 20 && s % 20 == 0)
1669                 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1670
1671         return 0;
1672 }
1673
1674 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1675 {
1676         struct hidpp_ff_private_data *data = dev->ff->private;
1677         u8 params[20];
1678         u8 size;
1679         int force;
1680
1681         /* set common parameters */
1682         params[2] = effect->replay.length >> 8;
1683         params[3] = effect->replay.length & 255;
1684         params[4] = effect->replay.delay >> 8;
1685         params[5] = effect->replay.delay & 255;
1686
1687         switch (effect->type) {
1688         case FF_CONSTANT:
1689                 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1690                 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1691                 params[6] = force >> 8;
1692                 params[7] = force & 255;
1693                 params[8] = effect->u.constant.envelope.attack_level >> 7;
1694                 params[9] = effect->u.constant.envelope.attack_length >> 8;
1695                 params[10] = effect->u.constant.envelope.attack_length & 255;
1696                 params[11] = effect->u.constant.envelope.fade_level >> 7;
1697                 params[12] = effect->u.constant.envelope.fade_length >> 8;
1698                 params[13] = effect->u.constant.envelope.fade_length & 255;
1699                 size = 14;
1700                 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1701                                 effect->u.constant.level,
1702                                 effect->direction, force);
1703                 dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1704                                 effect->u.constant.envelope.attack_level,
1705                                 effect->u.constant.envelope.attack_length,
1706                                 effect->u.constant.envelope.fade_level,
1707                                 effect->u.constant.envelope.fade_length);
1708                 break;
1709         case FF_PERIODIC:
1710         {
1711                 switch (effect->u.periodic.waveform) {
1712                 case FF_SINE:
1713                         params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1714                         break;
1715                 case FF_SQUARE:
1716                         params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1717                         break;
1718                 case FF_SAW_UP:
1719                         params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1720                         break;
1721                 case FF_SAW_DOWN:
1722                         params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1723                         break;
1724                 case FF_TRIANGLE:
1725                         params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1726                         break;
1727                 default:
1728                         hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1729                         return -EINVAL;
1730                 }
1731                 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1732                 params[6] = effect->u.periodic.magnitude >> 8;
1733                 params[7] = effect->u.periodic.magnitude & 255;
1734                 params[8] = effect->u.periodic.offset >> 8;
1735                 params[9] = effect->u.periodic.offset & 255;
1736                 params[10] = effect->u.periodic.period >> 8;
1737                 params[11] = effect->u.periodic.period & 255;
1738                 params[12] = effect->u.periodic.phase >> 8;
1739                 params[13] = effect->u.periodic.phase & 255;
1740                 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1741                 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1742                 params[16] = effect->u.periodic.envelope.attack_length & 255;
1743                 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1744                 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1745                 params[19] = effect->u.periodic.envelope.fade_length & 255;
1746                 size = 20;
1747                 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1748                                 effect->u.periodic.magnitude, effect->direction,
1749                                 effect->u.periodic.offset,
1750                                 effect->u.periodic.period,
1751                                 effect->u.periodic.phase);
1752                 dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1753                                 effect->u.periodic.envelope.attack_level,
1754                                 effect->u.periodic.envelope.attack_length,
1755                                 effect->u.periodic.envelope.fade_level,
1756                                 effect->u.periodic.envelope.fade_length);
1757                 break;
1758         }
1759         case FF_RAMP:
1760                 params[1] = HIDPP_FF_EFFECT_RAMP;
1761                 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1762                 params[6] = force >> 8;
1763                 params[7] = force & 255;
1764                 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1765                 params[8] = force >> 8;
1766                 params[9] = force & 255;
1767                 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1768                 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1769                 params[12] = effect->u.ramp.envelope.attack_length & 255;
1770                 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1771                 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1772                 params[15] = effect->u.ramp.envelope.fade_length & 255;
1773                 size = 16;
1774                 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1775                                 effect->u.ramp.start_level,
1776                                 effect->u.ramp.end_level,
1777                                 effect->direction, force);
1778                 dbg_hid("          envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1779                                 effect->u.ramp.envelope.attack_level,
1780                                 effect->u.ramp.envelope.attack_length,
1781                                 effect->u.ramp.envelope.fade_level,
1782                                 effect->u.ramp.envelope.fade_length);
1783                 break;
1784         case FF_FRICTION:
1785         case FF_INERTIA:
1786         case FF_SPRING:
1787         case FF_DAMPER:
1788                 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1789                 params[6] = effect->u.condition[0].left_saturation >> 9;
1790                 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1791                 params[8] = effect->u.condition[0].left_coeff >> 8;
1792                 params[9] = effect->u.condition[0].left_coeff & 255;
1793                 params[10] = effect->u.condition[0].deadband >> 9;
1794                 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1795                 params[12] = effect->u.condition[0].center >> 8;
1796                 params[13] = effect->u.condition[0].center & 255;
1797                 params[14] = effect->u.condition[0].right_coeff >> 8;
1798                 params[15] = effect->u.condition[0].right_coeff & 255;
1799                 params[16] = effect->u.condition[0].right_saturation >> 9;
1800                 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1801                 size = 18;
1802                 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1803                                 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1804                                 effect->u.condition[0].left_coeff,
1805                                 effect->u.condition[0].left_saturation,
1806                                 effect->u.condition[0].right_coeff,
1807                                 effect->u.condition[0].right_saturation);
1808                 dbg_hid("          deadband=%d, center=%d\n",
1809                                 effect->u.condition[0].deadband,
1810                                 effect->u.condition[0].center);
1811                 break;
1812         default:
1813                 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1814                 return -EINVAL;
1815         }
1816
1817         return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1818 }
1819
1820 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1821 {
1822         struct hidpp_ff_private_data *data = dev->ff->private;
1823         u8 params[2];
1824
1825         params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1826
1827         dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1828
1829         return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1830 }
1831
1832 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1833 {
1834         struct hidpp_ff_private_data *data = dev->ff->private;
1835         u8 slot = 0;
1836
1837         dbg_hid("Erasing effect %d.\n", effect_id);
1838
1839         return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1840 }
1841
1842 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1843 {
1844         struct hidpp_ff_private_data *data = dev->ff->private;
1845         u8 params[18];
1846
1847         dbg_hid("Setting autocenter to %d.\n", magnitude);
1848
1849         /* start a standard spring effect */
1850         params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1851         /* zero delay and duration */
1852         params[2] = params[3] = params[4] = params[5] = 0;
1853         /* set coeff to 25% of saturation */
1854         params[8] = params[14] = magnitude >> 11;
1855         params[9] = params[15] = (magnitude >> 3) & 255;
1856         params[6] = params[16] = magnitude >> 9;
1857         params[7] = params[17] = (magnitude >> 1) & 255;
1858         /* zero deadband and center */
1859         params[10] = params[11] = params[12] = params[13] = 0;
1860
1861         hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1862 }
1863
1864 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1865 {
1866         struct hidpp_ff_private_data *data = dev->ff->private;
1867         u8 params[4];
1868
1869         dbg_hid("Setting gain to %d.\n", gain);
1870
1871         params[0] = gain >> 8;
1872         params[1] = gain & 255;
1873         params[2] = 0; /* no boost */
1874         params[3] = 0;
1875
1876         hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1877 }
1878
1879 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1880 {
1881         struct hid_device *hid = to_hid_device(dev);
1882         struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1883         struct input_dev *idev = hidinput->input;
1884         struct hidpp_ff_private_data *data = idev->ff->private;
1885
1886         return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1887 }
1888
1889 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1890 {
1891         struct hid_device *hid = to_hid_device(dev);
1892         struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1893         struct input_dev *idev = hidinput->input;
1894         struct hidpp_ff_private_data *data = idev->ff->private;
1895         u8 params[2];
1896         int range = simple_strtoul(buf, NULL, 10);
1897
1898         range = clamp(range, 180, 900);
1899
1900         params[0] = range >> 8;
1901         params[1] = range & 0x00FF;
1902
1903         hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1904
1905         return count;
1906 }
1907
1908 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1909
1910 static void hidpp_ff_destroy(struct ff_device *ff)
1911 {
1912         struct hidpp_ff_private_data *data = ff->private;
1913
1914         kfree(data->effect_ids);
1915 }
1916
1917 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
1918 {
1919         struct hid_device *hid = hidpp->hid_dev;
1920         struct hid_input *hidinput;
1921         struct input_dev *dev;
1922         const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1923         const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1924         struct ff_device *ff;
1925         struct hidpp_report response;
1926         struct hidpp_ff_private_data *data;
1927         int error, j, num_slots;
1928         u8 version;
1929
1930         if (list_empty(&hid->inputs)) {
1931                 hid_err(hid, "no inputs found\n");
1932                 return -ENODEV;
1933         }
1934         hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1935         dev = hidinput->input;
1936
1937         if (!dev) {
1938                 hid_err(hid, "Struct input_dev not set!\n");
1939                 return -EINVAL;
1940         }
1941
1942         /* Get firmware release */
1943         version = bcdDevice & 255;
1944
1945         /* Set supported force feedback capabilities */
1946         for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1947                 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1948         if (version > 1)
1949                 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1950                         set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1951
1952         /* Read number of slots available in device */
1953         error = hidpp_send_fap_command_sync(hidpp, feature_index,
1954                 HIDPP_FF_GET_INFO, NULL, 0, &response);
1955         if (error) {
1956                 if (error < 0)
1957                         return error;
1958                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1959                         __func__, error);
1960                 return -EPROTO;
1961         }
1962
1963         num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1964
1965         error = input_ff_create(dev, num_slots);
1966
1967         if (error) {
1968                 hid_err(dev, "Failed to create FF device!\n");
1969                 return error;
1970         }
1971
1972         data = kzalloc(sizeof(*data), GFP_KERNEL);
1973         if (!data)
1974                 return -ENOMEM;
1975         data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1976         if (!data->effect_ids) {
1977                 kfree(data);
1978                 return -ENOMEM;
1979         }
1980         data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1981         if (!data->wq) {
1982                 kfree(data->effect_ids);
1983                 kfree(data);
1984                 return -ENOMEM;
1985         }
1986
1987         data->hidpp = hidpp;
1988         data->feature_index = feature_index;
1989         data->version = version;
1990         data->slot_autocenter = 0;
1991         data->num_effects = num_slots;
1992         for (j = 0; j < num_slots; j++)
1993                 data->effect_ids[j] = -1;
1994
1995         ff = dev->ff;
1996         ff->private = data;
1997
1998         ff->upload = hidpp_ff_upload_effect;
1999         ff->erase = hidpp_ff_erase_effect;
2000         ff->playback = hidpp_ff_playback;
2001         ff->set_gain = hidpp_ff_set_gain;
2002         ff->set_autocenter = hidpp_ff_set_autocenter;
2003         ff->destroy = hidpp_ff_destroy;
2004
2005
2006         /* reset all forces */
2007         error = hidpp_send_fap_command_sync(hidpp, feature_index,
2008                 HIDPP_FF_RESET_ALL, NULL, 0, &response);
2009
2010         /* Read current Range */
2011         error = hidpp_send_fap_command_sync(hidpp, feature_index,
2012                 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
2013         if (error)
2014                 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
2015         data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
2016
2017         /* Create sysfs interface */
2018         error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2019         if (error)
2020                 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2021
2022         /* Read the current gain values */
2023         error = hidpp_send_fap_command_sync(hidpp, feature_index,
2024                 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
2025         if (error)
2026                 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
2027         data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
2028         /* ignore boost value at response.fap.params[2] */
2029
2030         /* init the hardware command queue */
2031         atomic_set(&data->workqueue_size, 0);
2032
2033         /* initialize with zero autocenter to get wheel in usable state */
2034         hidpp_ff_set_autocenter(dev, 0);
2035
2036         hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2037                  version);
2038
2039         return 0;
2040 }
2041
2042 static int hidpp_ff_deinit(struct hid_device *hid)
2043 {
2044         struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2045         struct input_dev *dev = hidinput->input;
2046         struct hidpp_ff_private_data *data;
2047
2048         if (!dev) {
2049                 hid_err(hid, "Struct input_dev not found!\n");
2050                 return -EINVAL;
2051         }
2052
2053         hid_info(hid, "Unloading HID++ force feedback.\n");
2054         data = dev->ff->private;
2055         if (!data) {
2056                 hid_err(hid, "Private data not found!\n");
2057                 return -EINVAL;
2058         }
2059
2060         destroy_workqueue(data->wq);
2061         device_remove_file(&hid->dev, &dev_attr_range);
2062
2063         return 0;
2064 }
2065
2066
2067 /* ************************************************************************** */
2068 /*                                                                            */
2069 /* Device Support                                                             */
2070 /*                                                                            */
2071 /* ************************************************************************** */
2072
2073 /* -------------------------------------------------------------------------- */
2074 /* Touchpad HID++ devices                                                     */
2075 /* -------------------------------------------------------------------------- */
2076
2077 #define WTP_MANUAL_RESOLUTION                           39
2078
2079 struct wtp_data {
2080         struct input_dev *input;
2081         u16 x_size, y_size;
2082         u8 finger_count;
2083         u8 mt_feature_index;
2084         u8 button_feature_index;
2085         u8 maxcontacts;
2086         bool flip_y;
2087         unsigned int resolution;
2088 };
2089
2090 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2091                 struct hid_field *field, struct hid_usage *usage,
2092                 unsigned long **bit, int *max)
2093 {
2094         return -1;
2095 }
2096
2097 static void wtp_populate_input(struct hidpp_device *hidpp,
2098                 struct input_dev *input_dev, bool origin_is_hid_core)
2099 {
2100         struct wtp_data *wd = hidpp->private_data;
2101
2102         __set_bit(EV_ABS, input_dev->evbit);
2103         __set_bit(EV_KEY, input_dev->evbit);
2104         __clear_bit(EV_REL, input_dev->evbit);
2105         __clear_bit(EV_LED, input_dev->evbit);
2106
2107         input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2108         input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2109         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2110         input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2111
2112         /* Max pressure is not given by the devices, pick one */
2113         input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2114
2115         input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2116
2117         if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2118                 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2119         else
2120                 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2121
2122         input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2123                 INPUT_MT_DROP_UNUSED);
2124
2125         wd->input = input_dev;
2126 }
2127
2128 static void wtp_touch_event(struct wtp_data *wd,
2129         struct hidpp_touchpad_raw_xy_finger *touch_report)
2130 {
2131         int slot;
2132
2133         if (!touch_report->finger_id || touch_report->contact_type)
2134                 /* no actual data */
2135                 return;
2136
2137         slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2138
2139         input_mt_slot(wd->input, slot);
2140         input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2141                                         touch_report->contact_status);
2142         if (touch_report->contact_status) {
2143                 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2144                                 touch_report->x);
2145                 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2146                                 wd->flip_y ? wd->y_size - touch_report->y :
2147                                              touch_report->y);
2148                 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2149                                 touch_report->area);
2150         }
2151 }
2152
2153 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2154                 struct hidpp_touchpad_raw_xy *raw)
2155 {
2156         struct wtp_data *wd = hidpp->private_data;
2157         int i;
2158
2159         for (i = 0; i < 2; i++)
2160                 wtp_touch_event(wd, &(raw->fingers[i]));
2161
2162         if (raw->end_of_frame &&
2163             !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2164                 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2165
2166         if (raw->end_of_frame || raw->finger_count <= 2) {
2167                 input_mt_sync_frame(wd->input);
2168                 input_sync(wd->input);
2169         }
2170 }
2171
2172 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2173 {
2174         struct wtp_data *wd = hidpp->private_data;
2175         u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2176                       (data[7] >> 4) * (data[7] >> 4)) / 2;
2177         u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2178                       (data[13] >> 4) * (data[13] >> 4)) / 2;
2179         struct hidpp_touchpad_raw_xy raw = {
2180                 .timestamp = data[1],
2181                 .fingers = {
2182                         {
2183                                 .contact_type = 0,
2184                                 .contact_status = !!data[7],
2185                                 .x = get_unaligned_le16(&data[3]),
2186                                 .y = get_unaligned_le16(&data[5]),
2187                                 .z = c1_area,
2188                                 .area = c1_area,
2189                                 .finger_id = data[2],
2190                         }, {
2191                                 .contact_type = 0,
2192                                 .contact_status = !!data[13],
2193                                 .x = get_unaligned_le16(&data[9]),
2194                                 .y = get_unaligned_le16(&data[11]),
2195                                 .z = c2_area,
2196                                 .area = c2_area,
2197                                 .finger_id = data[8],
2198                         }
2199                 },
2200                 .finger_count = wd->maxcontacts,
2201                 .spurious_flag = 0,
2202                 .end_of_frame = (data[0] >> 7) == 0,
2203                 .button = data[0] & 0x01,
2204         };
2205
2206         wtp_send_raw_xy_event(hidpp, &raw);
2207
2208         return 1;
2209 }
2210
2211 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2212 {
2213         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2214         struct wtp_data *wd = hidpp->private_data;
2215         struct hidpp_report *report = (struct hidpp_report *)data;
2216         struct hidpp_touchpad_raw_xy raw;
2217
2218         if (!wd || !wd->input)
2219                 return 1;
2220
2221         switch (data[0]) {
2222         case 0x02:
2223                 if (size < 2) {
2224                         hid_err(hdev, "Received HID report of bad size (%d)",
2225                                 size);
2226                         return 1;
2227                 }
2228                 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2229                         input_event(wd->input, EV_KEY, BTN_LEFT,
2230                                         !!(data[1] & 0x01));
2231                         input_event(wd->input, EV_KEY, BTN_RIGHT,
2232                                         !!(data[1] & 0x02));
2233                         input_sync(wd->input);
2234                         return 0;
2235                 } else {
2236                         if (size < 21)
2237                                 return 1;
2238                         return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2239                 }
2240         case REPORT_ID_HIDPP_LONG:
2241                 /* size is already checked in hidpp_raw_event. */
2242                 if ((report->fap.feature_index != wd->mt_feature_index) ||
2243                     (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2244                         return 1;
2245                 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2246
2247                 wtp_send_raw_xy_event(hidpp, &raw);
2248                 return 0;
2249         }
2250
2251         return 0;
2252 }
2253
2254 static int wtp_get_config(struct hidpp_device *hidpp)
2255 {
2256         struct wtp_data *wd = hidpp->private_data;
2257         struct hidpp_touchpad_raw_info raw_info = {0};
2258         u8 feature_type;
2259         int ret;
2260
2261         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2262                 &wd->mt_feature_index, &feature_type);
2263         if (ret)
2264                 /* means that the device is not powered up */
2265                 return ret;
2266
2267         ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2268                 &raw_info);
2269         if (ret)
2270                 return ret;
2271
2272         wd->x_size = raw_info.x_size;
2273         wd->y_size = raw_info.y_size;
2274         wd->maxcontacts = raw_info.maxcontacts;
2275         wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2276         wd->resolution = raw_info.res;
2277         if (!wd->resolution)
2278                 wd->resolution = WTP_MANUAL_RESOLUTION;
2279
2280         return 0;
2281 }
2282
2283 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2284 {
2285         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2286         struct wtp_data *wd;
2287
2288         wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2289                         GFP_KERNEL);
2290         if (!wd)
2291                 return -ENOMEM;
2292
2293         hidpp->private_data = wd;
2294
2295         return 0;
2296 };
2297
2298 static int wtp_connect(struct hid_device *hdev, bool connected)
2299 {
2300         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2301         struct wtp_data *wd = hidpp->private_data;
2302         int ret;
2303
2304         if (!wd->x_size) {
2305                 ret = wtp_get_config(hidpp);
2306                 if (ret) {
2307                         hid_err(hdev, "Can not get wtp config: %d\n", ret);
2308                         return ret;
2309                 }
2310         }
2311
2312         return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2313                         true, true);
2314 }
2315
2316 /* ------------------------------------------------------------------------- */
2317 /* Logitech M560 devices                                                     */
2318 /* ------------------------------------------------------------------------- */
2319
2320 /*
2321  * Logitech M560 protocol overview
2322  *
2323  * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2324  * the sides buttons are pressed, it sends some keyboard keys events
2325  * instead of buttons ones.
2326  * To complicate things further, the middle button keys sequence
2327  * is different from the odd press and the even press.
2328  *
2329  * forward button -> Super_R
2330  * backward button -> Super_L+'d' (press only)
2331  * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2332  *                  2nd time: left-click (press only)
2333  * NB: press-only means that when the button is pressed, the
2334  * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2335  * together sequentially; instead when the button is released, no event is
2336  * generated !
2337  *
2338  * With the command
2339  *      10<xx>0a 3500af03 (where <xx> is the mouse id),
2340  * the mouse reacts differently:
2341  * - it never sends a keyboard key event
2342  * - for the three mouse button it sends:
2343  *      middle button               press   11<xx>0a 3500af00...
2344  *      side 1 button (forward)     press   11<xx>0a 3500b000...
2345  *      side 2 button (backward)    press   11<xx>0a 3500ae00...
2346  *      middle/side1/side2 button   release 11<xx>0a 35000000...
2347  */
2348
2349 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2350
2351 struct m560_private_data {
2352         struct input_dev *input;
2353 };
2354
2355 /* how buttons are mapped in the report */
2356 #define M560_MOUSE_BTN_LEFT             0x01
2357 #define M560_MOUSE_BTN_RIGHT            0x02
2358 #define M560_MOUSE_BTN_WHEEL_LEFT       0x08
2359 #define M560_MOUSE_BTN_WHEEL_RIGHT      0x10
2360
2361 #define M560_SUB_ID                     0x0a
2362 #define M560_BUTTON_MODE_REGISTER       0x35
2363
2364 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2365 {
2366         struct hidpp_report response;
2367         struct hidpp_device *hidpp_dev;
2368
2369         hidpp_dev = hid_get_drvdata(hdev);
2370
2371         return hidpp_send_rap_command_sync(
2372                 hidpp_dev,
2373                 REPORT_ID_HIDPP_SHORT,
2374                 M560_SUB_ID,
2375                 M560_BUTTON_MODE_REGISTER,
2376                 (u8 *)m560_config_parameter,
2377                 sizeof(m560_config_parameter),
2378                 &response
2379         );
2380 }
2381
2382 static int m560_allocate(struct hid_device *hdev)
2383 {
2384         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2385         struct m560_private_data *d;
2386
2387         d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2388                         GFP_KERNEL);
2389         if (!d)
2390                 return -ENOMEM;
2391
2392         hidpp->private_data = d;
2393
2394         return 0;
2395 };
2396
2397 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2398 {
2399         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2400         struct m560_private_data *mydata = hidpp->private_data;
2401
2402         /* sanity check */
2403         if (!mydata || !mydata->input) {
2404                 hid_err(hdev, "error in parameter\n");
2405                 return -EINVAL;
2406         }
2407
2408         if (size < 7) {
2409                 hid_err(hdev, "error in report\n");
2410                 return 0;
2411         }
2412
2413         if (data[0] == REPORT_ID_HIDPP_LONG &&
2414             data[2] == M560_SUB_ID && data[6] == 0x00) {
2415                 /*
2416                  * m560 mouse report for middle, forward and backward button
2417                  *
2418                  * data[0] = 0x11
2419                  * data[1] = device-id
2420                  * data[2] = 0x0a
2421                  * data[5] = 0xaf -> middle
2422                  *           0xb0 -> forward
2423                  *           0xae -> backward
2424                  *           0x00 -> release all
2425                  * data[6] = 0x00
2426                  */
2427
2428                 switch (data[5]) {
2429                 case 0xaf:
2430                         input_report_key(mydata->input, BTN_MIDDLE, 1);
2431                         break;
2432                 case 0xb0:
2433                         input_report_key(mydata->input, BTN_FORWARD, 1);
2434                         break;
2435                 case 0xae:
2436                         input_report_key(mydata->input, BTN_BACK, 1);
2437                         break;
2438                 case 0x00:
2439                         input_report_key(mydata->input, BTN_BACK, 0);
2440                         input_report_key(mydata->input, BTN_FORWARD, 0);
2441                         input_report_key(mydata->input, BTN_MIDDLE, 0);
2442                         break;
2443                 default:
2444                         hid_err(hdev, "error in report\n");
2445                         return 0;
2446                 }
2447                 input_sync(mydata->input);
2448
2449         } else if (data[0] == 0x02) {
2450                 /*
2451                  * Logitech M560 mouse report
2452                  *
2453                  * data[0] = type (0x02)
2454                  * data[1..2] = buttons
2455                  * data[3..5] = xy
2456                  * data[6] = wheel
2457                  */
2458
2459                 int v;
2460
2461                 input_report_key(mydata->input, BTN_LEFT,
2462                         !!(data[1] & M560_MOUSE_BTN_LEFT));
2463                 input_report_key(mydata->input, BTN_RIGHT,
2464                         !!(data[1] & M560_MOUSE_BTN_RIGHT));
2465
2466                 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2467                         input_report_rel(mydata->input, REL_HWHEEL, -1);
2468                 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2469                         input_report_rel(mydata->input, REL_HWHEEL, 1);
2470
2471                 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2472                 input_report_rel(mydata->input, REL_X, v);
2473
2474                 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2475                 input_report_rel(mydata->input, REL_Y, v);
2476
2477                 v = hid_snto32(data[6], 8);
2478                 input_report_rel(mydata->input, REL_WHEEL, v);
2479
2480                 input_sync(mydata->input);
2481         }
2482
2483         return 1;
2484 }
2485
2486 static void m560_populate_input(struct hidpp_device *hidpp,
2487                 struct input_dev *input_dev, bool origin_is_hid_core)
2488 {
2489         struct m560_private_data *mydata = hidpp->private_data;
2490
2491         mydata->input = input_dev;
2492
2493         __set_bit(EV_KEY, mydata->input->evbit);
2494         __set_bit(BTN_MIDDLE, mydata->input->keybit);
2495         __set_bit(BTN_RIGHT, mydata->input->keybit);
2496         __set_bit(BTN_LEFT, mydata->input->keybit);
2497         __set_bit(BTN_BACK, mydata->input->keybit);
2498         __set_bit(BTN_FORWARD, mydata->input->keybit);
2499
2500         __set_bit(EV_REL, mydata->input->evbit);
2501         __set_bit(REL_X, mydata->input->relbit);
2502         __set_bit(REL_Y, mydata->input->relbit);
2503         __set_bit(REL_WHEEL, mydata->input->relbit);
2504         __set_bit(REL_HWHEEL, mydata->input->relbit);
2505 }
2506
2507 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2508                 struct hid_field *field, struct hid_usage *usage,
2509                 unsigned long **bit, int *max)
2510 {
2511         return -1;
2512 }
2513
2514 /* ------------------------------------------------------------------------- */
2515 /* Logitech K400 devices                                                     */
2516 /* ------------------------------------------------------------------------- */
2517
2518 /*
2519  * The Logitech K400 keyboard has an embedded touchpad which is seen
2520  * as a mouse from the OS point of view. There is a hardware shortcut to disable
2521  * tap-to-click but the setting is not remembered accross reset, annoying some
2522  * users.
2523  *
2524  * We can toggle this feature from the host by using the feature 0x6010:
2525  * Touchpad FW items
2526  */
2527
2528 struct k400_private_data {
2529         u8 feature_index;
2530 };
2531
2532 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2533 {
2534         struct k400_private_data *k400 = hidpp->private_data;
2535         struct hidpp_touchpad_fw_items items = {};
2536         int ret;
2537         u8 feature_type;
2538
2539         if (!k400->feature_index) {
2540                 ret = hidpp_root_get_feature(hidpp,
2541                         HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2542                         &k400->feature_index, &feature_type);
2543                 if (ret)
2544                         /* means that the device is not powered up */
2545                         return ret;
2546         }
2547
2548         ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2549         if (ret)
2550                 return ret;
2551
2552         return 0;
2553 }
2554
2555 static int k400_allocate(struct hid_device *hdev)
2556 {
2557         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2558         struct k400_private_data *k400;
2559
2560         k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2561                             GFP_KERNEL);
2562         if (!k400)
2563                 return -ENOMEM;
2564
2565         hidpp->private_data = k400;
2566
2567         return 0;
2568 };
2569
2570 static int k400_connect(struct hid_device *hdev, bool connected)
2571 {
2572         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2573
2574         if (!disable_tap_to_click)
2575                 return 0;
2576
2577         return k400_disable_tap_to_click(hidpp);
2578 }
2579
2580 /* ------------------------------------------------------------------------- */
2581 /* Logitech G920 Driving Force Racing Wheel for Xbox One                     */
2582 /* ------------------------------------------------------------------------- */
2583
2584 #define HIDPP_PAGE_G920_FORCE_FEEDBACK                  0x8123
2585
2586 static int g920_get_config(struct hidpp_device *hidpp)
2587 {
2588         u8 feature_type;
2589         u8 feature_index;
2590         int ret;
2591
2592         /* Find feature and store for later use */
2593         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2594                 &feature_index, &feature_type);
2595         if (ret)
2596                 return ret;
2597
2598         ret = hidpp_ff_init(hidpp, feature_index);
2599         if (ret)
2600                 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2601                                 ret);
2602
2603         return 0;
2604 }
2605
2606 /* -------------------------------------------------------------------------- */
2607 /* Generic HID++ devices                                                      */
2608 /* -------------------------------------------------------------------------- */
2609
2610 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2611                 struct hid_field *field, struct hid_usage *usage,
2612                 unsigned long **bit, int *max)
2613 {
2614         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2615
2616         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2617                 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2618         else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2619                         field->application != HID_GD_MOUSE)
2620                 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2621
2622         return 0;
2623 }
2624
2625 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2626                 struct hid_field *field, struct hid_usage *usage,
2627                 unsigned long **bit, int *max)
2628 {
2629         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2630
2631         /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2632         if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2633                 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2634                                 usage->code == ABS_Y || usage->code == ABS_Z ||
2635                                 usage->code == ABS_RZ)) {
2636                         field->application = HID_GD_MULTIAXIS;
2637                 }
2638         }
2639
2640         return 0;
2641 }
2642
2643
2644 static void hidpp_populate_input(struct hidpp_device *hidpp,
2645                 struct input_dev *input, bool origin_is_hid_core)
2646 {
2647         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2648                 wtp_populate_input(hidpp, input, origin_is_hid_core);
2649         else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2650                 m560_populate_input(hidpp, input, origin_is_hid_core);
2651 }
2652
2653 static int hidpp_input_configured(struct hid_device *hdev,
2654                                 struct hid_input *hidinput)
2655 {
2656         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2657         struct input_dev *input = hidinput->input;
2658
2659         hidpp_populate_input(hidpp, input, true);
2660
2661         return 0;
2662 }
2663
2664 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2665                 int size)
2666 {
2667         struct hidpp_report *question = hidpp->send_receive_buf;
2668         struct hidpp_report *answer = hidpp->send_receive_buf;
2669         struct hidpp_report *report = (struct hidpp_report *)data;
2670         int ret;
2671
2672         /*
2673          * If the mutex is locked then we have a pending answer from a
2674          * previously sent command.
2675          */
2676         if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2677                 /*
2678                  * Check for a correct hidpp20 answer or the corresponding
2679                  * error
2680                  */
2681                 if (hidpp_match_answer(question, report) ||
2682                                 hidpp_match_error(question, report)) {
2683                         *answer = *report;
2684                         hidpp->answer_available = true;
2685                         wake_up(&hidpp->wait);
2686                         /*
2687                          * This was an answer to a command that this driver sent
2688                          * We return 1 to hid-core to avoid forwarding the
2689                          * command upstream as it has been treated by the driver
2690                          */
2691
2692                         return 1;
2693                 }
2694         }
2695
2696         if (unlikely(hidpp_report_is_connect_event(report))) {
2697                 atomic_set(&hidpp->connected,
2698                                 !(report->rap.params[0] & (1 << 6)));
2699                 if (schedule_work(&hidpp->work) == 0)
2700                         dbg_hid("%s: connect event already queued\n", __func__);
2701                 return 1;
2702         }
2703
2704         if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2705                 ret = hidpp20_battery_event(hidpp, data, size);
2706                 if (ret != 0)
2707                         return ret;
2708                 ret = hidpp_solar_battery_event(hidpp, data, size);
2709                 if (ret != 0)
2710                         return ret;
2711         }
2712
2713         if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2714                 ret = hidpp10_battery_event(hidpp, data, size);
2715                 if (ret != 0)
2716                         return ret;
2717         }
2718
2719         return 0;
2720 }
2721
2722 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2723                 u8 *data, int size)
2724 {
2725         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2726         int ret = 0;
2727
2728         /* Generic HID++ processing. */
2729         switch (data[0]) {
2730         case REPORT_ID_HIDPP_VERY_LONG:
2731                 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2732                         hid_err(hdev, "received hid++ report of bad size (%d)",
2733                                 size);
2734                         return 1;
2735                 }
2736                 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2737                 break;
2738         case REPORT_ID_HIDPP_LONG:
2739                 if (size != HIDPP_REPORT_LONG_LENGTH) {
2740                         hid_err(hdev, "received hid++ report of bad size (%d)",
2741                                 size);
2742                         return 1;
2743                 }
2744                 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2745                 break;
2746         case REPORT_ID_HIDPP_SHORT:
2747                 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2748                         hid_err(hdev, "received hid++ report of bad size (%d)",
2749                                 size);
2750                         return 1;
2751                 }
2752                 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2753                 break;
2754         }
2755
2756         /* If no report is available for further processing, skip calling
2757          * raw_event of subclasses. */
2758         if (ret != 0)
2759                 return ret;
2760
2761         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2762                 return wtp_raw_event(hdev, data, size);
2763         else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2764                 return m560_raw_event(hdev, data, size);
2765
2766         return 0;
2767 }
2768
2769 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2770 {
2771         static atomic_t battery_no = ATOMIC_INIT(0);
2772         struct power_supply_config cfg = { .drv_data = hidpp };
2773         struct power_supply_desc *desc = &hidpp->battery.desc;
2774         enum power_supply_property *battery_props;
2775         struct hidpp_battery *battery;
2776         unsigned int num_battery_props;
2777         unsigned long n;
2778         int ret;
2779
2780         if (hidpp->battery.ps)
2781                 return 0;
2782
2783         hidpp->battery.feature_index = 0xff;
2784         hidpp->battery.solar_feature_index = 0xff;
2785
2786         if (hidpp->protocol_major >= 2) {
2787                 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
2788                         ret = hidpp_solar_request_battery_event(hidpp);
2789                 else
2790                         ret = hidpp20_query_battery_info(hidpp);
2791
2792                 if (ret)
2793                         return ret;
2794                 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2795         } else {
2796                 ret = hidpp10_query_battery_status(hidpp);
2797                 if (ret) {
2798                         ret = hidpp10_query_battery_mileage(hidpp);
2799                         if (ret)
2800                                 return -ENOENT;
2801                         hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
2802                 } else {
2803                         hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
2804                 }
2805                 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
2806         }
2807
2808         battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
2809                                      hidpp_battery_props,
2810                                      sizeof(hidpp_battery_props),
2811                                      GFP_KERNEL);
2812         if (!battery_props)
2813                 return -ENOMEM;
2814
2815         num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
2816
2817         if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2818                 battery_props[num_battery_props++] =
2819                                 POWER_SUPPLY_PROP_CAPACITY;
2820
2821         if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
2822                 battery_props[num_battery_props++] =
2823                                 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
2824
2825         battery = &hidpp->battery;
2826
2827         n = atomic_inc_return(&battery_no) - 1;
2828         desc->properties = battery_props;
2829         desc->num_properties = num_battery_props;
2830         desc->get_property = hidpp_battery_get_property;
2831         sprintf(battery->name, "hidpp_battery_%ld", n);
2832         desc->name = battery->name;
2833         desc->type = POWER_SUPPLY_TYPE_BATTERY;
2834         desc->use_for_apm = 0;
2835
2836         battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2837                                                  &battery->desc,
2838                                                  &cfg);
2839         if (IS_ERR(battery->ps))
2840                 return PTR_ERR(battery->ps);
2841
2842         power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2843
2844         return ret;
2845 }
2846
2847 static void hidpp_overwrite_name(struct hid_device *hdev)
2848 {
2849         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2850         char *name;
2851
2852         if (hidpp->protocol_major < 2)
2853                 return;
2854
2855         name = hidpp_get_device_name(hidpp);
2856
2857         if (!name) {
2858                 hid_err(hdev, "unable to retrieve the name of the device");
2859         } else {
2860                 dbg_hid("HID++: Got name: %s\n", name);
2861                 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
2862         }
2863
2864         kfree(name);
2865 }
2866
2867 static int hidpp_input_open(struct input_dev *dev)
2868 {
2869         struct hid_device *hid = input_get_drvdata(dev);
2870
2871         return hid_hw_open(hid);
2872 }
2873
2874 static void hidpp_input_close(struct input_dev *dev)
2875 {
2876         struct hid_device *hid = input_get_drvdata(dev);
2877
2878         hid_hw_close(hid);
2879 }
2880
2881 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2882 {
2883         struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
2884         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2885
2886         if (!input_dev)
2887                 return NULL;
2888
2889         input_set_drvdata(input_dev, hdev);
2890         input_dev->open = hidpp_input_open;
2891         input_dev->close = hidpp_input_close;
2892
2893         input_dev->name = hidpp->name;
2894         input_dev->phys = hdev->phys;
2895         input_dev->uniq = hdev->uniq;
2896         input_dev->id.bustype = hdev->bus;
2897         input_dev->id.vendor  = hdev->vendor;
2898         input_dev->id.product = hdev->product;
2899         input_dev->id.version = hdev->version;
2900         input_dev->dev.parent = &hdev->dev;
2901
2902         return input_dev;
2903 }
2904
2905 static void hidpp_connect_event(struct hidpp_device *hidpp)
2906 {
2907         struct hid_device *hdev = hidpp->hid_dev;
2908         int ret = 0;
2909         bool connected = atomic_read(&hidpp->connected);
2910         struct input_dev *input;
2911         char *name, *devm_name;
2912
2913         if (!connected) {
2914                 if (hidpp->battery.ps) {
2915                         hidpp->battery.online = false;
2916                         hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
2917                         hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2918                         power_supply_changed(hidpp->battery.ps);
2919                 }
2920                 return;
2921         }
2922
2923         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2924                 ret = wtp_connect(hdev, connected);
2925                 if (ret)
2926                         return;
2927         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2928                 ret = m560_send_config_command(hdev, connected);
2929                 if (ret)
2930                         return;
2931         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2932                 ret = k400_connect(hdev, connected);
2933                 if (ret)
2934                         return;
2935         }
2936
2937         /* the device is already connected, we can ask for its name and
2938          * protocol */
2939         if (!hidpp->protocol_major) {
2940                 ret = !hidpp_is_connected(hidpp);
2941                 if (ret) {
2942                         hid_err(hdev, "Can not get the protocol version.\n");
2943                         return;
2944                 }
2945                 hid_info(hdev, "HID++ %u.%u device connected.\n",
2946                          hidpp->protocol_major, hidpp->protocol_minor);
2947         }
2948
2949         if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
2950                 name = hidpp_get_device_name(hidpp);
2951                 if (!name) {
2952                         hid_err(hdev,
2953                                 "unable to retrieve the name of the device");
2954                         return;
2955                 }
2956
2957                 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2958                 kfree(name);
2959                 if (!devm_name)
2960                         return;
2961
2962                 hidpp->name = devm_name;
2963         }
2964
2965         hidpp_initialize_battery(hidpp);
2966
2967         /* forward current battery state */
2968         if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
2969                 hidpp10_enable_battery_reporting(hidpp);
2970                 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
2971                         hidpp10_query_battery_mileage(hidpp);
2972                 else
2973                         hidpp10_query_battery_status(hidpp);
2974         } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2975                 hidpp20_query_battery_info(hidpp);
2976         }
2977         if (hidpp->battery.ps)
2978                 power_supply_changed(hidpp->battery.ps);
2979
2980         if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
2981                 /* if the input nodes are already created, we can stop now */
2982                 return;
2983
2984         input = hidpp_allocate_input(hdev);
2985         if (!input) {
2986                 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2987                 return;
2988         }
2989
2990         hidpp_populate_input(hidpp, input, false);
2991
2992         ret = input_register_device(input);
2993         if (ret)
2994                 input_free_device(input);
2995
2996         hidpp->delayed_input = input;
2997 }
2998
2999 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3000
3001 static struct attribute *sysfs_attrs[] = {
3002         &dev_attr_builtin_power_supply.attr,
3003         NULL
3004 };
3005
3006 static const struct attribute_group ps_attribute_group = {
3007         .attrs = sysfs_attrs
3008 };
3009
3010 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3011 {
3012         struct hidpp_device *hidpp;
3013         int ret;
3014         bool connected;
3015         unsigned int connect_mask = HID_CONNECT_DEFAULT;
3016
3017         hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
3018                         GFP_KERNEL);
3019         if (!hidpp)
3020                 return -ENOMEM;
3021
3022         hidpp->hid_dev = hdev;
3023         hidpp->name = hdev->name;
3024         hid_set_drvdata(hdev, hidpp);
3025
3026         hidpp->quirks = id->driver_data;
3027
3028         if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3029                 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3030
3031         if (disable_raw_mode) {
3032                 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
3033                 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
3034         }
3035
3036         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3037                 ret = wtp_allocate(hdev, id);
3038                 if (ret)
3039                         goto allocate_fail;
3040         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3041                 ret = m560_allocate(hdev);
3042                 if (ret)
3043                         goto allocate_fail;
3044         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3045                 ret = k400_allocate(hdev);
3046                 if (ret)
3047                         goto allocate_fail;
3048         }
3049
3050         INIT_WORK(&hidpp->work, delayed_work_cb);
3051         mutex_init(&hidpp->send_mutex);
3052         init_waitqueue_head(&hidpp->wait);
3053
3054         /* indicates we are handling the battery properties in the kernel */
3055         ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3056         if (ret)
3057                 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3058                          hdev->name);
3059
3060         ret = hid_parse(hdev);
3061         if (ret) {
3062                 hid_err(hdev, "%s:parse failed\n", __func__);
3063                 goto hid_parse_fail;
3064         }
3065
3066         if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3067                 connect_mask &= ~HID_CONNECT_HIDINPUT;
3068
3069         if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3070                 ret = hid_hw_start(hdev, connect_mask);
3071                 if (ret) {
3072                         hid_err(hdev, "hw start failed\n");
3073                         goto hid_hw_start_fail;
3074                 }
3075                 ret = hid_hw_open(hdev);
3076                 if (ret < 0) {
3077                         dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3078                                 __func__, ret);
3079                         hid_hw_stop(hdev);
3080                         goto hid_hw_start_fail;
3081                 }
3082         }
3083
3084
3085         /* Allow incoming packets */
3086         hid_device_io_start(hdev);
3087
3088         if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3089                 hidpp_unifying_init(hidpp);
3090         else if (hid_is_usb(hidpp->hid_dev))
3091                 hidpp_serial_init(hidpp);
3092
3093         connected = hidpp_is_connected(hidpp);
3094         atomic_set(&hidpp->connected, connected);
3095         if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
3096                 if (!connected) {
3097                         ret = -ENODEV;
3098                         hid_err(hdev, "Device not connected");
3099                         goto hid_hw_open_failed;
3100                 }
3101
3102                 hid_info(hdev, "HID++ %u.%u device connected.\n",
3103                          hidpp->protocol_major, hidpp->protocol_minor);
3104
3105                 hidpp_overwrite_name(hdev);
3106         }
3107
3108         if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
3109                 ret = wtp_get_config(hidpp);
3110                 if (ret)
3111                         goto hid_hw_open_failed;
3112         } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3113                 ret = g920_get_config(hidpp);
3114                 if (ret)
3115                         goto hid_hw_open_failed;
3116         }
3117
3118         /* Block incoming packets */
3119         hid_device_io_stop(hdev);
3120
3121         if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3122                 ret = hid_hw_start(hdev, connect_mask);
3123                 if (ret) {
3124                         hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3125                         goto hid_hw_start_fail;
3126                 }
3127         }
3128
3129         /* Allow incoming packets */
3130         hid_device_io_start(hdev);
3131
3132         schedule_work(&hidpp->work);
3133         flush_work(&hidpp->work);
3134
3135         return ret;
3136
3137 hid_hw_open_failed:
3138         hid_device_io_stop(hdev);
3139         if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3140                 hid_hw_close(hdev);
3141                 hid_hw_stop(hdev);
3142         }
3143 hid_hw_start_fail:
3144 hid_parse_fail:
3145         sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3146         cancel_work_sync(&hidpp->work);
3147         mutex_destroy(&hidpp->send_mutex);
3148 allocate_fail:
3149         hid_set_drvdata(hdev, NULL);
3150         return ret;
3151 }
3152
3153 static void hidpp_remove(struct hid_device *hdev)
3154 {
3155         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3156
3157         sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3158
3159         if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3160                 hidpp_ff_deinit(hdev);
3161                 hid_hw_close(hdev);
3162         }
3163         hid_hw_stop(hdev);
3164         cancel_work_sync(&hidpp->work);
3165         mutex_destroy(&hidpp->send_mutex);
3166 }
3167
3168 static const struct hid_device_id hidpp_devices[] = {
3169         { /* wireless touchpad */
3170           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3171                 USB_VENDOR_ID_LOGITECH, 0x4011),
3172           .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3173                          HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
3174         { /* wireless touchpad T650 */
3175           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3176                 USB_VENDOR_ID_LOGITECH, 0x4101),
3177           .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3178         { /* wireless touchpad T651 */
3179           HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3180                 USB_DEVICE_ID_LOGITECH_T651),
3181           .driver_data = HIDPP_QUIRK_CLASS_WTP },
3182         { /* Mouse logitech M560 */
3183           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3184                 USB_VENDOR_ID_LOGITECH, 0x402d),
3185           .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
3186         { /* Keyboard logitech K400 */
3187           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3188                 USB_VENDOR_ID_LOGITECH, 0x4024),
3189           .driver_data = HIDPP_QUIRK_CLASS_K400 },
3190         { /* Solar Keyboard Logitech K750 */
3191           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3192                 USB_VENDOR_ID_LOGITECH, 0x4002),
3193           .driver_data = HIDPP_QUIRK_CLASS_K750 },
3194
3195         { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
3196                 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
3197
3198         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
3199                 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
3200         {}
3201 };
3202
3203 MODULE_DEVICE_TABLE(hid, hidpp_devices);
3204
3205 static struct hid_driver hidpp_driver = {
3206         .name = "logitech-hidpp-device",
3207         .id_table = hidpp_devices,
3208         .probe = hidpp_probe,
3209         .remove = hidpp_remove,
3210         .raw_event = hidpp_raw_event,
3211         .input_configured = hidpp_input_configured,
3212         .input_mapping = hidpp_input_mapping,
3213         .input_mapped = hidpp_input_mapped,
3214 };
3215
3216 module_hid_driver(hidpp_driver);