hid-betopff.c 3.9 KB

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