Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelv...
[pandora-kernel.git] / drivers / staging / brcm80211 / util / nvram / nvram_ro.c
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 <linux/slab.h>
18 #include <linux/string.h>
19 #include <bcmdefs.h>
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
33 typedef 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
42 static vars_t *vars;
43
44 #define NVRAM_FILE      1
45
46 static char *findvar(char *vars, char *lim, const char *name);
47
48 #if defined(FLASH)
49 /* copy flash to ram */
50 static void get_flash_nvram(si_t *sih, struct nvram_header *nvh)
51 {
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
61         new = kmalloc(bufsz, GFP_ATOMIC);
62         if (new == NULL) {
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
80 int nvram_init(void *si)
81 {
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
91 int nvram_append(void *si, char *varlst, uint varsz)
92 {
93         uint bufsz = VARS_T_OH;
94         vars_t *new;
95
96         new = kmalloc(bufsz, GFP_ATOMIC);
97         if (new == NULL)
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
109 void nvram_exit(void *si)
110 {
111         vars_t *this, *next;
112         si_t *sih;
113
114         sih = (si_t *) si;
115         this = vars;
116
117         if (this)
118                 kfree(this->vars);
119
120         while (this) {
121                 next = this->next;
122                 kfree(this);
123                 this = next;
124         }
125         vars = NULL;
126 }
127
128 static 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] == '='))
137                         return &s[len + 1];
138
139                 while (*s++)
140                         ;
141         }
142
143         return NULL;
144 }
145
146 char *nvram_get(const char *name)
147 {
148         char *v = NULL;
149         vars_t *cur;
150
151         for (cur = vars; cur; cur = cur->next) {
152                 v = findvar(cur->vars, cur->vars + cur->size, name);
153                 if (v)
154                         break;
155         }
156
157         return v;
158 }
159
160 int nvram_set(const char *name, const char *value)
161 {
162         return 0;
163 }
164
165 int nvram_unset(const char *name)
166 {
167         return 0;
168 }
169
170 int nvram_reset(void *si)
171 {
172         return 0;
173 }
174
175 int nvram_commit(void)
176 {
177         return 0;
178 }
179
180 int 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;
191                 lim = (char *)(this->vars + this->size);
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 }