wsl 使用 usbipd 访问 USB 设备

注意:如下 usbipd 命令需要在 以管理员身份运行 的 powershell 中执行

usbipd: USB/IP open-source project

PS C:\WINDOWS\system32> usbipd wsl attach --busid 4-4
usbipd: error: The service is currently not running; a reboot should fix that.

https://github.com/dorssel/usbipd-win/issues/122

参考了上述链接,也做了重启,仍然报错,于是决定升级 usbipd

如下步骤也可以作为如何安装 usbipd,wsl 通过 usbipd 访问 USB 设备

usbipd 安装使用

https://github.com/dorssel/usbipd-win/releases/tag/v4.0.0

下载 usbipd-win_4.0.0.msi 并安装

需要注意的是,安装了这个版本发现 usbipd 命令的使用方式变了

PS C:\WINDOWS\system32> usbipd.exe wsl list
usbipd: error: The 'wsl' subcommand has been removed. Learn about the new syntax at https://learn.microsoft.com/windows/wsl/connect-usb#attach-a-usb-device.
PS C:\WINDOWS\system32> usbipd attach --busid 4-4
Option '--wsl' is required.

usbipd-win 4.0.0

Description:
  Attaches a USB device to a client.

  Currently, only WSL is supported. Other clients need to perform an attach using client-side tooling.

  Exactly one of the options '--busid' or '--hardware-id' is required.

Usage:
  usbipd attach [options]

Options:
  -a, --auto-attach                      Automatically re-attach when the device is detached or unplugged
  -b, --busid <BUSID>                    Attach device having <BUSID>
  -i, --hardware-id <VID:PID>            Attach device having <VID>:<PID>
  -w, --wsl <[DISTRIBUTION]> (REQUIRED)  Attach to WSL, optionally specifying the distribution to use
  -?, -h, --help                         Show help and usage information

https://github.com/MicrosoftDocs/WSL/issues/1868

正确的使用方式

PS C:\WINDOWS\system32> usbipd.exe list
Connected:
BUSID  VID:PID    DEVICE                                                        STATE
1-1    30c9:0096  HP 5MP Camera, HP IR Camera, Camera DFU Device                Not shared
1-10   8087:0033  英特尔(R) 无线 Bluetooth(R)                                   Not shared
4-3    0483:572a  USB 串行设备 (COM5), USB 大容量存储设备                       Not shared
4-4    3415:0308  adb                                                           Not shared

Persisted:
GUID                                  DEVICE
1bfb0182-4c41-487f-bdcb-530e2d2cd7ea  USB Serial Converter
d7c7745c-3765-49a5-8ad0-c4697d874eb4  Silicon Labs Dual CP2105 USB to UART Bridge: Enhanced COM...r
e7af58b3-5b09-4385-a043-6256222507d3  bst usb gadget function

usbipd: warning: USB filter 'USBPcap' is known to be incompatible with this software; 'bind --force' will be required.
PS C:\WINDOWS\system32> usbipd bind -b 4-4
usbipd: warning: USB filter 'USBPcap' is known to be incompatible with this software; 'bind --force' will be required.
PS C:\WINDOWS\system32> usbipd bind -b 4-4
usbipd: warning: USB filter 'USBPcap' is known to be incompatible with this software; 'bind --force' will be required.
PS C:\WINDOWS\system32> usbipd attach -w --busid 4-4
usbipd: info: Using WSL distribution 'Ubuntu' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Using IP address 172.25.192.1 to reach the host.

然后 wsl 中就可以访问到刚刚挂载的 usb 设备了 Bus 001 Device 002: ID 3415:0308 bst bst usb adb Gadget

luyang@KFC:/tmp$ sudo lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 3415:0308 bst bst usb adb Gadget
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

注意:usbipd attach 之前要先执行 bind 否则会报错误

PS C:\WINDOWS\system32> usbipd attach -w --busid 4-4
usbipd: error: Device is not shared; run 'usbipd bind -b 4-4' as administrator first.

参考链接

WSL2识别USB设备——安装开源 usbipd-win 项目-CSDN博客