/* usbreset -- send a USB port reset to a USB device * Source: * http://askubuntu.com/questions/645/how-do-you-reset-a-usb-device-from-the-command-line */ /* How to find device name: * lsusb * Find your device in the output, see the "Bus" and "Device numbers" * Device name is * /dev/bus/usb/<bus>/<device number> * For example, * /dev/bus/usb/001/001 */ #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <sys/ioctl.h> #include <linux/usbdevice_fs.h> int main(int argc, char **argv) { const char *filename; int fd; int rc; if (argc != 2) { fprintf(stderr, "Usage: usbreset device-filename\n"); return 1; } filename = argv[1]; fd = open(filename, O_WRONLY); if (fd < 0) { perror("Error opening output file"); return 1; } printf("Resetting USB device %s\n", filename); rc = ioctl(fd, USBDEVFS_RESET, 0); if (rc < 0) { perror("Error in ioctl"); return 1; } printf("Reset successful\n"); close(fd); return 0; }Source of this code
Compile the code:
cc usbreset.c -o usbresetMake the file executable
chmod +x usbresetThen, you can repeat the steps from this answer every time you need to restart the device. But you need to run manually lsusb, generate path to the device, and input superuser password every time. You can automatize this. We need to change owner to root and set setuid bit on the executable:
sudo chown root:root ~/src/usbreset/usbreset sudo chmod 4755 usbresetWe can see
$ ls -l usbreset -rwsr-xr-x 1 root root 8906 Sep 16 2015 usbresetMy PATH contains ~/bin directory, so in ~/bin I create symlink to the executable
ln -s ~/src/usbreset/usbreset usbreset-bin
$ ll usbreset-bin lrwxrwxrwx 1 alex alex 32 Oct 9 12:46 usbreset-bin -> /home/alex/src/usbreset/usbreset*Create a file
usbreset
in ~/bin
#!/usr/bin/python import subprocess devices = subprocess.check_output(['lsusb']).splitlines() wifi_devices = filter(lambda x: '802.11n' in x, devices) assert len(wifi_devices) == 1 wifi_device = wifi_devices[0] items = wifi_device.split(":")[0].split(" ") assert len(items) == 4 assert items[0] == "Bus" bus = items[1] assert items[2] == "Device" device = items[3] device_path = "/dev/bus/usb/{bus}/{device}".format(bus = bus, device = device) subprocess.call(("/home/alex/bin/usbreset-bin", device_path))You need to change "/home/alex" path to your home directory. Then, you can just run
usbresetin console for usb wi-fi resetting.
No comments:
Post a Comment