Project

General

Profile

Download (5.68 KB) Statistics
| Branch: | Tag: | Revision:
1
/* AT91SAM7 USB Request Context for OpenPCD / OpenPICC
2
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
3
 *
4
 *  This program is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by 
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program; if not, write to the Free Software
16
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 */
19

    
20
#include <unistd.h>
21
#include <stdlib.h>
22
#include <sys/types.h>
23
#include <asm/bitops.h>
24
#include <os/dbgu.h>
25
#include <os/req_ctx.h>
26

    
27
#include "../openpcd.h"
28

    
29
/* FIXME: locking, FIFO order processing */
30

    
31
#if defined(__AT91SAM7S64__) || defined(RUN_FROM_RAM)
32
#define NUM_RCTX_SMALL 0
33
#define NUM_RCTX_LARGE 8
34
#else
35
#define NUM_RCTX_SMALL 0
36
#define NUM_RCTX_LARGE 20
37
#endif
38

    
39
#define NUM_REQ_CTX	(NUM_RCTX_SMALL+NUM_RCTX_LARGE)
40

    
41
static uint8_t rctx_data[NUM_RCTX_SMALL][RCTX_SIZE_SMALL];
42
static uint8_t rctx_data_large[NUM_RCTX_LARGE][RCTX_SIZE_LARGE];
43

    
44
static struct req_ctx req_ctx[NUM_REQ_CTX];
45

    
46
/* queue of RCTX indexed by their current state */
47
static struct req_ctx *req_ctx_queues[RCTX_STATE_COUNT], *req_ctx_tails[RCTX_STATE_COUNT];
48
static unsigned req_counts[RCTX_STATE_COUNT];
49

    
50
struct req_ctx __ramfunc *req_ctx_find_get(int large,
51
				 unsigned long old_state, 
52
				 unsigned long new_state)
