hid-emsff.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Force feedback support for EMS Trio Linker Plus II
  3. *
  4. * Copyright (c) 2010 Ignaz Forster <ignaz.forster@gmx.de>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/hid.h>
  22. #include <linux/input.h>
  23. #include <linux/module.h>
  24. #include "hid-ids.h"
  25. struct emsff_device {
  26. struct hid_report *report;
  27. };
  28. static int emsff_play(struct input_dev *dev, void *data,
  29. struct ff_effect *effect)
  30. {
  31. struct hid_device *hid = input_get_drvdata(dev);
  32. struct emsff_device *emsff = data;
  33. int weak, strong;
  34. weak = effect->u.rumble.weak_magnitude;
  35. strong = effect->u.rumble.strong_magnitude;
  36. dbg_hid("called with 0x%04x 0x%04x\n", strong, weak);
  37. weak = weak * 0xff / 0xffff;
  38. strong = strong * 0xff / 0xffff;
  39. emsff->report->field[0]->value[1] = weak;
  40. emsff->report->field[0]->value[2] = strong;
  41. dbg_hid("running with 0x%02x 0x%02x\n", strong, weak);
  42. hid_hw_request(hid, emsff->report, HID_REQ_SET_REPORT);
  43. return 0;
  44. }
  45. static int emsff_init(struct hid_device *hid)
  46. {
  47. struct emsff_device *emsff;
  48. struct hid_report *report;
  49. struct hid_input *hidinput;
  50. struct list_head *report_list =
  51. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  52. struct input_dev *dev;
  53. int error;
  54. if (list_empty(&hid->inputs)) {
  55. hid_err(hid, "no inputs found\n");
  56. return -ENODEV;
  57. }
  58. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  59. dev = hidinput->input;
  60. if (list_empty(report_list)) {
  61. hid_err(hid, "no output reports found\n");
  62. return -ENODEV;
  63. }
  64. report = list_first_entry(report_list, struct hid_report, list);
  65. if (report->maxfield < 1) {
  66. hid_err(hid, "no fields in the report\n");
  67. return -ENODEV;
  68. }
  69. if (report->field[0]->report_count < 7) {
  70. hid_err(hid, "not enough values in the field\n");
  71. return -ENODEV;
  72. }
  73. emsff = kzalloc(sizeof(struct emsff_device), GFP_KERNEL);
  74. if (!emsff)
  75. return -ENOMEM;
  76. set_bit(FF_RUMBLE, dev->ffbit);
  77. error = input_ff_create_memless(dev, emsff, emsff_play);
  78. if (error) {
  79. kfree(emsff);
  80. return error;
  81. }
  82. emsff->report = report;
  83. emsff->report->field[0]->value[0] = 0x01;
  84. emsff->report->field[0]->value[1] = 0x00;
  85. emsff->report->field[0]->value[2] = 0x00;
  86. emsff->report->field[0]->value[3] = 0x00;
  87. emsff->report->field[0]->value[4] = 0x00;
  88. emsff->report->field[0]->value[5] = 0x00;
  89. emsff->report->field[0]->value[6] = 0x00;
  90. hid_hw_request(hid, emsff->report, HID_REQ_SET_REPORT);
  91. hid_info(hid, "force feedback for EMS based devices by Ignaz Forster <ignaz.forster@gmx.de>\n");
  92. return 0;
  93. }
  94. static int ems_probe(struct hid_device *hdev, const struct hid_device_id *id)
  95. {
  96. int ret;
  97. ret = hid_parse(hdev);
  98. if (ret) {
  99. hid_err(hdev, "parse failed\n");
  100. goto err;
  101. }
  102. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  103. if (ret) {
  104. hid_err(hdev, "hw start failed\n");
  105. goto err;
  106. }
  107. ret = emsff_init(hdev);
  108. if (ret) {
  109. dev_err(&hdev->dev, "force feedback init failed\n");
  110. hid_hw_stop(hdev);
  111. goto err;
  112. }
  113. return 0;
  114. err:
  115. return ret;
  116. }
  117. static const struct hid_device_id ems_devices[] = {
  118. { HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
  119. { }
  120. };
  121. MODULE_DEVICE_TABLE(hid, ems_devices);
  122. static struct hid_driver ems_driver = {
  123. .name = "hkems",
  124. .id_table = ems_devices,
  125. .probe = ems_probe,
  126. };
  127. module_hid_driver(ems_driver);
  128. MODULE_LICENSE("GPL");