hid-gembird.c 3.4 KB

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