WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Gamepad support in WSL2 (/dev/jsx)

Open Aerocatia opened this issue 4 years ago • 28 comments

Is your feature request related to a problem? Please describe. It would be nice to have gamepads exposed to Linux guests for the convince of running and testing cross platform programs that can make use of gamepads solely on Windows using WSLg. You can already attach USB gamepads to the WSL2 instance using usbipd-win, however the system dsitro does not have gamepad support enabled since this use-case was not considered.

Describe the solution you'd like The system distro to have USB gamepad support enabled by default.

Describe alternatives you've considered It is possible to modify the system distro kernel to enable USB gamepad support, however this is a rather large maintenance burden for such a dinky little feature since you have to continuously keep the custom kernel up to date.

Aerocatia avatar Nov 23 '21 14:11 Aerocatia

Actually I am pretty sure this is tied to the Linux HID subsystem so I am sure there are more serious potential use cases by having this enabled. It would apply for any USB input device.

Aerocatia avatar Nov 23 '21 14:11 Aerocatia

I have been facing the same. I have switched my PC to Win 11 in order to install and Run some ROS GUI apps, which is not possible in Win 10 and some heat issues on Ubuntu. But I need the USB to control the Robot and I could not make my Joypad work.

musantos avatar Aug 20 '22 14:08 musantos

I got xbox controller attached to wsl2 using usbipd and it appears under /dev/bus/usb/00x/ devices but not under /dev/input/jsx device. Is it some driver issue or what? I see that there is xpad driver in wsl kernel https://github.com/microsoft/WSL2-Linux-Kernel/blob/master/drivers/input/joystick/xpad.c. How should I make that device under dev/bus/usb/00x to use this xpad driver?

SCIEN-00 avatar Oct 14 '22 05:10 SCIEN-00

Have the same problem. Did you figure it out?

eyal-friedman avatar Nov 15 '22 22:11 eyal-friedman

Hello, i temporarily solved missing xpad by rebuilding kernel:

  1. clone your kernel version from this
  2. replace Microsoft/config-wsl with config-wsl from archive kernel-xpad.zip
  3. make KCONFIG_CONFIG=Microsoft/config-wsl

if u don't want to build kernel yourself:

  1. wsl --shutdown
  2. add in your .wslconfig line kernel=C:\\Path\\To\\bzImage with path to bzImage from archive.
  3. PROFIT

i using kernel 5.10.102.1 and add to config these lines:

CONFIG_LOCALVERSION="-kotc9"
CONFIG_INPUT_JOYDEV=y
CONFIG_INPUT_EVDEV=y
CONFIG_INPUT_JOYSTICK=y
CONFIG_JOYSTICK_ANALOG=y
CONFIG_JOYSTICK_XPAD=y
CONFIG_JOYSTICK_XPAD_FF=y

link that might help: https://microhobby.com.br/blog/2019/09/21/compiling-your-own-linux-kernel-for-windows-wsl2/

hope this helps you too!

изображение

kernel-xpad.zip

kotC9 avatar Nov 27 '22 10:11 kotC9

Add kernel-parport please. I don't want to build kernel. Thank you.

fenix00853 avatar Dec 07 '22 00:12 fenix00853

Hello, i temporarily solved missing xpad by rebuilding kernel:

  1. clone your kernel version from this
  2. replace Microsoft/config-wsl with config-wsl from archive kernel-xpad.zip
  3. make KCONFIG_CONFIG=Microsoft/config-wsl

if u don't want to build kernel yourself:

  1. wsl --shutdown
  2. add in your .wslconfig line kernel=C:\\Path\\To\\bzImage with path to bzImage from archive.
  3. PROFIT

i using kernel 5.10.102.1 and add to config these lines:

CONFIG_LOCALVERSION="-kotc9"
CONFIG_INPUT_JOYDEV=y
CONFIG_INPUT_EVDEV=y
CONFIG_INPUT_JOYSTICK=y
CONFIG_JOYSTICK_ANALOG=y
CONFIG_JOYSTICK_XPAD=y
CONFIG_JOYSTICK_XPAD_FF=y

