title: Read/write on a pen drive using libusb: libusb_bulk_transfer()
body: <p>I am trying to perform read and write operations on a <a href="http://en.wikipedia.org/wiki/USB_flash_drive" rel="nofollow">pen drive</a>.</p>
  
  <p>Details: Vendor ID: 8564 and Product ID: 1000. It is a <a href="http://en.wikipedia.org/wiki/Transcend_Information" rel="nofollow">Transcend</a> JetFlash mass storage device.</p>
  
  <p>I am keen to know that whether it is possible to achieve a read/write on a pen drive. If it is, then is it going to happen the way I have tried in the code provided below.</p>
  
  <p>I have learnt the methods to get the device id, product id and endpoint addresses. This is what I have implemented.</p>
  
  <p>Here the device is getting acknowledged and opened. And, even the interface claims it is successful.</p>
  
  <p>But bulk transfer functions return -1.</p>
  
  <p>What is the explanation?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;string.h&gt;
  #include &lt;/usr/include/libusb-1.0/libusb.h&gt;
  
  #define BULK_EP_OUT     0x82
  #define BULK_EP_IN      0x02
  
  int main(void)
  {
      int r = 0, e = 0;
      struct libusb_device_handle *handle = NULL;
      struct libusb_device **devs;
      struct libusb_device *dev;
      struct libusb_device_descriptor desc;
      char str1[256], str2[256];
  
      /* Init libusb */
      r = libusb_init(NULL);
      if (r &lt; 0)
      {
          printf("\nfailed to initialise libusb\n");
          return 1;
      }
  
      handle = libusb_open_device_with_vid_pid(NULL, 0x8564, 0x1000);
      if(handle == NULL)
      {
          printf("\nError in device opening!");
      }
      else
          printf("\nDevice Opened");
  
      // Tell libusb to use the CONFIGNUM configuration of the device
  
      libusb_set_configuration(handle, 1);
      if(libusb_kernel_driver_active(handle, 0) == 1)
      {
          printf("\nKernel Driver Active");
          if(libusb_detach_kernel_driver(handle, 0) == 0)
              printf("\nKernel Driver Detached!");
      }
  
      e = libusb_claim_interface(handle, 0);
      if(e &lt; 0)
      {
          printf("\nCannot Claim Interface");
      }
      else
          printf("\nClaimed Interface");
  
        /* Communicate */
  
      int bytes_read;
      int nbytes = 256;
      unsigned char *my_string, *my_string1;
      int transferred = 0;
      my_string = (unsigned char *) malloc (nbytes + 1);
      my_string1 = (unsigned char *) malloc (nbytes + 1);
  
      strcpy(my_string, "divesd");
      printf("\nTo be sent : %s", my_string);
  
      e = libusb_bulk_transfer(handle, BULK_EP_OUT, my_string, bytes_read, &amp;transferred, 5000);
      printf("\nXfer returned with %d", e);
      printf("\nSent %d bytes with string: %s\n", transferred, my_string);
  
      libusb_bulk_transfer(handle, BULK_EP_IN, my_string1, 256, &amp;transferred, 5000);
      printf("\nXfer returned with %d", e);     //It returns -1... This is an error, I guess.
      printf("\nReceived %d bytes with string: %s\n", transferred, my_string1);
  
      e = libusb_release_interface(handle, 0);
      libusb_close(handle);
      libusb_exit(NULL);
      return 0;
  }
  </code></pre>