Skip to content

Instantly share code, notes, and snippets.

@pavel-kirienko
Forked from dnmodder/fixcontroller.py
Last active June 30, 2024 18:08
Show Gist options
  • Save pavel-kirienko/86b9d039151405451130a0fb3896887c to your computer and use it in GitHub Desktop.
Save pavel-kirienko/86b9d039151405451130a0fb3896887c to your computer and use it in GitHub Desktop.
Use Microsoft X-Box 360 Gamepad with GNU/Linux
# By default, X-Box 360-compatible controllers are detectable but dysfunctional because they expect the host
# to send a particular USB control transfer with some sort of initialization command.
# This udev rule will invoke a trivial Python script automatically when the gamepad is connected that emits
# the required initialization command.
#
# Integration:
# 1. Put this rule into /etc/udev/rules.d/51-xbox-gamepad.rules
# 2. Install pyusb for the root's Python: sudo pip install pyusb
# 3. Reload udev rules as root: udevadm control --reload-rules && udevadm trigger
#
# If you are on a systemd-based distro, you can use this for troubleshooting:
# 1. Enable verbose logging: udevadm control --log-priority=info
# 2. Watch the logs while connecting the device: journalctl -f
#
# Inspired by Darvin Delgado's https://gist.github.com/dnmodder/de2df973323b7c6acf45f40dc66e8db3
ACTION=="add", ATTRS{idVendor}=="045e", ATTRS{idProduct}=="028e", \
RUN+="/usr/bin/env python3 -c 'for d in __import__(\"usb\").core.find(find_all=True, idVendor=0x045e, idProduct=0x028e): d.ctrl_transfer(0xc1, 0x01, 0x0100, 0x00, 0x14)'"
@adessein
Copy link

Thanks for sharing.
I had to adapt your script, probably because my user does not have the rights to access USB.
Here is what I did:

SUBSYSTEM=="usb", ATTRS{idVendor}=="045e", ATTRS{idProduct}=="028e", MODE="0666"

ACTION=="add", ATTRS{idVendor}=="045e", ATTRS{idProduct}=="028e", \
RUN+="/usr/bin/env python3 -c 'import usb.core; dev = usb.core.find(idVendor=0x045e, idProduct=0x028e); dev.ctrl_transfer(0xc1, 0x01, 0x0100, 0x00, 0x14)'"

@pavel-kirienko
Copy link
Author

Ah, yes indeed, it makes sense. I'm not sure though, can you not define MODE="0666" in the same rule like this:

ACTION=="add", ATTRS{idVendor}=="045e", ATTRS{idProduct}=="028e", MODE="0666" \
RUN+="/usr/bin/env python3 -c 'for d in __import__(\"usb\").core.find(find_all=True, idVendor=0x045e, idProduct=0x028e): d.ctrl_transfer(0xc1, 0x01, 0x0100, 0x00, 0x14)'"

If you validated it I would update the snippet.

@AmerXz
Copy link

AmerXz commented Jun 30, 2024

i have a fake ps3 controller that doesn't work even with these scripts i need help

dmesg :
[ 3224.411687] sony 0003:054C:0268.0010: input,hiddev97,hidraw2: USB HID v81.10 Joystick [Nintendo Co., Ltd. USB Gamepad] on usb-0000:00:14.0-1/input0

lsusb :
Bus 003 Device 012: ID 045e:028e Microsoft Corp. Xbox360 Controller

@pavel-kirienko
Copy link
Author

Please use the troubleshooting suggestions to investigate.

@AmerXz
Copy link

AmerXz commented Jun 30, 2024

journalctl -f
Jun 30 19:55:17 Abdulhafez-PC mtp-probe[87962]: checking bus 3, device 45: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1"
Jun 30 19:55:17 Abdulhafez-PC mtp-probe[87962]: bus: 3, device: 45 was not an MTP device
Jun 30 19:55:17 Abdulhafez-PC mtp-probe[87997]: checking bus 3, device 45: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1"
Jun 30 19:55:17 Abdulhafez-PC mtp-probe[87997]: bus: 3, device: 45 was not an MTP device
Jun 30 19:58:06 Abdulhafez-PC sudo[89041]: pam_systemd_home(sudo:auth): New sd-bus connection (system-bus-pam-systemd-home-89041) opened.
Jun 30 19:58:10 Abdulhafez-PC sudo[89041]: abdulhafez : TTY=pts/14 ; PWD=/home/abdulhafez ; USER=root ; COMMAND=/usr/bin/udevadm control --log-priority=info
Jun 30 19:58:10 Abdulhafez-PC sudo[89041]: pam_unix(sudo:session): session opened for user root(uid=0) by abdulhafez(uid=1000)
Jun 30 19:58:10 Abdulhafez-PC sudo[89041]: pam_unix(sudo:session): session closed for user root
Jun 30 19:58:19 Abdulhafez-PC kernel: usb 3-1: USB disconnect, device number 45
Jun 30 19:58:19 Abdulhafez-PC kernel: xpad 3-1:1.0: xpad_try_sending_next_out_packet - usb_submit_urb failed with result -19
Jun 30 19:58:40 Abdulhafez-PC kernel: usb 3-1: new full-speed USB device number 46 using xhci_hcd
Jun 30 19:58:40 Abdulhafez-PC kernel: usb 3-1: device descriptor read/64, error -71
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: New USB device found, idVendor=054c, idProduct=0268, bcdDevice= 1.00
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: Product: USB Gamepad
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: Manufacturer: Nintendo Co., Ltd.
Jun 30 19:58:41 Abdulhafez-PC kernel: input: Nintendo Co., Ltd. USB Gamepad Motion Sensors as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:054C:0268.0017/input/input79
Jun 30 19:58:41 Abdulhafez-PC kernel: input: Nintendo Co., Ltd. USB Gamepad as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:054C:0268.0017/input/input78
Jun 30 19:58:41 Abdulhafez-PC kernel: sony 0003:054C:0268.0017: input,hiddev97,hidraw2: USB HID v81.10 Joystick [Nintendo Co., Ltd. USB Gamepad] on usb-0000:00:14.0-1/input0
Jun 30 19:58:41 Abdulhafez-PC mtp-probe[89255]: checking bus 3, device 46: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1"
Jun 30 19:58:41 Abdulhafez-PC mtp-probe[89255]: bus: 3, device: 46 was not an MTP device
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: USB disconnect, device number 46
Jun 30 19:58:41 Abdulhafez-PC mtp-probe[89269]: checking bus 3, device 46: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1"
Jun 30 19:58:41 Abdulhafez-PC mtp-probe[89269]: bus: 3, device: 46 was not an MTP device
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: new full-speed USB device number 47 using xhci_hcd
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: New USB device found, idVendor=045e, idProduct=028e, bcdDevice= 5.72
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: Product: XBOX 360 For Windows
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: Manufacturer: Nintendo Co., Ltd.
Jun 30 19:58:41 Abdulhafez-PC kernel: usb 3-1: SerialNumber: 000000000001
Jun 30 19:58:41 Abdulhafez-PC kernel: input: Microsoft X-Box 360 pad as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/input/input80
Jun 30 19:58:41 Abdulhafez-PC mtp-probe[89291]: checking bus 3, device 47: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1"
Jun 30 19:58:41 Abdulhafez-PC mtp-probe[89291]: bus: 3, device: 47 was not an MTP device
Jun 30 19:58:42 Abdulhafez-PC mtp-probe[89337]: checking bus 3, device 47: "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1"
Jun 30 19:58:42 Abdulhafez-PC mtp-probe[89337]: bus: 3, device: 47 was not an MTP device

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