2 * Wireless network adapter utilities
4 * Copyright 2006, Broadcom Corporation
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 $
18 #include <sys/ioctl.h>
20 #include <linux/types.h>
22 typedef u_int64_t u64
;
23 typedef u_int32_t u32
;
24 typedef u_int16_t u16
;
26 #include <linux/sockios.h>
27 #include <linux/ethtool.h>
35 wl_ioctl(char *name
, int cmd
, void *buf
, int len
)
42 /* open socket to kernel */
43 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
52 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
53 ifr
.ifr_data
= (caddr_t
) &ioc
;
54 if ((ret
= ioctl(s
, SIOCDEVPRIVATE
, &ifr
)) < 0)
62 wl_get_dev_type(char *name
, void *buf
, int len
)
67 struct ethtool_drvinfo info
;
69 /* open socket to kernel */
70 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
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) {
83 strncpy(buf
, info
.driver
, len
);
94 if ((ret
= wl_get_dev_type(name
, buf
, 3)) < 0)
97 if (strncmp(buf
, "wl", 2))
99 if ((ret
= wl_ioctl(name
, WLC_GET_VERSION
, &val
, sizeof(val
))))
101 if (val
> WLC_IOCTL_VERSION
)
108 wl_iovar_getbuf(char *ifname
, char *iovar
, void *param
, int paramlen
, void *bufptr
, int buflen
)
114 namelen
= strlen(iovar
) + 1; /* length of iovar name plus null */
115 iolen
= namelen
+ paramlen
;
117 /* check for overflow */
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
);
130 wl_iovar_setbuf(char *ifname
, char *iovar
, void *param
, int paramlen
, void *bufptr
, int buflen
)
135 namelen
= strlen(iovar
) + 1; /* length of iovar name plus null */
136 iolen
= namelen
+ paramlen
;
138 /* check for overflow */
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
];
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
);
166 ret
= wl_iovar_getbuf(ifname
, iovar
, NULL
, 0, smbuf
, sizeof(smbuf
));
168 memcpy(bufptr
, smbuf
, buflen
);
176 * format a bsscfg indexed iovar buffer
179 wl_bssiovar_mkbuf(char *iovar
, int bssidx
, void *param
, int paramlen
, void *bufptr
, int buflen
,
182 char *prefix
= "bsscfg:";
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
) {
195 return BCME_BUFTOOSHORT
;
200 /* copy prefix, no null */
201 memcpy(p
, prefix
, prefixlen
);
204 /* copy iovar name including null */
205 memcpy(p
, iovar
, namelen
);
208 /* bss config index as first param */
209 memcpy(p
, &bssidx
, sizeof(int32
));
212 /* parameter buffer follows */
214 memcpy(p
, param
, paramlen
);
221 * set named & bss indexed driver variable to buffer value
224 wl_bssiovar_setbuf(char *ifname
, char *iovar
, int bssidx
, void *param
, int paramlen
, void *bufptr
,
230 err
= wl_bssiovar_mkbuf(iovar
, bssidx
, param
, paramlen
, bufptr
, buflen
, &iolen
);
234 return wl_ioctl(ifname
, WLC_SET_VAR
, bufptr
, iolen
);
238 * get named & bss indexed driver variable buffer value
241 wl_bssiovar_getbuf(char *ifname
, char *iovar
, int bssidx
, void *param
, int paramlen
, void *bufptr
,
247 err
= wl_bssiovar_mkbuf(iovar
, bssidx
, param
, paramlen
, bufptr
, buflen
, &iolen
);
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
];
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
);
278 memset(smbuf
, 0, sizeof(smbuf
));
279 err
= wl_bssiovar_getbuf(ifname
, iovar
, bssidx
, NULL
, 0, smbuf
, sizeof(smbuf
));
281 memcpy(outbuf
, smbuf
, len
);
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");
297 fprintf(stderr
, err_buf
);