53
{
54
	struct req_ctx *toReturn;
55
	unsigned long flags;
56

    
57
	if (old_state >= RCTX_STATE_COUNT || new_state >= RCTX_STATE_COUNT) {
58
		DEBUGPCR("Invalid parameters for req_ctx_find_get");
59
		return NULL;
60
	}
61
	local_irq_save(flags);
62
	toReturn = req_ctx_queues[old_state];
63
	if (toReturn) {
64
		if ((req_ctx_queues[old_state] = toReturn->next))
65
			toReturn->next->prev = NULL;
66
		else
67
			req_ctx_tails[old_state] = NULL;
68
		req_counts[old_state]--;
69
		if ((toReturn->prev = req_ctx_tails[new_state]))
70
			toReturn->prev->next = toReturn;
71
		else
72
			req_ctx_queues[new_state] = toReturn;
73
		req_ctx_tails[new_state] = toReturn;
74
		toReturn->state = new_state;
75
		toReturn->next = NULL;
76
		req_counts[new_state]++;
77
	}
78
	local_irq_restore(flags);
79
	return toReturn;
80
}
81

    
82
uint8_t req_ctx_num(struct req_ctx *ctx)
83
{
84
	return ctx - req_ctx;
85
}
86

    
87
void req_ctx_set_state(struct req_ctx *ctx, unsigned long new_state)
88
{
89
	unsigned long flags;
90
	unsigned old_state;
91

    
92
	if (new_state >= RCTX_STATE_COUNT) {
93
		DEBUGPCR("Invalid new_state for req_ctx_set_state");
94
		return;
95
	}
96
	local_irq_save(flags);
97
	old_state = ctx->state;
98
	if (ctx->prev)
99
		ctx->prev->next = ctx->next;
100
	else
101
		req_ctx_queues[old_state] = ctx->next;
102
	if (ctx->next)
103
		ctx->next->prev = ctx->prev;
104
	else
105
		req_ctx_tails[old_state] = ctx->prev;
106
	req_counts[old_state]--;
107
	if ((ctx->prev = req_ctx_tails[new_state]))
108
		ctx->prev->next = ctx;
109
	else
110
		req_ctx_queues[new_state] = ctx;
111
	req_ctx_tails[new_state] = ctx;
112
	ctx->state = new_state;
113
	ctx->next = NULL;
114
	req_counts[new_state]++;
115
	local_irq_restore(flags);
116
}
117

    
118
#ifdef DEBUG_REQCTX
119
void req_print(int state) {
120
	int count = 0;
121
	struct req_ctx *ctx, *last = NULL;
122
	DEBUGP("State [%02i] start <==> ", state);
123
	ctx = req_ctx_queues[state];
124
	while (ctx) {
125
		if (last != ctx->prev)
126
			DEBUGP("*INV_PREV* ");
127
		DEBUGP("%08X => ", ctx);
128
		last = ctx;
129
		ctx = ctx->next;
130
		count++;
131
		if (count > NUM_REQ_CTX) {
132
		  DEBUGP("*WILD POINTER* => ");
133
		  break;
134
		}
135
	}
136
	DEBUGPCR("NULL");
137
	if (!req_ctx_queues[state] && req_ctx_tails[state]) {
138
		DEBUGPCR("NULL head, NON-NULL tail");
139
	}
140
	if (last != req_ctx_tails[state]) {
141
		DEBUGPCR("Tail does not match last element");
142
	}
143
}
144
#endif
145

    
146
void req_ctx_put(struct req_ctx *ctx)
147
{
148
	unsigned long intcFlags;
149
	unsigned old_state;
150

    
151
	local_irq_save(intcFlags);
152
	old_state = ctx->state;
153
	if (ctx->prev)
154
		ctx->prev->next = ctx->next;
155
	else
156
		req_ctx_queues[old_state] = ctx->next;
157
	if (ctx->next)
158
		ctx->next->prev = ctx->prev;
159
	else
160
		req_ctx_tails[old_state] = ctx->prev;
161
	req_counts[old_state]--;
162
	if ((ctx->prev = req_ctx_tails[RCTX_STATE_FREE]))
163
		ctx->prev->next = ctx;
164
	else
165
		req_ctx_queues[RCTX_STATE_FREE] = ctx;
166
	req_ctx_tails[RCTX_STATE_FREE] = ctx;
167
	ctx->state = RCTX_STATE_FREE;
168
	ctx->next = NULL;
169
	req_counts[RCTX_STATE_FREE]++;
170
	local_irq_restore(intcFlags);
171
}
172

    
173
unsigned int req_ctx_count(unsigned long state)
174
{
175
	if (state >= RCTX_STATE_COUNT)
176
		return 0;
177
	return req_counts[state];
178
}
179

    
180
void req_ctx_init(void)
181
{
182
	int i;
183
	for (i = 0; i < NUM_RCTX_SMALL; i++) {
184
		req_ctx[i].prev = req_ctx + i - 1;
185
		req_ctx[i].next = req_ctx + i + 1;
186
		req_ctx[i].size = RCTX_SIZE_SMALL;
187
		req_ctx[i].tot_len = 0;
188
		req_ctx[i].data = rctx_data[i];
189
		req_ctx[i].state = RCTX_STATE_FREE;
190
		DEBUGPCR("SMALL req_ctx[%02i] initialized at %08X, Data: %08X => %08X",
191
			i, req_ctx + i, req_ctx[i].data, req_ctx[i].data + RCTX_SIZE_SMALL);
192
	}
193

    
194
	for (; i < NUM_REQ_CTX; i++) {
195
		req_ctx[i].prev = req_ctx + i - 1;
196
		req_ctx[i].next = req_ctx + i + 1;
197
		req_ctx[i].size = RCTX_SIZE_LARGE;
198
		req_ctx[i].tot_len = 0;
199
		req_ctx[i].data = rctx_data_large[i];
200
		req_ctx[i].state = RCTX_STATE_FREE;
201
		DEBUGPCR("LARGE req_ctx[%02i] initialized at %08X, Data: %08X => %08X",
202
			i, req_ctx + i, req_ctx[i].data, req_ctx[i].data + RCTX_SIZE_LARGE);
203
	}
204
	req_ctx[0].prev = NULL;
205
	req_ctx[NUM_REQ_CTX - 1].next = NULL;
206

    
207
	req_ctx_queues[RCTX_STATE_FREE] = req_ctx;
208
	req_ctx_tails[RCTX_STATE_FREE] = req_ctx + NUM_REQ_CTX - 1;
209
	req_counts[RCTX_STATE_FREE] = NUM_REQ_CTX;
210

    
211
	for (i = RCTX_STATE_FREE + 1; i < RCTX_STATE_COUNT; i++) {
212
		req_ctx_queues[i] = req_ctx_tails[i] = NULL;
213
		req_counts[i] = 0;
214
	}
215
}
(22-22/40)
Add picture from clipboard (Maximum size: 48.8 MB)