]> code.delx.au - monosys/blob - usb-reset
multiboot-setup: support Debian10 installer
[monosys] / usb-reset
1 #!/usr/bin/env python2
2
3 import fcntl
4 import os
5 import subprocess
6 import sys
7 import time
8
9 if not sys.platform.startswith("linux"):
10 print >>sys.stderr, "Sorry, this tool requires Linux"
11 sys.exit(1)
12
13 try:
14 search_usb_id = sys.argv[1].lower()
15 except IndexError:
16 print >>sys.stderr, "Usage: %s vendorid:devid" % sys.argv[0]
17 print >>sys.stderr, "\nThis tool will reset all USB devices with the given ID (eg 1f4d:a803)"
18 sys.exit(1)
19
20
21 USBDEVFS_RESET = 21780
22
23 os.umask(0007)
24
25 p = subprocess.Popen(["lsusb"], stdout=subprocess.PIPE)
26 for line in p.stdout:
27 line = line.split()
28 usb_id = line[5].lower()
29 if usb_id != search_usb_id:
30 continue
31 bus = line[1]
32 dev = line[3].replace(":", "")
33
34 filename = "/dev/bus/usb/%s/%s" % (bus, dev)
35 print "Resetting", filename, "...",
36 sys.stdout.flush()
37 fd = os.open(filename, os.O_WRONLY)
38 ret = fcntl.ioctl(fd, USBDEVFS_RESET, 0)
39 if ret < 0:
40 print >>sys.stderr, "\nError in ioctl:", ret
41 sys.exit(1)
42 os.close(fd)
43 time.sleep(1)
44 print "done"
45