title: configfs USB gadget write() call hangs
body: <p>I'm trying to emulate a HID device (a gamepad for PS3) from a Raspberry Pi Zero using configfs with kernel v4.19.40 (pretty much the same process as described <a href="https://www.isticktoit.net/?p=1383" rel="nofollow noreferrer">here</a>). Following the instructions in <a href="https://www.kernel.org/doc/Documentation/usb/gadget_hid.txt" rel="nofollow noreferrer">gadget_hid.txt</a>, I was able to get a working prototype that is able to send button presses from the Pi Zero to a USB host (a PS3). However, when some state changes on the USB host (i.e. a certain game is started), the program freezes. <code>strace</code> says that it is stuck on a call to <code>write()</code> to the configfs HID file (<code>/dev/hidgX</code>, in my case <code>/dev/hidg0</code>). How could the USB host block the device from sending data? If the USB host is not ready to receive, won't it just ignore the data sent?</p>
  
  <p>In debugging I found <a href="https://github.com/torvalds/linux/commit/072684e8c58d17e853f8e8b9f6d9ce2e58d2b036" rel="nofollow noreferrer">this patch</a> which seemed to be related but I saw no change after applying it.</p>
  
  <p>Here is a small code sample (the full version can be found in <a href="https://www.kernel.org/doc/Documentation/usb/gadget_hid.txt" rel="nofollow noreferrer">gadget_hid.txt</a>) that produces the same problem:</p>
  
  <pre class="lang-c prettyprint-override"><code>int main(int argc, const char *argv[]) {
    int fd = 0;
    char report[8];
    int to_send = 8;
    fd_set rfds;
  
    if ((fd = open("/dev/hidg0", O_RDWR, 0666)) == -1) {
      perror("/dev/hidg0");
      return 3;
    }
  
    while (1) {
      FD_ZERO(&amp;rfds);
      FD_SET(fd, &amp;rfds);
  
      // Generate some data to send
      fill_report(&amp;report);
  
      // Gets stuck here
      write(fd, report, to_send);
    }
  
    close(fd);
    return 0;
  }
  </code></pre>