hid-megaworld.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Vibration support for Mega World controllers
  4. *
  5. * Copyright 2022 Frank Zago
  6. *
  7. * Derived from hid-zpff.c:
  8. * Copyright (c) 2005, 2006 Anssi Hannula <[email protected]>
  9. */
  10. #include <linux/hid.h>
  11. #include <linux/input.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include "hid-ids.h"
  15. struct mwctrl_device {
  16. struct hid_report *report;
  17. s32 *weak;
  18. s32 *strong;
  19. };
  20. static int mwctrl_play(struct input_dev *dev, void *data,
  21. struct ff_effect *effect)
  22. {
  23. struct hid_device *hid = input_get_drvdata(dev);
  24. struct mwctrl_device *mwctrl = data;
  25. *mwctrl->strong = effect->u.rumble.strong_magnitude >> 8;
  26. *mwctrl->weak = effect->u.rumble.weak_magnitude >> 8;
  27. hid_hw_request(hid, mwctrl->report, HID_REQ_SET_REPORT);
  28. return 0;
  29. }
  30. static int mwctrl_init(struct hid_device *hid)
  31. {
  32. struct mwctrl_device *mwctrl;
  33. struct hid_report *report;
  34. struct hid_input *hidinput;
  35. struct input_dev *dev;
  36. int error;
  37. int i;
  38. if (list_empty(&hid->inputs)) {
  39. hid_err(hid, "no inputs found\n");
  40. return -ENODEV;
  41. }
  42. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  43. dev = hidinput->input;
  44. for (i = 0; i < 4; i++) {
  45. report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, i, 1);
  46. if (!report)
  47. return -ENODEV;
  48. }
  49. mwctrl = kzalloc(sizeof(struct mwctrl_device), GFP_KERNEL);
  50. if (!mwctrl)
  51. return -ENOMEM;
  52. set_bit(FF_RUMBLE, dev->ffbit);
  53. error = input_ff_create_memless(dev, mwctrl, mwctrl_play);
  54. if (error) {
  55. kfree(mwctrl);
  56. return error;
  57. }
  58. mwctrl->report = report;
  59. /* Field 0 is always 2, and field 1 is always 0. The original
  60. * windows driver has a 5 bytes command, where the 5th byte is
  61. * a repeat of the 3rd byte, however the device has only 4
  62. * fields. It could be a bug in the driver, or there is a
  63. * different device that needs it.
  64. */
  65. report->field[0]->value[0] = 0x02;
  66. mwctrl->strong = &report->field[2]->value[0];
  67. mwctrl->weak = &report->field[3]->value[0];
  68. return 0;
  69. }
  70. static int mwctrl_probe(struct hid_device *hdev, const struct hid_device_id *id)
  71. {
  72. int ret;
  73. ret = hid_parse(hdev);
  74. if (ret) {
  75. hid_err(hdev, "parse failed\n");
  76. return ret;
  77. }
  78. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  79. if (ret) {
  80. hid_err(hdev, "hw start failed\n");
  81. return ret;
  82. }
  83. ret = mwctrl_init(hdev);
  84. if (ret)
  85. hid_hw_stop(hdev);
  86. return ret;
  87. }
  88. static const struct hid_device_id mwctrl_devices[] = {
  89. { HID_USB_DEVICE(USB_VENDOR_MEGAWORLD,
  90. USB_DEVICE_ID_MEGAWORLD_GAMEPAD) },
  91. { }
  92. };
  93. MODULE_DEVICE_TABLE(hid, mwctrl_devices);
  94. static struct hid_driver mwctrl_driver = {
  95. .name = "megaworld",
  96. .id_table = mwctrl_devices,
  97. .probe = mwctrl_probe,
  98. };
  99. module_hid_driver(mwctrl_driver);
  100. MODULE_LICENSE("GPL");