title: Getting the v4l2 device number for a connected USB camera (webcam) from a C application (Linux)
body: <p>I'm working on a embedded linux system (yocto based) and I'm trying to simply get a list of the camera USB video devices (webcams) numbers with the related connected usb port from a C program.</p>
  <p>I'm able to get the devices list with vendor ID and connected port doing this:</p>
  <pre><code>void usbdevs()
  {
      libusb_device*** list=NULL;
      libusb_context *context = NULL;
      ssize_t count;
      uint8_t port;
      char ncameras=0;
  
      libusb_init(&amp;context);
  
      count = libusb_get_device_list(context,&amp;list);
  
      for(int i=0; i &lt; MAX_NUM_CAMS; i++)
          usb_dev_list[i]=0;
  
      for (size_t idx = 0; idx &lt; count; ++idx) {
          libusb_device *device = list[idx];
          struct libusb_device_descriptor desc = {0};
  
          libusb_get_device_descriptor(device, &amp;desc);
          port = libusb_get_port_number(device);
          printf(&quot;Vendor:Device = %04x:%04x Port: %d\n&quot;, desc.idVendor, desc.idProduct,port);
      }
  
      libusb_free_device_list(list, count);
      libusb_exit(context);
  
  }
  </code></pre>
  <p>What I need now is to know (from the C application) what v4l2 device number is related to the usb camera port, eg. I've got two webcam (same vendor ID) connected  which appear as /dev/video0 and /dev/video1 respectively and I can get the connected port for each one using the above code, but, how can I know which ports are connected each one?</p>
  <p>I tried to get information from the devices using ioctl calls as it is recommended <a href="https://stackoverflow.com/questions/4290834/how-to-get-a-list-of-video-capture-devices-web-cameras-on-linux-ubuntu-c/4290924">in this question</a> but when I run the code:</p>
  <pre><code>int checkvideodev()
  {
      int fd;
      struct video_capability video_cap;
      struct video_window     video_win;
      struct video_picture   video_pic;
  
      if((fd = open(&quot;/dev/video0&quot;, O_RDONLY)) == -1){
          perror(&quot;cam_info: Can't open device&quot;);
          return 1;
      }
  
      if(xioctl(fd, VIDIOCGCAP, &amp;video_cap) == -1)
          perror(&quot;cam_info: Can't get capabilities&quot;);
      else {
          printf(&quot;Name:\t\t '%s'\n&quot;, video_cap.name);
          printf(&quot;Minimum size:\t%d x %d\n&quot;, video_cap.minwidth, video_cap.minheight);
          printf(&quot;Maximum size:\t%d x %d\n&quot;, video_cap.maxwidth, video_cap.maxheight);
      }
  
      if(xioctl(fd, VIDIOCGWIN, &amp;video_win) == -1)
          perror(&quot;cam_info: Can't get window information&quot;);
      else
          printf(&quot;Current size:\t%d x %d\n&quot;, video_win.width, video_win.height);
  
      if(xioctl(fd, VIDIOCGPICT, &amp;video_pic) == -1)
          perror(&quot;cam_info: Can't get picture information&quot;);
      else
          printf(&quot;Current depth:\t%d\n&quot;, video_pic.depth);
  
      close(fd);
      return 0;
  }
  </code></pre>
  <p>I've got the next errors:</p>
  <pre><code>cam_info: Can't get capabilities: Inappropriate ioctl for device
  cam_info: Can't get window information: Inappropriate ioctl for device
  cam_info: Can't get picture information: Inappropriate ioctl for device
  </code></pre>
  <p>If I'm checking through command line for instance I can get the capabilities without issues running:</p>
  <pre><code>v4l2-ctl --device-/dev/video0 --list-formats-ext
  </code></pre>
  <p>Any ideas how can this be done?</p>
  <p>Thanks in advance.</p>