A collection of tips on using usb with qemu.
Picking a host adapter
The short answer for this one is: Unless you are running an
operating system museum just use
Any recent operating system should support xhci out-of-the box. The only OS without xhci support which is still in widespread use is Windows 7.
In case your qemu version doesn't support
-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.
When using xhci you should better not use
because you would get two usb busses then. Which is a valid
configuration, but requires naming the usb host adapter and
specifying the usb bus when adding usb devices if you want avoid
qemu picking a random usb bus:
-device qemu-xhci,id=xhci -device usb-tablet,bus=xhci.0
With a single usb bus you can just say
usb-tablet and be done with it.
Not enough usb ports?
Qemu can emulate an usb hub (
-device usb-hub). But the
hub supports usb1 only, so you should avoid using it. Better
solution is to just increase the number of root ports. xhci has
four root ports by default, but it supports up to 15 ports. And in
case this still isn't enough a second xhci adapter can be added to
the virtual machine.
To create a host adapter with 8 ports use
qemu-xhci,p2=8,p3=8. The libvirt configuration is:
In case you wonder why qemu-xhci needs both
p2 specifies the number
of usb2 ports (which support usb1 too), and
specifies the number of usb3 ports. It is possible to assign
different counts here. When using
qemu-xhci,p2=8,p3=4 you'll get an xhci adapter where ports
1-4 support both usb2 and usb3 and ports 5-8 are usb2-only. Can be
used to force a usb3-capable usb device into usb2 mode by plugging
it into a usb2-only xhci port. There should rarely be a need to
actually do that in practice though.