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