xref: /aosp_15_r20/external/crosvm/docs/book/src/devices/usb.md (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1# USB
2
3crosvm supports attaching USB devices from the host by emulating an
4[xhci backend](https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf).
5
6Unlike some other VM software like qemu, crosvm does not support attaching USB devices at boot time,
7however we can tell the VM to attach the devices once the kernel has booted, as long as we started
8crosvm with a control socket (see the control socket section in
9[advanced usage](../running_crosvm/advanced_usage.md#control-socket)).
10
11First, start crosvm making sure to specify the control socket:
12
13```shell
14$ crosvm run -s /run/crosvm.sock ${USUAL_CROSVM_ARGS}
15```
16
17Then, you need to identify which device you want to attach by looking for its USB bus and device
18number:
19
20```shell
21$ lsusb
22Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
23Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
24Bus 002 Device 022: ID 18d1:4ee7 Google Inc. Pixel 5
25Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
26Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
27```
28
29Assuming in this example the device you want is the `Google Inc. Pixel 5`, its bus and port numbers
30are 002 and 022 respectively.
31
32There should be a USB device file on the host at the path `/dev/bus/usb/002/022` which is what you
33want to pass to the `crosvm usb attach` command:
34
35```shell
36# crosvm usb attach 00:00:00:00 /dev/bus/usb/002/022 /run/crosvm.sock
37```
38
39You can run this command as root or make sure your current user has permissions to access the device
40file. Also make sure the device is not currently attached to any other drivers on the host and is
41not already in use.
42
43NOTE: You need to pass some string formatted like `00:00:00:00` as the first parameter to the
44`usb attach` command. This is a deprecated argument and **is not used** by crosvm, but we need to
45include it anyway for it to work. It will be removed in the future.
46
47On the host you should see a message like:
48
49```shell
50ok 9
51```
52
53Which tells you the operation succeeded and which port number the USB device is attached to (in this
54case `9`).
55
56Inside the VM you should see dmesg messages that the USB device has been attached successfully and
57you should be able to use it as normal.
58
59If you want to detach the device, simply issue a detach command to the same number as the port
60returned by the attach command:
61
62```shell
63# crosvm usb detach 9 /run/crosvm.sock
64```
65
66Which should return another `ok 9` confirmation.
67
68Keep in mind that when a USB device is attached to a VM, it is in exclusive mode and cannot be used
69by the host or attached to other VMs.
70