Merge branches 'for-4.16/upstream' and 'for-4.15/upstream-fixes' into for-linus
[muen/linux.git] / drivers / hid / hid-elecom.c
1 /*
2  *  HID driver for ELECOM devices:
3  *  - BM084 Bluetooth Mouse
4  *  - EX-G Trackball (Wired and wireless)
5  *  - DEFT Trackball (Wired and wireless)
6  *  - HUGE Trackball (Wired and wireless)
7  *
8  *  Copyright (c) 2010 Richard Nauber <Richard.Nauber@gmail.com>
9  *  Copyright (c) 2016 Yuxuan Shui <yshuiv7@gmail.com>
10  *  Copyright (c) 2017 Diego Elio Pettenò <flameeyes@flameeyes.eu>
11  *  Copyright (c) 2017 Alex Manoussakis <amanou@gnu.org>
12  *  Copyright (c) 2017 Tomasz Kramkowski <tk@the-tk.com>
13  */
14
15 /*
16  * This program is free software; you can redistribute it and/or modify it
17  * under the terms of the GNU General Public License as published by the Free
18  * Software Foundation; either version 2 of the License, or (at your option)
19  * any later version.
20  */
21
22 #include <linux/device.h>
23 #include <linux/hid.h>
24 #include <linux/module.h>
25
26 #include "hid-ids.h"
27
28 /*
29  * Certain ELECOM mice misreport their button count meaning that they only work
30  * correctly with the ELECOM mouse assistant software which is unavailable for
31  * Linux. A four extra INPUT reports and a FEATURE report are described by the
32  * report descriptor but it does not appear that these enable software to
33  * control what the extra buttons map to. The only simple and straightforward
34  * solution seems to involve fixing up the report descriptor.
35  *
36  * Report descriptor format:
37  * Positions 13, 15, 21 and 31 store the button bit count, button usage minimum,
38  * button usage maximum and padding bit count respectively.
39  */
40 #define MOUSE_BUTTONS_MAX 8
41 static void mouse_button_fixup(struct hid_device *hdev,
42                                __u8 *rdesc, unsigned int rsize,
43                                int nbuttons)
44 {
45         if (rsize < 32 || rdesc[12] != 0x95 ||
46             rdesc[14] != 0x75 || rdesc[15] != 0x01 ||
47             rdesc[20] != 0x29 || rdesc[30] != 0x75)
48                 return;
49         hid_info(hdev, "Fixing up Elecom mouse button count\n");
50         nbuttons = clamp(nbuttons, 0, MOUSE_BUTTONS_MAX);
51         rdesc[13] = nbuttons;
52         rdesc[21] = nbuttons;
53         rdesc[31] = MOUSE_BUTTONS_MAX - nbuttons;
54 }
55
56 static __u8 *elecom_report_fixup(struct hid_device *hdev, __u8 *rdesc,
57                 unsigned int *rsize)
58 {
59         switch (hdev->product) {
60         case USB_DEVICE_ID_ELECOM_BM084:
61                 /* The BM084 Bluetooth mouse includes a non-existing horizontal
62                  * wheel in the HID descriptor. */
63                 if (*rsize >= 48 && rdesc[46] == 0x05 && rdesc[47] == 0x0c) {
64                         hid_info(hdev, "Fixing up Elecom BM084 report descriptor\n");
65                         rdesc[47] = 0x00;
66                 }
67                 break;
68         case USB_DEVICE_ID_ELECOM_EX_G_WIRED:
69         case USB_DEVICE_ID_ELECOM_EX_G_WIRELESS:
70                 mouse_button_fixup(hdev, rdesc, *rsize, 6);
71                 break;
72         case USB_DEVICE_ID_ELECOM_DEFT_WIRED:
73         case USB_DEVICE_ID_ELECOM_DEFT_WIRELESS:
74         case USB_DEVICE_ID_ELECOM_HUGE_WIRED:
75         case USB_DEVICE_ID_ELECOM_HUGE_WIRELESS:
76                 mouse_button_fixup(hdev, rdesc, *rsize, 8);
77                 break;
78         }
79         return rdesc;
80 }
81
82 static const struct hid_device_id elecom_devices[] = {
83         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
84         { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_EX_G_WIRED) },
85         { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_EX_G_WIRELESS) },
86         { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_DEFT_WIRED) },
87         { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_DEFT_WIRELESS) },
88         { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_HUGE_WIRED) },
89         { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_HUGE_WIRELESS) },
90         { }
91 };
92 MODULE_DEVICE_TABLE(hid, elecom_devices);
93
94 static struct hid_driver elecom_driver = {
95         .name = "elecom",
96         .id_table = elecom_devices,
97         .report_fixup = elecom_report_fixup
98 };
99 module_hid_driver(elecom_driver);
100
101 MODULE_LICENSE("GPL");