]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/brcm80211/util/nvram/nvram_ro.c
Staging: brcm80211: remove BCMATTACHFN macro
[net-next-2.6.git] / drivers / staging / brcm80211 / util / nvram / nvram_ro.c
CommitLineData
a9533e7e
HP
1/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <typedefs.h>
18#include <bcmdefs.h>
3327989a 19#include <linux/string.h>
a9533e7e
HP
20#include <osl.h>
21#include <bcmutils.h>
22#include <siutils.h>
23#include <bcmendian.h>
24#include <bcmnvram.h>
25#include <sbchipc.h>
26#include <bcmsrom.h>
27#include <bcmotp.h>
28#include <bcmdevs.h>
29#include <hndsoc.h>
30
31#define NVR_MSG(x)
32
33typedef struct _vars {
34 struct _vars *next;
35 int bufsz; /* allocated size */
36 int size; /* actual vars size */
37 char *vars;
38} vars_t;
39
40#define VARS_T_OH sizeof(vars_t)
41
7e85c729 42static vars_t *vars;
a9533e7e
HP
43
44#define NVRAM_FILE 1
45
46static char *findvar(char *vars, char *lim, const char *name);
47
48#if defined(FLASH)
49/* copy flash to ram */
a2627bc0
JC
50static void BCMINITFN(get_flash_nvram) (si_t *sih, struct nvram_header *nvh)
51{
a9533e7e
HP
52 osl_t *osh;
53 uint nvs, bufsz;
54 vars_t *new;
55
56 osh = si_osh(sih);
57
58 nvs = R_REG(osh, &nvh->len) - sizeof(struct nvram_header);
59 bufsz = nvs + VARS_T_OH;
60
ca8c1e59
JC
61 new = (vars_t *) MALLOC(osh, bufsz);
62 if (new == NULL) {
a9533e7e
HP
63 NVR_MSG(("Out of memory for flash vars\n"));
64 return;
65 }
66 new->vars = (char *)new + VARS_T_OH;
67
68 new->bufsz = bufsz;
69 new->size = nvs;
70 new->next = vars;
71 vars = new;
72
73 bcopy((char *)(&nvh[1]), new->vars, nvs);
74
75 NVR_MSG(("%s: flash nvram @ %p, copied %d bytes to %p\n", __func__,
76 nvh, nvs, new->vars));
77}
78#endif /* FLASH */
79
0d2f0724 80int nvram_init(void *si)
a2627bc0 81{
a9533e7e
HP
82
83 /* Make sure we read nvram in flash just once before freeing the memory */
84 if (vars != NULL) {
85 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
86 return 0;
87 }
88 return 0;
89}
90
0d2f0724 91int nvram_append(void *si, char *varlst, uint varsz)
a2627bc0 92{
a9533e7e
HP
93 uint bufsz = VARS_T_OH;
94 vars_t *new;
95
ca8c1e59
JC
96 new = MALLOC(si_osh((si_t *) si), bufsz);
97 if (new == NULL)
a9533e7e
HP
98 return BCME_NOMEM;
99
100 new->vars = varlst;
101 new->bufsz = bufsz;
102 new->size = varsz;
103 new->next = vars;
104 vars = new;
105
106 return BCME_OK;
107}
108
a2627bc0
JC
109void BCMUNINITFN(nvram_exit) (void *si)
110{
a9533e7e
HP
111 vars_t *this, *next;
112 si_t *sih;
113
114 sih = (si_t *) si;
115 this = vars;
116
117 if (this)
118 MFREE(si_osh(sih), this->vars, this->size);
119
120 while (this) {
121 next = this->next;
122 MFREE(si_osh(sih), this, this->bufsz);
123 this = next;
124 }
125 vars = NULL;
126}
127
128static char *findvar(char *vars, char *lim, const char *name)
129{
130 char *s;
131 int len;
132
133 len = strlen(name);
134
135 for (s = vars; (s < lim) && *s;) {
136 if ((bcmp(s, name, len) == 0) && (s[len] == '='))
90ea2296 137 return &s[len + 1];
a9533e7e 138
62145822
JC
139 while (*s++)
140 ;
a9533e7e
HP
141 }
142
143 return NULL;
144}
145
146char *nvram_get(const char *name)
147{
148 char *v = NULL;
149 vars_t *cur;
150
ca8c1e59
JC
151 for (cur = vars; cur; cur = cur->next) {
152 v = findvar(cur->vars, cur->vars + cur->size, name);
153 if (v)
a9533e7e 154 break;
ca8c1e59 155 }
a9533e7e
HP
156
157 return v;
158}
159
0d2f0724 160int nvram_set(const char *name, const char *value)
a2627bc0 161{
a9533e7e
HP
162 return 0;
163}
164
0d2f0724 165int nvram_unset(const char *name)
a2627bc0 166{
a9533e7e
HP
167 return 0;
168}
169
0d2f0724 170int nvram_reset(void *si)
a2627bc0 171{
a9533e7e
HP
172 return 0;
173}
174
0d2f0724 175int nvram_commit(void)
a2627bc0 176{
a9533e7e
HP
177 return 0;
178}
179
180int nvram_getall(char *buf, int count)
181{
182 int len, resid = count;
183 vars_t *this;
184
185 this = vars;
186 while (this) {
187 char *from, *lim, *to;
188 int acc;
189
190 from = this->vars;
c03b63c1 191 lim = (char *)(this->vars + this->size);
a9533e7e
HP
192 to = buf;
193 acc = 0;
194 while ((from < lim) && (*from)) {
195 len = strlen(from) + 1;
196 if (resid < (acc + len))
197 return BCME_BUFTOOSHORT;
198 bcopy(from, to, len);
199 acc += len;
200 from += len;
201 to += len;
202 }
203
204 resid -= acc;
205 buf += acc;
206 this = this->next;
207 }
208 if (resid < 1)
209 return BCME_BUFTOOSHORT;
210 *buf = '\0';
211 return 0;
212}