move hardware monitoring support in a separate hwmon.mk (before adding other modules)
[openwrt.git] / package / nvram / src / wl.c
1 /*
2 * Wireless network adapter utilities
3 *
4 * Copyright 2004, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11 *
12 * $Id$
13 */
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <sys/ioctl.h>
19 #include <net/if.h>
20
21 #include <typedefs.h>
22 #include <wlutils.h>
23
24 int
25 wl_ioctl(char *name, int cmd, void *buf, int len)
26 {
27 struct ifreq ifr;
28 wl_ioctl_t ioc;
29 int ret = 0;
30 int s;
31
32 /* open socket to kernel */
33 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
34 perror("socket");
35 return errno;
36 }
37
38 /* do it */
39 ioc.cmd = cmd;
40 ioc.buf = buf;
41 ioc.len = len;
42 strncpy(ifr.ifr_name, name, IFNAMSIZ);
43 ifr.ifr_data = (caddr_t) &ioc;
44 if ((ret = ioctl(s, SIOCDEVPRIVATE, &ifr)) < 0)
45 if (cmd != WLC_GET_MAGIC)
46 perror(ifr.ifr_name);
47
48 /* cleanup */
49 close(s);
50 return ret;
51 }
52
53 int
54 wl_hwaddr(char *name, unsigned char *hwaddr)
55 {
56 struct ifreq ifr;
57 int ret = 0;
58 int s;
59
60 /* open socket to kernel */
61 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
62 perror("socket");
63 return errno;
64 }
65
66 /* do it */
67 strncpy(ifr.ifr_name, name, IFNAMSIZ);
68 if ((ret = ioctl(s, SIOCGIFHWADDR, &ifr)) == 0)
69 memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
70
71 /* cleanup */
72 close(s);
73 return ret;
74 }
75
76 int
77 wl_probe(char *name)
78 {
79 int ret, val;
80
81 /* Check interface */
82 if ((ret = wl_ioctl(name, WLC_GET_MAGIC, &val, sizeof(val))))
83 return ret;
84 if (val != WLC_IOCTL_MAGIC)
85 return -1;
86 if ((ret = wl_ioctl(name, WLC_GET_VERSION, &val, sizeof(val))))
87 return ret;
88 if (val > WLC_IOCTL_VERSION)
89 return -1;
90
91 return ret;
92 }
93
94 int
95 wl_set_val(char *name, char *var, void *val, int len)
96 {
97 char buf[128];
98 int buf_len;
99
100 /* check for overflow */
101 if ((buf_len = strlen(var)) + 1 + len > sizeof(buf))
102 return -1;
103
104 strcpy(buf, var);
105 buf_len += 1;
106
107 /* append int value onto the end of the name string */
108 memcpy(&buf[buf_len], val, len);
109 buf_len += len;
110
111 return wl_ioctl(name, WLC_SET_VAR, buf, buf_len);
112 }
113
114 int
115 wl_get_val(char *name, char *var, void *val, int len)
116 {
117 char buf[128];
118 int ret;
119
120 /* check for overflow */
121 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
122 return -1;
123
124 strcpy(buf, var);
125 if ((ret = wl_ioctl(name, WLC_GET_VAR, buf, sizeof(buf))))
126 return ret;
127
128 memcpy(val, buf, len);
129 return 0;
130 }
131
132 int
133 wl_set_int(char *name, char *var, int val)
134 {
135 return wl_set_val(name, var, &val, sizeof(val));
136 }
137
138 int
139 wl_get_int(char *name, char *var, int *val)
140 {
141 return wl_get_val(name, var, val, sizeof(*val));
142 }
143
144 /**************************************************************************
145 * The following code is from Broadcom (wl.c) *
146 **************************************************************************/
147
148 int
149 wl_iovar_getbuf(char *ifname, char *iovar, void *param,
150 int paramlen, void *bufptr, int buflen)
151 {
152 int err;
153 uint namelen;
154 uint iolen;
155
156 namelen = strlen(iovar) + 1; /* length of iovar name plus null */
157 iolen = namelen + paramlen;
158
159 /* check for overflow */
160 if (iolen > buflen)
161 return (-1);
162
163 memcpy(bufptr, iovar, namelen); /* copy iovar name including null */
164 memcpy((int8*)bufptr + namelen, param, paramlen);
165
166 err = wl_ioctl(ifname, WLC_GET_VAR, bufptr, buflen);
167
168 return (err);
169 }
170
171 int
172 wl_iovar_setbuf(char *ifname, char *iovar, void *param,
173 int paramlen, void *bufptr, int buflen)
174 {
175 uint namelen;
176 uint iolen;
177
178 namelen = strlen(iovar) + 1; /* length of iovar name plus null */
179 iolen = namelen + paramlen;
180
181 /* check for overflow */
182 if (iolen > buflen)
183 return (-1);
184
185 memcpy(bufptr, iovar, namelen); /* copy iovar name including null */
186 memcpy((int8*)bufptr + namelen, param, paramlen);
187
188 return wl_ioctl(ifname, WLC_SET_VAR, bufptr, iolen);
189 }
190
191 int
192 wl_iovar_set(char *ifname, char *iovar, void *param, int paramlen)
193 {
194 char smbuf[WLC_IOCTL_SMLEN];
195
196 return wl_iovar_setbuf(ifname, iovar, param, paramlen, smbuf, sizeof(smbuf));
197 }
198
199 int
200 wl_iovar_get(char *ifname, char *iovar, void *bufptr, int buflen)
201 {
202 char smbuf[WLC_IOCTL_SMLEN];
203 int ret;
204
205 /* use the return buffer if it is bigger than what we have on the stack */
206 if (buflen > sizeof(smbuf)) {
207 ret = wl_iovar_getbuf(ifname, iovar, NULL, 0, bufptr, buflen);
208 } else {
209 ret = wl_iovar_getbuf(ifname, iovar, NULL, 0, smbuf, sizeof(smbuf));
210 if (ret == 0)
211 memcpy(bufptr, smbuf, buflen);
212 }
213
214 return ret;
215 }
216
217 /*
218 * set named driver variable to int value
219 * calling example: wl_iovar_setint(ifname, "arate", rate)
220 */
221 int
222 wl_iovar_setint(char *ifname, char *iovar, int val)
223 {
224 return wl_iovar_set(ifname, iovar, &val, sizeof(val));
225 }
226
227 /*
228 * get named driver variable to int value and return error indication
229 * calling example: wl_iovar_getint(ifname, "arate", &rate)
230 */
231 int
232 wl_iovar_getint(char *ifname, char *iovar, int *val)
233 {
234 return wl_iovar_get(ifname, iovar, val, sizeof(int));
235 }
236
237 /*
238 * format a bsscfg indexed iovar buffer
239 */
240 static int
241 wl_bssiovar_mkbuf(char *iovar, int bssidx, void *param,
242 int paramlen, void *bufptr, int buflen, int *plen)
243 {
244 char *prefix = "bsscfg:";
245 int8* p;
246 uint prefixlen;
247 uint namelen;
248 uint iolen;
249
250 prefixlen = strlen(prefix); /* length of bsscfg prefix */
251 namelen = strlen(iovar) + 1; /* length of iovar name + null */
252 iolen = prefixlen + namelen + sizeof(int) + paramlen;
253
254 /* check for overflow */
255 if (buflen < 0 || iolen > (uint)buflen) {
256 *plen = 0;
257 return -1;
258 }
259
260 p = (int8*)bufptr;
261
262 /* copy prefix, no null */
263 memcpy(p, prefix, prefixlen);
264 p += prefixlen;
265
266 /* copy iovar name including null */
267 memcpy(p, iovar, namelen);
268 p += namelen;
269
270 /* bss config index as first param */
271 memcpy(p, &bssidx, sizeof(int32));
272 p += sizeof(int32);
273
274 /* parameter buffer follows */
275 if (paramlen)
276 memcpy(p, param, paramlen);
277
278 *plen = iolen;
279 return 0;
280 }
281
282 /*
283 * set named & bss indexed driver variable to buffer value
284 */
285 int
286 wl_bssiovar_setbuf(char *ifname, char *iovar, int bssidx, void *param,
287 int paramlen, void *bufptr, int buflen)
288 {
289 int err;
290 uint iolen;
291
292 err = wl_bssiovar_mkbuf(iovar, bssidx, param, paramlen, bufptr, buflen, &iolen);
293 if (err)
294 return err;
295
296 return wl_ioctl(ifname, WLC_SET_VAR, bufptr, iolen);
297 }
298
299 /*
300 * get named & bss indexed driver variable buffer value
301 */
302 int
303 wl_bssiovar_getbuf(char *ifname, char *iovar, int bssidx, void *param,
304 int paramlen, void *bufptr, int buflen)
305 {
306 int err;
307 uint iolen;
308
309 err = wl_bssiovar_mkbuf(iovar, bssidx, param, paramlen, bufptr, buflen, &iolen);
310 if (err)
311 return err;
312
313 return wl_ioctl(ifname, WLC_GET_VAR, bufptr, buflen);
314 }
315
316 /*
317 * set named & bss indexed driver variable to buffer value
318 */
319 int
320 wl_bssiovar_set(char *ifname, char *iovar, int bssidx, void *param, int paramlen)
321 {
322 char smbuf[WLC_IOCTL_SMLEN];
323
324 return wl_bssiovar_setbuf(ifname, iovar, bssidx, param, paramlen, smbuf, sizeof(smbuf));
325 }
326
327 /*
328 * get named & bss indexed driver variable buffer value
329 */
330 int
331 wl_bssiovar_get(char *ifname, char *iovar, int bssidx, void *outbuf, int len)
332 {
333 char smbuf[WLC_IOCTL_SMLEN];
334 int err;
335
336 /* use the return buffer if it is bigger than what we have on the stack */
337 if (len > (int)sizeof(smbuf)) {
338 err = wl_bssiovar_getbuf(ifname, iovar, bssidx, NULL, 0, outbuf, len);
339 } else {
340 memset(smbuf, 0, sizeof(smbuf));
341 err = wl_bssiovar_getbuf(ifname, iovar, bssidx, NULL, 0, smbuf, sizeof(smbuf));
342 if (err == 0)
343 memcpy(outbuf, smbuf, len);
344 }
345
346 return err;
347 }
348
349 /*
350 * set named & bss indexed driver variable to int value
351 */
352 int
353 wl_bssiovar_setint(char *ifname, char *iovar, int bssidx, int val)
354 {
355 return wl_bssiovar_set(ifname, iovar, bssidx, &val, sizeof(int));
356 }
This page took 0.077224 seconds and 5 git commands to generate.