USB rebind via sudo tee (permission sysfs requiert root)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-23 08:37:41 +02:00
parent 29a7db028e
commit e2f65b1ba7

View File

@@ -124,8 +124,9 @@ def _usb_reset_canon():
def _usb_rebind_canon():
"""Unbind/rebind USB Canon — simule un debranchement/rebranchement physique."""
import glob
"""Unbind/rebind USB Canon — simule un debranchement/rebranchement physique.
Necessite sudoers NOPASSWD pour tee sur /sys/bus/usb/drivers/usb/{un,}bind."""
import glob, subprocess, time
try:
for f in glob.glob("/sys/bus/usb/devices/*/idVendor"):
if open(f).read().strip() == "04a9":
@@ -133,12 +134,15 @@ def _usb_rebind_canon():
__import__("os.path", fromlist=["dirname"]).dirname(f)
)
log.info(f"USB rebind Canon: unbind {dev_id}")
with open("/sys/bus/usb/drivers/usb/unbind", "w") as fh:
fh.write(dev_id)
import time
subprocess.run(
["sudo", "tee", "/sys/bus/usb/drivers/usb/unbind"],
input=dev_id.encode(), stdout=subprocess.DEVNULL, timeout=5,
)
time.sleep(2)
with open("/sys/bus/usb/drivers/usb/bind", "w") as fh:
fh.write(dev_id)
subprocess.run(
["sudo", "tee", "/sys/bus/usb/drivers/usb/bind"],
input=dev_id.encode(), stdout=subprocess.DEVNULL, timeout=5,
)
log.info(f"USB rebind Canon: bind {dev_id}")
time.sleep(3)
return True