hid-elecom.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 of the License, or (at your option)
  18. * any later version.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/hid.h>
  22. #include <linux/module.h>
  23. #include "hid-ids.h"
  24. /*
  25. * Certain ELECOM mice misreport their button count meaning that they only work
  26. * correctly with the ELECOM mouse assistant software which is unavailable for
  27. * Linux. A four extra INPUT reports and a FEATURE report are described by the
  28. * report descriptor but it does not appear that these enable software to
  29. * control what the extra buttons map to. The only simple and straightforward
  30. * solution seems to involve fixing up the report descriptor.
  31. *
  32. * Report descriptor format:
  33. * Positions 13, 15, 21 and 31 store the button bit count, button usage minimum,
  34. * button usage maximum and padding bit count respectively.
  35. */
  36. #define MOUSE_BUTTONS_MAX 8
  37. static void mouse_button_fixup(struct hid_device *hdev,
  38. __u8 *rdesc, unsigned int rsize,
  39. int nbuttons)
  40. {
  41. if (rsize < 32 || rdesc[12] != 0x95 ||
  42. rdesc[14] != 0x75 || rdesc[15] != 0x01 ||
  43. rdesc[20] != 0x29 || rdesc[30] != 0x75)
  44. return;
  45. hid_info(hdev, "Fixing up Elecom mouse button count\n");
  46. nbuttons = clamp(nbuttons, 0, MOUSE_BUTTONS_MAX);
  47. rdesc[13] = nbuttons;
  48. rdesc[21] = nbuttons;
  49. rdesc[31] = MOUSE_BUTTONS_MAX - nbuttons;
  50. }
  51. static __u8 *elecom_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  52. unsigned int *rsize)
  53. {
  54. switch (hdev->product) {
  55. case USB_DEVICE_ID_ELECOM_BM084:
  56. /* The BM084 Bluetooth mouse includes a non-existing horizontal
  57. * wheel in the HID descriptor. */
  58. if (*rsize >= 48 && rdesc[46] == 0x05 && rdesc[47] == 0x0c) {
  59. hid_info(hdev, "Fixing up Elecom BM084 report descriptor\n");
  60. rdesc[47] = 0x00;
  61. }
  62. break;
  63. case USB_DEVICE_ID_ELECOM_M_XT3URBK:
  64. case USB_DEVICE_ID_ELECOM_M_XT3DRBK:
  65. case USB_DEVICE_ID_ELECOM_M_XT4DRBK:
  66. mouse_button_fixup(hdev, rdesc, *rsize, 6);
  67. break;
  68. case USB_DEVICE_ID_ELECOM_M_DT1URBK:
  69. case USB_DEVICE_ID_ELECOM_M_DT1DRBK:
  70. case USB_DEVICE_ID_ELECOM_M_HT1URBK:
  71. case USB_DEVICE_ID_ELECOM_M_HT1DRBK:
  72. mouse_button_fixup(hdev, rdesc, *rsize, 8);
  73. break;
  74. }
  75. return rdesc;
  76. }
  77. static const struct hid_device_id elecom_devices[] = {
  78. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) },
  79. { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT3URBK) },
  80. { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT3DRBK) },
  81. { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_XT4DRBK) },
  82. { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_DT1URBK) },
  83. { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_DT1DRBK) },
  84. { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_HT1URBK) },
  85. { HID_USB_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_M_HT1DRBK) },
  86. { }
  87. };
  88. MODULE_DEVICE_TABLE(hid, elecom_devices);
  89. static struct hid_driver elecom_driver = {
  90. .name = "elecom",
  91. .id_table = elecom_devices,
  92. .report_fixup = elecom_report_fixup
  93. };
  94. module_hid_driver(elecom_driver);
  95. MODULE_LICENSE("GPL");