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 <stdint.h>
|
21
|
#include <stdlib.h>
|
22
|
|
23
|
#include <board.h>
|
24
|
#define __INLINE inline
|
25
|
#define IRQn_Type int
|
26
|
#include <cmsis/core_cm3.h>
|
27
|
|
28
|
#include "req_ctx.h"
|
29
|
|
30
|
|
31
|
#define local_irq_save(x) do { __disable_fault_irq(); __disable_irq(); } while(0)
|
32
|
#define local_irq_restore(x) do { __enable_fault_irq(); __enable_irq(); } while(0)
|
33
|
|
34
|
#define NUM_RCTX_SMALL 20
|
35
|
#define NUM_RCTX_LARGE 0
|
36
|
|
37
|
#define NUM_REQ_CTX (NUM_RCTX_SMALL+NUM_RCTX_LARGE)
|
38
|
|
39
|
static uint8_t rctx_data[NUM_RCTX_SMALL][RCTX_SIZE_SMALL];
|
40
|
static uint8_t rctx_data_large[NUM_RCTX_LARGE][RCTX_SIZE_LARGE];
|
41
|
|
42
|
static struct req_ctx req_ctx[NUM_REQ_CTX];
|
43
|
|
44
|
struct req_ctx __ramfunc *req_ctx_find_get(int large,
|
45
|
unsigned long old_state,
|
46
|
unsigned long new_state)
|
47
|
{
|
48
|
unsigned long flags;
|
49
|
uint8_t i;
|
50
|
|
51
|
if (large)
|
52
|
i = NUM_RCTX_SMALL;
|
53
|
else
|
54
|
i = 0;
|
55
|
|
56
|
for (; i < NUM_REQ_CTX; i++) {
|
57
|
local_irq_save(flags);
|
58
|
if (req_ctx[i].state == old_state) {
|
59
|
req_ctx[i].state = new_state;
|
60
|
local_irq_restore(flags);
|
61
|
return &req_ctx[i];
|
62
|
}
|
63
|
local_irq_restore(flags);
|
64
|
}
|
65
|
|
66
|
return NULL;
|
67
|
}
|
68
|
|
69
|
uint8_t req_ctx_num(struct req_ctx *ctx)
|
70
|
{
|
71
|
return ((char *)ctx - (char *)&req_ctx[0])/sizeof(*ctx);
|
72
|
}
|
73
|
|
74
|
void req_ctx_set_state(struct req_ctx *ctx, unsigned long new_state)
|
75
|
{
|
76
|
unsigned long flags;
|
77
|
|
78
|
/* FIXME: do we need this kind of locking, we're UP! */
|
79
|
local_irq_save(flags);
|
80
|
ctx->state = new_state;
|
81
|
local_irq_restore(flags);
|
82
|
}
|
83
|
|
84
|
void req_ctx_put(struct req_ctx *ctx)
|
85
|
{
|
86
|
req_ctx_set_state(ctx, RCTX_STATE_FREE);
|
87
|
}
|
88
|
|
89
|
void req_ctx_init(void)
|
90
|
{
|
91
|
int i;
|
92
|
|
93
|
for (i = 0; i < NUM_RCTX_SMALL; i++) {
|
94
|
req_ctx[i].size = RCTX_SIZE_SMALL;
|
95
|
req_ctx[i].data = rctx_data[i];
|
96
|
req_ctx[i].state = RCTX_STATE_FREE;
|
97
|
}
|
98
|
|
99
|
for (i = 0; i < NUM_RCTX_LARGE; i++) {
|
100
|
req_ctx[NUM_RCTX_SMALL+i].size = RCTX_SIZE_LARGE;
|
101
|
req_ctx[NUM_RCTX_SMALL+i].data = rctx_data_large[i];
|
102
|
req_ctx[NUM_RCTX_SMALL+i].state = RCTX_STATE_FREE;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
struct req_ctx *req_ctx_dequeue(struct llist_head *list)
|
107
|
{
|
108
|
unsigned long flags;
|
109
|
struct req_ctx *rctx;
|
110
|
|
111
|
local_irq_save(flags);
|
112
|
if (llist_empty(list)) {
|
113
|
local_irq_restore(flags);
|
114
|
return NULL;
|
115
|
}
|
116
|
|
117
|
rctx = llist_entry(list->next, struct req_ctx, list);
|
118
|
llist_del(&rctx->list);
|
119
|
local_irq_restore(flags);
|
120
|
|
121
|
return rctx;
|
122
|
}
|
123
|
|
124
|
void req_ctx_enqueue(struct llist_head *list, struct req_ctx *rctx)
|
125
|
{
|
126
|
unsigned long flags;
|
127
|
|
128
|
/* FIXME: do we need this kind of locking, we're UP! */
|
129
|
local_irq_save(flags);
|
130
|
llist_add_tail(&rctx->list, list);
|
131
|
local_irq_restore(flags);
|
132
|
}
|
133
|
|
134
|
void req_ctx_dump()
|
135
|
{
|
136
|
int i;
|
137
|
|
138
|
local_irq_save(flags);
|
139
|
printf("ctx status: ");
|
140
|
for(i = 0; i < NUM_REQ_CTX; i++)
|
141
|
printf(" %02x", req_ctx[i].state);
|
142
|
local_irq_restore(flags);
|
143
|
printf("\n\r");
|
144
|
}
|