hid-sjoy.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Force feedback support for SmartJoy PLUS PS2->USB adapter
  3. *
  4. * Copyright (c) 2009 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * Based of hid-pl.c and hid-gaff.c
  7. * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
  8. * Copyright (c) 2008 Lukasz Lubojanski <lukasz@lubojanski.info>
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /* #define DEBUG */
  26. #include <linux/input.h>
  27. #include <linux/usb.h>
  28. #include <linux/hid.h>
  29. #include "hid-ids.h"
  30. #ifdef CONFIG_SMARTJOYPLUS_FF
  31. #include "usbhid/usbhid.h"
  32. struct sjoyff_device {
  33. struct hid_report *report;
  34. };
  35. static int hid_sjoyff_play(struct input_dev *dev, void *data,
  36. struct ff_effect *effect)
  37. {
  38. struct hid_device *hid = input_get_drvdata(dev);
  39. struct sjoyff_device *sjoyff = data;
  40. u32 left, right;
  41. left = effect->u.rumble.strong_magnitude;
  42. right = effect->u.rumble.weak_magnitude;
  43. dev_dbg(&dev->dev, "called with 0x%08x 0x%08x\n", left, right);
  44. left = left * 0xff / 0xffff;
  45. right = (right != 0); /* on/off only */
  46. sjoyff->report->field[0]->value[1] = right;
  47. sjoyff->report->field[0]->value[2] = left;
  48. dev_dbg(&dev->dev, "running with 0x%02x 0x%02x\n", left, right);
  49. usbhid_submit_report(hid, sjoyff->report, USB_DIR_OUT);
  50. return 0;
  51. }
  52. static int sjoyff_init(struct hid_device *hid)
  53. {
  54. struct sjoyff_device *sjoyff;
  55. struct hid_report *report;
  56. struct hid_input *hidinput = list_entry(hid->inputs.next,
  57. struct hid_input, list);
  58. struct list_head *report_list =
  59. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  60. struct list_head *report_ptr = report_list;
  61. struct input_dev *dev;
  62. int error;
  63. if (list_empty(report_list)) {
  64. dev_err(&hid->dev, "no output reports found\n");
  65. return -ENODEV;
  66. }
  67. report_ptr = report_ptr->next;
  68. if (report_ptr == report_list) {
  69. dev_err(&hid->dev, "required output report is "
  70. "missing\n");
  71. return -ENODEV;
  72. }
  73. report = list_entry(report_ptr, struct hid_report, list);
  74. if (report->maxfield < 1) {
  75. dev_err(&hid->dev, "no fields in the report\n");
  76. return -ENODEV;
  77. }
  78. if (report->field[0]->report_count < 3) {
  79. dev_err(&hid->dev, "not enough values in the field\n");
  80. return -ENODEV;
  81. }
  82. sjoyff = kzalloc(sizeof(struct sjoyff_device), GFP_KERNEL);
  83. if (!sjoyff)
  84. return -ENOMEM;
  85. dev = hidinput->input;
  86. set_bit(FF_RUMBLE, dev->ffbit);
  87. error = input_ff_create_memless(dev, sjoyff, hid_sjoyff_play);
  88. if (error) {
  89. kfree(sjoyff);
  90. return error;
  91. }
  92. sjoyff->report = report;
  93. sjoyff->report->field[0]->value[0] = 0x01;
  94. sjoyff->report->field[0]->value[1] = 0x00;
  95. sjoyff->report->field[0]->value[2] = 0x00;
  96. usbhid_submit_report(hid, sjoyff->report, USB_DIR_OUT);
  97. dev_info(&hid->dev,
  98. "Force feedback for SmartJoy PLUS PS2/USB adapter\n");
  99. return 0;
  100. }
  101. #else
  102. static inline int sjoyff_init(struct hid_device *hid)
  103. {
  104. return 0;
  105. }
  106. #endif
  107. static int sjoy_probe(struct hid_device *hdev, const struct hid_device_id *id)
  108. {
  109. int ret;
  110. ret = hid_parse(hdev);
  111. if (ret) {
  112. dev_err(&hdev->dev, "parse failed\n");
  113. goto err;
  114. }
  115. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  116. if (ret) {
  117. dev_err(&hdev->dev, "hw start failed\n");
  118. goto err;
  119. }
  120. sjoyff_init(hdev);
  121. return 0;
  122. err:
  123. return ret;
  124. }
  125. static const struct hid_device_id sjoy_devices[] = {
  126. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
  127. { }
  128. };
  129. MODULE_DEVICE_TABLE(hid, sjoy_devices);
  130. static struct hid_driver sjoy_driver = {
  131. .name = "smartjoyplus",
  132. .id_table = sjoy_devices,
  133. .probe = sjoy_probe,
  134. };
  135. static int sjoy_init(void)
  136. {
  137. return hid_register_driver(&sjoy_driver);
  138. }
  139. static void sjoy_exit(void)
  140. {
  141. hid_unregister_driver(&sjoy_driver);
  142. }
  143. module_init(sjoy_init);
  144. module_exit(sjoy_exit);
  145. MODULE_LICENSE("GPL");
  146. MODULE_AUTHOR("Jussi Kivilinna");