hid-mf.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Force feedback support for Mayflash game controller adapters.
  3. *
  4. * These devices are manufactured by Mayflash but identify themselves
  5. * using the vendor ID of DragonRise Inc.
  6. *
  7. * Tested with:
  8. * 0079:1801 "DragonRise Inc. Mayflash PS3 Game Controller Adapter"
  9. * 0079:1803 "DragonRise Inc. Mayflash Wireless Sensor DolphinBar"
  10. * 0079:1843 "DragonRise Inc. Mayflash GameCube Game Controller Adapter"
  11. * 0079:1844 "DragonRise Inc. Mayflash GameCube Game Controller Adapter (v04)"
  12. *
  13. * The following adapters probably work too, but need to be tested:
  14. * 0079:1800 "DragonRise Inc. Mayflash WIIU Game Controller Adapter"
  15. *
  16. * Copyright (c) 2016-2017 Marcel Hasler <mahasler@gmail.com>
  17. */
  18. /*
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. */
  29. #include <linux/input.h>
  30. #include <linux/slab.h>
  31. #include <linux/hid.h>
  32. #include <linux/module.h>
  33. #include "hid-ids.h"
  34. struct mf_device {
  35. struct hid_report *report;
  36. };
  37. static int mf_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  38. {
  39. struct hid_device *hid = input_get_drvdata(dev);
  40. struct mf_device *mf = data;
  41. int strong, weak;
  42. strong = effect->u.rumble.strong_magnitude;
  43. weak = effect->u.rumble.weak_magnitude;
  44. dbg_hid("Called with 0x%04x 0x%04x.\n", strong, weak);
  45. strong = strong * 0xff / 0xffff;
  46. weak = weak * 0xff / 0xffff;
  47. dbg_hid("Running with 0x%02x 0x%02x.\n", strong, weak);
  48. mf->report->field[0]->value[0] = weak;
  49. mf->report->field[0]->value[1] = strong;
  50. hid_hw_request(hid, mf->report, HID_REQ_SET_REPORT);
  51. return 0;
  52. }
  53. static int mf_init(struct hid_device *hid)
  54. {
  55. struct mf_device *mf;
  56. struct list_head *report_list =
  57. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  58. struct list_head *report_ptr;
  59. struct hid_report *report;
  60. struct list_head *input_ptr = &hid->inputs;
  61. struct hid_input *input;
  62. struct input_dev *dev;
  63. int error;
  64. /* Setup each of the four inputs */
  65. list_for_each(report_ptr, report_list) {
  66. report = list_entry(report_ptr, struct hid_report, list);
  67. if (report->maxfield < 1 || report->field[0]->report_count < 2) {
  68. hid_err(hid, "Invalid report, this should never happen!\n");
  69. return -ENODEV;
  70. }
  71. if (list_is_last(input_ptr, &hid->inputs)) {
  72. hid_err(hid, "Missing input, this should never happen!\n");
  73. return -ENODEV;
  74. }
  75. input_ptr = input_ptr->next;
  76. input = list_entry(input_ptr, struct hid_input, list);
  77. mf = kzalloc(sizeof(struct mf_device), GFP_KERNEL);
  78. if (!mf)
  79. return -ENOMEM;
  80. dev = input->input;
  81. set_bit(FF_RUMBLE, dev->ffbit);
  82. error = input_ff_create_memless(dev, mf, mf_play);
  83. if (error) {
  84. kfree(mf);
  85. return error;
  86. }
  87. mf->report = report;
  88. mf->report->field[0]->value[0] = 0x00;
  89. mf->report->field[0]->value[1] = 0x00;
  90. hid_hw_request(hid, mf->report, HID_REQ_SET_REPORT);
  91. }
  92. hid_info(hid, "Force feedback for HJZ Mayflash game controller "
  93. "adapters by Marcel Hasler <mahasler@gmail.com>\n");
  94. return 0;
  95. }
  96. static int mf_probe(struct hid_device *hid, const struct hid_device_id *id)
  97. {
  98. int error;
  99. dev_dbg(&hid->dev, "Mayflash HID hardware probe...\n");
  100. /* Apply quirks as needed */
  101. hid->quirks |= id->driver_data;
  102. error = hid_parse(hid);
  103. if (error) {
  104. hid_err(hid, "HID parse failed.\n");
  105. return error;
  106. }
  107. error = hid_hw_start(hid, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  108. if (error) {
  109. hid_err(hid, "HID hw start failed\n");
  110. return error;
  111. }
  112. error = mf_init(hid);
  113. if (error) {
  114. hid_err(hid, "Force feedback init failed.\n");
  115. hid_hw_stop(hid);
  116. return error;
  117. }
  118. return 0;
  119. }
  120. static const struct hid_device_id mf_devices[] = {
  121. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_PS3),
  122. .driver_data = HID_QUIRK_MULTI_INPUT },
  123. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_DOLPHINBAR),
  124. .driver_data = HID_QUIRK_MULTI_INPUT },
  125. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_GAMECUBE1),
  126. .driver_data = HID_QUIRK_MULTI_INPUT },
  127. { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_GAMECUBE2),
  128. .driver_data = 0 }, /* No quirk required */
  129. { }
  130. };
  131. MODULE_DEVICE_TABLE(hid, mf_devices);
  132. static struct hid_driver mf_driver = {
  133. .name = "hid_mf",
  134. .id_table = mf_devices,
  135. .probe = mf_probe,
  136. };
  137. module_hid_driver(mf_driver);
  138. MODULE_LICENSE("GPL");