1 |
32985a29
|
laforge
|
/* 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 |
5f148f59
|
(no author)
|
#include <unistd.h>
|
21 |
|
|
#include <stdlib.h>
|
22 |
caf50003
|
(no author)
|
#include <sys/types.h>
|
23 |
|
|
#include <asm/bitops.h>
|
24 |
520784c7
|
(no author)
|
#include <os/dbgu.h>
|
25 |
|
|
#include <os/req_ctx.h>
|
26 |
caf50003
|
(no author)
|
|
27 |
520784c7
|
(no author)
|
#include "../openpcd.h"
|
28 |
5f148f59
|
(no author)
|
|
29 |
|
|
/* FIXME: locking, FIFO order processing */
|
30 |
|
|
|
31 |
5a8cd9fe
|
Harald Welte
|
#if defined(__AT91SAM7S64__) || defined(RUN_FROM_RAM)
|
32 |
9662f283
|
Min Xu
|
#define NUM_RCTX_SMALL 0
|
33 |
|
|
#define NUM_RCTX_LARGE 8
|
34 |
b27e1861
|
Harald Welte
|
#else
|
35 |
9662f283
|
Min Xu
|
#define NUM_RCTX_SMALL 0
|
36 |
|
|
#define NUM_RCTX_LARGE 20
|
37 |
b27e1861
|
Harald Welte
|
#endif
|
38 |
9662f283
|
Min Xu
|
|
39 |
514b0f72
|
laforge
|
#define NUM_REQ_CTX (NUM_RCTX_SMALL+NUM_RCTX_LARGE)
|
40 |
|
|
|
41 |
373c172a
|
Harald Welte
|
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 |
514b0f72
|
laforge
|
|
44 |
c19a9345
|
(no author)
|
static struct req_ctx req_ctx[NUM_REQ_CTX];
|
45 |
5f148f59
|
(no author)
|
|
46 |
b6b2c7d4
|
Min Xu
|
/* 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 |
3cd467a8
|
Min Xu
|
static unsigned req_counts[RCTX_STATE_COUNT];
|
49 |
b6b2c7d4
|
Min Xu
|
|
50 |
2db7d2d9
|
laforge
|
struct req_ctx __ramfunc *req_ctx_find_get(int large,
|
51 |
514b0f72
|
laforge
|
unsigned long old_state,
|
52 |
|
|
unsigned long new_state)
|
53 |
5f148f59
|
(no author)
|
{
|
54 |
b6b2c7d4
|
Min Xu
|
struct req_ctx *toReturn;
|
55 |
c19a9345
|
(no author)
|
unsigned long flags;
|
56 |
5f148f59
|
(no author)
|
|
57 |
b6b2c7d4
|
Min Xu
|
if (old_state >= RCTX_STATE_COUNT || new_state >= RCTX_STATE_COUNT) {
|
58 |
|
|
DEBUGPCR("Invalid parameters for req_ctx_find_get");
|
59 |
|
|
return NULL;
|
60 |
5f148f59
|
(no author)
|
}
|
61 |
b6b2c7d4
|
Min Xu
|
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 |
3cd467a8
|
Min Xu
|
req_counts[old_state]--;
|
69 |
b6b2c7d4
|
Min Xu
|
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 |
3cd467a8
|
Min Xu
|
req_counts[new_state]++;
|
77 |
b6b2c7d4
|
Min Xu
|
}
|
78 |
|
|
local_irq_restore(flags);
|
79 |
|
|
return toReturn;
|
80 |
caf50003
|
(no author)
|
}
|
81 |
5f148f59
|
(no author)
|
|
82 |
373c172a
|
Harald Welte
|
uint8_t req_ctx_num(struct req_ctx *ctx)
|
83 |
5f148f59
|
(no author)
|
{
|
84 |
b6b2c7d4
|
Min Xu
|
return ctx - req_ctx;
|
85 |
5f148f59
|
(no author)
|
}
|
86 |
|
|
|
87 |
c19a9345
|
(no author)
|
void req_ctx_set_state(struct req_ctx *ctx, unsigned long new_state)
|
88 |
5f148f59
|
(no author)
|
{
|
89 |
c19a9345
|
(no author)
|
unsigned long flags;
|
90 |
b6b2c7d4
|
Min Xu
|
unsigned old_state;
|
91 |
c19a9345
|
(no author)
|
|
92 |
b6b2c7d4
|
Min Xu
|
if (new_state >= RCTX_STATE_COUNT) {
|
93 |
|
|
DEBUGPCR("Invalid new_state for req_ctx_set_state");
|
94 |
|
|
return;
|
95 |
|
|
}
|
96 |
c19a9345
|
(no author)
|
local_irq_save(flags);
|
97 |
b6b2c7d4
|
Min Xu
|
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 |
3cd467a8
|
Min Xu
|
req_counts[old_state]--;
|
107 |
b6b2c7d4
|
Min Xu
|
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 |
c19a9345
|
(no author)
|
ctx->state = new_state;
|
113 |
b6b2c7d4
|
Min Xu
|
ctx->next = NULL;
|
114 |
3cd467a8
|
Min Xu
|
req_counts[new_state]++;
|
115 |
c19a9345
|
(no author)
|
local_irq_restore(flags);
|
116 |
|
|
}
|
117 |
5f148f59
|
(no author)
|
|
118 |
b6b2c7d4
|
Min Xu
|
#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 |
c19a9345
|
(no author)
|
void req_ctx_put(struct req_ctx *ctx)
|
147 |
|
|
{
|
148 |
b6b2c7d4
|
Min Xu
|
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 |
3cd467a8
|
Min Xu
|
req_counts[old_state]--;
|
162 |
b6b2c7d4
|
Min Xu
|
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 |
3cd467a8
|
Min Xu
|
req_counts[RCTX_STATE_FREE]++;
|
170 |
b6b2c7d4
|
Min Xu
|
local_irq_restore(intcFlags);
|
171 |
5f148f59
|
(no author)
|
}
|
172 |
514b0f72
|
laforge
|
|
173 |
3cd467a8
|
Min Xu
|
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 |
514b0f72
|
laforge
|
void req_ctx_init(void)
|
181 |
|
|
{
|
182 |
|
|
int i;
|
183 |
|
|
for (i = 0; i < NUM_RCTX_SMALL; i++) {
|
184 |
b6b2c7d4
|
Min Xu
|
req_ctx[i].prev = req_ctx + i - 1;
|
185 |
|
|
req_ctx[i].next = req_ctx + i + 1;
|
186 |
514b0f72
|
laforge
|
req_ctx[i].size = RCTX_SIZE_SMALL;
|
187 |
b6b2c7d4
|
Min Xu
|
req_ctx[i].tot_len = 0;
|
188 |
514b0f72
|
laforge
|
req_ctx[i].data = rctx_data[i];
|
189 |
2fa10741
|
laforge
|
req_ctx[i].state = RCTX_STATE_FREE;
|
190 |
b6b2c7d4
|
Min Xu
|
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 |
514b0f72
|
laforge
|
}
|
193 |
|
|
|
194 |
b6b2c7d4
|
Min Xu
|
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 |
3cd467a8
|
Min Xu
|
req_counts[RCTX_STATE_FREE] = NUM_REQ_CTX;
|
210 |
b6b2c7d4
|
Min Xu
|
|
211 |
|
|
for (i = RCTX_STATE_FREE + 1; i < RCTX_STATE_COUNT; i++) {
|
212 |
|
|
req_ctx_queues[i] = req_ctx_tails[i] = NULL;
|
213 |
3cd467a8
|
Min Xu
|
req_counts[i] = 0;
|
214 |
514b0f72
|
laforge
|
}
|
215 |
|
|
}
|