Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/1a01cec63f8410a63a99c1fd9d143c65 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/1a01cec63f8410a63a99c1fd9d143c65 to your computer and use it in GitHub Desktop.
Virtualizing Linux USB Host Controller Drivers for QEMU #linux #qemu

Virtualizing Linux USB Host Controller Drivers for QEMU

USB is a device-host protocol. So the host, which is likely your operating system requires a host controller interface (which is a piece of hardware).

The host controllers require drivers to run.

There are 3 kinds of USB host controller drivers:

  • ohci - Open Host Controller Interface - USB 1
  • uhci - Universal Host Controller Interface - USB 1
  • ehci - Enhanced Host Controller Interface - USB 2, 1
  • xhci - eXtensible Host Controller Interface - USB 3, 2, 1

For more information see: https://gist.github.com/CMCDragonkai/810f78ee29c8fce916d072875f7e1751

When virtualizing USB (such as using QEMU), we need to first virtualize the host controller interface.

For QEMU, this means where is a various options to achieve this.

The -usb command line switch adds usb controllers matching the emulated hardware platform. So for the 'pc' machine type (emulating a 20+ year old i440FX chipset) this is a uhci host adapter (supporting usb1). For the 'q35' machine type (emulating a almost 10 year old Q35 chipset) it is ehci (for usb2 devices) with uhci companions (for usb1 devices). This is what you can use when running old guests which lack xhci support. https://www.kraxel.org/blog/2018/08/qemu-usb-tips/

QEMU has a list of virtualized devices for supporting these host controllers: https://en.wikibooks.org/wiki/QEMU/Devices/USB/Root

The most likely options you want is something like this:

-device qemu-xhci,id=xhci -device usb-...,bus=xhci.0

Note that .0 means the port of the interface. Interfaces have a limited number of ports, and it is possible to control them or add more than 1 virtual host controller interfaces.

The rest of the options are located here: https://qemu.weilnetz.de/doc/qemu-doc.html#usb_005fdevices

The only option that involves pass-through from the host to the guest system is usb-host. The other options are virtualizing the USB device itself.

To passthrough you need the hostbus and hostaddr or hostbus and hostport or vendorid and productid. All of this information can be acquired by lsusb.

The vendorid and productid are hex numbers, so always pass in 0x....

@CMCDragonkai
Copy link
Author

CMCDragonkai commented Nov 20, 2019

3 things though:

  1. You may need -accel kvm
  2. You may need to ensure that your USB device when passing through is not plugged into USB Type-C
  3. You may need to use sudo

In the guest machine, it should be possible to see the plugged in device with lsusb and lsblk (if usb mass storage).

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