hid-emsff.c 3.8 KB

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