Skip to content

Commit

Permalink
Add mechanism to reset device just before attaching
Browse files Browse the repository at this point in the history
Some device require reset after interacting with previous driver. When
device was used with usbip-host before, it took care of that when
detaching from previous target (VM). But if it was attached to some
other driver in sys-usb before, this needs to be done manually.

To limit possible impact, make this opt-in. There are two methods to opt
into this feature:
1. Enable it for all devices using (adjust sys-usb name if applicable):
   qvm-service --enable sys-usb usb-reset-on-attach
2. Enable for specific device by setting QUBES_USB_RESET udev property.

QubesOS/qubes-issues#8953
  • Loading branch information
marmarek committed May 29, 2024
1 parent 4f7fe16 commit 9cfe720
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions rpm_spec/qubes-usb-proxy.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ make install-vm DESTDIR=${RPM_BUILD_ROOT}
/usr/lib/qubes/usb-import
/usr/lib/qubes/usb-export
/usr/lib/qubes/usb-detach-all
/usr/lib/qubes/usb-reset

%changelog
@CHANGELOG@
10 changes: 10 additions & 0 deletions src/usb-export
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ fi
printf 'add %s' "$busid" > "$SYS_USBIP_HOST/match_busid" || exit 1
if [ -n "$attach_to_usbip" ]; then
echo "$busid" > "$SYS_USBIP_HOST/bind" || exit 1

# optionally reset the device to clear any state from previous driver
reset_on_attach=$(udevadm info --query=property \
--value --property=QUBES_USB_RESET --path="$devpath")
if [ -f /run/qubes-service/usb-reset-on-attach ]; then
reset_on_attach=1
fi
if [ -n "$reset_on_attach" ]; then
/usr/lib/qubes/usb-reset "$devpath"
fi
fi

# One more safety check - make sure the device is available
Expand Down
25 changes: 25 additions & 0 deletions src/usb-reset
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/python3

import sys
import os
import fcntl
from pathlib import Path

# from /usr/include/linux/usbdevice_fs.h
# _IO('U', 20)
USBDEVFS_RESET = 0x5514

def main():
if len(sys.argv) != 2:
print("Usage: usb-reset sysfs-devpath", file=sys.stderr())
exit(2)
devpath = sys.argv[1]
uevent = (Path(devpath) / "uevent").read_text()
devname = [line.partition("=")[2]
for line in uevent.splitlines()
if line.startswith("DEVNAME=")][0]
with (Path("/dev") / devname).open("w") as dev_f:
fcntl.ioctl(dev_f, USBDEVFS_RESET, 0)

if __name__ == "__main__":
main()

0 comments on commit 9cfe720

Please sign in to comment.