staging: brcm80211: replaced BCME_OK by 0
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / brcm80211 / util / nvram / nvram_ro.c
blob3b2090d56343acc8d9b335812f612312b513ffce
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <bcmdefs.h>
20 #include <bcmutils.h>
21 #include <siutils.h>
22 #include <bcmnvram.h>
23 #include <sbchipc.h>
24 #include <bcmsrom.h>
25 #include <bcmotp.h>
26 #include <bcmdevs.h>
27 #include <hndsoc.h>
29 #define NVR_MSG(x)
31 typedef struct _vars {
32 struct _vars *next;
33 int bufsz; /* allocated size */
34 int size; /* actual vars size */
35 char *vars;
36 } vars_t;
38 #define VARS_T_OH sizeof(vars_t)
40 static vars_t *vars;
42 #define NVRAM_FILE 1
44 static char *findvar(char *vars, char *lim, const char *name);
46 #if defined(FLASH)
47 /* copy flash to ram */
48 static void get_flash_nvram(si_t *sih, struct nvram_header *nvh)
50 uint nvs, bufsz;
51 vars_t *new;
53 nvs = R_REG(&nvh->len) - sizeof(struct nvram_header);
54 bufsz = nvs + VARS_T_OH;
56 new = kmalloc(bufsz, GFP_ATOMIC);
57 if (new == NULL) {
58 NVR_MSG(("Out of memory for flash vars\n"));
59 return;
61 new->vars = (char *)new + VARS_T_OH;
63 new->bufsz = bufsz;
64 new->size = nvs;
65 new->next = vars;
66 vars = new;
68 memcpy(new->vars, &nvh[1], nvs);
70 NVR_MSG(("%s: flash nvram @ %p, copied %d bytes to %p\n", __func__,
71 nvh, nvs, new->vars));
73 #endif /* FLASH */
75 int nvram_init(void *si)
78 /* Make sure we read nvram in flash just once before freeing the memory */
79 if (vars != NULL) {
80 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
81 return 0;
83 return 0;
86 int nvram_append(void *si, char *varlst, uint varsz)
88 uint bufsz = VARS_T_OH;
89 vars_t *new;
91 new = kmalloc(bufsz, GFP_ATOMIC);
92 if (new == NULL)
93 return BCME_NOMEM;
95 new->vars = varlst;
96 new->bufsz = bufsz;
97 new->size = varsz;
98 new->next = vars;
99 vars = new;
101 return 0;
104 void nvram_exit(void *si)
106 vars_t *this, *next;
107 si_t *sih;
109 sih = (si_t *) si;
110 this = vars;
112 if (this)
113 kfree(this->vars);
115 while (this) {
116 next = this->next;
117 kfree(this);
118 this = next;
120 vars = NULL;
123 static char *findvar(char *vars, char *lim, const char *name)
125 char *s;
126 int len;
128 len = strlen(name);
130 for (s = vars; (s < lim) && *s;) {
131 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
132 return &s[len + 1];
134 while (*s++)
138 return NULL;
141 char *nvram_get(const char *name)
143 char *v = NULL;
144 vars_t *cur;
146 for (cur = vars; cur; cur = cur->next) {
147 v = findvar(cur->vars, cur->vars + cur->size, name);
148 if (v)
149 break;
152 return v;
155 int nvram_set(const char *name, const char *value)
157 return 0;
160 int nvram_unset(const char *name)
162 return 0;
165 int nvram_reset(void *si)
167 return 0;
170 int nvram_commit(void)
172 return 0;
175 int nvram_getall(char *buf, int count)
177 int len, resid = count;
178 vars_t *this;
180 this = vars;
181 while (this) {
182 char *from, *lim, *to;
183 int acc;
185 from = this->vars;
186 lim = (char *)(this->vars + this->size);
187 to = buf;
188 acc = 0;
189 while ((from < lim) && (*from)) {
190 len = strlen(from) + 1;
191 if (resid < (acc + len))
192 return BCME_BUFTOOSHORT;
193 memcpy(to, from, len);
194 acc += len;
195 from += len;
196 to += len;
199 resid -= acc;
200 buf += acc;
201 this = this->next;
203 if (resid < 1)
204 return BCME_BUFTOOSHORT;
205 *buf = '\0';
206 return 0;