hid-zpff.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for Zeroplus based devices
  4. *
  5. * Copyright (c) 2005, 2006 Anssi Hannula <[email protected]>
  6. */
  7. /*
  8. */
  9. #include <linux/hid.h>
  10. #include <linux/input.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include "hid-ids.h"
  14. #ifdef CONFIG_ZEROPLUS_FF
  15. struct zpff_device {
  16. struct hid_report *report;
  17. };
  18. static int zpff_play(struct input_dev *dev, void *data,
  19. struct ff_effect *effect)
  20. {
  21. struct hid_device *hid = input_get_drvdata(dev);
  22. struct zpff_device *zpff = data;
  23. int left, right;
  24. /*
  25. * The following is specified the other way around in the Zeroplus
  26. * datasheet but the order below is correct for the XFX Executioner;
  27. * however it is possible that the XFX Executioner is an exception
  28. */
  29. left = effect->u.rumble.strong_magnitude;
  30. right = effect->u.rumble.weak_magnitude;
  31. dbg_hid("called with 0x%04x 0x%04x\n", left, right);
  32. left = left * 0x7f / 0xffff;
  33. right = right * 0x7f / 0xffff;
  34. zpff->report->field[2]->value[0] = left;
  35. zpff->report->field[3]->value[0] = right;
  36. dbg_hid("running with 0x%02x 0x%02x\n", left, right);
  37. hid_hw_request(hid, zpff->report, HID_REQ_SET_REPORT);
  38. return 0;
  39. }
  40. static int zpff_init(struct hid_device *hid)
  41. {
  42. struct zpff_device *zpff;
  43. struct hid_report *report;
  44. struct hid_input *hidinput;
  45. struct input_dev *dev;
  46. int i, error;
  47. if (list_empty(&hid->inputs)) {
  48. hid_err(hid, "no inputs found\n");
  49. return -ENODEV;
  50. }
  51. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  52. dev = hidinput->input;
  53. for (i = 0; i < 4; i++) {
  54. report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, i, 1);
  55. if (!report)
  56. return -ENODEV;
  57. }
  58. zpff = kzalloc(sizeof(struct zpff_device), GFP_KERNEL);
  59. if (!zpff)
  60. return -ENOMEM;
  61. set_bit(FF_RUMBLE, dev->ffbit);
  62. error = input_ff_create_memless(dev, zpff, zpff_play);
  63. if (error) {
  64. kfree(zpff);
  65. return error;
  66. }
  67. zpff->report = report;
  68. zpff->report->field[0]->value[0] = 0x00;
  69. zpff->report->field[1]->value[0] = 0x02;
  70. zpff->report->field[2]->value[0] = 0x00;
  71. zpff->report->field[3]->value[0] = 0x00;
  72. hid_hw_request(hid, zpff->report, HID_REQ_SET_REPORT);
  73. hid_info(hid, "force feedback for Zeroplus based devices by Anssi Hannula <[email protected]>\n");
  74. return 0;
  75. }
  76. #else
  77. static inline int zpff_init(struct hid_device *hid)
  78. {
  79. return 0;
  80. }
  81. #endif
  82. static int zp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  83. {
  84. int ret;
  85. ret = hid_parse(hdev);
  86. if (ret) {
  87. hid_err(hdev, "parse failed\n");
  88. goto err;
  89. }
  90. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  91. if (ret) {
  92. hid_err(hdev, "hw start failed\n");
  93. goto err;
  94. }
  95. zpff_init(hdev);
  96. return 0;
  97. err:
  98. return ret;
  99. }
  100. static const struct hid_device_id zp_devices[] = {
  101. { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0005) },
  102. { HID_USB_DEVICE(USB_VENDOR_ID_ZEROPLUS, 0x0030) },
  103. { }
  104. };
  105. MODULE_DEVICE_TABLE(hid, zp_devices);
  106. static struct hid_driver zp_driver = {
  107. .name = "zeroplus",
  108. .id_table = zp_devices,
  109. .probe = zp_probe,
  110. };
  111. module_hid_driver(zp_driver);
  112. MODULE_LICENSE("GPL");