bash C - 如何在 Linux 下重置 USB 总线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18195237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:13:40  来源:igfitidea点击:

C - how can i reset the USB Bus under linux?

clinuxbashusb

提问by

Daily--about 5 to 10 times--my USB camera disappears from the system. It's been happening from day 1 because the manufacturer driver is not compatible with Linux. lsusband dmesgshow it correctly at first, but after a longer period of time sometimes it disappears. The best solution to fix it is to reset the USB interface for that camera to get it back. I did it a few times manually using the C source code and it worked, but when I am doing it in a Bash loop it seems it's failing over and over. Any idea?

每天——大约 5 到 10 次——我的 USB 摄像头从系统中消失。它从第一天开始就发生了,因为制造商驱动程序与 Linux 不兼容。lsusb并且一dmesg开始正确显示,但经过较长时间后有时会消失。修复它的最佳解决方案是重置该相机的 USB 接口以将其恢复。我使用 C 源代码手动做了几次并且它工作了,但是当我在 Bash 循环中做它时,它似乎一遍又一遍地失败。任何的想法?

How can I make both programs work, so that I get my /dev/video0or 1or 2always available?

我怎样才能让这两个程序的工作,让我得到我的 /dev/video0或者12始终可用?

Step 1:Resetting the USB bus under Linux using this code:

步骤 1:使用以下代码在 Linux 下重置 USB 总线:

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

Step 2:Running as scanner - to make sure video0 or 1 or 2 is available, if not available resetting the USB bus

第 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"

Step 3:Still it's not available?

第 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

回答by Jiejing Zhang

Don't sure your reset usb code right...

不确定您的重置 USB 代码是否正确...

Can you find the usb device ?

你能找到USB设备吗?

If you can, try below:

如果可以,请尝试以下操作:

BTW, This code is from here. should be work if you can find the usb node:

顺便说一句,此代码来自此处。如果你能找到 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;
}

If you can't, you maybe want to rescan to find the usb device like this:

如果你不能,你可能想重新扫描以找到这样的 USB 设备:

echo 1 > /sys/dev/block/8:16/device/rescan