block: fix warning with calling smp_processor_id() in preemptible section
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmsmac / nvram.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 <bcmutils.h>
21 #include <bcmnvram.h>
22 #include <sbchipc.h>
23 #include <bcmdevs.h>
24 #include <hndsoc.h>
25
26 #define NVR_MSG(x)
27
28 typedef struct _vars {
29         struct _vars *next;
30         int bufsz;              /* allocated size */
31         int size;               /* actual vars size */
32         char *vars;
33 } vars_t;
34
35 #define VARS_T_OH       sizeof(vars_t)
36
37 static vars_t *vars;
38
39 #define NVRAM_FILE      1
40
41 static char *findvar(char *vars, char *lim, const char *name);
42
43 int nvram_init(void)
44 {
45
46         /* Make sure we read nvram in flash just once before freeing the memory */
47         if (vars != NULL) {
48                 NVR_MSG(("nvram_init: called again without calling nvram_exit()\n"));
49                 return 0;
50         }
51         return 0;
52 }
53
54 int nvram_append(char *varlst, uint varsz)
55 {
56         uint bufsz = VARS_T_OH;
57         vars_t *new;
58
59         new = kmalloc(bufsz, GFP_ATOMIC);
60         if (new == NULL)
61                 return -ENOMEM;
62
63         new->vars = varlst;
64         new->bufsz = bufsz;
65         new->size = varsz;
66         new->next = vars;
67         vars = new;
68
69         return 0;
70 }
71
72 void nvram_exit(void)
73 {
74         vars_t *this, *next;
75
76         this = vars;
77         if (this)
78                 kfree(this->vars);
79
80         while (this) {
81                 next = this->next;
82                 kfree(this);
83                 this = next;
84         }
85         vars = NULL;
86 }
87
88 static char *findvar(char *vars, char *lim, const char *name)
89 {
90         char *s;
91         int len;
92
93         len = strlen(name);
94
95         for (s = vars; (s < lim) && *s;) {
96                 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
97                         return &s[len + 1];
98
99                 while (*s++)
100                         ;
101         }
102
103         return NULL;
104 }
105
106 /*
107  * Search the name=value vars for a specific one and return its value.
108  * Returns NULL if not found.
109  */
110 char *getvar(char *vars, const char *name)
111 {
112         char *s;
113         int len;
114
115         if (!name)
116                 return NULL;
117
118         len = strlen(name);
119         if (len == 0)
120                 return NULL;
121
122         /* first look in vars[] */
123         for (s = vars; s && *s;) {
124                 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
125                         return &s[len + 1];
126
127                 while (*s++)
128                         ;
129         }
130         /* then query nvram */
131         return nvram_get(name);
132 }
133
134 /*
135  * Search the vars for a specific one and return its value as
136  * an integer. Returns 0 if not found.
137  */
138 int getintvar(char *vars, const char *name)
139 {
140         char *val;
141
142         val = getvar(vars, name);
143         if (val == NULL)
144                 return 0;
145
146         return simple_strtoul(val, NULL, 0);
147 }
148
149 char *nvram_get(const char *name)
150 {
151         char *v = NULL;
152         vars_t *cur;
153
154         for (cur = vars; cur; cur = cur->next) {
155                 v = findvar(cur->vars, cur->vars + cur->size, name);
156                 if (v)
157                         break;
158         }
159
160         return v;
161 }
162
163 int nvram_set(const char *name, const char *value)
164 {
165         return 0;
166 }
167
168 int nvram_unset(const char *name)
169 {
170         return 0;
171 }
172
173 int nvram_reset(void)
174 {
175         return 0;
176 }
177
178 int nvram_commit(void)
179 {
180         return 0;
181 }
182
183 int nvram_getall(char *buf, int count)
184 {
185         int len, resid = count;
186         vars_t *this;
187
188         this = vars;
189         while (this) {
190                 char *from, *lim, *to;
191                 int acc;
192
193                 from = this->vars;
194                 lim = (char *)(this->vars + this->size);
195                 to = buf;
196                 acc = 0;
197                 while ((from < lim) && (*from)) {
198                         len = strlen(from) + 1;
199                         if (resid < (acc + len))
200                                 return -EOVERFLOW;
201                         memcpy(to, from, len);
202                         acc += len;
203                         from += len;
204                         to += len;
205                 }
206
207                 resid -= acc;
208                 buf += acc;
209                 this = this->next;
210         }
211         if (resid < 1)
212                 return -EOVERFLOW;
213         *buf = '\0';
214         return 0;
215 }