From c36ee55bd707b8467b6f560312e978c62d5fc1bd Mon Sep 17 00:00:00 2001
From: Min Xu <min.xu[A.T]min-info[D.O.T]net>
Date: Tue, 10 Sep 2013 14:23:18 -1000
Subject: [PATCH 3/6] req_ctx size changed to 960, and added function to count
 number of req_ctx in a specific state

additional usb state debugging in transmit
---
 firmware/src/os/pcd_enumerate.c |   26 +++++++++++++-------------
 firmware/src/os/req_ctx.c       |   32 +++++++++++++++++++++-----------
 firmware/src/os/req_ctx.h       |    9 +++------
 3 files changed, 37 insertions(+), 30 deletions(-)

diff --git a/firmware/src/os/pcd_enumerate.c b/firmware/src/os/pcd_enumerate.c
index 9223cd7..8dc7800 100644
--- a/firmware/src/os/pcd_enumerate.c
+++ b/firmware/src/os/pcd_enumerate.c
@@ -114,8 +114,6 @@ static void reset_ep(unsigned int ep)
 	AT91PS_UDP pUDP = upcd.pUdp;
 	struct req_ctx *rctx;
 
-	//pUDP->UDP_CSR[ep] = AT91C_UDP_EPEDS;
-
 	atomic_set(&upcd.ep[ep].pkts_in_transit, 0);
 
 	/* free all currently transmitting contexts */
@@ -127,6 +125,7 @@ static void reset_ep(unsigned int ep)
 
 	pUDP->UDP_RSTEP |= (1 << ep);
 	pUDP->UDP_RSTEP &= ~(1 << ep);
+	pUDP->UDP_CSR[ep] = AT91C_UDP_EPEDS;
 
 	upcd.ep[ep].incomplete.rctx = NULL;
 }
@@ -154,7 +153,8 @@ int udp_refill_ep(int ep)
 
 	/* If there are already two packets in transit, the DPR of
 	 * the SAM7 UDC doesn't have space for more data */
-	if (atomic_read(&upcd.ep[ep].pkts_in_transit) == 2 || (pUDP->UDP_CSR[ep] & AT91C_UDP_TXPKTRDY)) {
+	//	if (atomic_read(&upcd.ep[ep].pkts_in_transit) == 2 || (pUDP->UDP_CSR[ep] & AT91C_UDP_TXPKTRDY)) {
+	if (atomic_read(&upcd.ep[ep].pkts_in_transit) == 2) {
 		return -EBUSY;
 	}
 
@@ -178,12 +178,13 @@ int udp_refill_ep(int ep)
 			req_ctx_put(rctx);
 			return 0;
 		}
-		DEBUGPCR("USBTx %08X, Len = %04u, Head 4/Tail 4 bytes: %02X %02X %02X %02X / %02X %02X %02X %02X",
-			 rctx->data, rctx->tot_len,
+
+		DEBUGPCR("USBT(D=%08X, L=%04u, P=%02u) H4/T4: %02X %02X %02X %02X / %02X %02X %02X %02X",
+			 rctx->data, rctx->tot_len, req_ctx_count(epstate[ep].state_pending),
 			 rctx->data[4], rctx->data[5], rctx->data[6], rctx->data[7],
 			 rctx->data[rctx->tot_len - 4], rctx->data[rctx->tot_len - 3], 
 			 rctx->data[rctx->tot_len - 2], rctx->data[rctx->tot_len - 1]);
-		
+
 		start = 0;
 
 		upcd.ep[ep].incomplete.bytes_sent = 0;
@@ -226,19 +227,18 @@ static void udp_irq(void)
 		DEBUGI("ENDBUSRES ");
 		pUDP->UDP_ICR = AT91C_UDP_ENDBUSRES;
 		pUDP->UDP_IER = AT91C_UDP_EPINT0;
-		/* reset all endpoints */
-		pUDP->UDP_RSTEP = (unsigned int)-1;
-		pUDP->UDP_RSTEP = 0;
+		reset_ep(0);
+		reset_ep(1);
+		reset_ep(2);
+		reset_ep(3);
+
 		/* Enable the function */
 		pUDP->UDP_FADDR = AT91C_UDP_FEN;
+
 		/* Configure endpoint 0 */
 		pUDP->UDP_CSR[0] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL);
 		upcd.cur_config = 0;
 		upcd.state = USB_STATE_DEFAULT;
