This device has a quirky initialization process. Depending on how it was initialized, behaves completely differently. In default mode, it behaves as expected, but in fallback it disables force-feedback, analog stick configurations and L3/R3. Different OEMs manufactures joypads with same vid:pid but different axis/button mapping[1], and I don't know which one has which layout, so, we'll let hid-core figure that out, and handle only FF here. * The one I have has different axis layout than the one of Huseyin. Signed-off-by: Huseyin BIYIK <huseyinbiyik@xxxxxxxxxxx> Signed-off-by: Mubashshir <ahmubashshir@xxxxxxxxx> --- v5: Use hid_{get,set}_drvdata to pass data to `->play()` drivers/hid/Kconfig | 19 +++++ drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 3 + drivers/hid/hid-shanwan.c | 145 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 drivers/hid/hid-shanwan.c diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 4ce012f83253..e6c8aa855252 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -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 diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 5d37cacbde33..52878455fc10 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -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 diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index d79e946acdcb..04c3324dc453 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -627,6 +627,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 diff --git a/drivers/hid/hid-shanwan.c b/drivers/hid/hid-shanwan.c new file mode 100644 index 000000000000..c80bfcac5dc7 --- /dev/null +++ b/drivers/hid/hid-shanwan.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Force feedback support for Shanwan USB WirelessGamepad + * + * Copyright (c) 2022-2023 Huseyin BIYIK <huseyinbiyik@xxxxxxxxxxx> + * Copyright (c) 2023 Ahmad Hasan Mubashshir <ahmubashshir@xxxxxxxxx> + * + */ + +#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" + +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 +static int shanwan_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect) +{ + struct hid_device *hid = input_get_drvdata(dev); + struct hid_report *report = hid_get_drvdata(hid); + 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, + (sizeof(s32) * 8), + payload_template, + (sizeof(s32) * 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 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); + hid_set_drvdata(hid, report); + + set_bit(FF_RUMBLE, dev->ffbit); + if (input_ff_create_memless(dev, NULL, shanwan_play_effect)) + return -ENODEV; + + 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@xxxxxxxxxxx>"); +MODULE_AUTHOR("Ahmad Hasan Mubashshir <ahmubashshir@xxxxxxxxx>"); +MODULE_DESCRIPTION("Force feedback support for Shanwan USB WirelessGamepad"); +MODULE_LICENSE("GPL"); + +// vim: ts=8:noet -- 2.40.1