title: C - how can i reset the USB Bus under linux?
body: <p>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. <code>lsusb</code> and <code>dmesg</code> show 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?</p>
  
  <p>How can I make both programs work, so that I get my  <code>/dev/video0</code> or <code>1</code> or <code>2</code> always available?</p>
  
  <p><em><strong>Step 1:</em></strong> Resetting the USB bus under Linux using this code:</p>
  
  <pre><code>/* few times it's resetting but when I use it in a Bash loop it's not doing it */
  #include &lt;stdio.h&gt;
  #include &lt;usb.h&gt;
  
  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-&gt;next) {
      struct usb_device *dev;
      int val;
      usb_dev_handle *junk;
      for (dev = bus-&gt;devices; dev; dev = dev-&gt;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);
      }
    }
  }
  </code></pre>
  
  <p><em><strong>Step 2:</em></strong> Running as scanner - to make sure video0 or 1 or 2 is available, if not available resetting the USB bus </p>
  
  <pre><code>#!/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"
  </code></pre>
  
  <p><em><strong>Step 3:</em></strong> Still it's not available?</p>
  
  <pre><code>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
  </code></pre>