link that might help: https://microhobby.com.br/blog/2019/09/21/compiling-your-own-linux-kernel-for-windows-wsl2/

hope this helps you too!

изображение

kernel-xpad.zip

This solution worked great for me, thanks for the help!

Just to add on a little bit to the solution to get my xbox elite series 1 controller working (wired):

Without the changes to the kernel, my xbox controller would not show up under /dev/input.

I ran lsusb and found the device not present in WSL. To get the device passed from W11 -> Ubuntu (WSL) -> Docker, I enlisted the help of usbipd. Using this, I attached to the device to the usb bus into my WSL instance. You will know it's working when your lsusb output it the same in both docker and WSL.

After that, I ran jstest and commands are showing up just fine. However, for my application, it still wasn't detected. If that's the case, run sudo chmod 777 event0 and sudo chmod 777 js0. Everything starts talking beautifully after that. It was something to do with permission issue on my end after plugging in the device.

billyliuschill avatar Dec 15 '22 00:12 billyliuschill

I made the discussed edits to the most recent kernel in this repo. Feel free to expand upon the explanation I put in the README, or let me know how to improve the repo as a resource via PRs.

atticusrussell avatar Feb 27 '23 06:02 atticusrussell

I made the discussed edits to the most recent kernel in this repo. Feel free to expand upon the explanation I put in the README, or let me know how to improve the repo as a resource via PRs.

thx for this, is there a way to download a binary to put this version of wls on my pc? I really know nothing of compiling kernels.

skunx99 avatar May 23 '24 22:05 skunx99

do you just run the two command in the wsl terminal?

second fails.

make KCONFIG_CONFIG=Microsoft/config-wsl make: *** No targets specified and no makefile found. Stop.

guessing I need to download the repo ?

I really wish someone could just explain the steps to do this for those that can not follow all the tech data. I get explaining what you did is helpful and thoughtful but it just confuses things to no end for me. I also know it is very common not to include binaries and expect anyone to just make build and run, but its not for us all.

skunx99 avatar May 26 '24 22:05 skunx99

I made the discussed edits to the most recent kernel in this repo. Feel free to expand upon the explanation I put in the README, or let me know how to improve the repo as a resource via PRs.

@atticusrussell Thanks for the excellent instructions you added to your repo! I'm working on a similar ROS2 project and having the same issue you had last year enabling joystick support. It has been quite the headache.

Since your fork of the kernel is a little outdated, I forked a fresh copy and made what I think are the same changes you did to the config.wsl file (see commit here). I was able to compile the kernel, replace it in WSL2, bind/attach it with usbipd on Windows, and see the device on /dev/input/js0. I am SO close to getting this to work and for the first time, I can see the device using jstest-gtk. But for the life of me I can't get the joystick events to work. Did you ever run into this issue? If not, do you have any thoughts as to what might be broken? I've had the same results from both a cheap USB gamepad and one from an XBOX One.

@skunx99 To answer your question, you first need to clone the repo. If you are using the one from @atticusrussell, log into your wsl terminal first and clone it with git clone https://github.com/atticusrussell/WSL2-Linux-Kernel. Then cd WSL2-Linux-Kernel into the directory and run through the rest of the build steps in his readme. You'll probably need to install the build tools first. I'm not sure all of these are needed, but I did that by running sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison dwarves. I've never done this either and it was much easier than I thought.

timmhayes avatar Jun 01 '24 16:06 timmhayes

My attempt

  1. First make sure you are at home so you do not make a mess elsewhere
  2. git clone https://github.com/atticusrussell/WSL2-Linux-Kernel
  3. sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison dwarves
  4. cd WSL2-Linux-Kernel
  5. make KCONFIG_CONFIG=Microsoft/config-wsl Answer yes to everything I guess? Did see that talked about
  6. cp vmlinux /mnt/c/Users/<winUserName>/ <<<<----- make sure to put in your user name and not < and > -- i.e "cp vmlinux /mnt/c/Users/john"
  7. vi /mnt/c/Users//.wslconfig make sure to replace name again.
  8. add this to the file make sure to put in the right user name again [wsl2] kernel=C:\Users\<winUserName>\vmlinux save and exit in vi :w :q
  9. wsl --shutdown Ubuntu from power shell in windows
  10. run wsl again

