hid-gembird.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for Gembird Joypad, "PC Game Controller"
  4. *
  5. * Copyright (c) 2015 Red Hat, Inc
  6. * Copyright (c) 2015 Benjamin Tissoires
  7. */
  8. /*
  9. */
  10. #include <linux/device.h>
  11. #include <linux/hid.h>
  12. #include <linux/module.h>
  13. #include "hid-ids.h"
  14. #define GEMBIRD_START_FAULTY_RDESC 8
  15. static const __u8 gembird_jpd_faulty_rdesc[] = {
  16. 0x75, 0x08, /* Report Size (8) */
  17. 0x95, 0x05, /* Report Count (5) */
  18. 0x15, 0x00, /* Logical Minimum (0) */
  19. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  20. 0x35, 0x00, /* Physical Minimum (0) */
  21. 0x46, 0xff, 0x00, /* Physical Maximum (255) */
  22. 0x09, 0x30, /* Usage (X) */
  23. 0x09, 0x31, /* Usage (Y) */
  24. 0x09, 0x32, /* Usage (Z) */
  25. 0x09, 0x32, /* Usage (Z) */
  26. 0x09, 0x35, /* Usage (Rz) */
  27. 0x81, 0x02, /* Input (Data,Var,Abs) */
  28. };
  29. /*
  30. * we fix the report descriptor by:
  31. * - marking the first Z axis as constant (so it is ignored by HID)
  32. * - assign the original second Z to Rx
  33. * - assign the original Rz to Ry
  34. */
  35. static const __u8 gembird_jpd_fixed_rdesc[] = {
  36. 0x75, 0x08, /* Report Size (8) */
  37. 0x95, 0x02, /* Report Count (2) */
  38. 0x15, 0x00, /* Logical Minimum (0) */
  39. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  40. 0x35, 0x00, /* Physical Minimum (0) */
  41. 0x46, 0xff, 0x00, /* Physical Maximum (255) */
  42. 0x09, 0x30, /* Usage (X) */
  43. 0x09, 0x31, /* Usage (Y) */
  44. 0x81, 0x02, /* Input (Data,Var,Abs) */
  45. 0x95, 0x01, /* Report Count (1) */
  46. 0x09, 0x32, /* Usage (Z) */
  47. 0x81, 0x01, /* Input (Cnst,Arr,Abs) */
  48. 0x95, 0x02, /* Report Count (2) */
  49. 0x09, 0x33, /* Usage (Rx) */
  50. 0x09, 0x34, /* Usage (Ry) */
  51. 0x81, 0x02, /* Input (Data,Var,Abs) */
  52. };
  53. static __u8 *gembird_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  54. unsigned int *rsize)
  55. {
  56. __u8 *new_rdesc;
  57. /* delta_size is > 0 */
  58. size_t delta_size = sizeof(gembird_jpd_fixed_rdesc) -
  59. sizeof(gembird_jpd_faulty_rdesc);
  60. size_t new_size = *rsize + delta_size;
  61. if (*rsize >= 31 && !memcmp(&rdesc[GEMBIRD_START_FAULTY_RDESC],
  62. gembird_jpd_faulty_rdesc,
  63. sizeof(gembird_jpd_faulty_rdesc))) {
  64. new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
  65. if (new_rdesc == NULL)
  66. return rdesc;
  67. dev_info(&hdev->dev,
  68. "fixing Gembird JPD-DualForce 2 report descriptor.\n");
  69. /* start by copying the end of the rdesc */
  70. memcpy(new_rdesc + delta_size, rdesc, *rsize);
  71. /* add the correct beginning */
  72. memcpy(new_rdesc, rdesc, GEMBIRD_START_FAULTY_RDESC);
  73. /* replace the faulty part with the fixed one */
  74. memcpy(new_rdesc + GEMBIRD_START_FAULTY_RDESC,
  75. gembird_jpd_fixed_rdesc,
  76. sizeof(gembird_jpd_fixed_rdesc));
  77. *rsize = new_size;
  78. rdesc = new_rdesc;
  79. }
  80. return rdesc;
  81. }
  82. static const struct hid_device_id gembird_devices[] = {
  83. { HID_USB_DEVICE(USB_VENDOR_ID_GEMBIRD,
  84. USB_DEVICE_ID_GEMBIRD_JPD_DUALFORCE2) },
  85. { }
  86. };
  87. MODULE_DEVICE_TABLE(hid, gembird_devices);
  88. static struct hid_driver gembird_driver = {
  89. .name = "gembird",
  90. .id_table = gembird_devices,
  91. .report_fixup = gembird_report_fixup,
  92. };
  93. module_hid_driver(gembird_driver);
  94. MODULE_AUTHOR("Benjamin Tissoires <[email protected]>");
  95. MODULE_DESCRIPTION("HID Gembird joypad driver");
  96. MODULE_LICENSE("GPL");