From 8c57d8966ea966d23fbc4db77f7a8cdc0623b544 Mon Sep 17 00:00:00 2001
From: Nikias Bassen <nikias@gmx.li>
Date: Fri, 28 Aug 2009 00:05:45 +0200
Subject: [PATCH] New flag LIBUSB_TRANSFER_ZERO_PACKET for terminating bulk transfers

This patch fixes the Zero Length Packet issue by allowing the client
program to specify the LIBUSB_TRANSFER_ZERO_PACKET flag when calling
libusb_submit_transfer when the packet size is a multiple of
wMaxPacketSize. This will pass the URB_ZERO_PACKET flag to the
appropriate ioctl and thus triggers sending of the packet inside the
kernel. Otherwise such a packet would never get sent.
---
 libusb/libusb.h         |   23 ++++++++++++++++++++++-
 libusb/os/linux_usbfs.c |    3 +++
 libusb/os/linux_usbfs.h |    1 +
 3 files changed, 26 insertions(+), 1 deletions(-)

diff --git a/libusb/libusb.h b/libusb/libusb.h
index 1126380..ce7bfcf 100644
--- a/libusb/libusb.h
+++ b/libusb/libusb.h
@@ -672,7 +672,28 @@ enum libusb_transfer_flags {
 	 * If this flag is set, it is illegal to call libusb_free_transfer()
 	 * from your transfer callback, as this will result in a double-free
 	 * when this flag is acted upon. */
-	LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2
+	LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
+
+	/** Terminate bulk transfer by a zero length URB.
+	 * According to the USB spec the last packet of a bulk transfer _must_
+	 * be terminated by a zero length URB if it has the exact size of the
+	 * endpoint's maximum packet size (wMaxPacketSize).
+	 * By setting this flag the URB_ZERO_PACKET flag will be set before
+	 * performing IOCTL_USBFS_SUBMITURB. This will trigger sending of the
+	 * mentioned packet which otherwise would never get transmitted.
+	 *
+	 * @note Implementers: You need to read the wMaxPacketSize attribute
+	 *       of the usb endpoint you are writing to. If the size of the
+	 *       packet you are submitting is a multiple of wMaxPacketSize
+	 *       you need to set the LIBUSB_TRANSFER_ZERO_PACKET flag before
+	 *       calling libusb_submit_transfer(), something like:
+	 *
+	 *       if (length % dev->wMaxPacketSize == 0) {
+	 *           xfer->flags |= LIBUSB_TRANSFER_ZERO_PACKET;
+	 *       }
+	 *       libusb_submit_transfer(xfer);
+	 */
+	LIBUSB_TRANSFER_ZERO_PACKET = 1<<3
 };
 
 /** \ingroup asyncio
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c
index 1280188..1f6c989 100644
--- a/libusb/os/linux_usbfs.c
+++ b/libusb/os/linux_usbfs.c
@@ -1307,6 +1307,9 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer,
 		else
 			urb->buffer_length = MAX_BULK_BUFFER_LENGTH;
 
+		if (transfer->flags & LIBUSB_TRANSFER_ZERO_PACKET)
+			urb->flags |= USBFS_URB_ZERO_PACKET;
+
 		r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
 		if (r < 0) {
 			int j;
diff --git a/libusb/os/linux_usbfs.h b/libusb/os/linux_usbfs.h
index fdf5e9b..9ed0b4f 100644
--- a/libusb/os/linux_usbfs.h
+++ b/libusb/os/linux_usbfs.h
@@ -63,6 +63,7 @@ struct usbfs_getdriver {
 #define USBFS_URB_DISABLE_SPD	1
 #define USBFS_URB_ISO_ASAP	2
 #define USBFS_URB_QUEUE_BULK	0x10
+#define USBFS_URB_ZERO_PACKET	0x40
 
 enum usbfs_urb_type {
 	USBFS_URB_TYPE_ISO = 0,
-- 
1.6.0.4


