xref: /linux/drivers/hid/hid-elecom.c (revision 307797159ac25fe5a2048bf5c6a5718298edca57)
1 /*
2  *  HID driver for ELECOM devices:
3  *  - BM084 Bluetooth Mouse
4  *  - EX-G Trackballs (M-XT3DRBK, M-XT3URBK, M-XT4DRBK)
5  *  - DEFT Trackballs (M-DT1DRBK, M-DT1URBK, M-DT2DRBK, M-DT2URBK)
6  *  - HUGE Trackballs (M-HT1DRBK, M-HT1URBK)
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_M_XT3URBK:
69 	case USB_DEVICE_ID_ELECOM_M_XT3DRBK:
70 	case USB_DEVICE_ID_ELECOM_M_XT4DRBK:
71 		mouse_button_fixup(hdev, rdesc, *rsize, 6);
72 		break;
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:
77 		mouse_button_fixup(hdev, rdesc, *rsize, 8);
78 		break;
79 	}
80 	return rdesc;
81 }
82 
83 static const struct hid_device_id elecom_devices[] = {
84 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
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) },
87 	{ HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT4DRBK) },
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) },
92 	{ }
93 };
94 MODULE_DEVICE_TABLE(hid, elecom_devices);
95 
96 static struct hid_driver elecom_driver = {
97 	.name = "elecom",
98 	.id_table = elecom_devices,
99 	.report_fixup = elecom_report_fixup
100 };
101 module_hid_driver(elecom_driver);
102 
103 MODULE_LICENSE("GPL");
104