C - how can i reset the USB Bus under linux? -
daily--about 5 10 times--my usb camera disappears system. it's been happening day 1 because manufacturer driver not compatible linux. lsusb
, dmesg
show correctly @ first, after longer period of time disappears. best solution fix reset usb interface camera back. did few times manually using c source code , worked, when doing in bash loop seems it's failing on , over. idea?
how can make both programs work, /dev/video0
or 1
or 2
available?
step 1: resetting usb bus under linux using code:
/* few times it's resetting when use in bash loop it's not doing */ #include <stdio.h> #include <usb.h> int main(int argc, char *argv[]) { struct usb_bus *busses; usb_init(); usb_find_busses(); usb_find_devices(); busses = usb_get_busses(); struct usb_bus *bus; int c, i, a; (bus = busses; bus; bus = bus->next) { struct usb_device *dev; int val; usb_dev_handle *junk; (dev = bus->devices; dev; dev = dev->next) { char buf[1024]; junk = usb_open ( dev ); usb_get_string_simple(junk,2,buf,1023); switch(argc) { case 1: if ( junk == null ) { printf("can't open %p (%s)\n", dev, buf ); } else if (strcmp(buf,"hd pro webcam c920")==0) { val = usb_reset(junk); printf( "reset %p %d (%s)\n", dev, val, buf ); } break; default: if ( junk == null ){ printf("can't open %p (%s)\n", dev, buf ); } else { val = usb_reset(junk); printf( "reset %p %d (%s)\n", dev, val, buf ); } } usb_close(junk); } } }
step 2: running scanner - make sure video0 or 1 or 2 available, if not available resetting usb bus
#!/bin/bash j=true while $j in 0 1 2 tmp="/dev/video$i" if [ -e $tmp ] echo "/dev/video$i" j=false else echo "no - camera found - restarting usb $i" echo root_password_to_execute | sudo -s /var/tmp/c-restartusb/restartusb fi done done echo "camera - logic ended, expecting camera available now"
step 3: still it's not available?
no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1 no - camera found - restart usb 2 no - camera found - restart usb 0 no - camera found - restart usb 1
don't sure reset usb code right...
can find usb device ?
if can, try below:
btw, code here. should work if can find usb node:
/* usbreset -- send usb port reset 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; }
if can't, maybe want rescan find usb device this:
echo 1 > /sys/dev/block/8:16/device/rescan
Comments
Post a Comment