HID: hid-logitech-hidpp: Add range sysfs for Logitech G920
[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/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/kfifo.h>
23 #include <linux/input/mt.h>
24 #include <asm/unaligned.h>
25 #include "hid-ids.h"
26
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
30
31 static bool disable_raw_mode;
32 module_param(disable_raw_mode, bool, 0644);
33 MODULE_PARM_DESC(disable_raw_mode,
34         "Disable Raw mode reporting for touchpads and keep firmware gestures.");
35
36 static bool disable_tap_to_click;
37 module_param(disable_tap_to_click, bool, 0644);
38 MODULE_PARM_DESC(disable_tap_to_click,
39         "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
40
41 #define REPORT_ID_HIDPP_SHORT                   0x10
42 #define REPORT_ID_HIDPP_LONG                    0x11
43 #define REPORT_ID_HIDPP_VERY_LONG               0x12
44
45 #define HIDPP_REPORT_SHORT_LENGTH               7
46 #define HIDPP_REPORT_LONG_LENGTH                20
47 #define HIDPP_REPORT_VERY_LONG_LENGTH           64
48
49 #define HIDPP_QUIRK_CLASS_WTP                   BIT(0)
50 #define HIDPP_QUIRK_CLASS_M560                  BIT(1)
51 #define HIDPP_QUIRK_CLASS_K400                  BIT(2)
52 #define HIDPP_QUIRK_CLASS_G920                  BIT(3)
53
54 /* bits 2..20 are reserved for classes */
55 #define HIDPP_QUIRK_CONNECT_EVENTS              BIT(21)
56 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS        BIT(22)
57 #define HIDPP_QUIRK_NO_HIDINPUT                 BIT(23)
58 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS        BIT(24)
59
60 #define HIDPP_QUIRK_DELAYED_INIT                (HIDPP_QUIRK_NO_HIDINPUT | \
61                                                  HIDPP_QUIRK_CONNECT_EVENTS)
62
63 /*
64  * There are two hidpp protocols in use, the first version hidpp10 is known
65  * as register access protocol or RAP, the second version hidpp20 is known as
66  * feature access protocol or FAP
67  *
68  * Most older devices (including the Unifying usb receiver) use the RAP protocol
69  * where as most newer devices use the FAP protocol. Both protocols are
70  * compatible with the underlying transport, which could be usb, Unifiying, or
71  * bluetooth. The message lengths are defined by the hid vendor specific report
72  * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
73  * the HIDPP_LONG report type (total message length 20 bytes)
74  *
75  * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
76  * messages. The Unifying receiver itself responds to RAP messages (device index
77  * is 0xFF for the receiver), and all messages (short or long) with a device
78  * index between 1 and 6 are passed untouched to the corresponding paired
79  * Unifying device.
80  *
81  * The paired device can be RAP or FAP, it will receive the message untouched
82  * from the Unifiying receiver.
83  */
84
85 struct fap {
86         u8 feature_index;
87         u8 funcindex_clientid;
88         u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
89 };
90
91 struct rap {
92         u8 sub_id;
93         u8 reg_address;
94         u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
95 };
96
97 struct hidpp_report {
98         u8 report_id;
99         u8 device_index;
100         union {
101                 struct fap fap;
102                 struct rap rap;
103                 u8 rawbytes[sizeof(struct fap)];
104         };
105 } __packed;
106
107 struct hidpp_device {
108         struct hid_device *hid_dev;
109         struct mutex send_mutex;
110         void *send_receive_buf;
111         char *name;             /* will never be NULL and should not be freed */
112         wait_queue_head_t wait;
113         bool answer_available;
114         u8 protocol_major;
115         u8 protocol_minor;
116
117         void *private_data;
118
119         struct work_struct work;
120         struct kfifo delayed_work_fifo;
121         atomic_t connected;
122         struct input_dev *delayed_input;
123
124         unsigned long quirks;
125 };
126
127
128 /* HID++ 1.0 error codes */
129 #define HIDPP_ERROR                             0x8f
130 #define HIDPP_ERROR_SUCCESS                     0x00
131 #define HIDPP_ERROR_INVALID_SUBID               0x01
132 #define HIDPP_ERROR_INVALID_ADRESS              0x02
133 #define HIDPP_ERROR_INVALID_VALUE               0x03
134 #define HIDPP_ERROR_CONNECT_FAIL                0x04
135 #define HIDPP_ERROR_TOO_MANY_DEVICES            0x05
136 #define HIDPP_ERROR_ALREADY_EXISTS              0x06
137 #define HIDPP_ERROR_BUSY                        0x07
138 #define HIDPP_ERROR_UNKNOWN_DEVICE              0x08
139 #define HIDPP_ERROR_RESOURCE_ERROR              0x09
140 #define HIDPP_ERROR_REQUEST_UNAVAILABLE         0x0a
141 #define HIDPP_ERROR_INVALID_PARAM_VALUE         0x0b
142 #define HIDPP_ERROR_WRONG_PIN_CODE              0x0c
143 /* HID++ 2.0 error codes */
144 #define HIDPP20_ERROR                           0xff
145
146 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
147
148 static int __hidpp_send_report(struct hid_device *hdev,
149                                 struct hidpp_report *hidpp_report)
150 {
151         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
152         int fields_count, ret;
153
154         hidpp = hid_get_drvdata(hdev);
155
156         switch (hidpp_report->report_id) {
157         case REPORT_ID_HIDPP_SHORT:
158                 fields_count = HIDPP_REPORT_SHORT_LENGTH;
159                 break;
160         case REPORT_ID_HIDPP_LONG:
161                 fields_count = HIDPP_REPORT_LONG_LENGTH;
162                 break;
163         case REPORT_ID_HIDPP_VERY_LONG:
164                 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
165                 break;
166         default:
167                 return -ENODEV;
168         }
169
170         /*
171          * set the device_index as the receiver, it will be overwritten by
172          * hid_hw_request if needed
173          */
174         hidpp_report->device_index = 0xff;
175
176         if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
177                 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
178         } else {
179                 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
180                         (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
181                         HID_REQ_SET_REPORT);
182         }
183
184         return ret == fields_count ? 0 : -1;
185 }
186
187 /**
188  * hidpp_send_message_sync() returns 0 in case of success, and something else
189  * in case of a failure.
190  * - If ' something else' is positive, that means that an error has been raised
191  *   by the protocol itself.
192  * - If ' something else' is negative, that means that we had a classic error
193  *   (-ENOMEM, -EPIPE, etc...)
194  */
195 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
196         struct hidpp_report *message,
197         struct hidpp_report *response)
198 {
199         int ret;
200
201         mutex_lock(&hidpp->send_mutex);
202
203         hidpp->send_receive_buf = response;
204         hidpp->answer_available = false;
205
206         /*
207          * So that we can later validate the answer when it arrives
208          * in hidpp_raw_event
209          */
210         *response = *message;
211
212         ret = __hidpp_send_report(hidpp->hid_dev, message);
213
214         if (ret) {
215                 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
216                 memset(response, 0, sizeof(struct hidpp_report));
217                 goto exit;
218         }
219
220         if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
221                                 5*HZ)) {
222                 dbg_hid("%s:timeout waiting for response\n", __func__);
223                 memset(response, 0, sizeof(struct hidpp_report));
224                 ret = -ETIMEDOUT;
225         }
226
227         if (response->report_id == REPORT_ID_HIDPP_SHORT &&
228             response->rap.sub_id == HIDPP_ERROR) {
229                 ret = response->rap.params[1];
230                 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
231                 goto exit;
232         }
233
234         if ((response->report_id == REPORT_ID_HIDPP_LONG ||
235                         response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
236                         response->fap.feature_index == HIDPP20_ERROR) {
237                 ret = response->fap.params[1];
238                 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
239                 goto exit;
240         }
241
242 exit:
243         mutex_unlock(&hidpp->send_mutex);
244         return ret;
245
246 }
247
248 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
249         u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
250         struct hidpp_report *response)
251 {
252         struct hidpp_report *message;
253         int ret;
254
255         if (param_count > sizeof(message->fap.params))
256                 return -EINVAL;
257
258         message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
259         if (!message)
260                 return -ENOMEM;
261
262         if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
263                 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
264         else
265                 message->report_id = REPORT_ID_HIDPP_LONG;
266         message->fap.feature_index = feat_index;
267         message->fap.funcindex_clientid = funcindex_clientid;
268         memcpy(&message->fap.params, params, param_count);
269
270         ret = hidpp_send_message_sync(hidpp, message, response);
271         kfree(message);
272         return ret;
273 }
274
275 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
276         u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
277         struct hidpp_report *response)
278 {
279         struct hidpp_report *message;
280         int ret, max_count;
281
282         switch (report_id) {
283         case REPORT_ID_HIDPP_SHORT:
284                 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
285                 break;
286         case REPORT_ID_HIDPP_LONG:
287                 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
288                 break;
289         case REPORT_ID_HIDPP_VERY_LONG:
290                 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
291                 break;
292         default:
293                 return -EINVAL;
294         }
295
296         if (param_count > max_count)
297                 return -EINVAL;
298
299         message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
300         if (!message)
301                 return -ENOMEM;
302         message->report_id = report_id;
303         message->rap.sub_id = sub_id;
304         message->rap.reg_address = reg_address;
305         memcpy(&message->rap.params, params, param_count);
306
307         ret = hidpp_send_message_sync(hidpp_dev, message, response);
308         kfree(message);
309         return ret;
310 }
311
312 static void delayed_work_cb(struct work_struct *work)
313 {
314         struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
315                                                         work);
316         hidpp_connect_event(hidpp);
317 }
318
319 static inline bool hidpp_match_answer(struct hidpp_report *question,
320                 struct hidpp_report *answer)
321 {
322         return (answer->fap.feature_index == question->fap.feature_index) &&
323            (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
324 }
325
326 static inline bool hidpp_match_error(struct hidpp_report *question,
327                 struct hidpp_report *answer)
328 {
329         return ((answer->rap.sub_id == HIDPP_ERROR) ||
330             (answer->fap.feature_index == HIDPP20_ERROR)) &&
331             (answer->fap.funcindex_clientid == question->fap.feature_index) &&
332             (answer->fap.params[0] == question->fap.funcindex_clientid);
333 }
334
335 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
336 {
337         return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
338                 (report->rap.sub_id == 0x41);
339 }
340
341 /**
342  * hidpp_prefix_name() prefixes the current given name with "Logitech ".
343  */
344 static void hidpp_prefix_name(char **name, int name_length)
345 {
346 #define PREFIX_LENGTH 9 /* "Logitech " */
347
348         int new_length;
349         char *new_name;
350
351         if (name_length > PREFIX_LENGTH &&
352             strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
353                 /* The prefix has is already in the name */
354                 return;
355
356         new_length = PREFIX_LENGTH + name_length;
357         new_name = kzalloc(new_length, GFP_KERNEL);
358         if (!new_name)
359                 return;
360
361         snprintf(new_name, new_length, "Logitech %s", *name);
362
363         kfree(*name);
364
365         *name = new_name;
366 }
367
368 /* -------------------------------------------------------------------------- */
369 /* HIDP++ 1.0 commands                                                        */
370 /* -------------------------------------------------------------------------- */
371
372 #define HIDPP_SET_REGISTER                              0x80
373 #define HIDPP_GET_REGISTER                              0x81
374 #define HIDPP_SET_LONG_REGISTER                         0x82
375 #define HIDPP_GET_LONG_REGISTER                         0x83
376
377 #define HIDPP_REG_PAIRING_INFORMATION                   0xB5
378 #define DEVICE_NAME                                     0x40
379
380 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
381 {
382         struct hidpp_report response;
383         int ret;
384         /* hid-logitech-dj is in charge of setting the right device index */
385         u8 params[1] = { DEVICE_NAME };
386         char *name;
387         int len;
388
389         ret = hidpp_send_rap_command_sync(hidpp_dev,
390                                         REPORT_ID_HIDPP_SHORT,
391                                         HIDPP_GET_LONG_REGISTER,
392                                         HIDPP_REG_PAIRING_INFORMATION,
393                                         params, 1, &response);
394         if (ret)
395                 return NULL;
396
397         len = response.rap.params[1];
398
399         if (2 + len > sizeof(response.rap.params))
400                 return NULL;
401
402         name = kzalloc(len + 1, GFP_KERNEL);
403         if (!name)
404                 return NULL;
405
406         memcpy(name, &response.rap.params[2], len);
407
408         /* include the terminating '\0' */
409         hidpp_prefix_name(&name, len + 1);
410
411         return name;
412 }
413
414 /* -------------------------------------------------------------------------- */
415 /* 0x0000: Root                                                               */
416 /* -------------------------------------------------------------------------- */
417
418 #define HIDPP_PAGE_ROOT                                 0x0000
419 #define HIDPP_PAGE_ROOT_IDX                             0x00
420
421 #define CMD_ROOT_GET_FEATURE                            0x01
422 #define CMD_ROOT_GET_PROTOCOL_VERSION                   0x11
423
424 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
425         u8 *feature_index, u8 *feature_type)
426 {
427         struct hidpp_report response;
428         int ret;
429         u8 params[2] = { feature >> 8, feature & 0x00FF };
430
431         ret = hidpp_send_fap_command_sync(hidpp,
432                         HIDPP_PAGE_ROOT_IDX,
433                         CMD_ROOT_GET_FEATURE,
434                         params, 2, &response);
435         if (ret)
436                 return ret;
437
438         *feature_index = response.fap.params[0];
439         *feature_type = response.fap.params[1];
440
441         return ret;
442 }
443
444 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
445 {
446         struct hidpp_report response;
447         int ret;
448
449         ret = hidpp_send_fap_command_sync(hidpp,
450                         HIDPP_PAGE_ROOT_IDX,
451                         CMD_ROOT_GET_PROTOCOL_VERSION,
452                         NULL, 0, &response);
453
454         if (ret == HIDPP_ERROR_INVALID_SUBID) {
455                 hidpp->protocol_major = 1;
456                 hidpp->protocol_minor = 0;
457                 return 0;
458         }
459
460         /* the device might not be connected */
461         if (ret == HIDPP_ERROR_RESOURCE_ERROR)
462                 return -EIO;
463
464         if (ret > 0) {
465                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
466                         __func__, ret);
467                 return -EPROTO;
468         }
469         if (ret)
470                 return ret;
471
472         hidpp->protocol_major = response.fap.params[0];
473         hidpp->protocol_minor = response.fap.params[1];
474
475         return ret;
476 }
477
478 static bool hidpp_is_connected(struct hidpp_device *hidpp)
479 {
480         int ret;
481
482         ret = hidpp_root_get_protocol_version(hidpp);
483         if (!ret)
484                 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
485                         hidpp->protocol_major, hidpp->protocol_minor);
486         return ret == 0;
487 }
488
489 /* -------------------------------------------------------------------------- */
490 /* 0x0005: GetDeviceNameType                                                  */
491 /* -------------------------------------------------------------------------- */
492
493 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE                 0x0005
494
495 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT              0x01
496 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME        0x11
497 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE               0x21
498
499 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
500         u8 feature_index, u8 *nameLength)
501 {
502         struct hidpp_report response;
503         int ret;
504
505         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
506                 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
507
508         if (ret > 0) {
509                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
510                         __func__, ret);
511                 return -EPROTO;
512         }
513         if (ret)
514                 return ret;
515
516         *nameLength = response.fap.params[0];
517
518         return ret;
519 }
520
521 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
522         u8 feature_index, u8 char_index, char *device_name, int len_buf)
523 {
524         struct hidpp_report response;
525         int ret, i;
526         int count;
527
528         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
529                 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
530                 &response);
531
532         if (ret > 0) {
533                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
534                         __func__, ret);
535                 return -EPROTO;
536         }
537         if (ret)
538                 return ret;
539
540         switch (response.report_id) {
541         case REPORT_ID_HIDPP_VERY_LONG:
542                 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
543                 break;
544         case REPORT_ID_HIDPP_LONG:
545                 count = HIDPP_REPORT_LONG_LENGTH - 4;
546                 break;
547         case REPORT_ID_HIDPP_SHORT:
548                 count = HIDPP_REPORT_SHORT_LENGTH - 4;
549                 break;
550         default:
551                 return -EPROTO;
552         }
553
554         if (len_buf < count)
555                 count = len_buf;
556
557         for (i = 0; i < count; i++)
558                 device_name[i] = response.fap.params[i];
559
560         return count;
561 }
562
563 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
564 {
565         u8 feature_type;
566         u8 feature_index;
567         u8 __name_length;
568         char *name;
569         unsigned index = 0;
570         int ret;
571
572         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
573                 &feature_index, &feature_type);
574         if (ret)
575                 return NULL;
576
577         ret = hidpp_devicenametype_get_count(hidpp, feature_index,
578                 &__name_length);
579         if (ret)
580                 return NULL;
581
582         name = kzalloc(__name_length + 1, GFP_KERNEL);
583         if (!name)
584                 return NULL;
585
586         while (index < __name_length) {
587                 ret = hidpp_devicenametype_get_device_name(hidpp,
588                         feature_index, index, name + index,
589                         __name_length - index);
590                 if (ret <= 0) {
591                         kfree(name);
592                         return NULL;
593                 }
594                 index += ret;
595         }
596
597         /* include the terminating '\0' */
598         hidpp_prefix_name(&name, __name_length + 1);
599
600         return name;
601 }
602
603 /* -------------------------------------------------------------------------- */
604 /* 0x6010: Touchpad FW items                                                  */
605 /* -------------------------------------------------------------------------- */
606
607 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS                    0x6010
608
609 #define CMD_TOUCHPAD_FW_ITEMS_SET                       0x10
610
611 struct hidpp_touchpad_fw_items {
612         uint8_t presence;
613         uint8_t desired_state;
614         uint8_t state;
615         uint8_t persistent;
616 };
617
618 /**
619  * send a set state command to the device by reading the current items->state
620  * field. items is then filled with the current state.
621  */
622 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
623                                        u8 feature_index,
624                                        struct hidpp_touchpad_fw_items *items)
625 {
626         struct hidpp_report response;
627         int ret;
628         u8 *params = (u8 *)response.fap.params;
629
630         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
631                 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
632
633         if (ret > 0) {
634                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
635                         __func__, ret);
636                 return -EPROTO;
637         }
638         if (ret)
639                 return ret;
640
641         items->presence = params[0];
642         items->desired_state = params[1];
643         items->state = params[2];
644         items->persistent = params[3];
645
646         return 0;
647 }
648
649 /* -------------------------------------------------------------------------- */
650 /* 0x6100: TouchPadRawXY                                                      */
651 /* -------------------------------------------------------------------------- */
652
653 #define HIDPP_PAGE_TOUCHPAD_RAW_XY                      0x6100
654
655 #define CMD_TOUCHPAD_GET_RAW_INFO                       0x01
656 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE               0x21
657
658 #define EVENT_TOUCHPAD_RAW_XY                           0x00
659
660 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT               0x01
661 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT               0x03
662
663 struct hidpp_touchpad_raw_info {
664         u16 x_size;
665         u16 y_size;
666         u8 z_range;
667         u8 area_range;
668         u8 timestamp_unit;
669         u8 maxcontacts;
670         u8 origin;
671         u16 res;
672 };
673
674 struct hidpp_touchpad_raw_xy_finger {
675         u8 contact_type;
676         u8 contact_status;
677         u16 x;
678         u16 y;
679         u8 z;
680         u8 area;
681         u8 finger_id;
682 };
683
684 struct hidpp_touchpad_raw_xy {
685         u16 timestamp;
686         struct hidpp_touchpad_raw_xy_finger fingers[2];
687         u8 spurious_flag;
688         u8 end_of_frame;
689         u8 finger_count;
690         u8 button;
691 };
692
693 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
694         u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
695 {
696         struct hidpp_report response;
697         int ret;
698         u8 *params = (u8 *)response.fap.params;
699
700         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
701                 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
702
703         if (ret > 0) {
704                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
705                         __func__, ret);
706                 return -EPROTO;
707         }
708         if (ret)
709                 return ret;
710
711         raw_info->x_size = get_unaligned_be16(&params[0]);
712         raw_info->y_size = get_unaligned_be16(&params[2]);
713         raw_info->z_range = params[4];
714         raw_info->area_range = params[5];
715         raw_info->maxcontacts = params[7];
716         raw_info->origin = params[8];
717         /* res is given in unit per inch */
718         raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
719
720         return ret;
721 }
722
723 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
724                 u8 feature_index, bool send_raw_reports,
725                 bool sensor_enhanced_settings)
726 {
727         struct hidpp_report response;
728
729         /*
730          * Params:
731          *   bit 0 - enable raw
732          *   bit 1 - 16bit Z, no area
733          *   bit 2 - enhanced sensitivity
734          *   bit 3 - width, height (4 bits each) instead of area
735          *   bit 4 - send raw + gestures (degrades smoothness)
736          *   remaining bits - reserved
737          */
738         u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
739
740         return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
741                 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
742 }
743
744 static void hidpp_touchpad_touch_event(u8 *data,
745         struct hidpp_touchpad_raw_xy_finger *finger)
746 {
747         u8 x_m = data[0] << 2;
748         u8 y_m = data[2] << 2;
749
750         finger->x = x_m << 6 | data[1];
751         finger->y = y_m << 6 | data[3];
752
753         finger->contact_type = data[0] >> 6;
754         finger->contact_status = data[2] >> 6;
755
756         finger->z = data[4];
757         finger->area = data[5];
758         finger->finger_id = data[6] >> 4;
759 }
760
761 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
762                 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
763 {
764         memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
765         raw_xy->end_of_frame = data[8] & 0x01;
766         raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
767         raw_xy->finger_count = data[15] & 0x0f;
768         raw_xy->button = (data[8] >> 2) & 0x01;
769
770         if (raw_xy->finger_count) {
771                 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
772                 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
773         }
774 }
775
776 /* ************************************************************************** */
777 /*                                                                            */
778 /* Device Support                                                             */
779 /*                                                                            */
780 /* ************************************************************************** */
781
782 /* -------------------------------------------------------------------------- */
783 /* Touchpad HID++ devices                                                     */
784 /* -------------------------------------------------------------------------- */
785
786 #define WTP_MANUAL_RESOLUTION                           39
787
788 struct wtp_data {
789         struct input_dev *input;
790         u16 x_size, y_size;
791         u8 finger_count;
792         u8 mt_feature_index;
793         u8 button_feature_index;
794         u8 maxcontacts;
795         bool flip_y;
796         unsigned int resolution;
797 };
798
799 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
800                 struct hid_field *field, struct hid_usage *usage,
801                 unsigned long **bit, int *max)
802 {
803         return -1;
804 }
805
806 static void wtp_populate_input(struct hidpp_device *hidpp,
807                 struct input_dev *input_dev, bool origin_is_hid_core)
808 {
809         struct wtp_data *wd = hidpp->private_data;
810
811         __set_bit(EV_ABS, input_dev->evbit);
812         __set_bit(EV_KEY, input_dev->evbit);
813         __clear_bit(EV_REL, input_dev->evbit);
814         __clear_bit(EV_LED, input_dev->evbit);
815
816         input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
817         input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
818         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
819         input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
820
821         /* Max pressure is not given by the devices, pick one */
822         input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
823
824         input_set_capability(input_dev, EV_KEY, BTN_LEFT);
825
826         if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
827                 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
828         else
829                 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
830
831         input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
832                 INPUT_MT_DROP_UNUSED);
833
834         wd->input = input_dev;
835 }
836
837 static void wtp_touch_event(struct wtp_data *wd,
838         struct hidpp_touchpad_raw_xy_finger *touch_report)
839 {
840         int slot;
841
842         if (!touch_report->finger_id || touch_report->contact_type)
843                 /* no actual data */
844                 return;
845
846         slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
847
848         input_mt_slot(wd->input, slot);
849         input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
850                                         touch_report->contact_status);
851         if (touch_report->contact_status) {
852                 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
853                                 touch_report->x);
854                 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
855                                 wd->flip_y ? wd->y_size - touch_report->y :
856                                              touch_report->y);
857                 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
858                                 touch_report->area);
859         }
860 }
861
862 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
863                 struct hidpp_touchpad_raw_xy *raw)
864 {
865         struct wtp_data *wd = hidpp->private_data;
866         int i;
867
868         for (i = 0; i < 2; i++)
869                 wtp_touch_event(wd, &(raw->fingers[i]));
870
871         if (raw->end_of_frame &&
872             !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
873                 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
874
875         if (raw->end_of_frame || raw->finger_count <= 2) {
876                 input_mt_sync_frame(wd->input);
877                 input_sync(wd->input);
878         }
879 }
880
881 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
882 {
883         struct wtp_data *wd = hidpp->private_data;
884         u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
885                       (data[7] >> 4) * (data[7] >> 4)) / 2;
886         u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
887                       (data[13] >> 4) * (data[13] >> 4)) / 2;
888         struct hidpp_touchpad_raw_xy raw = {
889                 .timestamp = data[1],
890                 .fingers = {
891                         {
892                                 .contact_type = 0,
893                                 .contact_status = !!data[7],
894                                 .x = get_unaligned_le16(&data[3]),
895                                 .y = get_unaligned_le16(&data[5]),
896                                 .z = c1_area,
897                                 .area = c1_area,
898                                 .finger_id = data[2],
899                         }, {
900                                 .contact_type = 0,
901                                 .contact_status = !!data[13],
902                                 .x = get_unaligned_le16(&data[9]),
903                                 .y = get_unaligned_le16(&data[11]),
904                                 .z = c2_area,
905                                 .area = c2_area,
906                                 .finger_id = data[8],
907                         }
908                 },
909                 .finger_count = wd->maxcontacts,
910                 .spurious_flag = 0,
911                 .end_of_frame = (data[0] >> 7) == 0,
912                 .button = data[0] & 0x01,
913         };
914
915         wtp_send_raw_xy_event(hidpp, &raw);
916
917         return 1;
918 }
919
920 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
921 {
922         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
923         struct wtp_data *wd = hidpp->private_data;
924         struct hidpp_report *report = (struct hidpp_report *)data;
925         struct hidpp_touchpad_raw_xy raw;
926
927         if (!wd || !wd->input)
928                 return 1;
929
930         switch (data[0]) {
931         case 0x02:
932                 if (size < 2) {
933                         hid_err(hdev, "Received HID report of bad size (%d)",
934                                 size);
935                         return 1;
936                 }
937                 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
938                         input_event(wd->input, EV_KEY, BTN_LEFT,
939                                         !!(data[1] & 0x01));
940                         input_event(wd->input, EV_KEY, BTN_RIGHT,
941                                         !!(data[1] & 0x02));
942                         input_sync(wd->input);
943                         return 0;
944                 } else {
945                         if (size < 21)
946                                 return 1;
947                         return wtp_mouse_raw_xy_event(hidpp, &data[7]);
948                 }
949         case REPORT_ID_HIDPP_LONG:
950                 /* size is already checked in hidpp_raw_event. */
951                 if ((report->fap.feature_index != wd->mt_feature_index) ||
952                     (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
953                         return 1;
954                 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
955
956                 wtp_send_raw_xy_event(hidpp, &raw);
957                 return 0;
958         }
959
960         return 0;
961 }
962
963 static int wtp_get_config(struct hidpp_device *hidpp)
964 {
965         struct wtp_data *wd = hidpp->private_data;
966         struct hidpp_touchpad_raw_info raw_info = {0};
967         u8 feature_type;
968         int ret;
969
970         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
971                 &wd->mt_feature_index, &feature_type);
972         if (ret)
973                 /* means that the device is not powered up */
974                 return ret;
975
976         ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
977                 &raw_info);
978         if (ret)
979                 return ret;
980
981         wd->x_size = raw_info.x_size;
982         wd->y_size = raw_info.y_size;
983         wd->maxcontacts = raw_info.maxcontacts;
984         wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
985         wd->resolution = raw_info.res;
986         if (!wd->resolution)
987                 wd->resolution = WTP_MANUAL_RESOLUTION;
988
989         return 0;
990 }
991
992 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
993 {
994         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
995         struct wtp_data *wd;
996
997         wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
998                         GFP_KERNEL);
999         if (!wd)
1000                 return -ENOMEM;
1001
1002         hidpp->private_data = wd;
1003
1004         return 0;
1005 };
1006
1007 static int wtp_connect(struct hid_device *hdev, bool connected)
1008 {
1009         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1010         struct wtp_data *wd = hidpp->private_data;
1011         int ret;
1012
1013         if (!connected)
1014                 return 0;
1015
1016         if (!wd->x_size) {
1017                 ret = wtp_get_config(hidpp);
1018                 if (ret) {
1019                         hid_err(hdev, "Can not get wtp config: %d\n", ret);
1020                         return ret;
1021                 }
1022         }
1023
1024         return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
1025                         true, true);
1026 }
1027
1028 /* ------------------------------------------------------------------------- */
1029 /* Logitech M560 devices                                                     */
1030 /* ------------------------------------------------------------------------- */
1031
1032 /*
1033  * Logitech M560 protocol overview
1034  *
1035  * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1036  * the sides buttons are pressed, it sends some keyboard keys events
1037  * instead of buttons ones.
1038  * To complicate things further, the middle button keys sequence
1039  * is different from the odd press and the even press.
1040  *
1041  * forward button -> Super_R
1042  * backward button -> Super_L+'d' (press only)
1043  * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1044  *                  2nd time: left-click (press only)
1045  * NB: press-only means that when the button is pressed, the
1046  * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1047  * together sequentially; instead when the button is released, no event is
1048  * generated !
1049  *
1050  * With the command
1051  *      10<xx>0a 3500af03 (where <xx> is the mouse id),
1052  * the mouse reacts differently:
1053  * - it never sends a keyboard key event
1054  * - for the three mouse button it sends:
1055  *      middle button               press   11<xx>0a 3500af00...
1056  *      side 1 button (forward)     press   11<xx>0a 3500b000...
1057  *      side 2 button (backward)    press   11<xx>0a 3500ae00...
1058  *      middle/side1/side2 button   release 11<xx>0a 35000000...
1059  */
1060
1061 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1062
1063 struct m560_private_data {
1064         struct input_dev *input;
1065 };
1066
1067 /* how buttons are mapped in the report */
1068 #define M560_MOUSE_BTN_LEFT             0x01
1069 #define M560_MOUSE_BTN_RIGHT            0x02
1070 #define M560_MOUSE_BTN_WHEEL_LEFT       0x08
1071 #define M560_MOUSE_BTN_WHEEL_RIGHT      0x10
1072
1073 #define M560_SUB_ID                     0x0a
1074 #define M560_BUTTON_MODE_REGISTER       0x35
1075
1076 static int m560_send_config_command(struct hid_device *hdev, bool connected)
1077 {
1078         struct hidpp_report response;
1079         struct hidpp_device *hidpp_dev;
1080
1081         hidpp_dev = hid_get_drvdata(hdev);
1082
1083         if (!connected)
1084                 return -ENODEV;
1085
1086         return hidpp_send_rap_command_sync(
1087                 hidpp_dev,
1088                 REPORT_ID_HIDPP_SHORT,
1089                 M560_SUB_ID,
1090                 M560_BUTTON_MODE_REGISTER,
1091                 (u8 *)m560_config_parameter,
1092                 sizeof(m560_config_parameter),
1093                 &response
1094         );
1095 }
1096
1097 static int m560_allocate(struct hid_device *hdev)
1098 {
1099         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1100         struct m560_private_data *d;
1101
1102         d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1103                         GFP_KERNEL);
1104         if (!d)
1105                 return -ENOMEM;
1106
1107         hidpp->private_data = d;
1108
1109         return 0;
1110 };
1111
1112 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1113 {
1114         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1115         struct m560_private_data *mydata = hidpp->private_data;
1116
1117         /* sanity check */
1118         if (!mydata || !mydata->input) {
1119                 hid_err(hdev, "error in parameter\n");
1120                 return -EINVAL;
1121         }
1122
1123         if (size < 7) {
1124                 hid_err(hdev, "error in report\n");
1125                 return 0;
1126         }
1127
1128         if (data[0] == REPORT_ID_HIDPP_LONG &&
1129             data[2] == M560_SUB_ID && data[6] == 0x00) {
1130                 /*
1131                  * m560 mouse report for middle, forward and backward button
1132                  *
1133                  * data[0] = 0x11
1134                  * data[1] = device-id
1135                  * data[2] = 0x0a
1136                  * data[5] = 0xaf -> middle
1137                  *           0xb0 -> forward
1138                  *           0xae -> backward
1139                  *           0x00 -> release all
1140                  * data[6] = 0x00
1141                  */
1142
1143                 switch (data[5]) {
1144                 case 0xaf:
1145                         input_report_key(mydata->input, BTN_MIDDLE, 1);
1146                         break;
1147                 case 0xb0:
1148                         input_report_key(mydata->input, BTN_FORWARD, 1);
1149                         break;
1150                 case 0xae:
1151                         input_report_key(mydata->input, BTN_BACK, 1);
1152                         break;
1153                 case 0x00:
1154                         input_report_key(mydata->input, BTN_BACK, 0);
1155                         input_report_key(mydata->input, BTN_FORWARD, 0);
1156                         input_report_key(mydata->input, BTN_MIDDLE, 0);
1157                         break;
1158                 default:
1159                         hid_err(hdev, "error in report\n");
1160                         return 0;
1161                 }
1162                 input_sync(mydata->input);
1163
1164         } else if (data[0] == 0x02) {
1165                 /*
1166                  * Logitech M560 mouse report
1167                  *
1168                  * data[0] = type (0x02)
1169                  * data[1..2] = buttons
1170                  * data[3..5] = xy
1171                  * data[6] = wheel
1172                  */
1173
1174                 int v;
1175
1176                 input_report_key(mydata->input, BTN_LEFT,
1177                         !!(data[1] & M560_MOUSE_BTN_LEFT));
1178                 input_report_key(mydata->input, BTN_RIGHT,
1179                         !!(data[1] & M560_MOUSE_BTN_RIGHT));
1180
1181                 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
1182                         input_report_rel(mydata->input, REL_HWHEEL, -1);
1183                 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
1184                         input_report_rel(mydata->input, REL_HWHEEL, 1);
1185
1186                 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
1187                 input_report_rel(mydata->input, REL_X, v);
1188
1189                 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
1190                 input_report_rel(mydata->input, REL_Y, v);
1191
1192                 v = hid_snto32(data[6], 8);
1193                 input_report_rel(mydata->input, REL_WHEEL, v);
1194
1195                 input_sync(mydata->input);
1196         }
1197
1198         return 1;
1199 }
1200
1201 static void m560_populate_input(struct hidpp_device *hidpp,
1202                 struct input_dev *input_dev, bool origin_is_hid_core)
1203 {
1204         struct m560_private_data *mydata = hidpp->private_data;
1205
1206         mydata->input = input_dev;
1207
1208         __set_bit(EV_KEY, mydata->input->evbit);
1209         __set_bit(BTN_MIDDLE, mydata->input->keybit);
1210         __set_bit(BTN_RIGHT, mydata->input->keybit);
1211         __set_bit(BTN_LEFT, mydata->input->keybit);
1212         __set_bit(BTN_BACK, mydata->input->keybit);
1213         __set_bit(BTN_FORWARD, mydata->input->keybit);
1214
1215         __set_bit(EV_REL, mydata->input->evbit);
1216         __set_bit(REL_X, mydata->input->relbit);
1217         __set_bit(REL_Y, mydata->input->relbit);
1218         __set_bit(REL_WHEEL, mydata->input->relbit);
1219         __set_bit(REL_HWHEEL, mydata->input->relbit);
1220 }
1221
1222 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1223                 struct hid_field *field, struct hid_usage *usage,
1224                 unsigned long **bit, int *max)
1225 {
1226         return -1;
1227 }
1228
1229 /* ------------------------------------------------------------------------- */
1230 /* Logitech K400 devices                                                     */
1231 /* ------------------------------------------------------------------------- */
1232
1233 /*
1234  * The Logitech K400 keyboard has an embedded touchpad which is seen
1235  * as a mouse from the OS point of view. There is a hardware shortcut to disable
1236  * tap-to-click but the setting is not remembered accross reset, annoying some
1237  * users.
1238  *
1239  * We can toggle this feature from the host by using the feature 0x6010:
1240  * Touchpad FW items
1241  */
1242
1243 struct k400_private_data {
1244         u8 feature_index;
1245 };
1246
1247 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
1248 {
1249         struct k400_private_data *k400 = hidpp->private_data;
1250         struct hidpp_touchpad_fw_items items = {};
1251         int ret;
1252         u8 feature_type;
1253
1254         if (!k400->feature_index) {
1255                 ret = hidpp_root_get_feature(hidpp,
1256                         HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
1257                         &k400->feature_index, &feature_type);
1258                 if (ret)
1259                         /* means that the device is not powered up */
1260                         return ret;
1261         }
1262
1263         ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
1264         if (ret)
1265                 return ret;
1266
1267         return 0;
1268 }
1269
1270 static int k400_allocate(struct hid_device *hdev)
1271 {
1272         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1273         struct k400_private_data *k400;
1274
1275         k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
1276                             GFP_KERNEL);
1277         if (!k400)
1278                 return -ENOMEM;
1279
1280         hidpp->private_data = k400;
1281
1282         return 0;
1283 };
1284
1285 static int k400_connect(struct hid_device *hdev, bool connected)
1286 {
1287         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1288
1289         if (!connected)
1290                 return 0;
1291
1292         if (!disable_tap_to_click)
1293                 return 0;
1294
1295         return k400_disable_tap_to_click(hidpp);
1296 }
1297
1298 /* ------------------------------------------------------------------------- */
1299 /* Logitech G920 Driving Force Racing Wheel for Xbox One                     */
1300 /* ------------------------------------------------------------------------- */
1301
1302 #define HIDPP_PAGE_G920_FORCE_FEEDBACK                  0x8123
1303
1304 /* Using session ID = 1 */
1305 #define CMD_G920_FORCE_GET_APERTURE                     0x51
1306 #define CMD_G920_FORCE_SET_APERTURE                     0x61
1307
1308 struct g920_private_data {
1309         u8 force_feature;
1310         u16 range;
1311 };
1312
1313 #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
1314
1315 static ssize_t g920_range_show(struct device *dev, struct device_attribute *attr,
1316                                 char *buf)
1317 {
1318         struct hid_device *hid = to_hid_device(dev);
1319         struct hidpp_device *hidpp = hid_get_drvdata(hid);
1320         struct g920_private_data *pdata;
1321
1322         pdata = hidpp->private_data;
1323         if (!pdata) {
1324                 hid_err(hid, "Private driver data not found!\n");
1325                 return -EINVAL;
1326         }
1327
1328         return scnprintf(buf, PAGE_SIZE, "%u\n", pdata->range);
1329 }
1330
1331 static ssize_t g920_range_store(struct device *dev, struct device_attribute *attr,
1332                                  const char *buf, size_t count)
1333 {
1334         struct hid_device *hid = to_hid_device(dev);
1335         struct hidpp_device *hidpp = hid_get_drvdata(hid);
1336         struct g920_private_data *pdata;
1337         struct hidpp_report response;
1338         u8 params[2];
1339         int ret;
1340         u16 range = simple_strtoul(buf, NULL, 10);
1341
1342         pdata = hidpp->private_data;
1343         if (!pdata) {
1344                 hid_err(hid, "Private driver data not found!\n");
1345                 return -EINVAL;
1346         }
1347
1348         if (range < 180)
1349                 range = 180;
1350         else if (range > 900)
1351                 range = 900;
1352
1353         params[0] = range >> 8;
1354         params[1] = range & 0x00FF;
1355
1356         ret = hidpp_send_fap_command_sync(hidpp, pdata->force_feature,
1357                 CMD_G920_FORCE_SET_APERTURE, params, 2, &response);
1358         if (ret)
1359                 return ret;
1360
1361         pdata->range = range;
1362         return count;
1363 }
1364
1365 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, g920_range_show, g920_range_store);
1366
1367 static int g920_allocate(struct hid_device *hdev)
1368 {
1369         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1370         struct g920_private_data *pdata;
1371
1372         pdata = devm_kzalloc(&hdev->dev, sizeof(struct g920_private_data),
1373                         GFP_KERNEL);
1374         if (!pdata)
1375                 return -ENOMEM;
1376
1377         hidpp->private_data = pdata;
1378
1379         return 0;
1380 }
1381
1382 static int g920_get_config(struct hidpp_device *hidpp)
1383 {
1384         struct g920_private_data *pdata = hidpp->private_data;
1385         struct hidpp_report response;
1386         u8 feature_type;
1387         u8 feature_index;
1388         int ret;
1389
1390         pdata = hidpp->private_data;
1391         if (!pdata) {
1392                 hid_err(hidpp->hid_dev, "Private driver data not found!\n");
1393                 return -EINVAL;
1394         }
1395
1396         /* Find feature and store for later use */
1397         ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
1398                 &feature_index, &feature_type);
1399         if (ret)
1400                 return ret;
1401
1402         pdata->force_feature = feature_index;
1403
1404         /* Read current Range */
1405         ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1406                 CMD_G920_FORCE_GET_APERTURE, NULL, 0, &response);
1407         if (ret > 0) {
1408                 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1409                         __func__, ret);
1410                 return -EPROTO;
1411         }
1412         if (ret)
1413                 return ret;
1414
1415         pdata->range = get_unaligned_be16(&response.fap.params[0]);
1416
1417         /* Create sysfs interface */
1418         ret = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1419         if (ret)
1420                 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d\n", ret);
1421
1422         return 0;
1423 }
1424
1425 /* -------------------------------------------------------------------------- */
1426 /* Generic HID++ devices                                                      */
1427 /* -------------------------------------------------------------------------- */
1428
1429 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1430                 struct hid_field *field, struct hid_usage *usage,
1431                 unsigned long **bit, int *max)
1432 {
1433         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1434
1435         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1436                 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
1437         else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
1438                         field->application != HID_GD_MOUSE)
1439                 return m560_input_mapping(hdev, hi, field, usage, bit, max);
1440
1441         return 0;
1442 }
1443
1444 static void hidpp_populate_input(struct hidpp_device *hidpp,
1445                 struct input_dev *input, bool origin_is_hid_core)
1446 {
1447         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1448                 wtp_populate_input(hidpp, input, origin_is_hid_core);
1449         else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1450                 m560_populate_input(hidpp, input, origin_is_hid_core);
1451 }
1452
1453 static int hidpp_input_configured(struct hid_device *hdev,
1454                                 struct hid_input *hidinput)
1455 {
1456         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1457         struct input_dev *input = hidinput->input;
1458
1459         hidpp_populate_input(hidpp, input, true);
1460
1461         return 0;
1462 }
1463
1464 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
1465                 int size)
1466 {
1467         struct hidpp_report *question = hidpp->send_receive_buf;
1468         struct hidpp_report *answer = hidpp->send_receive_buf;
1469         struct hidpp_report *report = (struct hidpp_report *)data;
1470
1471         /*
1472          * If the mutex is locked then we have a pending answer from a
1473          * previously sent command.
1474          */
1475         if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
1476                 /*
1477                  * Check for a correct hidpp20 answer or the corresponding
1478                  * error
1479                  */
1480                 if (hidpp_match_answer(question, report) ||
1481                                 hidpp_match_error(question, report)) {
1482                         *answer = *report;
1483                         hidpp->answer_available = true;
1484                         wake_up(&hidpp->wait);
1485                         /*
1486                          * This was an answer to a command that this driver sent
1487                          * We return 1 to hid-core to avoid forwarding the
1488                          * command upstream as it has been treated by the driver
1489                          */
1490
1491                         return 1;
1492                 }
1493         }
1494
1495         if (unlikely(hidpp_report_is_connect_event(report))) {
1496                 atomic_set(&hidpp->connected,
1497                                 !(report->rap.params[0] & (1 << 6)));
1498                 if ((hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) &&
1499                     (schedule_work(&hidpp->work) == 0))
1500                         dbg_hid("%s: connect event already queued\n", __func__);
1501                 return 1;
1502         }
1503
1504         return 0;
1505 }
1506
1507 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1508                 u8 *data, int size)
1509 {
1510         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1511         int ret = 0;
1512
1513         /* Generic HID++ processing. */
1514         switch (data[0]) {
1515         case REPORT_ID_HIDPP_VERY_LONG:
1516                 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
1517                         hid_err(hdev, "received hid++ report of bad size (%d)",
1518                                 size);
1519                         return 1;
1520                 }
1521                 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1522                 break;
1523         case REPORT_ID_HIDPP_LONG:
1524                 if (size != HIDPP_REPORT_LONG_LENGTH) {
1525                         hid_err(hdev, "received hid++ report of bad size (%d)",
1526                                 size);
1527                         return 1;
1528                 }
1529                 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1530                 break;
1531         case REPORT_ID_HIDPP_SHORT:
1532                 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1533                         hid_err(hdev, "received hid++ report of bad size (%d)",
1534                                 size);
1535                         return 1;
1536                 }
1537                 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1538                 break;
1539         }
1540
1541         /* If no report is available for further processing, skip calling
1542          * raw_event of subclasses. */
1543         if (ret != 0)
1544                 return ret;
1545
1546         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1547                 return wtp_raw_event(hdev, data, size);
1548         else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1549                 return m560_raw_event(hdev, data, size);
1550
1551         return 0;
1552 }
1553
1554 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
1555 {
1556         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1557         char *name;
1558
1559         if (use_unifying)
1560                 /*
1561                  * the device is connected through an Unifying receiver, and
1562                  * might not be already connected.
1563                  * Ask the receiver for its name.
1564                  */
1565                 name = hidpp_get_unifying_name(hidpp);
1566         else
1567                 name = hidpp_get_device_name(hidpp);
1568
1569         if (!name) {
1570                 hid_err(hdev, "unable to retrieve the name of the device");
1571         } else {
1572                 dbg_hid("HID++: Got name: %s\n", name);
1573                 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1574         }
1575
1576         kfree(name);
1577 }
1578
1579 static int hidpp_input_open(struct input_dev *dev)
1580 {
1581         struct hid_device *hid = input_get_drvdata(dev);
1582
1583         return hid_hw_open(hid);
1584 }
1585
1586 static void hidpp_input_close(struct input_dev *dev)
1587 {
1588         struct hid_device *hid = input_get_drvdata(dev);
1589
1590         hid_hw_close(hid);
1591 }
1592
1593 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1594 {
1595         struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
1596         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1597
1598         if (!input_dev)
1599                 return NULL;
1600
1601         input_set_drvdata(input_dev, hdev);
1602         input_dev->open = hidpp_input_open;
1603         input_dev->close = hidpp_input_close;
1604
1605         input_dev->name = hidpp->name;
1606         input_dev->phys = hdev->phys;
1607         input_dev->uniq = hdev->uniq;
1608         input_dev->id.bustype = hdev->bus;
1609         input_dev->id.vendor  = hdev->vendor;
1610         input_dev->id.product = hdev->product;
1611         input_dev->id.version = hdev->version;
1612         input_dev->dev.parent = &hdev->dev;
1613
1614         return input_dev;
1615 }
1616
1617 static void hidpp_connect_event(struct hidpp_device *hidpp)
1618 {
1619         struct hid_device *hdev = hidpp->hid_dev;
1620         int ret = 0;
1621         bool connected = atomic_read(&hidpp->connected);
1622         struct input_dev *input;
1623         char *name, *devm_name;
1624
1625         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1626                 ret = wtp_connect(hdev, connected);
1627                 if (ret)
1628                         return;
1629         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1630                 ret = m560_send_config_command(hdev, connected);
1631                 if (ret)
1632                         return;
1633         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
1634                 ret = k400_connect(hdev, connected);
1635                 if (ret)
1636                         return;
1637         }
1638
1639         if (!connected || hidpp->delayed_input)
1640                 return;
1641
1642         /* the device is already connected, we can ask for its name and
1643          * protocol */
1644         if (!hidpp->protocol_major) {
1645                 ret = !hidpp_is_connected(hidpp);
1646                 if (ret) {
1647                         hid_err(hdev, "Can not get the protocol version.\n");
1648                         return;
1649                 }
1650                 hid_info(hdev, "HID++ %u.%u device connected.\n",
1651                          hidpp->protocol_major, hidpp->protocol_minor);
1652         }
1653
1654         if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
1655                 /* if HID created the input nodes for us, we can stop now */
1656                 return;
1657
1658         if (!hidpp->name || hidpp->name == hdev->name) {
1659                 name = hidpp_get_device_name(hidpp);
1660                 if (!name) {
1661                         hid_err(hdev,
1662                                 "unable to retrieve the name of the device");
1663                         return;
1664                 }
1665
1666                 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1667                 kfree(name);
1668                 if (!devm_name)
1669                         return;
1670
1671                 hidpp->name = devm_name;
1672         }
1673
1674         input = hidpp_allocate_input(hdev);
1675         if (!input) {
1676                 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1677                 return;
1678         }
1679
1680         hidpp_populate_input(hidpp, input, false);
1681
1682         ret = input_register_device(input);
1683         if (ret)
1684                 input_free_device(input);
1685
1686         hidpp->delayed_input = input;
1687 }
1688
1689 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1690 {
1691         struct hidpp_device *hidpp;
1692         int ret;
1693         bool connected;
1694         unsigned int connect_mask = HID_CONNECT_DEFAULT;
1695
1696         hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1697                         GFP_KERNEL);
1698         if (!hidpp)
1699                 return -ENOMEM;
1700
1701         hidpp->hid_dev = hdev;
1702         hidpp->name = hdev->name;
1703         hid_set_drvdata(hdev, hidpp);
1704
1705         hidpp->quirks = id->driver_data;
1706
1707         if (disable_raw_mode) {
1708                 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
1709                 hidpp->quirks &= ~HIDPP_QUIRK_CONNECT_EVENTS;
1710                 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
1711         }
1712
1713         if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1714                 ret = wtp_allocate(hdev, id);
1715                 if (ret)
1716                         goto allocate_fail;
1717         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1718                 ret = m560_allocate(hdev);
1719                 if (ret)
1720                         goto allocate_fail;
1721         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
1722                 ret = k400_allocate(hdev);
1723                 if (ret)
1724                         goto allocate_fail;
1725         } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1726                 ret = g920_allocate(hdev);
1727                 if (ret)
1728                         goto allocate_fail;
1729         }
1730
1731         INIT_WORK(&hidpp->work, delayed_work_cb);
1732         mutex_init(&hidpp->send_mutex);
1733         init_waitqueue_head(&hidpp->wait);
1734
1735         ret = hid_parse(hdev);
1736         if (ret) {
1737                 hid_err(hdev, "%s:parse failed\n", __func__);
1738                 goto hid_parse_fail;
1739         }
1740
1741         if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
1742                 connect_mask &= ~HID_CONNECT_HIDINPUT;
1743
1744         if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1745                 ret = hid_hw_start(hdev, connect_mask);
1746                 if (ret) {
1747                         hid_err(hdev, "hw start failed\n");
1748                         goto hid_hw_start_fail;
1749                 }
1750                 ret = hid_hw_open(hdev);
1751                 if (ret < 0) {
1752                         dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1753                                 __func__, ret);
1754                         hid_hw_stop(hdev);
1755                         goto hid_hw_start_fail;
1756                 }
1757         }
1758
1759
1760         /* Allow incoming packets */
1761         hid_device_io_start(hdev);
1762
1763         connected = hidpp_is_connected(hidpp);
1764         if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1765                 if (!connected) {
1766                         ret = -ENODEV;
1767                         hid_err(hdev, "Device not connected");
1768                         goto hid_hw_open_failed;
1769                 }
1770
1771                 hid_info(hdev, "HID++ %u.%u device connected.\n",
1772                          hidpp->protocol_major, hidpp->protocol_minor);
1773         }
1774
1775         hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
1776         atomic_set(&hidpp->connected, connected);
1777
1778         if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
1779                 ret = wtp_get_config(hidpp);
1780                 if (ret)
1781                         goto hid_hw_open_failed;
1782         } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
1783                 ret = g920_get_config(hidpp);
1784                 if (ret)
1785                         goto hid_hw_open_failed;
1786         }
1787
1788         /* Block incoming packets */
1789         hid_device_io_stop(hdev);
1790
1791         if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
1792                 ret = hid_hw_start(hdev, connect_mask);
1793                 if (ret) {
1794                         hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1795                         goto hid_hw_start_fail;
1796                 }
1797         }
1798
1799         if (hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) {
1800                 /* Allow incoming packets */
1801                 hid_device_io_start(hdev);
1802
1803                 hidpp_connect_event(hidpp);
1804         }
1805
1806         return ret;
1807
1808 hid_hw_open_failed:
1809         hid_device_io_stop(hdev);
1810         if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1811                 device_remove_file(&hdev->dev, &dev_attr_range);
1812                 hid_hw_close(hdev);
1813                 hid_hw_stop(hdev);
1814         }
1815 hid_hw_start_fail:
1816 hid_parse_fail:
1817         cancel_work_sync(&hidpp->work);
1818         mutex_destroy(&hidpp->send_mutex);
1819 allocate_fail:
1820         hid_set_drvdata(hdev, NULL);
1821         return ret;
1822 }
1823
1824 static void hidpp_remove(struct hid_device *hdev)
1825 {
1826         struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1827
1828         if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1829                 device_remove_file(&hdev->dev, &dev_attr_range);
1830                 hid_hw_close(hdev);
1831         }
1832         hid_hw_stop(hdev);
1833         cancel_work_sync(&hidpp->work);
1834         mutex_destroy(&hidpp->send_mutex);
1835 }
1836
1837 static const struct hid_device_id hidpp_devices[] = {
1838         { /* wireless touchpad */
1839           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1840                 USB_VENDOR_ID_LOGITECH, 0x4011),
1841           .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1842                          HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
1843         { /* wireless touchpad T650 */
1844           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1845                 USB_VENDOR_ID_LOGITECH, 0x4101),
1846           .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
1847         { /* wireless touchpad T651 */
1848           HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1849                 USB_DEVICE_ID_LOGITECH_T651),
1850           .driver_data = HIDPP_QUIRK_CLASS_WTP },
1851         { /* Mouse logitech M560 */
1852           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1853                 USB_VENDOR_ID_LOGITECH, 0x402d),
1854           .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
1855         { /* Keyboard logitech K400 */
1856           HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1857                 USB_VENDOR_ID_LOGITECH, 0x4024),
1858           .driver_data = HIDPP_QUIRK_CONNECT_EVENTS | HIDPP_QUIRK_CLASS_K400 },
1859
1860         { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1861                 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1862
1863         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
1864                 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
1865         {}
1866 };
1867
1868 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1869
1870 static struct hid_driver hidpp_driver = {
1871         .name = "logitech-hidpp-device",
1872         .id_table = hidpp_devices,
1873         .probe = hidpp_probe,
1874         .remove = hidpp_remove,
1875         .raw_event = hidpp_raw_event,
1876         .input_configured = hidpp_input_configured,
1877         .input_mapping = hidpp_input_mapping,
1878 };
1879
1880 module_hid_driver(hidpp_driver);