hid-sjoy.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/slab.h>
  28. #include <linux/usb.h>
  29. #include <linux/hid.h>
  30. #include <linux/module.h>
  31. #include "hid-ids.h"
  32. #ifdef CONFIG_SMARTJOYPLUS_FF
  33. #include "usbhid/usbhid.h"
  34. struct sjoyff_device {
  35. struct hid_report *report;
  36. };
  37. static int hid_sjoyff_play(struct input_dev *dev, void *data,
  38. struct ff_effect *effect)
  39. {
  40. struct hid_device *hid = input_get_drvdata(dev);
  41. struct sjoyff_device *sjoyff = data;
  42. u32 left, right;
  43. left = effect->u.rumble.strong_magnitude;
  44. right = effect->u.rumble.weak_magnitude;
  45. dev_dbg(&dev->dev, "called with 0x%08x 0x%08x\n", left, right);
  46. left = left * 0xff / 0xffff;
  47. right = (right != 0); /* on/off only */
  48. sjoyff->report->field[0]->value[1] = right;
  49. sjoyff->report->field[0]->value[2] = left;
  50. dev_dbg(&dev->dev, "running with 0x%02x 0x%02x\n", left, right);
  51. usbhid_submit_report(hid, sjoyff->report, USB_DIR_OUT);
  52. return 0;
  53. }
  54. static int sjoyff_init(struct hid_device *hid)
  55. {
  56. struct sjoyff_device *sjoyff;
  57. struct hid_report *report;
  58. struct hid_input *hidinput;
  59. struct list_head *report_list =
  60. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  61. struct list_head *report_ptr = report_list;
  62. struct input_dev *dev;
  63. int error;
  64. if (list_empty(report_list)) {
  65. hid_err(hid, "no output reports found\n");
  66. return -ENODEV;
  67. }
  68. list_for_each_entry(hidinput, &hid->inputs, list) {
  69. report_ptr = report_ptr->next;
  70. if (report_ptr == report_list) {
  71. hid_err(hid, "required output report is missing\n");
  72. return -ENODEV;
  73. }
  74. report = list_entry(report_ptr, struct hid_report, list);
  75. if (report->maxfield < 1) {
  76. hid_err(hid, "no fields in the report\n");
  77. return -ENODEV;
  78. }
  79. if (report->field[0]->report_count < 3) {
  80. hid_err(hid, "not enough values in the field\n");
  81. return -ENODEV;
  82. }
  83. sjoyff = kzalloc(sizeof(struct sjoyff_device), GFP_KERNEL);
  84. if (!sjoyff)
  85. return -ENOMEM;
  86. dev = hidinput->input;
  87. set_bit(FF_RUMBLE, dev->ffbit);
  88. error = input_ff_create_memless(dev, sjoyff, hid_sjoyff_play);
  89. if (error) {
  90. kfree(sjoyff);
  91. return error;
  92. }
  93. sjoyff->report = report;
  94. sjoyff->report->field[0]->value[0] = 0x01;
  95. sjoyff->report->field[0]->value[1] = 0x00;
  96. sjoyff->report->field[0]->value[2] = 0x00;
  97. usbhid_submit_report(hid, sjoyff->report, USB_DIR_OUT);
  98. }
  99. hid_info(hid, "Force feedback for SmartJoy PLUS PS2/USB adapter\n");
  100. return 0;
  101. }
  102. #else
  103. static inline int sjoyff_init(struct hid_device *hid)
  104. {
  105. return 0;
  106. }
  107. #endif
  108. static int sjoy_probe(struct hid_device *hdev, const struct hid_device_id *id)
  109. {
  110. int ret;
  111. hdev->quirks |= id->driver_data;
  112. ret = hid_parse(hdev);
  113. if (ret) {
  114. hid_err(hdev, "parse failed\n");
  115. goto err;
  116. }
  117. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  118. if (ret) {
  119. hid_err(hdev, "hw start failed\n");
  120. goto err;
  121. }
  122. sjoyff_init(hdev);
  123. return 0;
  124. err:
  125. return ret;
  126. }
  127. static const struct hid_device_id sjoy_devices[] = {
  128. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_3_PRO),
  129. .driver_data = HID_QUIRK_NOGET },
  130. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_DUAL_BOX_PRO),
  131. .driver_data = HID_QUIRK_MULTI_INPUT | HID_QUIRK_NOGET |
  132. HID_QUIRK_SKIP_OUTPUT_REPORTS },
  133. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP_LTD, USB_DEVICE_ID_SUPER_JOY_BOX_5_PRO),
  134. .driver_data = HID_QUIRK_MULTI_INPUT | HID_QUIRK_NOGET |
  135. HID_QUIRK_SKIP_OUTPUT_REPORTS },
  136. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
  137. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SUPER_JOY_BOX_3) },
  138. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD),
  139. .driver_data = HID_QUIRK_MULTI_INPUT |
  140. HID_QUIRK_SKIP_OUTPUT_REPORTS },
  141. { }
  142. };
  143. MODULE_DEVICE_TABLE(hid, sjoy_devices);
  144. static struct hid_driver sjoy_driver = {
  145. .name = "smartjoyplus",
  146. .id_table = sjoy_devices,
  147. .probe = sjoy_probe,
  148. };
  149. static int __init sjoy_init(void)
  150. {
  151. return hid_register_driver(&sjoy_driver);
  152. }
  153. static void __exit sjoy_exit(void)
  154. {
  155. hid_unregister_driver(&sjoy_driver);
  156. }
  157. module_init(sjoy_init);
  158. module_exit(sjoy_exit);
  159. MODULE_LICENSE("GPL");
  160. MODULE_AUTHOR("Jussi Kivilinna");