^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * HID driver for ELECOM devices:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * - BM084 Bluetooth Mouse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * - EX-G Trackballs (M-XT3DRBK, M-XT3URBK, M-XT4DRBK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * - DEFT Trackballs (M-DT1DRBK, M-DT1URBK, M-DT2DRBK, M-DT2URBK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * - HUGE Trackballs (M-HT1DRBK, M-HT1URBK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (c) 2010 Richard Nauber <Richard.Nauber@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (c) 2016 Yuxuan Shui <yshuiv7@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (c) 2017 Diego Elio Pettenò <flameeyes@flameeyes.eu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Copyright (c) 2017 Alex Manoussakis <amanou@gnu.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (c) 2017 Tomasz Kramkowski <tk@the-tk.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Certain ELECOM mice misreport their button count meaning that they only work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * correctly with the ELECOM mouse assistant software which is unavailable for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Linux. A four extra INPUT reports and a FEATURE report are described by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * report descriptor but it does not appear that these enable software to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * control what the extra buttons map to. The only simple and straightforward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * solution seems to involve fixing up the report descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Report descriptor format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Positions 13, 15, 21 and 31 store the button bit count, button usage minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * button usage maximum and padding bit count respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define MOUSE_BUTTONS_MAX 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void mouse_button_fixup(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __u8 *rdesc, unsigned int rsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int nbuttons)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (rsize < 32 || rdesc[12] != 0x95 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) rdesc[14] != 0x75 || rdesc[15] != 0x01 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) rdesc[20] != 0x29 || rdesc[30] != 0x75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) hid_info(hdev, "Fixing up Elecom mouse button count\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) nbuttons = clamp(nbuttons, 0, MOUSE_BUTTONS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) rdesc[13] = nbuttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) rdesc[21] = nbuttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) rdesc[31] = MOUSE_BUTTONS_MAX - nbuttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static __u8 *elecom_report_fixup(struct hid_device *hdev, __u8 *rdesc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int *rsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) case USB_DEVICE_ID_ELECOM_BM084:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* The BM084 Bluetooth mouse includes a non-existing horizontal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * wheel in the HID descriptor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (*rsize >= 48 && rdesc[46] == 0x05 && rdesc[47] == 0x0c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) hid_info(hdev, "Fixing up Elecom BM084 report descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) rdesc[47] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) case USB_DEVICE_ID_ELECOM_M_XT3URBK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) case USB_DEVICE_ID_ELECOM_M_XT3DRBK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) case USB_DEVICE_ID_ELECOM_M_XT4DRBK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) mouse_button_fixup(hdev, rdesc, *rsize, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) case USB_DEVICE_ID_ELECOM_M_DT1URBK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case USB_DEVICE_ID_ELECOM_M_DT1DRBK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) case USB_DEVICE_ID_ELECOM_M_HT1URBK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) case USB_DEVICE_ID_ELECOM_M_HT1DRBK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) mouse_button_fixup(hdev, rdesc, *rsize, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return rdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static const struct hid_device_id elecom_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT3URBK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT3DRBK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT4DRBK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_DT1URBK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_DT1DRBK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_HT1URBK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_HT1DRBK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) MODULE_DEVICE_TABLE(hid, elecom_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static struct hid_driver elecom_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .name = "elecom",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .id_table = elecom_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .report_fixup = elecom_report_fixup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) module_hid_driver(elecom_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) MODULE_LICENSE("GPL");