Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script should no longer be necessary thanks to the latest changes made to the master branch of the xpad [https://github.com/paroj/xpad] driver, please give it a try and report any regressions you find.
#!/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()
@laserburn
Copy link

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
Copy link
Author

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
Copy link

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
Copy link
Author

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
Copy link

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
Copy link

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
Copy link
Author

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.

@grka
Copy link

grka commented Nov 25, 2020

Hi,

I used this script to make work my controller. And it worked ... Since I updated to linux kernel 5.9 from 5.8
I don't know if it's related or not ?

Here is dmesg related

[ 6420.822948] xpad 1-2.2:1.0: xpad_try_sending_next_out_packet - usb_submit_urb failed with result -19
[ 6423.200370] usb 1-2.2: new full-speed USB device number 12 using xhci_hcd
[ 6423.532529] usb 1-2.2: New USB device found, idVendor=2563, idProduct=0575, bcdDevice= 2.00
[ 6423.532532] usb 1-2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 6423.532534] usb 1-2.2: Product: PS3/PC Gamepad
[ 6423.532535] usb 1-2.2: Manufacturer: SHANWAN
[ 6423.565903] input: SHANWAN PS3/PC Gamepad as /devices/pci0000:00/0000:00:01.3/0000:02:00.0/usb1/1-2/1-2.2/1-2.2:1.0/0003:2563:0575.000A/input/input31

And lsusb

Bus 001 Device 013: ID 045e:028e Microsoft Corp. Xbox360 Controller

Any idea ?

Thank's !

@ghistes
Copy link

ghistes commented Nov 28, 2020

@grka:

This is exactly the problem I also see on my system. It used to work and now it no longer does with the usb_submit_urb failed.
My kernel however is older: 5.6.

@grka
Copy link

grka commented Nov 29, 2020

@ghistes

Maybe an xpad driver update ?

@ghistes
Copy link

ghistes commented Nov 29, 2020

@grka

What do you mean? A driver update may have caused the problem or a driver update would maybe solve it?

And would I update the xpad driver? By upgrading the kernel?

@grka
Copy link

grka commented Dec 9, 2020

@ghistes

I moved from xpad driver to the xboxdrv and it can now use my gamepad (I initialize this script before running the xboxdrv)

@randomparanoia
Copy link

randomparanoia commented Jan 1, 2021

thanks for the script! Any way to make it permanent? Every time I unplug usb I have to rerun the script.

@Technohacker
Copy link

Technohacker commented Jan 2, 2021

@randomparanoia I have a udev rule in /etc/udev/rules,d/80-controller.conf, adjust as needed for your controller's USB ID :)

ACTION=="add", ATTRS{idProduct}=="<product ID>", ATTRS{idVendor}=="<vendor ID>", DRIVERS=="usb", RUN+="<path to fixcontroller.py>"

@dnmodder
Copy link
Author

dnmodder commented Jan 4, 2021

ATTRS{idProduct}=="", ATTRS{idVendor}==""

Don't work for me.

@Technohacker
Copy link

Technohacker commented Jan 4, 2021

This is what I have, it's some Amkette gamepad, stored this script in /usr/local/bin/fix_controller

ACTION=="add", ATTRS{idProduct}=="028e", ATTRS{idVendor}=="045e", DRIVERS=="usb", RUN+="/usr/local/bin/fix_controller"

@dnmodder
Copy link
Author

dnmodder commented Jan 4, 2021

This is what I have, it's some Amkette gamepad, stored this script in /usr/local/bin/fix_controller

ACTION=="add", ATTRS{idProduct}=="028e", ATTRS{idVendor}=="045e", DRIVERS=="usb", RUN+="/usr/local/bin/fix_controller"

Ahh ok, I was putting 0x028e and 0x045e

@l-gthb
Copy link

l-gthb commented Jan 23, 2021

Hi,

