hid-plff.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Force feedback support for PantherLord USB/PS2 2in1 Adapter devices
  3. *
  4. * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
  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. /* #define DEBUG */
  22. #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
  23. #include <linux/input.h>
  24. #include <linux/usb.h>
  25. #include <linux/hid.h>
  26. #include "usbhid.h"
  27. struct plff_device {
  28. struct hid_report *report;
  29. };
  30. static int hid_plff_play(struct input_dev *dev, void *data,
  31. struct ff_effect *effect)
  32. {
  33. struct hid_device *hid = dev->private;
  34. struct plff_device *plff = data;
  35. int left, right;
  36. left = effect->u.rumble.strong_magnitude;
  37. right = effect->u.rumble.weak_magnitude;
  38. debug("called with 0x%04x 0x%04x", left, right);
  39. left = left * 0x7f / 0xffff;
  40. right = right * 0x7f / 0xffff;
  41. plff->report->field[0]->value[2] = left;
  42. plff->report->field[0]->value[3] = right;
  43. debug("running with 0x%02x 0x%02x", left, right);
  44. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  45. return 0;
  46. }
  47. int hid_plff_init(struct hid_device *hid)
  48. {
  49. struct plff_device *plff;
  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 list_head *report_ptr = report_list;
  55. struct input_dev *dev;
  56. int error;
  57. /* The device contains 2 output reports (one for each
  58. HID_QUIRK_MULTI_INPUT device), both containing 1 field, which
  59. contains 4 ff00.0002 usages and 4 16bit absolute values.
  60. The 2 input reports also contain a field which contains
  61. 8 ff00.0001 usages and 8 boolean values. Their meaning is
  62. currently unknown. */
  63. if (list_empty(report_list)) {
  64. printk(KERN_ERR "hid-plff: no output reports found\n");
  65. return -ENODEV;
  66. }
  67. list_for_each_entry(hidinput, &hid->inputs, list) {
  68. report_ptr = report_ptr->next;
  69. if (report_ptr == report_list) {
  70. printk(KERN_ERR "hid-plff: required output report is missing\n");
  71. return -ENODEV;
  72. }
  73. report = list_entry(report_ptr, struct hid_report, list);
  74. if (report->maxfield < 1) {
  75. printk(KERN_ERR "hid-plff: no fields in the report\n");
  76. return -ENODEV;
  77. }
  78. if (report->field[0]->report_count < 4) {
  79. printk(KERN_ERR "hid-plff: not enough values in the field\n");
  80. return -ENODEV;
  81. }
  82. plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
  83. if (!plff)
  84. return -ENOMEM;
  85. dev = hidinput->input;
  86. set_bit(FF_RUMBLE, dev->ffbit);
  87. error = input_ff_create_memless(dev, plff, hid_plff_play);
  88. if (error) {
  89. kfree(plff);
  90. return error;
  91. }
  92. plff->report = report;
  93. plff->report->field[0]->value[0] = 0x00;
  94. plff->report->field[0]->value[1] = 0x00;
  95. plff->report->field[0]->value[2] = 0x00;
  96. plff->report->field[0]->value[3] = 0x00;
  97. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  98. }
  99. printk(KERN_INFO "hid-plff: Force feedback for PantherLord USB/PS2 "
  100. "2in1 Adapters by Anssi Hannula <anssi.hannula@gmail.com>\n");
  101. return 0;
  102. }