hid-emsff.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 = list_first_entry(&hid->inputs,
  50. struct hid_input, list);
  51. struct list_head *report_list =
  52. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  53. struct input_dev *dev = hidinput->input;
  54. int error;
  55. if (list_empty(report_list)) {
  56. hid_err(hid, "no output reports found\n");
  57. return -ENODEV;
  58. }
  59. report = list_first_entry(report_list, struct hid_report, list);
  60. if (report->maxfield < 1) {
  61. hid_err(hid, "no fields in the report\n");
  62. return -ENODEV;
  63. }
  64. if (report->field[0]->report_count < 7) {
  65. hid_err(hid, "not enough values in the field\n");
  66. return -ENODEV;
  67. }
  68. emsff = kzalloc(sizeof(struct emsff_device), GFP_KERNEL);
  69. if (!emsff)
  70. return -ENOMEM;
  71. set_bit(FF_RUMBLE, dev->ffbit);
  72. error = input_ff_create_memless(dev, emsff, emsff_play);
  73. if (error) {
  74. kfree(emsff);
  75. return error;
  76. }
  77. emsff->report = report;
  78. emsff->report->field[0]->value[0] = 0x01;
  79. emsff->report->field[0]->value[1] = 0x00;
  80. emsff->report->field[0]->value[2] = 0x00;
  81. emsff->report->field[0]->value[3] = 0x00;
  82. emsff->report->field[0]->value[4] = 0x00;
  83. emsff->report->field[0]->value[5] = 0x00;
  84. emsff->report->field[0]->value[6] = 0x00;
  85. hid_hw_request(hid, emsff->report, HID_REQ_SET_REPORT);
  86. hid_info(hid, "force feedback for EMS based devices by Ignaz Forster <ignaz.forster@gmx.de>\n");
  87. return 0;
  88. }
  89. static int ems_probe(struct hid_device *hdev, const struct hid_device_id *id)
  90. {
  91. int ret;
  92. ret = hid_parse(hdev);
  93. if (ret) {
  94. hid_err(hdev, "parse failed\n");
  95. goto err;
  96. }
  97. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  98. if (ret) {
  99. hid_err(hdev, "hw start failed\n");
  100. goto err;
  101. }
  102. ret = emsff_init(hdev);
  103. if (ret) {
  104. dev_err(&hdev->dev, "force feedback init failed\n");
  105. hid_hw_stop(hdev);
  106. goto err;
  107. }
  108. return 0;
  109. err:
  110. return ret;
  111. }
  112. static const struct hid_device_id ems_devices[] = {
  113. { HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
  114. { }
  115. };
  116. MODULE_DEVICE_TABLE(hid, ems_devices);
  117. static struct hid_driver ems_driver = {
  118. .name = "hkems",
  119. .id_table = ems_devices,
  120. .probe = ems_probe,
  121. };
  122. module_hid_driver(ems_driver);
  123. MODULE_LICENSE("GPL");