qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

usb.rst (15428B)


      1 .. _pcsys_005fusb:
      2 
      3 USB emulation
      4 -------------
      5 
      6 QEMU can emulate a PCI UHCI, OHCI, EHCI or XHCI USB controller. You can
      7 plug virtual USB devices or real host USB devices (only works with
      8 certain host operating systems). QEMU will automatically create and
      9 connect virtual USB hubs as necessary to connect multiple USB devices.
     10 
     11 USB controllers
     12 ~~~~~~~~~~~~~~~
     13 
     14 XHCI controller support
     15 ^^^^^^^^^^^^^^^^^^^^^^^
     16 
     17 QEMU has XHCI host adapter support.  The XHCI hardware design is much
     18 more virtualization-friendly when compared to EHCI and UHCI, thus XHCI
     19 emulation uses less resources (especially CPU).  So if your guest
     20 supports XHCI (which should be the case for any operating system
     21 released around 2010 or later) we recommend using it:
     22 
     23     qemu -device qemu-xhci
     24 
     25 XHCI supports USB 1.1, USB 2.0 and USB 3.0 devices, so this is the
     26 only controller you need.  With only a single USB controller (and
     27 therefore only a single USB bus) present in the system there is no
     28 need to use the bus= parameter when adding USB devices.
     29 
     30 
     31 EHCI controller support
     32 ^^^^^^^^^^^^^^^^^^^^^^^
     33 
     34 The QEMU EHCI Adapter supports USB 2.0 devices.  It can be used either
     35 standalone or with companion controllers (UHCI, OHCI) for USB 1.1
     36 devices.  The companion controller setup is more convenient to use
     37 because it provides a single USB bus supporting both USB 2.0 and USB
     38 1.1 devices.  See next section for details.
     39 
     40 When running EHCI in standalone mode you can add UHCI or OHCI
     41 controllers for USB 1.1 devices too.  Each controller creates its own
     42 bus though, so there are two completely separate USB buses: One USB
     43 1.1 bus driven by the UHCI controller and one USB 2.0 bus driven by
     44 the EHCI controller.  Devices must be attached to the correct
     45 controller manually.
     46 
     47 The 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
     49 the PIIX3 chipset.  The USB 1.1 bus will carry the name ``usb-bus.0``.
     50 
     51 You can use the standard ``-device`` switch to add a EHCI controller to
     52 your virtual machine.  It is strongly recommended to specify an ID for
     53 the 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 
     57 When adding USB devices using the ``-device`` switch you can specify the
     58 bus 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 
     69 This attaches a USB tablet to the UHCI adapter and a USB mass storage
     70 device to the EHCI adapter.
     71 
     72 
     73 Companion controller support
     74 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     75 
     76 The UHCI and OHCI controllers can attach to a USB bus created by EHCI
     77 as companion controllers.  This is done by specifying the ``masterbus``
     78 and ``firstport`` properties.  ``masterbus`` specifies the bus name the
     79 controller should attach to.  ``firstport`` specifies the first port the
     80 controller should attach to, which is needed as usually one EHCI
     81 controller with six ports has three UHCI companion controllers with
     82 two ports each.
     83 
     84 There is a config file in docs which will do all this for
     85 you, which you can use like this:
     86 
     87 .. parsed-literal::
     88 
     89    |qemu_system| -readconfig docs/config/ich9-ehci-uhci.cfg
     90 
     91 Then use ``bus=ehci.0`` to assign your USB devices to that bus.
     92 
     93 Using the ``-usb`` switch for ``q35`` machines will create a similar
     94 USB controller configuration.
     95 
     96 
     97 .. _Connecting USB devices:
     98 
     99 Connecting USB devices
    100 ~~~~~~~~~~~~~~~~~~~~~~
    101 
    102 USB devices can be connected with the ``-device usb-...`` command line
    103 option 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 
    218 Physical port addressing
    219 ^^^^^^^^^^^^^^^^^^^^^^^^
    220 
    221 For all the above USB devices, by default QEMU will plug the device
    222 into the next available port on the specified USB bus, or onto
    223 some available USB bus if you didn't specify one explicitly.
    224 If you need to, you can also specify the physical port where
    225 the 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
    227 ports (1-6), and the emulated (1.1) USB hub has eight ports.
    228 
    229 Plugging a tablet into UHCI port 1 works like this::
    230 
    231         -device usb-tablet,bus=usb-bus.0,port=1
    232 
    233 Plugging a hub into UHCI port 2 works like this::
    234 
    235         -device usb-hub,bus=usb-bus.0,port=2
    236 
    237 Plugging a virtual USB stick into port 4 of the hub just plugged works
    238 this way::
    239 
    240         -device usb-storage,bus=usb-bus.0,port=2.4,drive=...
    241 
    242 In the monitor, the ``device_add` command also accepts a ``port``
    243 property specification. If you want to unplug devices too you should
    244 specify some unique id which you can use to refer to the device.
    245 You can then use ``device_del`` to unplug the device later.
    246 For example::
    247 
    248         (qemu) device_add usb-tablet,bus=usb-bus.0,port=1,id=my-tablet
    249         (qemu) device_del my-tablet
    250 
    251 Hotplugging USB storage
    252 ~~~~~~~~~~~~~~~~~~~~~~~
    253 
    254 The ``usb-bot`` and ``usb-uas`` devices can be hotplugged.  In the hotplug
    255 case they are added with ``attached = false`` so the guest will not see
    256 the device until the ``attached`` property is explicitly set to true.
    257 That allows you to attach one or more scsi devices before making the
    258 device 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 
    267 Using host USB devices on a Linux host
    268 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    269 
    270 WARNING: this is an experimental feature. QEMU will slow down when using
    271 it. USB devices requiring real time streaming (i.e. USB Video Cameras)
    272 are not supported yet.
    273 
    274 1. 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 
    279 2. 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 
    287 3. 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 
    295 4. 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 
    306 5. 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 
    315 6. Now you can try to use the host USB device in QEMU.
    316 
    317 When relaunching QEMU, you may have to unplug and plug again the USB
    318 device to make it work again (this is a bug).
    319 
    320 ``usb-host`` properties for specifying the host device
    321 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    322 
    323 The example above uses the ``vendorid`` and ``productid`` to
    324 specify which host device to pass through, but this is not
    325 the only way to specify the host device. ``usb-host`` supports
    326 the 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 
    339 In theory you can combine all these properties as you like.  In
    340 practice 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 
    353 Note that on the host USB 1.1 devices are handled by UHCI/OHCI and USB
    354 2.0 by EHCI.  That means different USB devices plugged into the very
    355 same physical port on the host may show up on different host buses
    356 depending on the speed. Supposing that devices plugged into a given
    357 physical port appear as bus 1 + port 1 for 2.0 devices and bus 3 + port 1
    358 for 1.1 devices, you can pass through any device plugged into that port
    359 and 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 
    372 The ``guest-reset`` and ``guest-reset-all`` properties control
    373 whenever the guest is allowed to reset the physical usb device on the
    374 host.  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 
    387 The reason for this existing are broken usb devices.  In theory one
    388 should be able to reset (and re-initialize) usb devices at any time.
    389 In practice that may result in shitty usb device firmware crashing and
    390 the device not responding any more until you power-cycle (aka un-plug
    391 and re-plug) it.
    392 
    393 What works best pretty much depends on the behavior of the specific
    394 usb device at hand, so it's a trial-and-error game.  If the default
    395 doesn't work, try another option and see whenever the situation
    396 improves.
    397 
    398 record usb transfers
    399 ^^^^^^^^^^^^^^^^^^^^
    400 
    401 All usb devices have support for recording the usb traffic.  This can
    402 be enabled using the ``pcap=<file>`` property, for example:
    403 
    404 ``-device usb-mouse,pcap=mouse.pcap``
    405 
    406 The pcap files are compatible with the linux kernels usbmon.  Many
    407 tools, including ``wireshark``, can decode and inspect these trace
    408 files.