2013-08-12 1 views
2

Ежедневно - примерно от 5 до 10 раз - моя USB-камера исчезает из системы. Это происходит с первого дня, потому что драйвер производителя не совместим с Linux. lsusb и dmesg покажите это правильно, но через более длительный период времени он исчезает. Лучшим решением для исправления является сброс интерфейса USB для этой камеры, чтобы вернуть его. Я делал это несколько раз вручную, используя исходный код C, и это сработало, но когда я делаю это в цикле Bash, кажется, что он терпит неудачу снова и снова. Есть идеи?C - как я могу сбросить USB-шину под Linux?

Как я могу заставить обе программы работать, так что я получаю свои /dev/video0 или 1 или 2 всегда доступны?

Шаг 1: Сброс шины USB под Linux, используя этот код:

/* few times it's resetting but when I use it in a Bash loop it's not doing it */ 
#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; 
    for (bus = busses; bus; bus = bus->next) { 
    struct usb_device *dev; 
    int val; 
    usb_dev_handle *junk; 
    for (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); 
    } 
    } 
} 

Шаг 2: Запуск в качестве сканера - чтобы убедиться, что video0 или 1 или 2 доступна, если нет в наличии сброса USB шины

#!/bin/bash 
j=true 
while $j 
do 
    for i in 0 1 2 
    do 
    tmp="/dev/video$i" 
    if [ -e $tmp ] 
    then 
     echo "/dev/video$i" 
     j=false 
    else 
     echo "NO - camera found - restarting the USB $i" 
     echo ROOT_PASSWORD_TO_EXECUTE | sudo -S /var/tmp/c-restartusb/restartusb 
    fi 
    done 
done 
echo "Camera - logic ended, expecting the camera is available now" 

Шаг 3: Еще не доступен?

NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 
NO - camera found - restart the USB 2 
NO - camera found - restart the USB 0 
NO - camera found - restart the USB 1 

ответ

0

Не уверен, что ваш сброс USB-код прямо ...

Можете ли вы найти устройство USB?

Если вы можете, попробуйте ниже:

КСТАТИ Этот код из here. должна быть работа, если вы можете найти узел USB:

/* 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; 
} 

Если вы не можете, вы, возможно, хотите отсканировать, чтобы найти USB устройства, как это:

echo 1 > /sys/dev/block/8:16/device/rescan 
Смежные вопросы