hid-emsff.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for EMS Trio Linker Plus II
  4. *
  5. * Copyright (c) 2010 Ignaz Forster <ignaz.forster@gmx.de>
  6. */
  7. /*
  8. */
  9. #include <linux/hid.h>
  10. #include <linux/input.h>
  11. #include <linux/module.h>
  12. #include "hid-ids.h"
  13. struct emsff_device {
  14. struct hid_report *report;
  15. };
  16. static int emsff_play(struct input_dev *dev, void *data,
  17. struct ff_effect *effect)
  18. {
  19. struct hid_device *hid = input_get_drvdata(dev);
  20. struct emsff_device *emsff = data;
  21. int weak, strong;
  22. weak = effect->u.rumble.weak_magnitude;
  23. strong = effect->u.rumble.strong_magnitude;
  24. dbg_hid("called with 0x%04x 0x%04x\n", strong, weak);
  25. weak = weak * 0xff / 0xffff;
  26. strong = strong * 0xff / 0xffff;
  27. emsff->report->field[0]->value[1] = weak;
  28. emsff->report->field[0]->value[2] = strong;
  29. dbg_hid("running with 0x%02x 0x%02x\n", strong, weak);
  30. hid_hw_request(hid, emsff->report, HID_REQ_SET_REPORT);
  31. return 0;
  32. }
  33. static int emsff_init(struct hid_device *hid)
  34. {
  35. struct emsff_device *emsff;
  36. struct hid_report *report;
  37. struct hid_input *hidinput;
  38. struct list_head *report_list =
  39. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  40. struct input_dev *dev;
  41. int error;
  42. if (list_empty(&hid->inputs)) {
  43. hid_err(hid, "no inputs found\n");
  44. return -ENODEV;
  45. }
  46. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  47. dev = hidinput->input;
  48. if (list_empty(report_list)) {
  49. hid_err(hid, "no output reports found\n");
  50. return -ENODEV;
  51. }
  52. report = list_first_entry(report_list, struct hid_report, list);
  53. if (report->maxfield < 1) {
  54. hid_err(hid, "no fields in the report\n");
  55. return -ENODEV;
  56. }
  57. if (report->field[0]->report_count < 7) {
  58. hid_err(hid, "not enough values in the field\n");
  59. return -ENODEV;
  60. }
  61. emsff = kzalloc(sizeof(struct emsff_device), GFP_KERNEL);
  62. if (!emsff)
  63. return -ENOMEM;
  64. set_bit(FF_RUMBLE, dev->ffbit);
  65. error = input_ff_create_memless(dev, emsff, emsff_play);
  66. if (error) {
  67. kfree(emsff);
  68. return error;
  69. }
  70. emsff->report = report;
  71. emsff->report->field[0]->value[0] = 0x01;
  72. emsff->report->field[0]->value[1] = 0x00;
  73. emsff->report->field[0]->value[2] = 0x00;
  74. emsff->report->field[0]->value[3] = 0x00;
  75. emsff->report->field[0]->value[4] = 0x00;
  76. emsff->report->field[0]->value[5] = 0x00;
  77. emsff->report->field[0]->value[6] = 0x00;
  78. hid_hw_request(hid, emsff->report, HID_REQ_SET_REPORT);
  79. hid_info(hid, "force feedback for EMS based devices by Ignaz Forster <ignaz.forster@gmx.de>\n");
  80. return 0;
  81. }
  82. static int ems_probe(struct hid_device *hdev, const struct hid_device_id *id)
  83. {
  84. int ret;
  85. ret = hid_parse(hdev);
  86. if (ret) {
  87. hid_err(hdev, "parse failed\n");
  88. goto err;
  89. }
  90. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  91. if (ret) {
  92. hid_err(hdev, "hw start failed\n");
  93. goto err;
  94. }
  95. ret = emsff_init(hdev);
  96. if (ret) {
  97. dev_err(&hdev->dev, "force feedback init failed\n");
  98. hid_hw_stop(hdev);
  99. goto err;
  100. }
  101. return 0;
  102. err:
  103. return ret;
  104. }
  105. static const struct hid_device_id ems_devices[] = {
  106. { HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
  107. { }
  108. };
  109. MODULE_DEVICE_TABLE(hid, ems_devices);
  110. static struct hid_driver ems_driver = {
  111. .name = "hkems",
  112. .id_table = ems_devices,
  113. .probe = ems_probe,
  114. };
  115. module_hid_driver(ems_driver);
  116. MODULE_LICENSE("GPL");