-
-		reset_ep(1);
-		reset_ep(2);
-		reset_ep(3);
 		
 #ifdef CONFIG_DFU
 		if (*dfu->dfu_state == DFU_STATE_appDETACH) {
diff --git a/firmware/src/os/req_ctx.c b/firmware/src/os/req_ctx.c
index b571f14..ab7775c 100644
--- a/firmware/src/os/req_ctx.c
+++ b/firmware/src/os/req_ctx.c
@@ -27,19 +27,15 @@
 #include "../openpcd.h"
 
 /* FIXME: locking, FIFO order processing */
-#define NUM_RCTX_SMALL 0
-#define NUM_RCTX_LARGE 19
 
-// 8 * 128 + 4 * 2048 =>   18 * 270 + 2 * 2048
-/*
 #if defined(__AT91SAM7S64__) || defined(RUN_FROM_RAM)
-#define NUM_RCTX_SMALL 16
-#define NUM_RCTX_LARGE 1
+#define NUM_RCTX_SMALL 0
+#define NUM_RCTX_LARGE 8
 #else
-#define NUM_RCTX_SMALL 8
-#define NUM_RCTX_LARGE 4
+#define NUM_RCTX_SMALL 0
+#define NUM_RCTX_LARGE 20
 #endif
-*/
+
 #define NUM_REQ_CTX	(NUM_RCTX_SMALL+NUM_RCTX_LARGE)
 
 static u_int8_t rctx_data[NUM_RCTX_SMALL][RCTX_SIZE_SMALL];
@@ -48,6 +44,7 @@ static u_int8_t rctx_data_large[NUM_RCTX_LARGE][RCTX_SIZE_LARGE], rctx_data_TEST
 static struct req_ctx req_ctx[NUM_REQ_CTX];
 
 static struct req_ctx *req_ctx_queues[RCTX_STATE_COUNT], *req_ctx_tails[RCTX_STATE_COUNT];
+static unsigned req_counts[RCTX_STATE_COUNT];
 
 struct req_ctx __ramfunc *req_ctx_find_get(int large,
 				 unsigned long old_state, 
@@ -67,6 +64,7 @@ struct req_ctx __ramfunc *req_ctx_find_get(int large,
 			toReturn->next->prev = NULL;
 		else
 			req_ctx_tails[old_state] = NULL;
+		req_counts[old_state]--;
 		if ((toReturn->prev = req_ctx_tails[new_state]))
 	  		toReturn->prev->next = toReturn;
 		else
@@ -74,6 +72,7 @@ struct req_ctx __ramfunc *req_ctx_find_get(int large,
 		req_ctx_tails[new_state] = toReturn;
 		toReturn->state = new_state;
 		toReturn->next = NULL;
+		req_counts[new_state]++;
 	}
 	local_irq_restore(flags);
 	return toReturn;
@@ -103,7 +102,7 @@ void req_ctx_set_state(struct req_ctx *ctx, unsigned long new_state)
 		ctx->next->prev = ctx->prev;
 	else
 		req_ctx_tails[old_state] = ctx->prev;
-
+	req_counts[old_state]--;
 	if ((ctx->prev = req_ctx_tails[new_state]))
 		ctx->prev->next = ctx;
 	else
@@ -111,6 +110,7 @@ void req_ctx_set_state(struct req_ctx *ctx, unsigned long new_state)
 	req_ctx_tails[new_state] = ctx;
 	ctx->state = new_state;
 	ctx->next = NULL;
+	req_counts[new_state]++;
 	local_irq_restore(flags);
 }
 
@@ -157,7 +157,7 @@ void req_ctx_put(struct req_ctx *ctx)
 		ctx->next->prev = ctx->prev;
 	else
 		req_ctx_tails[old_state] = ctx->prev;
-
+	req_counts[old_state]--;
 	if ((ctx->prev = req_ctx_tails[RCTX_STATE_FREE]))
 		ctx->prev->next = ctx;
 	else
@@ -165,9 +165,17 @@ void req_ctx_put(struct req_ctx *ctx)
 	req_ctx_tails[RCTX_STATE_FREE] = ctx;
 	ctx->state = RCTX_STATE_FREE;
 	ctx->next = NULL;
+	req_counts[RCTX_STATE_FREE]++;
 	local_irq_restore(intcFlags);
 }
 
+unsigned req_ctx_count(unsigned long state)
+{
+	if (state >= RCTX_STATE_COUNT)
+		return 0;
+	return req_counts[state];
+}
+
 void req_ctx_init(void)
 {
 	int i;
@@ -195,8 +203,10 @@ void req_ctx_init(void)
 
 	req_ctx_queues[RCTX_STATE_FREE] = req_ctx;
 	req_ctx_tails[RCTX_STATE_FREE] = req_ctx + NUM_REQ_CTX - 1;
+	req_counts[RCTX_STATE_FREE] = NUM_REQ_CTX;
 
 	for (i = RCTX_STATE_FREE + 1; i < RCTX_STATE_COUNT; i++) {
 		req_ctx_queues[i] = req_ctx_tails[i] = NULL;
+		req_counts[i] = 0;
 	}
 }
diff --git a/firmware/src/os/req_ctx.h b/firmware/src/os/req_ctx.h
index 22aac8e..7d09124 100644
--- a/firmware/src/os/req_ctx.h
+++ b/firmware/src/os/req_ctx.h
@@ -1,14 +1,10 @@
 #ifndef _REQ_CTX_H
 #define _REQ_CTX_H
 
-//#define RCTX_SIZE_LARGE	2048
-//#define RCTX_SIZE_SMALL	128
-
-#define RCTX_SIZE_LARGE       1000
-#define RCTX_SIZE_SMALL       270
+#define RCTX_SIZE_LARGE       960
+#define RCTX_SIZE_SMALL       320
 
 #define MAX_HDRSIZE	sizeof(struct openpcd_hdr)
-#define MAX_REQSIZE	(64-MAX_HDRSIZE)
 
 #define req_buf_payload(x)	(x->data[x->hdr_len])
 #define req_buf_hdr(x)		(x->data[0])
@@ -52,5 +48,6 @@ extern struct req_ctx *req_ctx_find_busy(void);
 extern void req_ctx_set_state(struct req_ctx *ctx, unsigned long new_state);
 extern void req_ctx_put(struct req_ctx *ctx);
 extern u_int8_t req_ctx_num(struct req_ctx *ctx);
+unsigned req_ctx_count(unsigned long state);
 
 #endif /* _REQ_CTX_H */
-- 
1.7.9.5