From the look of that I think it failed... wsl does not do anything now. .wslconfig is set to the right dir ( case sensitive ) vmlinux is in that folder. If I rename the files and reboot I can get back to the old linux. Is there a log file somewhere?

skunx99 avatar Jun 01 '24 17:06 skunx99

@skunx99 Make sure you change all the usernames where appropriate. Let's say your Windows' username is skunx99. Then you should be able to open the following file in Windows. C:\Users\skunx99\.wslconfig. The vmlinux file should be in that folder too. And the contents of the .wslconfig should look like this:

[wsl2]
kernel=C:\\Users\\skunx99\\vmlinux

If any of that is different, fit it and restart WSL. Then check the kernel version with uname -r in WSL.

timmhayes avatar Jun 01 '24 17:06 timmhayes

this is all correct. On windows I always use Administrator image image

best info I can get is

image

then it freezes

BTW: safe mode works... So I know its reading the .wslconfig uname -r 5.15.133.1-microsoft-standard-WSL2 <---- looks like it didn't read the image ? but its in safemode

I really do not understand why linux needs a build, why can't we just share the vmlinux file? What is it about linux that is so special that you can not share binaries? I never understood that. Making and building projects is not for everyone. Especially a wsl that is pretty much the same for everyone. Granted I'm on ubuntu 22.. but is it that different?

skunx99 avatar Jun 01 '24 18:06 skunx99

Not sure. The build in https://github.com/atticusrussell/WSL2-Linux-Kernel was forked from kernel 5.15.90.1 and is currently 8,708 commits behind current. You could try the build I just made, which I forked this morning from the latest copy (5.15.153.1). I tried to mirror all the changes from @atticusrussell's fork and can now see the joystick in WSL, but as I said I'm still having issues with the events. But it might be worthwhile. All the steps are the same, but you'd start with git clone https://github.com/timmhayes/WSL2-Linux-Kernel. I'd love to hear if it works for you. I feel like I'm still missing something.

timmhayes avatar Jun 01 '24 19:06 timmhayes

I apologize for the redundant question. but why not just share the binary? will start the 1hr build...

skunx99 avatar Jun 01 '24 20:06 skunx99

that one worked all buttons and events are good with my device image

for anyone that does not want to do all of this crap, and just use a built binary ( again, not sure this this is bad or against rules?) but I did ask, a few times http://spawnlinux.ddns.net/DoCz/down_m/temp/vmlinux

skunx99 avatar Jun 01 '24 21:06 skunx99

@skunx99 Great, glad to hear it. I was just trying to figure out how to publish my binary as a release.

I get the same view using my joystick, but the UI doesn't update when I push the buttons. Are all the buttons mapping correctly for you? I can't get that part to work and I'm not sure how to fix it.

timmhayes avatar Jun 01 '24 22:06 timmhayes

yeah all is good here, interestingly this is my wheelhouse. I maybe can help. Let me try my Xbox one.

looks like the xbox one is jacked up.

L and R should be using the same analog but they are on there own. only is one capable of %50. This is clearly a bug knowing how this controller should work.

right stick in on 3 and 4 not 2 and 3. easy to remap.

D-pad is set to axis 6 and 7 and should be HAT (buttons), ok if mapping can fix it in game.

but otherwise it works.

all buttons are good. image

Does it work in windows for you? Did you use js0? Did you install any other xbox drivers?

skunx99 avatar Jun 02 '24 00:06 skunx99

