HID: flydigi: add support for vader 5 pro
Subsystems (2)
cover HID: flydigi: add support for vader 5 pro
Hi all,
A few days ago I was contacted by a friend of mine who was having his share of issues with his Flydigi Vader 5 Pro controller: specifically the rumble didn't work correctly, alongside other issues that were partially solved by using the linked project.
However at the current time his main interest is in Steam support and the driver was working in everything but Steam, which insists on using the controller in hid mode rather than evdev mode by default, and when in evdev mode it is disregarding additional buttons and the gyroscope.
So we ended up with a perfectly working driver for the controller, that at the cost of gyroscope support and back buttons has rumble working correctly in Steam.
Since the driver works in basically everything that is not Steam, including the browser, I decided to submit it anyway as someone else will surely be interested in using some other way that is not the aforementioned program.
To make optimal use of this device the xpad driver should not bind to the controller, and this is the case for more than just this controller as Derek told me, so I added some basic infrastructure to use the best available driver for the device.
I also want to thank the original authors that did the work of implementing the controller the first time (in SDL).
Best regards,
Denis Benato
Denis Benato (2):
HID: flydigi: add support for vader 5 pro
Input: xpad - introduce a static table to ignore devices
drivers/hid/Kconfig | 8 + drivers/hid/Makefile | 1 + drivers/hid/hid-flydigi.c | 887 ++++++++++++++++++++++++++++++++++ drivers/hid/hid-ids.h | 3 + drivers/input/joystick/xpad.c | 24 + 5 files changed, 923 insertions(+) create mode 100644 drivers/hid/hid-flydigi.c
Signature · 1 line
2.47.3
PATCH 1/2 HID: flydigi: add support for vader 5 pro
The Flydigi Vader Pro 5 is a gamepad with additional buttons and a gyroscope that is connected wireless to a PC via its own dongle so it's always a USB device: add support for it by following the enstablished pattern of spawning two evdevs that share the same uniqid so that SDL can match the two and expose them as a single controller.
--- drivers/hid/Kconfig | 8 + drivers/hid/Makefile | 1 + drivers/hid/hid-flydigi.c | 887 ++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-ids.h | 3 + 4 files changed, 899 insertions(+) create mode 100644 drivers/hid/hid-flydigi.c
drivers/hid/Kconfig +8 -0
@@ -1205,6 +1205,14 @@ config STEAM_FF Say Y here if you want to enable force feedback support for the Steam Deck. +config HID_FLYDIGI + tristate "Flydigi game controller support" + depends on USB_HID + select INPUT_FF_MEMLESS + help + Support for Flydigi game controllers that use the vendor-specific + HID protocol, including Vader 5 Pro. + config HID_STEELSERIES tristate "Steelseries devices support" depends on USB_HID
drivers/hid/Makefile +1 -0
@@ -56,6 +56,7 @@ obj-$(CONFIG_HID_FT260) += hid-ft260.o obj-$(CONFIG_HID_GEMBIRD) += hid-gembird.o obj-$(CONFIG_HID_GFRM) += hid-gfrm.o obj-$(CONFIG_HID_GLORIOUS) += hid-glorious.o +obj-$(CONFIG_HID_FLYDIGI) += hid-flydigi.o obj-$(CONFIG_HID_VIVALDI_COMMON) += hid-vivaldi-common.o obj-$(CONFIG_HID_GOODIX_SPI) += hid-goodix-spi.o obj-$(CONFIG_HID_GOOGLE_HAMMER) += hid-google-hammer.o
drivers/hid/hid-flydigi.c new file +887 -0
887 lines not on this page · raw message
drivers/hid/hid-ids.h +3 -0
@@ -515,6 +515,9 @@#define USB_VENDOR_ID_FLATFROG 0x25b5#define USB_DEVICE_ID_MULTITOUCH_3200 0x0002 +#define USB_VENDOR_ID_FLYDIGI 0x37d7 +#define USB_DEVICE_ID_FLYDIGI_VADER5 0x2401 +#define USB_VENDOR_ID_FUTABA 0x0547#define USB_DEVICE_ID_LED_DISPLAY 0x7000
Signature · 1 line
2.47.3
PATCH 2/2 Input: xpad - introduce a static table to ignore devices
In reply to Denis BenatoCertain devices can work both as HID and as an xpad device: binding both would duplicate inputs causing userspace to receive double inputs; at the same time if a HID device is available it is to be preferred as it can support additional features such as integrated IMU and additional buttons and more, depending on the specific driver, therefore allow only the best driver available to bind the device.
--- drivers/input/joystick/xpad.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
drivers/input/joystick/xpad.c +24 -0
@@ -406,6 +406,18 @@ static const struct xpad_device { { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN } }; +#define XPAD_SUBSTITUTE (_vid, _pid, _enabled) \ + { .vid = (_vid), .pid = (_pid), .enabled = (_enabled) } + +static const struct xpad_excluded_device { +u16 vid; +u16 pid; +bool enabled; +} xpad_excluded_devices[] = { +XPAD_SUBSTITUTE (0x37d7 ,0x2401 ,IS_ENABLED (CONFIG_HID_FLYDIGI )), + { } +}; +/* buttons shared with xbox and xbox360 */ static const signed short xpad_common_btn[] = {BTN_A ,BTN_B ,BTN_X ,BTN_Y ,/* "analog" buttons */
@@ -2044,11 +2056,23 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *idstruct usb_device *udev =interface_to_usbdev (intf);struct usb_xpad *xpad;struct usb_endpoint_descriptor *ep_irq_in, *ep_irq_out; +const struct xpad_excluded_device *excluded;int i, error;if (intf->cur_altsetting->desc.bNumEndpoints !=2 )return -ENODEV ; +for (excluded = xpad_excluded_devices; + excluded->vid || excluded->pid; + excluded++) { +if (!excluded->enabled) +continue ; + +if (le16_to_cpu (udev->descriptor.idVendor) == excluded->vid && +le16_to_cpu (udev->descriptor.idProduct) == excluded->pid) +return -ENODEV ; + } +for (i =0 ; xpad_device[i].idVendor; i++) {if ((le16_to_cpu (udev->descriptor.idVendor) == xpad_device[i].idVendor) && (le16_to_cpu (udev->descriptor.idProduct) == xpad_device[i].idProduct))
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] Unconditional scheduling of `rumble_work` in `flydigi_gamepad_close()` leads to a Use-After-Free…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Unconditional scheduling of rumble_work in flydigi_gamepad_close() leads to a Use-After-Free during devm teardown.
- [High] Early HID reports cause a NULL pointer dereference because fd->gamepad is accessed before it is initialized.
- [High] The HID raw_event handler attempts to acquire a mutex and perform synchronous USB control transfers from an interrupt context, triggering a system panic.
- [High] The output_buf array is embedded in struct flydigi_device without cache-line alignment, violating DMA API requirements.
- [High] Incoming HID raw events can reschedule acquire_work after it has been explicitly canceled in flydigi_remove(), leading to a Use-After-Free.
- [Low] Canceling acquire_work before setting gamepad_open to false leaks an armed timer.
- [Low] Negating a signed 16-bit sensor axis value can produce an integer that exceeds the configured ABS maximum.
--
--- Patch [1]: [PATCH 1/2] HID: flydigi: add support for vader 5 pro --- commit 01b1533a1ac56f03f32a4b26cb0c7795d18213a6
Author: Denis Benato <[email protected]>
HID: flydigi: add support for vader 5 pro
The Flydigi Vader Pro 5 is a gamepad with additional buttons and a gyroscope that is connected wireless to a PC via its own dongle so it's always a USB device: add support for it by following the enstablished pattern of spawning two evdevs that share the same uniqid so that SDL can match the two and expose them as a single controller.
Quoted 5 lines
drivers/hid/hid-flydigi.c new file +0
-0
[ ... ]
drivers/hid/hid-flydigi.c line 69
Quoted 9 lines
drivers/hid/hid-flydigi.c new file +9
-0+struct flydigi_device { +struct hid_device *hdev; +struct input_dev *gamepad; +struct input_dev *sensors; + +struct mutex output_mutex; +spinlock_t lock; +u8 output_buf[FLYDIGI_REPORT_SIZE ]; +u8 output_report_id;
[Severity: High]
Is this embedded DMA buffer cache-line aligned?
On architectures that are not hardware cache-coherent, passing an unaligned embedded buffer to the USB subsystem for DMA can cause memory corruption on adjacent fields (like output_report_id or lock).
[ ... ]
drivers/hid/hid-flydigi.c line 359
Quoted 5 lines
drivers/hid/hid-flydigi.c new file +5
-0+static void flydigi_gamepad_close (struct input_dev *dev) +{ +struct flydigi_device *fd =input_get_drvdata (dev); + +cancel_delayed_work_sync (&fd->acquire_work);
[Severity: Low]
Is it possible for the worker to re-arm itself here?
If the worker is running when cancel_delayed_work_sync() is called, it waits for the worker to finish. However, because gamepad_open is still true at this point, the worker could evaluate the flag as true and call schedule_delayed_work(), leaking an armed timer after the device is closed.
drivers/hid/hid-flydigi.c line 370
Quoted 11 lines
drivers/hid/hid-flydigi.c new file +11
-0+ + { +guard (spinlock_irqsave)(&fd->lock); + + fd->gamepad_open = false; + fd->strong =0 ; + fd->weak =0 ; + } + +schedule_work (&fd->rumble_work); +}
[Severity: High]
Does this unconditionally schedule the work item during teardown?
Because the input device is allocated with devm_input_allocate_device(), the device is automatically unregistered by devres after flydigi_remove() returns. The input core will call this close() callback during that unregistration if the device was open.
Since this schedules the work right as devres is freeing the fd structure, can this lead to a Use-After-Free when the worker thread executes?
[ ... ]
drivers/hid/hid-flydigi.c line 430
Quoted 9 lines
drivers/hid/hid-flydigi.c new file +9
-0+static void flydigi_report_gamepad (struct flydigi_device *fd,const u8 *data) +{ +struct input_dev *gamepad = fd->gamepad; +s16 axis; +int hat_x; +int hat_y; + +flydigi_map_hat (data[FLYDIGI_OFF_DPAD_ABXY ], &hat_x, &hat_y); +input_report_abs (gamepad,ABS_HAT0X , hat_x);
[Severity: High]
Can gamepad be NULL here?
In flydigi_probe(), hid_hw_open() is called before flydigi_register_gamepad(). Calling hid_hw_open() starts the hardware polling, allowing an early FLYDIGI_CMD_INPUT event to trigger flydigi_raw_event() and subsequently flydigi_report_gamepad().
Since fd->gamepad is assigned only at the end of flydigi_register_gamepad(), will this early hardware polling cause a NULL pointer dereference here?
[ ... ]
Quoted 7 lines
drivers/hid/hid-flydigi.c new file +7
-0+static void flydigi_report_sensors (struct flydigi_device *fd,const u8 *data) +{ +struct input_dev *sensors = fd->sensors; + +if (!sensors) +return ; +
drivers/hid/hid-flydigi.c line 532
Quoted 10 lines
drivers/hid/hid-flydigi.c new file +10
-0+input_event (sensors,EV_MSC ,MSC_TIMESTAMP , +ktime_to_us (ktime_get_boottime ())); + +/* Accelerometer */ +input_report_abs (sensors,ABS_X , + (s16 )get_unaligned_le16 (&data[FLYDIGI_OFF_ACCEL_X ])); +input_report_abs (sensors,ABS_Y , + (s16 )get_unaligned_le16 (&data[FLYDIGI_OFF_ACCEL_Y ])); +input_report_abs (sensors,ABS_Z , + -(s16 )get_unaligned_le16 (&data[FLYDIGI_OFF_ACCEL_Z ]));
[Severity: Low]
Can this negation produce an out-of-bounds value?
If the raw 16-bit sensor value is exactly -32768, casting it to s16 and negating it will yield 32768 due to C integer promotion rules, which exceeds the declared ABS maximum of 32767.
[ ... ]
Quoted 5 lines
drivers/hid/hid-flydigi.c new file +5
-0+static int flydigi_raw_event (struct hid_device *hdev,struct hid_report *report, +u8 *data,int size) +{ +struct flydigi_device *fd =hid_get_drvdata (hdev); +const u8 *payload = data;
[ ... ]
drivers/hid/hid-flydigi.c line 594
Quoted 13 lines
drivers/hid/hid-flydigi.c new file +13
-0+switch (payload[2 ]) { +case FLYDIGI_CMD_INPUT : +flydigi_report_gamepad (fd, payload); +flydigi_report_sensors (fd, payload); +break ; +case FLYDIGI_CMD_INFO : +case FLYDIGI_CMD_STATUS : +case FLYDIGI_CMD_ACQUIRE : +case FLYDIGI_CMD_HAPTIC : +/* Expected replies/acks to commands sent by this driver. */ +break ; +case FLYDIGI_CMD_STATUS_UPDATE : +flydigi_send_status_request (fd);
[Severity: High]
Can this execute a sleeping USB control transfer in an atomic context?
This function is called from an URB completion handler, but flydigi_send_status_request() eventually calls flydigi_send_output() which acquires a blocking mutex and calls hid_hw_raw_request(). Does this cause a scheduling while atomic panic?
drivers/hid/hid-flydigi.c
Quoted 1 line
drivers/hid/hid-flydigi.c new file +1
-0+schedule_delayed_work (&fd->acquire_work,0 );
[Severity: High]
Does this re-arm acquire_work during device removal?
In flydigi_remove(), cancel_delayed_work_sync(&fd->acquire_work) is called before hid_hw_stop(). If the hardware sends a FLYDIGI_CMD_STATUS_UPDATE packet in this small window, this will unconditionally re-schedule the work.
If the gamepad was not open, can this leave the work queued after fd is freed, resulting in a Use-After-Free?
Quoted 9 lines
drivers/hid/hid-flydigi.c new file +9
-0+break ; +default : +hid_dbg (hdev,"unhandled command: cmd=%02x sz=%d\n" , + payload[2 ], len); +break ; + } + +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1