Merge tag 'staging-5.0-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-block.git] / drivers / hid / hid-elecom.c
CommitLineData
64b386ea 1/*
ac58eec2
TK
2 * HID driver for ELECOM devices:
3 * - BM084 Bluetooth Mouse
fbb77e88 4 * - EX-G Trackballs (M-XT3DRBK, M-XT3URBK, M-XT4DRBK)
79837ede
TK
5 * - DEFT Trackballs (M-DT1DRBK, M-DT1URBK, M-DT2DRBK, M-DT2URBK)
6 * - HUGE Trackballs (M-HT1DRBK, M-HT1URBK)
ac58eec2 7 *
64b386ea 8 * Copyright (c) 2010 Richard Nauber <Richard.Nauber@gmail.com>
0bb7a37f
DEP
9 * Copyright (c) 2016 Yuxuan Shui <yshuiv7@gmail.com>
10 * Copyright (c) 2017 Diego Elio Pettenò <flameeyes@flameeyes.eu>
a0933a45 11 * Copyright (c) 2017 Alex Manoussakis <amanou@gnu.org>
ac58eec2 12 * Copyright (c) 2017 Tomasz Kramkowski <tk@the-tk.com>
64b386ea
RN
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
ac58eec2
TK
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
41static 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
73e4008d
NK
56static __u8 *elecom_report_fixup(struct hid_device *hdev, __u8 *rdesc,
57 unsigned int *rsize)
64b386ea 58{
0bb7a37f
DEP
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;
79837ede
TK
68 case USB_DEVICE_ID_ELECOM_M_XT3URBK:
69 case USB_DEVICE_ID_ELECOM_M_XT3DRBK:
fbb77e88 70 case USB_DEVICE_ID_ELECOM_M_XT4DRBK:
ac58eec2
TK
71 mouse_button_fixup(hdev, rdesc, *rsize, 6);
72 break;
79837ede
TK
73 case USB_DEVICE_ID_ELECOM_M_DT1URBK:
74 case USB_DEVICE_ID_ELECOM_M_DT1DRBK:
75 case USB_DEVICE_ID_ELECOM_M_HT1URBK:
76 case USB_DEVICE_ID_ELECOM_M_HT1DRBK:
ac58eec2 77 mouse_button_fixup(hdev, rdesc, *rsize, 8);
0bb7a37f 78 break;
64b386ea 79 }
636a89d4 80 return rdesc;
64b386ea
RN
81}
82
83static const struct hid_device_id elecom_devices[] = {
0bb7a37f 84 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
79837ede
TK
85 { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT3URBK) },
86 { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT3DRBK) },
fbb77e88 87 { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT4DRBK) },
79837ede
TK
88 { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_DT1URBK) },
89 { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_DT1DRBK) },
90 { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_HT1URBK) },
91 { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_HT1DRBK) },
64b386ea
RN
92 { }
93};
94MODULE_DEVICE_TABLE(hid, elecom_devices);
95
96static struct hid_driver elecom_driver = {
97 .name = "elecom",
98 .id_table = elecom_devices,
99 .report_fixup = elecom_report_fixup
100};
f425458e 101module_hid_driver(elecom_driver);
64b386ea 102
64b386ea 103MODULE_LICENSE("GPL");