PageRenderTime 78ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/hid/hid-sjoy.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 179 lines | 120 code | 34 blank | 25 comment | 10 complexity | 35aab92dd1fa552f46498ef84c9d3623 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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 "hid-ids.h"
  31. #ifdef CONFIG_SMARTJOYPLUS_FF
  32. #include "usbhid/usbhid.h"
  33. struct sjoyff_device {
  34. struct hid_report *report;
  35. };
  36. static int hid_sjoyff_play(struct input_dev *dev, void *data,
  37. struct ff_effect *effect)
  38. {
  39. struct hid_device *hid = input_get_drvdata(dev);
  40. struct sjoyff_device *sjoyff = data;
  41. u32 left, right;
  42. left = effect->u.rumble.strong_magnitude;
  43. right = effect->u.rumble.weak_magnitude;
  44. dev_dbg(&dev->dev, "called with 0x%08x 0x%08x\n", left, right);
  45. left = left * 0xff / 0xffff;
  46. right = (right != 0); /* on/off only */
  47. sjoyff->report->field[0]->value[1] = right;
  48. sjoyff->report->field[0]->value[2] = left;
  49. dev_dbg(&dev->dev, "running with 0x%02x 0x%02x\n", left, right);
  50. usbhid_submit_report(hid, sjoyff->report, USB_DIR_OUT);
  51. return 0;
  52. }
  53. static int sjoyff_init(struct hid_device *hid)
  54. {
  55. struct sjoyff_device *sjoyff;
  56. struct hid_report *report;
  57. struct hid_input *hidinput = list_entry(hid->inputs.next,
  58. struct hid_input, list);
  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. report_ptr = report_ptr->next;
  69. if (report_ptr == report_list) {
  70. hid_err(hid, "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. hid_err(hid, "no fields in the report\n");
  76. return -ENODEV;
  77. }
  78. if (report->field[0]->report_count < 3) {
  79. hid_err(hid, "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. hid_info(hid, "Force feedback for SmartJoy PLUS PS2/USB adapter\n");
  98. return 0;
  99. }
  100. #else
  101. static inline int sjoyff_init(struct hid_device *hid)
  102. {
  103. return 0;
  104. }
  105. #endif
  106. static int sjoy_probe(struct hid_device *hdev, const struct hid_device_id *id)
  107. {
  108. int ret;
  109. ret = hid_parse(hdev);
  110. if (ret) {
  111. hid_err(hdev, "parse failed\n");
  112. goto err;
  113. }
  114. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  115. if (ret) {
  116. hid_err(hdev, "hw start failed\n");
  117. goto err;
  118. }
  119. sjoyff_init(hdev);
  120. return 0;
  121. err:
  122. return ret;
  123. }
  124. static const struct hid_device_id sjoy_devices[] = {
  125. { HID_USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_SMARTJOY_PLUS) },
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(hid, sjoy_devices);
  129. static struct hid_driver sjoy_driver = {
  130. .name = "smartjoyplus",
  131. .id_table = sjoy_devices,
  132. .probe = sjoy_probe,
  133. };
  134. static int __init sjoy_init(void)
  135. {
  136. return hid_register_driver(&sjoy_driver);
  137. }
  138. static void __exit sjoy_exit(void)
  139. {
  140. hid_unregister_driver(&sjoy_driver);
  141. }
  142. module_init(sjoy_init);
  143. module_exit(sjoy_exit);
  144. MODULE_LICENSE("GPL");
  145. MODULE_AUTHOR("Jussi Kivilinna");