Casto se mi stava, ze softwarove odpojim USB flash a hned po odpojeni si vzpomenu, ze chci jeste nejaky soubor nakopirovat. Nejjednodussi reseni je vytahnou a znovu zastrcit flashku do USB portu. Jenze co delat, kdyz flashka je pripojena do vzdaleneho pocitace?
Nemusi jit hned o flashku, muze jit treba o nereagujici usb modem, nebo jine libovolne zarizeni.
Resenim je poslat USB zarizeni USBDEVFS_RESET. Staci si napsat jednoduchy program, jehoz zdrojak je na stackoverflow:
/* usbreset -- send a USB port reset to a USB device */
#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;
}
Po kompilaci pomoci sudo gcc usbreset.c -o /usr/local/bin/usbreset
muzeme nastroj otestovat.
Pripojime flashku do USB. Mela by se sama pripojit:
martin@martin:/tmp$ mount | grep sdd
/dev/sdd1 on /media/martin/VANCL type vfat (rw,nosuid,nodev,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,flush,uhelper=udisks2)
martin@martin:/tmp$ lsusb | grep "Flash Drive"
Bus 002 Device 013: ID 1307:0165 Transcend Information, Inc. 2GB/4GB Flash Drive
martin@martin~$ ls /dev/ | grep sdd
sdd
sdd1


martin@martin:/tmp$ mount | grep sddmartin@martin:/tmp$ lsusb | grep "Flash Drive"Bus 002 Device 013: ID 1307:0165 Transcend Information, Inc. 2GB/4GB Flash Drive
martin@martin:~$ ls /dev/ | grep sddsdd# usbreset /dev/bus/usb/002/013Resetting USB device /dev/bus/usb/002/013Reset successfulmartin@martin:/tmp$ lsusb | grep "Flash Drive"Bus 002 Device 013: ID 1307:0165 Transcend Information, Inc. 2GB/4GB Flash Drivemartin@martin:/tmp$ udisks --detach /dev/sddmartin@martin:/tmp$ lsusb | grep "Flash Drive"martin@martin:~$ ls /dev/ | grep sddmartin@martin:~$ Pouzite zdroje: