hid-glorious.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * USB HID driver for Glorious PC Gaming Race
  4. * Glorious Model O, O- and D mice.
  5. *
  6. * Copyright (c) 2020 Samuel Čavoj <sammko@sammserver.com>
  7. */
  8. /*
  9. */
  10. #include <linux/hid.h>
  11. #include <linux/module.h>
  12. #include "hid-ids.h"
  13. MODULE_AUTHOR("Samuel Čavoj <sammko@sammserver.com>");
  14. MODULE_DESCRIPTION("HID driver for Glorious PC Gaming Race mice");
  15. /*
  16. * Glorious Model O and O- specify the const flag in the consumer input
  17. * report descriptor, which leads to inputs being ignored. Fix this
  18. * by patching the descriptor.
  19. *
  20. * Glorious Model I incorrectly specifes the Usage Minimum for its
  21. * keyboard HID report, causing keycodes to be misinterpreted.
  22. * Fix this by setting Usage Minimum to 0 in that report.
  23. */
  24. static const __u8 *glorious_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  25. unsigned int *rsize)
  26. {
  27. if (*rsize == 213 &&
  28. rdesc[84] == 129 && rdesc[112] == 129 && rdesc[140] == 129 &&
  29. rdesc[85] == 3 && rdesc[113] == 3 && rdesc[141] == 3) {
  30. hid_info(hdev, "patching Glorious Model O consumer control report descriptor\n");
  31. rdesc[85] = rdesc[113] = rdesc[141] = \
  32. HID_MAIN_ITEM_VARIABLE | HID_MAIN_ITEM_RELATIVE;
  33. }
  34. if (*rsize == 156 && rdesc[41] == 1) {
  35. hid_info(hdev, "patching Glorious Model I keyboard report descriptor\n");
  36. rdesc[41] = 0;
  37. }
  38. return rdesc;
  39. }
  40. static void glorious_update_name(struct hid_device *hdev)
  41. {
  42. const char *model = "Device";
  43. switch (hdev->product) {
  44. case USB_DEVICE_ID_GLORIOUS_MODEL_O:
  45. model = "Model O"; break;
  46. case USB_DEVICE_ID_GLORIOUS_MODEL_D:
  47. model = "Model D"; break;
  48. case USB_DEVICE_ID_GLORIOUS_MODEL_I:
  49. model = "Model I"; break;
  50. }
  51. snprintf(hdev->name, sizeof(hdev->name), "%s %s", "Glorious", model);
  52. }
  53. static int glorious_probe(struct hid_device *hdev,
  54. const struct hid_device_id *id)
  55. {
  56. int ret;
  57. hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
  58. ret = hid_parse(hdev);
  59. if (ret)
  60. return ret;
  61. glorious_update_name(hdev);
  62. return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  63. }
  64. static const struct hid_device_id glorious_devices[] = {
  65. { HID_USB_DEVICE(USB_VENDOR_ID_SINOWEALTH,
  66. USB_DEVICE_ID_GLORIOUS_MODEL_O) },
  67. { HID_USB_DEVICE(USB_VENDOR_ID_SINOWEALTH,
  68. USB_DEVICE_ID_GLORIOUS_MODEL_D) },
  69. { HID_USB_DEVICE(USB_VENDOR_ID_LAVIEW,
  70. USB_DEVICE_ID_GLORIOUS_MODEL_I) },
  71. { }
  72. };
  73. MODULE_DEVICE_TABLE(hid, glorious_devices);
  74. static struct hid_driver glorious_driver = {
  75. .name = "glorious",
  76. .id_table = glorious_devices,
  77. .probe = glorious_probe,
  78. .report_fixup = glorious_report_fixup
  79. };
  80. module_hid_driver(glorious_driver);
  81. MODULE_LICENSE("GPL");