# Connecting USB devices to wsl2
This document contains information on forwarding usb devices to wsl2 in windows 10/11 systems.
### My setup
1. Windows 11
2. wsl2
a. Kernel info
```
$ uname -r
5.15.90.1-microsoft-standard-WSL2
```
b. OS information
```
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
```
### Install package for windows
Download latest installer(.msi) from here https://github.com/dorssel/usbipd-win/releases/tag/v4.0.0
### Install packages for wsl2/ubuntu
1. Sync'ng time between wsl2 and windows. This is required to avoid any ssl errors
```
sudo hwclock -s
```
2. Update packages and sources
```
sudo apt update
```
3. Install required packages
As documented [here](https://learn.microsoft.com/en-us/windows/wsl/connect-usb#install-the-usbip-tools-and-hardware-database-in-linux)
```
sudo apt install linux-tools-generic hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*-generic/usbip 20
```
### Attach the device to wsl2
1. Connect a usb device which is to be forwarded to wsl2.
2. Ensure that **wsl2** is up and running.
3. Get the busid of device by running `usbipd list` in **windows** `powershell`(admin)
![image](https://hackmd.io/_uploads/H18rU0_v6.png)
4. Bind the device. In **windows** `powershell` (admin) run
> This is required only once for a device
```
usbipd bind --busid=2-2
```
5. Attach device to **wsl2**. In **windows** `powershell` (admin) run
> This is required every time device reconnects
```
usbipd attach --wsl --busid=1-2
```
6. Now in **wsl2**, run `lsusb` . The device should show up.
```
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 0483:374e STMicroelectronics STLINK-V3
```
### Setting up permissions for usb debugging in wsl2
1. Setup `udev` rules
The following list contains a few of the commonly used debuggers. For custom rules see this [tutorial](https://www.clearpathrobotics.com/assets/guides/kinetic/ros/Udev%20Rules.html)
a. Setup OpenOCD udev rules
```
curl -fsSL https://raw.githubusercontent.com/openocd-org/openocd/master/contrib/60-openocd.rules | sudo tee /etc/udev/rules.d/60-openocd.rules
```
b. Setup SEGGER udev rules
//Todo
c. Wlink udev rules
//Todo
2. Restart udev service
```
sudo service udev restart
```
3. Add user to `plugdev` group
```
sudo usermod -aG plugdev $USER
```
4. Verify whether the current user was added
```
groups `whoami`
```
5. Now reconnect the device and attach it to wsl2 as stated above.
6. Verify if rules are set.
a. Find device id
```
$ lsusb
Bus 001 Device 006: ID 0483:374e STMicroelectronics STLINK-V3
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
```
b. Check device permissions and user group
Use the syntax `ls -l /dev/bus/usb/<Bus>/<Device>`
```
$ ls -l /dev/bus/usb/001/006
crw-rw-rw- 1 root plugdev 189, 5 Dec 27 14:14 /dev/bus/usb/001/006
```
Ensure that the device is accesible by plugdev group and also the read, write permissions
### Now the device should be accessible by wsl2 applications
## References
1. https://github.com/arduino/OpenOCD/blob/master/contrib/60-openocd.rules
2. https://docs.platformio.org/en/stable/core/installation/udev-rules.html
3. https://embedded-trainings.ferrous-systems.com/installation#linux-only-usb