title: libusb_bulk_transfer() read hangs after first successful libusb_bulk_transfer() write on USB mass storage device
body: <p>I am trying to perform write/read libusb_bulk_transfer() on my <a href="http://en.wikipedia.org/wiki/USB" rel="nofollow">USB</a> mass storage device. But after the first successful libusb_bulk_transfer() write, my program just hangs and does not perform a libusb_bulk_transfer() read. I observe that my USB device gets detached. Why am I not able to perform a read?</p>
  
  <p>Following is a snippet of my code:</p>
  
  <pre><code>e = libusb_get_configuration(handle, &amp;config2);
  if (e!=0)
  {
      printf("\n***Error in libusb_get_configuration\n");
      libusb_free_device_list(devs, 1);
      libusb_close(handle);
      return -1;
  }
  printf("\nConfigured value : %d", config2);
  
  if (config2 != 1)
  {
      libusb_set_configuration(handle, 1);
      if (e!=0)
      {
          printf("Error in libusb_set_configuration\n");
          libusb_free_device_list(devs, 1);
          libusb_close(handle);
          return -1;
      }
      else
          printf("\nDevice is in configured state!");
  }
  
  if (libusb_kernel_driver_active(handle, 0) == 1)
  {
      printf("\nKernel Driver Active");
      if (libusb_detach_kernel_driver(handle, 0) == 0)
          printf("\nKernel Driver Detached!");
      else
      {
          printf("\nCouldn't detach kernel driver!\n");
          libusb_free_device_list(devs, 1);
          libusb_close(handle);
          return -1;
      }
  }
  
  e = libusb_claim_interface(handle, 0);
  if (e &lt; 0)
  {
      printf("\nCannot Claim Interface");
      libusb_free_device_list(devs, 1);
      libusb_close(handle);
      return -1;
  }
  else
      printf("\nClaimed Interface\n");
  
  char *Report_to_write, *Report_to_read;
  int transferred = 0;
  int received = 0;
  int nbytes = 8; // As protocol suggested it should be 8 bytes
  
  Report_to_write = (char *)malloc(nbytes);
  Report_to_read = (char *)malloc(nbytes);
  
  Report_to_write[0] = 0x01; // cmd
  Report_to_write[1] = Report_to_write[2] = Report_to_write[3] = 15; // HOUR
  Report_to_write[4] = Report_to_write[5] = Report_to_write[6] = 30; // MIN
  Report_to_write[7] = 255; // some checksum
  
  memset(Report_to_read, '\0', 8);
  
  e = libusb_bulk_transfer(handle, BULK_EP_OUT, Report_to_write, 8, &amp;transferred, 0);
  
  if (e == 0 &amp;&amp; transferred == 8)
  {
      printf("\nWrite successful!");
      printf("\nSent %d bytes with string: %s\n", transferred, Report_to_write);
  }
  else
      printf("\nError in write! e = %d and transferred = %d\n", e, transferred);
  
  e = libusb_bulk_transfer(handle, BULK_EP_IN, Report_to_read, 8, &amp;received, 0);  //64: Max Packet   Length
  if (e == 0)
  {
      printf("\nReceived: ");
      printf("%c", Report_to_read[1]); //Will read a string
      //sleep(1);
  }
  else
  {
      printf("\nError in read! e = %d and received = %d\n", e, received);
      return -1;
  }
  
  e = libusb_release_interface(handle, 0);
  libusb_free_device_list(devs, 1);
  libusb_close(handle);
  libusb_exit(NULL);
  </code></pre>