hid-plff.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Force feedback support for PantherLord/GreenAsia based devices
  3. *
  4. * The devices are distributed under various names and the same USB device ID
  5. * can be used in both adapters and actual game controllers.
  6. *
  7. * 0810:0001 "Twin USB Joystick"
  8. * - tested with PantherLord USB/PS2 2in1 Adapter
  9. * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
  10. *
  11. * 0e8f:0003 "GreenAsia Inc. USB Joystick "
  12. * - tested with Köng Gaming gamepad
  13. *
  14. * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
  15. */
  16. /*
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. /* #define DEBUG */
  32. #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
  33. #include <linux/input.h>
  34. #include <linux/usb.h>
  35. #include <linux/hid.h>
  36. #include "usbhid.h"
  37. struct plff_device {
  38. struct hid_report *report;
  39. };
  40. static int hid_plff_play(struct input_dev *dev, void *data,
  41. struct ff_effect *effect)
  42. {
  43. struct hid_device *hid = input_get_drvdata(dev);
  44. struct plff_device *plff = data;
  45. int left, right;
  46. left = effect->u.rumble.strong_magnitude;
  47. right = effect->u.rumble.weak_magnitude;
  48. debug("called with 0x%04x 0x%04x", left, right);
  49. left = left * 0x7f / 0xffff;
  50. right = right * 0x7f / 0xffff;
  51. plff->report->field[0]->value[2] = left;
  52. plff->report->field[0]->value[3] = right;
  53. debug("running with 0x%02x 0x%02x", left, right);
  54. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  55. return 0;
  56. }
  57. int hid_plff_init(struct hid_device *hid)
  58. {
  59. struct plff_device *plff;
  60. struct hid_report *report;
  61. struct hid_input *hidinput;
  62. struct list_head *report_list =
  63. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  64. struct list_head *report_ptr = report_list;
  65. struct input_dev *dev;
  66. int error;
  67. /* The device contains one output report per physical device, all
  68. containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
  69. absolute values.
  70. The input reports also contain a field which contains
  71. 8 ff00.0001 usages and 8 boolean values. Their meaning is
  72. currently unknown. */
  73. if (list_empty(report_list)) {
  74. printk(KERN_ERR "hid-plff: no output reports found\n");
  75. return -ENODEV;
  76. }
  77. list_for_each_entry(hidinput, &hid->inputs, list) {
  78. report_ptr = report_ptr->next;
  79. if (report_ptr == report_list) {
  80. printk(KERN_ERR "hid-plff: required output report is missing\n");
  81. return -ENODEV;
  82. }
  83. report = list_entry(report_ptr, struct hid_report, list);
  84. if (report->maxfield < 1) {
  85. printk(KERN_ERR "hid-plff: no fields in the report\n");
  86. return -ENODEV;
  87. }
  88. if (report->field[0]->report_count < 4) {
  89. printk(KERN_ERR "hid-plff: not enough values in the field\n");
  90. return -ENODEV;
  91. }
  92. plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
  93. if (!plff)
  94. return -ENOMEM;
  95. dev = hidinput->input;
  96. set_bit(FF_RUMBLE, dev->ffbit);
  97. error = input_ff_create_memless(dev, plff, hid_plff_play);
  98. if (error) {
  99. kfree(plff);
  100. return error;
  101. }
  102. plff->report = report;
  103. plff->report->field[0]->value[0] = 0x00;
  104. plff->report->field[0]->value[1] = 0x00;
  105. plff->report->field[0]->value[2] = 0x00;
  106. plff->report->field[0]->value[3] = 0x00;
  107. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  108. }
  109. printk(KERN_INFO "hid-plff: Force feedback for PantherLord/GreenAsia "
  110. "devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
  111. return 0;
  112. }