繁体   English   中英

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

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

每天-大约5至10次-我的USB相机从系统中消失。 从第一天开始就发生了这种情况,因为制造商驱动程序与Linux不兼容。 lsusbdmesg最初会正确显示它,但较长时间后有时会消失。 解决该问题的最佳方法是重置该相机的USB接口以将其取回。 我使用C源代码手动完成了几次,并且可以正常工作,但是当我在Bash循环中进行操作时,它似乎一次又一次地失败。 任何想法?

如何使两个程序都能工作,以使/dev/video012始终可用?

步骤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);
    }
  }
}

第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

不确定您是否正确设置了USB代码...

可以找到USB设备吗?

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

顺便说一句,此代码是从这里开始 如果可以找到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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM