upgrade the ixp4xx ethernet driver to v0.3.1, split the Gateway 7001 support to two...
[openwrt.git] / package / broadcom-wl / src / wlc / ioctl.c
1 /*
2 * Wireless network adapter utilities
3 *
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.
11 *
12 * $Id: wl.c,v 1.1.1.11 2006/02/27 03:43:20 honor Exp $
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 #include <linux/types.h>
21
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>
28
29 #include <typedefs.h>
30 #include <wlioctl.h>
31 #include <bcmutils.h>
32 #include <wlutils.h>
33
34 int
35 wl_ioctl(char *name, int cmd, void *buf, int len)
36 {
37 struct ifreq ifr;
38 wl_ioctl_t ioc;
39 int ret = 0;
40 int s;
41
42 /* open socket to kernel */
43 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
44 perror("socket");
45 return errno;
46 }
47
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)
55
56 /* cleanup */
57 close(s);
58 return ret;
59 }
60
61 static inline int
62 wl_get_dev_type(char *name, void *buf, int len)
63 {
64 int s;
65 int ret;
66 struct ifreq ifr;
67 struct ethtool_drvinfo info;
68
69 /* open socket to kernel */
70 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
71 perror("socket");
72 return -1;
73 }
74
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);
84
85 close(s);
86 return ret;
87 }
88
89 int
90 wl_probe(char *name)
91 {
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;
103
104 return ret;
105 }
106
107 static int
108 wl_iovar_getbuf(char *ifname, char *iovar, void *param, int paramlen, void *bufptr, int buflen)
109 {
110 int err;
111 uint namelen;
112 uint iolen;
113
114 namelen = strlen(iovar) + 1; /* length of iovar name plus null */
115 iolen = namelen + paramlen;
116
117 /* check for overflow */
118 if (iolen > buflen)
119 return (BCME_BUFTOOSHORT);
120
121 memcpy(bufptr, iovar, namelen); /* copy iovar name including null */
122 memcpy((int8*)bufptr + namelen, param, paramlen);
123
124 err = wl_ioctl(ifname, WLC_GET_VAR, bufptr, buflen);
125
126 return (err);
127 }
128
129 static int
130 wl_iovar_setbuf(char *ifname, char *iovar, void *param, int paramlen, void *bufptr, int buflen)
131 {
132 uint namelen;
133 uint iolen;
134
135 namelen = strlen(iovar) + 1; /* length of iovar name plus null */
136 iolen = namelen + paramlen;
137
138 /* check for overflow */
139 if (iolen > buflen)
140 return (BCME_BUFTOOSHORT);
141
142 memcpy(bufptr, iovar, namelen); /* copy iovar name including null */
143 memcpy((int8*)bufptr + namelen, param, paramlen);
144
145 return wl_ioctl(ifname, WLC_SET_VAR, bufptr, iolen);
146 }
147
148 int
149 wl_iovar_set(char *ifname, char *iovar, void *param, int paramlen)
150 {
151 char smbuf[WLC_IOCTL_SMLEN];
152
153 return wl_iovar_setbuf(ifname, iovar, param, paramlen, smbuf, sizeof(smbuf));
154 }
155
156 int
157 wl_iovar_get(char *ifname, char *iovar, void *bufptr, int buflen)
158 {
159 char smbuf[WLC_IOCTL_SMLEN];
160 int ret;
161
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);
169 }
170
171 return ret;
172 }
173
174
175 /*
176 * format a bsscfg indexed iovar buffer
177 */
178 static int
179 wl_bssiovar_mkbuf(char *iovar, int bssidx, void *param, int paramlen, void *bufptr, int buflen,
180 int *plen)
181 {
182 char *prefix = "bsscfg:";
183 int8* p;
184 uint prefixlen;
185 uint namelen;
186 uint iolen;
187
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;
191
192 /* check for overflow */
193 if (buflen < 0 || iolen > (uint)buflen) {
194 *plen = 0;
195 return BCME_BUFTOOSHORT;
196 }
197
198 p = (int8*)bufptr;
199
200 /* copy prefix, no null */
201 memcpy(p, prefix, prefixlen);
202 p += prefixlen;
203
204 /* copy iovar name including null */
205 memcpy(p, iovar, namelen);
206 p += namelen;
207
208 /* bss config index as first param */
209 memcpy(p, &bssidx, sizeof(int32));
210 p += sizeof(int32);
211
212 /* parameter buffer follows */
213 if (paramlen)
214 memcpy(p, param, paramlen);
215
216 *plen = iolen;
217 return 0;
218 }
219
220 /*
221 * set named & bss indexed driver variable to buffer value
222 */
223 static int
224 wl_bssiovar_setbuf(char *ifname, char *iovar, int bssidx, void *param, int paramlen, void *bufptr,
225 int buflen)
226 {
227 int err;
228 int iolen;
229
230 err = wl_bssiovar_mkbuf(iovar, bssidx, param, paramlen, bufptr, buflen, &iolen);
231 if (err)
232 return err;
233
234 return wl_ioctl(ifname, WLC_SET_VAR, bufptr, iolen);
235 }
236
237 /*
238 * get named & bss indexed driver variable buffer value
239 */
240 static int
241 wl_bssiovar_getbuf(char *ifname, char *iovar, int bssidx, void *param, int paramlen, void *bufptr,
242 int buflen)
243 {
244 int err;
245 int iolen;
246
247 err = wl_bssiovar_mkbuf(iovar, bssidx, param, paramlen, bufptr, buflen, &iolen);
248 if (err)
249 return err;
250
251 return wl_ioctl(ifname, WLC_GET_VAR, bufptr, buflen);
252 }
253
254 /*
255 * set named & bss indexed driver variable to buffer value
256 */
257 int
258 wl_bssiovar_set(char *ifname, char *iovar, int bssidx, void *param, int paramlen)
259 {
260 char smbuf[WLC_IOCTL_SMLEN];
261
262 return wl_bssiovar_setbuf(ifname, iovar, bssidx, param, paramlen, smbuf, sizeof(smbuf));
263 }
264
265 /*
266 * get named & bss indexed driver variable buffer value
267 */
268 int
269 wl_bssiovar_get(char *ifname, char *iovar, int bssidx, void *outbuf, int len)
270 {
271 char smbuf[WLC_IOCTL_SMLEN];
272 int err;
273
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);
282 }
283
284 return err;
285 }
286
287 void
288 wl_printlasterror(char *name)
289 {
290 char err_buf[WLC_IOCTL_SMLEN];
291 strcpy(err_buf, "bcmerrstr");
292
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);
298 }
This page took 0.064838 seconds and 5 git commands to generate.