Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python3
import os
import sys
try:
import usb.core
import usb.util
except ImportError:
print("First, install the pyusb module with PIP or your package manager.")
else:
if os.geteuid() != 0:
print("You need to run this script with sudo")
sys.exit()
dev = usb.core.find(find_all=True)
for d in dev:
if d.idVendor == 0x045e and d.idProduct == 0x028e:
d.ctrl_transfer(0xc1, 0x01, 0x0100, 0x00, 0x14)
finally:
sys.exit()
@Mikaela

This comment has been minimized.

Copy link

@Mikaela Mikaela commented Mar 7, 2019

Thank you, used with xboxdrv this fixes Piranha 3-in-1 Controller (PS2 / PS3 / PC), which appears as ID 045e:028e Microsoft Corp. Xbox360 Controller.

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Mar 21, 2019

Thank you, used with xboxdrv this fixes Piranha 3-in-1 Controller (PS2 / PS3 / PC), which appears as ID 045e:028e Microsoft Corp. Xbox360 Controller.

Maybe this and this other is more useful to you.

@noahares

This comment has been minimized.

Copy link

@noahares noahares commented Mar 21, 2019

Thank you so much. Works like a charm with my EasySMX Game Controller

@Mikaela

This comment has been minimized.

Copy link

@Mikaela Mikaela commented Mar 24, 2019

It seems that I don't get emails from gist comments. It turns out that it's user error with xpad.

When I use xboxdrv & fixcontroller.py the controller lights up all the time, when I use xpad & fixcontroller.py the controller lights up only when there is jstest or pcsx2 (or likely anything using it) running.

I seem to just have hotplug issue and will need to investigate what it is with.

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Apr 14, 2019

If you find something else you tell me, Oblomov added other initialization codes that he found when analyzing the Windows driver using Wireshark, maybe it will take another one for your controller to initialize correctly.

@blacksoil

This comment has been minimized.

Copy link

@blacksoil blacksoil commented Apr 20, 2019

Thank you so much! With this my Rexus G2X or internally knowns as Shanwan controller works!

@adessein

This comment has been minimized.

Copy link

@adessein adessein commented May 22, 2019

Thank you, used with xboxdrv this fixes Piranha 3-in-1 Controller (PS2 / PS3 / PC), which appears as ID 045e:028e Microsoft Corp. Xbox360 Controller.

Same for me, except that I did not have to install xboxdrv on (Debian 9)
Thanks a lot

@SzieberthAdam

This comment has been minimized.

Copy link

@SzieberthAdam SzieberthAdam commented Jul 8, 2019

Thanks a lot! Works with SHANWAN uRage PC Gamepad, Wired (Hama Vendetta ^2)

@promi

This comment has been minimized.

Copy link

@promi promi commented Nov 11, 2019

Works with a Trust GXT 540 gamepad in X mode.

@cbacott

This comment has been minimized.

Copy link

@cbacott cbacott commented Nov 15, 2019

Thank you. This activated my cheapo Shanwan S2 wired controller.

@Technohacker

This comment has been minimized.

Copy link

@Technohacker Technohacker commented Feb 23, 2020

Perfect, this works for my Amkette EVO :D

@elolivei

This comment has been minimized.

Copy link

@elolivei elolivei commented May 22, 2020

Very good, but it only worked with one controller. I adapted the script for use with more than one control.

fix

@Technohacker

This comment has been minimized.

Copy link

@Technohacker Technohacker commented May 22, 2020

@elolivei Would be great to send it as text instead 😅

@elolivei

This comment has been minimized.

Copy link

@elolivei elolivei commented May 23, 2020

#!/usr/bin/env python3

import usb.core
import usb.util

dev = usb.core.find(find_all=True)

for d in dev :
    
   if d.idVendor == 0x045e and d.idProduct == 0x028e :
       d.ctrl_transfer(0xc1, 0x01, 0x0100, 0x00, 0x14)
    

@elolivei

This comment has been minimized.

Copy link

@elolivei elolivei commented May 23, 2020

👍

@zeunus

This comment has been minimized.

Copy link

@zeunus zeunus commented Jun 25, 2020

Hello, I am using Batocera Linux, which has built-in xpadder, I bought a control with 2.4 dongle and I have a similar problem. However, I have no idea of ​​programming or anything, but logically, I think what you came up with is the solution I need. Could you help me ?, I need to know where to put this file / code. If I have to modify an existing file or something.
Thank you.
I accompany dmesg of my control.
shanwan

@gnumoksha

This comment has been minimized.

Copy link

@gnumoksha gnumoksha commented Jul 11, 2020

@dnmodder How did you get those initialization values? I've intercepted the USB communication on Windows but I'm having trouble identifying the init sequence.

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Jul 12, 2020

Very good, but it only worked with one controller. I adapted the script for use with more than one control.

fix