I used this script to make work my controller. And it worked ... Since I updated to linux kernel 5.9 from 5.8
I don't know if it's related or not ?

Here is dmesg related

[ 6420.822948] xpad 1-2.2:1.0: xpad_try_sending_next_out_packet - usb_submit_urb failed with result -19
[ 6423.200370] usb 1-2.2: new full-speed USB device number 12 using xhci_hcd
[ 6423.532529] usb 1-2.2: New USB device found, idVendor=2563, idProduct=0575, bcdDevice= 2.00
[ 6423.532532] usb 1-2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 6423.532534] usb 1-2.2: Product: PS3/PC Gamepad
[ 6423.532535] usb 1-2.2: Manufacturer: SHANWAN
[ 6423.565903] input: SHANWAN PS3/PC Gamepad as /devices/pci0000:00/0000:00:01.3/0000:02:00.0/usb1/1-2/1-2.2/1-2.2:1.0/0003:2563:0575.000A/input/input31

And lsusb

Bus 001 Device 013: ID 045e:028e Microsoft Corp. Xbox360 Controller

Any idea ?

Thank's !

i just tried it for the first time with a gamepad from the same manufacturer and it works. i'm on kernel 5.10

@dnmodder thanks a lot mate! i finally have this thing working as it was supposed to.

@Gildum
Copy link

Gildum commented Mar 30, 2021

Thank you for the fix! My gamepad 'GameSir G3w' now works great on MX Linux.

Edit: I notcied that you have to run the script each time after a reboot. It's no big deal, but is there a way to make the changes permanently?

@pavel-kirienko
Copy link

pavel-kirienko commented Apr 1, 2021

I forked this into a standalone udev rule that doesn't require an external script to run: https://gist.github.com/pavel-kirienko/86b9d039151405451130a0fb3896887c

@AruMoon
Copy link

AruMoon commented May 8, 2021

Thank you for the fix! My gamepad 'GameSir G3w' now works great on MX Linux.

Edit: I notcied that you have to run the script each time after a reboot. It's no big deal, but is there a way to make the changes permanently?

If you still didn't resolved your question, add udev rule as in https://gist.github.com/dnmodder/de2df973323b7c6acf45f40dc66e8db3#gistcomment-3582029 or https://gist.github.com/pavel-kirienko/86b9d039151405451130a0fb3896887c

@adnanh
Copy link

adnanh commented Jun 15, 2021

Thank you!

This also fixes Corsair Esperanza EG106 wired PC/PS2/PS3 controller :-)

@flashrocket
Copy link

flashrocket commented Jul 25, 2021

Had to modify this script to make it work with my ANT Esports GP100 controller. I have posted the modified script to https://github.com/flashrocket/shanwan-controller-fix.

@Barthezzcz
Copy link

Barthezzcz commented Sep 20, 2021

wow, its working on Trust GXT540
Bus 001 Device 005: ID 045e:028e Microsoft Corp. Xbox360 Controller

THANKS ~!

@flashrocket
Copy link

flashrocket commented Sep 21, 2021

Guys. The xpad driver now has the shanwan controller support built in. No need for workaround scripts. You can install the new driver from https://github.com/paroj/xpad and help test the controller and iron out any problems.

@Technohacker
Copy link

Technohacker commented Sep 21, 2021

@flashrocket any idea if they're planning on upstreaming those changes to mainline later?

@flashrocket
Copy link

flashrocket commented Sep 21, 2021

@Technohacker No idea bud. @paroj may be able to let us know.

@paroj
Copy link

paroj commented Sep 21, 2021

these changes are now in https://github.com/paroj/xpad master. If they turn out to work without side-effects, we can think of sending them to linux upstream.

@lumin4ry
Copy link

lumin4ry commented Feb 27, 2022

thank you so much for this

@flashrocket
Copy link

flashrocket commented Mar 4, 2022

@lumin4ry you can use the new xpad driver itself instead of using the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment