hid-emsff.c 4.0 KB

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