hid-google-stadiaff.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Stadia controller rumble support.
  4. *
  5. * Copyright 2023 Google LLC
  6. */
  7. #include <linux/hid.h>
  8. #include <linux/input.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include "hid-ids.h"
  12. #define STADIA_FF_REPORT_ID 5
  13. struct stadiaff_device {
  14. struct hid_device *hid;
  15. struct hid_report *report;
  16. spinlock_t lock;
  17. bool removed;
  18. uint16_t strong_magnitude;
  19. uint16_t weak_magnitude;
  20. struct work_struct work;
  21. };
  22. static void stadiaff_work(struct work_struct *work)
  23. {
  24. struct stadiaff_device *stadiaff =
  25. container_of(work, struct stadiaff_device, work);
  26. struct hid_field *rumble_field = stadiaff->report->field[0];
  27. unsigned long flags;
  28. spin_lock_irqsave(&stadiaff->lock, flags);
  29. rumble_field->value[0] = stadiaff->strong_magnitude;
  30. rumble_field->value[1] = stadiaff->weak_magnitude;
  31. spin_unlock_irqrestore(&stadiaff->lock, flags);
  32. hid_hw_request(stadiaff->hid, stadiaff->report, HID_REQ_SET_REPORT);
  33. }
  34. static int stadiaff_play(struct input_dev *dev, void *data,
  35. struct ff_effect *effect)
  36. {
  37. struct hid_device *hid = input_get_drvdata(dev);
  38. struct stadiaff_device *stadiaff = hid_get_drvdata(hid);
  39. unsigned long flags;
  40. spin_lock_irqsave(&stadiaff->lock, flags);
  41. if (!stadiaff->removed) {
  42. stadiaff->strong_magnitude = effect->u.rumble.strong_magnitude;
  43. stadiaff->weak_magnitude = effect->u.rumble.weak_magnitude;
  44. schedule_work(&stadiaff->work);
  45. }
  46. spin_unlock_irqrestore(&stadiaff->lock, flags);
  47. return 0;
  48. }
  49. static int stadiaff_init(struct hid_device *hid)
  50. {
  51. struct stadiaff_device *stadiaff;
  52. struct hid_report *report;
  53. struct hid_input *hidinput;
  54. struct input_dev *dev;
  55. int error;
  56. if (list_empty(&hid->inputs)) {
  57. hid_err(hid, "no inputs found\n");
  58. return -ENODEV;
  59. }
  60. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  61. dev = hidinput->input;
  62. report = hid_validate_values(hid, HID_OUTPUT_REPORT,
  63. STADIA_FF_REPORT_ID, 0, 2);
  64. if (!report)
  65. return -ENODEV;
  66. stadiaff = devm_kzalloc(&hid->dev, sizeof(struct stadiaff_device),
  67. GFP_KERNEL);
  68. if (!stadiaff)
  69. return -ENOMEM;
  70. hid_set_drvdata(hid, stadiaff);
  71. input_set_capability(dev, EV_FF, FF_RUMBLE);
  72. error = input_ff_create_memless(dev, NULL, stadiaff_play);
  73. if (error)
  74. return error;
  75. stadiaff->removed = false;
  76. stadiaff->hid = hid;
  77. stadiaff->report = report;
  78. INIT_WORK(&stadiaff->work, stadiaff_work);
  79. spin_lock_init(&stadiaff->lock);
  80. hid_info(hid, "Force Feedback for Google Stadia controller\n");
  81. return 0;
  82. }
  83. static int stadia_probe(struct hid_device *hdev, const struct hid_device_id *id)
  84. {
  85. int ret;
  86. ret = hid_parse(hdev);
  87. if (ret) {
  88. hid_err(hdev, "parse failed\n");
  89. return ret;
  90. }
  91. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  92. if (ret) {
  93. hid_err(hdev, "hw start failed\n");
  94. return ret;
  95. }
  96. ret = stadiaff_init(hdev);
  97. if (ret) {
  98. hid_err(hdev, "force feedback init failed\n");
  99. hid_hw_stop(hdev);
  100. return ret;
  101. }
  102. return 0;
  103. }
  104. static void stadia_remove(struct hid_device *hid)
  105. {
  106. struct stadiaff_device *stadiaff = hid_get_drvdata(hid);
  107. unsigned long flags;
  108. spin_lock_irqsave(&stadiaff->lock, flags);
  109. stadiaff->removed = true;
  110. spin_unlock_irqrestore(&stadiaff->lock, flags);
  111. cancel_work_sync(&stadiaff->work);
  112. hid_hw_stop(hid);
  113. }
  114. static const struct hid_device_id stadia_devices[] = {
  115. { HID_USB_DEVICE(USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STADIA) },
  116. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STADIA) },
  117. { }
  118. };
  119. MODULE_DEVICE_TABLE(hid, stadia_devices);
  120. static struct hid_driver stadia_driver = {
  121. .name = "stadia",
  122. .id_table = stadia_devices,
  123. .probe = stadia_probe,
  124. .remove = stadia_remove,
  125. };
  126. module_hid_driver(stadia_driver);
  127. MODULE_DESCRIPTION("Google Stadia controller rumble support.");
  128. MODULE_LICENSE("GPL");