@@ -990,6 +990,25 @@ config HID_SEMITEK
- Woo-dy
- X-Bows Nature/Knight
+config HID_SHANWAN
+ tristate "ShanWan USB WirelessGamepad"
+ depends on USB_HID
+ help
+ Support for Shanwan USB WirelessGamepad (and clones).
+
+ This device has a quirky initialization process.
+ Depending on how it was initialized, it behaves completely differently.
+ In default mode, it behaves as expected, but in fallback it disables
+ force-feedback, analog stick configurations and L3/R3.
+
+config SHANWAN_FF
+ bool "ShanWan USB WirelessGamepad force feedback support"
+ depends on HID_SHANWAN
+ select INPUT_FF_MEMLESS
+ help
+ Say Y here if you have a ShanWan USB WirelessGamepad and want to enable
+ force feedback support for it.
+
config HID_SIGMAMICRO
tristate "SiGma Micro-based keyboards"
depends on USB_HID
@@ -116,6 +116,7 @@ obj-$(CONFIG_HID_RMI) += hid-rmi.o
obj-$(CONFIG_HID_SAITEK) += hid-saitek.o
obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o
obj-$(CONFIG_HID_SEMITEK) += hid-semitek.o
+obj-$(CONFIG_HID_SHANWAN) += hid-shanwan.o
obj-$(CONFIG_HID_SIGMAMICRO) += hid-sigmamicro.o
obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o
obj-$(CONFIG_HID_SONY) += hid-sony.o
@@ -623,6 +623,9 @@
#define USB_PRODUCT_ID_HP_PIXART_OEM_USB_OPTICAL_MOUSE_0641 0x0641
#define USB_PRODUCT_ID_HP_PIXART_OEM_USB_OPTICAL_MOUSE_1f4a 0x1f4a
+#define USB_VENDOR_ID_SHANWAN 0x2563
+#define USB_PRODUCT_ID_SHANWAN_USB_WIRELESSGAMEPAD 0x0575
+
#define USB_VENDOR_ID_HUION 0x256c
#define USB_DEVICE_ID_HUION_TABLET 0x006e
#define USB_DEVICE_ID_HUION_TABLET2 0x006d
new file mode 100644
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Force feedback support for Shanwan USB WirelessGamepad
+ *
+ * Copyright (c) 2022-2023 Huseyin BIYIK <huseyinbiyik@hotmail.com>
+ * Copyright (c) 2023 Ahmad Hasan Mubashshir <ahmubashshir@gmail.com>
+ *
+ */
+
+#include <linux/input.h>
+#include <linux/slab.h>
+#include <linux/hid.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/string.h>
+
+#include "hid-ids.h"
+
+#define SHANWAN_PAYLOAD_LEN(x) (sizeof(s32) * x)
+
+static bool swap_motors;
+module_param_named(swap, swap_motors, bool, 0);
+MODULE_PARM_DESC(swap, "Swap Weak/Strong Feedback motors");
+
+#ifdef CONFIG_SHANWAN_FF
+struct shanwan_device {
+ struct hid_report *report;
+};
+
+static int shanwan_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
+{
+ struct hid_device *hid = input_get_drvdata(dev);
+ struct shanwan_device *shanwan = data;
+ struct hid_report *report = shanwan->report;
+ struct hid_field *field0 = report->field[0];
+ s32 payload_template[] = {
+ 0x02, // 2 = rumble effect message
+ 0x08, // reserved value, always 8
+ 0x00, // rumble value
+ 0x00, // rumble value
+ 0xff // duration 0-254 (255 = nonstop)
+ };
+
+ if (effect->type != FF_RUMBLE)
+ return 0;
+
+ memcpy_and_pad(field0->value, SHANWAN_PAYLOAD_LEN(8), payload_template, SHANWAN_PAYLOAD_LEN(4), 0x00);
+
+ if (swap_motors) {
+ /* weak rumble / strong rumble */
+ field0->value[2] = effect->u.rumble.strong_magnitude / 256;
+ field0->value[3] = effect->u.rumble.weak_magnitude / 256;
+ } else {
+ /* strong rumble / weak rumble */
+ field0->value[2] = effect->u.rumble.weak_magnitude / 256;
+ field0->value[3] = effect->u.rumble.strong_magnitude / 256;
+ }
+
+ hid_hw_request(hid, report, HID_REQ_SET_REPORT);
+
+ return 0;
+}
+
+static int shanwan_init_ff(struct hid_device *hid)
+{
+ struct shanwan_device *shanwan;
+ struct hid_report *report;
+ struct hid_input *hidinput;
+ struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
+ struct input_dev *dev;
+
+ if (list_empty(&hid->inputs)) {
+ hid_err(hid, "no inputs found\n");
+ return -ENODEV;
+ }
+ hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
+ dev = hidinput->input;
+
+ if (list_empty(report_list)) {
+ hid_err(hid, "no output reports found\n");
+ return -ENODEV;
+ }
+
+ report = list_first_entry(report_list, struct hid_report, list);
+
+ shanwan = kzalloc(sizeof(*shanwan), GFP_KERNEL);
+ if (!shanwan)
+ return -ENOMEM;
+
+ set_bit(FF_RUMBLE, dev->ffbit);
+
+ if (input_ff_create_memless(dev, shanwan, shanwan_play_effect)) {
+ kfree(shanwan);
+ return -ENODEV;
+ }
+
+ shanwan->report = report;
+
+ return 0;
+}
+#else
+static int shanwan_init_ff(struct hid_device *hid)
+{
+ return 0;
+}
+#endif
+
+static int shanwan_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+ int ret;
+
+ ret = hid_parse(hdev);
+ if (ret) {
+ hid_err(hdev, "parse failed\n");
+ return ret;
+ }
+
+ ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
+ if (ret) {
+ hid_err(hdev, "hw start failed\n");
+ return ret;
+ }
+
+ ret = shanwan_init_ff(hdev);
+ if (ret)
+ hid_warn(hdev, "Failed to enable force feedback support, error: %d\n", ret);
+
+ ret = hid_hw_open(hdev);
+ if (ret) {
+ dev_err(&hdev->dev, "hw open failed\n");
+ hid_hw_stop(hdev);
+ return ret;
+ }
+
+ hid_hw_close(hdev);
+ return ret;
+}
+
+static const struct hid_device_id shanwan_devices[] = {
+ { HID_USB_DEVICE(USB_VENDOR_ID_SHANWAN, USB_PRODUCT_ID_SHANWAN_USB_WIRELESSGAMEPAD) },
+ { }
+};
+MODULE_DEVICE_TABLE(hid, shanwan_devices);
+
+static struct hid_driver shanwan_driver = {
+ .name = "shanwan",
+ .id_table = shanwan_devices,
+ .probe = shanwan_probe,
+};
+module_hid_driver(shanwan_driver);
+
+MODULE_AUTHOR("Huseyin BIYIK <huseyinbiyik@hotmail.com>");
+MODULE_AUTHOR("Ahmad Hasan Mubashshir <ahmubashshir@gmail.com>");
+MODULE_DESCRIPTION("Force feedback support for Shanwan USB WirelessGamepad");
+MODULE_LICENSE("GPL");