X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/0a966150a0bf41466c3c3e0e39672892bf6bdd01..fc893f8e33c70e0cd7e93a7997e53f7c23fe8de3:/common/usb_cdc.c

diff --git a/common/usb_cdc.c b/common/usb_cdc.c
index c8d5af1c..049cb8cf 100644
--- a/common/usb_cdc.c
+++ b/common/usb_cdc.c
@@ -33,7 +33,7 @@
  */
 
 #include "usb_cdc.h"
-#include "../include/config_gpio.h"
+#include "config_gpio.h"
 
 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
@@ -256,7 +256,8 @@ void usb_enable() {
 
 	// Wait for a short while
 	for (volatile size_t i=0; i<0x100000; i++);
-
+    //sleep(1);
+	
 	// Reconnect USB reconnect
 	AT91C_BASE_PIOA->PIO_SODR = GPIO_USB_PU;
 	AT91C_BASE_PIOA->PIO_OER = GPIO_USB_PU;
@@ -293,6 +294,21 @@ bool usb_poll()
   return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);
 }
 
+/**
+	In github PR #129, some users appears to get a false positive from
+	usb_poll, which returns true, but the usb_read operation
+	still returns 0.
+	This check is basically the same as above, but also checks
+	that the length available to read is non-zero, thus hopefully fixes the
+	bug.
+**/
+bool usb_poll_validate_length()
+{
+	if (!usb_check()) return false;
+	if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false;
+	return (pUdp->UDP_CSR[AT91C_EP_OUT] >> 16) >  0;
+}
+
 //*----------------------------------------------------------------------------
 //* \fn    usb_read
 //* \brief Read available data from Endpoint OUT
@@ -310,12 +326,13 @@ uint32_t usb_read(byte_t* data, size_t len) {
 			len -= packetSize;
 			while(packetSize--)
 				data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT];
+			
 			pUdp->UDP_CSR[AT91C_EP_OUT] &= ~(bank);
-			if (bank == AT91C_UDP_RX_DATA_BK0) {
+			
+			if (bank == AT91C_UDP_RX_DATA_BK0)
 				bank = AT91C_UDP_RX_DATA_BK1;
-			} else {
-				bank = AT91C_UDP_RX_DATA_BK0;
-			}
+			else
+				bank = AT91C_UDP_RX_DATA_BK0;		
 		}
 		if (time_out++ == 0x1fff) break;
 	}
@@ -329,11 +346,11 @@ uint32_t usb_read(byte_t* data, size_t len) {
 //* \brief Send through endpoint 2
 //*----------------------------------------------------------------------------
 uint32_t usb_write(const byte_t* data, const size_t len) {
-  size_t length = len;
+	size_t length = len;
 	uint32_t cpt = 0;
 
-  if (!length) return 0;
-  if (!usb_check()) return 0;
+	if (!length) return 0;
+	if (!usb_check()) return 0;
   
 	// Send the first packet
 	cpt = MIN(length, AT91C_EP_IN_SIZE-1);
@@ -351,14 +368,16 @@ uint32_t usb_write(const byte_t* data, const size_t len) {
 			if (!usb_check()) return length;
 		}
 		pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP);
+		
 		while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);
+		
 		pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY;
 	}
   
 	// Wait for the end of transfer
 	while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {
 		if (!usb_check()) return length;
-  }
+	}
   
 	pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP);
 	while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);
@@ -370,7 +389,7 @@ uint32_t usb_write(const byte_t* data, const size_t len) {
 //* \fn    AT91F_USB_SendData
 //* \brief Send Data through the control endpoint
 //*----------------------------------------------------------------------------
-unsigned int csrTab[100];
+unsigned int csrTab[100] = {0x00};
 unsigned char csrIdx = 0;
 
 static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {