xref: /openbmc/qemu/docs/system/devices/usb.rst (revision f16d15c9)
1.. _pcsys_005fusb:
2
3USB emulation
4-------------
5
6QEMU can emulate a PCI UHCI, OHCI, EHCI or XHCI USB controller. You can
7plug virtual USB devices or real host USB devices (only works with
8certain host operating systems). QEMU will automatically create and
9connect virtual USB hubs as necessary to connect multiple USB devices.
10
11USB controllers
12~~~~~~~~~~~~~~~
13
14XHCI controller support
15^^^^^^^^^^^^^^^^^^^^^^^
16
17QEMU has XHCI host adapter support.  The XHCI hardware design is much
18more virtualization-friendly when compared to EHCI and UHCI, thus XHCI
19emulation uses less resources (especially CPU).  So if your guest
20supports XHCI (which should be the case for any operating system
21released around 2010 or later) we recommend using it:
22
23    qemu -device qemu-xhci
24
25XHCI supports USB 1.1, USB 2.0 and USB 3.0 devices, so this is the
26only controller you need.  With only a single USB controller (and
27therefore only a single USB bus) present in the system there is no
28need to use the bus= parameter when adding USB devices.
29
30
31EHCI controller support
32^^^^^^^^^^^^^^^^^^^^^^^
33
34The QEMU EHCI Adapter supports USB 2.0 devices.  It can be used either
35standalone or with companion controllers (UHCI, OHCI) for USB 1.1
36devices.  The companion controller setup is more convenient to use
37because it provides a single USB bus supporting both USB 2.0 and USB
381.1 devices.  See next section for details.
39
40When running EHCI in standalone mode you can add UHCI or OHCI
41controllers for USB 1.1 devices too.  Each controller creates its own
42bus though, so there are two completely separate USB buses: One USB
431.1 bus driven by the UHCI controller and one USB 2.0 bus driven by
44the EHCI controller.  Devices must be attached to the correct
45controller manually.
46
47The easiest way to add a UHCI controller to a ``pc`` machine is the
48``-usb`` switch.  QEMU will create the UHCI controller as function of
49the PIIX3 chipset.  The USB 1.1 bus will carry the name ``usb-bus.0``.
50
51You can use the standard ``-device`` switch to add a EHCI controller to
52your virtual machine.  It is strongly recommended to specify an ID for
53the controller so the USB 2.0 bus gets an individual name, for example
54``-device usb-ehci,id=ehci``.  This will give you a USB 2.0 bus named
55``ehci.0``.
56
57When adding USB devices using the ``-device`` switch you can specify the
58bus they should be attached to.  Here is a complete example:
59
60.. parsed-literal::
61
62    |qemu_system| -M pc ${otheroptions}                        \\
63        -drive if=none,id=usbstick,format=raw,file=/path/to/image   \\
64        -usb                                                        \\
65        -device usb-ehci,id=ehci                                    \\
66        -device usb-tablet,bus=usb-bus.0                            \\
67        -device usb-storage,bus=ehci.0,drive=usbstick
68
69This attaches a USB tablet to the UHCI adapter and a USB mass storage
70device to the EHCI adapter.
71
72
73Companion controller support
74^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75
76The UHCI and OHCI controllers can attach to a USB bus created by EHCI
77as companion controllers.  This is done by specifying the ``masterbus``
78and ``firstport`` properties.  ``masterbus`` specifies the bus name the
79controller should attach to.  ``firstport`` specifies the first port the
80controller should attach to, which is needed as usually one EHCI
81controller with six ports has three UHCI companion controllers with
82two ports each.
83
84There is a config file in docs which will do all this for
85you, which you can use like this:
86
87.. parsed-literal::
88
89   |qemu_system| -readconfig docs/config/ich9-ehci-uhci.cfg
90
91Then use ``bus=ehci.0`` to assign your USB devices to that bus.
92
93Using the ``-usb`` switch for ``q35`` machines will create a similar
94USB controller configuration.
95
96
97.. _Connecting USB devices:
98
99Connecting USB devices
100~~~~~~~~~~~~~~~~~~~~~~
101
102USB devices can be connected with the ``-device usb-...`` command line
103option or the ``device_add`` monitor command. Available devices are:
104
105``usb-mouse``
106   Virtual Mouse. This will override the PS/2 mouse emulation when
107   activated.
108
109``usb-tablet``
110   Pointer device that uses absolute coordinates (like a touchscreen).
111   This means QEMU is able to report the mouse position without having
112   to grab the mouse. Also overrides the PS/2 mouse emulation when
113   activated.
114
115``usb-storage,drive=drive_id``
116   Mass storage device backed by drive_id (see the :ref:`disk images`
117   chapter in the System Emulation Users Guide). This is the classic
118   bulk-only transport protocol used by 99% of USB sticks. This
119   example shows it connected to an XHCI USB controller and with
120   a drive backed by a raw format disk image:
121
122   .. parsed-literal::
123
124       |qemu_system| [...]                                   \\
125        -drive if=none,id=stick,format=raw,file=/path/to/file.img \\
126        -device nec-usb-xhci,id=xhci                              \\
127        -device usb-storage,bus=xhci.0,drive=stick
128
129``usb-uas``
130   USB attached SCSI device. This does not create a SCSI disk, so
131   you need to explicitly create a ``scsi-hd`` or ``scsi-cd`` device
132   on the command line, as well as using the ``-drive`` option to
133   specify what those disks are backed by. One ``usb-uas`` device can
134   handle multiple logical units (disks). This example creates three
135   logical units: two disks and one cdrom drive:
136
137   .. parsed-literal::
138
139      |qemu_system| [...]                                         \\
140       -drive if=none,id=uas-disk1,format=raw,file=/path/to/file1.img  \\
141       -drive if=none,id=uas-disk2,format=raw,file=/path/to/file2.img  \\
142       -drive if=none,id=uas-cdrom,media=cdrom,format=raw,file=/path/to/image.iso \\
143       -device nec-usb-xhci,id=xhci                                    \\
144       -device usb-uas,id=uas,bus=xhci.0                               \\
145       -device scsi-hd,bus=uas.0,scsi-id=0,lun=0,drive=uas-disk1       \\
146       -device scsi-hd,bus=uas.0,scsi-id=0,lun=1,drive=uas-disk2       \\
147       -device scsi-cd,bus=uas.0,scsi-id=0,lun=5,drive=uas-cdrom
148
149``usb-bot``
150   Bulk-only transport storage device. This presents the guest with the
151   same USB bulk-only transport protocol interface as ``usb-storage``, but
152   the QEMU command line option works like ``usb-uas`` and does not
153   automatically create SCSI disks for you. ``usb-bot`` supports up to
154   16 LUNs. Unlike ``usb-uas``, the LUN numbers must be continuous,
155   i.e. for three devices you must use 0+1+2. The 0+1+5 numbering from the
156   ``usb-uas`` example above won't work with ``usb-bot``.
157
158``usb-mtp,rootdir=dir``
159   Media transfer protocol device, using dir as root of the file tree
160   that is presented to the guest.
161
162``usb-host,hostbus=bus,hostaddr=addr``
163   Pass through the host device identified by bus and addr
164
165``usb-host,vendorid=vendor,productid=product``
166   Pass through the host device identified by vendor and product ID
167
168``usb-wacom-tablet``
169   Virtual Wacom PenPartner tablet. This device is similar to the
170   ``tablet`` above but it can be used with the tslib library because in
171   addition to touch coordinates it reports touch pressure.
172
173``usb-kbd``
174   Standard USB keyboard. Will override the PS/2 keyboard (if present).
175
176``usb-serial,chardev=id``
177   Serial converter. This emulates an FTDI FT232BM chip connected to
178   host character device id.
179
180``usb-braille,chardev=id``
181   Braille device. This emulates a Baum Braille device USB port. id has to
182   specify a character device defined with ``-chardev …,id=id``.  One will
183   normally use BrlAPI to display the braille output on a BRLTTY-supported
184   device with
185
186   .. parsed-literal::
187
188      |qemu_system| [...] -chardev braille,id=brl -device usb-braille,chardev=brl
189
190   or alternatively, use the following equivalent shortcut:
191
192   .. parsed-literal::
193
194      |qemu_system| [...] -usbdevice braille
195
196``usb-net[,netdev=id]``
197   Network adapter that supports CDC ethernet and RNDIS protocols. id
198   specifies a netdev defined with ``-netdev …,id=id``. For instance,
199   user-mode networking can be used with
200
201   .. parsed-literal::
202
203      |qemu_system| [...] -netdev user,id=net0 -device usb-net,netdev=net0
204
205``usb-ccid``
206   Smartcard reader device
207
208``usb-audio``
209   USB audio device
210
211``u2f-{emulated,passthru}``
212   Universal Second Factor device
213
214``canokey``
215   An Open-source Secure Key implementing FIDO2, OpenPGP, PIV and more.
216   For more information, see :ref:`canokey`.
217
218Physical port addressing
219^^^^^^^^^^^^^^^^^^^^^^^^
220
221For all the above USB devices, by default QEMU will plug the device
222into the next available port on the specified USB bus, or onto
223some available USB bus if you didn't specify one explicitly.
224If you need to, you can also specify the physical port where
225the device will show up in the guest.  This can be done using the
226``port`` property.  UHCI has two root ports (1,2).  EHCI has six root
227ports (1-6), and the emulated (1.1) USB hub has eight ports.
228
229Plugging a tablet into UHCI port 1 works like this::
230
231        -device usb-tablet,bus=usb-bus.0,port=1
232
233Plugging a hub into UHCI port 2 works like this::
234
235        -device usb-hub,bus=usb-bus.0,port=2
236
237Plugging a virtual USB stick into port 4 of the hub just plugged works
238this way::
239
240        -device usb-storage,bus=usb-bus.0,port=2.4,drive=...
241
242In the monitor, the ``device_add` command also accepts a ``port``
243property specification. If you want to unplug devices too you should
244specify some unique id which you can use to refer to the device.
245You can then use ``device_del`` to unplug the device later.
246For example::
247
248        (qemu) device_add usb-tablet,bus=usb-bus.0,port=1,id=my-tablet
249        (qemu) device_del my-tablet
250
251Hotplugging USB storage
252~~~~~~~~~~~~~~~~~~~~~~~
253
254The ``usb-bot`` and ``usb-uas`` devices can be hotplugged.  In the hotplug
255case they are added with ``attached = false`` so the guest will not see
256the device until the ``attached`` property is explicitly set to true.
257That allows you to attach one or more scsi devices before making the
258device visible to the guest. The workflow looks like this:
259
260#. ``device-add usb-bot,id=foo``
261#. ``device-add scsi-{hd,cd},bus=foo.0,lun=0``
262#. optionally add more devices (luns 1 ... 15)
263#. ``scripts/qmp/qom-set foo.attached = true``
264
265.. _host_005fusb_005fdevices:
266
267Using host USB devices on a Linux host
268~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269
270WARNING: this is an experimental feature. QEMU will slow down when using
271it. USB devices requiring real time streaming (i.e. USB Video Cameras)
272are not supported yet.
273
2741. If you use an early Linux 2.4 kernel, verify that no Linux driver is
275   actually using the USB device. A simple way to do that is simply to
276   disable the corresponding kernel module by renaming it from
277   ``mydriver.o`` to ``mydriver.o.disabled``.
278
2792. Verify that ``/proc/bus/usb`` is working (most Linux distributions
280   should enable it by default). You should see something like that:
281
282   ::
283
284      ls /proc/bus/usb
285      001  devices  drivers
286
2873. Since only root can access to the USB devices directly, you can
288   either launch QEMU as root or change the permissions of the USB
289   devices you want to use. For testing, the following suffices:
290
291   ::
292
293      chown -R myuid /proc/bus/usb
294
2954. Launch QEMU and do in the monitor:
296
297   ::
298
299      info usbhost
300        Device 1.2, speed 480 Mb/s
301          Class 00: USB device 1234:5678, USB DISK
302
303   You should see the list of the devices you can use (Never try to use
304   hubs, it won't work).
305
3065. Add the device in QEMU by using:
307
308   ::
309
310      device_add usb-host,vendorid=0x1234,productid=0x5678
311
312   Normally the guest OS should report that a new USB device is plugged.
313   You can use the option ``-device usb-host,...`` to do the same.
314
3156. Now you can try to use the host USB device in QEMU.
316
317When relaunching QEMU, you may have to unplug and plug again the USB
318device to make it work again (this is a bug).
319
320``usb-host`` properties for specifying the host device
321^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
322
323The example above uses the ``vendorid`` and ``productid`` to
324specify which host device to pass through, but this is not
325the only way to specify the host device. ``usb-host`` supports
326the following properties:
327
328``hostbus=<nr>``
329  Specifies the bus number the device must be attached to
330``hostaddr=<nr>``
331  Specifies the device address the device got assigned by the guest os
332``hostport=<str>``
333  Specifies the physical port the device is attached to
334``vendorid=<hexnr>``
335  Specifies the vendor ID of the device
336``productid=<hexnr>``
337  Specifies the product ID of the device.
338
339In theory you can combine all these properties as you like.  In
340practice only a few combinations are useful:
341
342- ``vendorid`` and ``productid`` -- match for a specific device, pass it to
343  the guest when it shows up somewhere in the host.
344
345- ``hostbus`` and ``hostport`` -- match for a specific physical port in the
346  host, any device which is plugged in there gets passed to the
347  guest.
348
349- ``hostbus`` and ``hostaddr`` -- most useful for ad-hoc pass through as the
350  hostaddr isn't stable. The next time you plug the device into the host it
351  will get a new hostaddr.
352
353Note that on the host USB 1.1 devices are handled by UHCI/OHCI and USB
3542.0 by EHCI.  That means different USB devices plugged into the very
355same physical port on the host may show up on different host buses
356depending on the speed. Supposing that devices plugged into a given
357physical port appear as bus 1 + port 1 for 2.0 devices and bus 3 + port 1
358for 1.1 devices, you can pass through any device plugged into that port
359and also assign it to the correct USB bus in QEMU like this:
360
361.. parsed-literal::
362
363   |qemu_system| -M pc [...]                            \\
364        -usb                                                 \\
365        -device usb-ehci,id=ehci                             \\
366        -device usb-host,bus=usb-bus.0,hostbus=3,hostport=1  \\
367        -device usb-host,bus=ehci.0,hostbus=1,hostport=1
368
369``usb-host`` properties for reset behavior
370^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
371
372The ``guest-reset`` and ``guest-reset-all`` properties control
373whenever the guest is allowed to reset the physical usb device on the
374host.  There are three cases:
375
376``guest-reset=false``
377  The guest is not allowed to reset the (physical) usb device.
378
379``guest-reset=true,guest-resets-all=false``
380  The guest is allowed to reset the device when it is not yet
381  initialized (aka no usb bus address assigned).  Usually this results
382  in one guest reset being allowed.  This is the default behavior.
383
384``guest-reset=true,guest-resets-all=true``
385  The guest is allowed to reset the device as it pleases.
386
387The reason for this existing are broken usb devices.  In theory one
388should be able to reset (and re-initialize) usb devices at any time.
389In practice that may result in shitty usb device firmware crashing and
390the device not responding any more until you power-cycle (aka un-plug
391and re-plug) it.
392
393What works best pretty much depends on the behavior of the specific
394usb device at hand, so it's a trial-and-error game.  If the default
395doesn't work, try another option and see whenever the situation
396improves.
397
398record usb transfers
399^^^^^^^^^^^^^^^^^^^^
400
401All usb devices have support for recording the usb traffic.  This can
402be enabled using the ``pcap=<file>`` property, for example:
403
404``-device usb-mouse,pcap=mouse.pcap``
405
406The pcap files are compatible with the linux kernels usbmon.  Many
407tools, including ``wireshark``, can decode and inspect these trace
408files.
409