From 868919107abb88c7e8dd9e1471a247d63f70522b Mon Sep 17 00:00:00 2001
From: Romuald Conty <romuald@libnfc.org>
Date: Thu, 23 Sep 2010 10:28:35 +0000
Subject: [PATCH] Fix Zero Lenght Packet problem on USB (Thanks to Phil)

---
 libnfc/drivers/pn53x_usb.c | 7 +++++++
 libnfc/drivers/pn53x_usb.h | 1 +
 2 files changed, 8 insertions(+)

diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c
index 2b7da83..6b80a35 100644
--- a/libnfc/drivers/pn53x_usb.c
+++ b/libnfc/drivers/pn53x_usb.c
@@ -65,10 +65,12 @@ get_end_points (struct usb_device *dev, usb_spec_t * pus)
     // Test if we dealing with a bulk IN endpoint
     if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) {
       pus->uiEndPointIn = uiEndPoint;
+      pus->wMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
     }
     // Test if we dealing with a bulk OUT endpoint
     if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) {
       pus->uiEndPointOut = uiEndPoint;
+      pus->wMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
     }
   }
 }
@@ -258,6 +260,11 @@ pn53x_usb_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szT
 #endif
 
   ret = usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) abtTx, szTxLen + 7, USB_TIMEOUT);
+  // XXX This little hack is a well know problem of libusb, see http://www.libusb.org/ticket/6 for more details
+  if ((ret % pus->wMaxPacketSize) == 0) {
+    usb_bulk_write (pus->pudh, pus->uiEndPointOut, "\0", 0, USB_TIMEOUT);
+  }
+
   if (ret < 0) {
     DBG ("usb_bulk_write failed with error %d", ret);
     pnd->iLastError = DEIO;
diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h
index 845b9fd..c93278f 100644
--- a/libnfc/drivers/pn53x_usb.h
+++ b/libnfc/drivers/pn53x_usb.h
@@ -27,6 +27,7 @@ typedef struct {
   usb_dev_handle *pudh;
   uint32_t uiEndPointIn;
   uint32_t uiEndPointOut;
+  uint32_t wMaxPacketSize;
 } usb_spec_t;
 
 typedef struct {