hid-betopff.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for Betop based devices
  4. *
  5. * The devices are distributed under various names and the same USB device ID
  6. * can be used in both adapters and actual game controllers.
  7. *
  8. * 0x11c2:0x2208 "BTP2185 BFM mode Joystick"
  9. * - tested with BTP2185 BFM Mode.
  10. *
  11. * 0x11C0:0x5506 "BTP2185 PC mode Joystick"
  12. * - tested with BTP2185 PC Mode.
  13. *
  14. * 0x8380:0x1850 "BTP2185 V2 PC mode USB Gamepad"
  15. * - tested with BTP2185 PC Mode with another version.
  16. *
  17. * 0x20bc:0x5500 "BTP2185 V2 BFM mode Joystick"
  18. * - tested with BTP2171s.
  19. * Copyright (c) 2014 Huang Bo <[email protected]>
  20. */
  21. /*
  22. */
  23. #include <linux/input.h>
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <linux/hid.h>
  27. #include "hid-ids.h"
  28. struct betopff_device {
  29. struct hid_report *report;
  30. };
  31. static int hid_betopff_play(struct input_dev *dev, void *data,
  32. struct ff_effect *effect)
  33. {
  34. struct hid_device *hid = input_get_drvdata(dev);
  35. struct betopff_device *betopff = data;
  36. __u16 left, right;
  37. left = effect->u.rumble.strong_magnitude;
  38. right = effect->u.rumble.weak_magnitude;
  39. betopff->report->field[2]->value[0] = left / 256;
  40. betopff->report->field[3]->value[0] = right / 256;
  41. hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT);
  42. return 0;
  43. }
  44. static int betopff_init(struct hid_device *hid)
  45. {
  46. struct betopff_device *betopff;
  47. struct hid_report *report;
  48. struct hid_input *hidinput;
  49. struct list_head *report_list =
  50. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  51. struct input_dev *dev;
  52. int error;
  53. int i, j;
  54. if (list_empty(&hid->inputs)) {
  55. hid_err(hid, "no inputs found\n");
  56. return -ENODEV;
  57. }
  58. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  59. dev = hidinput->input;
  60. if (list_empty(report_list)) {
  61. hid_err(hid, "no output reports found\n");
  62. return -ENODEV;
  63. }
  64. report = list_first_entry(report_list, struct hid_report, list);
  65. /*
  66. * Actually there are 4 fields for 4 Bytes as below:
  67. * -----------------------------------------
  68. * Byte0 Byte1 Byte2 Byte3
  69. * 0x00 0x00 left_motor right_motor
  70. * -----------------------------------------
  71. * Do init them with default value.
  72. */
  73. if (report->maxfield < 4) {
  74. hid_err(hid, "not enough fields in the report: %d\n",
  75. report->maxfield);
  76. return -ENODEV;
  77. }
  78. for (i = 0; i < report->maxfield; i++) {
  79. if (report->field[i]->report_count < 1) {
  80. hid_err(hid, "no values in the field\n");
  81. return -ENODEV;
  82. }
  83. for (j = 0; j < report->field[i]->report_count; j++) {
  84. report->field[i]->value[j] = 0x00;
  85. }
  86. }
  87. betopff = kzalloc(sizeof(*betopff), GFP_KERNEL);
  88. if (!betopff)
  89. return -ENOMEM;
  90. set_bit(FF_RUMBLE, dev->ffbit);
  91. error = input_ff_create_memless(dev, betopff, hid_betopff_play);
  92. if (error) {
  93. kfree(betopff);
  94. return error;
  95. }
  96. betopff->report = report;
  97. hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT);
  98. hid_info(hid, "Force feedback for betop devices by huangbo <[email protected]>\n");
  99. return 0;
  100. }
  101. static int betop_probe(struct hid_device *hdev, const struct hid_device_id *id)
  102. {
  103. int ret;
  104. if (id->driver_data)
  105. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  106. ret = hid_parse(hdev);
  107. if (ret) {
  108. hid_err(hdev, "parse failed\n");
  109. goto err;
  110. }
  111. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  112. if (ret) {
  113. hid_err(hdev, "hw start failed\n");
  114. goto err;
  115. }
  116. betopff_init(hdev);
  117. return 0;
  118. err:
  119. return ret;
  120. }
  121. static const struct hid_device_id betop_devices[] = {
  122. { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185BFM, 0x2208) },
  123. { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185PC, 0x5506) },
  124. { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2PC, 0x1850) },
  125. { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2BFM, 0x5500) },
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(hid, betop_devices);
  129. static struct hid_driver betop_driver = {
  130. .name = "betop",
  131. .id_table = betop_devices,
  132. .probe = betop_probe,
  133. };
  134. module_hid_driver(betop_driver);
  135. MODULE_LICENSE("GPL");