I have an old GameSir G3 gamepad and an XBOX controller. Both work fine from https://hardwaretester.com/gamepad using the Gamepad API from my Windows machine. I've tried attaching both at different times through usbipd and they seem to work, creating a device at /dev/input/js0 and /dev/input/event0. And when I run jstest-gtk from Ubuntu, I see the following:

jstest gtk

I had installed other XBOX drivers at some point, but I wiped those out when I updated my kernel this morning. So I guess the answer to that question is no.

So everything seems to work, but I don't see anything update in jstest-gtk when I use the controls... so the events are not working.

timmhayes avatar Jun 02 '24 01:06 timmhayes

maybe try jstest alone ? also try with the event instead of js, I have seen that with some xbox drivers. but you should get an error if its a normal driver.

you can get the raw data by doing cat /dev/input/js0 and see if its at least sending from the driver.

Also do evtest then 0 does that work?

skunx99 avatar Jun 02 '24 02:06 skunx99

maybe try jstest alone

jstest on /dev/input/js0 gets me this, with no updates jstest

also try with the event instead of js, I have seen that with some xbox drivers. but you should get an error if its a normal driver.

jstest on /dev/input/event/0 gets me the error you are talking about: jstest - event0

you can get the raw data by doing cat /dev/input/js0 and see if its at least sending from the driver.

I tried that before. Running cat it shows a small amount of data when it loads, but doesn't stream any additional data as I use the gamepad.

Also do evtest then 0

That pumps out a list of supported events, but gets to Properties and hangs at Testing ... (interrupt to exit). So something is breaking there...

timmhayes avatar Jun 02 '24 02:06 timmhayes

Ok that's the issue, the event processing is busted. You can check dmesg and also wireshark to maybe see what is going on, but my guess is this is driver related. Though I suppose it could also be how windows is handling the to translation. I did update my usbipd the old version was not working. Also There was something about a firewall and giving it permissions. A warning about not skipping that step.

could also try evemu? http://who-t.blogspot.com/2013/11/evtest-is-dead-long-live-evemu.html

skunx99 avatar Jun 02 '24 03:06 skunx99

Here is that warning. https://github.com/dorssel/usbipd-win/issues/335

skunx99 avatar Jun 02 '24 15:06 skunx99

Thanks, those are good ideas. I don't think I'm having firewall issues, as I'm using the standard Windows firewall. I can see the usbipd firewall rule is correctly listed and I'm not getting any errors when I bind the gamepad. I also have the latest copy of usbipd.

I tried evemu and it behaves like the other tools, pumping out all the info about my control, but stopping at events.

Dmesg has some interesting errors about the speed not being set, but I need to do more research about what that means:

dmesg

RIght now I'm going through this usbipd troubleshooting page running the server with debug messages: https://github.com/dorssel/usbipd-win/wiki/Troubleshooting

There I'm seeing a message that says, "connection close: Unable to read beyond the end of the stream". I might try posting an issue on their repo, since it looks likely the issue is with that software.

timmhayes avatar Jun 02 '24 18:06 timmhayes

@skunx99 I'm not sure what changed, but my GameSir controller is now working! The Xbox is still out, but I don't care which I use. Thanks again for the help.

timmhayes avatar Jun 06 '24 00:06 timmhayes

I followed the steps and I made the kernel with the correct settings from https://github.com/atticusrussell/WSL2-Linux-Kernel?tab=readme-ov-file. I can see /dev/input/js0.

However, Docker stops working. When I renamed the .wslconfig, Docker started working again. :S Has anyone tried it before?

msariisik avatar May 05 '25 15:05 msariisik

I followed the steps and I made the kernel with the correct settings from https://github.com/atticusrussell/WSL2-Linux-Kernel?tab=readme-ov-file. I can see /dev/input/js0.

However, Docker stops working. When I renamed the .wslconfig, Docker started working again. :S Has anyone tried it before?

I made the necessary changes on top of config-wsl All I did was to modify the CONFIGs which contain joystick. Due to the absence of CONFIG_PAHOLE_VERSION, Docker stopped.

Then it worked.

msariisik avatar May 06 '25 13:05 msariisik