[MINI2440] Updated defconfig to add (optional) packages
[openwrt/mini2440.git] / package / broadcom-wl / src / wlc / ioctl.c
blob6c676c7c9bf0ba3beb358e8f3278249822d194c9
1 /*
2 * Wireless network adapter utilities
4 * Copyright 2006, 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.
12 * $Id: wl.c,v 1.1.1.11 2006/02/27 03:43:20 honor Exp $
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 #include <linux/types.h>
22 typedef u_int64_t u64;
23 typedef u_int32_t u32;
24 typedef u_int16_t u16;
25 typedef u_int8_t u8;
26 #include <linux/sockios.h>
27 #include <linux/ethtool.h>
29 #include <typedefs.h>
30 #include <wlioctl.h>
31 #include <bcmutils.h>
32 #include <wlutils.h>
34 int
35 wl_ioctl(char *name, int cmd, void *buf, int len)
37 struct ifreq ifr;
38 wl_ioctl_t ioc;
39 int ret = 0;
40 int s;
42 /* open socket to kernel */
43 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
44 perror("socket");
45 return errno;
48 /* do it */
49 ioc.cmd = cmd;
50 ioc.buf = buf;
51 ioc.len = len;
52 strncpy(ifr.ifr_name, name, IFNAMSIZ);
53 ifr.ifr_data = (caddr_t) &ioc;
54 if ((ret = ioctl(s, SIOCDEVPRIVATE, &ifr)) < 0)
56 /* cleanup */
57 close(s);
58 return ret;
61 static inline int
62 wl_get_dev_type(char *name, void *buf, int len)
64 int s;
65 int ret;
66 struct ifreq ifr;
67 struct ethtool_drvinfo info;
69 /* open socket to kernel */
70 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
71 perror("socket");
72 return -1;
75 /* get device type */
76 memset(&info, 0, sizeof(info));
77 info.cmd = ETHTOOL_GDRVINFO;
78 ifr.ifr_data = (caddr_t)&info;
79 strncpy(ifr.ifr_name, name, IFNAMSIZ);
80 if ((ret = ioctl(s, SIOCETHTOOL, &ifr)) < 0) {
81 *(char *)buf = '\0';
82 } else
83 strncpy(buf, info.driver, len);
85 close(s);
86 return ret;
89 int
90 wl_probe(char *name)
92 int ret, val;
93 char buf[3];
94 if ((ret = wl_get_dev_type(name, buf, 3)) < 0)
95 return ret;
96 /* Check interface */
97 if (strncmp(buf, "wl", 2))
98 return -1;
99 if ((ret = wl_ioctl(name, WLC_GET_VERSION, &val, sizeof(val))))
100 return ret;
101 if (val > WLC_IOCTL_VERSION)
102 return -1;
104 return ret;
107 static int
108 wl_iovar_getbuf(char *ifname, char *iovar, void *param, int paramlen, void *bufptr, int buflen)
110 int err;
111 uint namelen;
112 uint iolen;
114 namelen = strlen(iovar) + 1; /* length of iovar name plus null */
115 iolen = namelen + paramlen;
117 /* check for overflow */
118 if (iolen > buflen)
119 return (BCME_BUFTOOSHORT);
121 memcpy(bufptr, iovar, namelen); /* copy iovar name including null */
122 memcpy((int8*)bufptr + namelen, param, paramlen);
124 err = wl_ioctl(ifname, WLC_GET_VAR, bufptr, buflen);
126 return (err);
129 static int
130 wl_iovar_setbuf(char *ifname, char *iovar, void *param, int paramlen, void *bufptr, int buflen)
132 uint namelen;
133 uint iolen;
135 namelen = strlen(iovar) + 1; /* length of iovar name plus null */
136 iolen = namelen + paramlen;
138 /* check for overflow */
139 if (iolen > buflen)
140 return (BCME_BUFTOOSHORT);
142 memcpy(bufptr, iovar, namelen); /* copy iovar name including null */
143 memcpy((int8*)bufptr + namelen, param, paramlen);
145 return wl_ioctl(ifname, WLC_SET_VAR, bufptr, iolen);
149 wl_iovar_set(char *ifname, char *iovar, void *param, int paramlen)
151 char smbuf[WLC_IOCTL_SMLEN];
153 return wl_iovar_setbuf(ifname, iovar, param, paramlen, smbuf, sizeof(smbuf));
157 wl_iovar_get(char *ifname, char *iovar, void *bufptr, int buflen)
159 char smbuf[WLC_IOCTL_SMLEN];
160 int ret;
162 /* use the return buffer if it is bigger than what we have on the stack */
163 if (buflen > sizeof(smbuf)) {
164 ret = wl_iovar_getbuf(ifname, iovar, NULL, 0, bufptr, buflen);
165 } else {
166 ret = wl_iovar_getbuf(ifname, iovar, NULL, 0, smbuf, sizeof(smbuf));
167 if (ret == 0)
168 memcpy(bufptr, smbuf, buflen);
171 return ret;
176 * format a bsscfg indexed iovar buffer
178 static int
179 wl_bssiovar_mkbuf(char *iovar, int bssidx, void *param, int paramlen, void *bufptr, int buflen,
180 int *plen)
182 char *prefix = "bsscfg:";
183 int8* p;
184 uint prefixlen;
185 uint namelen;
186 uint iolen;
188 prefixlen = strlen(prefix); /* length of bsscfg prefix */
189 namelen = strlen(iovar) + 1; /* length of iovar name + null */
190 iolen = prefixlen + namelen + sizeof(int) + paramlen;
192 /* check for overflow */
193 if (buflen < 0 || iolen > (uint)buflen) {
194 *plen = 0;
195 return BCME_BUFTOOSHORT;
198 p = (int8*)bufptr;
200 /* copy prefix, no null */
201 memcpy(p, prefix, prefixlen);
202 p += prefixlen;
204 /* copy iovar name including null */
205 memcpy(p, iovar, namelen);
206 p += namelen;
208 /* bss config index as first param */
209 memcpy(p, &bssidx, sizeof(int32));
210 p += sizeof(int32);
212 /* parameter buffer follows */
213 if (paramlen)
214 memcpy(p, param, paramlen);
216 *plen = iolen;
217 return 0;
221 * set named & bss indexed driver variable to buffer value
223 static int
224 wl_bssiovar_setbuf(char *ifname, char *iovar, int bssidx, void *param, int paramlen, void *bufptr,
225 int buflen)
227 int err;
228 int iolen;
230 err = wl_bssiovar_mkbuf(iovar, bssidx, param, paramlen, bufptr, buflen, &iolen);
231 if (err)
232 return err;
234 return wl_ioctl(ifname, WLC_SET_VAR, bufptr, iolen);
238 * get named & bss indexed driver variable buffer value
240 static int
241 wl_bssiovar_getbuf(char *ifname, char *iovar, int bssidx, void *param, int paramlen, void *bufptr,
242 int buflen)
244 int err;
245 int iolen;
247 err = wl_bssiovar_mkbuf(iovar, bssidx, param, paramlen, bufptr, buflen, &iolen);
248 if (err)
249 return err;
251 return wl_ioctl(ifname, WLC_GET_VAR, bufptr, buflen);
255 * set named & bss indexed driver variable to buffer value
258 wl_bssiovar_set(char *ifname, char *iovar, int bssidx, void *param, int paramlen)
260 char smbuf[WLC_IOCTL_SMLEN];
262 return wl_bssiovar_setbuf(ifname, iovar, bssidx, param, paramlen, smbuf, sizeof(smbuf));
266 * get named & bss indexed driver variable buffer value
269 wl_bssiovar_get(char *ifname, char *iovar, int bssidx, void *outbuf, int len)
271 char smbuf[WLC_IOCTL_SMLEN];
272 int err;
274 /* use the return buffer if it is bigger than what we have on the stack */
275 if (len > (int)sizeof(smbuf)) {
276 err = wl_bssiovar_getbuf(ifname, iovar, bssidx, NULL, 0, outbuf, len);
277 } else {
278 memset(smbuf, 0, sizeof(smbuf));
279 err = wl_bssiovar_getbuf(ifname, iovar, bssidx, NULL, 0, smbuf, sizeof(smbuf));
280 if (err == 0)
281 memcpy(outbuf, smbuf, len);
284 return err;
287 void
288 wl_printlasterror(char *name)
290 char err_buf[WLC_IOCTL_SMLEN];
291 strcpy(err_buf, "bcmerrstr");
293 fprintf(stderr, "Error: ");
294 if ( wl_ioctl(name, WLC_GET_VAR, err_buf, sizeof (err_buf)) != 0)
295 fprintf(stderr, "Error getting the Errorstring from driver\n");
296 else
297 fprintf(stderr, err_buf);