Good idea, thanks

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Jul 12, 2020

@dnmodder How did you get those initialization values? I've intercepted the USB communication on Windows but I'm having trouble identifying the init sequence.

Windows (VirtualBox) + WireShark + usbmon

I'm not an expert on that, I just looked at which sequences Windows sent and Linux didn't send.

https://github.com/openrazer/openrazer/wiki/Reverse-Engineering-USB-Protocol

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Jul 12, 2020

Hello, I am using Batocera Linux, which has built-in xpadder, I bought a control with 2.4 dongle and I have a similar problem. However, I have no idea of ​​programming or anything, but logically, I think what you came up with is the solution I need. Could you help me ?, I need to know where to put this file / code. If I have to modify an existing file or something.
Thank you.
I accompany dmesg of my control.
shanwan

It is just a script, you just have to give it execution permissions and run it from the terminal, but it happens that this is more for controls that emulate an Xbox 360 Controller, yours seems to be one that emulates an Xbox One Controller, I don't think this will help you

@laserburn

This comment has been minimized.

Copy link

@laserburn laserburn commented Sep 19, 2020

I tried running this on Ubuntu 20.04 and I get an exception when trying to import usb.core module. It would be nice to state which dependencies this script requires in order to run. Not for me, the store accepted to exchange Trust GXT 540 controller for the old reliable Logitech F310 no questions asked, but there are many others still stuck with controllers that can't work in Xinput mode in Linux.

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Sep 19, 2020

I tried running this on Ubuntu 20.04 and I get an exception when trying to import usb.core module. It would be nice to state which dependencies this script requires in order to run. Not for me, the store accepted to exchange Trust GXT 540 controller for the old reliable Logitech F310 no questions asked, but there are many others still stuck with controllers that can't work in Xinput mode in Linux.

Ready, I have added a message when the pyusb module is not found.

@jave808

This comment has been minimized.

Copy link

@jave808 jave808 commented Sep 22, 2020

Running Linux Mint 20. Can't install python-pip, BUT can install python3-pip. Then install pyusb with "pip3 install pyusb". Installs successfully.

But I get errors running your fixcontroller.py code. I gather you need python 2?

jave@ryzen7:~/bin$ python3 fixcontroller.py 
Traceback (most recent call last):
  File "fixcontroller.py", line 13, in <module>
    d.ctrl_transfer(0xc1, 0x01, 0x0100, 0x00, 0x14)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/core.py", line 1059, in ctrl_transfer
    self._ctx.managed_open()
  File "/home/jave/.local/lib/python3.8/site-packages/usb/core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/core.py", line 131, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 804, in open_device
    return _DeviceHandle(dev)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 652, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "/home/jave/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)

Tried with sudo and get the "install pyusb module" error.

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Sep 22, 2020

Running Linux Mint 20. Can't install python-pip, BUT can install python3-pip. Then install pyusb with "pip3 install pyusb". Installs successfully.

But I get errors running your fixcontroller.py code. I gather you need python 2?

jave@ryzen7:~/bin$ python3 fixcontroller.py 
Traceback (most recent call last):
  File "fixcontroller.py", line 13, in <module>
    d.ctrl_transfer(0xc1, 0x01, 0x0100, 0x00, 0x14)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/core.py", line 1059, in ctrl_transfer
    self._ctx.managed_open()
  File "/home/jave/.local/lib/python3.8/site-packages/usb/core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/core.py", line 131, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 804, in open_device
    return _DeviceHandle(dev)
  File "/home/jave/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 652, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "/home/jave/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)

Tried with sudo and get the "install pyusb module" error.

You need administrator permissions

@jave808

This comment has been minimized.

Copy link

@jave808 jave808 commented Sep 22, 2020

You need administrator permissions

I have managed to get it working now. I installed python-usb and python3-usb, but now am having another issue.

I have no /dev/input/js0. lsusb picks up my controller, and "dmesg|grep" usb returns a SHANWAN controller.

@jave808

This comment has been minimized.

Copy link

@jave808 jave808 commented Sep 22, 2020

I have no /dev/input/js0

Okay, I think I have it now. xpad was being blacklisted by a previous xboxdrv install. Removed xboxdrv.conf and rebooted.

Ran your fixcontroller.py script - ok. Then jstest-gtk - success! BUT when I unplug the gamepad and plug back in I lose connection - is this normal?

@dnmodder

This comment has been minimized.

Copy link
Owner Author

@dnmodder dnmodder commented Sep 23, 2020

I have no /dev/input/js0

Okay, I think I have it now. xpad was being blacklisted by a previous xboxdrv install. Removed xboxdrv.conf and rebooted.

Ran your fixcontroller.py script - ok. Then jstest-gtk - success! BUT when I unplug the gamepad and plug back in I lose connection - is this normal?

🤔 No, but I don't think it's the script's fault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.