hid-emsff.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/usb.h>
  24. #include <linux/module.h>
  25. #include "hid-ids.h"
  26. #include "usbhid/usbhid.h"
  27. struct emsff_device {
  28. struct hid_report *report;
  29. };
  30. static int emsff_play(struct input_dev *dev, void *data,
  31. struct ff_effect *effect)
  32. {
  33. struct hid_device *hid = input_get_drvdata(dev);
  34. struct emsff_device *emsff = data;
  35. int weak, strong;
  36. weak = effect->u.rumble.weak_magnitude;
  37. strong = effect->u.rumble.strong_magnitude;
  38. dbg_hid("called with 0x%04x 0x%04x\n", strong, weak);
  39. weak = weak * 0xff / 0xffff;
  40. strong = strong * 0xff / 0xffff;
  41. emsff->report->field[0]->value[1] = weak;
  42. emsff->report->field[0]->value[2] = strong;
  43. dbg_hid("running with 0x%02x 0x%02x\n", strong, weak);
  44. usbhid_submit_report(hid, emsff->report, USB_DIR_OUT);
  45. return 0;
  46. }
  47. static int emsff_init(struct hid_device *hid)
  48. {
  49. struct emsff_device *emsff;
  50. struct hid_report *report;
  51. struct hid_input *hidinput;
  52. struct list_head *report_list =
  53. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  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_first_entry(&hid->inputs, struct hid_input, list);
  61. dev = hidinput->input;
  62. if (list_empty(report_list)) {
  63. hid_err(hid, "no output reports found\n");
  64. return -ENODEV;
  65. }
  66. report = list_first_entry(report_list, struct hid_report, list);
  67. if (report->maxfield < 1) {
  68. hid_err(hid, "no fields in the report\n");
  69. return -ENODEV;
  70. }
  71. if (report->field[0]->report_count < 7) {
  72. hid_err(hid, "not enough values in the field\n");
  73. return -ENODEV;
  74. }
  75. emsff = kzalloc(sizeof(struct emsff_device), GFP_KERNEL);
  76. if (!emsff)
  77. return -ENOMEM;
  78. set_bit(FF_RUMBLE, dev->ffbit);
  79. error = input_ff_create_memless(dev, emsff, emsff_play);
  80. if (error) {
  81. kfree(emsff);
  82. return error;
  83. }
  84. emsff->report = report;
  85. emsff->report->field[0]->value[0] = 0x01;
  86. emsff->report->field[0]->value[1] = 0x00;
  87. emsff->report->field[0]->value[2] = 0x00;
  88. emsff->report->field[0]->value[3] = 0x00;
  89. emsff->report->field[0]->value[4] = 0x00;
  90. emsff->report->field[0]->value[5] = 0x00;
  91. emsff->report->field[0]->value[6] = 0x00;
  92. usbhid_submit_report(hid, emsff->report, USB_DIR_OUT);
  93. hid_info(hid, "force feedback for EMS based devices by Ignaz Forster <ignaz.forster@gmx.de>\n");
  94. return 0;
  95. }
  96. static int ems_probe(struct hid_device *hdev, const struct hid_device_id *id)
  97. {
  98. int ret;
  99. ret = hid_parse(hdev);
  100. if (ret) {
  101. hid_err(hdev, "parse failed\n");
  102. goto err;
  103. }
  104. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  105. if (ret) {
  106. hid_err(hdev, "hw start failed\n");
  107. goto err;
  108. }
  109. ret = emsff_init(hdev);
  110. if (ret) {
  111. dev_err(&hdev->dev, "force feedback init failed\n");
  112. hid_hw_stop(hdev);
  113. goto err;
  114. }
  115. return 0;
  116. err:
  117. return ret;
  118. }
  119. static const struct hid_device_id ems_devices[] = {
  120. { HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
  121. { }
  122. };
  123. MODULE_DEVICE_TABLE(hid, ems_devices);
  124. static struct hid_driver ems_driver = {
  125. .name = "hkems",
  126. .id_table = ems_devices,
  127. .probe = ems_probe,
  128. };
  129. static int ems_init(void)
  130. {
  131. return hid_register_driver(&ems_driver);
  132. }
  133. static void ems_exit(void)
  134. {
  135. hid_unregister_driver(&ems_driver);
  136. }
  137. module_init(ems_init);
  138. module_exit(ems_exit);
  139. MODULE_LICENSE("GPL");