
* Run docker probe only if docker or podman are available The docker probe uses "sudo -n" which can cause an e-mail with a security warning each time when configure is run. Therefore run docker probe only if either docker or podman are available. That avoids the problematic "sudo -n" on build environments which have neither docker nor podman installed. Fixes: c4575b59155e2e00 ("configure: store container engine in config-host.mak") Signed-off-by: Stefan Weil <sw@weilnetz.de> Message-Id: <20221030083510.310584-1-sw@weilnetz.de> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Thomas Huth <thuth@redhat.com> Message-Id: <20221117172532.538149-2-alex.bennee@linaro.org> * tests/avocado/machine_aspeed.py: Reduce noise on the console for SDK tests The Aspeed SDK images are based on OpenBMC which starts a lot of services. The output noise on the console can break from time to time the test waiting for the logging prompt. Change the U-Boot bootargs variable to add "quiet" to the kernel command line and reduce the output volume. This also drops the test on the CPU id which was nice to have but not essential. Signed-off-by: Cédric Le Goater <clg@kaod.org> Message-Id: <20221104075347.370503-1-clg@kaod.org> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Message-Id: <20221117172532.538149-3-alex.bennee@linaro.org> * tests/docker: allow user to override check target This is useful when trying to bisect a particular failing test behind a docker run. For example: make docker-test-clang@fedora \ TARGET_LIST=arm-softmmu \ TEST_COMMAND="meson test qtest-arm/qos-test" \ J=9 V=1 Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-4-alex.bennee@linaro.org> * docs/devel: add a maintainers section to development process We don't currently have a clear place in the documentation to describe the roles and responsibilities of a maintainer. Lets create one so we can. I've moved a few small bits out of other files to try and keep everything in one place. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-5-alex.bennee@linaro.org> * docs/devel: make language a little less code centric We welcome all sorts of patches. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-6-alex.bennee@linaro.org> * docs/devel: simplify the minimal checklist The bullet points are quite long and contain process tips. Move those bits of the bullet to the relevant sections and link to them. Use a table for nicer formatting of the checklist. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-7-alex.bennee@linaro.org> * docs/devel: try and improve the language around patch review It is important that contributors take the review process seriously and we collaborate in a respectful way while avoiding personal attacks. Try and make this clear in the language. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-8-alex.bennee@linaro.org> * tests/avocado: Raise timeout for boot_linux.py:BootLinuxPPC64.test_pseries_tcg On my machine, a debug build of QEMU takes about 260 seconds to complete this test, so with the current timeout value of 180 seconds it always times out. Double the timeout value to 360 so the test definitely has enough time to complete. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Message-Id: <20221110142901.3832318-1-peter.maydell@linaro.org> Message-Id: <20221117172532.538149-9-alex.bennee@linaro.org> * tests/avocado: introduce alpine virt test for CI The boot_linux tests download and run a full cloud image boot and start a full distro. While the ability to test the full boot chain is worthwhile it is perhaps a little too heavy weight and causes issues in CI. Fix this by introducing a new alpine linux ISO boot in machine_aarch64_virt. This boots a fully loaded -cpu max with all the bells and whistles in 31s on my machine. A full debug build takes around 180s on my machine so we set a more generous timeout to cover that. We don't add a test for lesser GIC versions although there is some coverage for that already in the boot_xen.py tests. If we want to introduce more comprehensive testing we can do it with a custom kernel and initrd rather than a full distro boot. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-10-alex.bennee@linaro.org> * tests/avocado: skip aarch64 cloud TCG tests in CI We now have a much lighter weight test in machine_aarch64_virt which tests the full boot chain in less time. Rename the tests while we are at it to make it clear it is a Fedora cloud image. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-11-alex.bennee@linaro.org> * gitlab: integrate coverage report This should hopefully give is nice coverage information about what our tests (or at least the subset we are running) have hit. Ideally we would want a way to trigger coverage on tests likely to be affected by the current commit. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Acked-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221117172532.538149-12-alex.bennee@linaro.org> * vhost: mask VIRTIO_F_RING_RESET for vhost and vhost-user devices Commit 69e1c14aa2 ("virtio: core: vq reset feature negotation support") enabled VIRTIO_F_RING_RESET by default for all virtio devices. This feature is not currently emulated by QEMU, so for vhost and vhost-user devices we need to make sure it is supported by the offloaded device emulation (in-kernel or in another process). To do this we need to add VIRTIO_F_RING_RESET to the features bitmap passed to vhost_get_features(). This way it will be masked if the device does not support it. This issue was initially discovered with vhost-vsock and vhost-user-vsock, and then also tested with vhost-user-rng which confirmed the same issue. They fail when sending features through VHOST_SET_FEATURES ioctl or VHOST_USER_SET_FEATURES message, since VIRTIO_F_RING_RESET is negotiated by the guest (Linux >= v6.0), but not supported by the device. Fixes: 69e1c14aa2 ("virtio: core: vq reset feature negotation support") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1318 Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Message-Id: <20221121101101.29400-1-sgarzare@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Acked-by: Raphael Norwitz <raphael.norwitz@nutanix.com> Acked-by: Jason Wang <jasowang@redhat.com> * tests: acpi: whitelist DSDT before moving PRQx to _SB scope Signed-off-by: Igor Mammedov <imammedo@redhat.com> Message-Id: <20221121153613.3972225-2-imammedo@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * acpi: x86: move RPQx field back to _SB scope Commit 47a373faa6b2 (acpi: pc/q35: drop ad-hoc PCI-ISA bridge AML routines and let bus ennumeration generate AML) moved ISA bridge AML generation to respective devices and was using aml_alias() to provide PRQx fields in _SB. scope. However, it turned out that SeaBIOS was not able to process Alias opcode when parsing DSDT, resulting in lack of keyboard during boot (SeaBIOS console, grub, FreeDOS). While fix for SeaBIOS is posted https://mail.coreboot.org/hyperkitty/list/seabios@seabios.org/thread/RGPL7HESH5U5JRLEO6FP77CZVHZK5J65/ fixed SeaBIOS might not make into QEMU-7.2 in time. Hence this workaround that puts PRQx back into _SB scope and gets rid of aliases in ISA bridge description, so DSDT will be parsable by broken SeaBIOS. That brings back hardcoded references to ISA bridge PCI0.S08.P40C/PCI0.SF8.PIRQ where middle part now is auto generated based on slot it's plugged in, but it should be fine as bridge initialization also hardcodes PCI address of the bridge so it can't ever move. Once QEMU tree has fixed SeaBIOS blob, we should be able to drop this part and revert back to alias based approach Reported-by: Volker Rümelin <vr_qemu@t-online.de> Signed-off-by: Igor Mammedov <imammedo@redhat.com> Message-Id: <20221121153613.3972225-3-imammedo@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * tests: acpi: x86: update expected DSDT after moving PRQx fields in _SB scope Expected DSDT changes, pc: - Field (P40C, ByteAcc, NoLock, Preserve) + Scope (\_SB) { - PRQ0, 8, - PRQ1, 8, - PRQ2, 8, - PRQ3, 8 + Field (PCI0.S08.P40C, ByteAcc, NoLock, Preserve) + { + PRQ0, 8, + PRQ1, 8, + PRQ2, 8, + PRQ3, 8 + } } - Alias (PRQ0, \_SB.PRQ0) - Alias (PRQ1, \_SB.PRQ1) - Alias (PRQ2, \_SB.PRQ2) - Alias (PRQ3, \_SB.PRQ3) q35: - Field (PIRQ, ByteAcc, NoLock, Preserve) - { - PRQA, 8, - PRQB, 8, - PRQC, 8, - PRQD, 8, - Offset (0x08), - PRQE, 8, - PRQF, 8, - PRQG, 8, - PRQH, 8 + Scope (\_SB) + { + Field (PCI0.SF8.PIRQ, ByteAcc, NoLock, Preserve) + { + PRQA, 8, + PRQB, 8, + PRQC, 8, + PRQD, 8, + Offset (0x08), + PRQE, 8, + PRQF, 8, + PRQG, 8, + PRQH, 8 + } } - Alias (PRQA, \_SB.PRQA) - Alias (PRQB, \_SB.PRQB) - Alias (PRQC, \_SB.PRQC) - Alias (PRQD, \_SB.PRQD) - Alias (PRQE, \_SB.PRQE) - Alias (PRQF, \_SB.PRQF) - Alias (PRQG, \_SB.PRQG) - Alias (PRQH, \_SB.PRQH) Signed-off-by: Igor Mammedov <imammedo@redhat.com> Message-Id: <20221121153613.3972225-4-imammedo@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * MAINTAINERS: add mst to list of biosbits maintainers Adding Michael's name to the list of bios bits maintainers so that all changes and fixes into biosbits framework can go through his tree and he is notified. Suggested-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Ani Sinha <ani@anisinha.ca> Message-Id: <20221111151138.36988-1-ani@anisinha.ca> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * tests/avocado: configure acpi-bits to use avocado timeout Instead of using a hardcoded timeout, just rely on Avocado's built-in test case timeout. This helps avoid timeout issues on machines where 60 seconds is not sufficient. Signed-off-by: John Snow <jsnow@redhat.com> Message-Id: <20221115212759.3095751-1-jsnow@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Ani Sinha <ani@anisinha.ca> * acpi/tests/avocado/bits: keep the work directory when BITS_DEBUG is set in env Debugging bits issue often involves running the QEMU command line manually outside of the avocado environment with the generated ISO. Hence, its inconvenient if the iso gets cleaned up after the test has finished. This change makes sure that the work directory is kept after the test finishes if the test is run with BITS_DEBUG=1 in the environment so that the iso is available for use with the QEMU command line. CC: Daniel P. Berrangé <berrange@redhat.com> Signed-off-by: Ani Sinha <ani@anisinha.ca> Message-Id: <20221117113630.543495-1-ani@anisinha.ca> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * virtio: disable error for out of spec queue-enable Virtio 1.0 is pretty clear that features have to be negotiated before enabling VQs. Unfortunately Seabios ignored this ever since gaining 1.0 support (UEFI is ok). Comment the error out for now, and add a TODO. Fixes: 3c37f8b8d1 ("virtio: introduce virtio_queue_enable()") Cc: "Kangjie Xu" <kangjie.xu@linux.alibaba.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221121200339.362452-1-mst@redhat.com> * hw/loongarch: Add default stdout uart in fdt Add "chosen" subnode into LoongArch fdt, and set it's "stdout-path" prop to uart node. Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Song Gao <gaosong@loongson.cn> Message-Id: <20221115114923.3372414-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * hw/loongarch: Fix setprop_sized method in fdt rtc node. Fix setprop_sized method in fdt rtc node. Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Song Gao <gaosong@loongson.cn> Message-Id: <20221116040300.3459818-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * hw/loongarch: Replace the value of uart info with macro Using macro to replace the value of uart info such as addr, size in acpi_build method. Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Song Gao <gaosong@loongson.cn> Message-Id: <20221115115008.3372489-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * target/arm: Don't do two-stage lookup if stage 2 is disabled In get_phys_addr_with_struct(), we call get_phys_addr_twostage() if the CPU supports EL2. However, we don't check here that stage 2 is actually enabled. Instead we only check that inside get_phys_addr_twostage() to skip stage 2 translation. This means that even if stage 2 is disabled we still tell the stage 1 lookup to do its page table walks via stage 2. This works by luck for normal CPU accesses, but it breaks for debug accesses, which are used by the disassembler and also by semihosting file reads and writes, because the debug case takes a different code path inside S1_ptw_translate(). This means that setups that use semihosting for file loads are broken (a regression since 7.1, introduced in recent ptw refactoring), and that sometimes disassembly in debug logs reports "unable to read memory" rather than showing the guest insns. Fix the bug by hoisting the "is stage 2 enabled?" check up to get_phys_addr_with_struct(), so that we handle S2 disabled the same way we do the "no EL2" case, with a simple single stage lookup. Reported-by: Jens Wiklander <jens.wiklander@linaro.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Message-id: 20221121212404.1450382-1-peter.maydell@linaro.org * target/arm: Use signed quantity to represent VMSAv8-64 translation level The LPA2 extension implements 52-bit virtual addressing for 4k and 16k translation granules, and for the former, this means an additional level of translation is needed. This means we start counting at -1 instead of 0 when doing a walk, and so 'level' is now a signed quantity, and should be typed as such. So turn it from uint32_t into int32_t. This avoids a level of -1 getting misinterpreted as being >= 3, and terminating a page table walk prematurely with a bogus output address. Cc: Peter Maydell <peter.maydell@linaro.org> Cc: Philippe Mathieu-Daudé <f4bug@amsat.org> Cc: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> * Update VERSION for v7.2.0-rc2 Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> * tests/avocado: Update the URLs of the advent calendar images The qemu-advent-calendar.org server will be decommissioned soon. I've mirrored the images that we use for the QEMU CI to gitlab, so update their URLs to point to the new location. Message-Id: <20221121102436.78635-1-thuth@redhat.com> Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * tests/qtest: Decrease the amount of output from the qom-test The logs in the gitlab-CI have a size constraint, and sometimes we already hit this limit. The biggest part of the log then seems to be filled by the qom-test, so we should decrease the size of the output - which can be done easily by not printing the path for each property, since the path has already been logged at the beginning of each node that we handle here. However, if we omit the path, we should make sure to not recurse into child nodes in between, so that it is clear to which node each property belongs. Thus store the children and links in a temporary list and recurse only at the end of each node, when all properties have already been printed. Message-Id: <20221121194240.149268-1-thuth@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Thomas Huth <thuth@redhat.com> * tests/avocado: use new rootfs for orangepi test The old URL wasn't stable. I suspect the current URL will only be stable for a few months so maybe we need another strategy for hosting rootfs snapshots? Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Message-Id: <20221118113309.1057790-1-alex.bennee@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * Revert "usbredir: avoid queuing hello packet on snapshot restore" Run state is also in RUN_STATE_PRELAUNCH while "-S" is used. This reverts commit 0631d4b448454ae8a1ab091c447e3f71ab6e088a Signed-off-by: Joelle van Dyne <j@getutm.app> Reviewed-by: Ján Tomko <jtomko@redhat.com> The original commit broke the usage of usbredir with libvirt, which starts every domain with "-S". This workaround is no longer needed because the usbredir behavior has been fixed in the meantime: https://gitlab.freedesktop.org/spice/usbredir/-/merge_requests/61 Signed-off-by: Ján Tomko <jtomko@redhat.com> Message-Id: <1689cec3eadcea87255e390cb236033aca72e168.1669193161.git.jtomko@redhat.com> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * gtk: disable GTK Clipboard with a new meson option The GTK Clipboard implementation may cause guest hangs. Therefore implement new configure switch: --enable-gtk-clipboard, as a meson option disabled by default, which warns in the help text about the experimental nature of the feature. Regenerate the meson build options to include it. The initialization of the clipboard is gtk.c, as well as the compilation of gtk-clipboard.c are now conditional on this new option to be set. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1150 Signed-off-by: Claudio Fontana <cfontana@suse.de> Acked-by: Gerd Hoffmann <kraxel@redhat.com> Reviewed-by: Jim Fehlig <jfehlig@suse.com> Message-Id: <20221121135538.14625-1-cfontana@suse.de> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/usb/hcd-xhci.c: spelling: tranfer Fixes: effaf5a240e03020f4ae953e10b764622c3e87cc Signed-off-by: Michael Tokarev <mjt@tls.msk.ru> Reviewed-by: Thomas Huth <thuth@redhat.com> Reviewed-by: Stefan Weil <sw@weilnetz.de> Message-Id: <20221105114851.306206-1-mjt@msgid.tls.msk.ru> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * ui/gtk: prevent ui lock up when dpy_gl_update called again before current draw event occurs A warning, "qemu: warning: console: no gl-unblock within" followed by guest scanout lockup can happen if dpy_gl_update is called in a row and the second call is made before gd_draw_event scheduled by the first call is taking place. This is because draw call returns without decrementing gl_block ref count if the dmabuf was already submitted as shown below. (gd_gl_area_draw/gd_egl_draw) if (dmabuf) { if (!dmabuf->draw_submitted) { return; } else { dmabuf->draw_submitted = false; } } So it should not schedule any redundant draw event in case draw_submitted is already set in gd_egl_fluch/gd_gl_area_scanout_flush. Cc: Gerd Hoffmann <kraxel@redhat.com> Cc: Vivek Kasireddy <vivek.kasireddy@intel.com> Signed-off-by: Dongwon Kim <dongwon.kim@intel.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20221021192315.9110-1-dongwon.kim@intel.com> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/usb/hcd-xhci: Reset the XHCIState with device_cold_reset() Currently the hcd-xhci-pci and hcd-xhci-sysbus devices, which are mostly wrappers around the TYPE_XHCI device, which is a direct subclass of TYPE_DEVICE. Since TYPE_DEVICE devices are not on any qbus and do not get automatically reset, the wrapper devices both reset the TYPE_XHCI device in their own reset functions. However, they do this using device_legacy_reset(), which will reset the device itself but not any bus it has. Switch to device_cold_reset(), which avoids using a deprecated function and also propagates reset along any child buses. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Message-Id: <20221014145423.2102706-1-peter.maydell@linaro.org> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/audio/intel-hda: don't reset codecs twice Currently the intel-hda device has a reset method which manually resets all the codecs by calling device_legacy_reset() on them. This means they get reset twice, once because child devices on a qbus get reset before the parent device's reset method is called, and then again because we're manually resetting them. Drop the manual reset call, and ensure that codecs are still reset when the guest does a reset via ICH6_GCTL_RESET by using device_cold_reset() (which resets all the devices on the qbus as well as the device itself) instead of a direct call to the reset function. This is a slight ordering change because the (only) codec reset now happens before the controller registers etc are reset, rather than once before and then once after, but the codec reset function hda_audio_reset() doesn't care. This lets us drop a use of device_legacy_reset(), which is deprecated. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221014142632.2092404-2-peter.maydell@linaro.org> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/audio/intel-hda: Drop unnecessary prototype The only use of intel_hda_reset() is after its definition, so we don't need to separately declare its prototype at the top of the file; drop the unnecessary line. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221014142632.2092404-3-peter.maydell@linaro.org> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * add syx snapshot extras * it compiles! * virtiofsd: Add `sigreturn` to the seccomp whitelist The virtiofsd currently crashes on s390x. This is because of a `sigreturn` system call. See audit log below: type=SECCOMP msg=audit(1669382477.611:459): auid=4294967295 uid=0 gid=0 ses=4294967295 subj=system_u:system_r:virtd_t:s0-s0:c0.c1023 pid=6649 comm="virtiofsd" exe="/usr/libexec/virtiofsd" sig=31 arch=80000016 syscall=119 compat=0 ip=0x3fff15f748a code=0x80000000AUID="unset" UID="root" GID="root" ARCH=s390x SYSCALL=sigreturn Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com> Reviewed-by: German Maglione <gmaglione@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221125143946.27717-1-mhartmay@linux.ibm.com> * libvhost-user: Fix wrong type of argument to formatting function (reported by LGTM) Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Signed-off-by: Stefan Weil <sw@weilnetz.de> Message-Id: <20220422070144.1043697-2-sw@weilnetz.de> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-2-sw@weilnetz.de> * libvhost-user: Fix format strings Signed-off-by: Stefan Weil <sw@weilnetz.de> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20220422070144.1043697-3-sw@weilnetz.de> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-3-sw@weilnetz.de> * libvhost-user: Fix two more format strings This fix is required for 32 bit hosts. The bug was detected by CI for arm-linux, but is also relevant for i386-linux. Reported-by: Stefan Hajnoczi <stefanha@gmail.com> Signed-off-by: Stefan Weil <sw@weilnetz.de> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-4-sw@weilnetz.de> * libvhost-user: Add format attribute to local function vu_panic Signed-off-by: Stefan Weil <sw@weilnetz.de> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20220422070144.1043697-4-sw@weilnetz.de> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-5-sw@weilnetz.de> * MAINTAINERS: Add subprojects/libvhost-user to section "vhost" Signed-off-by: Stefan Weil <sw@weilnetz.de> [Michael agreed to act as maintainer for libvhost-user via email in https://lore.kernel.org/qemu-devel/20221123015218-mutt-send-email-mst@kernel.org/. --Stefan] Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-6-sw@weilnetz.de> * Add G_GNUC_PRINTF to function qemu_set_info_str and fix related issues With the G_GNUC_PRINTF function attribute the compiler detects two potential insecure format strings: ../../../net/stream.c:248:31: warning: format string is not a string literal (potentially insecure) [-Wformat-security] qemu_set_info_str(&s->nc, uri); ^~~ ../../../net/stream.c:322:31: warning: format string is not a string literal (potentially insecure) [-Wformat-security] qemu_set_info_str(&s->nc, uri); ^~~ There are also two other warnings: ../../../net/socket.c:182:35: warning: zero-length gnu_printf format string [-Wformat-zero-length] 182 | qemu_set_info_str(&s->nc, ""); | ^~ ../../../net/stream.c:170:35: warning: zero-length gnu_printf format string [-Wformat-zero-length] 170 | qemu_set_info_str(&s->nc, ""); Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Weil <sw@weilnetz.de> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-7-sw@weilnetz.de> * del ramfile * update seabios source from 1.16.0 to 1.16.1 git shortlog rel-1.16.0..rel-1.16.1 =================================== Gerd Hoffmann (3): malloc: use variable for ZoneHigh size malloc: use large ZoneHigh when there is enough memory virtio-blk: use larger default request size Igor Mammedov (1): acpi: parse Alias object Volker Rümelin (2): pci: refactor the pci_config_*() functions reset: force standard PCI configuration access Xiaofei Lee (1): virtio-blk: Fix incorrect type conversion in virtio_blk_op() Xuan Zhuo (2): virtio-mmio: read/write the hi 32 features for mmio virtio: finalize features before using device Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * update seabios binaries to 1.16.1 Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * fix for non i386 archs * replay: Fix declaration of replay_read_next_clock Fixes the build with gcc 13: replay/replay-time.c:34:6: error: conflicting types for \ 'replay_read_next_clock' due to enum/integer mismatch; \ have 'void(ReplayClockKind)' [-Werror=enum-int-mismatch] 34 | void replay_read_next_clock(ReplayClockKind kind) | ^~~~~~~~~~~~~~~~~~~~~~ In file included from ../qemu/replay/replay-time.c:14: replay/replay-internal.h:139:6: note: previous declaration of \ 'replay_read_next_clock' with type 'void(unsigned int)' 139 | void replay_read_next_clock(unsigned int kind); | ^~~~~~~~~~~~~~~~~~~~~~ Fixes: 8eda206e090 ("replay: recording and replaying clock ticks") Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Wilfred Mallawa <wilfred.mallawa@wdc.com> Reviewed-by: Pavel Dovgalyuk <Pavel.Dovgalyuk@ispras.ru> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221129010547.284051-1-richard.henderson@linaro.org> * hw/display/qxl: Have qxl_log_command Return early if no log_cmd handler Only 3 command types are logged: no need to call qxl_phys2virt() for the other types. Using different cases will help to pass different structure sizes to qxl_phys2virt() in a pair of commits. Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-2-philmd@linaro.org> * hw/display/qxl: Document qxl_phys2virt() Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-3-philmd@linaro.org> * hw/display/qxl: Pass requested buffer size to qxl_phys2virt() Currently qxl_phys2virt() doesn't check for buffer overrun. In order to do so in the next commit, pass the buffer size as argument. For QXLCursor in qxl_render_cursor() -> qxl_cursor() we verify the size of the chunked data ahead, checking we can access 'sizeof(QXLCursor) + chunk->data_size' bytes. Since in the SPICE_CURSOR_TYPE_MONO case the cursor is assumed to fit in one chunk, no change are required. In SPICE_CURSOR_TYPE_ALPHA the ahead read is handled in qxl_unpack_chunks(). Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Acked-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-4-philmd@linaro.org> * hw/display/qxl: Avoid buffer overrun in qxl_phys2virt (CVE-2022-4144) Have qxl_get_check_slot_offset() return false if the requested buffer size does not fit within the slot memory region. Similarly qxl_phys2virt() now returns NULL in such case, and qxl_dirty_one_surface() aborts. This avoids buffer overrun in the host pointer returned by memory_region_get_ram_ptr(). Fixes: CVE-2022-4144 (out-of-bounds read) Reported-by: Wenxu Yin (@awxylitol) Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1336 Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-5-philmd@linaro.org> * hw/display/qxl: Assert memory slot fits in preallocated MemoryRegion Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-6-philmd@linaro.org> * block-backend: avoid bdrv_unregister_buf() NULL pointer deref bdrv_*() APIs expect a valid BlockDriverState. Calling them with bs=NULL leads to undefined behavior. Jonathan Cameron reported this following NULL pointer dereference when a VM with a virtio-blk device and a memory-backend-file object is terminated: 1. qemu_cleanup() closes all drives, setting blk->root to NULL 2. qemu_cleanup() calls user_creatable_cleanup(), which results in a RAM block notifier callback because the memory-backend-file is destroyed. 3. blk_unregister_buf() is called by virtio-blk's BlockRamRegistrar notifier callback and undefined behavior occurs. Fixes: baf422684d73 ("virtio-blk: use BDRV_REQ_REGISTERED_BUF optimization hint") Co-authored-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221121211923.1993171-1-stefanha@redhat.com> * target/arm: Set TCGCPUOps.restore_state_to_opc for v7m This setting got missed, breaking v7m. Fixes: 56c6c98df85c ("target/arm: Convert to tcg_ops restore_state_to_opc") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1347 Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221129204146.550394-1-richard.henderson@linaro.org> * Update VERSION for v7.2.0-rc3 Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> * hooks are now post mem access * tests/qtests: override "force-legacy" for gpio virtio-mmio tests The GPIO device is a VIRTIO_F_VERSION_1 devices but running with a legacy MMIO interface we miss out that feature bit causing confusion. For the GPIO test force the mmio bus to support non-legacy so we can properly test it. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1333 Message-Id: <20221130112439.2527228-2-alex.bennee@linaro.org> Acked-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * vhost: enable vrings in vhost_dev_start() for vhost-user devices Commit 02b61f38d3 ("hw/virtio: incorporate backend features in features") properly negotiates VHOST_USER_F_PROTOCOL_FEATURES with the vhost-user backend, but we forgot to enable vrings as specified in docs/interop/vhost-user.rst: If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state. If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1. Some vhost-user front-ends already did this by calling vhost_ops.vhost_set_vring_enable() directly: - backends/cryptodev-vhost.c - hw/net/virtio-net.c - hw/virtio/vhost-user-gpio.c But most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled. Let's fix this issue by enabling the vrings in vhost_dev_start() for vhost-user front-ends that don't already do this directly. Same thing also in vhost_dev_stop() where we disable vrings. [1] https://gitlab.com/virtio-fs/virtiofsd [2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217 Fixes: 02b61f38d3 ("hw/virtio: incorporate backend features in features") Reported-by: German Maglione <gmaglione@redhat.com> Tested-by: German Maglione <gmaglione@redhat.com> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Acked-by: Raphael Norwitz <raphael.norwitz@nutanix.com> Message-Id: <20221123131630.52020-1-sgarzare@redhat.com> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221130112439.2527228-3-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * hw/virtio: add started_vu status field to vhost-user-gpio As per the fix to vhost-user-blk in f5b22d06fb (vhost: recheck dev state in the vhost_migration_log routine) we really should track the connection and starting separately. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221130112439.2527228-4-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * hw/virtio: generalise CHR_EVENT_CLOSED handling ..and use for both virtio-user-blk and virtio-user-gpio. This avoids the circular close by deferring shutdown due to disconnection until a later point. virtio-user-blk already had this mechanism in place so generalise it as a vhost-user helper function and use for both blk and gpio devices. While we are at it we also fix up vhost-user-gpio to re-establish the event handler after close down so we can reconnect later. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Raphael Norwitz <raphael.norwitz@nutanix.com> Message-Id: <20221130112439.2527228-5-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * include/hw: VM state takes precedence in virtio_device_should_start The VM status should always preempt the device status for these checks. This ensures the device is in the correct state when we suspend the VM prior to migrations. This restores the checks to the order they where in before the refactoring moved things around. While we are at it lets improve our documentation of the various fields involved and document the two functions. Fixes: 9f6bcfd99f (hw/virtio: move vm_running check to virtio_device_started) Fixes: 259d69c00b (hw/virtio: introduce virtio_device_should_start) Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Tested-by: Christian Borntraeger <borntraeger@linux.ibm.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221130112439.2527228-6-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * hw/nvme: fix aio cancel in format There are several bugs in the async cancel code for the Format command. Firstly, cancelling a format operation neglects to set iocb->ret as well as clearing the iocb->aiocb after cancelling the underlying aiocb which causes the aio callback to ignore the cancellation. Trivial fix. Secondly, and worse, because the request is queued up for posting to the CQ in a bottom half, if the cancellation is due to the submission queue being deleted (which calls blk_aio_cancel), the req structure is deallocated in nvme_del_sq prior to the bottom half being schedulued. Fix this by simply removing the bottom half, there is no reason to defer it anyway. Fixes: 3bcf26d3d619 ("hw/nvme: reimplement format nvm to allow cancellation") Reported-by: Jonathan Derrick <jonathan.derrick@linux.dev> Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: fix aio cancel in flush Make sure that iocb->aiocb is NULL'ed when cancelling. Fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: 38f4ac65ac88 ("hw/nvme: reimplement flush to allow cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: fix aio cancel in zone reset If the zone reset operation is cancelled but the block unmap operation completes normally, the callback will continue resetting the next zone since it neglects to check iocb->ret which will have been set to -ECANCELED. Make sure that this is checked and bail out if an error is present. Secondly, fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: 63d96e4ffd71 ("hw/nvme: reimplement zone reset to allow cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: fix aio cancel in dsm When the DSM operation is cancelled asynchronously, we set iocb->ret to -ECANCELED. However, the callback function only checks the return value of the completed aio, which may have completed succesfully prior to the cancellation and thus the callback ends up continuing the dsm operation instead of bailing out. Fix this. Secondly, fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: remove copy bh scheduling Fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: 796d20681d9b ("hw/nvme: reimplement the copy command to allow aio cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * target/i386: allow MMX instructions with CR4.OSFXSR=0 MMX state is saved/restored by FSAVE/FRSTOR so the instructions are not illegal opcodes even if CR4.OSFXSR=0. Make sure that validate_vex takes into account the prefix and only checks HF_OSFXSR_MASK in the presence of an SSE instruction. Fixes: 20581aadec5e ("target/i386: validate VEX prefixes via the instructions' exception classes", 2022-10-18) Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1350 Reported-by: Helge Konetzka (@hejko on gitlab.com) Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> * target/i386: Always completely initialize TranslateFault In get_physical_address, the canonical address check failed to set TranslateFault.stage2, which resulted in an uninitialized read from the struct when reporting the fault in x86_cpu_tlb_fill. Adjust all error paths to use structure assignment so that the entire struct is always initialized. Reported-by: Daniel Hoffman <dhoff749@gmail.com> Fixes: 9bbcf372193a ("target/i386: Reorg GET_HPHYS") Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Message-Id: <20221201074522.178498-1-richard.henderson@linaro.org> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1324 Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> * hw/loongarch/virt: Add cfi01 pflash device Add cfi01 pflash device for LoongArch virt machine Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221130100647.398565-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * Sync pc on breakpoints * tests/qtest/migration-test: Fix unlink error and memory leaks When running the migration test compiled with Clang from Fedora 37 and sanitizers enabled, there is an error complaining about unlink(): ../tests/qtest/migration-test.c:1072:12: runtime error: null pointer passed as argument 1, which is declared to never be null /usr/include/unistd.h:858:48: note: nonnull attribute specified here SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../tests/qtest/migration-test.c:1072:12 in (test program exited with status code 1) TAP parsing error: Too few tests run (expected 33, got 20) The data->clientcert and data->clientkey pointers can indeed be unset in some tests, so we have to check them before calling unlink() with those. While we're at it, I also noticed that the code is only freeing some but not all of the allocated strings in this function, and indeed, valgrind is also complaining about memory leaks here. So let's call g_free() on all allocated strings to avoid leaking memory here. Message-Id: <20221125083054.117504-1-thuth@redhat.com> Tested-by: Bin Meng <bmeng@tinylab.org> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Reviewed-by: Juan Quintela <quintela@redhat.com> Signed-off-by: Thomas Huth <thuth@redhat.com> * target/s390x/tcg: Fix and improve the SACF instruction The SET ADDRESS SPACE CONTROL FAST instruction is not privileged, it can be used from problem space, too. Just the switching to the home address space is privileged and should still generate a privilege exception. This bug is e.g. causing programs like Java that use the "getcpu" vdso kernel function to crash (see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=990417#26 ). While we're at it, also check if DAT is not enabled. In that case the instruction is supposed to generate a special operation exception. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/655 Message-Id: <20221201184443.136355-1-thuth@redhat.com> Reviewed-by: Ilya Leoshkevich <iii@linux.ibm.com> Reviewed-by: David Hildenbrand <david@redhat.com> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * hw/display/next-fb: Fix comment typo Signed-off-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Message-Id: <20221125160849.23711-1-evgeny.v.ermakov@gmail.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * fix dev snapshots * working syx snaps * Revert "hw/loongarch/virt: Add cfi01 pflash device" This reverts commit 14dccc8ea6ece7ee63273144fb55e4770a05e0fd. Signed-off-by: Song Gao <gaosong@loongson.cn> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221205113007.683505-1-gaosong@loongson.cn> * Update VERSION for v7.2.0-rc4 Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Stefan Weil <sw@weilnetz.de> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Signed-off-by: Cédric Le Goater <clg@kaod.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Igor Mammedov <imammedo@redhat.com> Signed-off-by: Ani Sinha <ani@anisinha.ca> Signed-off-by: John Snow <jsnow@redhat.com> Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Claudio Fontana <cfontana@suse.de> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru> Signed-off-by: Dongwon Kim <dongwon.kim@intel.com> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Co-authored-by: Stefan Weil <sw@weilnetz.de> Co-authored-by: Cédric Le Goater <clg@kaod.org> Co-authored-by: Alex Bennée <alex.bennee@linaro.org> Co-authored-by: Peter Maydell <peter.maydell@linaro.org> Co-authored-by: Stefano Garzarella <sgarzare@redhat.com> Co-authored-by: Igor Mammedov <imammedo@redhat.com> Co-authored-by: Ani Sinha <ani@anisinha.ca> Co-authored-by: John Snow <jsnow@redhat.com> Co-authored-by: Michael S. Tsirkin <mst@redhat.com> Co-authored-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Co-authored-by: Stefan Hajnoczi <stefanha@redhat.com> Co-authored-by: Ard Biesheuvel <ardb@kernel.org> Co-authored-by: Thomas Huth <thuth@redhat.com> Co-authored-by: Joelle van Dyne <j@getutm.app> Co-authored-by: Claudio Fontana <cfontana@suse.de> Co-authored-by: Michael Tokarev <mjt@tls.msk.ru> Co-authored-by: Dongwon Kim <dongwon.kim@intel.com> Co-authored-by: Marc Hartmayer <mhartmay@linux.ibm.com> Co-authored-by: Stefan Weil via <qemu-devel@nongnu.org> Co-authored-by: Gerd Hoffmann <kraxel@redhat.com> Co-authored-by: Richard Henderson <richard.henderson@linaro.org> Co-authored-by: Philippe Mathieu-Daudé <philmd@linaro.org> Co-authored-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Co-authored-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Co-authored-by: Klaus Jensen <k.jensen@samsung.com> Co-authored-by: Paolo Bonzini <pbonzini@redhat.com> Co-authored-by: Song Gao <gaosong@loongson.cn>
2631 lines
85 KiB
C
2631 lines
85 KiB
C
/*
|
|
* USB redirector usb-guest
|
|
*
|
|
* Copyright (c) 2011-2012 Red Hat, Inc.
|
|
*
|
|
* Red Hat Authors:
|
|
* Hans de Goede <hdegoede@redhat.com>
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "qemu/osdep.h"
|
|
#include "qemu/cutils.h"
|
|
#include "qemu/units.h"
|
|
#include "qapi/error.h"
|
|
#include "qemu/timer.h"
|
|
#include "sysemu/runstate.h"
|
|
#include "sysemu/sysemu.h"
|
|
#include "qapi/qmp/qerror.h"
|
|
#include "qemu/error-report.h"
|
|
#include "qemu/iov.h"
|
|
#include "qemu/module.h"
|
|
#include "chardev/char-fe.h"
|
|
|
|
#include <usbredirparser.h>
|
|
#include <usbredirfilter.h>
|
|
|
|
#include "hw/qdev-properties.h"
|
|
#include "hw/qdev-properties-system.h"
|
|
#include "hw/usb.h"
|
|
#include "migration/qemu-file-types.h"
|
|
#include "migration/vmstate.h"
|
|
#include "qom/object.h"
|
|
|
|
/* ERROR is defined below. Remove any previous definition. */
|
|
#undef ERROR
|
|
|
|
#define MAX_ENDPOINTS 32
|
|
#define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */
|
|
#define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
|
|
#define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
|
|
#define USBEP2I(usb_ep) (((usb_ep)->pid == USB_TOKEN_IN) ? \
|
|
((usb_ep)->nr | 0x10) : ((usb_ep)->nr))
|
|
#define I2USBEP(d, i) (usb_ep_get(&(d)->dev, \
|
|
((i) & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, \
|
|
(i) & 0x0f))
|
|
|
|
#ifndef USBREDIR_VERSION /* This is not defined in older usbredir versions */
|
|
#define USBREDIR_VERSION 0
|
|
#endif
|
|
|
|
typedef struct USBRedirDevice USBRedirDevice;
|
|
|
|
/* Struct to hold buffered packets */
|
|
struct buf_packet {
|
|
uint8_t *data;
|
|
void *free_on_destroy;
|
|
uint16_t len;
|
|
uint16_t offset;
|
|
uint8_t status;
|
|
QTAILQ_ENTRY(buf_packet)next;
|
|
};
|
|
|
|
struct endp_data {
|
|
USBRedirDevice *dev;
|
|
uint8_t type;
|
|
uint8_t interval;
|
|
uint8_t interface; /* bInterfaceNumber this ep belongs to */
|
|
uint16_t max_packet_size; /* In bytes, not wMaxPacketSize format !! */
|
|
uint32_t max_streams;
|
|
uint8_t iso_started;
|
|
uint8_t iso_error; /* For reporting iso errors to the HC */
|
|
uint8_t interrupt_started;
|
|
uint8_t interrupt_error;
|
|
uint8_t bulk_receiving_enabled;
|
|
uint8_t bulk_receiving_started;
|
|
uint8_t bufpq_prefilled;
|
|
uint8_t bufpq_dropping_packets;
|
|
QTAILQ_HEAD(, buf_packet) bufpq;
|
|
int32_t bufpq_size;
|
|
int32_t bufpq_target_size;
|
|
USBPacket *pending_async_packet;
|
|
};
|
|
|
|
struct PacketIdQueueEntry {
|
|
uint64_t id;
|
|
QTAILQ_ENTRY(PacketIdQueueEntry)next;
|
|
};
|
|
|
|
struct PacketIdQueue {
|
|
USBRedirDevice *dev;
|
|
const char *name;
|
|
QTAILQ_HEAD(, PacketIdQueueEntry) head;
|
|
int size;
|
|
};
|
|
|
|
struct USBRedirDevice {
|
|
USBDevice dev;
|
|
/* Properties */
|
|
CharBackend cs;
|
|
bool enable_streams;
|
|
bool suppress_remote_wake;
|
|
bool in_write;
|
|
uint8_t debug;
|
|
int32_t bootindex;
|
|
char *filter_str;
|
|
/* Data passed from chardev the fd_read cb to the usbredirparser read cb */
|
|
const uint8_t *read_buf;
|
|
int read_buf_size;
|
|
/* Active chardev-watch-tag */
|
|
guint watch;
|
|
/* For async handling of close / reject */
|
|
QEMUBH *chardev_close_bh;
|
|
QEMUBH *device_reject_bh;
|
|
/* To delay the usb attach in case of quick chardev close + open */
|
|
QEMUTimer *attach_timer;
|
|
int64_t next_attach_time;
|
|
struct usbredirparser *parser;
|
|
struct endp_data endpoint[MAX_ENDPOINTS];
|
|
struct PacketIdQueue cancelled;
|
|
struct PacketIdQueue already_in_flight;
|
|
void (*buffered_bulk_in_complete)(USBRedirDevice *, USBPacket *, uint8_t);
|
|
/* Data for device filtering */
|
|
struct usb_redir_device_connect_header device_info;
|
|
struct usb_redir_interface_info_header interface_info;
|
|
struct usbredirfilter_rule *filter_rules;
|
|
int filter_rules_count;
|
|
int compatible_speedmask;
|
|
VMChangeStateEntry *vmstate;
|
|
};
|
|
|
|
#define TYPE_USB_REDIR "usb-redir"
|
|
DECLARE_INSTANCE_CHECKER(USBRedirDevice, USB_REDIRECT,
|
|
TYPE_USB_REDIR)
|
|
|
|
static void usbredir_hello(void *priv, struct usb_redir_hello_header *h);
|
|
static void usbredir_device_connect(void *priv,
|
|
struct usb_redir_device_connect_header *device_connect);
|
|
static void usbredir_device_disconnect(void *priv);
|
|
static void usbredir_interface_info(void *priv,
|
|
struct usb_redir_interface_info_header *interface_info);
|
|
static void usbredir_ep_info(void *priv,
|
|
struct usb_redir_ep_info_header *ep_info);
|
|
static void usbredir_configuration_status(void *priv, uint64_t id,
|
|
struct usb_redir_configuration_status_header *configuration_status);
|
|
static void usbredir_alt_setting_status(void *priv, uint64_t id,
|
|
struct usb_redir_alt_setting_status_header *alt_setting_status);
|
|
static void usbredir_iso_stream_status(void *priv, uint64_t id,
|
|
struct usb_redir_iso_stream_status_header *iso_stream_status);
|
|
static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
|
|
struct usb_redir_interrupt_receiving_status_header
|
|
*interrupt_receiving_status);
|
|
static void usbredir_bulk_streams_status(void *priv, uint64_t id,
|
|
struct usb_redir_bulk_streams_status_header *bulk_streams_status);
|
|
static void usbredir_bulk_receiving_status(void *priv, uint64_t id,
|
|
struct usb_redir_bulk_receiving_status_header *bulk_receiving_status);
|
|
static void usbredir_control_packet(void *priv, uint64_t id,
|
|
struct usb_redir_control_packet_header *control_packet,
|
|
uint8_t *data, int data_len);
|
|
static void usbredir_bulk_packet(void *priv, uint64_t id,
|
|
struct usb_redir_bulk_packet_header *bulk_packet,
|
|
uint8_t *data, int data_len);
|
|
static void usbredir_iso_packet(void *priv, uint64_t id,
|
|
struct usb_redir_iso_packet_header *iso_packet,
|
|
uint8_t *data, int data_len);
|
|
static void usbredir_interrupt_packet(void *priv, uint64_t id,
|
|
struct usb_redir_interrupt_packet_header *interrupt_header,
|
|
uint8_t *data, int data_len);
|
|
static void usbredir_buffered_bulk_packet(void *priv, uint64_t id,
|
|
struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet,
|
|
uint8_t *data, int data_len);
|
|
|
|
static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p,
|
|
int status);
|
|
|
|
#define VERSION "qemu usb-redir guest " QEMU_VERSION
|
|
|
|
/*
|
|
* Logging stuff
|
|
*/
|
|
|
|
#define ERROR(...) \
|
|
do { \
|
|
if (dev->debug >= usbredirparser_error) { \
|
|
error_report("usb-redir error: " __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
#define WARNING(...) \
|
|
do { \
|
|
if (dev->debug >= usbredirparser_warning) { \
|
|
warn_report("" __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
#define INFO(...) \
|
|
do { \
|
|
if (dev->debug >= usbredirparser_info) { \
|
|
error_report("usb-redir: " __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
#define DPRINTF(...) \
|
|
do { \
|
|
if (dev->debug >= usbredirparser_debug) { \
|
|
error_report("usb-redir: " __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
#define DPRINTF2(...) \
|
|
do { \
|
|
if (dev->debug >= usbredirparser_debug_data) { \
|
|
error_report("usb-redir: " __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
|
|
static void usbredir_log(void *priv, int level, const char *msg)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
if (dev->debug < level) {
|
|
return;
|
|
}
|
|
|
|
error_report("%s", msg);
|
|
}
|
|
|
|
static void usbredir_log_data(USBRedirDevice *dev, const char *desc,
|
|
const uint8_t *data, int len)
|
|
{
|
|
if (dev->debug < usbredirparser_debug_data) {
|
|
return;
|
|
}
|
|
qemu_hexdump(stderr, desc, data, len);
|
|
}
|
|
|
|
/*
|
|
* usbredirparser io functions
|
|
*/
|
|
|
|
static int usbredir_read(void *priv, uint8_t *data, int count)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
if (dev->read_buf_size < count) {
|
|
count = dev->read_buf_size;
|
|
}
|
|
|
|
memcpy(data, dev->read_buf, count);
|
|
|
|
dev->read_buf_size -= count;
|
|
if (dev->read_buf_size) {
|
|
dev->read_buf += count;
|
|
} else {
|
|
dev->read_buf = NULL;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
static gboolean usbredir_write_unblocked(void *do_not_use, GIOCondition cond,
|
|
void *opaque)
|
|
{
|
|
USBRedirDevice *dev = opaque;
|
|
|
|
dev->watch = 0;
|
|
usbredirparser_do_write(dev->parser);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
static int usbredir_write(void *priv, uint8_t *data, int count)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
int r;
|
|
|
|
if (!qemu_chr_fe_backend_open(&dev->cs)) {
|
|
return 0;
|
|
}
|
|
|
|
/* Don't send new data to the chardev until our state is fully synced */
|
|
if (!runstate_check(RUN_STATE_RUNNING)) {
|
|
return 0;
|
|
}
|
|
|
|
/* Recursion check */
|
|
if (dev->in_write) {
|
|
DPRINTF("usbredir_write recursion\n");
|
|
return 0;
|
|
}
|
|
dev->in_write = true;
|
|
|
|
r = qemu_chr_fe_write(&dev->cs, data, count);
|
|
if (r < count) {
|
|
if (!dev->watch) {
|
|
dev->watch = qemu_chr_fe_add_watch(&dev->cs, G_IO_OUT | G_IO_HUP,
|
|
usbredir_write_unblocked, dev);
|
|
}
|
|
if (r < 0) {
|
|
r = 0;
|
|
}
|
|
}
|
|
dev->in_write = false;
|
|
return r;
|
|
}
|
|
|
|
/*
|
|
* Cancelled and buffered packets helpers
|
|
*/
|
|
|
|
static void packet_id_queue_init(struct PacketIdQueue *q,
|
|
USBRedirDevice *dev, const char *name)
|
|
{
|
|
q->dev = dev;
|
|
q->name = name;
|
|
QTAILQ_INIT(&q->head);
|
|
q->size = 0;
|
|
}
|
|
|
|
static void packet_id_queue_add(struct PacketIdQueue *q, uint64_t id)
|
|
{
|
|
USBRedirDevice *dev = q->dev;
|
|
struct PacketIdQueueEntry *e;
|
|
|
|
DPRINTF("adding packet id %"PRIu64" to %s queue\n", id, q->name);
|
|
|
|
e = g_new0(struct PacketIdQueueEntry, 1);
|
|
e->id = id;
|
|
QTAILQ_INSERT_TAIL(&q->head, e, next);
|
|
q->size++;
|
|
}
|
|
|
|
static int packet_id_queue_remove(struct PacketIdQueue *q, uint64_t id)
|
|
{
|
|
USBRedirDevice *dev = q->dev;
|
|
struct PacketIdQueueEntry *e;
|
|
|
|
QTAILQ_FOREACH(e, &q->head, next) {
|
|
if (e->id == id) {
|
|
DPRINTF("removing packet id %"PRIu64" from %s queue\n",
|
|
id, q->name);
|
|
QTAILQ_REMOVE(&q->head, e, next);
|
|
q->size--;
|
|
g_free(e);
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void packet_id_queue_empty(struct PacketIdQueue *q)
|
|
{
|
|
USBRedirDevice *dev = q->dev;
|
|
struct PacketIdQueueEntry *e, *next_e;
|
|
|
|
DPRINTF("removing %d packet-ids from %s queue\n", q->size, q->name);
|
|
|
|
QTAILQ_FOREACH_SAFE(e, &q->head, next, next_e) {
|
|
QTAILQ_REMOVE(&q->head, e, next);
|
|
g_free(e);
|
|
}
|
|
q->size = 0;
|
|
}
|
|
|
|
static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
int i = USBEP2I(p->ep);
|
|
|
|
if (p->combined) {
|
|
usb_combined_packet_cancel(udev, p);
|
|
return;
|
|
}
|
|
|
|
if (dev->endpoint[i].pending_async_packet) {
|
|
assert(dev->endpoint[i].pending_async_packet == p);
|
|
dev->endpoint[i].pending_async_packet = NULL;
|
|
return;
|
|
}
|
|
|
|
packet_id_queue_add(&dev->cancelled, p->id);
|
|
usbredirparser_send_cancel_data_packet(dev->parser, p->id);
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
|
|
static int usbredir_is_cancelled(USBRedirDevice *dev, uint64_t id)
|
|
{
|
|
if (!dev->dev.attached) {
|
|
return 1; /* Treat everything as cancelled after a disconnect */
|
|
}
|
|
return packet_id_queue_remove(&dev->cancelled, id);
|
|
}
|
|
|
|
static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice *dev,
|
|
struct USBEndpoint *ep)
|
|
{
|
|
static USBPacket *p;
|
|
|
|
/* async handled packets for bulk receiving eps do not count as inflight */
|
|
if (dev->endpoint[USBEP2I(ep)].bulk_receiving_started) {
|
|
return;
|
|
}
|
|
|
|
QTAILQ_FOREACH(p, &ep->queue, queue) {
|
|
/* Skip combined packets, except for the first */
|
|
if (p->combined && p != p->combined->first) {
|
|
continue;
|
|
}
|
|
if (p->state == USB_PACKET_ASYNC) {
|
|
packet_id_queue_add(&dev->already_in_flight, p->id);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void usbredir_fill_already_in_flight(USBRedirDevice *dev)
|
|
{
|
|
int ep;
|
|
struct USBDevice *udev = &dev->dev;
|
|
|
|
usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_ctl);
|
|
|
|
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
|
|
usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_in[ep]);
|
|
usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_out[ep]);
|
|
}
|
|
}
|
|
|
|
static int usbredir_already_in_flight(USBRedirDevice *dev, uint64_t id)
|
|
{
|
|
return packet_id_queue_remove(&dev->already_in_flight, id);
|
|
}
|
|
|
|
static USBPacket *usbredir_find_packet_by_id(USBRedirDevice *dev,
|
|
uint8_t ep, uint64_t id)
|
|
{
|
|
USBPacket *p;
|
|
|
|
if (usbredir_is_cancelled(dev, id)) {
|
|
return NULL;
|
|
}
|
|
|
|
p = usb_ep_find_packet_by_id(&dev->dev,
|
|
(ep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT,
|
|
ep & 0x0f, id);
|
|
if (p == NULL) {
|
|
ERROR("could not find packet with id %"PRIu64"\n", id);
|
|
}
|
|
return p;
|
|
}
|
|
|
|
static int bufp_alloc(USBRedirDevice *dev, uint8_t *data, uint16_t len,
|
|
uint8_t status, uint8_t ep, void *free_on_destroy)
|
|
{
|
|
struct buf_packet *bufp;
|
|
|
|
if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets &&
|
|
dev->endpoint[EP2I(ep)].bufpq_size >
|
|
2 * dev->endpoint[EP2I(ep)].bufpq_target_size) {
|
|
DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1;
|
|
}
|
|
/* Since we're interupting the stream anyways, drop enough packets to get
|
|
back to our target buffer size */
|
|
if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) {
|
|
if (dev->endpoint[EP2I(ep)].bufpq_size >
|
|
dev->endpoint[EP2I(ep)].bufpq_target_size) {
|
|
free(free_on_destroy);
|
|
return -1;
|
|
}
|
|
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
|
|
}
|
|
|
|
bufp = g_new(struct buf_packet, 1);
|
|
bufp->data = data;
|
|
bufp->len = len;
|
|
bufp->offset = 0;
|
|
bufp->status = status;
|
|
bufp->free_on_destroy = free_on_destroy;
|
|
QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
|
|
dev->endpoint[EP2I(ep)].bufpq_size++;
|
|
return 0;
|
|
}
|
|
|
|
static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp,
|
|
uint8_t ep)
|
|
{
|
|
QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
|
|
dev->endpoint[EP2I(ep)].bufpq_size--;
|
|
free(bufp->free_on_destroy);
|
|
g_free(bufp);
|
|
}
|
|
|
|
static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep)
|
|
{
|
|
struct buf_packet *buf, *buf_next;
|
|
|
|
QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) {
|
|
bufp_free(dev, buf, ep);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* USBDevice callbacks
|
|
*/
|
|
|
|
static void usbredir_handle_reset(USBDevice *udev)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
|
|
DPRINTF("reset device\n");
|
|
usbredirparser_send_reset(dev->parser);
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
|
|
static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
|
|
uint8_t ep)
|
|
{
|
|
int status, len;
|
|
if (!dev->endpoint[EP2I(ep)].iso_started &&
|
|
!dev->endpoint[EP2I(ep)].iso_error) {
|
|
struct usb_redir_start_iso_stream_header start_iso = {
|
|
.endpoint = ep,
|
|
};
|
|
int pkts_per_sec;
|
|
|
|
if (dev->dev.speed == USB_SPEED_HIGH) {
|
|
pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval;
|
|
} else {
|
|
pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval;
|
|
}
|
|
/* Testing has shown that we need circa 60 ms buffer */
|
|
dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000;
|
|
|
|
/* Aim for approx 100 interrupts / second on the client to
|
|
balance latency and interrupt load */
|
|
start_iso.pkts_per_urb = pkts_per_sec / 100;
|
|
if (start_iso.pkts_per_urb < 1) {
|
|
start_iso.pkts_per_urb = 1;
|
|
} else if (start_iso.pkts_per_urb > 32) {
|
|
start_iso.pkts_per_urb = 32;
|
|
}
|
|
|
|
start_iso.no_urbs = DIV_ROUND_UP(
|
|
dev->endpoint[EP2I(ep)].bufpq_target_size,
|
|
start_iso.pkts_per_urb);
|
|
/* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
|
|
as overflow buffer. Also see the usbredir protocol documentation */
|
|
if (!(ep & USB_DIR_IN)) {
|
|
start_iso.no_urbs *= 2;
|
|
}
|
|
if (start_iso.no_urbs > 16) {
|
|
start_iso.no_urbs = 16;
|
|
}
|
|
|
|
/* No id, we look at the ep when receiving a status back */
|
|
usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso);
|
|
usbredirparser_do_write(dev->parser);
|
|
DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
|
|
pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep);
|
|
dev->endpoint[EP2I(ep)].iso_started = 1;
|
|
dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
|
|
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
|
|
}
|
|
|
|
if (ep & USB_DIR_IN) {
|
|
struct buf_packet *isop;
|
|
|
|
if (dev->endpoint[EP2I(ep)].iso_started &&
|
|
!dev->endpoint[EP2I(ep)].bufpq_prefilled) {
|
|
if (dev->endpoint[EP2I(ep)].bufpq_size <
|
|
dev->endpoint[EP2I(ep)].bufpq_target_size) {
|
|
return;
|
|
}
|
|
dev->endpoint[EP2I(ep)].bufpq_prefilled = 1;
|
|
}
|
|
|
|
isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq);
|
|
if (isop == NULL) {
|
|
DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
|
|
ep, dev->endpoint[EP2I(ep)].iso_error);
|
|
/* Re-fill the buffer */
|
|
dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
|
|
/* Check iso_error for stream errors, otherwise its an underrun */
|
|
status = dev->endpoint[EP2I(ep)].iso_error;
|
|
dev->endpoint[EP2I(ep)].iso_error = 0;
|
|
p->status = status ? USB_RET_IOERROR : USB_RET_SUCCESS;
|
|
return;
|
|
}
|
|
DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep,
|
|
isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size);
|
|
|
|
status = isop->status;
|
|
len = isop->len;
|
|
if (len > p->iov.size) {
|
|
ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
|
|
ep, len, (int)p->iov.size);
|
|
len = p->iov.size;
|
|
status = usb_redir_babble;
|
|
}
|
|
usb_packet_copy(p, isop->data, len);
|
|
bufp_free(dev, isop, ep);
|
|
usbredir_handle_status(dev, p, status);
|
|
} else {
|
|
/* If the stream was not started because of a pending error don't
|
|
send the packet to the usb-host */
|
|
if (dev->endpoint[EP2I(ep)].iso_started) {
|
|
struct usb_redir_iso_packet_header iso_packet = {
|
|
.endpoint = ep,
|
|
.length = p->iov.size
|
|
};
|
|
g_autofree uint8_t *buf = g_malloc(p->iov.size);
|
|
/* No id, we look at the ep when receiving a status back */
|
|
usb_packet_copy(p, buf, p->iov.size);
|
|
usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet,
|
|
buf, p->iov.size);
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
status = dev->endpoint[EP2I(ep)].iso_error;
|
|
dev->endpoint[EP2I(ep)].iso_error = 0;
|
|
DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status,
|
|
p->iov.size);
|
|
usbredir_handle_status(dev, p, status);
|
|
}
|
|
}
|
|
|
|
static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep)
|
|
{
|
|
struct usb_redir_stop_iso_stream_header stop_iso_stream = {
|
|
.endpoint = ep
|
|
};
|
|
if (dev->endpoint[EP2I(ep)].iso_started) {
|
|
usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream);
|
|
DPRINTF("iso stream stopped ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].iso_started = 0;
|
|
}
|
|
dev->endpoint[EP2I(ep)].iso_error = 0;
|
|
usbredir_free_bufpq(dev, ep);
|
|
}
|
|
|
|
/*
|
|
* The usb-host may poll the endpoint faster then our guest, resulting in lots
|
|
* of smaller bulkp-s. The below buffered_bulk_in_complete* functions combine
|
|
* data from multiple bulkp-s into a single packet, avoiding bufpq overflows.
|
|
*/
|
|
static void usbredir_buffered_bulk_add_data_to_packet(USBRedirDevice *dev,
|
|
struct buf_packet *bulkp, int count, USBPacket *p, uint8_t ep)
|
|
{
|
|
usb_packet_copy(p, bulkp->data + bulkp->offset, count);
|
|
bulkp->offset += count;
|
|
if (bulkp->offset == bulkp->len) {
|
|
/* Store status in the last packet with data from this bulkp */
|
|
usbredir_handle_status(dev, p, bulkp->status);
|
|
bufp_free(dev, bulkp, ep);
|
|
}
|
|
}
|
|
|
|
static void usbredir_buffered_bulk_in_complete_raw(USBRedirDevice *dev,
|
|
USBPacket *p, uint8_t ep)
|
|
{
|
|
struct buf_packet *bulkp;
|
|
int count;
|
|
|
|
while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) &&
|
|
p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) {
|
|
count = bulkp->len - bulkp->offset;
|
|
if (count > (p->iov.size - p->actual_length)) {
|
|
count = p->iov.size - p->actual_length;
|
|
}
|
|
usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep);
|
|
}
|
|
}
|
|
|
|
static void usbredir_buffered_bulk_in_complete_ftdi(USBRedirDevice *dev,
|
|
USBPacket *p, uint8_t ep)
|
|
{
|
|
const int maxp = dev->endpoint[EP2I(ep)].max_packet_size;
|
|
uint8_t header[2] = { 0, 0 };
|
|
struct buf_packet *bulkp;
|
|
int count;
|
|
|
|
while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) &&
|
|
p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) {
|
|
if (bulkp->len < 2) {
|
|
WARNING("malformed ftdi bulk in packet\n");
|
|
bufp_free(dev, bulkp, ep);
|
|
continue;
|
|
}
|
|
|
|
if ((p->actual_length % maxp) == 0) {
|
|
usb_packet_copy(p, bulkp->data, 2);
|
|
memcpy(header, bulkp->data, 2);
|
|
} else {
|
|
if (bulkp->data[0] != header[0] || bulkp->data[1] != header[1]) {
|
|
break; /* Different header, add to next packet */
|
|
}
|
|
}
|
|
|
|
if (bulkp->offset == 0) {
|
|
bulkp->offset = 2; /* Skip header */
|
|
}
|
|
count = bulkp->len - bulkp->offset;
|
|
/* Must repeat the header at maxp interval */
|
|
if (count > (maxp - (p->actual_length % maxp))) {
|
|
count = maxp - (p->actual_length % maxp);
|
|
}
|
|
usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep);
|
|
}
|
|
}
|
|
|
|
static void usbredir_buffered_bulk_in_complete(USBRedirDevice *dev,
|
|
USBPacket *p, uint8_t ep)
|
|
{
|
|
p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
|
|
dev->buffered_bulk_in_complete(dev, p, ep);
|
|
DPRINTF("bulk-token-in ep %02X status %d len %d id %"PRIu64"\n",
|
|
ep, p->status, p->actual_length, p->id);
|
|
}
|
|
|
|
static void usbredir_handle_buffered_bulk_in_data(USBRedirDevice *dev,
|
|
USBPacket *p, uint8_t ep)
|
|
{
|
|
/* Input bulk endpoint, buffered packet input */
|
|
if (!dev->endpoint[EP2I(ep)].bulk_receiving_started) {
|
|
int bpt;
|
|
struct usb_redir_start_bulk_receiving_header start = {
|
|
.endpoint = ep,
|
|
.stream_id = 0,
|
|
.no_transfers = 5,
|
|
};
|
|
/* Round bytes_per_transfer up to a multiple of max_packet_size */
|
|
bpt = 512 + dev->endpoint[EP2I(ep)].max_packet_size - 1;
|
|
bpt /= dev->endpoint[EP2I(ep)].max_packet_size;
|
|
bpt *= dev->endpoint[EP2I(ep)].max_packet_size;
|
|
start.bytes_per_transfer = bpt;
|
|
/* No id, we look at the ep when receiving a status back */
|
|
usbredirparser_send_start_bulk_receiving(dev->parser, 0, &start);
|
|
usbredirparser_do_write(dev->parser);
|
|
DPRINTF("bulk receiving started bytes/transfer %u count %d ep %02X\n",
|
|
start.bytes_per_transfer, start.no_transfers, ep);
|
|
dev->endpoint[EP2I(ep)].bulk_receiving_started = 1;
|
|
/* We don't really want to drop bulk packets ever, but
|
|
having some upper limit to how much we buffer is good. */
|
|
dev->endpoint[EP2I(ep)].bufpq_target_size = 5000;
|
|
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
|
|
}
|
|
|
|
if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) {
|
|
DPRINTF("bulk-token-in ep %02X, no bulkp\n", ep);
|
|
assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL);
|
|
dev->endpoint[EP2I(ep)].pending_async_packet = p;
|
|
p->status = USB_RET_ASYNC;
|
|
return;
|
|
}
|
|
usbredir_buffered_bulk_in_complete(dev, p, ep);
|
|
}
|
|
|
|
static void usbredir_stop_bulk_receiving(USBRedirDevice *dev, uint8_t ep)
|
|
{
|
|
struct usb_redir_stop_bulk_receiving_header stop_bulk = {
|
|
.endpoint = ep,
|
|
.stream_id = 0,
|
|
};
|
|
if (dev->endpoint[EP2I(ep)].bulk_receiving_started) {
|
|
usbredirparser_send_stop_bulk_receiving(dev->parser, 0, &stop_bulk);
|
|
DPRINTF("bulk receiving stopped ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].bulk_receiving_started = 0;
|
|
}
|
|
usbredir_free_bufpq(dev, ep);
|
|
}
|
|
|
|
static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
|
|
uint8_t ep)
|
|
{
|
|
struct usb_redir_bulk_packet_header bulk_packet;
|
|
size_t size = usb_packet_size(p);
|
|
const int maxp = dev->endpoint[EP2I(ep)].max_packet_size;
|
|
|
|
if (usbredir_already_in_flight(dev, p->id)) {
|
|
p->status = USB_RET_ASYNC;
|
|
return;
|
|
}
|
|
|
|
if (dev->endpoint[EP2I(ep)].bulk_receiving_enabled) {
|
|
if (size != 0 && (size % maxp) == 0) {
|
|
usbredir_handle_buffered_bulk_in_data(dev, p, ep);
|
|
return;
|
|
}
|
|
WARNING("bulk recv invalid size %zd ep %02x, disabling\n", size, ep);
|
|
assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL);
|
|
usbredir_stop_bulk_receiving(dev, ep);
|
|
dev->endpoint[EP2I(ep)].bulk_receiving_enabled = 0;
|
|
}
|
|
|
|
DPRINTF("bulk-out ep %02X stream %u len %zd id %"PRIu64"\n",
|
|
ep, p->stream, size, p->id);
|
|
|
|
bulk_packet.endpoint = ep;
|
|
bulk_packet.length = size;
|
|
bulk_packet.stream_id = p->stream;
|
|
bulk_packet.length_high = size >> 16;
|
|
assert(bulk_packet.length_high == 0 ||
|
|
usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_32bits_bulk_length));
|
|
|
|
if (ep & USB_DIR_IN || size == 0) {
|
|
usbredirparser_send_bulk_packet(dev->parser, p->id,
|
|
&bulk_packet, NULL, 0);
|
|
} else {
|
|
g_autofree uint8_t *buf = g_malloc(size);
|
|
usb_packet_copy(p, buf, size);
|
|
usbredir_log_data(dev, "bulk data out:", buf, size);
|
|
usbredirparser_send_bulk_packet(dev->parser, p->id,
|
|
&bulk_packet, buf, size);
|
|
}
|
|
usbredirparser_do_write(dev->parser);
|
|
p->status = USB_RET_ASYNC;
|
|
}
|
|
|
|
static void usbredir_handle_interrupt_in_data(USBRedirDevice *dev,
|
|
USBPacket *p, uint8_t ep)
|
|
{
|
|
/* Input interrupt endpoint, buffered packet input */
|
|
struct buf_packet *intp, *intp_to_free;
|
|
int status, len, sum;
|
|
|
|
if (!dev->endpoint[EP2I(ep)].interrupt_started &&
|
|
!dev->endpoint[EP2I(ep)].interrupt_error) {
|
|
struct usb_redir_start_interrupt_receiving_header start_int = {
|
|
.endpoint = ep,
|
|
};
|
|
/* No id, we look at the ep when receiving a status back */
|
|
usbredirparser_send_start_interrupt_receiving(dev->parser, 0,
|
|
&start_int);
|
|
usbredirparser_do_write(dev->parser);
|
|
DPRINTF("interrupt recv started ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].interrupt_started = 1;
|
|
/* We don't really want to drop interrupt packets ever, but
|
|
having some upper limit to how much we buffer is good. */
|
|
dev->endpoint[EP2I(ep)].bufpq_target_size = 1000;
|
|
dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
|
|
}
|
|
|
|
/* check for completed interrupt message (with all fragments) */
|
|
sum = 0;
|
|
QTAILQ_FOREACH(intp, &dev->endpoint[EP2I(ep)].bufpq, next) {
|
|
sum += intp->len;
|
|
if (intp->len < dev->endpoint[EP2I(ep)].max_packet_size ||
|
|
sum >= p->iov.size)
|
|
break;
|
|
}
|
|
|
|
if (intp == NULL) {
|
|
DPRINTF2("interrupt-token-in ep %02X, no intp, buffered %d\n", ep, sum);
|
|
/* Check interrupt_error for stream errors */
|
|
status = dev->endpoint[EP2I(ep)].interrupt_error;
|
|
dev->endpoint[EP2I(ep)].interrupt_error = 0;
|
|
if (status) {
|
|
usbredir_handle_status(dev, p, status);
|
|
} else {
|
|
p->status = USB_RET_NAK;
|
|
}
|
|
return;
|
|
}
|
|
|
|
/* copy of completed interrupt message */
|
|
sum = 0;
|
|
status = usb_redir_success;
|
|
intp_to_free = NULL;
|
|
QTAILQ_FOREACH(intp, &dev->endpoint[EP2I(ep)].bufpq, next) {
|
|
if (intp_to_free) {
|
|
bufp_free(dev, intp_to_free, ep);
|
|
}
|
|
DPRINTF("interrupt-token-in ep %02X fragment status %d len %d\n", ep,
|
|
intp->status, intp->len);
|
|
|
|
sum += intp->len;
|
|
len = intp->len;
|
|
if (status == usb_redir_success) {
|
|
status = intp->status;
|
|
}
|
|
if (sum > p->iov.size) {
|
|
ERROR("received int data is larger then packet ep %02X\n", ep);
|
|
len -= (sum - p->iov.size);
|
|
sum = p->iov.size;
|
|
status = usb_redir_babble;
|
|
}
|
|
|
|
usb_packet_copy(p, intp->data, len);
|
|
|
|
intp_to_free = intp;
|
|
if (intp->len < dev->endpoint[EP2I(ep)].max_packet_size ||
|
|
sum >= p->iov.size)
|
|
break;
|
|
}
|
|
if (intp_to_free) {
|
|
bufp_free(dev, intp_to_free, ep);
|
|
}
|
|
DPRINTF("interrupt-token-in ep %02X summary status %d len %d\n", ep,
|
|
status, sum);
|
|
usbredir_handle_status(dev, p, status);
|
|
}
|
|
|
|
/*
|
|
* Handle interrupt out data, the usbredir protocol expects us to do this
|
|
* async, so that it can report back a completion status. But guests will
|
|
* expect immediate completion for an interrupt endpoint, and handling this
|
|
* async causes migration issues. So we report success directly, counting
|
|
* on the fact that output interrupt packets normally always succeed.
|
|
*/
|
|
static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev,
|
|
USBPacket *p, uint8_t ep)
|
|
{
|
|
struct usb_redir_interrupt_packet_header interrupt_packet;
|
|
g_autofree uint8_t *buf = g_malloc(p->iov.size);
|
|
|
|
DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep,
|
|
p->iov.size, p->id);
|
|
|
|
interrupt_packet.endpoint = ep;
|
|
interrupt_packet.length = p->iov.size;
|
|
|
|
usb_packet_copy(p, buf, p->iov.size);
|
|
usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size);
|
|
usbredirparser_send_interrupt_packet(dev->parser, p->id,
|
|
&interrupt_packet, buf, p->iov.size);
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
|
|
static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev,
|
|
uint8_t ep)
|
|
{
|
|
struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = {
|
|
.endpoint = ep
|
|
};
|
|
if (dev->endpoint[EP2I(ep)].interrupt_started) {
|
|
usbredirparser_send_stop_interrupt_receiving(dev->parser, 0,
|
|
&stop_interrupt_recv);
|
|
DPRINTF("interrupt recv stopped ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].interrupt_started = 0;
|
|
}
|
|
dev->endpoint[EP2I(ep)].interrupt_error = 0;
|
|
usbredir_free_bufpq(dev, ep);
|
|
}
|
|
|
|
static void usbredir_handle_data(USBDevice *udev, USBPacket *p)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
uint8_t ep;
|
|
|
|
ep = p->ep->nr;
|
|
if (p->pid == USB_TOKEN_IN) {
|
|
ep |= USB_DIR_IN;
|
|
}
|
|
|
|
switch (dev->endpoint[EP2I(ep)].type) {
|
|
case USB_ENDPOINT_XFER_CONTROL:
|
|
ERROR("handle_data called for control transfer on ep %02X\n", ep);
|
|
p->status = USB_RET_NAK;
|
|
break;
|
|
case USB_ENDPOINT_XFER_BULK:
|
|
if (p->state == USB_PACKET_SETUP && p->pid == USB_TOKEN_IN &&
|
|
p->ep->pipeline) {
|
|
p->status = USB_RET_ADD_TO_QUEUE;
|
|
break;
|
|
}
|
|
usbredir_handle_bulk_data(dev, p, ep);
|
|
break;
|
|
case USB_ENDPOINT_XFER_ISOC:
|
|
usbredir_handle_iso_data(dev, p, ep);
|
|
break;
|
|
case USB_ENDPOINT_XFER_INT:
|
|
if (ep & USB_DIR_IN) {
|
|
usbredir_handle_interrupt_in_data(dev, p, ep);
|
|
} else {
|
|
usbredir_handle_interrupt_out_data(dev, p, ep);
|
|
}
|
|
break;
|
|
default:
|
|
ERROR("handle_data ep %02X has unknown type %d\n", ep,
|
|
dev->endpoint[EP2I(ep)].type);
|
|
p->status = USB_RET_NAK;
|
|
}
|
|
}
|
|
|
|
static void usbredir_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
|
|
{
|
|
if (ep->pid == USB_TOKEN_IN && ep->pipeline) {
|
|
usb_ep_combine_input_packets(ep);
|
|
}
|
|
}
|
|
|
|
static void usbredir_stop_ep(USBRedirDevice *dev, int i)
|
|
{
|
|
uint8_t ep = I2EP(i);
|
|
|
|
switch (dev->endpoint[i].type) {
|
|
case USB_ENDPOINT_XFER_BULK:
|
|
if (ep & USB_DIR_IN) {
|
|
usbredir_stop_bulk_receiving(dev, ep);
|
|
}
|
|
break;
|
|
case USB_ENDPOINT_XFER_ISOC:
|
|
usbredir_stop_iso_stream(dev, ep);
|
|
break;
|
|
case USB_ENDPOINT_XFER_INT:
|
|
if (ep & USB_DIR_IN) {
|
|
usbredir_stop_interrupt_receiving(dev, ep);
|
|
}
|
|
break;
|
|
}
|
|
usbredir_free_bufpq(dev, ep);
|
|
}
|
|
|
|
static void usbredir_ep_stopped(USBDevice *udev, USBEndpoint *uep)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
|
|
usbredir_stop_ep(dev, USBEP2I(uep));
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
|
|
static void usbredir_set_config(USBRedirDevice *dev, USBPacket *p,
|
|
int config)
|
|
{
|
|
struct usb_redir_set_configuration_header set_config;
|
|
int i;
|
|
|
|
DPRINTF("set config %d id %"PRIu64"\n", config, p->id);
|
|
|
|
for (i = 0; i < MAX_ENDPOINTS; i++) {
|
|
usbredir_stop_ep(dev, i);
|
|
}
|
|
|
|
set_config.configuration = config;
|
|
usbredirparser_send_set_configuration(dev->parser, p->id, &set_config);
|
|
usbredirparser_do_write(dev->parser);
|
|
p->status = USB_RET_ASYNC;
|
|
}
|
|
|
|
static void usbredir_get_config(USBRedirDevice *dev, USBPacket *p)
|
|
{
|
|
DPRINTF("get config id %"PRIu64"\n", p->id);
|
|
|
|
usbredirparser_send_get_configuration(dev->parser, p->id);
|
|
usbredirparser_do_write(dev->parser);
|
|
p->status = USB_RET_ASYNC;
|
|
}
|
|
|
|
static void usbredir_set_interface(USBRedirDevice *dev, USBPacket *p,
|
|
int interface, int alt)
|
|
{
|
|
struct usb_redir_set_alt_setting_header set_alt;
|
|
int i;
|
|
|
|
DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id);
|
|
|
|
for (i = 0; i < MAX_ENDPOINTS; i++) {
|
|
if (dev->endpoint[i].interface == interface) {
|
|
usbredir_stop_ep(dev, i);
|
|
}
|
|
}
|
|
|
|
set_alt.interface = interface;
|
|
set_alt.alt = alt;
|
|
usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt);
|
|
usbredirparser_do_write(dev->parser);
|
|
p->status = USB_RET_ASYNC;
|
|
}
|
|
|
|
static void usbredir_get_interface(USBRedirDevice *dev, USBPacket *p,
|
|
int interface)
|
|
{
|
|
struct usb_redir_get_alt_setting_header get_alt;
|
|
|
|
DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id);
|
|
|
|
get_alt.interface = interface;
|
|
usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt);
|
|
usbredirparser_do_write(dev->parser);
|
|
p->status = USB_RET_ASYNC;
|
|
}
|
|
|
|
static void usbredir_handle_control(USBDevice *udev, USBPacket *p,
|
|
int request, int value, int index, int length, uint8_t *data)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
struct usb_redir_control_packet_header control_packet;
|
|
|
|
if (usbredir_already_in_flight(dev, p->id)) {
|
|
p->status = USB_RET_ASYNC;
|
|
return;
|
|
}
|
|
|
|
/* Special cases for certain standard device requests */
|
|
switch (request) {
|
|
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
|
DPRINTF("set address %d\n", value);
|
|
dev->dev.addr = value;
|
|
return;
|
|
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
|
usbredir_set_config(dev, p, value & 0xff);
|
|
return;
|
|
case DeviceRequest | USB_REQ_GET_CONFIGURATION:
|
|
usbredir_get_config(dev, p);
|
|
return;
|
|
case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
|
|
usbredir_set_interface(dev, p, index, value);
|
|
return;
|
|
case InterfaceRequest | USB_REQ_GET_INTERFACE:
|
|
usbredir_get_interface(dev, p, index);
|
|
return;
|
|
}
|
|
|
|
/* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
|
|
DPRINTF(
|
|
"ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n",
|
|
request >> 8, request & 0xff, value, index, length, p->id);
|
|
|
|
control_packet.request = request & 0xFF;
|
|
control_packet.requesttype = request >> 8;
|
|
control_packet.endpoint = control_packet.requesttype & USB_DIR_IN;
|
|
control_packet.value = value;
|
|
control_packet.index = index;
|
|
control_packet.length = length;
|
|
|
|
if (control_packet.requesttype & USB_DIR_IN) {
|
|
usbredirparser_send_control_packet(dev->parser, p->id,
|
|
&control_packet, NULL, 0);
|
|
} else {
|
|
usbredir_log_data(dev, "ctrl data out:", data, length);
|
|
usbredirparser_send_control_packet(dev->parser, p->id,
|
|
&control_packet, data, length);
|
|
}
|
|
usbredirparser_do_write(dev->parser);
|
|
p->status = USB_RET_ASYNC;
|
|
}
|
|
|
|
static int usbredir_alloc_streams(USBDevice *udev, USBEndpoint **eps,
|
|
int nr_eps, int streams)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
#if USBREDIR_VERSION >= 0x000700
|
|
struct usb_redir_alloc_bulk_streams_header alloc_streams;
|
|
int i;
|
|
|
|
if (!usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_bulk_streams)) {
|
|
ERROR("peer does not support streams\n");
|
|
goto reject;
|
|
}
|
|
|
|
if (streams == 0) {
|
|
ERROR("request to allocate 0 streams\n");
|
|
return -1;
|
|
}
|
|
|
|
alloc_streams.no_streams = streams;
|
|
alloc_streams.endpoints = 0;
|
|
for (i = 0; i < nr_eps; i++) {
|
|
alloc_streams.endpoints |= 1 << USBEP2I(eps[i]);
|
|
}
|
|
usbredirparser_send_alloc_bulk_streams(dev->parser, 0, &alloc_streams);
|
|
usbredirparser_do_write(dev->parser);
|
|
|
|
return 0;
|
|
#else
|
|
ERROR("usbredir_alloc_streams not implemented\n");
|
|
goto reject;
|
|
#endif
|
|
reject:
|
|
ERROR("streams are not available, disconnecting\n");
|
|
qemu_bh_schedule(dev->device_reject_bh);
|
|
return -1;
|
|
}
|
|
|
|
static void usbredir_free_streams(USBDevice *udev, USBEndpoint **eps,
|
|
int nr_eps)
|
|
{
|
|
#if USBREDIR_VERSION >= 0x000700
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
struct usb_redir_free_bulk_streams_header free_streams;
|
|
int i;
|
|
|
|
if (!usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_bulk_streams)) {
|
|
return;
|
|
}
|
|
|
|
free_streams.endpoints = 0;
|
|
for (i = 0; i < nr_eps; i++) {
|
|
free_streams.endpoints |= 1 << USBEP2I(eps[i]);
|
|
}
|
|
usbredirparser_send_free_bulk_streams(dev->parser, 0, &free_streams);
|
|
usbredirparser_do_write(dev->parser);
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
* Close events can be triggered by usbredirparser_do_write which gets called
|
|
* from within the USBDevice data / control packet callbacks and doing a
|
|
* usb_detach from within these callbacks is not a good idea.
|
|
*
|
|
* So we use a bh handler to take care of close events.
|
|
*/
|
|
static void usbredir_chardev_close_bh(void *opaque)
|
|
{
|
|
USBRedirDevice *dev = opaque;
|
|
|
|
qemu_bh_cancel(dev->device_reject_bh);
|
|
usbredir_device_disconnect(dev);
|
|
|
|
if (dev->parser) {
|
|
DPRINTF("destroying usbredirparser\n");
|
|
usbredirparser_destroy(dev->parser);
|
|
dev->parser = NULL;
|
|
}
|
|
if (dev->watch) {
|
|
g_source_remove(dev->watch);
|
|
dev->watch = 0;
|
|
}
|
|
}
|
|
|
|
static void usbredir_create_parser(USBRedirDevice *dev)
|
|
{
|
|
uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, };
|
|
int flags = 0;
|
|
|
|
DPRINTF("creating usbredirparser\n");
|
|
|
|
dev->parser = usbredirparser_create();
|
|
if (!dev->parser) {
|
|
error_report("usbredirparser_create() failed");
|
|
exit(1);
|
|
}
|
|
dev->parser->priv = dev;
|
|
dev->parser->log_func = usbredir_log;
|
|
dev->parser->read_func = usbredir_read;
|
|
dev->parser->write_func = usbredir_write;
|
|
dev->parser->hello_func = usbredir_hello;
|
|
dev->parser->device_connect_func = usbredir_device_connect;
|
|
dev->parser->device_disconnect_func = usbredir_device_disconnect;
|
|
dev->parser->interface_info_func = usbredir_interface_info;
|
|
dev->parser->ep_info_func = usbredir_ep_info;
|
|
dev->parser->configuration_status_func = usbredir_configuration_status;
|
|
dev->parser->alt_setting_status_func = usbredir_alt_setting_status;
|
|
dev->parser->iso_stream_status_func = usbredir_iso_stream_status;
|
|
dev->parser->interrupt_receiving_status_func =
|
|
usbredir_interrupt_receiving_status;
|
|
dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status;
|
|
dev->parser->bulk_receiving_status_func = usbredir_bulk_receiving_status;
|
|
dev->parser->control_packet_func = usbredir_control_packet;
|
|
dev->parser->bulk_packet_func = usbredir_bulk_packet;
|
|
dev->parser->iso_packet_func = usbredir_iso_packet;
|
|
dev->parser->interrupt_packet_func = usbredir_interrupt_packet;
|
|
dev->parser->buffered_bulk_packet_func = usbredir_buffered_bulk_packet;
|
|
dev->read_buf = NULL;
|
|
dev->read_buf_size = 0;
|
|
|
|
usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version);
|
|
usbredirparser_caps_set_cap(caps, usb_redir_cap_filter);
|
|
usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size);
|
|
usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids);
|
|
usbredirparser_caps_set_cap(caps, usb_redir_cap_32bits_bulk_length);
|
|
usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_receiving);
|
|
#if USBREDIR_VERSION >= 0x000700
|
|
if (dev->enable_streams) {
|
|
usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_streams);
|
|
}
|
|
#endif
|
|
|
|
if (runstate_check(RUN_STATE_INMIGRATE)) {
|
|
flags |= usbredirparser_fl_no_hello;
|
|
}
|
|
usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE,
|
|
flags);
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
|
|
static void usbredir_reject_device(USBRedirDevice *dev)
|
|
{
|
|
usbredir_device_disconnect(dev);
|
|
if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) {
|
|
usbredirparser_send_filter_reject(dev->parser);
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* We may need to reject the device when the hcd calls alloc_streams, doing
|
|
* an usb_detach from within a hcd call is not a good idea, hence this bh.
|
|
*/
|
|
static void usbredir_device_reject_bh(void *opaque)
|
|
{
|
|
USBRedirDevice *dev = opaque;
|
|
|
|
usbredir_reject_device(dev);
|
|
}
|
|
|
|
static void usbredir_do_attach(void *opaque)
|
|
{
|
|
USBRedirDevice *dev = opaque;
|
|
Error *local_err = NULL;
|
|
|
|
/* In order to work properly with XHCI controllers we need these caps */
|
|
if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !(
|
|
usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_ep_info_max_packet_size) &&
|
|
usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_32bits_bulk_length) &&
|
|
usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_64bits_ids))) {
|
|
ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
|
|
usbredir_reject_device(dev);
|
|
return;
|
|
}
|
|
|
|
usb_device_attach(&dev->dev, &local_err);
|
|
if (local_err) {
|
|
error_report_err(local_err);
|
|
WARNING("rejecting device due to speed mismatch\n");
|
|
usbredir_reject_device(dev);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* chardev callbacks
|
|
*/
|
|
|
|
static int usbredir_chardev_can_read(void *opaque)
|
|
{
|
|
USBRedirDevice *dev = opaque;
|
|
|
|
if (!dev->parser) {
|
|
WARNING("chardev_can_read called on non open chardev!\n");
|
|
return 0;
|
|
}
|
|
|
|
/* Don't read new data from the chardev until our state is fully synced */
|
|
if (!runstate_check(RUN_STATE_RUNNING)) {
|
|
return 0;
|
|
}
|
|
|
|
/* usbredir_parser_do_read will consume *all* data we give it */
|
|
return 1 * MiB;
|
|
}
|
|
|
|
static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size)
|
|
{
|
|
USBRedirDevice *dev = opaque;
|
|
|
|
/* No recursion allowed! */
|
|
assert(dev->read_buf == NULL);
|
|
|
|
dev->read_buf = buf;
|
|
dev->read_buf_size = size;
|
|
|
|
usbredirparser_do_read(dev->parser);
|
|
/* Send any acks, etc. which may be queued now */
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
|
|
static void usbredir_chardev_event(void *opaque, QEMUChrEvent event)
|
|
{
|
|
USBRedirDevice *dev = opaque;
|
|
|
|
switch (event) {
|
|
case CHR_EVENT_OPENED:
|
|
DPRINTF("chardev open\n");
|
|
/* Make sure any pending closes are handled (no-op if none pending) */
|
|
usbredir_chardev_close_bh(dev);
|
|
qemu_bh_cancel(dev->chardev_close_bh);
|
|
usbredir_create_parser(dev);
|
|
break;
|
|
case CHR_EVENT_CLOSED:
|
|
DPRINTF("chardev close\n");
|
|
qemu_bh_schedule(dev->chardev_close_bh);
|
|
break;
|
|
case CHR_EVENT_BREAK:
|
|
case CHR_EVENT_MUX_IN:
|
|
case CHR_EVENT_MUX_OUT:
|
|
/* Ignore */
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* init + destroy
|
|
*/
|
|
|
|
static void usbredir_vm_state_change(void *priv, bool running, RunState state)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
if (state == RUN_STATE_RUNNING && dev->parser != NULL) {
|
|
usbredirparser_do_write(dev->parser); /* Flush any pending writes */
|
|
}
|
|
}
|
|
|
|
static void usbredir_init_endpoints(USBRedirDevice *dev)
|
|
{
|
|
int i;
|
|
|
|
usb_ep_init(&dev->dev);
|
|
memset(dev->endpoint, 0, sizeof(dev->endpoint));
|
|
for (i = 0; i < MAX_ENDPOINTS; i++) {
|
|
dev->endpoint[i].dev = dev;
|
|
QTAILQ_INIT(&dev->endpoint[i].bufpq);
|
|
}
|
|
}
|
|
|
|
static void usbredir_realize(USBDevice *udev, Error **errp)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
int i;
|
|
|
|
if (!qemu_chr_fe_backend_connected(&dev->cs)) {
|
|
error_setg(errp, QERR_MISSING_PARAMETER, "chardev");
|
|
return;
|
|
}
|
|
|
|
if (dev->filter_str) {
|
|
i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|",
|
|
&dev->filter_rules,
|
|
&dev->filter_rules_count);
|
|
if (i) {
|
|
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "filter",
|
|
"a usb device filter string");
|
|
return;
|
|
}
|
|
}
|
|
|
|
dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev);
|
|
dev->device_reject_bh = qemu_bh_new(usbredir_device_reject_bh, dev);
|
|
dev->attach_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, usbredir_do_attach, dev);
|
|
|
|
packet_id_queue_init(&dev->cancelled, dev, "cancelled");
|
|
packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight");
|
|
usbredir_init_endpoints(dev);
|
|
|
|
/* We'll do the attach once we receive the speed from the usb-host */
|
|
udev->auto_attach = 0;
|
|
|
|
/* Will be cleared during setup when we find conflicts */
|
|
dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
|
|
|
|
/* Let the backend know we are ready */
|
|
qemu_chr_fe_set_handlers(&dev->cs, usbredir_chardev_can_read,
|
|
usbredir_chardev_read, usbredir_chardev_event,
|
|
NULL, dev, NULL, true);
|
|
|
|
dev->vmstate =
|
|
qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev);
|
|
}
|
|
|
|
static void usbredir_cleanup_device_queues(USBRedirDevice *dev)
|
|
{
|
|
int i;
|
|
|
|
packet_id_queue_empty(&dev->cancelled);
|
|
packet_id_queue_empty(&dev->already_in_flight);
|
|
for (i = 0; i < MAX_ENDPOINTS; i++) {
|
|
usbredir_free_bufpq(dev, I2EP(i));
|
|
}
|
|
}
|
|
|
|
static void usbredir_unrealize(USBDevice *udev)
|
|
{
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
|
|
qemu_chr_fe_deinit(&dev->cs, true);
|
|
|
|
/* Note must be done after qemu_chr_close, as that causes a close event */
|
|
qemu_bh_delete(dev->chardev_close_bh);
|
|
qemu_bh_delete(dev->device_reject_bh);
|
|
|
|
timer_free(dev->attach_timer);
|
|
|
|
usbredir_cleanup_device_queues(dev);
|
|
|
|
if (dev->parser) {
|
|
usbredirparser_destroy(dev->parser);
|
|
}
|
|
if (dev->watch) {
|
|
g_source_remove(dev->watch);
|
|
}
|
|
|
|
free(dev->filter_rules);
|
|
qemu_del_vm_change_state_handler(dev->vmstate);
|
|
}
|
|
|
|
static int usbredir_check_filter(USBRedirDevice *dev)
|
|
{
|
|
if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
|
|
ERROR("No interface info for device\n");
|
|
goto error;
|
|
}
|
|
|
|
if (dev->filter_rules) {
|
|
if (!usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_connect_device_version)) {
|
|
ERROR("Device filter specified and peer does not have the "
|
|
"connect_device_version capability\n");
|
|
goto error;
|
|
}
|
|
|
|
if (usbredirfilter_check(
|
|
dev->filter_rules,
|
|
dev->filter_rules_count,
|
|
dev->device_info.device_class,
|
|
dev->device_info.device_subclass,
|
|
dev->device_info.device_protocol,
|
|
dev->interface_info.interface_class,
|
|
dev->interface_info.interface_subclass,
|
|
dev->interface_info.interface_protocol,
|
|
dev->interface_info.interface_count,
|
|
dev->device_info.vendor_id,
|
|
dev->device_info.product_id,
|
|
dev->device_info.device_version_bcd,
|
|
0) != 0) {
|
|
goto error;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
|
|
error:
|
|
usbredir_reject_device(dev);
|
|
return -1;
|
|
}
|
|
|
|
static void usbredir_check_bulk_receiving(USBRedirDevice *dev)
|
|
{
|
|
int i, j, quirks;
|
|
|
|
if (!usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_bulk_receiving)) {
|
|
return;
|
|
}
|
|
|
|
for (i = EP2I(USB_DIR_IN); i < MAX_ENDPOINTS; i++) {
|
|
dev->endpoint[i].bulk_receiving_enabled = 0;
|
|
}
|
|
|
|
if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < dev->interface_info.interface_count; i++) {
|
|
quirks = usb_get_quirks(dev->device_info.vendor_id,
|
|
dev->device_info.product_id,
|
|
dev->interface_info.interface_class[i],
|
|
dev->interface_info.interface_subclass[i],
|
|
dev->interface_info.interface_protocol[i]);
|
|
if (!(quirks & USB_QUIRK_BUFFER_BULK_IN)) {
|
|
continue;
|
|
}
|
|
if (quirks & USB_QUIRK_IS_FTDI) {
|
|
dev->buffered_bulk_in_complete =
|
|
usbredir_buffered_bulk_in_complete_ftdi;
|
|
} else {
|
|
dev->buffered_bulk_in_complete =
|
|
usbredir_buffered_bulk_in_complete_raw;
|
|
}
|
|
|
|
for (j = EP2I(USB_DIR_IN); j < MAX_ENDPOINTS; j++) {
|
|
if (dev->endpoint[j].interface ==
|
|
dev->interface_info.interface[i] &&
|
|
dev->endpoint[j].type == USB_ENDPOINT_XFER_BULK &&
|
|
dev->endpoint[j].max_packet_size != 0) {
|
|
dev->endpoint[j].bulk_receiving_enabled = 1;
|
|
/*
|
|
* With buffering pipelining is not necessary. Also packet
|
|
* combining and bulk in buffering don't play nice together!
|
|
*/
|
|
I2USBEP(dev, j)->pipeline = false;
|
|
break; /* Only buffer for the first ep of each intf */
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* usbredirparser packet complete callbacks
|
|
*/
|
|
|
|
static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p,
|
|
int status)
|
|
{
|
|
switch (status) {
|
|
case usb_redir_success:
|
|
p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
|
|
break;
|
|
case usb_redir_stall:
|
|
p->status = USB_RET_STALL;
|
|
break;
|
|
case usb_redir_cancelled:
|
|
/*
|
|
* When the usbredir-host unredirects a device, it will report a status
|
|
* of cancelled for all pending packets, followed by a disconnect msg.
|
|
*/
|
|
p->status = USB_RET_IOERROR;
|
|
break;
|
|
case usb_redir_inval:
|
|
WARNING("got invalid param error from usb-host?\n");
|
|
p->status = USB_RET_IOERROR;
|
|
break;
|
|
case usb_redir_babble:
|
|
p->status = USB_RET_BABBLE;
|
|
break;
|
|
case usb_redir_ioerror:
|
|
case usb_redir_timeout:
|
|
default:
|
|
p->status = USB_RET_IOERROR;
|
|
}
|
|
}
|
|
|
|
static void usbredir_hello(void *priv, struct usb_redir_hello_header *h)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
/* Try to send the filter info now that we've the usb-host's caps */
|
|
if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) &&
|
|
dev->filter_rules) {
|
|
usbredirparser_send_filter_filter(dev->parser, dev->filter_rules,
|
|
dev->filter_rules_count);
|
|
usbredirparser_do_write(dev->parser);
|
|
}
|
|
}
|
|
|
|
static void usbredir_device_connect(void *priv,
|
|
struct usb_redir_device_connect_header *device_connect)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
const char *speed;
|
|
|
|
if (timer_pending(dev->attach_timer) || dev->dev.attached) {
|
|
ERROR("Received device connect while already connected\n");
|
|
return;
|
|
}
|
|
|
|
switch (device_connect->speed) {
|
|
case usb_redir_speed_low:
|
|
speed = "low speed";
|
|
dev->dev.speed = USB_SPEED_LOW;
|
|
dev->compatible_speedmask &= ~USB_SPEED_MASK_FULL;
|
|
dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
|
|
break;
|
|
case usb_redir_speed_full:
|
|
speed = "full speed";
|
|
dev->dev.speed = USB_SPEED_FULL;
|
|
dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
|
|
break;
|
|
case usb_redir_speed_high:
|
|
speed = "high speed";
|
|
dev->dev.speed = USB_SPEED_HIGH;
|
|
break;
|
|
case usb_redir_speed_super:
|
|
speed = "super speed";
|
|
dev->dev.speed = USB_SPEED_SUPER;
|
|
break;
|
|
default:
|
|
speed = "unknown speed";
|
|
dev->dev.speed = USB_SPEED_FULL;
|
|
}
|
|
|
|
if (usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_connect_device_version)) {
|
|
INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
|
|
speed, device_connect->vendor_id, device_connect->product_id,
|
|
((device_connect->device_version_bcd & 0xf000) >> 12) * 10 +
|
|
((device_connect->device_version_bcd & 0x0f00) >> 8),
|
|
((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 +
|
|
((device_connect->device_version_bcd & 0x000f) >> 0),
|
|
device_connect->device_class);
|
|
} else {
|
|
INFO("attaching %s device %04x:%04x class %02x\n", speed,
|
|
device_connect->vendor_id, device_connect->product_id,
|
|
device_connect->device_class);
|
|
}
|
|
|
|
dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
|
|
dev->device_info = *device_connect;
|
|
|
|
if (usbredir_check_filter(dev)) {
|
|
WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
|
|
device_connect->vendor_id, device_connect->product_id);
|
|
return;
|
|
}
|
|
|
|
usbredir_check_bulk_receiving(dev);
|
|
timer_mod(dev->attach_timer, dev->next_attach_time);
|
|
}
|
|
|
|
static void usbredir_device_disconnect(void *priv)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
/* Stop any pending attaches */
|
|
timer_del(dev->attach_timer);
|
|
|
|
if (dev->dev.attached) {
|
|
DPRINTF("detaching device\n");
|
|
usb_device_detach(&dev->dev);
|
|
/*
|
|
* Delay next usb device attach to give the guest a chance to see
|
|
* see the detach / attach in case of quick close / open succession
|
|
*/
|
|
dev->next_attach_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 200;
|
|
}
|
|
|
|
/* Reset state so that the next dev connected starts with a clean slate */
|
|
usbredir_cleanup_device_queues(dev);
|
|
usbredir_init_endpoints(dev);
|
|
dev->interface_info.interface_count = NO_INTERFACE_INFO;
|
|
dev->dev.addr = 0;
|
|
dev->dev.speed = 0;
|
|
dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
|
|
}
|
|
|
|
static void usbredir_interface_info(void *priv,
|
|
struct usb_redir_interface_info_header *interface_info)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
dev->interface_info = *interface_info;
|
|
|
|
/*
|
|
* If we receive interface info after the device has already been
|
|
* connected (ie on a set_config), re-check interface dependent things.
|
|
*/
|
|
if (timer_pending(dev->attach_timer) || dev->dev.attached) {
|
|
usbredir_check_bulk_receiving(dev);
|
|
if (usbredir_check_filter(dev)) {
|
|
ERROR("Device no longer matches filter after interface info "
|
|
"change, disconnecting!\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
static void usbredir_mark_speed_incompatible(USBRedirDevice *dev, int speed)
|
|
{
|
|
dev->compatible_speedmask &= ~(1 << speed);
|
|
dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
|
|
}
|
|
|
|
static void usbredir_set_pipeline(USBRedirDevice *dev, struct USBEndpoint *uep)
|
|
{
|
|
if (uep->type != USB_ENDPOINT_XFER_BULK) {
|
|
return;
|
|
}
|
|
if (uep->pid == USB_TOKEN_OUT) {
|
|
uep->pipeline = true;
|
|
}
|
|
if (uep->pid == USB_TOKEN_IN && uep->max_packet_size != 0 &&
|
|
usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_32bits_bulk_length)) {
|
|
uep->pipeline = true;
|
|
}
|
|
}
|
|
|
|
static void usbredir_setup_usb_eps(USBRedirDevice *dev)
|
|
{
|
|
struct USBEndpoint *usb_ep;
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_ENDPOINTS; i++) {
|
|
usb_ep = I2USBEP(dev, i);
|
|
usb_ep->type = dev->endpoint[i].type;
|
|
usb_ep->ifnum = dev->endpoint[i].interface;
|
|
usb_ep->max_packet_size = dev->endpoint[i].max_packet_size;
|
|
usb_ep->max_streams = dev->endpoint[i].max_streams;
|
|
usbredir_set_pipeline(dev, usb_ep);
|
|
}
|
|
}
|
|
|
|
static void usbredir_ep_info(void *priv,
|
|
struct usb_redir_ep_info_header *ep_info)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
int i;
|
|
|
|
assert(dev != NULL);
|
|
for (i = 0; i < MAX_ENDPOINTS; i++) {
|
|
dev->endpoint[i].type = ep_info->type[i];
|
|
dev->endpoint[i].interval = ep_info->interval[i];
|
|
dev->endpoint[i].interface = ep_info->interface[i];
|
|
if (usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_ep_info_max_packet_size)) {
|
|
dev->endpoint[i].max_packet_size = ep_info->max_packet_size[i];
|
|
}
|
|
#if USBREDIR_VERSION >= 0x000700
|
|
if (usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_bulk_streams)) {
|
|
dev->endpoint[i].max_streams = ep_info->max_streams[i];
|
|
}
|
|
#endif
|
|
switch (dev->endpoint[i].type) {
|
|
case usb_redir_type_invalid:
|
|
break;
|
|
case usb_redir_type_iso:
|
|
usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
|
|
usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
|
|
/* Fall through */
|
|
case usb_redir_type_interrupt:
|
|
if (!usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_ep_info_max_packet_size) ||
|
|
ep_info->max_packet_size[i] > 64) {
|
|
usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
|
|
}
|
|
if (!usbredirparser_peer_has_cap(dev->parser,
|
|
usb_redir_cap_ep_info_max_packet_size) ||
|
|
ep_info->max_packet_size[i] > 1024) {
|
|
usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
|
|
}
|
|
if (dev->endpoint[i].interval == 0) {
|
|
ERROR("Received 0 interval for isoc or irq endpoint\n");
|
|
usbredir_reject_device(dev);
|
|
return;
|
|
}
|
|
/* Fall through */
|
|
case usb_redir_type_control:
|
|
case usb_redir_type_bulk:
|
|
DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i),
|
|
dev->endpoint[i].type, dev->endpoint[i].interface);
|
|
break;
|
|
default:
|
|
ERROR("Received invalid endpoint type\n");
|
|
usbredir_reject_device(dev);
|
|
return;
|
|
}
|
|
}
|
|
/* The new ep info may have caused a speed incompatibility, recheck */
|
|
if (dev->dev.attached &&
|
|
!(dev->dev.port->speedmask & dev->dev.speedmask)) {
|
|
ERROR("Device no longer matches speed after endpoint info change, "
|
|
"disconnecting!\n");
|
|
usbredir_reject_device(dev);
|
|
return;
|
|
}
|
|
usbredir_setup_usb_eps(dev);
|
|
usbredir_check_bulk_receiving(dev);
|
|
}
|
|
|
|
static void usbredir_configuration_status(void *priv, uint64_t id,
|
|
struct usb_redir_configuration_status_header *config_status)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
USBPacket *p;
|
|
|
|
DPRINTF("set config status %d config %d id %"PRIu64"\n",
|
|
config_status->status, config_status->configuration, id);
|
|
|
|
p = usbredir_find_packet_by_id(dev, 0, id);
|
|
if (p) {
|
|
if (dev->dev.setup_buf[0] & USB_DIR_IN) {
|
|
dev->dev.data_buf[0] = config_status->configuration;
|
|
p->actual_length = 1;
|
|
}
|
|
usbredir_handle_status(dev, p, config_status->status);
|
|
usb_generic_async_ctrl_complete(&dev->dev, p);
|
|
}
|
|
}
|
|
|
|
static void usbredir_alt_setting_status(void *priv, uint64_t id,
|
|
struct usb_redir_alt_setting_status_header *alt_setting_status)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
USBPacket *p;
|
|
|
|
DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n",
|
|
alt_setting_status->status, alt_setting_status->interface,
|
|
alt_setting_status->alt, id);
|
|
|
|
p = usbredir_find_packet_by_id(dev, 0, id);
|
|
if (p) {
|
|
if (dev->dev.setup_buf[0] & USB_DIR_IN) {
|
|
dev->dev.data_buf[0] = alt_setting_status->alt;
|
|
p->actual_length = 1;
|
|
}
|
|
usbredir_handle_status(dev, p, alt_setting_status->status);
|
|
usb_generic_async_ctrl_complete(&dev->dev, p);
|
|
}
|
|
}
|
|
|
|
static void usbredir_iso_stream_status(void *priv, uint64_t id,
|
|
struct usb_redir_iso_stream_status_header *iso_stream_status)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t ep = iso_stream_status->endpoint;
|
|
|
|
DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status,
|
|
ep, id);
|
|
|
|
if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) {
|
|
return;
|
|
}
|
|
|
|
dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status;
|
|
if (iso_stream_status->status == usb_redir_stall) {
|
|
DPRINTF("iso stream stopped by peer ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].iso_started = 0;
|
|
}
|
|
}
|
|
|
|
static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
|
|
struct usb_redir_interrupt_receiving_status_header
|
|
*interrupt_receiving_status)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t ep = interrupt_receiving_status->endpoint;
|
|
|
|
DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n",
|
|
interrupt_receiving_status->status, ep, id);
|
|
|
|
if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) {
|
|
return;
|
|
}
|
|
|
|
dev->endpoint[EP2I(ep)].interrupt_error =
|
|
interrupt_receiving_status->status;
|
|
if (interrupt_receiving_status->status == usb_redir_stall) {
|
|
DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].interrupt_started = 0;
|
|
}
|
|
}
|
|
|
|
static void usbredir_bulk_streams_status(void *priv, uint64_t id,
|
|
struct usb_redir_bulk_streams_status_header *bulk_streams_status)
|
|
{
|
|
#if USBREDIR_VERSION >= 0x000700
|
|
USBRedirDevice *dev = priv;
|
|
|
|
if (bulk_streams_status->status == usb_redir_success) {
|
|
DPRINTF("bulk streams status %d eps %08x\n",
|
|
bulk_streams_status->status, bulk_streams_status->endpoints);
|
|
} else {
|
|
ERROR("bulk streams %s failed status %d eps %08x\n",
|
|
(bulk_streams_status->no_streams == 0) ? "free" : "alloc",
|
|
bulk_streams_status->status, bulk_streams_status->endpoints);
|
|
ERROR("usb-redir-host does not provide streams, disconnecting\n");
|
|
usbredir_reject_device(dev);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static void usbredir_bulk_receiving_status(void *priv, uint64_t id,
|
|
struct usb_redir_bulk_receiving_status_header *bulk_receiving_status)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t ep = bulk_receiving_status->endpoint;
|
|
|
|
DPRINTF("bulk recv status %d ep %02X id %"PRIu64"\n",
|
|
bulk_receiving_status->status, ep, id);
|
|
|
|
if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].bulk_receiving_started) {
|
|
return;
|
|
}
|
|
|
|
if (bulk_receiving_status->status == usb_redir_stall) {
|
|
DPRINTF("bulk receiving stopped by peer ep %02X\n", ep);
|
|
dev->endpoint[EP2I(ep)].bulk_receiving_started = 0;
|
|
}
|
|
}
|
|
|
|
static void usbredir_control_packet(void *priv, uint64_t id,
|
|
struct usb_redir_control_packet_header *control_packet,
|
|
uint8_t *data, int data_len)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
USBPacket *p;
|
|
int len = control_packet->length;
|
|
|
|
DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status,
|
|
len, id);
|
|
|
|
/* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
|
|
* to work redirected to a not superspeed capable hcd */
|
|
if (dev->dev.speed == USB_SPEED_SUPER &&
|
|
!((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER)) &&
|
|
control_packet->requesttype == 0x80 &&
|
|
control_packet->request == 6 &&
|
|
control_packet->value == 0x100 && control_packet->index == 0 &&
|
|
data_len >= 18 && data[7] == 9) {
|
|
data[7] = 64;
|
|
}
|
|
|
|
p = usbredir_find_packet_by_id(dev, 0, id);
|
|
if (p) {
|
|
usbredir_handle_status(dev, p, control_packet->status);
|
|
if (data_len > 0) {
|
|
usbredir_log_data(dev, "ctrl data in:", data, data_len);
|
|
if (data_len > sizeof(dev->dev.data_buf)) {
|
|
ERROR("ctrl buffer too small (%d > %zu)\n",
|
|
data_len, sizeof(dev->dev.data_buf));
|
|
p->status = USB_RET_STALL;
|
|
data_len = len = sizeof(dev->dev.data_buf);
|
|
}
|
|
memcpy(dev->dev.data_buf, data, data_len);
|
|
}
|
|
p->actual_length = len;
|
|
/*
|
|
* If this is GET_DESCRIPTOR request for configuration descriptor,
|
|
* remove 'remote wakeup' flag from it to prevent idle power down
|
|
* in Windows guest
|
|
*/
|
|
if (dev->suppress_remote_wake &&
|
|
control_packet->requesttype == USB_DIR_IN &&
|
|
control_packet->request == USB_REQ_GET_DESCRIPTOR &&
|
|
control_packet->value == (USB_DT_CONFIG << 8) &&
|
|
control_packet->index == 0 &&
|
|
/* bmAttributes field of config descriptor */
|
|
len > 7 && (dev->dev.data_buf[7] & USB_CFG_ATT_WAKEUP)) {
|
|
DPRINTF("Removed remote wake %04X:%04X\n",
|
|
dev->device_info.vendor_id,
|
|
dev->device_info.product_id);
|
|
dev->dev.data_buf[7] &= ~USB_CFG_ATT_WAKEUP;
|
|
}
|
|
usb_generic_async_ctrl_complete(&dev->dev, p);
|
|
}
|
|
free(data);
|
|
}
|
|
|
|
static void usbredir_bulk_packet(void *priv, uint64_t id,
|
|
struct usb_redir_bulk_packet_header *bulk_packet,
|
|
uint8_t *data, int data_len)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t ep = bulk_packet->endpoint;
|
|
int len = (bulk_packet->length_high << 16) | bulk_packet->length;
|
|
USBPacket *p;
|
|
|
|
DPRINTF("bulk-in status %d ep %02X stream %u len %d id %"PRIu64"\n",
|
|
bulk_packet->status, ep, bulk_packet->stream_id, len, id);
|
|
|
|
p = usbredir_find_packet_by_id(dev, ep, id);
|
|
if (p) {
|
|
size_t size = usb_packet_size(p);
|
|
usbredir_handle_status(dev, p, bulk_packet->status);
|
|
if (data_len > 0) {
|
|
usbredir_log_data(dev, "bulk data in:", data, data_len);
|
|
if (data_len > size) {
|
|
ERROR("bulk got more data then requested (%d > %zd)\n",
|
|
data_len, p->iov.size);
|
|
p->status = USB_RET_BABBLE;
|
|
data_len = len = size;
|
|
}
|
|
usb_packet_copy(p, data, data_len);
|
|
}
|
|
p->actual_length = len;
|
|
if (p->pid == USB_TOKEN_IN && p->ep->pipeline) {
|
|
usb_combined_input_packet_complete(&dev->dev, p);
|
|
} else {
|
|
usb_packet_complete(&dev->dev, p);
|
|
}
|
|
}
|
|
free(data);
|
|
}
|
|
|
|
static void usbredir_iso_packet(void *priv, uint64_t id,
|
|
struct usb_redir_iso_packet_header *iso_packet,
|
|
uint8_t *data, int data_len)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t ep = iso_packet->endpoint;
|
|
|
|
DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n",
|
|
iso_packet->status, ep, data_len, id);
|
|
|
|
if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) {
|
|
ERROR("received iso packet for non iso endpoint %02X\n", ep);
|
|
free(data);
|
|
return;
|
|
}
|
|
|
|
if (dev->endpoint[EP2I(ep)].iso_started == 0) {
|
|
DPRINTF("received iso packet for non started stream ep %02X\n", ep);
|
|
free(data);
|
|
return;
|
|
}
|
|
|
|
/* bufp_alloc also adds the packet to the ep queue */
|
|
bufp_alloc(dev, data, data_len, iso_packet->status, ep, data);
|
|
}
|
|
|
|
static void usbredir_interrupt_packet(void *priv, uint64_t id,
|
|
struct usb_redir_interrupt_packet_header *interrupt_packet,
|
|
uint8_t *data, int data_len)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t ep = interrupt_packet->endpoint;
|
|
|
|
DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n",
|
|
interrupt_packet->status, ep, data_len, id);
|
|
|
|
if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) {
|
|
ERROR("received int packet for non interrupt endpoint %02X\n", ep);
|
|
free(data);
|
|
return;
|
|
}
|
|
|
|
if (ep & USB_DIR_IN) {
|
|
if (dev->endpoint[EP2I(ep)].interrupt_started == 0) {
|
|
DPRINTF("received int packet while not started ep %02X\n", ep);
|
|
free(data);
|
|
return;
|
|
}
|
|
|
|
/* bufp_alloc also adds the packet to the ep queue */
|
|
bufp_alloc(dev, data, data_len, interrupt_packet->status, ep, data);
|
|
|
|
/* insufficient data solved with USB_RET_NAK */
|
|
usb_wakeup(usb_ep_get(&dev->dev, USB_TOKEN_IN, ep & 0x0f), 0);
|
|
} else {
|
|
/*
|
|
* We report output interrupt packets as completed directly upon
|
|
* submission, so all we can do here if one failed is warn.
|
|
*/
|
|
if (interrupt_packet->status) {
|
|
WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n",
|
|
interrupt_packet->status, ep, id);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void usbredir_buffered_bulk_packet(void *priv, uint64_t id,
|
|
struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet,
|
|
uint8_t *data, int data_len)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t status, ep = buffered_bulk_packet->endpoint;
|
|
void *free_on_destroy;
|
|
int i, len;
|
|
|
|
DPRINTF("buffered-bulk-in status %d ep %02X len %d id %"PRIu64"\n",
|
|
buffered_bulk_packet->status, ep, data_len, id);
|
|
|
|
if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_BULK) {
|
|
ERROR("received buffered-bulk packet for non bulk ep %02X\n", ep);
|
|
free(data);
|
|
return;
|
|
}
|
|
|
|
if (dev->endpoint[EP2I(ep)].bulk_receiving_started == 0) {
|
|
DPRINTF("received buffered-bulk packet on not started ep %02X\n", ep);
|
|
free(data);
|
|
return;
|
|
}
|
|
|
|
/* Data must be in maxp chunks for buffered_bulk_add_*_data_to_packet */
|
|
len = dev->endpoint[EP2I(ep)].max_packet_size;
|
|
status = usb_redir_success;
|
|
free_on_destroy = NULL;
|
|
for (i = 0; i < data_len; i += len) {
|
|
int r;
|
|
if (len >= (data_len - i)) {
|
|
len = data_len - i;
|
|
status = buffered_bulk_packet->status;
|
|
free_on_destroy = data;
|
|
}
|
|
/* bufp_alloc also adds the packet to the ep queue */
|
|
r = bufp_alloc(dev, data + i, len, status, ep, free_on_destroy);
|
|
if (r) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (dev->endpoint[EP2I(ep)].pending_async_packet) {
|
|
USBPacket *p = dev->endpoint[EP2I(ep)].pending_async_packet;
|
|
dev->endpoint[EP2I(ep)].pending_async_packet = NULL;
|
|
usbredir_buffered_bulk_in_complete(dev, p, ep);
|
|
usb_packet_complete(&dev->dev, p);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Migration code
|
|
*/
|
|
|
|
static int usbredir_pre_save(void *priv)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
usbredir_fill_already_in_flight(dev);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int usbredir_post_load(void *priv, int version_id)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
|
|
if (dev == NULL || dev->parser == NULL) {
|
|
return 0;
|
|
}
|
|
|
|
switch (dev->device_info.speed) {
|
|
case usb_redir_speed_low:
|
|
dev->dev.speed = USB_SPEED_LOW;
|
|
break;
|
|
case usb_redir_speed_full:
|
|
dev->dev.speed = USB_SPEED_FULL;
|
|
break;
|
|
case usb_redir_speed_high:
|
|
dev->dev.speed = USB_SPEED_HIGH;
|
|
break;
|
|
case usb_redir_speed_super:
|
|
dev->dev.speed = USB_SPEED_SUPER;
|
|
break;
|
|
default:
|
|
dev->dev.speed = USB_SPEED_FULL;
|
|
}
|
|
dev->dev.speedmask = (1 << dev->dev.speed);
|
|
|
|
usbredir_setup_usb_eps(dev);
|
|
usbredir_check_bulk_receiving(dev);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* For usbredirparser migration */
|
|
static int usbredir_put_parser(QEMUFile *f, void *priv, size_t unused,
|
|
const VMStateField *field, JSONWriter *vmdesc)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t *data;
|
|
int len;
|
|
|
|
if (dev->parser == NULL) {
|
|
qemu_put_be32(f, 0);
|
|
return 0;
|
|
}
|
|
|
|
usbredirparser_serialize(dev->parser, &data, &len);
|
|
if (!data) {
|
|
error_report("usbredirparser_serialize failed");
|
|
exit(1);
|
|
}
|
|
|
|
qemu_put_be32(f, len);
|
|
qemu_put_buffer(f, data, len);
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused,
|
|
const VMStateField *field)
|
|
{
|
|
USBRedirDevice *dev = priv;
|
|
uint8_t *data;
|
|
int len, ret;
|
|
|
|
len = qemu_get_be32(f);
|
|
if (len == 0) {
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* If our chardev is not open already at this point the usbredir connection
|
|
* has been broken (non seamless migration, or restore from disk).
|
|
*
|
|
* In this case create a temporary parser to receive the migration data,
|
|
* and schedule the close_bh to report the device as disconnected to the
|
|
* guest and to destroy the parser again.
|
|
*/
|
|
if (dev->parser == NULL) {
|
|
WARNING("usb-redir connection broken during migration\n");
|
|
usbredir_create_parser(dev);
|
|
qemu_bh_schedule(dev->chardev_close_bh);
|
|
}
|
|
|
|
data = g_malloc(len);
|
|
qemu_get_buffer(f, data, len);
|
|
|
|
ret = usbredirparser_unserialize(dev->parser, data, len);
|
|
|
|
g_free(data);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const VMStateInfo usbredir_parser_vmstate_info = {
|
|
.name = "usb-redir-parser",
|
|
.put = usbredir_put_parser,
|
|
.get = usbredir_get_parser,
|
|
};
|
|
|
|
|
|
/* For buffered packets (iso/irq) queue migration */
|
|
static int usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused,
|
|
const VMStateField *field, JSONWriter *vmdesc)
|
|
{
|
|
struct endp_data *endp = priv;
|
|
USBRedirDevice *dev = endp->dev;
|
|
struct buf_packet *bufp;
|
|
int len, i = 0;
|
|
|
|
qemu_put_be32(f, endp->bufpq_size);
|
|
QTAILQ_FOREACH(bufp, &endp->bufpq, next) {
|
|
len = bufp->len - bufp->offset;
|
|
DPRINTF("put_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size,
|
|
len, bufp->status);
|
|
qemu_put_be32(f, len);
|
|
qemu_put_be32(f, bufp->status);
|
|
qemu_put_buffer(f, bufp->data + bufp->offset, len);
|
|
i++;
|
|
}
|
|
assert(i == endp->bufpq_size);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused,
|
|
const VMStateField *field)
|
|
{
|
|
struct endp_data *endp = priv;
|
|
USBRedirDevice *dev = endp->dev;
|
|
struct buf_packet *bufp;
|
|
int i;
|
|
|
|
endp->bufpq_size = qemu_get_be32(f);
|
|
for (i = 0; i < endp->bufpq_size; i++) {
|
|
bufp = g_new(struct buf_packet, 1);
|
|
bufp->len = qemu_get_be32(f);
|
|
bufp->status = qemu_get_be32(f);
|
|
bufp->offset = 0;
|
|
bufp->data = malloc(bufp->len); /* regular malloc! */
|
|
if (!bufp->data) {
|
|
error_report("usbredir_get_bufpq: out of memory");
|
|
exit(1);
|
|
}
|
|
bufp->free_on_destroy = bufp->data;
|
|
qemu_get_buffer(f, bufp->data, bufp->len);
|
|
QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next);
|
|
DPRINTF("get_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size,
|
|
bufp->len, bufp->status);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static const VMStateInfo usbredir_ep_bufpq_vmstate_info = {
|
|
.name = "usb-redir-bufpq",
|
|
.put = usbredir_put_bufpq,
|
|
.get = usbredir_get_bufpq,
|
|
};
|
|
|
|
|
|
/* For endp_data migration */
|
|
static bool usbredir_bulk_receiving_needed(void *priv)
|
|
{
|
|
struct endp_data *endp = priv;
|
|
|
|
return endp->bulk_receiving_started;
|
|
}
|
|
|
|
static const VMStateDescription usbredir_bulk_receiving_vmstate = {
|
|
.name = "usb-redir-ep/bulk-receiving",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.needed = usbredir_bulk_receiving_needed,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_UINT8(bulk_receiving_started, struct endp_data),
|
|
VMSTATE_END_OF_LIST()
|
|
}
|
|
};
|
|
|
|
static bool usbredir_stream_needed(void *priv)
|
|
{
|
|
struct endp_data *endp = priv;
|
|
|
|
return endp->max_streams;
|
|
}
|
|
|
|
static const VMStateDescription usbredir_stream_vmstate = {
|
|
.name = "usb-redir-ep/stream-state",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.needed = usbredir_stream_needed,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_UINT32(max_streams, struct endp_data),
|
|
VMSTATE_END_OF_LIST()
|
|
}
|
|
};
|
|
|
|
static const VMStateDescription usbredir_ep_vmstate = {
|
|
.name = "usb-redir-ep",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_UINT8(type, struct endp_data),
|
|
VMSTATE_UINT8(interval, struct endp_data),
|
|
VMSTATE_UINT8(interface, struct endp_data),
|
|
VMSTATE_UINT16(max_packet_size, struct endp_data),
|
|
VMSTATE_UINT8(iso_started, struct endp_data),
|
|
VMSTATE_UINT8(iso_error, struct endp_data),
|
|
VMSTATE_UINT8(interrupt_started, struct endp_data),
|
|
VMSTATE_UINT8(interrupt_error, struct endp_data),
|
|
VMSTATE_UINT8(bufpq_prefilled, struct endp_data),
|
|
VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data),
|
|
{
|
|
.name = "bufpq",
|
|
.version_id = 0,
|
|
.field_exists = NULL,
|
|
.size = 0,
|
|
.info = &usbredir_ep_bufpq_vmstate_info,
|
|
.flags = VMS_SINGLE,
|
|
.offset = 0,
|
|
},
|
|
VMSTATE_INT32(bufpq_target_size, struct endp_data),
|
|
VMSTATE_END_OF_LIST()
|
|
},
|
|
.subsections = (const VMStateDescription*[]) {
|
|
&usbredir_bulk_receiving_vmstate,
|
|
&usbredir_stream_vmstate,
|
|
NULL
|
|
}
|
|
};
|
|
|
|
|
|
/* For PacketIdQueue migration */
|
|
static int usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused,
|
|
const VMStateField *field,
|
|
JSONWriter *vmdesc)
|
|
{
|
|
struct PacketIdQueue *q = priv;
|
|
USBRedirDevice *dev = q->dev;
|
|
struct PacketIdQueueEntry *e;
|
|
int remain = q->size;
|
|
|
|
DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size);
|
|
qemu_put_be32(f, q->size);
|
|
QTAILQ_FOREACH(e, &q->head, next) {
|
|
qemu_put_be64(f, e->id);
|
|
remain--;
|
|
}
|
|
assert(remain == 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused,
|
|
const VMStateField *field)
|
|
{
|
|
struct PacketIdQueue *q = priv;
|
|
USBRedirDevice *dev = q->dev;
|
|
int i, size;
|
|
uint64_t id;
|
|
|
|
size = qemu_get_be32(f);
|
|
DPRINTF("get_packet_id_q %s size %d\n", q->name, size);
|
|
for (i = 0; i < size; i++) {
|
|
id = qemu_get_be64(f);
|
|
packet_id_queue_add(q, id);
|
|
}
|
|
assert(q->size == size);
|
|
return 0;
|
|
}
|
|
|
|
static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = {
|
|
.name = "usb-redir-packet-id-q",
|
|
.put = usbredir_put_packet_id_q,
|
|
.get = usbredir_get_packet_id_q,
|
|
};
|
|
|
|
static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = {
|
|
.name = "usb-redir-packet-id-queue",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.fields = (VMStateField[]) {
|
|
{
|
|
.name = "queue",
|
|
.version_id = 0,
|
|
.field_exists = NULL,
|
|
.size = 0,
|
|
.info = &usbredir_ep_packet_id_q_vmstate_info,
|
|
.flags = VMS_SINGLE,
|
|
.offset = 0,
|
|
},
|
|
VMSTATE_END_OF_LIST()
|
|
}
|
|
};
|
|
|
|
|
|
/* For usb_redir_device_connect_header migration */
|
|
static const VMStateDescription usbredir_device_info_vmstate = {
|
|
.name = "usb-redir-device-info",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_UINT8(speed, struct usb_redir_device_connect_header),
|
|
VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header),
|
|
VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header),
|
|
VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header),
|
|
VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header),
|
|
VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header),
|
|
VMSTATE_UINT16(device_version_bcd,
|
|
struct usb_redir_device_connect_header),
|
|
VMSTATE_END_OF_LIST()
|
|
}
|
|
};
|
|
|
|
|
|
/* For usb_redir_interface_info_header migration */
|
|
static const VMStateDescription usbredir_interface_info_vmstate = {
|
|
.name = "usb-redir-interface-info",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_UINT32(interface_count,
|
|
struct usb_redir_interface_info_header),
|
|
VMSTATE_UINT8_ARRAY(interface,
|
|
struct usb_redir_interface_info_header, 32),
|
|
VMSTATE_UINT8_ARRAY(interface_class,
|
|
struct usb_redir_interface_info_header, 32),
|
|
VMSTATE_UINT8_ARRAY(interface_subclass,
|
|
struct usb_redir_interface_info_header, 32),
|
|
VMSTATE_UINT8_ARRAY(interface_protocol,
|
|
struct usb_redir_interface_info_header, 32),
|
|
VMSTATE_END_OF_LIST()
|
|
}
|
|
};
|
|
|
|
|
|
/* And finally the USBRedirDevice vmstate itself */
|
|
static const VMStateDescription usbredir_vmstate = {
|
|
.name = "usb-redir",
|
|
.version_id = 1,
|
|
.minimum_version_id = 1,
|
|
.pre_save = usbredir_pre_save,
|
|
.post_load = usbredir_post_load,
|
|
.fields = (VMStateField[]) {
|
|
VMSTATE_USB_DEVICE(dev, USBRedirDevice),
|
|
VMSTATE_TIMER_PTR(attach_timer, USBRedirDevice),
|
|
{
|
|
.name = "parser",
|
|
.version_id = 0,
|
|
.field_exists = NULL,
|
|
.size = 0,
|
|
.info = &usbredir_parser_vmstate_info,
|
|
.flags = VMS_SINGLE,
|
|
.offset = 0,
|
|
},
|
|
VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1,
|
|
usbredir_ep_vmstate, struct endp_data),
|
|
VMSTATE_STRUCT(cancelled, USBRedirDevice, 1,
|
|
usbredir_ep_packet_id_queue_vmstate,
|
|
struct PacketIdQueue),
|
|
VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1,
|
|
usbredir_ep_packet_id_queue_vmstate,
|
|
struct PacketIdQueue),
|
|
VMSTATE_STRUCT(device_info, USBRedirDevice, 1,
|
|
usbredir_device_info_vmstate,
|
|
struct usb_redir_device_connect_header),
|
|
VMSTATE_STRUCT(interface_info, USBRedirDevice, 1,
|
|
usbredir_interface_info_vmstate,
|
|
struct usb_redir_interface_info_header),
|
|
VMSTATE_END_OF_LIST()
|
|
}
|
|
};
|
|
|
|
static Property usbredir_properties[] = {
|
|
DEFINE_PROP_CHR("chardev", USBRedirDevice, cs),
|
|
DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, usbredirparser_warning),
|
|
DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str),
|
|
DEFINE_PROP_BOOL("streams", USBRedirDevice, enable_streams, true),
|
|
DEFINE_PROP_BOOL("suppress-remote-wake", USBRedirDevice,
|
|
suppress_remote_wake, true),
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
};
|
|
|
|
static void usbredir_class_initfn(ObjectClass *klass, void *data)
|
|
{
|
|
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
uc->realize = usbredir_realize;
|
|
uc->product_desc = "USB Redirection Device";
|
|
uc->unrealize = usbredir_unrealize;
|
|
uc->cancel_packet = usbredir_cancel_packet;
|
|
uc->handle_reset = usbredir_handle_reset;
|
|
uc->handle_data = usbredir_handle_data;
|
|
uc->handle_control = usbredir_handle_control;
|
|
uc->flush_ep_queue = usbredir_flush_ep_queue;
|
|
uc->ep_stopped = usbredir_ep_stopped;
|
|
uc->alloc_streams = usbredir_alloc_streams;
|
|
uc->free_streams = usbredir_free_streams;
|
|
dc->vmsd = &usbredir_vmstate;
|
|
device_class_set_props(dc, usbredir_properties);
|
|
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
|
|
}
|
|
|
|
static void usbredir_instance_init(Object *obj)
|
|
{
|
|
USBDevice *udev = USB_DEVICE(obj);
|
|
USBRedirDevice *dev = USB_REDIRECT(udev);
|
|
|
|
device_add_bootindex_property(obj, &dev->bootindex,
|
|
"bootindex", NULL,
|
|
&udev->qdev);
|
|
}
|
|
|
|
static const TypeInfo usbredir_dev_info = {
|
|
.name = TYPE_USB_REDIR,
|
|
.parent = TYPE_USB_DEVICE,
|
|
.instance_size = sizeof(USBRedirDevice),
|
|
.class_init = usbredir_class_initfn,
|
|
.instance_init = usbredir_instance_init,
|
|
};
|
|
module_obj(TYPE_USB_REDIR);
|
|
module_kconfig(USB);
|
|
|
|
static void usbredir_register_types(void)
|
|
{
|
|
type_register_static(&usbredir_dev_info);
|
|
}
|
|
|
|
type_init(usbredir_register_types)
|