xref: /OK3568_Linux_fs/external/rkwifibt/drivers/bcmdhd/wl_iw.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Linux Wireless Extensions support
3  *
4  * Copyright (C) 1999-2017, Broadcom Corporation
5  *
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  *
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *      Notwithstanding the above, under no circumstances may you combine this
21  * software in any way with any other Broadcom software provided under a license
22  * other than the GPL, without Broadcom's express prior written consent.
23  *
24  *
25  * <<Broadcom-WL-IPTag/Open:>>
26  *
27  * $Id: wl_iw.c 616333 2016-02-01 05:30:29Z $
28  */
29 
30 #if defined(USE_IW)
31 #define LINUX_PORT
32 
33 #include <typedefs.h>
34 #include <linuxver.h>
35 #include <osl.h>
36 
37 #include <bcmutils.h>
38 #include <bcmendian.h>
39 #include <ethernet.h>
40 
41 #include <linux/if_arp.h>
42 #include <asm/uaccess.h>
43 #include <wlioctl.h>
44 #ifdef WL_NAN
45 #include <wlioctl_utils.h>
46 #endif
47 #include <wl_iw.h>
48 #include <wl_android.h>
49 #ifdef WL_ESCAN
50 #include <wl_escan.h>
51 #endif
52 #include <dhd_config.h>
53 
54 uint iw_msg_level = WL_ERROR_LEVEL;
55 
56 #define WL_ERROR_MSG(x, args...) \
57 	do { \
58 		if (iw_msg_level & WL_ERROR_LEVEL) { \
59 			printf("WEXT-ERROR) %s : " x, __func__, ## args); \
60 		} \
61 	} while (0)
62 #define WL_TRACE_MSG(x, args...) \
63 	do { \
64 		if (iw_msg_level & WL_TRACE_LEVEL) { \
65 			printf("WEXT-TRACE) %s : " x, __func__, ## args); \
66 		} \
67 	} while (0)
68 #define WL_SCAN_MSG(x, args...) \
69 	do { \
70 		if (iw_msg_level & WL_SCAN_LEVEL) { \
71 			printf("WEXT-SCAN) %s : " x, __func__, ## args); \
72 		} \
73 	} while (0)
74 #define WL_WSEC_MSG(x, args...) \
75 	do { \
76 		if (iw_msg_level & WL_WSEC_LEVEL) { \
77 			printf("WEXT-WSEC) %s : " x, __func__, ## args); \
78 		} \
79 	} while (0)
80 #define WL_ERROR(x) WL_ERROR_MSG x
81 #define WL_TRACE(x) WL_TRACE_MSG x
82 #define WL_SCAN(x) WL_SCAN_MSG x
83 #define WL_WSEC(x) WL_WSEC_MSG x
84 
85 #ifdef BCMWAPI_WPI
86 /* these items should evetually go into wireless.h of the linux system headfile dir */
87 #ifndef IW_ENCODE_ALG_SM4
88 #define IW_ENCODE_ALG_SM4 0x20
89 #endif
90 
91 #ifndef IW_AUTH_WAPI_ENABLED
92 #define IW_AUTH_WAPI_ENABLED 0x20
93 #endif
94 
95 #ifndef IW_AUTH_WAPI_VERSION_1
96 #define IW_AUTH_WAPI_VERSION_1	0x00000008
97 #endif
98 
99 #ifndef IW_AUTH_CIPHER_SMS4
100 #define IW_AUTH_CIPHER_SMS4	0x00000020
101 #endif
102 
103 #ifndef IW_AUTH_KEY_MGMT_WAPI_PSK
104 #define IW_AUTH_KEY_MGMT_WAPI_PSK 4
105 #endif
106 
107 #ifndef IW_AUTH_KEY_MGMT_WAPI_CERT
108 #define IW_AUTH_KEY_MGMT_WAPI_CERT 8
109 #endif
110 #endif /* BCMWAPI_WPI */
111 
112 /* Broadcom extensions to WEXT, linux upstream has obsoleted WEXT */
113 #ifndef IW_AUTH_KEY_MGMT_FT_802_1X
114 #define IW_AUTH_KEY_MGMT_FT_802_1X 0x04
115 #endif
116 
117 #ifndef IW_AUTH_KEY_MGMT_FT_PSK
118 #define IW_AUTH_KEY_MGMT_FT_PSK 0x08
119 #endif
120 
121 #ifndef IW_ENC_CAPA_FW_ROAM_ENABLE
122 #define IW_ENC_CAPA_FW_ROAM_ENABLE	0x00000020
123 #endif
124 
125 
126 /* FC9: wireless.h 2.6.25-14.fc9.i686 is missing these, even though WIRELESS_EXT is set to latest
127  * version 22.
128  */
129 #ifndef IW_ENCODE_ALG_PMK
130 #define IW_ENCODE_ALG_PMK 4
131 #endif
132 #ifndef IW_ENC_CAPA_4WAY_HANDSHAKE
133 #define IW_ENC_CAPA_4WAY_HANDSHAKE 0x00000010
134 #endif
135 /* End FC9. */
136 
137 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
138 #include <linux/rtnetlink.h>
139 #endif
140 
141 extern bool wl_iw_conn_status_str(uint32 event_type, uint32 status,
142 	uint32 reason, char* stringBuf, uint buflen);
143 
144 uint wl_msg_level = WL_ERROR_VAL;
145 
146 #define MAX_WLIW_IOCTL_LEN WLC_IOCTL_MEDLEN
147 
148 /* IOCTL swapping mode for Big Endian host with Little Endian dongle.  Default to off */
149 #define htod32(i) (i)
150 #define htod16(i) (i)
151 #define dtoh32(i) (i)
152 #define dtoh16(i) (i)
153 #define htodchanspec(i) (i)
154 #define dtohchanspec(i) (i)
155 
156 extern struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev);
157 extern int dhd_wait_pend8021x(struct net_device *dev);
158 
159 #if WIRELESS_EXT < 19
160 #define IW_IOCTL_IDX(cmd)	((cmd) - SIOCIWFIRST)
161 #define IW_EVENT_IDX(cmd)	((cmd) - IWEVFIRST)
162 #endif /* WIRELESS_EXT < 19 */
163 
164 
165 #ifndef WL_ESCAN
166 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
167 #define DAEMONIZE(a)	do { \
168 		allow_signal(SIGKILL);	\
169 		allow_signal(SIGTERM);	\
170 	} while (0)
171 #elif ((LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) && \
172 	(LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)))
173 #define DAEMONIZE(a) daemonize(a); \
174 	allow_signal(SIGKILL); \
175 	allow_signal(SIGTERM);
176 #else /* Linux 2.4 (w/o preemption patch) */
177 #define RAISE_RX_SOFTIRQ() \
178 	cpu_raise_softirq(smp_processor_id(), NET_RX_SOFTIRQ)
179 #define DAEMONIZE(a) daemonize(); \
180 	do { if (a) \
181 		strncpy(current->comm, a, MIN(sizeof(current->comm), (strlen(a) + 1))); \
182 	} while (0);
183 #endif /* LINUX_VERSION_CODE  */
184 
185 #define ISCAN_STATE_IDLE   0
186 #define ISCAN_STATE_SCANING 1
187 
188 /* the buf lengh can be WLC_IOCTL_MAXLEN (8K) to reduce iteration */
189 #define WLC_IW_ISCAN_MAXLEN   2048
190 typedef struct iscan_buf {
191 	struct iscan_buf * next;
192 	char   iscan_buf[WLC_IW_ISCAN_MAXLEN];
193 } iscan_buf_t;
194 
195 typedef struct iscan_info {
196 	struct net_device *dev;
197 	timer_list_compat_t timer;
198 	uint32 timer_ms;
199 	uint32 timer_on;
200 	int    iscan_state;
201 	iscan_buf_t * list_hdr;
202 	iscan_buf_t * list_cur;
203 
204 	/* Thread to work on iscan */
205 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
206 	struct task_struct *kthread;
207 #endif
208 	long sysioc_pid;
209 	struct semaphore sysioc_sem;
210 	struct completion sysioc_exited;
211 	char ioctlbuf[WLC_IOCTL_SMLEN];
212 } iscan_info_t;
213 static void wl_iw_timerfunc(ulong data);
214 static void wl_iw_set_event_mask(struct net_device *dev);
215 static int wl_iw_iscan(iscan_info_t *iscan, wlc_ssid_t *ssid, uint16 action);
216 #endif /* !WL_ESCAN */
217 
218 struct pmk_list {
219 	pmkid_list_v1_t pmkids;
220 	pmkid_v1_t foo[MAXPMKID - 1];
221 };
222 
223 typedef struct wl_wext_info {
224 	struct net_device *dev;
225 	dhd_pub_t *dhd;
226 	struct delayed_work pm_enable_work;
227 	struct mutex pm_sync;
228 	struct wl_conn_info conn_info;
229 	struct pmk_list pmk_list;
230 #ifndef WL_ESCAN
231 	struct iscan_info iscan;
232 #endif
233 } wl_wext_info_t;
234 
235 /* priv_link becomes netdev->priv and is the link between netdev and wlif struct */
236 typedef struct priv_link {
237 	wl_iw_t *wliw;
238 } priv_link_t;
239 
240 /* dev to priv_link */
241 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24))
242 #define WL_DEV_LINK(dev)       (priv_link_t*)(dev->priv)
243 #else
244 #define WL_DEV_LINK(dev)       (priv_link_t*)netdev_priv(dev)
245 #endif
246 
247 /* dev to wl_iw_t */
248 #define IW_DEV_IF(dev)          ((wl_iw_t*)(WL_DEV_LINK(dev))->wliw)
249 
swap_key_from_BE(wl_wsec_key_t * key)250 static void swap_key_from_BE(
251 	        wl_wsec_key_t *key
252 )
253 {
254 	key->index = htod32(key->index);
255 	key->len = htod32(key->len);
256 	key->algo = htod32(key->algo);
257 	key->flags = htod32(key->flags);
258 	key->rxiv.hi = htod32(key->rxiv.hi);
259 	key->rxiv.lo = htod16(key->rxiv.lo);
260 	key->iv_initialized = htod32(key->iv_initialized);
261 }
262 
swap_key_to_BE(wl_wsec_key_t * key)263 static void swap_key_to_BE(
264 	        wl_wsec_key_t *key
265 )
266 {
267 	key->index = dtoh32(key->index);
268 	key->len = dtoh32(key->len);
269 	key->algo = dtoh32(key->algo);
270 	key->flags = dtoh32(key->flags);
271 	key->rxiv.hi = dtoh32(key->rxiv.hi);
272 	key->rxiv.lo = dtoh16(key->rxiv.lo);
273 	key->iv_initialized = dtoh32(key->iv_initialized);
274 }
275 
276 static int
dev_wlc_ioctl(struct net_device * dev,int cmd,void * arg,int len)277 dev_wlc_ioctl(
278 	struct net_device *dev,
279 	int cmd,
280 	void *arg,
281 	int len
282 )
283 {
284 	struct dhd_pub *dhd = dhd_get_pub(dev);
285 	dhd_ioctl_t ioc;
286 	int8 index;
287 	int ret;
288 
289 	memset(&ioc, 0, sizeof(ioc));
290 	ioc.cmd = cmd;
291 	ioc.buf = arg;
292 	ioc.len = len;
293 
294 	index = dhd_net2idx(dhd->info, dev);
295 	if (index == DHD_BAD_IF) {
296 		WL_ERROR(("Bad ifidx from %s\n", dev->name));
297 		return -ENODEV;
298 	}
299 	ret = dhd_ioctl_process(dhd, index, &ioc, arg);
300 
301 	return ret;
302 }
303 
304 /*
305 set named driver variable to int value and return error indication
306 calling example: dev_wlc_intvar_set(dev, "arate", rate)
307 */
308 
309 static int
dev_wlc_intvar_set(struct net_device * dev,char * name,int val)310 dev_wlc_intvar_set(
311 	struct net_device *dev,
312 	char *name,
313 	int val)
314 {
315 	char buf[WLC_IOCTL_SMLEN];
316 	uint len;
317 
318 	val = htod32(val);
319 	len = bcm_mkiovar(name, (char *)(&val), sizeof(val), buf, sizeof(buf));
320 	ASSERT(len);
321 
322 	return (dev_wlc_ioctl(dev, WLC_SET_VAR, buf, len));
323 }
324 
325 #ifndef WL_ESCAN
326 static int
dev_iw_iovar_setbuf(struct net_device * dev,char * iovar,void * param,int paramlen,void * bufptr,int buflen)327 dev_iw_iovar_setbuf(
328 	struct net_device *dev,
329 	char *iovar,
330 	void *param,
331 	int paramlen,
332 	void *bufptr,
333 	int buflen)
334 {
335 	int iolen;
336 
337 	iolen = bcm_mkiovar(iovar, param, paramlen, bufptr, buflen);
338 	ASSERT(iolen);
339 	BCM_REFERENCE(iolen);
340 
341 	return (dev_wlc_ioctl(dev, WLC_SET_VAR, bufptr, iolen));
342 }
343 
344 static int
dev_iw_iovar_getbuf(struct net_device * dev,char * iovar,void * param,int paramlen,void * bufptr,int buflen)345 dev_iw_iovar_getbuf(
346 	struct net_device *dev,
347 	char *iovar,
348 	void *param,
349 	int paramlen,
350 	void *bufptr,
351 	int buflen)
352 {
353 	int iolen;
354 
355 	iolen = bcm_mkiovar(iovar, param, paramlen, bufptr, buflen);
356 	ASSERT(iolen);
357 	BCM_REFERENCE(iolen);
358 
359 	return (dev_wlc_ioctl(dev, WLC_GET_VAR, bufptr, buflen));
360 }
361 #endif
362 
363 #if WIRELESS_EXT > 17
364 static int
dev_wlc_bufvar_set(struct net_device * dev,char * name,char * buf,int len)365 dev_wlc_bufvar_set(
366 	struct net_device *dev,
367 	char *name,
368 	char *buf, int len)
369 {
370 	char *ioctlbuf;
371 	uint buflen;
372 	int error;
373 
374 	ioctlbuf = kmalloc(MAX_WLIW_IOCTL_LEN, GFP_KERNEL);
375 	if (!ioctlbuf)
376 		return -ENOMEM;
377 
378 	buflen = bcm_mkiovar(name, buf, len, ioctlbuf, MAX_WLIW_IOCTL_LEN);
379 	ASSERT(buflen);
380 	error = dev_wlc_ioctl(dev, WLC_SET_VAR, ioctlbuf, buflen);
381 
382 	kfree(ioctlbuf);
383 	return error;
384 }
385 #endif /* WIRELESS_EXT > 17 */
386 
387 /*
388 get named driver variable to int value and return error indication
389 calling example: dev_wlc_bufvar_get(dev, "arate", &rate)
390 */
391 
392 static int
dev_wlc_bufvar_get(struct net_device * dev,char * name,char * buf,int buflen)393 dev_wlc_bufvar_get(
394 	struct net_device *dev,
395 	char *name,
396 	char *buf, int buflen)
397 {
398 	char *ioctlbuf;
399 	int error;
400 
401 	uint len;
402 
403 	ioctlbuf = kmalloc(MAX_WLIW_IOCTL_LEN, GFP_KERNEL);
404 	if (!ioctlbuf)
405 		return -ENOMEM;
406 	len = bcm_mkiovar(name, NULL, 0, ioctlbuf, MAX_WLIW_IOCTL_LEN);
407 	ASSERT(len);
408 	BCM_REFERENCE(len);
409 	error = dev_wlc_ioctl(dev, WLC_GET_VAR, (void *)ioctlbuf, MAX_WLIW_IOCTL_LEN);
410 	if (!error)
411 		bcopy(ioctlbuf, buf, buflen);
412 
413 	kfree(ioctlbuf);
414 	return (error);
415 }
416 
417 /*
418 get named driver variable to int value and return error indication
419 calling example: dev_wlc_intvar_get(dev, "arate", &rate)
420 */
421 
422 static int
dev_wlc_intvar_get(struct net_device * dev,char * name,int * retval)423 dev_wlc_intvar_get(
424 	struct net_device *dev,
425 	char *name,
426 	int *retval)
427 {
428 	union {
429 		char buf[WLC_IOCTL_SMLEN];
430 		int val;
431 	} var;
432 	int error;
433 
434 	uint len;
435 	uint data_null;
436 
437 	len = bcm_mkiovar(name, (char *)(&data_null), 0, (char *)(&var), sizeof(var.buf));
438 	ASSERT(len);
439 	error = dev_wlc_ioctl(dev, WLC_GET_VAR, (void *)&var, len);
440 
441 	*retval = dtoh32(var.val);
442 
443 	return (error);
444 }
445 
446 /* Maintain backward compatibility */
447 #if WIRELESS_EXT < 13
448 struct iw_request_info
449 {
450 	__u16		cmd;		/* Wireless Extension command */
451 	__u16		flags;		/* More to come ;-) */
452 };
453 
454 typedef int (*iw_handler)(struct net_device *dev, struct iw_request_info *info,
455 	void *wrqu, char *extra);
456 #endif /* WIRELESS_EXT < 13 */
457 
458 #if WIRELESS_EXT > 12
459 #ifdef CONFIG_WEXT_PRIV
460 static int
wl_iw_set_leddc(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)461 wl_iw_set_leddc(
462 	struct net_device *dev,
463 	struct iw_request_info *info,
464 	union iwreq_data *wrqu,
465 	char *extra
466 )
467 {
468 	int dc = *(int *)extra;
469 	int error;
470 
471 	error = dev_wlc_intvar_set(dev, "leddc", dc);
472 	return error;
473 }
474 
475 static int
wl_iw_set_vlanmode(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)476 wl_iw_set_vlanmode(
477 	struct net_device *dev,
478 	struct iw_request_info *info,
479 	union iwreq_data *wrqu,
480 	char *extra
481 )
482 {
483 	int mode = *(int *)extra;
484 	int error;
485 
486 	mode = htod32(mode);
487 	error = dev_wlc_intvar_set(dev, "vlan_mode", mode);
488 	return error;
489 }
490 
491 static int
wl_iw_set_pm(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)492 wl_iw_set_pm(
493 	struct net_device *dev,
494 	struct iw_request_info *info,
495 	union iwreq_data *wrqu,
496 	char *extra
497 )
498 {
499 	int pm = *(int *)extra;
500 	int error;
501 
502 	pm = htod32(pm);
503 	error = dev_wlc_ioctl(dev, WLC_SET_PM, &pm, sizeof(pm));
504 	return error;
505 }
506 #endif /* CONFIG_WEXT_PRIV */
507 #endif /* WIRELESS_EXT > 12 */
508 
509 int
wl_iw_send_priv_event(struct net_device * dev,char * flag)510 wl_iw_send_priv_event(
511 	struct net_device *dev,
512 	char *flag
513 )
514 {
515 	union iwreq_data wrqu;
516 	char extra[IW_CUSTOM_MAX + 1];
517 	int cmd;
518 
519 	cmd = IWEVCUSTOM;
520 	memset(&wrqu, 0, sizeof(wrqu));
521 	if (strlen(flag) > sizeof(extra))
522 		return -1;
523 
524 	strncpy(extra, flag, sizeof(extra));
525 	extra[sizeof(extra) - 1] = '\0';
526 	wrqu.data.length = strlen(extra);
527 	wireless_send_event(dev, cmd, &wrqu, extra);
528 	WL_TRACE(("Send IWEVCUSTOM Event as %s\n", extra));
529 
530 	return 0;
531 }
532 
533 static int
wl_iw_config_commit(struct net_device * dev,struct iw_request_info * info,union iwreq_data * zwrq,char * extra)534 wl_iw_config_commit(
535 	struct net_device *dev,
536 	struct iw_request_info *info,
537 	union iwreq_data *zwrq,
538 	char *extra
539 )
540 {
541 	wlc_ssid_t ssid;
542 	int error;
543 	struct sockaddr bssid;
544 
545 	WL_TRACE(("%s: SIOCSIWCOMMIT\n", dev->name));
546 
547 	if ((error = dev_wlc_ioctl(dev, WLC_GET_SSID, &ssid, sizeof(ssid))))
548 		return error;
549 
550 	ssid.SSID_len = dtoh32(ssid.SSID_len);
551 
552 	if (!ssid.SSID_len)
553 		return 0;
554 
555 	bzero(&bssid, sizeof(struct sockaddr));
556 	if ((error = dev_wlc_ioctl(dev, WLC_REASSOC, &bssid, ETHER_ADDR_LEN))) {
557 		WL_ERROR(("WLC_REASSOC failed (%d)\n", error));
558 		return error;
559 	}
560 
561 	return 0;
562 }
563 
564 static int
wl_iw_get_name(struct net_device * dev,struct iw_request_info * info,union iwreq_data * cwrq,char * extra)565 wl_iw_get_name(
566 	struct net_device *dev,
567 	struct iw_request_info *info,
568 	union iwreq_data *cwrq,
569 	char *extra
570 )
571 {
572 	int phytype, err;
573 	uint band[3];
574 	char cap[5];
575 
576 	WL_TRACE(("%s: SIOCGIWNAME\n", dev->name));
577 
578 	cap[0] = 0;
579 	if ((err = dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &phytype, sizeof(phytype))) < 0)
580 		goto done;
581 	if ((err = dev_wlc_ioctl(dev, WLC_GET_BANDLIST, band, sizeof(band))) < 0)
582 		goto done;
583 
584 	band[0] = dtoh32(band[0]);
585 	switch (phytype) {
586 		case WLC_PHY_TYPE_A:
587 			strncpy(cap, "a", sizeof(cap));
588 			break;
589 		case WLC_PHY_TYPE_B:
590 			strncpy(cap, "b", sizeof(cap));
591 			break;
592 		case WLC_PHY_TYPE_G:
593 			if (band[0] >= 2)
594 				strncpy(cap, "abg", sizeof(cap));
595 			else
596 				strncpy(cap, "bg", sizeof(cap));
597 			break;
598 		case WLC_PHY_TYPE_N:
599 			if (band[0] >= 2)
600 				strncpy(cap, "abgn", sizeof(cap));
601 			else
602 				strncpy(cap, "bgn", sizeof(cap));
603 			break;
604 	}
605 done:
606 	(void)snprintf(cwrq->name, IFNAMSIZ, "IEEE 802.11%s", cap);
607 
608 	return 0;
609 }
610 
611 #define DHD_CHECK(dhd, dev) \
612 	if (!dhd) { \
613 		WL_ERROR (("[%s] dhd is NULL\n", dev->name)); \
614 		return -ENODEV; \
615 	} \
616 
617 static int
wl_iw_set_freq(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)618 wl_iw_set_freq(
619 	struct net_device *dev,
620 	struct iw_request_info *info,
621 	union iwreq_data *wrqu,
622 	char *extra
623 )
624 {
625 	struct iw_freq *fwrq = &wrqu->freq;
626 	int error, chan;
627 	uint sf = 0;
628 	struct dhd_pub *dhd = dhd_get_pub(dev);
629 	wl_wext_info_t *wext_info = NULL;
630 
631 	WL_TRACE(("%s: SIOCSIWFREQ\n", dev->name));
632 	DHD_CHECK(dhd, dev);
633 	wext_info = dhd->wext_info;
634 
635 	/* Setting by channel number */
636 	if (fwrq->e == 0 && fwrq->m < MAXCHANNEL) {
637 		chan = fwrq->m;
638 	}
639 
640 	/* Setting by frequency */
641 	else {
642 		/* Convert to MHz as best we can */
643 		if (fwrq->e >= 6) {
644 			fwrq->e -= 6;
645 			while (fwrq->e--)
646 				fwrq->m *= 10;
647 		} else if (fwrq->e < 6) {
648 			while (fwrq->e++ < 6)
649 				fwrq->m /= 10;
650 		}
651 	/* handle 4.9GHz frequencies as Japan 4 GHz based channelization */
652 		if (fwrq->m > 4000 && fwrq->m < 5000) {
653 			sf = WF_CHAN_FACTOR_4_G; /* start factor for 4 GHz */
654 		}
655 		chan = wf_mhz2channel(fwrq->m, sf);
656 	}
657 	if (wext_info)
658 		wext_info->conn_info.channel = chan;
659 	WL_MSG(dev->name, "chan=%d\n", chan);
660 	chan = htod32(chan);
661 	if ((error = dev_wlc_ioctl(dev, WLC_SET_CHANNEL, &chan, sizeof(chan)))) {
662 		WL_ERROR(("WLC_SET_CHANNEL failed (%d).\n", error));
663 		return error;
664 	}
665 
666 	/* -EINPROGRESS: Call commit handler */
667 	return -EINPROGRESS;
668 }
669 
670 static int
wl_iw_get_freq(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)671 wl_iw_get_freq(
672 	struct net_device *dev,
673 	struct iw_request_info *info,
674 	union iwreq_data *wrqu,
675 	char *extra
676 )
677 {
678 	struct dhd_pub *dhd = dhd_get_pub(dev);
679 	struct iw_freq *fwrq = &wrqu->freq;
680 	int error;
681 	u32 val;
682 	chanspec_t chanspec = 0;
683 	int ctl_chan;
684 
685 	WL_TRACE(("%s: SIOCGIWFREQ\n", dev->name));
686 
687 	DHD_CHECK(dhd, dev);
688 	if ((error = dev_wlc_intvar_get(dev, "chanspec", &val)))
689 		return error;
690 	chanspec = val;
691 	chanspec = wl_ext_chspec_driver_to_host(dhd, chanspec);
692 	ctl_chan = wf_chspec_ctlchan(chanspec);
693 
694 	/* Return radio channel in channel form */
695 	fwrq->m = ctl_chan;
696 	fwrq->e = dtoh32(0);
697 	return 0;
698 }
699 
700 static int
wl_iw_set_mode(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)701 wl_iw_set_mode(
702 	struct net_device *dev,
703 	struct iw_request_info *info,
704 	union iwreq_data *wrqu,
705 	char *extra
706 )
707 {
708 	int infra = 0, ap = 0, error = 0;
709 	struct dhd_pub *dhd = dhd_get_pub(dev);
710 	wl_wext_info_t *wext_info = NULL;
711 
712 	WL_TRACE(("%s: SIOCSIWMODE\n", dev->name));
713 	DHD_CHECK(dhd, dev);
714 	wext_info = dhd->wext_info;
715 	if (wext_info) {
716 		memset(&wext_info->conn_info.ssid, 0, sizeof(wlc_ssid_t));
717 		memset(&wext_info->conn_info.bssid, 0, sizeof(struct ether_addr));
718 		wext_info->conn_info.channel = 0;
719 	}
720 
721 	switch (wrqu->mode) {
722 	case IW_MODE_MASTER:
723 		infra = ap = 1;
724 		break;
725 	case IW_MODE_ADHOC:
726 	case IW_MODE_AUTO:
727 		break;
728 	case IW_MODE_INFRA:
729 		infra = 1;
730 		break;
731 	default:
732 		return -EINVAL;
733 	}
734 	infra = htod32(infra);
735 	ap = htod32(ap);
736 
737 	if ((error = dev_wlc_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra))) ||
738 	    (error = dev_wlc_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap))))
739 		return error;
740 
741 	/* -EINPROGRESS: Call commit handler */
742 	return -EINPROGRESS;
743 }
744 
745 static int
wl_iw_get_mode(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)746 wl_iw_get_mode(
747 	struct net_device *dev,
748 	struct iw_request_info *info,
749 	union iwreq_data *wrqu,
750 	char *extra
751 )
752 {
753 	int error, infra = 0, ap = 0;
754 
755 	WL_TRACE(("%s: SIOCGIWMODE\n", dev->name));
756 
757 	if ((error = dev_wlc_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra))) ||
758 	    (error = dev_wlc_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap))))
759 		return error;
760 
761 	infra = dtoh32(infra);
762 	ap = dtoh32(ap);
763 	wrqu->mode = infra ? ap ? IW_MODE_MASTER : IW_MODE_INFRA : IW_MODE_ADHOC;
764 
765 	return 0;
766 }
767 
768 static int
wl_iw_get_range(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)769 wl_iw_get_range(
770 	struct net_device *dev,
771 	struct iw_request_info *info,
772 	union iwreq_data *wrqu,
773 	char *extra
774 )
775 {
776 	struct iw_point *dwrq = (struct iw_point *)wrqu;
777 	struct iw_range *range = (struct iw_range *) extra;
778 	static int channels[MAXCHANNEL+1];
779 	wl_uint32_list_t *list = (wl_uint32_list_t *) channels;
780 	wl_rateset_t rateset;
781 	int error, i, k;
782 	uint sf, ch;
783 
784 	int phytype;
785 	int bw_cap = 0, sgi_tx = 0, nmode = 0;
786 	channel_info_t ci;
787 	uint8 nrate_list2copy = 0;
788 	uint16 nrate_list[4][8] = { {13, 26, 39, 52, 78, 104, 117, 130},
789 		{14, 29, 43, 58, 87, 116, 130, 144},
790 		{27, 54, 81, 108, 162, 216, 243, 270},
791 		{30, 60, 90, 120, 180, 240, 270, 300}};
792 	int fbt_cap = 0;
793 
794 	WL_TRACE(("%s: SIOCGIWRANGE\n", dev->name));
795 
796 	if (!extra)
797 		return -EINVAL;
798 
799 	dwrq->length = sizeof(struct iw_range);
800 	memset(range, 0, sizeof(*range));
801 
802 	/* We don't use nwids */
803 	range->min_nwid = range->max_nwid = 0;
804 
805 	/* Set available channels/frequencies */
806 	list->count = htod32(MAXCHANNEL);
807 	if ((error = dev_wlc_ioctl(dev, WLC_GET_VALID_CHANNELS, channels, sizeof(channels))))
808 		return error;
809 	for (i = 0; i < dtoh32(list->count) && i < IW_MAX_FREQUENCIES; i++) {
810 		range->freq[i].i = dtoh32(list->element[i]);
811 
812 		ch = dtoh32(list->element[i]);
813 		if (ch <= CH_MAX_2G_CHANNEL)
814 			sf = WF_CHAN_FACTOR_2_4_G;
815 		else
816 			sf = WF_CHAN_FACTOR_5_G;
817 
818 		range->freq[i].m = wf_channel2mhz(ch, sf);
819 		range->freq[i].e = 6;
820 	}
821 	range->num_frequency = range->num_channels = i;
822 
823 	/* Link quality (use NDIS cutoffs) */
824 	range->max_qual.qual = 5;
825 	/* Signal level (use RSSI) */
826 	range->max_qual.level = 0x100 - 200;	/* -200 dBm */
827 	/* Noise level (use noise) */
828 	range->max_qual.noise = 0x100 - 200;	/* -200 dBm */
829 	/* Signal level threshold range (?) */
830 	range->sensitivity = 65535;
831 
832 #if WIRELESS_EXT > 11
833 	/* Link quality (use NDIS cutoffs) */
834 	range->avg_qual.qual = 3;
835 	/* Signal level (use RSSI) */
836 	range->avg_qual.level = 0x100 + WL_IW_RSSI_GOOD;
837 	/* Noise level (use noise) */
838 	range->avg_qual.noise = 0x100 - 75;	/* -75 dBm */
839 #endif /* WIRELESS_EXT > 11 */
840 
841 	/* Set available bitrates */
842 	if ((error = dev_wlc_ioctl(dev, WLC_GET_CURR_RATESET, &rateset, sizeof(rateset))))
843 		return error;
844 	rateset.count = dtoh32(rateset.count);
845 	range->num_bitrates = rateset.count;
846 	for (i = 0; i < rateset.count && i < IW_MAX_BITRATES; i++)
847 		range->bitrate[i] = (rateset.rates[i] & 0x7f) * 500000; /* convert to bps */
848 	if ((error = dev_wlc_intvar_get(dev, "nmode", &nmode)))
849 		return error;
850 	if ((error = dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &phytype, sizeof(phytype))))
851 		return error;
852 	if (nmode == 1 && (((phytype == WLC_PHY_TYPE_LCN) ||
853 	                    (phytype == WLC_PHY_TYPE_LCN40)))) {
854 		if ((error = dev_wlc_intvar_get(dev, "mimo_bw_cap", &bw_cap)))
855 			return error;
856 		if ((error = dev_wlc_intvar_get(dev, "sgi_tx", &sgi_tx)))
857 			return error;
858 		if ((error = dev_wlc_ioctl(dev, WLC_GET_CHANNEL, &ci, sizeof(channel_info_t))))
859 			return error;
860 		ci.hw_channel = dtoh32(ci.hw_channel);
861 
862 		if (bw_cap == 0 ||
863 			(bw_cap == 2 && ci.hw_channel <= 14)) {
864 			if (sgi_tx == 0)
865 				nrate_list2copy = 0;
866 			else
867 				nrate_list2copy = 1;
868 		}
869 		if (bw_cap == 1 ||
870 			(bw_cap == 2 && ci.hw_channel >= 36)) {
871 			if (sgi_tx == 0)
872 				nrate_list2copy = 2;
873 			else
874 				nrate_list2copy = 3;
875 		}
876 		range->num_bitrates += 8;
877 		ASSERT(range->num_bitrates < IW_MAX_BITRATES);
878 		for (k = 0; i < range->num_bitrates; k++, i++) {
879 			/* convert to bps */
880 			range->bitrate[i] = (nrate_list[nrate_list2copy][k]) * 500000;
881 		}
882 	}
883 
884 	/* Set an indication of the max TCP throughput
885 	 * in bit/s that we can expect using this interface.
886 	 * May be use for QoS stuff... Jean II
887 	 */
888 	if ((error = dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &i, sizeof(i))))
889 		return error;
890 	i = dtoh32(i);
891 	if (i == WLC_PHY_TYPE_A)
892 		range->throughput = 24000000;	/* 24 Mbits/s */
893 	else
894 		range->throughput = 1500000;	/* 1.5 Mbits/s */
895 
896 	/* RTS and fragmentation thresholds */
897 	range->min_rts = 0;
898 	range->max_rts = 2347;
899 	range->min_frag = 256;
900 	range->max_frag = 2346;
901 
902 	range->max_encoding_tokens = DOT11_MAX_DEFAULT_KEYS;
903 	range->num_encoding_sizes = 4;
904 	range->encoding_size[0] = WEP1_KEY_SIZE;
905 	range->encoding_size[1] = WEP128_KEY_SIZE;
906 #if WIRELESS_EXT > 17
907 	range->encoding_size[2] = TKIP_KEY_SIZE;
908 #else
909 	range->encoding_size[2] = 0;
910 #endif
911 	range->encoding_size[3] = AES_KEY_SIZE;
912 
913 	/* Do not support power micro-management */
914 	range->min_pmp = 0;
915 	range->max_pmp = 0;
916 	range->min_pmt = 0;
917 	range->max_pmt = 0;
918 	range->pmp_flags = 0;
919 	range->pm_capa = 0;
920 
921 	/* Transmit Power - values are in mW */
922 	range->num_txpower = 2;
923 	range->txpower[0] = 1;
924 	range->txpower[1] = 255;
925 	range->txpower_capa = IW_TXPOW_MWATT;
926 
927 #if WIRELESS_EXT > 10
928 	range->we_version_compiled = WIRELESS_EXT;
929 	range->we_version_source = 19;
930 
931 	/* Only support retry limits */
932 	range->retry_capa = IW_RETRY_LIMIT;
933 	range->retry_flags = IW_RETRY_LIMIT;
934 	range->r_time_flags = 0;
935 	/* SRL and LRL limits */
936 	range->min_retry = 1;
937 	range->max_retry = 255;
938 	/* Retry lifetime limits unsupported */
939 	range->min_r_time = 0;
940 	range->max_r_time = 0;
941 #endif /* WIRELESS_EXT > 10 */
942 
943 #if WIRELESS_EXT > 17
944 	range->enc_capa = IW_ENC_CAPA_WPA;
945 	range->enc_capa |= IW_ENC_CAPA_CIPHER_TKIP;
946 	range->enc_capa |= IW_ENC_CAPA_CIPHER_CCMP;
947 	range->enc_capa |= IW_ENC_CAPA_WPA2;
948 
949 	/* Determine driver FBT capability. */
950 	if (dev_wlc_intvar_get(dev, "fbt_cap", &fbt_cap) == 0) {
951 		if (fbt_cap == WLC_FBT_CAP_DRV_4WAY_AND_REASSOC) {
952 			/* Tell the host (e.g. wpa_supplicant) to let driver do the handshake */
953 //			range->enc_capa |= IW_ENC_CAPA_4WAY_HANDSHAKE;
954 		}
955 	}
956 
957 #ifdef BCMFW_ROAM_ENABLE_WEXT
958 	/* Advertise firmware roam capability to the external supplicant */
959 	range->enc_capa |= IW_ENC_CAPA_FW_ROAM_ENABLE;
960 #endif /* BCMFW_ROAM_ENABLE_WEXT */
961 
962 	/* Event capability (kernel) */
963 	IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
964 	/* Event capability (driver) */
965 	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
966 	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
967 	IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
968 	IW_EVENT_CAPA_SET(range->event_capa, IWEVMICHAELMICFAILURE);
969 	IW_EVENT_CAPA_SET(range->event_capa, IWEVASSOCREQIE);
970 	IW_EVENT_CAPA_SET(range->event_capa, IWEVASSOCRESPIE);
971 	IW_EVENT_CAPA_SET(range->event_capa, IWEVPMKIDCAND);
972 
973 #if WIRELESS_EXT >= 22 && defined(IW_SCAN_CAPA_ESSID)
974 	/* FC7 wireless.h defines EXT 22 but doesn't define scan_capa bits */
975 	range->scan_capa = IW_SCAN_CAPA_ESSID;
976 #endif
977 #endif /* WIRELESS_EXT > 17 */
978 
979 	return 0;
980 }
981 
982 #ifndef WL_ESCAN
983 static int
rssi_to_qual(int rssi)984 rssi_to_qual(int rssi)
985 {
986 	if (rssi <= WL_IW_RSSI_NO_SIGNAL)
987 		return 0;
988 	else if (rssi <= WL_IW_RSSI_VERY_LOW)
989 		return 1;
990 	else if (rssi <= WL_IW_RSSI_LOW)
991 		return 2;
992 	else if (rssi <= WL_IW_RSSI_GOOD)
993 		return 3;
994 	else if (rssi <= WL_IW_RSSI_VERY_GOOD)
995 		return 4;
996 	else
997 		return 5;
998 }
999 #endif /* WL_ESCAN */
1000 
1001 static int
wl_iw_set_spy(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1002 wl_iw_set_spy(
1003 	struct net_device *dev,
1004 	struct iw_request_info *info,
1005 	union iwreq_data *wrqu,
1006 	char *extra
1007 )
1008 {
1009 	struct iw_point *dwrq = (struct iw_point *)wrqu;
1010 	wl_iw_t *iw = IW_DEV_IF(dev);
1011 	struct sockaddr *addr = (struct sockaddr *) extra;
1012 	int i;
1013 
1014 	WL_TRACE(("%s: SIOCSIWSPY\n", dev->name));
1015 
1016 	if (!extra)
1017 		return -EINVAL;
1018 
1019 	iw->spy_num = MIN(ARRAYSIZE(iw->spy_addr), dwrq->length);
1020 	for (i = 0; i < iw->spy_num; i++)
1021 		memcpy(&iw->spy_addr[i], addr[i].sa_data, ETHER_ADDR_LEN);
1022 	memset(iw->spy_qual, 0, sizeof(iw->spy_qual));
1023 
1024 	return 0;
1025 }
1026 
1027 static int
wl_iw_get_spy(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1028 wl_iw_get_spy(
1029 	struct net_device *dev,
1030 	struct iw_request_info *info,
1031 	union iwreq_data *wrqu,
1032 	char *extra
1033 )
1034 {
1035 	struct iw_point *dwrq = (struct iw_point *)wrqu;
1036 	wl_iw_t *iw = IW_DEV_IF(dev);
1037 	struct sockaddr *addr = (struct sockaddr *) extra;
1038 	struct iw_quality *qual = (struct iw_quality *) &addr[iw->spy_num];
1039 	int i;
1040 
1041 	WL_TRACE(("%s: SIOCGIWSPY\n", dev->name));
1042 
1043 	if (!extra)
1044 		return -EINVAL;
1045 
1046 	dwrq->length = iw->spy_num;
1047 	for (i = 0; i < iw->spy_num; i++) {
1048 		memcpy(addr[i].sa_data, &iw->spy_addr[i], ETHER_ADDR_LEN);
1049 		addr[i].sa_family = AF_UNIX;
1050 		memcpy(&qual[i], &iw->spy_qual[i], sizeof(struct iw_quality));
1051 		iw->spy_qual[i].updated = 0;
1052 	}
1053 
1054 	return 0;
1055 }
1056 
1057 static int
wl_iw_set_wap(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1058 wl_iw_set_wap(
1059 	struct net_device *dev,
1060 	struct iw_request_info *info,
1061 	union iwreq_data *wrqu,
1062 	char *extra
1063 )
1064 {
1065 	int error = -EINVAL;
1066 	struct dhd_pub *dhd = dhd_get_pub(dev);
1067 	wl_wext_info_t *wext_info = NULL;
1068 
1069 	WL_TRACE(("%s: SIOCSIWAP\n", dev->name));
1070 	DHD_CHECK(dhd, dev);
1071 	wext_info = dhd->wext_info;
1072 	if (wrqu->ap_addr.sa_family != ARPHRD_ETHER) {
1073 		WL_ERROR(("Invalid Header...sa_family\n"));
1074 		return -EINVAL;
1075 	}
1076 
1077 	/* Ignore "auto" or "off" */
1078 	if (ETHER_ISBCAST(wrqu->ap_addr.sa_data) || ETHER_ISNULLADDR(wrqu->ap_addr.sa_data)) {
1079 		scb_val_t scbval;
1080 		bzero(&scbval, sizeof(scb_val_t));
1081 		WL_MSG(dev->name, "WLC_DISASSOC\n");
1082 		if ((error = dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval, sizeof(scb_val_t)))) {
1083 			WL_ERROR(("WLC_DISASSOC failed (%d).\n", error));
1084 		}
1085 #ifdef WL_EXT_IAPSTA
1086 		wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
1087 			WL_EXT_STATUS_DISCONNECTING, NULL);
1088 #endif
1089 		return 0;
1090 	}
1091 	/* WL_ASSOC(("Assoc to %s\n", bcm_ether_ntoa((struct ether_addr *)&(wrqu->ap_addr.sa_data),
1092 	 * eabuf)));
1093 	 */
1094 	/* Reassociate to the specified AP */
1095 	if (wext_info)
1096 		memcpy(&wext_info->conn_info.bssid, wrqu->ap_addr.sa_data, ETHER_ADDR_LEN);
1097 	if (wext_info && wext_info->conn_info.ssid.SSID_len) {
1098 		if ((error = wl_ext_connect(dev, &wext_info->conn_info)))
1099 			return error;
1100 	} else {
1101 		if ((error = dev_wlc_ioctl(dev, WLC_REASSOC, wrqu->ap_addr.sa_data, ETHER_ADDR_LEN))) {
1102 			WL_ERROR(("WLC_REASSOC failed (%d).\n", error));
1103 			return error;
1104 		}
1105 		WL_MSG(dev->name, "join BSSID="MACSTR"\n", MAC2STR((u8 *)wrqu->ap_addr.sa_data));
1106 	}
1107 #ifdef WL_EXT_IAPSTA
1108 	wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY, WL_EXT_STATUS_CONNECTING, NULL);
1109 #endif
1110 
1111 	return 0;
1112 }
1113 
1114 static int
wl_iw_get_wap(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1115 wl_iw_get_wap(
1116 	struct net_device *dev,
1117 	struct iw_request_info *info,
1118 	union iwreq_data *wrqu,
1119 	char *extra
1120 )
1121 {
1122 	WL_TRACE(("%s: SIOCGIWAP\n", dev->name));
1123 
1124 	wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1125 	memset(wrqu->ap_addr.sa_data, 0, ETHER_ADDR_LEN);
1126 
1127 	/* Ignore error (may be down or disassociated) */
1128 	(void) dev_wlc_ioctl(dev, WLC_GET_BSSID, wrqu->ap_addr.sa_data, ETHER_ADDR_LEN);
1129 
1130 	return 0;
1131 }
1132 
1133 #if WIRELESS_EXT > 17
1134 static int
wl_iw_mlme(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1135 wl_iw_mlme(
1136 	struct net_device *dev,
1137 	struct iw_request_info *info,
1138 	union iwreq_data *wrqu,
1139 	char *extra
1140 )
1141 {
1142 	struct iw_mlme *mlme;
1143 	scb_val_t scbval;
1144 	int error  = -EINVAL;
1145 
1146 	WL_TRACE(("%s: SIOCSIWMLME\n", dev->name));
1147 
1148 	mlme = (struct iw_mlme *)extra;
1149 	if (mlme == NULL) {
1150 		WL_ERROR(("Invalid ioctl data.\n"));
1151 		return error;
1152 	}
1153 
1154 	scbval.val = mlme->reason_code;
1155 	bcopy(&mlme->addr.sa_data, &scbval.ea, ETHER_ADDR_LEN);
1156 
1157 	if (mlme->cmd == IW_MLME_DISASSOC) {
1158 		scbval.val = htod32(scbval.val);
1159 		WL_MSG(dev->name, "WLC_DISASSOC\n");
1160 		error = dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval, sizeof(scb_val_t));
1161 	}
1162 	else if (mlme->cmd == IW_MLME_DEAUTH) {
1163 		scbval.val = htod32(scbval.val);
1164 		WL_MSG(dev->name, "WLC_SCB_DEAUTHENTICATE_FOR_REASON\n");
1165 		error = dev_wlc_ioctl(dev, WLC_SCB_DEAUTHENTICATE_FOR_REASON, &scbval,
1166 			sizeof(scb_val_t));
1167 	}
1168 	else {
1169 		WL_ERROR(("Invalid ioctl data.\n"));
1170 		return error;
1171 	}
1172 #ifdef WL_EXT_IAPSTA
1173 	wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
1174 			WL_EXT_STATUS_DISCONNECTING, NULL);
1175 #endif
1176 
1177 	return error;
1178 }
1179 #endif /* WIRELESS_EXT > 17 */
1180 
1181 #ifndef WL_ESCAN
1182 static int
wl_iw_get_aplist(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1183 wl_iw_get_aplist(
1184 	struct net_device *dev,
1185 	struct iw_request_info *info,
1186 	union iwreq_data *wrqu,
1187 	char *extra
1188 )
1189 {
1190 	struct iw_point *dwrq = (struct iw_point *)wrqu;
1191 	wl_scan_results_v109_t *list;
1192 	struct sockaddr *addr = (struct sockaddr *) extra;
1193 	struct iw_quality qual[IW_MAX_AP];
1194 	wl_bss_info_v109_t *bi = NULL;
1195 	int error, i;
1196 	uint buflen = dwrq->length;
1197 	int16 rssi;
1198 
1199 	WL_TRACE(("%s: SIOCGIWAPLIST\n", dev->name));
1200 
1201 	if (!extra)
1202 		return -EINVAL;
1203 
1204 	/* Get scan results (too large to put on the stack) */
1205 	list = kmalloc(buflen, GFP_KERNEL);
1206 	if (!list)
1207 		return -ENOMEM;
1208 	memset(list, 0, buflen);
1209 	list->buflen = htod32(buflen);
1210 	if ((error = dev_wlc_ioctl(dev, WLC_SCAN_RESULTS, list, buflen))) {
1211 		WL_ERROR(("%d: Scan results error %d\n", __LINE__, error));
1212 		kfree(list);
1213 		return error;
1214 	}
1215 	list->buflen = dtoh32(list->buflen);
1216 	list->version = dtoh32(list->version);
1217 	list->count = dtoh32(list->count);
1218 	ASSERT(list->version == WL_BSS_INFO_VERSION);
1219 
1220 	for (i = 0, dwrq->length = 0; i < list->count && dwrq->length < IW_MAX_AP; i++) {
1221 		bi = bi ? (wl_bss_info_v109_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1222 		ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1223 			buflen));
1224 
1225 		/* Infrastructure only */
1226 		if (!(dtoh16(bi->capability) & DOT11_CAP_ESS))
1227 			continue;
1228 
1229 		/* BSSID */
1230 		memcpy(addr[dwrq->length].sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1231 		addr[dwrq->length].sa_family = ARPHRD_ETHER;
1232 		// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1233 		rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1234 		qual[dwrq->length].qual = rssi_to_qual(rssi);
1235 		qual[dwrq->length].level = 0x100 + rssi;
1236 		qual[dwrq->length].noise = 0x100 + bi->phy_noise;
1237 
1238 		/* Updated qual, level, and noise */
1239 #if WIRELESS_EXT > 18
1240 		qual[dwrq->length].updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1241 #else
1242 		qual[dwrq->length].updated = 7;
1243 #endif /* WIRELESS_EXT > 18 */
1244 
1245 		dwrq->length++;
1246 	}
1247 
1248 	kfree(list);
1249 
1250 	if (dwrq->length) {
1251 		memcpy(&addr[dwrq->length], qual, sizeof(struct iw_quality) * dwrq->length);
1252 		/* Provided qual */
1253 		dwrq->flags = 1;
1254 	}
1255 
1256 	return 0;
1257 }
1258 
1259 static int
wl_iw_iscan_get_aplist(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1260 wl_iw_iscan_get_aplist(
1261 	struct net_device *dev,
1262 	struct iw_request_info *info,
1263 	union iwreq_data *wrqu,
1264 	char *extra
1265 )
1266 {
1267 	struct iw_point *dwrq = (struct iw_point *)wrqu;
1268 	wl_scan_results_v109_t *list;
1269 	iscan_buf_t * buf;
1270 	iscan_info_t *iscan;
1271 
1272 	struct sockaddr *addr = (struct sockaddr *) extra;
1273 	struct iw_quality qual[IW_MAX_AP];
1274 	wl_bss_info_v109_t *bi = NULL;
1275 	int i;
1276 	int16 rssi;
1277 	struct dhd_pub *dhd = dhd_get_pub(dev);
1278 	wl_wext_info_t *wext_info = NULL;
1279 
1280 	WL_TRACE(("%s: SIOCGIWAPLIST\n", dev->name));
1281 	DHD_CHECK(dhd, dev);
1282 	wext_info = dhd->wext_info;
1283 	iscan = &wext_info->iscan;
1284 
1285 	if (!extra)
1286 		return -EINVAL;
1287 
1288 	if ((!iscan) || (iscan->sysioc_pid < 0)) {
1289 		return wl_iw_get_aplist(dev, info, dwrq, extra);
1290 	}
1291 
1292 	buf = iscan->list_hdr;
1293 	/* Get scan results (too large to put on the stack) */
1294 	while (buf) {
1295 	    list = &((wl_iscan_results_t*)buf->iscan_buf)->results;
1296 	    ASSERT(list->version == WL_BSS_INFO_VERSION);
1297 
1298 	    bi = NULL;
1299 	for (i = 0, dwrq->length = 0; i < list->count && dwrq->length < IW_MAX_AP; i++) {
1300 		bi = bi ? (wl_bss_info_v109_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1301 		ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1302 			WLC_IW_ISCAN_MAXLEN));
1303 
1304 		/* Infrastructure only */
1305 		if (!(dtoh16(bi->capability) & DOT11_CAP_ESS))
1306 			continue;
1307 
1308 		/* BSSID */
1309 		memcpy(addr[dwrq->length].sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1310 		addr[dwrq->length].sa_family = ARPHRD_ETHER;
1311 		// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1312 		rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1313 		qual[dwrq->length].qual = rssi_to_qual(rssi);
1314 		qual[dwrq->length].level = 0x100 + rssi;
1315 		qual[dwrq->length].noise = 0x100 + bi->phy_noise;
1316 
1317 		/* Updated qual, level, and noise */
1318 #if WIRELESS_EXT > 18
1319 		qual[dwrq->length].updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1320 #else
1321 		qual[dwrq->length].updated = 7;
1322 #endif /* WIRELESS_EXT > 18 */
1323 
1324 		dwrq->length++;
1325 	    }
1326 	    buf = buf->next;
1327 	}
1328 	if (dwrq->length) {
1329 		memcpy(&addr[dwrq->length], qual, sizeof(struct iw_quality) * dwrq->length);
1330 		/* Provided qual */
1331 		dwrq->flags = 1;
1332 	}
1333 
1334 	return 0;
1335 }
1336 #endif
1337 
1338 #if WIRELESS_EXT > 13
1339 #ifndef WL_ESCAN
1340 static int
wl_iw_set_scan(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1341 wl_iw_set_scan(
1342 	struct net_device *dev,
1343 	struct iw_request_info *info,
1344 	union iwreq_data *wrqu,
1345 	char *extra
1346 )
1347 {
1348 	wlc_ssid_t ssid;
1349 
1350 	WL_TRACE(("%s: SIOCSIWSCAN\n", dev->name));
1351 
1352 	/* default Broadcast scan */
1353 	memset(&ssid, 0, sizeof(ssid));
1354 
1355 #if WIRELESS_EXT > 17
1356 	/* check for given essid */
1357 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
1358 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1359 			struct iw_scan_req *req = (struct iw_scan_req *)extra;
1360 			ssid.SSID_len = MIN(sizeof(ssid.SSID), req->essid_len);
1361 			memcpy(ssid.SSID, req->essid, ssid.SSID_len);
1362 			ssid.SSID_len = htod32(ssid.SSID_len);
1363 		}
1364 	}
1365 #endif
1366 	/* Ignore error (most likely scan in progress) */
1367 	(void) dev_wlc_ioctl(dev, WLC_SCAN, &ssid, sizeof(ssid));
1368 
1369 	return 0;
1370 }
1371 #endif
1372 
1373 static int
wl_iw_iscan_set_scan(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1374 wl_iw_iscan_set_scan(
1375 	struct net_device *dev,
1376 	struct iw_request_info *info,
1377 	union iwreq_data *wrqu,
1378 	char *extra
1379 )
1380 {
1381 	struct dhd_pub *dhd = dhd_get_pub(dev);
1382 	wlc_ssid_t ssid;
1383 #ifdef WL_ESCAN
1384 	wl_scan_info_t *scan_info = NULL;
1385 	int err;
1386 #else
1387 	wl_wext_info_t *wext_info = NULL;
1388 	iscan_info_t *iscan;
1389 #ifdef WL_EXT_IAPSTA
1390 	int err;
1391 #endif
1392 #endif
1393 
1394 	DHD_CHECK(dhd, dev);
1395 #ifdef WL_ESCAN
1396 	/* default Broadcast scan */
1397 	memset(&ssid, 0, sizeof(ssid));
1398 #if WIRELESS_EXT > 17
1399 	/* check for given essid */
1400 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
1401 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1402 			struct iw_scan_req *req = (struct iw_scan_req *)extra;
1403 			ssid.SSID_len = MIN(sizeof(ssid.SSID), req->essid_len);
1404 			memcpy(ssid.SSID, req->essid, ssid.SSID_len);
1405 			ssid.SSID_len = htod32(ssid.SSID_len);
1406 		}
1407 	}
1408 #endif
1409 	scan_info = kmalloc(sizeof(wl_scan_info_t), GFP_KERNEL);
1410 	if (scan_info == NULL) {
1411 		WL_ERROR(("kzalloc failed\n"));
1412 		return -ENOMEM;
1413 	}
1414 	memset(scan_info, 0, sizeof(wl_scan_info_t));
1415 	scan_info->bcast_ssid = TRUE;
1416 	memcpy(scan_info->ssid.SSID, ssid.SSID, ssid.SSID_len);
1417 	scan_info->ssid.SSID_len = ssid.SSID_len;
1418 	err = wl_escan_set_scan(dev, scan_info);
1419 	kfree(scan_info);
1420 	return err;
1421 #else
1422 	wext_info = dhd->wext_info;
1423 	iscan = &wext_info->iscan;
1424 	WL_TRACE(("%s: SIOCSIWSCAN iscan=%p\n", dev->name, iscan));
1425 #ifdef WL_EXT_IAPSTA
1426 	err = wl_ext_in4way_sync_wext(dev, STA_NO_SCAN_IN4WAY, WL_EXT_STATUS_SCAN, NULL);
1427 	if (err)
1428 		return err;
1429 #endif
1430 
1431 	/* use backup if our thread is not successful */
1432 	if ((!iscan) || (iscan->sysioc_pid < 0)) {
1433 		return wl_iw_set_scan(dev, info, wrqu, extra);
1434 	}
1435 	if (iscan->iscan_state == ISCAN_STATE_SCANING) {
1436 		return 0;
1437 	}
1438 
1439 	/* default Broadcast scan */
1440 	memset(&ssid, 0, sizeof(ssid));
1441 
1442 #if WIRELESS_EXT > 17
1443 	/* check for given essid */
1444 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
1445 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1446 			struct iw_scan_req *req = (struct iw_scan_req *)extra;
1447 			ssid.SSID_len = MIN(sizeof(ssid.SSID), req->essid_len);
1448 			memcpy(ssid.SSID, req->essid, ssid.SSID_len);
1449 			ssid.SSID_len = htod32(ssid.SSID_len);
1450 		}
1451 	}
1452 #endif
1453 
1454 	iscan->list_cur = iscan->list_hdr;
1455 	iscan->iscan_state = ISCAN_STATE_SCANING;
1456 
1457 
1458 	wl_iw_set_event_mask(dev);
1459 	wl_iw_iscan(iscan, &ssid, WL_SCAN_ACTION_START);
1460 
1461 	iscan->timer.expires = jiffies + msecs_to_jiffies(iscan->timer_ms);
1462 	add_timer(&iscan->timer);
1463 	iscan->timer_on = 1;
1464 
1465 	return 0;
1466 #endif
1467 }
1468 
1469 #if WIRELESS_EXT > 17
1470 static bool
ie_is_wpa_ie(uint8 ** wpaie,uint8 ** tlvs,int * tlvs_len)1471 ie_is_wpa_ie(uint8 **wpaie, uint8 **tlvs, int *tlvs_len)
1472 {
1473 /* Is this body of this tlvs entry a WPA entry? If */
1474 /* not update the tlvs buffer pointer/length */
1475 	uint8 *ie = *wpaie;
1476 
1477 	/* If the contents match the WPA_OUI and type=1 */
1478 	if ((ie[1] >= 6) &&
1479 		!bcmp((const void *)&ie[2], (const void *)(WPA_OUI "\x01"), 4)) {
1480 		return TRUE;
1481 	}
1482 
1483 	/* point to the next ie */
1484 	ie += ie[1] + 2;
1485 	/* calculate the length of the rest of the buffer */
1486 	*tlvs_len -= (int)(ie - *tlvs);
1487 	/* update the pointer to the start of the buffer */
1488 	*tlvs = ie;
1489 	return FALSE;
1490 }
1491 
1492 static bool
ie_is_wps_ie(uint8 ** wpsie,uint8 ** tlvs,int * tlvs_len)1493 ie_is_wps_ie(uint8 **wpsie, uint8 **tlvs, int *tlvs_len)
1494 {
1495 /* Is this body of this tlvs entry a WPS entry? If */
1496 /* not update the tlvs buffer pointer/length */
1497 	uint8 *ie = *wpsie;
1498 
1499 	/* If the contents match the WPA_OUI and type=4 */
1500 	if ((ie[1] >= 4) &&
1501 		!bcmp((const void *)&ie[2], (const void *)(WPA_OUI "\x04"), 4)) {
1502 		return TRUE;
1503 	}
1504 
1505 	/* point to the next ie */
1506 	ie += ie[1] + 2;
1507 	/* calculate the length of the rest of the buffer */
1508 	*tlvs_len -= (int)(ie - *tlvs);
1509 	/* update the pointer to the start of the buffer */
1510 	*tlvs = ie;
1511 	return FALSE;
1512 }
1513 #endif /* WIRELESS_EXT > 17 */
1514 
1515 #ifdef BCMWAPI_WPI
_wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len,int uppercase)1516 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
1517 	size_t len, int uppercase)
1518 {
1519 	size_t i;
1520 	char *pos = buf, *end = buf + buf_size;
1521 	int ret;
1522 	if (buf_size == 0)
1523 		return 0;
1524 	for (i = 0; i < len; i++) {
1525 		ret = snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
1526 			data[i]);
1527 		if (ret < 0 || ret >= end - pos) {
1528 			end[-1] = '\0';
1529 			return pos - buf;
1530 		}
1531 		pos += ret;
1532 	}
1533 	end[-1] = '\0';
1534 	return pos - buf;
1535 }
1536 
1537 /**
1538  * wpa_snprintf_hex - Print data as a hex string into a buffer
1539  * @buf: Memory area to use as the output buffer
1540  * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
1541  * @data: Data to be printed
1542  * @len: Length of data in bytes
1543  * Returns: Number of bytes written
1544  */
1545 static int
wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len)1546 wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
1547 {
1548 	return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
1549 }
1550 #endif /* BCMWAPI_WPI */
1551 
1552 #ifndef WL_ESCAN
1553 static
1554 #endif
1555 int
wl_iw_handle_scanresults_ies(char ** event_p,char * end,struct iw_request_info * info,wl_bss_info_v109_t * bi)1556 wl_iw_handle_scanresults_ies(char **event_p, char *end,
1557 	struct iw_request_info *info, wl_bss_info_v109_t *bi)
1558 {
1559 #if WIRELESS_EXT > 17
1560 	struct iw_event	iwe;
1561 	char *event;
1562 #ifdef BCMWAPI_WPI
1563 	char *buf;
1564 	int custom_event_len;
1565 #endif
1566 
1567 	event = *event_p;
1568 	if (bi->ie_length) {
1569 		/* look for wpa/rsn ies in the ie list... */
1570 		bcm_tlv_t *ie;
1571 		uint8 *ptr = ((uint8 *)bi) + bi->ie_offset;
1572 		int ptr_len = bi->ie_length;
1573 
1574 		/* OSEN IE */
1575 		if ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_VS_ID)) &&
1576 			ie->len > WFA_OUI_LEN + 1 &&
1577 			!bcmp((const void *)&ie->data[0], (const void *)WFA_OUI, WFA_OUI_LEN) &&
1578 			ie->data[WFA_OUI_LEN] == WFA_OUI_TYPE_OSEN) {
1579 			iwe.cmd = IWEVGENIE;
1580 			iwe.u.data.length = ie->len + 2;
1581 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1582 		}
1583 		ptr = ((uint8 *)bi) + bi->ie_offset;
1584 
1585 		if ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_RSN_ID))) {
1586 			iwe.cmd = IWEVGENIE;
1587 			iwe.u.data.length = ie->len + 2;
1588 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1589 		}
1590 		ptr = ((uint8 *)bi) + bi->ie_offset;
1591 
1592 		if ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_MDIE_ID))) {
1593 			iwe.cmd = IWEVGENIE;
1594 			iwe.u.data.length = ie->len + 2;
1595 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1596 		}
1597 		ptr = ((uint8 *)bi) + bi->ie_offset;
1598 
1599 		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WPA_ID))) {
1600 			/* look for WPS IE */
1601 			if (ie_is_wps_ie(((uint8 **)&ie), &ptr, &ptr_len)) {
1602 				iwe.cmd = IWEVGENIE;
1603 				iwe.u.data.length = ie->len + 2;
1604 				event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1605 				break;
1606 			}
1607 		}
1608 
1609 		ptr = ((uint8 *)bi) + bi->ie_offset;
1610 		ptr_len = bi->ie_length;
1611 		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WPA_ID))) {
1612 			if (ie_is_wpa_ie(((uint8 **)&ie), &ptr, &ptr_len)) {
1613 				iwe.cmd = IWEVGENIE;
1614 				iwe.u.data.length = ie->len + 2;
1615 				event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1616 				break;
1617 			}
1618 		}
1619 
1620 #ifdef BCMWAPI_WPI
1621 		ptr = ((uint8 *)bi) + sizeof(wl_bss_info_v109_t);
1622 		ptr_len = bi->ie_length;
1623 
1624 		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WAPI_ID))) {
1625 			WL_TRACE(("found a WAPI IE...\n"));
1626 #ifdef WAPI_IE_USE_GENIE
1627 			iwe.cmd = IWEVGENIE;
1628 			iwe.u.data.length = ie->len + 2;
1629 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1630 #else /* using CUSTOM event */
1631 			iwe.cmd = IWEVCUSTOM;
1632 			custom_event_len = strlen("wapi_ie=") + 2*(ie->len + 2);
1633 			iwe.u.data.length = custom_event_len;
1634 
1635 			buf = kmalloc(custom_event_len+1, GFP_KERNEL);
1636 			if (buf == NULL)
1637 			{
1638 				WL_ERROR(("malloc(%d) returned NULL...\n", custom_event_len));
1639 				break;
1640 			}
1641 
1642 			memcpy(buf, "wapi_ie=", 8);
1643 			wpa_snprintf_hex(buf + 8, 2+1, &(ie->id), 1);
1644 			wpa_snprintf_hex(buf + 10, 2+1, &(ie->len), 1);
1645 			wpa_snprintf_hex(buf + 12, 2*ie->len+1, ie->data, ie->len);
1646 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, buf);
1647 			kfree(buf);
1648 #endif /* WAPI_IE_USE_GENIE */
1649 			break;
1650 		}
1651 #endif /* BCMWAPI_WPI */
1652 		*event_p = event;
1653 	}
1654 
1655 #endif /* WIRELESS_EXT > 17 */
1656 	return 0;
1657 }
1658 
1659 #ifndef WL_ESCAN
1660 static int
wl_iw_get_scan(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1661 wl_iw_get_scan(
1662 	struct net_device *dev,
1663 	struct iw_request_info *info,
1664 	union iwreq_data *wrqu,
1665 	char *extra
1666 )
1667 {
1668 	struct iw_point *dwrq = (struct iw_point *)wrqu;
1669 	channel_info_t ci;
1670 	wl_scan_results_v109_t *list;
1671 	struct iw_event	iwe;
1672 	wl_bss_info_v109_t *bi = NULL;
1673 	int error, i, j;
1674 	char *event = extra, *end = extra + dwrq->length, *value;
1675 	uint buflen = dwrq->length;
1676 	int16 rssi;
1677 	int channel;
1678 
1679 	WL_TRACE(("%s SIOCGIWSCAN\n", dev->name));
1680 
1681 	if (!extra)
1682 		return -EINVAL;
1683 
1684 	/* Check for scan in progress */
1685 	if ((error = dev_wlc_ioctl(dev, WLC_GET_CHANNEL, &ci, sizeof(ci))))
1686 		return error;
1687 	ci.scan_channel = dtoh32(ci.scan_channel);
1688 	if (ci.scan_channel)
1689 		return -EAGAIN;
1690 
1691 	/* Get scan results (too large to put on the stack) */
1692 	list = kmalloc(buflen, GFP_KERNEL);
1693 	if (!list)
1694 		return -ENOMEM;
1695 	memset(list, 0, buflen);
1696 	list->buflen = htod32(buflen);
1697 	if ((error = dev_wlc_ioctl(dev, WLC_SCAN_RESULTS, list, buflen))) {
1698 		kfree(list);
1699 		return error;
1700 	}
1701 	list->buflen = dtoh32(list->buflen);
1702 	list->version = dtoh32(list->version);
1703 	list->count = dtoh32(list->count);
1704 
1705 	ASSERT(list->version == WL_BSS_INFO_VERSION);
1706 
1707 	for (i = 0; i < list->count && i < IW_MAX_AP; i++) {
1708 		bi = bi ? (wl_bss_info_v109_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1709 		ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1710 			buflen));
1711 
1712 		// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1713 		rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1714 		channel = (bi->ctl_ch == 0) ? CHSPEC_CHANNEL(bi->chanspec) : bi->ctl_ch;
1715 		WL_SCAN(("BSSID="MACSTR", channel=%d, RSSI=%d, SSID=\"%s\"\n",
1716 			MAC2STR(bi->BSSID.octet), channel, rssi, bi->SSID));
1717 
1718 		/* First entry must be the BSSID */
1719 		iwe.cmd = SIOCGIWAP;
1720 		iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1721 		memcpy(iwe.u.ap_addr.sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1722 		event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_ADDR_LEN);
1723 
1724 		/* SSID */
1725 		iwe.u.data.length = dtoh32(bi->SSID_len);
1726 		iwe.cmd = SIOCGIWESSID;
1727 		iwe.u.data.flags = 1;
1728 		event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, bi->SSID);
1729 
1730 		/* Mode */
1731 		if (dtoh16(bi->capability) & (DOT11_CAP_ESS | DOT11_CAP_IBSS)) {
1732 			iwe.cmd = SIOCGIWMODE;
1733 			if (dtoh16(bi->capability) & DOT11_CAP_ESS)
1734 				iwe.u.mode = IW_MODE_INFRA;
1735 			else
1736 				iwe.u.mode = IW_MODE_ADHOC;
1737 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_UINT_LEN);
1738 		}
1739 
1740 		/* Channel */
1741 		iwe.cmd = SIOCGIWFREQ;
1742 
1743 		iwe.u.freq.m = wf_channel2mhz(CHSPEC_CHANNEL(bi->chanspec),
1744 			(CHSPEC_IS2G(bi->chanspec)) ?
1745 			WF_CHAN_FACTOR_2_4_G : WF_CHAN_FACTOR_5_G);
1746 		iwe.u.freq.e = 6;
1747 		event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_FREQ_LEN);
1748 
1749 		/* Channel quality */
1750 		iwe.cmd = IWEVQUAL;
1751 		iwe.u.qual.qual = rssi_to_qual(rssi);
1752 		iwe.u.qual.level = 0x100 + rssi;
1753 		iwe.u.qual.noise = 0x100 + bi->phy_noise;
1754 		event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_QUAL_LEN);
1755 
1756 		 wl_iw_handle_scanresults_ies(&event, end, info, bi);
1757 
1758 		/* Encryption */
1759 		iwe.cmd = SIOCGIWENCODE;
1760 		if (dtoh16(bi->capability) & DOT11_CAP_PRIVACY)
1761 			iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1762 		else
1763 			iwe.u.data.flags = IW_ENCODE_DISABLED;
1764 		iwe.u.data.length = 0;
1765 		event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)event);
1766 
1767 		/* Rates */
1768 		if (bi->rateset.count) {
1769 			value = event + IW_EV_LCP_LEN;
1770 			iwe.cmd = SIOCGIWRATE;
1771 			/* Those two flags are ignored... */
1772 			iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1773 			for (j = 0; j < bi->rateset.count && j < IW_MAX_BITRATES; j++) {
1774 				iwe.u.bitrate.value = (bi->rateset.rates[j] & 0x7f) * 500000;
1775 				value = IWE_STREAM_ADD_VALUE(info, event, value, end, &iwe,
1776 					IW_EV_PARAM_LEN);
1777 			}
1778 			event = value;
1779 		}
1780 	}
1781 
1782 	kfree(list);
1783 
1784 	dwrq->length = event - extra;
1785 	dwrq->flags = 0;	/* todo */
1786 
1787 	return 0;
1788 }
1789 #endif /* WL_ESCAN */
1790 
1791 static int
wl_iw_iscan_get_scan(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1792 wl_iw_iscan_get_scan(
1793 	struct net_device *dev,
1794 	struct iw_request_info *info,
1795 	union iwreq_data *wrqu,
1796 	char *extra
1797 )
1798 {
1799 	struct iw_point *dwrq = (struct iw_point *)wrqu;
1800 	struct dhd_pub *dhd = dhd_get_pub(dev);
1801 #ifndef WL_ESCAN
1802 	wl_wext_info_t *wext_info = NULL;
1803 	wl_scan_results_v109_t *list;
1804 	struct iw_event	iwe;
1805 	wl_bss_info_v109_t *bi = NULL;
1806 	int ii, j;
1807 	int apcnt;
1808 	char *event = extra, *end = extra + dwrq->length, *value;
1809 	iscan_buf_t * p_buf;
1810 	int16 rssi;
1811 	int channel;
1812 	iscan_info_t *iscan;
1813 #endif
1814 
1815 	DHD_CHECK(dhd, dev);
1816 #ifdef WL_ESCAN
1817 	return wl_escan_get_scan(dev, info, dwrq, extra);
1818 #else
1819 	WL_TRACE(("%s SIOCGIWSCAN\n", dev->name));
1820 
1821 	if (!extra)
1822 		return -EINVAL;
1823 
1824 	wext_info = dhd->wext_info;
1825 	/* use backup if our thread is not successful */
1826 	iscan = &wext_info->iscan;
1827 	if ((!iscan) || (iscan->sysioc_pid < 0)) {
1828 		return wl_iw_get_scan(dev, info, dwrq, extra);
1829 	}
1830 
1831 	/* Check for scan in progress */
1832 	if (iscan->iscan_state == ISCAN_STATE_SCANING) {
1833 		WL_TRACE(("%s: SIOCGIWSCAN GET still scanning\n", dev->name));
1834 		return -EAGAIN;
1835 	}
1836 
1837 	apcnt = 0;
1838 	p_buf = iscan->list_hdr;
1839 	/* Get scan results */
1840 	while (p_buf != iscan->list_cur) {
1841 		list = &((wl_iscan_results_t*)p_buf->iscan_buf)->results;
1842 
1843 		if (list->version != WL_BSS_INFO_VERSION) {
1844 			WL_ERROR(("list->version %d != WL_BSS_INFO_VERSION\n", list->version));
1845 		}
1846 
1847 		bi = NULL;
1848 		for (ii = 0; ii < list->count && apcnt < IW_MAX_AP; apcnt++, ii++) {
1849 			bi = bi ? (wl_bss_info_v109_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1850 			ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1851 				WLC_IW_ISCAN_MAXLEN));
1852 
1853 			/* overflow check cover fields before wpa IEs */
1854 			if (event + ETHER_ADDR_LEN + bi->SSID_len + IW_EV_UINT_LEN + IW_EV_FREQ_LEN +
1855 				IW_EV_QUAL_LEN >= end)
1856 				return -E2BIG;
1857 
1858 			// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1859 			rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1860 			channel = (bi->ctl_ch == 0) ? CHSPEC_CHANNEL(bi->chanspec) : bi->ctl_ch;
1861 			WL_SCAN(("BSSID="MACSTR", channel=%d, RSSI=%d, SSID=\"%s\"\n",
1862 				MAC2STR(bi->BSSID.octet), channel, rssi, bi->SSID));
1863 
1864 			/* First entry must be the BSSID */
1865 			iwe.cmd = SIOCGIWAP;
1866 			iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1867 			memcpy(iwe.u.ap_addr.sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1868 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_ADDR_LEN);
1869 
1870 			/* SSID */
1871 			iwe.u.data.length = dtoh32(bi->SSID_len);
1872 			iwe.cmd = SIOCGIWESSID;
1873 			iwe.u.data.flags = 1;
1874 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, bi->SSID);
1875 
1876 			/* Mode */
1877 			if (dtoh16(bi->capability) & (DOT11_CAP_ESS | DOT11_CAP_IBSS)) {
1878 				iwe.cmd = SIOCGIWMODE;
1879 				if (dtoh16(bi->capability) & DOT11_CAP_ESS)
1880 					iwe.u.mode = IW_MODE_INFRA;
1881 				else
1882 					iwe.u.mode = IW_MODE_ADHOC;
1883 				event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_UINT_LEN);
1884 			}
1885 
1886 			/* Channel */
1887 			iwe.cmd = SIOCGIWFREQ;
1888 			iwe.u.freq.m = wf_channel2mhz(CHSPEC_CHANNEL(bi->chanspec),
1889 				(CHSPEC_IS2G(bi->chanspec)) ?
1890 				WF_CHAN_FACTOR_2_4_G : WF_CHAN_FACTOR_5_G);
1891 			iwe.u.freq.e = 6;
1892 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_FREQ_LEN);
1893 
1894 			/* Channel quality */
1895 			iwe.cmd = IWEVQUAL;
1896 			iwe.u.qual.qual = rssi_to_qual(rssi);
1897 			iwe.u.qual.level = 0x100 + rssi;
1898 			iwe.u.qual.noise = 0x100 + bi->phy_noise;
1899 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_QUAL_LEN);
1900 
1901 			wl_iw_handle_scanresults_ies(&event, end, info, bi);
1902 
1903 			/* Encryption */
1904 			iwe.cmd = SIOCGIWENCODE;
1905 			if (dtoh16(bi->capability) & DOT11_CAP_PRIVACY)
1906 				iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1907 			else
1908 				iwe.u.data.flags = IW_ENCODE_DISABLED;
1909 			iwe.u.data.length = 0;
1910 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)event);
1911 
1912 			/* Rates */
1913 			if (bi->rateset.count <= sizeof(bi->rateset.rates)) {
1914 				if (event + IW_MAX_BITRATES*IW_EV_PARAM_LEN >= end)
1915 					return -E2BIG;
1916 
1917 				value = event + IW_EV_LCP_LEN;
1918 				iwe.cmd = SIOCGIWRATE;
1919 				/* Those two flags are ignored... */
1920 				iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1921 				for (j = 0; j < bi->rateset.count && j < IW_MAX_BITRATES; j++) {
1922 					iwe.u.bitrate.value = (bi->rateset.rates[j] & 0x7f) * 500000;
1923 					value = IWE_STREAM_ADD_VALUE(info, event, value, end, &iwe,
1924 						IW_EV_PARAM_LEN);
1925 				}
1926 				event = value;
1927 			}
1928 		}
1929 		p_buf = p_buf->next;
1930 	} /* while (p_buf) */
1931 
1932 	dwrq->length = event - extra;
1933 	dwrq->flags = 0;	/* todo */
1934 	WL_SCAN(("apcnt=%d\n", apcnt));
1935 
1936 	return 0;
1937 #endif
1938 }
1939 #endif /* WIRELESS_EXT > 13 */
1940 
1941 
1942 static int
wl_iw_set_essid(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1943 wl_iw_set_essid(
1944 	struct net_device *dev,
1945 	struct iw_request_info *info,
1946 	union iwreq_data *wrqu,
1947 	char *extra
1948 )
1949 {
1950 	struct iw_point *dwrq = (struct iw_point *)wrqu;
1951 	wlc_ssid_t ssid;
1952 	int error;
1953 	struct dhd_pub *dhd = dhd_get_pub(dev);
1954 	wl_wext_info_t *wext_info = NULL;
1955 
1956 	WL_TRACE(("%s: SIOCSIWESSID\n", dev->name));
1957 	DHD_CHECK(dhd, dev);
1958 	wext_info = dhd->wext_info;
1959 
1960 	/* default Broadcast SSID */
1961 	memset(&ssid, 0, sizeof(ssid));
1962 	if (dwrq->length && extra) {
1963 #if WIRELESS_EXT > 20
1964 		ssid.SSID_len = MIN(sizeof(ssid.SSID), dwrq->length);
1965 #else
1966 		ssid.SSID_len = MIN(sizeof(ssid.SSID), dwrq->length-1);
1967 #endif
1968 		memcpy(ssid.SSID, extra, ssid.SSID_len);
1969 		ssid.SSID_len = htod32(ssid.SSID_len);
1970 
1971 		if (wext_info) {
1972 			memcpy(wext_info->conn_info.ssid.SSID, ssid.SSID, ssid.SSID_len);
1973 			wext_info->conn_info.ssid.SSID_len = ssid.SSID_len;
1974 		}
1975 		if (wext_info && memcmp(&ether_null, &wext_info->conn_info.bssid, ETHER_ADDR_LEN)) {
1976 			if ((error = wl_ext_connect(dev, &wext_info->conn_info)))
1977 				return error;
1978 		} else {
1979 			if ((error = dev_wlc_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)))) {
1980 				WL_ERROR(("WLC_SET_SSID failed (%d).\n", error));
1981 				return error;
1982 			}
1983 			WL_MSG(dev->name, "join SSID=\"%s\"\n", ssid.SSID);
1984 		}
1985 #ifdef WL_EXT_IAPSTA
1986 		wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY, WL_EXT_STATUS_CONNECTING, NULL);
1987 #endif
1988 	}
1989 	/* If essid null then it is "iwconfig <interface> essid off" command */
1990 	else {
1991 		scb_val_t scbval;
1992 		bzero(&scbval, sizeof(scb_val_t));
1993 		WL_MSG(dev->name, "WLC_DISASSOC\n");
1994 		if ((error = dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval, sizeof(scb_val_t)))) {
1995 			WL_ERROR(("WLC_DISASSOC failed (%d).\n", error));
1996 			return error;
1997 		}
1998 #ifdef WL_EXT_IAPSTA
1999 		wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
2000 			WL_EXT_STATUS_DISCONNECTING, NULL);
2001 #endif
2002 	}
2003 	return 0;
2004 }
2005 
2006 static int
wl_iw_get_essid(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2007 wl_iw_get_essid(
2008 	struct net_device *dev,
2009 	struct iw_request_info *info,
2010 	union iwreq_data *wrqu,
2011 	char *extra
2012 )
2013 {
2014 	struct iw_point *dwrq = (struct iw_point *)wrqu;
2015 	wlc_ssid_t ssid;
2016 	int error;
2017 
2018 	WL_TRACE(("%s: SIOCGIWESSID\n", dev->name));
2019 
2020 	if (!extra)
2021 		return -EINVAL;
2022 
2023 	if ((error = dev_wlc_ioctl(dev, WLC_GET_SSID, &ssid, sizeof(ssid)))) {
2024 		WL_ERROR(("Error getting the SSID %d\n", error));
2025 		return error;
2026 	}
2027 
2028 	ssid.SSID_len = dtoh32(ssid.SSID_len);
2029 
2030 	/* Max SSID length check */
2031 	if (ssid.SSID_len > IW_ESSID_MAX_SIZE) {
2032 		ssid.SSID_len = IW_ESSID_MAX_SIZE;
2033 		/* Get the current SSID */
2034 		memcpy(extra, ssid.SSID, ssid.SSID_len);
2035 		/* NULL terminating as length of extra buffer is IW_ESSID_MAX_SIZE ie 32 */
2036 		extra[IW_ESSID_MAX_SIZE-1] = '\0';
2037 	} else {
2038 		/* Get the current SSID */
2039 		memcpy(extra, ssid.SSID, ssid.SSID_len);
2040 	}
2041 
2042 	dwrq->length = ssid.SSID_len;
2043 
2044 	dwrq->flags = 1; /* active */
2045 
2046 	return 0;
2047 }
2048 
2049 static int
wl_iw_set_nick(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2050 wl_iw_set_nick(
2051 	struct net_device *dev,
2052 	struct iw_request_info *info,
2053 	union iwreq_data *wrqu,
2054 	char *extra
2055 )
2056 {
2057 	struct iw_point *dwrq = (struct iw_point *)wrqu;
2058 	wl_iw_t *iw = IW_DEV_IF(dev);
2059 	WL_TRACE(("%s: SIOCSIWNICKN\n", dev->name));
2060 
2061 	if (!extra)
2062 		return -EINVAL;
2063 
2064 	/* Check the size of the string */
2065 	if (dwrq->length > sizeof(iw->nickname))
2066 		return -E2BIG;
2067 
2068 	memcpy(iw->nickname, extra, dwrq->length);
2069 	iw->nickname[dwrq->length - 1] = '\0';
2070 
2071 	return 0;
2072 }
2073 
2074 static int
wl_iw_get_nick(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2075 wl_iw_get_nick(
2076 	struct net_device *dev,
2077 	struct iw_request_info *info,
2078 	union iwreq_data *wrqu,
2079 	char *extra
2080 )
2081 {
2082 	struct iw_point *dwrq = (struct iw_point *)wrqu;
2083 	wl_iw_t *iw = IW_DEV_IF(dev);
2084 	WL_TRACE(("%s: SIOCGIWNICKN\n", dev->name));
2085 
2086 	if (!extra)
2087 		return -EINVAL;
2088 
2089 	strcpy(extra, iw->nickname);
2090 	dwrq->length = strlen(extra) + 1;
2091 
2092 	return 0;
2093 }
2094 
wl_iw_set_rate(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2095 static int wl_iw_set_rate(
2096 	struct net_device *dev,
2097 	struct iw_request_info *info,
2098 	union iwreq_data *wrqu,
2099 	char *extra
2100 )
2101 {
2102 	struct iw_param	*vwrq = &wrqu->bitrate;
2103 	wl_rateset_t rateset;
2104 	int error, rate, i, error_bg, error_a;
2105 
2106 	WL_TRACE(("%s: SIOCSIWRATE\n", dev->name));
2107 
2108 	/* Get current rateset */
2109 	if ((error = dev_wlc_ioctl(dev, WLC_GET_CURR_RATESET, &rateset, sizeof(rateset))))
2110 		return error;
2111 
2112 	rateset.count = dtoh32(rateset.count);
2113 
2114 	if (vwrq->value < 0) {
2115 		/* Select maximum rate */
2116 		rate = rateset.rates[rateset.count - 1] & 0x7f;
2117 	} else if (vwrq->value < rateset.count) {
2118 		/* Select rate by rateset index */
2119 		rate = rateset.rates[vwrq->value] & 0x7f;
2120 	} else {
2121 		/* Specified rate in bps */
2122 		rate = vwrq->value / 500000;
2123 	}
2124 
2125 	if (vwrq->fixed) {
2126 		/*
2127 			Set rate override,
2128 			Since the is a/b/g-blind, both a/bg_rate are enforced.
2129 		*/
2130 		error_bg = dev_wlc_intvar_set(dev, "bg_rate", rate);
2131 		error_a = dev_wlc_intvar_set(dev, "a_rate", rate);
2132 
2133 		if (error_bg && error_a)
2134 			return (error_bg | error_a);
2135 	} else {
2136 		/*
2137 			clear rate override
2138 			Since the is a/b/g-blind, both a/bg_rate are enforced.
2139 		*/
2140 		/* 0 is for clearing rate override */
2141 		error_bg = dev_wlc_intvar_set(dev, "bg_rate", 0);
2142 		/* 0 is for clearing rate override */
2143 		error_a = dev_wlc_intvar_set(dev, "a_rate", 0);
2144 
2145 		if (error_bg && error_a)
2146 			return (error_bg | error_a);
2147 
2148 		/* Remove rates above selected rate */
2149 		for (i = 0; i < rateset.count; i++)
2150 			if ((rateset.rates[i] & 0x7f) > rate)
2151 				break;
2152 		rateset.count = htod32(i);
2153 
2154 		/* Set current rateset */
2155 		if ((error = dev_wlc_ioctl(dev, WLC_SET_RATESET, &rateset, sizeof(rateset))))
2156 			return error;
2157 	}
2158 
2159 	return 0;
2160 }
2161 
wl_iw_get_rate(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2162 static int wl_iw_get_rate(
2163 	struct net_device *dev,
2164 	struct iw_request_info *info,
2165 	union iwreq_data *wrqu,
2166 	char *extra
2167 )
2168 {
2169 	struct iw_param	*vwrq = &wrqu->bitrate;
2170 	int error, rate;
2171 
2172 	WL_TRACE(("%s: SIOCGIWRATE\n", dev->name));
2173 
2174 	/* Report the current tx rate */
2175 	if ((error = dev_wlc_ioctl(dev, WLC_GET_RATE, &rate, sizeof(rate))))
2176 		return error;
2177 	rate = dtoh32(rate);
2178 	vwrq->value = rate * 500000;
2179 
2180 	return 0;
2181 }
2182 
2183 static int
wl_iw_set_rts(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2184 wl_iw_set_rts(
2185 	struct net_device *dev,
2186 	struct iw_request_info *info,
2187 	union iwreq_data *wrqu,
2188 	char *extra
2189 )
2190 {
2191 	struct iw_param	*vwrq = &wrqu->rts;
2192 	int error, rts;
2193 
2194 	WL_TRACE(("%s: SIOCSIWRTS\n", dev->name));
2195 
2196 	if (vwrq->disabled)
2197 		rts = DOT11_DEFAULT_RTS_LEN;
2198 	else if (vwrq->value < 0 || vwrq->value > DOT11_DEFAULT_RTS_LEN)
2199 		return -EINVAL;
2200 	else
2201 		rts = vwrq->value;
2202 
2203 	if ((error = dev_wlc_intvar_set(dev, "rtsthresh", rts)))
2204 		return error;
2205 
2206 	return 0;
2207 }
2208 
2209 static int
wl_iw_get_rts(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2210 wl_iw_get_rts(
2211 	struct net_device *dev,
2212 	struct iw_request_info *info,
2213 	union iwreq_data *wrqu,
2214 	char *extra
2215 )
2216 {
2217 	struct iw_param	*vwrq = &wrqu->rts;
2218 	int error, rts;
2219 
2220 	WL_TRACE(("%s: SIOCGIWRTS\n", dev->name));
2221 
2222 	if ((error = dev_wlc_intvar_get(dev, "rtsthresh", &rts)))
2223 		return error;
2224 
2225 	vwrq->value = rts;
2226 	vwrq->disabled = (rts >= DOT11_DEFAULT_RTS_LEN);
2227 	vwrq->fixed = 1;
2228 
2229 	return 0;
2230 }
2231 
2232 static int
wl_iw_set_frag(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2233 wl_iw_set_frag(
2234 	struct net_device *dev,
2235 	struct iw_request_info *info,
2236 	union iwreq_data *wrqu,
2237 	char *extra
2238 )
2239 {
2240 	struct iw_param	*vwrq = &wrqu->frag;
2241 	int error, frag;
2242 
2243 	WL_TRACE(("%s: SIOCSIWFRAG\n", dev->name));
2244 
2245 	if (vwrq->disabled)
2246 		frag = DOT11_DEFAULT_FRAG_LEN;
2247 	else if (vwrq->value < 0 || vwrq->value > DOT11_DEFAULT_FRAG_LEN)
2248 		return -EINVAL;
2249 	else
2250 		frag = vwrq->value;
2251 
2252 	if ((error = dev_wlc_intvar_set(dev, "fragthresh", frag)))
2253 		return error;
2254 
2255 	return 0;
2256 }
2257 
2258 static int
wl_iw_get_frag(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2259 wl_iw_get_frag(
2260 	struct net_device *dev,
2261 	struct iw_request_info *info,
2262 	union iwreq_data *wrqu,
2263 	char *extra
2264 )
2265 {
2266 	struct iw_param	*vwrq = &wrqu->frag;
2267 	int error, fragthreshold;
2268 
2269 	WL_TRACE(("%s: SIOCGIWFRAG\n", dev->name));
2270 
2271 	if ((error = dev_wlc_intvar_get(dev, "fragthresh", &fragthreshold)))
2272 		return error;
2273 
2274 	vwrq->value = fragthreshold;
2275 	vwrq->disabled = (fragthreshold >= DOT11_DEFAULT_FRAG_LEN);
2276 	vwrq->fixed = 1;
2277 
2278 	return 0;
2279 }
2280 
2281 static int
wl_iw_set_txpow(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2282 wl_iw_set_txpow(
2283 	struct net_device *dev,
2284 	struct iw_request_info *info,
2285 	union iwreq_data *wrqu,
2286 	char *extra
2287 )
2288 {
2289 	struct iw_param	*vwrq = &wrqu->txpower;
2290 	int error, disable;
2291 	uint16 txpwrmw;
2292 	WL_TRACE(("%s: SIOCSIWTXPOW\n", dev->name));
2293 
2294 	/* Make sure radio is off or on as far as software is concerned */
2295 	disable = vwrq->disabled ? WL_RADIO_SW_DISABLE : 0;
2296 	disable += WL_RADIO_SW_DISABLE << 16;
2297 
2298 	disable = htod32(disable);
2299 	if ((error = dev_wlc_ioctl(dev, WLC_SET_RADIO, &disable, sizeof(disable))))
2300 		return error;
2301 
2302 	/* If Radio is off, nothing more to do */
2303 	if (disable & WL_RADIO_SW_DISABLE)
2304 		return 0;
2305 
2306 	/* Only handle mW */
2307 	if (!(vwrq->flags & IW_TXPOW_MWATT))
2308 		return -EINVAL;
2309 
2310 	/* Value < 0 means just "on" or "off" */
2311 	if (vwrq->value < 0)
2312 		return 0;
2313 
2314 	if (vwrq->value > 0xffff) txpwrmw = 0xffff;
2315 	else txpwrmw = (uint16)vwrq->value;
2316 
2317 
2318 	error = dev_wlc_intvar_set(dev, "qtxpower", (int)(bcm_mw_to_qdbm(txpwrmw)));
2319 	return error;
2320 }
2321 
2322 static int
wl_iw_get_txpow(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2323 wl_iw_get_txpow(
2324 	struct net_device *dev,
2325 	struct iw_request_info *info,
2326 	union iwreq_data *wrqu,
2327 	char *extra
2328 )
2329 {
2330 	struct iw_param	*vwrq = &wrqu->txpower;
2331 	int error, disable, txpwrdbm;
2332 	uint8 result;
2333 
2334 	WL_TRACE(("%s: SIOCGIWTXPOW\n", dev->name));
2335 
2336 	if ((error = dev_wlc_ioctl(dev, WLC_GET_RADIO, &disable, sizeof(disable))) ||
2337 	    (error = dev_wlc_intvar_get(dev, "qtxpower", &txpwrdbm)))
2338 		return error;
2339 
2340 	disable = dtoh32(disable);
2341 	result = (uint8)(txpwrdbm & ~WL_TXPWR_OVERRIDE);
2342 	vwrq->value = (int32)bcm_qdbm_to_mw(result);
2343 	vwrq->fixed = 0;
2344 	vwrq->disabled = (disable & (WL_RADIO_SW_DISABLE | WL_RADIO_HW_DISABLE)) ? 1 : 0;
2345 	vwrq->flags = IW_TXPOW_MWATT;
2346 
2347 	return 0;
2348 }
2349 
2350 #if WIRELESS_EXT > 10
2351 static int
wl_iw_set_retry(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2352 wl_iw_set_retry(
2353 	struct net_device *dev,
2354 	struct iw_request_info *info,
2355 	union iwreq_data *wrqu,
2356 	char *extra
2357 )
2358 {
2359 	struct iw_param	*vwrq = &wrqu->retry;
2360 	int error, lrl, srl;
2361 
2362 	WL_TRACE(("%s: SIOCSIWRETRY\n", dev->name));
2363 
2364 	/* Do not handle "off" or "lifetime" */
2365 	if (vwrq->disabled || (vwrq->flags & IW_RETRY_LIFETIME))
2366 		return -EINVAL;
2367 
2368 	/* Handle "[min|max] limit" */
2369 	if (vwrq->flags & IW_RETRY_LIMIT) {
2370 		/* "max limit" or just "limit" */
2371 #if WIRELESS_EXT > 20
2372 		if ((vwrq->flags & IW_RETRY_LONG) ||(vwrq->flags & IW_RETRY_MAX) ||
2373 			!((vwrq->flags & IW_RETRY_SHORT) || (vwrq->flags & IW_RETRY_MIN)))
2374 #else
2375 		if ((vwrq->flags & IW_RETRY_MAX) || !(vwrq->flags & IW_RETRY_MIN))
2376 #endif /* WIRELESS_EXT > 20 */
2377 		{
2378 			lrl = htod32(vwrq->value);
2379 			if ((error = dev_wlc_ioctl(dev, WLC_SET_LRL, &lrl, sizeof(lrl))))
2380 				return error;
2381 		}
2382 		/* "min limit" or just "limit" */
2383 #if WIRELESS_EXT > 20
2384 		if ((vwrq->flags & IW_RETRY_SHORT) ||(vwrq->flags & IW_RETRY_MIN) ||
2385 			!((vwrq->flags & IW_RETRY_LONG) || (vwrq->flags & IW_RETRY_MAX)))
2386 #else
2387 		if ((vwrq->flags & IW_RETRY_MIN) || !(vwrq->flags & IW_RETRY_MAX))
2388 #endif /* WIRELESS_EXT > 20 */
2389 		{
2390 			srl = htod32(vwrq->value);
2391 			if ((error = dev_wlc_ioctl(dev, WLC_SET_SRL, &srl, sizeof(srl))))
2392 				return error;
2393 		}
2394 	}
2395 
2396 	return 0;
2397 }
2398 
2399 static int
wl_iw_get_retry(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2400 wl_iw_get_retry(
2401 	struct net_device *dev,
2402 	struct iw_request_info *info,
2403 	union iwreq_data *wrqu,
2404 	char *extra
2405 )
2406 {
2407 	struct iw_param	*vwrq = &wrqu->retry;
2408 	int error, lrl, srl;
2409 
2410 	WL_TRACE(("%s: SIOCGIWRETRY\n", dev->name));
2411 
2412 	vwrq->disabled = 0;      /* Can't be disabled */
2413 
2414 	/* Do not handle lifetime queries */
2415 	if ((vwrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME)
2416 		return -EINVAL;
2417 
2418 	/* Get retry limits */
2419 	if ((error = dev_wlc_ioctl(dev, WLC_GET_LRL, &lrl, sizeof(lrl))) ||
2420 	    (error = dev_wlc_ioctl(dev, WLC_GET_SRL, &srl, sizeof(srl))))
2421 		return error;
2422 
2423 	lrl = dtoh32(lrl);
2424 	srl = dtoh32(srl);
2425 
2426 	/* Note : by default, display the min retry number */
2427 	if (vwrq->flags & IW_RETRY_MAX) {
2428 		vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
2429 		vwrq->value = lrl;
2430 	} else {
2431 		vwrq->flags = IW_RETRY_LIMIT;
2432 		vwrq->value = srl;
2433 		if (srl != lrl)
2434 			vwrq->flags |= IW_RETRY_MIN;
2435 	}
2436 
2437 	return 0;
2438 }
2439 #endif /* WIRELESS_EXT > 10 */
2440 
2441 static int
wl_iw_set_encode(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2442 wl_iw_set_encode(
2443 	struct net_device *dev,
2444 	struct iw_request_info *info,
2445 	union iwreq_data *wrqu,
2446 	char *extra
2447 )
2448 {
2449 	struct iw_point *dwrq = (struct iw_point *)wrqu;
2450 	wl_wsec_key_t key;
2451 	int error, val, wsec;
2452 
2453 	WL_TRACE(("%s: SIOCSIWENCODE\n", dev->name));
2454 
2455 	memset(&key, 0, sizeof(key));
2456 
2457 	if ((dwrq->flags & IW_ENCODE_INDEX) == 0) {
2458 		/* Find the current key */
2459 		for (key.index = 0; key.index < DOT11_MAX_DEFAULT_KEYS; key.index++) {
2460 			val = htod32(key.index);
2461 			if ((error = dev_wlc_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val))))
2462 				return error;
2463 			val = dtoh32(val);
2464 			if (val)
2465 				break;
2466 		}
2467 		/* Default to 0 */
2468 		if (key.index == DOT11_MAX_DEFAULT_KEYS)
2469 			key.index = 0;
2470 	} else {
2471 		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
2472 		if (key.index >= DOT11_MAX_DEFAULT_KEYS)
2473 			return -EINVAL;
2474 	}
2475 
2476 	/* Interpret "off" to mean no encryption */
2477 	wsec = (dwrq->flags & IW_ENCODE_DISABLED) ? 0 : WEP_ENABLED;
2478 
2479 	if ((error = dev_wlc_intvar_set(dev, "wsec", wsec)))
2480 		return error;
2481 
2482 	/* Old API used to pass a NULL pointer instead of IW_ENCODE_NOKEY */
2483 	if (!extra || !dwrq->length || (dwrq->flags & IW_ENCODE_NOKEY)) {
2484 		/* Just select a new current key */
2485 		val = htod32(key.index);
2486 		if ((error = dev_wlc_ioctl(dev, WLC_SET_KEY_PRIMARY, &val, sizeof(val))))
2487 			return error;
2488 	} else {
2489 		key.len = dwrq->length;
2490 
2491 		if (dwrq->length > sizeof(key.data))
2492 			return -EINVAL;
2493 
2494 		memcpy(key.data, extra, dwrq->length);
2495 
2496 		key.flags = WL_PRIMARY_KEY;
2497 		switch (key.len) {
2498 		case WEP1_KEY_SIZE:
2499 			key.algo = CRYPTO_ALGO_WEP1;
2500 			break;
2501 		case WEP128_KEY_SIZE:
2502 			key.algo = CRYPTO_ALGO_WEP128;
2503 			break;
2504 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
2505 		case TKIP_KEY_SIZE:
2506 			key.algo = CRYPTO_ALGO_TKIP;
2507 			break;
2508 #endif
2509 		case AES_KEY_SIZE:
2510 			key.algo = CRYPTO_ALGO_AES_CCM;
2511 			break;
2512 		default:
2513 			return -EINVAL;
2514 		}
2515 
2516 		/* Set the new key/index */
2517 		swap_key_from_BE(&key);
2518 		if ((error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key))))
2519 			return error;
2520 	}
2521 
2522 	/* Interpret "restricted" to mean shared key authentication */
2523 	val = (dwrq->flags & IW_ENCODE_RESTRICTED) ? 1 : 0;
2524 	val = htod32(val);
2525 	if ((error = dev_wlc_ioctl(dev, WLC_SET_AUTH, &val, sizeof(val))))
2526 		return error;
2527 
2528 	return 0;
2529 }
2530 
2531 static int
wl_iw_get_encode(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2532 wl_iw_get_encode(
2533 	struct net_device *dev,
2534 	struct iw_request_info *info,
2535 	union iwreq_data *wrqu,
2536 	char *extra
2537 )
2538 {
2539 	struct iw_point *dwrq = (struct iw_point *)wrqu;
2540 	wl_wsec_key_t key;
2541 	int error, val, wsec, auth;
2542 
2543 	WL_TRACE(("%s: SIOCGIWENCODE\n", dev->name));
2544 
2545 	/* assure default values of zero for things we don't touch */
2546 	bzero(&key, sizeof(wl_wsec_key_t));
2547 
2548 	if ((dwrq->flags & IW_ENCODE_INDEX) == 0) {
2549 		/* Find the current key */
2550 		for (key.index = 0; key.index < DOT11_MAX_DEFAULT_KEYS; key.index++) {
2551 			val = key.index;
2552 			if ((error = dev_wlc_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val))))
2553 				return error;
2554 			val = dtoh32(val);
2555 			if (val)
2556 				break;
2557 		}
2558 	} else
2559 		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
2560 
2561 	if (key.index >= DOT11_MAX_DEFAULT_KEYS)
2562 		key.index = 0;
2563 
2564 	/* Get info */
2565 
2566 	if ((error = dev_wlc_ioctl(dev, WLC_GET_WSEC, &wsec, sizeof(wsec))) ||
2567 	    (error = dev_wlc_ioctl(dev, WLC_GET_AUTH, &auth, sizeof(auth))))
2568 		return error;
2569 
2570 	swap_key_to_BE(&key);
2571 
2572 	wsec = dtoh32(wsec);
2573 	auth = dtoh32(auth);
2574 	/* Get key length */
2575 	dwrq->length = MIN(IW_ENCODING_TOKEN_MAX, key.len);
2576 
2577 	/* Get flags */
2578 	dwrq->flags = key.index + 1;
2579 	if (!(wsec & (WEP_ENABLED | TKIP_ENABLED | AES_ENABLED))) {
2580 		/* Interpret "off" to mean no encryption */
2581 		dwrq->flags |= IW_ENCODE_DISABLED;
2582 	}
2583 	if (auth) {
2584 		/* Interpret "restricted" to mean shared key authentication */
2585 		dwrq->flags |= IW_ENCODE_RESTRICTED;
2586 	}
2587 
2588 	/* Get key */
2589 	if (dwrq->length && extra)
2590 		memcpy(extra, key.data, dwrq->length);
2591 
2592 	return 0;
2593 }
2594 
2595 static int
wl_iw_set_power(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2596 wl_iw_set_power(
2597 	struct net_device *dev,
2598 	struct iw_request_info *info,
2599 	union iwreq_data *wrqu,
2600 	char *extra
2601 )
2602 {
2603 	struct iw_param	*vwrq = &wrqu->power;
2604 	int error, pm;
2605 
2606 	WL_TRACE(("%s: SIOCSIWPOWER\n", dev->name));
2607 
2608 	pm = vwrq->disabled ? PM_OFF : PM_MAX;
2609 
2610 	pm = htod32(pm);
2611 	if ((error = dev_wlc_ioctl(dev, WLC_SET_PM, &pm, sizeof(pm))))
2612 		return error;
2613 
2614 	return 0;
2615 }
2616 
2617 static int
wl_iw_get_power(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2618 wl_iw_get_power(
2619 	struct net_device *dev,
2620 	struct iw_request_info *info,
2621 	union iwreq_data *wrqu,
2622 	char *extra
2623 )
2624 {
2625 	struct iw_param	*vwrq = &wrqu->power;
2626 	int error, pm;
2627 
2628 	WL_TRACE(("%s: SIOCGIWPOWER\n", dev->name));
2629 
2630 	if ((error = dev_wlc_ioctl(dev, WLC_GET_PM, &pm, sizeof(pm))))
2631 		return error;
2632 
2633 	pm = dtoh32(pm);
2634 	vwrq->disabled = pm ? 0 : 1;
2635 	vwrq->flags = IW_POWER_ALL_R;
2636 
2637 	return 0;
2638 }
2639 
2640 #if WIRELESS_EXT > 17
2641 static int
wl_iw_set_wpaie(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2642 wl_iw_set_wpaie(
2643 	struct net_device *dev,
2644 	struct iw_request_info *info,
2645 	union iwreq_data *wrqu,
2646 	char *extra
2647 )
2648 {
2649 	struct iw_point *iwp = (struct iw_point *)wrqu;
2650 #if defined(BCMWAPI_WPI)
2651 	uchar buf[WLC_IOCTL_SMLEN] = {0};
2652 	uchar *p = buf;
2653 	int wapi_ie_size;
2654 
2655 	WL_TRACE(("%s: SIOCSIWGENIE\n", dev->name));
2656 
2657 	if (extra[0] == DOT11_MNG_WAPI_ID)
2658 	{
2659 		wapi_ie_size = iwp->length;
2660 		memcpy(p, extra, iwp->length);
2661 		dev_wlc_bufvar_set(dev, "wapiie", buf, wapi_ie_size);
2662 	}
2663 	else
2664 #endif
2665 		dev_wlc_bufvar_set(dev, "wpaie", extra, iwp->length);
2666 
2667 	return 0;
2668 }
2669 
2670 static int
wl_iw_get_wpaie(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2671 wl_iw_get_wpaie(
2672 	struct net_device *dev,
2673 	struct iw_request_info *info,
2674 	union iwreq_data *wrqu,
2675 	char *extra
2676 )
2677 {
2678 	struct iw_point *iwp = (struct iw_point *)wrqu;
2679 	WL_TRACE(("%s: SIOCGIWGENIE\n", dev->name));
2680 	iwp->length = 64;
2681 	dev_wlc_bufvar_get(dev, "wpaie", extra, iwp->length);
2682 	return 0;
2683 }
2684 
2685 static int
wl_iw_set_encodeext(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2686 wl_iw_set_encodeext(
2687 	struct net_device *dev,
2688 	struct iw_request_info *info,
2689 	union iwreq_data *wrqu,
2690 	char *extra
2691 )
2692 {
2693 	struct iw_point *dwrq = (struct iw_point *)wrqu;
2694 	wl_wsec_key_t key;
2695 	int error;
2696 	struct iw_encode_ext *iwe;
2697 
2698 	WL_TRACE(("%s: SIOCSIWENCODEEXT\n", dev->name));
2699 
2700 	memset(&key, 0, sizeof(key));
2701 	iwe = (struct iw_encode_ext *)extra;
2702 
2703 	/* disable encryption completely  */
2704 	if (dwrq->flags & IW_ENCODE_DISABLED) {
2705 
2706 	}
2707 
2708 	/* get the key index */
2709 	key.index = 0;
2710 	if (dwrq->flags & IW_ENCODE_INDEX)
2711 		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
2712 
2713 	key.len = iwe->key_len;
2714 
2715 	/* Instead of bcast for ea address for default wep keys, driver needs it to be Null */
2716 	if (!ETHER_ISMULTI(iwe->addr.sa_data))
2717 		bcopy((void *)&iwe->addr.sa_data, (char *)&key.ea, ETHER_ADDR_LEN);
2718 
2719 	/* check for key index change */
2720 	if (key.len == 0) {
2721 		if (iwe->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
2722 			WL_WSEC(("Changing the the primary Key to %d\n", key.index));
2723 			/* change the key index .... */
2724 			key.index = htod32(key.index);
2725 			error = dev_wlc_ioctl(dev, WLC_SET_KEY_PRIMARY,
2726 				&key.index, sizeof(key.index));
2727 			if (error)
2728 				return error;
2729 		}
2730 		/* key delete */
2731 		else {
2732 			swap_key_from_BE(&key);
2733 			error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key));
2734 			if (error)
2735 				return error;
2736 		}
2737 	}
2738 	/* This case is used to allow an external 802.1x supplicant
2739 	 * to pass the PMK to the in-driver supplicant for use in
2740 	 * the 4-way handshake.
2741 	 */
2742 	else if (iwe->alg == IW_ENCODE_ALG_PMK) {
2743 		int j;
2744 		wsec_pmk_t pmk;
2745 		char keystring[WSEC_MAX_PSK_LEN + 1];
2746 		char* charptr = keystring;
2747 		uint len;
2748 
2749 		/* copy the raw hex key to the appropriate format */
2750 		for (j = 0; j < (WSEC_MAX_PSK_LEN / 2); j++) {
2751 			(void)snprintf(charptr, 3, "%02x", iwe->key[j]);
2752 			charptr += 2;
2753 		}
2754 		len = strlen(keystring);
2755 		pmk.key_len = htod16(len);
2756 		bcopy(keystring, pmk.key, len);
2757 		pmk.flags = htod16(WSEC_PASSPHRASE);
2758 
2759 		WL_WSEC(("set key %s\n", keystring));
2760 		error = dev_wlc_ioctl(dev, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
2761 		if (error) {
2762 			WL_ERROR(("WLC_SET_WSEC_PMK error %d\n", error));
2763 			return error;
2764 		}
2765 	}
2766 
2767 	else {
2768 		if (iwe->key_len > sizeof(key.data))
2769 			return -EINVAL;
2770 
2771 		WL_WSEC(("Setting the key index %d\n", key.index));
2772 		if (iwe->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
2773 			WL_WSEC(("key is a Primary Key\n"));
2774 			key.flags = WL_PRIMARY_KEY;
2775 		}
2776 
2777 		bcopy((void *)iwe->key, key.data, iwe->key_len);
2778 
2779 		if (iwe->alg == IW_ENCODE_ALG_TKIP) {
2780 			uint8 keybuf[8];
2781 			bcopy(&key.data[24], keybuf, sizeof(keybuf));
2782 			bcopy(&key.data[16], &key.data[24], sizeof(keybuf));
2783 			bcopy(keybuf, &key.data[16], sizeof(keybuf));
2784 		}
2785 
2786 		/* rx iv */
2787 		if (iwe->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
2788 			uchar *ivptr;
2789 			ivptr = (uchar *)iwe->rx_seq;
2790 			key.rxiv.hi = (ivptr[5] << 24) | (ivptr[4] << 16) |
2791 				(ivptr[3] << 8) | ivptr[2];
2792 			key.rxiv.lo = (ivptr[1] << 8) | ivptr[0];
2793 			key.iv_initialized = TRUE;
2794 		}
2795 
2796 		switch (iwe->alg) {
2797 			case IW_ENCODE_ALG_NONE:
2798 				key.algo = CRYPTO_ALGO_OFF;
2799 				break;
2800 			case IW_ENCODE_ALG_WEP:
2801 				if (iwe->key_len == WEP1_KEY_SIZE)
2802 					key.algo = CRYPTO_ALGO_WEP1;
2803 				else
2804 					key.algo = CRYPTO_ALGO_WEP128;
2805 				break;
2806 			case IW_ENCODE_ALG_TKIP:
2807 				key.algo = CRYPTO_ALGO_TKIP;
2808 				break;
2809 			case IW_ENCODE_ALG_CCMP:
2810 				key.algo = CRYPTO_ALGO_AES_CCM;
2811 				break;
2812 #ifdef BCMWAPI_WPI
2813 			case IW_ENCODE_ALG_SM4:
2814 				key.algo = CRYPTO_ALGO_SMS4;
2815 				if (iwe->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
2816 					key.flags &= ~WL_PRIMARY_KEY;
2817 				}
2818 				break;
2819 #endif
2820 			default:
2821 				break;
2822 		}
2823 		swap_key_from_BE(&key);
2824 
2825 		dhd_wait_pend8021x(dev);
2826 
2827 		error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key));
2828 		if (error)
2829 			return error;
2830 #ifdef WL_EXT_IAPSTA
2831 		wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY, WL_EXT_STATUS_ADD_KEY, NULL);
2832 #endif
2833 	}
2834 	return 0;
2835 }
2836 
2837 /* wpa2 pmk list */
2838 static int
wl_iw_set_pmksa(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2839 wl_iw_set_pmksa(
2840 	struct net_device *dev,
2841 	struct iw_request_info *info,
2842 	union iwreq_data *wrqu,
2843 	char *extra
2844 )
2845 {
2846 	struct pmk_list *pmk_list = NULL;
2847 	struct iw_pmksa *iwpmksa;
2848 	uint i;
2849 	char eabuf[ETHER_ADDR_STR_LEN];
2850 	pmkid_v1_t *pmkid_array = NULL;
2851 	struct dhd_pub *dhd = dhd_get_pub(dev);
2852 	wl_wext_info_t *wext_info = NULL;
2853 
2854 	WL_TRACE(("%s: SIOCSIWPMKSA\n", dev->name));
2855 	DHD_CHECK(dhd, dev);
2856 	wext_info = dhd->wext_info;
2857 	pmk_list = &wext_info->pmk_list;
2858 	if (pmk_list)
2859 		pmkid_array = pmk_list->pmkids.pmkid;
2860 	iwpmksa = (struct iw_pmksa *)extra;
2861 	bzero((char *)eabuf, ETHER_ADDR_STR_LEN);
2862 	if (iwpmksa->cmd == IW_PMKSA_FLUSH) {
2863 		WL_TRACE(("wl_iw_set_pmksa - IW_PMKSA_FLUSH\n"));
2864 		bzero((char *)pmk_list, sizeof(struct pmk_list));
2865 	}
2866 	if (iwpmksa->cmd == IW_PMKSA_REMOVE) {
2867 		pmkid_list_v1_t pmkid, *pmkidptr;
2868 		pmkidptr = &pmkid;
2869 		bcopy(&iwpmksa->bssid.sa_data[0], &pmkidptr->pmkid[0].BSSID, ETHER_ADDR_LEN);
2870 		bcopy(&iwpmksa->pmkid[0], &pmkidptr->pmkid[0].PMKID, WPA2_PMKID_LEN);
2871 		{
2872 			uint j;
2873 			WL_TRACE(("wl_iw_set_pmksa,IW_PMKSA_REMOVE - PMKID: %s = ",
2874 				bcm_ether_ntoa(&pmkidptr->pmkid[0].BSSID,
2875 				eabuf)));
2876 			for (j = 0; j < WPA2_PMKID_LEN; j++)
2877 				WL_TRACE(("%02x ", pmkidptr->pmkid[0].PMKID[j]));
2878 			WL_TRACE(("\n"));
2879 		}
2880 		for (i = 0; i < pmk_list->pmkids.npmkid; i++)
2881 			if (!bcmp(&iwpmksa->bssid.sa_data[0], &pmkid_array[i].BSSID,
2882 				ETHER_ADDR_LEN))
2883 				break;
2884 		for (; i < pmk_list->pmkids.npmkid; i++) {
2885 			bcopy(&pmkid_array[i+1].BSSID,
2886 				&pmkid_array[i].BSSID,
2887 				ETHER_ADDR_LEN);
2888 			bcopy(&pmkid_array[i+1].PMKID,
2889 				&pmkid_array[i].PMKID,
2890 				WPA2_PMKID_LEN);
2891 		}
2892 		pmk_list->pmkids.npmkid--;
2893 	}
2894 	if (iwpmksa->cmd == IW_PMKSA_ADD) {
2895 		bcopy(&iwpmksa->bssid.sa_data[0],
2896 			&pmkid_array[pmk_list->pmkids.npmkid].BSSID,
2897 			ETHER_ADDR_LEN);
2898 		bcopy(&iwpmksa->pmkid[0], &pmkid_array[pmk_list->pmkids.npmkid].PMKID,
2899 			WPA2_PMKID_LEN);
2900 		{
2901 			uint j;
2902 			uint k;
2903 			k = pmk_list->pmkids.npmkid;
2904 			BCM_REFERENCE(k);
2905 			WL_TRACE(("wl_iw_set_pmksa,IW_PMKSA_ADD - PMKID: %s = ",
2906 				bcm_ether_ntoa(&pmkid_array[k].BSSID,
2907 				eabuf)));
2908 			for (j = 0; j < WPA2_PMKID_LEN; j++)
2909 				WL_TRACE(("%02x ", pmkid_array[k].PMKID[j]));
2910 			WL_TRACE(("\n"));
2911 		}
2912 		pmk_list->pmkids.npmkid++;
2913 	}
2914 	WL_TRACE(("PRINTING pmkid LIST - No of elements %d\n", pmk_list->pmkids.npmkid));
2915 	for (i = 0; i < pmk_list->pmkids.npmkid; i++) {
2916 		uint j;
2917 		WL_TRACE(("PMKID[%d]: %s = ", i,
2918 			bcm_ether_ntoa(&pmkid_array[i].BSSID,
2919 			eabuf)));
2920 		for (j = 0; j < WPA2_PMKID_LEN; j++)
2921 			WL_TRACE(("%02x ", pmkid_array[i].PMKID[j]));
2922 		printf("\n");
2923 	}
2924 	dev_wlc_bufvar_set(dev, "pmkid_info", (char *)pmk_list, sizeof(struct pmk_list));
2925 	return 0;
2926 }
2927 
2928 static int
wl_iw_get_encodeext(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2929 wl_iw_get_encodeext(
2930 	struct net_device *dev,
2931 	struct iw_request_info *info,
2932 	union iwreq_data *wrqu,
2933 	char *extra
2934 )
2935 {
2936 	WL_TRACE(("%s: SIOCGIWENCODEEXT\n", dev->name));
2937 	return 0;
2938 }
2939 
2940 static int
wl_iw_set_wpaauth(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2941 wl_iw_set_wpaauth(
2942 	struct net_device *dev,
2943 	struct iw_request_info *info,
2944 	union iwreq_data *wrqu,
2945 	char *extra
2946 )
2947 {
2948 	struct iw_param	*vwrq = &wrqu->param;
2949 	int error = 0;
2950 	int paramid;
2951 	int paramval;
2952 	uint32 cipher_combined;
2953 	int val = 0;
2954 	wl_iw_t *iw = IW_DEV_IF(dev);
2955 
2956 	WL_TRACE(("%s: SIOCSIWAUTH\n", dev->name));
2957 
2958 	paramid = vwrq->flags & IW_AUTH_INDEX;
2959 	paramval = vwrq->value;
2960 
2961 	WL_TRACE(("%s: SIOCSIWAUTH, paramid = 0x%0x, paramval = 0x%0x\n",
2962 		dev->name, paramid, paramval));
2963 
2964 	switch (paramid) {
2965 
2966 	case IW_AUTH_WPA_VERSION:
2967 		/* supported wpa version disabled or wpa or wpa2 */
2968 		if (paramval & IW_AUTH_WPA_VERSION_DISABLED)
2969 			val = WPA_AUTH_DISABLED;
2970 		else if (paramval & (IW_AUTH_WPA_VERSION_WPA))
2971 			val = WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED;
2972 		else if (paramval & IW_AUTH_WPA_VERSION_WPA2)
2973 			val = WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED;
2974 #ifdef BCMWAPI_WPI
2975 		else if (paramval & IW_AUTH_WAPI_VERSION_1)
2976 			val = WAPI_AUTH_UNSPECIFIED;
2977 #endif
2978 		WL_TRACE(("%d: setting wpa_auth to 0x%0x\n", __LINE__, val));
2979 		if ((error = dev_wlc_intvar_set(dev, "wpa_auth", val)))
2980 			return error;
2981 		break;
2982 
2983 	case IW_AUTH_CIPHER_PAIRWISE:
2984 	case IW_AUTH_CIPHER_GROUP: {
2985 //		int fbt_cap = 0;
2986 
2987 		if (paramid == IW_AUTH_CIPHER_PAIRWISE) {
2988 			iw->pwsec = paramval;
2989 		}
2990 		else {
2991 			iw->gwsec = paramval;
2992 		}
2993 
2994 		if ((error = dev_wlc_intvar_get(dev, "wsec", &val))) {
2995 			WL_ERROR(("wsec error %d\n", error));
2996 			return error;
2997 		}
2998 		WL_WSEC(("get wsec=0x%x\n", val));
2999 
3000 		cipher_combined = iw->gwsec | iw->pwsec;
3001 		val &= ~(WEP_ENABLED | TKIP_ENABLED | AES_ENABLED);
3002 		if (cipher_combined & (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
3003 			val |= WEP_ENABLED;
3004 		if (cipher_combined & IW_AUTH_CIPHER_TKIP)
3005 			val |= TKIP_ENABLED;
3006 		if (cipher_combined & IW_AUTH_CIPHER_CCMP)
3007 			val |= AES_ENABLED;
3008 #ifdef BCMWAPI_WPI
3009 		val &= ~SMS4_ENABLED;
3010 		if (cipher_combined & IW_AUTH_CIPHER_SMS4)
3011 			val |= SMS4_ENABLED;
3012 #endif
3013 
3014 		if (iw->privacy_invoked && !val) {
3015 			WL_WSEC(("%s: 'Privacy invoked' TRUE but clearing wsec, assuming "
3016 			         "we're a WPS enrollee\n", dev->name));
3017 			if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", TRUE))) {
3018 				WL_WSEC(("Failed to set iovar is_WPS_enrollee\n"));
3019 				return error;
3020 			}
3021 		} else if (val) {
3022 			if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", FALSE))) {
3023 				WL_WSEC(("Failed to clear iovar is_WPS_enrollee\n"));
3024 				return error;
3025 			}
3026 		}
3027 
3028 		WL_WSEC(("set wsec=0x%x\n", val));
3029 		if ((error = dev_wlc_intvar_set(dev, "wsec", val))) {
3030 			WL_ERROR(("wsec error %d\n", error));
3031 			return error;
3032 		}
3033 
3034 #if 0
3035 		/* Ensure in-dongle supplicant is turned on when FBT wants to do the 4-way
3036 		 * handshake.
3037 		 */
3038 		if (dev_wlc_intvar_get(dev, "fbt_cap", &fbt_cap) == 0) {
3039 			WL_WSEC(("get fbt_cap=0x%x\n", fbt_cap));
3040 			if (fbt_cap == WLC_FBT_CAP_DRV_4WAY_AND_REASSOC) {
3041 				if ((paramid == IW_AUTH_CIPHER_PAIRWISE) && (val & AES_ENABLED)) {
3042 					if ((error = dev_wlc_intvar_set(dev, "sup_wpa", 1))) {
3043 						WL_ERROR(("sup_wpa 1 error %d\n", error));
3044 						return error;
3045 					}
3046 				}
3047 				else if (val == 0) {
3048 					if ((error = dev_wlc_intvar_set(dev, "sup_wpa", 0))) {
3049 						WL_ERROR(("sup_wpa 0 error %d\n", error));
3050 						return error;
3051 					}
3052 				}
3053 			}
3054 		}
3055 #endif
3056 		break;
3057 	}
3058 
3059 	case IW_AUTH_KEY_MGMT:
3060 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val))) {
3061 			WL_ERROR(("wpa_auth error %d\n", error));
3062 			return error;
3063 		}
3064 		WL_WSEC(("get wpa_auth to %d\n", val));
3065 
3066 		if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED)) {
3067 			if (paramval & (IW_AUTH_KEY_MGMT_FT_PSK | IW_AUTH_KEY_MGMT_PSK))
3068 				val = WPA_AUTH_PSK;
3069 			else
3070 				val = WPA_AUTH_UNSPECIFIED;
3071 			if (paramval & (IW_AUTH_KEY_MGMT_FT_802_1X | IW_AUTH_KEY_MGMT_FT_PSK))
3072 				val |= WPA2_AUTH_FT;
3073 		}
3074 		else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED)) {
3075 			if (paramval & (IW_AUTH_KEY_MGMT_FT_PSK | IW_AUTH_KEY_MGMT_PSK))
3076 				val = WPA2_AUTH_PSK;
3077 			else
3078 				val = WPA2_AUTH_UNSPECIFIED;
3079 			if (paramval & (IW_AUTH_KEY_MGMT_FT_802_1X | IW_AUTH_KEY_MGMT_FT_PSK))
3080 				val |= WPA2_AUTH_FT;
3081 		}
3082 #ifdef BCMWAPI_WPI
3083 		if (paramval & (IW_AUTH_KEY_MGMT_WAPI_PSK | IW_AUTH_KEY_MGMT_WAPI_CERT))
3084 			val = WAPI_AUTH_UNSPECIFIED;
3085 #endif
3086 		WL_TRACE(("%d: setting wpa_auth to %d\n", __LINE__, val));
3087 		if ((error = dev_wlc_intvar_set(dev, "wpa_auth", val)))
3088 			return error;
3089 		break;
3090 
3091 	case IW_AUTH_TKIP_COUNTERMEASURES:
3092 		dev_wlc_bufvar_set(dev, "tkip_countermeasures", (char *)&paramval, 1);
3093 		break;
3094 
3095 	case IW_AUTH_80211_AUTH_ALG:
3096 		/* open shared */
3097 		WL_MSG(dev->name, "Setting the D11auth %d\n", paramval);
3098 		if (paramval & IW_AUTH_ALG_OPEN_SYSTEM)
3099 			val = 0;
3100 		else if (paramval & IW_AUTH_ALG_SHARED_KEY)
3101 			val = 1;
3102 		else
3103 			error = 1;
3104 		if (!error && (error = dev_wlc_intvar_set(dev, "auth", val)))
3105 			return error;
3106 		break;
3107 
3108 	case IW_AUTH_WPA_ENABLED:
3109 		if (paramval == 0) {
3110 			val = 0;
3111 			WL_TRACE(("%d: setting wpa_auth to %d\n", __LINE__, val));
3112 			error = dev_wlc_intvar_set(dev, "wpa_auth", val);
3113 			return error;
3114 		}
3115 		else {
3116 			/* If WPA is enabled, wpa_auth is set elsewhere */
3117 		}
3118 		break;
3119 
3120 	case IW_AUTH_DROP_UNENCRYPTED:
3121 		dev_wlc_bufvar_set(dev, "wsec_restrict", (char *)&paramval, 1);
3122 		break;
3123 
3124 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3125 		dev_wlc_bufvar_set(dev, "rx_unencrypted_eapol", (char *)&paramval, 1);
3126 		break;
3127 
3128 #if WIRELESS_EXT > 17
3129 
3130 	case IW_AUTH_ROAMING_CONTROL:
3131 		WL_TRACE(("IW_AUTH_ROAMING_CONTROL\n"));
3132 		/* driver control or user space app control */
3133 		break;
3134 
3135 	case IW_AUTH_PRIVACY_INVOKED: {
3136 		int wsec;
3137 
3138 		if (paramval == 0) {
3139 			iw->privacy_invoked = FALSE;
3140 			if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", FALSE))) {
3141 				WL_WSEC(("Failed to clear iovar is_WPS_enrollee\n"));
3142 				return error;
3143 			}
3144 		} else {
3145 			iw->privacy_invoked = TRUE;
3146 			if ((error = dev_wlc_intvar_get(dev, "wsec", &wsec)))
3147 				return error;
3148 
3149 			if (!WSEC_ENABLED(wsec)) {
3150 				/* if privacy is true, but wsec is false, we are a WPS enrollee */
3151 				if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", TRUE))) {
3152 					WL_WSEC(("Failed to set iovar is_WPS_enrollee\n"));
3153 					return error;
3154 				}
3155 			} else {
3156 				if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", FALSE))) {
3157 					WL_WSEC(("Failed to clear iovar is_WPS_enrollee\n"));
3158 					return error;
3159 				}
3160 			}
3161 		}
3162 		break;
3163 	}
3164 
3165 
3166 #endif /* WIRELESS_EXT > 17 */
3167 
3168 #ifdef BCMWAPI_WPI
3169 
3170 	case IW_AUTH_WAPI_ENABLED:
3171 		if ((error = dev_wlc_intvar_get(dev, "wsec", &val)))
3172 			return error;
3173 		if (paramval) {
3174 			val |= SMS4_ENABLED;
3175 			if ((error = dev_wlc_intvar_set(dev, "wsec", val))) {
3176 				WL_ERROR(("setting wsec to 0x%0x returned error %d\n",
3177 					val, error));
3178 				return error;
3179 			}
3180 			if ((error = dev_wlc_intvar_set(dev, "wpa_auth", WAPI_AUTH_UNSPECIFIED))) {
3181 				WL_ERROR(("setting wpa_auth(%d) returned %d\n",
3182 					WAPI_AUTH_UNSPECIFIED,
3183 					error));
3184 				return error;
3185 			}
3186 		}
3187 
3188 		break;
3189 
3190 #endif /* BCMWAPI_WPI */
3191 
3192 	default:
3193 		break;
3194 	}
3195 	return 0;
3196 }
3197 #define VAL_PSK(_val) (((_val) & WPA_AUTH_PSK) || ((_val) & WPA2_AUTH_PSK))
3198 
3199 static int
wl_iw_get_wpaauth(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)3200 wl_iw_get_wpaauth(
3201 	struct net_device *dev,
3202 	struct iw_request_info *info,
3203 	union iwreq_data *wrqu,
3204 	char *extra
3205 )
3206 {
3207 	struct iw_param	*vwrq = &wrqu->param;
3208 	int error;
3209 	int paramid;
3210 	int paramval = 0;
3211 	int val;
3212 	wl_iw_t *iw = IW_DEV_IF(dev);
3213 
3214 	WL_TRACE(("%s: SIOCGIWAUTH\n", dev->name));
3215 
3216 	paramid = vwrq->flags & IW_AUTH_INDEX;
3217 
3218 	switch (paramid) {
3219 	case IW_AUTH_WPA_VERSION:
3220 		/* supported wpa version disabled or wpa or wpa2 */
3221 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val)))
3222 			return error;
3223 		if (val & (WPA_AUTH_NONE | WPA_AUTH_DISABLED))
3224 			paramval = IW_AUTH_WPA_VERSION_DISABLED;
3225 		else if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED))
3226 			paramval = IW_AUTH_WPA_VERSION_WPA;
3227 		else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED))
3228 			paramval = IW_AUTH_WPA_VERSION_WPA2;
3229 		break;
3230 
3231 	case IW_AUTH_CIPHER_PAIRWISE:
3232 		paramval = iw->pwsec;
3233 		break;
3234 
3235 	case IW_AUTH_CIPHER_GROUP:
3236 		paramval = iw->gwsec;
3237 		break;
3238 
3239 	case IW_AUTH_KEY_MGMT:
3240 		/* psk, 1x */
3241 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val)))
3242 			return error;
3243 		if (VAL_PSK(val))
3244 			paramval = IW_AUTH_KEY_MGMT_PSK;
3245 		else
3246 			paramval = IW_AUTH_KEY_MGMT_802_1X;
3247 
3248 		break;
3249 	case IW_AUTH_TKIP_COUNTERMEASURES:
3250 		dev_wlc_bufvar_get(dev, "tkip_countermeasures", (char *)&paramval, 1);
3251 		break;
3252 
3253 	case IW_AUTH_DROP_UNENCRYPTED:
3254 		dev_wlc_bufvar_get(dev, "wsec_restrict", (char *)&paramval, 1);
3255 		break;
3256 
3257 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3258 		dev_wlc_bufvar_get(dev, "rx_unencrypted_eapol", (char *)&paramval, 1);
3259 		break;
3260 
3261 	case IW_AUTH_80211_AUTH_ALG:
3262 		/* open, shared, leap */
3263 		if ((error = dev_wlc_intvar_get(dev, "auth", &val)))
3264 			return error;
3265 		if (!val)
3266 			paramval = IW_AUTH_ALG_OPEN_SYSTEM;
3267 		else
3268 			paramval = IW_AUTH_ALG_SHARED_KEY;
3269 		break;
3270 	case IW_AUTH_WPA_ENABLED:
3271 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val)))
3272 			return error;
3273 		if (val)
3274 			paramval = TRUE;
3275 		else
3276 			paramval = FALSE;
3277 		break;
3278 
3279 #if WIRELESS_EXT > 17
3280 
3281 	case IW_AUTH_ROAMING_CONTROL:
3282 		WL_ERROR(("IW_AUTH_ROAMING_CONTROL\n"));
3283 		/* driver control or user space app control */
3284 		break;
3285 
3286 	case IW_AUTH_PRIVACY_INVOKED:
3287 		paramval = iw->privacy_invoked;
3288 		break;
3289 
3290 #endif /* WIRELESS_EXT > 17 */
3291 	}
3292 	vwrq->value = paramval;
3293 	return 0;
3294 }
3295 #endif /* WIRELESS_EXT > 17 */
3296 
3297 static const iw_handler wl_iw_handler[] =
3298 {
3299 	(iw_handler) wl_iw_config_commit,	/* SIOCSIWCOMMIT */
3300 	(iw_handler) wl_iw_get_name,		/* SIOCGIWNAME */
3301 	(iw_handler) NULL,			/* SIOCSIWNWID */
3302 	(iw_handler) NULL,			/* SIOCGIWNWID */
3303 	(iw_handler) wl_iw_set_freq,		/* SIOCSIWFREQ */
3304 	(iw_handler) wl_iw_get_freq,		/* SIOCGIWFREQ */
3305 	(iw_handler) wl_iw_set_mode,		/* SIOCSIWMODE */
3306 	(iw_handler) wl_iw_get_mode,		/* SIOCGIWMODE */
3307 	(iw_handler) NULL,			/* SIOCSIWSENS */
3308 	(iw_handler) NULL,			/* SIOCGIWSENS */
3309 	(iw_handler) NULL,			/* SIOCSIWRANGE */
3310 	(iw_handler) wl_iw_get_range,		/* SIOCGIWRANGE */
3311 	(iw_handler) NULL,			/* SIOCSIWPRIV */
3312 	(iw_handler) NULL,			/* SIOCGIWPRIV */
3313 	(iw_handler) NULL,			/* SIOCSIWSTATS */
3314 	(iw_handler) NULL,			/* SIOCGIWSTATS */
3315 	(iw_handler) wl_iw_set_spy,		/* SIOCSIWSPY */
3316 	(iw_handler) wl_iw_get_spy,		/* SIOCGIWSPY */
3317 	(iw_handler) NULL,			/* -- hole -- */
3318 	(iw_handler) NULL,			/* -- hole -- */
3319 	(iw_handler) wl_iw_set_wap,		/* SIOCSIWAP */
3320 	(iw_handler) wl_iw_get_wap,		/* SIOCGIWAP */
3321 #if WIRELESS_EXT > 17
3322 	(iw_handler) wl_iw_mlme,		/* SIOCSIWMLME */
3323 #else
3324 	(iw_handler) NULL,			/* -- hole -- */
3325 #endif
3326 #ifdef WL_ESCAN
3327 	(iw_handler) NULL,			/* SIOCGIWAPLIST */
3328 #else
3329 	(iw_handler) wl_iw_iscan_get_aplist,	/* SIOCGIWAPLIST */
3330 #endif
3331 #if WIRELESS_EXT > 13
3332 	(iw_handler) wl_iw_iscan_set_scan,	/* SIOCSIWSCAN */
3333 	(iw_handler) wl_iw_iscan_get_scan,	/* SIOCGIWSCAN */
3334 #else	/* WIRELESS_EXT > 13 */
3335 	(iw_handler) NULL,			/* SIOCSIWSCAN */
3336 	(iw_handler) NULL,			/* SIOCGIWSCAN */
3337 #endif	/* WIRELESS_EXT > 13 */
3338 	(iw_handler) wl_iw_set_essid,		/* SIOCSIWESSID */
3339 	(iw_handler) wl_iw_get_essid,		/* SIOCGIWESSID */
3340 	(iw_handler) wl_iw_set_nick,		/* SIOCSIWNICKN */
3341 	(iw_handler) wl_iw_get_nick,		/* SIOCGIWNICKN */
3342 	(iw_handler) NULL,			/* -- hole -- */
3343 	(iw_handler) NULL,			/* -- hole -- */
3344 	(iw_handler) wl_iw_set_rate,		/* SIOCSIWRATE */
3345 	(iw_handler) wl_iw_get_rate,		/* SIOCGIWRATE */
3346 	(iw_handler) wl_iw_set_rts,		/* SIOCSIWRTS */
3347 	(iw_handler) wl_iw_get_rts,		/* SIOCGIWRTS */
3348 	(iw_handler) wl_iw_set_frag,		/* SIOCSIWFRAG */
3349 	(iw_handler) wl_iw_get_frag,		/* SIOCGIWFRAG */
3350 	(iw_handler) wl_iw_set_txpow,		/* SIOCSIWTXPOW */
3351 	(iw_handler) wl_iw_get_txpow,		/* SIOCGIWTXPOW */
3352 #if WIRELESS_EXT > 10
3353 	(iw_handler) wl_iw_set_retry,		/* SIOCSIWRETRY */
3354 	(iw_handler) wl_iw_get_retry,		/* SIOCGIWRETRY */
3355 #endif /* WIRELESS_EXT > 10 */
3356 	(iw_handler) wl_iw_set_encode,		/* SIOCSIWENCODE */
3357 	(iw_handler) wl_iw_get_encode,		/* SIOCGIWENCODE */
3358 	(iw_handler) wl_iw_set_power,		/* SIOCSIWPOWER */
3359 	(iw_handler) wl_iw_get_power,		/* SIOCGIWPOWER */
3360 #if WIRELESS_EXT > 17
3361 	(iw_handler) NULL,			/* -- hole -- */
3362 	(iw_handler) NULL,			/* -- hole -- */
3363 	(iw_handler) wl_iw_set_wpaie,		/* SIOCSIWGENIE */
3364 	(iw_handler) wl_iw_get_wpaie,		/* SIOCGIWGENIE */
3365 	(iw_handler) wl_iw_set_wpaauth,		/* SIOCSIWAUTH */
3366 	(iw_handler) wl_iw_get_wpaauth,		/* SIOCGIWAUTH */
3367 	(iw_handler) wl_iw_set_encodeext,	/* SIOCSIWENCODEEXT */
3368 	(iw_handler) wl_iw_get_encodeext,	/* SIOCGIWENCODEEXT */
3369 	(iw_handler) wl_iw_set_pmksa,		/* SIOCSIWPMKSA */
3370 #endif /* WIRELESS_EXT > 17 */
3371 };
3372 
3373 #if WIRELESS_EXT > 12
3374 #ifdef CONFIG_WEXT_PRIV
3375 enum {
3376 	WL_IW_SET_LEDDC = SIOCIWFIRSTPRIV,
3377 	WL_IW_SET_VLANMODE,
3378 	WL_IW_SET_PM,
3379 	WL_IW_SET_LAST
3380 };
3381 
3382 static iw_handler wl_iw_priv_handler[] = {
3383 	wl_iw_set_leddc,
3384 	wl_iw_set_vlanmode,
3385 	wl_iw_set_pm,
3386 	NULL
3387 };
3388 
3389 static struct iw_priv_args wl_iw_priv_args[] = {
3390 	{
3391 		WL_IW_SET_LEDDC,
3392 		IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3393 		0,
3394 		"set_leddc"
3395 	},
3396 	{
3397 		WL_IW_SET_VLANMODE,
3398 		IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3399 		0,
3400 		"set_vlanmode"
3401 	},
3402 	{
3403 		WL_IW_SET_PM,
3404 		IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3405 		0,
3406 		"set_pm"
3407 	},
3408 	{ 0, 0, 0, { 0 } }
3409 };
3410 #endif /* CONFIG_WEXT_PRIV */
3411 
3412 const struct iw_handler_def wl_iw_handler_def =
3413 {
3414 	.num_standard = ARRAYSIZE(wl_iw_handler),
3415 	.standard = (const iw_handler *) wl_iw_handler,
3416 #ifdef CONFIG_WEXT_PRIV
3417 	.num_private = ARRAY_SIZE(wl_iw_priv_handler),
3418 	.num_private_args = ARRAY_SIZE(wl_iw_priv_args),
3419 	.private = wl_iw_priv_handler,
3420 	.private_args = wl_iw_priv_args,
3421 #endif /* CONFIG_WEXT_PRIV */
3422 #if WIRELESS_EXT >= 19
3423 	get_wireless_stats: dhd_get_wireless_stats,
3424 #endif /* WIRELESS_EXT >= 19 */
3425 	};
3426 #endif /* WIRELESS_EXT > 12 */
3427 
3428 int
wl_iw_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)3429 wl_iw_ioctl(
3430 	struct net_device *dev,
3431 	struct ifreq *rq,
3432 	int cmd
3433 )
3434 {
3435 	struct iwreq *wrq = (struct iwreq *) rq;
3436 	struct iw_request_info info;
3437 	iw_handler handler;
3438 	char *extra = NULL;
3439 	size_t token_size = 1;
3440 	int max_tokens = 0, ret = 0;
3441 #ifndef WL_ESCAN
3442 	struct dhd_pub *dhd = dhd_get_pub(dev);
3443 	wl_wext_info_t *wext_info = NULL;
3444 	iscan_info_t *iscan;
3445 
3446 	DHD_CHECK(dhd, dev);
3447 	wext_info = dhd->wext_info;
3448 	iscan = &wext_info->iscan;
3449 #endif
3450 
3451 	if (cmd < SIOCIWFIRST ||
3452 		IW_IOCTL_IDX(cmd) >= ARRAYSIZE(wl_iw_handler) ||
3453 		!(handler = wl_iw_handler[IW_IOCTL_IDX(cmd)]))
3454 		return -EOPNOTSUPP;
3455 
3456 	switch (cmd) {
3457 
3458 	case SIOCSIWESSID:
3459 	case SIOCGIWESSID:
3460 	case SIOCSIWNICKN:
3461 	case SIOCGIWNICKN:
3462 		max_tokens = IW_ESSID_MAX_SIZE + 1;
3463 		break;
3464 
3465 	case SIOCSIWENCODE:
3466 	case SIOCGIWENCODE:
3467 #if WIRELESS_EXT > 17
3468 	case SIOCSIWENCODEEXT:
3469 	case SIOCGIWENCODEEXT:
3470 #endif
3471 		max_tokens = IW_ENCODING_TOKEN_MAX;
3472 		break;
3473 
3474 	case SIOCGIWRANGE:
3475 		max_tokens = sizeof(struct iw_range);
3476 		break;
3477 
3478 	case SIOCGIWAPLIST:
3479 		token_size = sizeof(struct sockaddr) + sizeof(struct iw_quality);
3480 		max_tokens = IW_MAX_AP;
3481 		break;
3482 
3483 #if WIRELESS_EXT > 13
3484 	case SIOCGIWSCAN:
3485 #ifndef WL_ESCAN
3486 		if (iscan)
3487 			max_tokens = wrq->u.data.length;
3488 		else
3489 #endif
3490 		max_tokens = IW_SCAN_MAX_DATA;
3491 		break;
3492 #endif /* WIRELESS_EXT > 13 */
3493 
3494 	case SIOCSIWSPY:
3495 		token_size = sizeof(struct sockaddr);
3496 		max_tokens = IW_MAX_SPY;
3497 		break;
3498 
3499 	case SIOCGIWSPY:
3500 		token_size = sizeof(struct sockaddr) + sizeof(struct iw_quality);
3501 		max_tokens = IW_MAX_SPY;
3502 		break;
3503 	default:
3504 		break;
3505 	}
3506 
3507 	if (max_tokens && wrq->u.data.pointer) {
3508 		if (wrq->u.data.length > max_tokens)
3509 			return -E2BIG;
3510 
3511 		if (!(extra = kmalloc(max_tokens * token_size, GFP_KERNEL)))
3512 			return -ENOMEM;
3513 
3514 		if (copy_from_user(extra, wrq->u.data.pointer, wrq->u.data.length * token_size)) {
3515 			kfree(extra);
3516 			return -EFAULT;
3517 		}
3518 	}
3519 
3520 	info.cmd = cmd;
3521 	info.flags = 0;
3522 
3523 	ret = handler(dev, &info, &wrq->u, extra);
3524 
3525 	if (extra) {
3526 		if (copy_to_user(wrq->u.data.pointer, extra, wrq->u.data.length * token_size)) {
3527 			kfree(extra);
3528 			return -EFAULT;
3529 		}
3530 
3531 		kfree(extra);
3532 	}
3533 
3534 	return ret;
3535 }
3536 
3537 /* Convert a connection status event into a connection status string.
3538  * Returns TRUE if a matching connection status string was found.
3539  */
3540 bool
wl_iw_conn_status_str(uint32 event_type,uint32 status,uint32 reason,char * stringBuf,uint buflen)3541 wl_iw_conn_status_str(uint32 event_type, uint32 status, uint32 reason,
3542 	char* stringBuf, uint buflen)
3543 {
3544 	typedef struct conn_fail_event_map_t {
3545 		uint32 inEvent;			/* input: event type to match */
3546 		uint32 inStatus;		/* input: event status code to match */
3547 		uint32 inReason;		/* input: event reason code to match */
3548 		const char* outName;	/* output: failure type */
3549 		const char* outCause;	/* output: failure cause */
3550 	} conn_fail_event_map_t;
3551 
3552 	/* Map of WLC_E events to connection failure strings */
3553 #	define WL_IW_DONT_CARE	9999
3554 	const conn_fail_event_map_t event_map [] = {
3555 		/* inEvent           inStatus                inReason         */
3556 		/* outName outCause                                           */
3557 		{WLC_E_SET_SSID,     WLC_E_STATUS_SUCCESS,   WL_IW_DONT_CARE,
3558 		"Conn", "Success"},
3559 		{WLC_E_SET_SSID,     WLC_E_STATUS_NO_NETWORKS, WL_IW_DONT_CARE,
3560 		"Conn", "NoNetworks"},
3561 		{WLC_E_SET_SSID,     WLC_E_STATUS_FAIL,      WL_IW_DONT_CARE,
3562 		"Conn", "ConfigMismatch"},
3563 		{WLC_E_PRUNE,        WL_IW_DONT_CARE,        WLC_E_PRUNE_ENCR_MISMATCH,
3564 		"Conn", "EncrypMismatch"},
3565 		{WLC_E_PRUNE,        WL_IW_DONT_CARE,        WLC_E_RSN_MISMATCH,
3566 		"Conn", "RsnMismatch"},
3567 		{WLC_E_AUTH,         WLC_E_STATUS_TIMEOUT,   WL_IW_DONT_CARE,
3568 		"Conn", "AuthTimeout"},
3569 		{WLC_E_AUTH,         WLC_E_STATUS_FAIL,      WL_IW_DONT_CARE,
3570 		"Conn", "AuthFail"},
3571 		{WLC_E_AUTH,         WLC_E_STATUS_NO_ACK,    WL_IW_DONT_CARE,
3572 		"Conn", "AuthNoAck"},
3573 		{WLC_E_REASSOC,      WLC_E_STATUS_FAIL,      WL_IW_DONT_CARE,
3574 		"Conn", "ReassocFail"},
3575 		{WLC_E_REASSOC,      WLC_E_STATUS_TIMEOUT,   WL_IW_DONT_CARE,
3576 		"Conn", "ReassocTimeout"},
3577 		{WLC_E_REASSOC,      WLC_E_STATUS_ABORT,     WL_IW_DONT_CARE,
3578 		"Conn", "ReassocAbort"},
3579 		{WLC_E_PSK_SUP,      WLC_SUP_KEYED,          WL_IW_DONT_CARE,
3580 		"Sup", "ConnSuccess"},
3581 		{WLC_E_PSK_SUP,      WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3582 		"Sup", "WpaHandshakeFail"},
3583 		{WLC_E_DEAUTH_IND,   WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3584 		"Conn", "Deauth"},
3585 		{WLC_E_DISASSOC_IND, WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3586 		"Conn", "DisassocInd"},
3587 		{WLC_E_DISASSOC,     WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3588 		"Conn", "Disassoc"}
3589 	};
3590 
3591 	const char* name = "";
3592 	const char* cause = NULL;
3593 	int i;
3594 
3595 	/* Search the event map table for a matching event */
3596 	for (i = 0;  i < sizeof(event_map)/sizeof(event_map[0]);  i++) {
3597 		const conn_fail_event_map_t* row = &event_map[i];
3598 		if (row->inEvent == event_type &&
3599 		    (row->inStatus == status || row->inStatus == WL_IW_DONT_CARE) &&
3600 		    (row->inReason == reason || row->inReason == WL_IW_DONT_CARE)) {
3601 			name = row->outName;
3602 			cause = row->outCause;
3603 			break;
3604 		}
3605 	}
3606 
3607 	/* If found, generate a connection failure string and return TRUE */
3608 	if (cause) {
3609 		memset(stringBuf, 0, buflen);
3610 		(void)snprintf(stringBuf, buflen, "%s %s %02d %02d", name, cause, status, reason);
3611 		WL_TRACE(("Connection status: %s\n", stringBuf));
3612 		return TRUE;
3613 	} else {
3614 		return FALSE;
3615 	}
3616 }
3617 
3618 #if (WIRELESS_EXT > 14)
3619 /* Check if we have received an event that indicates connection failure
3620  * If so, generate a connection failure report string.
3621  * The caller supplies a buffer to hold the generated string.
3622  */
3623 static bool
wl_iw_check_conn_fail(const wl_event_msg_t * e,char * stringBuf,uint buflen)3624 wl_iw_check_conn_fail(const wl_event_msg_t *e, char* stringBuf, uint buflen)
3625 {
3626 	uint32 event = ntoh32(e->event_type);
3627 	uint32 status =  ntoh32(e->status);
3628 	uint32 reason =  ntoh32(e->reason);
3629 
3630 	if (wl_iw_conn_status_str(event, status, reason, stringBuf, buflen)) {
3631 		return TRUE;
3632 	} else
3633 	{
3634 		return FALSE;
3635 	}
3636 }
3637 #endif /* WIRELESS_EXT > 14 */
3638 
3639 #ifndef IW_CUSTOM_MAX
3640 #define IW_CUSTOM_MAX 256 /* size of extra buffer used for translation of events */
3641 #endif /* IW_CUSTOM_MAX */
3642 
3643 void
wl_iw_event(struct net_device * dev,void * argu,const wl_event_msg_t * e,void * data)3644 wl_iw_event(struct net_device *dev, void *argu,
3645 	const wl_event_msg_t *e, void* data)
3646 {
3647 #if WIRELESS_EXT > 13
3648 	union iwreq_data wrqu;
3649 	char extra[IW_CUSTOM_MAX + 1];
3650 	int cmd = 0;
3651 	uint32 event_type = ntoh32(e->event_type);
3652 	uint16 flags =  ntoh16(e->flags);
3653 	uint32 datalen = ntoh32(e->datalen);
3654 	uint32 status =  ntoh32(e->status);
3655 	uint32 reason =  ntoh32(e->reason);
3656 #ifndef WL_ESCAN
3657 	struct wl_wext_info *wext_info = (struct wl_wext_info *)argu;
3658 	iscan_info_t *iscan = &wext_info->iscan;
3659 #endif
3660 
3661 	memset(&wrqu, 0, sizeof(wrqu));
3662 	memset(extra, 0, sizeof(extra));
3663 
3664 	memcpy(wrqu.addr.sa_data, &e->addr, ETHER_ADDR_LEN);
3665 	wrqu.addr.sa_family = ARPHRD_ETHER;
3666 
3667 	switch (event_type) {
3668 	case WLC_E_TXFAIL:
3669 		cmd = IWEVTXDROP;
3670 		break;
3671 #if WIRELESS_EXT > 14
3672 	case WLC_E_JOIN:
3673 	case WLC_E_ASSOC_IND:
3674 	case WLC_E_REASSOC_IND:
3675 		cmd = IWEVREGISTERED;
3676 		break;
3677 	case WLC_E_DEAUTH:
3678 	case WLC_E_DISASSOC:
3679 #ifdef WL_EXT_IAPSTA
3680 		wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY,
3681 			WL_EXT_STATUS_DISCONNECTED, NULL);
3682 #endif
3683 		WL_MSG_RLMT(dev->name, &e->addr, ETHER_ADDR_LEN,
3684 			"disconnected with "MACSTR", event %d, reason %d\n",
3685 			MAC2STR((u8 *)wrqu.addr.sa_data), event_type, reason);
3686 		break;
3687 	case WLC_E_DEAUTH_IND:
3688 	case WLC_E_DISASSOC_IND:
3689 		cmd = SIOCGIWAP;
3690 		WL_MSG(dev->name, "disconnected with "MACSTR", event %d, reason %d\n",
3691 			MAC2STR((u8 *)wrqu.addr.sa_data), event_type, reason);
3692 		bzero(wrqu.addr.sa_data, ETHER_ADDR_LEN);
3693 		bzero(&extra, ETHER_ADDR_LEN);
3694 #ifdef WL_EXT_IAPSTA
3695 		wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY,
3696 			WL_EXT_STATUS_DISCONNECTED, NULL);
3697 #endif
3698 		break;
3699 
3700 	case WLC_E_LINK:
3701 		cmd = SIOCGIWAP;
3702 		if (!(flags & WLC_EVENT_MSG_LINK)) {
3703 			WL_MSG(dev->name, "Link Down with "MACSTR", reason=%d\n",
3704 				MAC2STR((u8 *)wrqu.addr.sa_data), reason);
3705 			bzero(wrqu.addr.sa_data, ETHER_ADDR_LEN);
3706 			bzero(&extra, ETHER_ADDR_LEN);
3707 #ifdef WL_EXT_IAPSTA
3708 			wl_ext_in4way_sync_wext(dev, STA_NO_BTC_IN4WAY,
3709 				WL_EXT_STATUS_DISCONNECTED, NULL);
3710 #endif
3711 		} else {
3712 			WL_MSG(dev->name, "Link UP with "MACSTR"\n",
3713 				MAC2STR((u8 *)wrqu.addr.sa_data));
3714 		}
3715 		break;
3716 	case WLC_E_ACTION_FRAME:
3717 		cmd = IWEVCUSTOM;
3718 		if (datalen + 1 <= sizeof(extra)) {
3719 			wrqu.data.length = datalen + 1;
3720 			extra[0] = WLC_E_ACTION_FRAME;
3721 			memcpy(&extra[1], data, datalen);
3722 			WL_TRACE(("WLC_E_ACTION_FRAME len %d \n", wrqu.data.length));
3723 		}
3724 		break;
3725 
3726 	case WLC_E_ACTION_FRAME_COMPLETE:
3727 		cmd = IWEVCUSTOM;
3728 		if (sizeof(status) + 1 <= sizeof(extra)) {
3729 			wrqu.data.length = sizeof(status) + 1;
3730 			extra[0] = WLC_E_ACTION_FRAME_COMPLETE;
3731 			memcpy(&extra[1], &status, sizeof(status));
3732 			WL_TRACE(("wl_iw_event status %d  \n", status));
3733 		}
3734 		break;
3735 #endif /* WIRELESS_EXT > 14 */
3736 #if WIRELESS_EXT > 17
3737 	case WLC_E_MIC_ERROR: {
3738 		struct	iw_michaelmicfailure  *micerrevt = (struct  iw_michaelmicfailure  *)&extra;
3739 		cmd = IWEVMICHAELMICFAILURE;
3740 		wrqu.data.length = sizeof(struct iw_michaelmicfailure);
3741 		if (flags & WLC_EVENT_MSG_GROUP)
3742 			micerrevt->flags |= IW_MICFAILURE_GROUP;
3743 		else
3744 			micerrevt->flags |= IW_MICFAILURE_PAIRWISE;
3745 		memcpy(micerrevt->src_addr.sa_data, &e->addr, ETHER_ADDR_LEN);
3746 		micerrevt->src_addr.sa_family = ARPHRD_ETHER;
3747 
3748 		break;
3749 	}
3750 
3751 	case WLC_E_ASSOC_REQ_IE:
3752 		cmd = IWEVASSOCREQIE;
3753 		wrqu.data.length = datalen;
3754 		if (datalen < sizeof(extra))
3755 			memcpy(extra, data, datalen);
3756 		break;
3757 
3758 	case WLC_E_ASSOC_RESP_IE:
3759 		cmd = IWEVASSOCRESPIE;
3760 		wrqu.data.length = datalen;
3761 		if (datalen < sizeof(extra))
3762 			memcpy(extra, data, datalen);
3763 		break;
3764 
3765 	case WLC_E_PMKID_CACHE: {
3766 		struct iw_pmkid_cand *iwpmkidcand = (struct iw_pmkid_cand *)&extra;
3767 		pmkid_cand_list_t *pmkcandlist;
3768 		pmkid_cand_t	*pmkidcand;
3769 		int count;
3770 
3771 		if (data == NULL)
3772 			break;
3773 
3774 		cmd = IWEVPMKIDCAND;
3775 		pmkcandlist = data;
3776 		count = ntoh32_ua((uint8 *)&pmkcandlist->npmkid_cand);
3777 		wrqu.data.length = sizeof(struct iw_pmkid_cand);
3778 		pmkidcand = pmkcandlist->pmkid_cand;
3779 		while (count) {
3780 			bzero(iwpmkidcand, sizeof(struct iw_pmkid_cand));
3781 			if (pmkidcand->preauth)
3782 				iwpmkidcand->flags |= IW_PMKID_CAND_PREAUTH;
3783 			bcopy(&pmkidcand->BSSID, &iwpmkidcand->bssid.sa_data,
3784 			      ETHER_ADDR_LEN);
3785 			wireless_send_event(dev, cmd, &wrqu, extra);
3786 			pmkidcand++;
3787 			count--;
3788 		}
3789 		break;
3790 	}
3791 #endif /* WIRELESS_EXT > 17 */
3792 
3793 #ifndef WL_ESCAN
3794 	case WLC_E_SCAN_COMPLETE:
3795 #if WIRELESS_EXT > 14
3796 		cmd = SIOCGIWSCAN;
3797 #endif
3798 		WL_TRACE(("event WLC_E_SCAN_COMPLETE\n"));
3799 		// terence 20150224: fix "wlan0: (WE) : Wireless Event too big (65306)"
3800 		memset(&wrqu, 0, sizeof(wrqu));
3801 		if ((iscan) && (iscan->sysioc_pid >= 0) &&
3802 			(iscan->iscan_state != ISCAN_STATE_IDLE))
3803 			up(&iscan->sysioc_sem);
3804 		break;
3805 #endif
3806 
3807 	default:
3808 		/* Cannot translate event */
3809 		break;
3810 	}
3811 
3812 	if (cmd) {
3813 #ifndef WL_ESCAN
3814 		if (cmd == SIOCGIWSCAN) {
3815 			if ((!iscan) || (iscan->sysioc_pid < 0)) {
3816 				wireless_send_event(dev, cmd, &wrqu, NULL);
3817 			}
3818 		} else
3819 #endif
3820 			wireless_send_event(dev, cmd, &wrqu, extra);
3821 	}
3822 
3823 #if WIRELESS_EXT > 14
3824 	/* Look for WLC events that indicate a connection failure.
3825 	 * If found, generate an IWEVCUSTOM event.
3826 	 */
3827 	memset(extra, 0, sizeof(extra));
3828 	if (wl_iw_check_conn_fail(e, extra, sizeof(extra))) {
3829 		cmd = IWEVCUSTOM;
3830 		wrqu.data.length = strlen(extra);
3831 		wireless_send_event(dev, cmd, &wrqu, extra);
3832 	}
3833 #endif /* WIRELESS_EXT > 14 */
3834 
3835 #endif /* WIRELESS_EXT > 13 */
3836 }
3837 
3838 #ifdef WL_NAN
wl_iw_get_wireless_stats_cbfn(void * ctx,const uint8 * data,uint16 type,uint16 len)3839 static int wl_iw_get_wireless_stats_cbfn(void *ctx, const uint8 *data, uint16 type, uint16 len)
3840 {
3841 	struct iw_statistics *wstats = ctx;
3842 	int res = BCME_OK;
3843 
3844 	switch (type) {
3845 		case WL_CNT_XTLV_WLC: {
3846 			wl_cnt_wlc_t *cnt = (wl_cnt_wlc_t *)data;
3847 			if (len > sizeof(wl_cnt_wlc_t)) {
3848 				printf("counter structure length invalid! %d > %d\n",
3849 					len, (int)sizeof(wl_cnt_wlc_t));
3850 			}
3851 			wstats->discard.nwid = 0;
3852 			wstats->discard.code = dtoh32(cnt->rxundec);
3853 			wstats->discard.fragment = dtoh32(cnt->rxfragerr);
3854 			wstats->discard.retries = dtoh32(cnt->txfail);
3855 			wstats->discard.misc = dtoh32(cnt->rxrunt) + dtoh32(cnt->rxgiant);
3856 			wstats->miss.beacon = 0;
3857 			WL_TRACE(("wl_iw_get_wireless_stats counters txframe=%d txbyte=%d\n",
3858 				dtoh32(cnt->txframe), dtoh32(cnt->txbyte)));
3859 			WL_TRACE(("wl_iw_get_wireless_stats counters rxundec=%d\n",
3860 				dtoh32(cnt->rxundec)));
3861 			WL_TRACE(("wl_iw_get_wireless_stats counters txfail=%d\n",
3862 				dtoh32(cnt->txfail)));
3863 			WL_TRACE(("wl_iw_get_wireless_stats counters rxfragerr=%d\n",
3864 				dtoh32(cnt->rxfragerr)));
3865 			WL_TRACE(("wl_iw_get_wireless_stats counters rxrunt=%d\n",
3866 				dtoh32(cnt->rxrunt)));
3867 			WL_TRACE(("wl_iw_get_wireless_stats counters rxgiant=%d\n",
3868 				dtoh32(cnt->rxgiant)));
3869 			break;
3870 		}
3871 		case WL_CNT_XTLV_CNTV_LE10_UCODE:
3872 		case WL_CNT_XTLV_LT40_UCODE_V1:
3873 		case WL_CNT_XTLV_GE40_UCODE_V1:
3874 		{
3875 			/* Offsets of rxfrmtoolong and rxbadplcp are the same in
3876 			 * wl_cnt_v_le10_mcst_t, wl_cnt_lt40mcst_v1_t, and wl_cnt_ge40mcst_v1_t.
3877 			 * So we can just cast to wl_cnt_v_le10_mcst_t here.
3878 			 */
3879 			wl_cnt_v_le10_mcst_t *cnt = (wl_cnt_v_le10_mcst_t *)data;
3880 			if (len != WL_CNT_MCST_STRUCT_SZ) {
3881 				printf("counter structure length mismatch! %d != %d\n",
3882 					len, WL_CNT_MCST_STRUCT_SZ);
3883 			}
3884 			WL_TRACE(("wl_iw_get_wireless_stats counters rxfrmtoolong=%d\n",
3885 				dtoh32(cnt->rxfrmtoolong)));
3886 			WL_TRACE(("wl_iw_get_wireless_stats counters rxbadplcp=%d\n",
3887 				dtoh32(cnt->rxbadplcp)));
3888 			BCM_REFERENCE(cnt);
3889 			break;
3890 		}
3891 		default:
3892 			WL_ERROR(("%d: Unsupported type %d\n", __LINE__, type));
3893 			break;
3894 	}
3895 	return res;
3896 }
3897 #endif
3898 
wl_iw_get_wireless_stats(struct net_device * dev,struct iw_statistics * wstats)3899 int wl_iw_get_wireless_stats(struct net_device *dev, struct iw_statistics *wstats)
3900 {
3901 	int res = 0;
3902 	int phy_noise;
3903 	int rssi;
3904 	scb_val_t scb_val;
3905 #if WIRELESS_EXT > 11
3906 #ifdef WL_NAN
3907 	char *cntbuf = NULL;
3908 	wl_cnt_info_t *cntinfo;
3909 	uint16 ver;
3910 	uint32 corerev = 0;
3911 #endif
3912 #endif /* WIRELESS_EXT > 11 */
3913 
3914 	phy_noise = 0;
3915 	if ((res = dev_wlc_ioctl(dev, WLC_GET_PHY_NOISE, &phy_noise, sizeof(phy_noise)))) {
3916 		WL_TRACE(("WLC_GET_PHY_NOISE error=%d\n", res));
3917 		goto done;
3918 	}
3919 
3920 	phy_noise = dtoh32(phy_noise);
3921 	WL_TRACE(("wl_iw_get_wireless_stats phy noise=%d\n *****", phy_noise));
3922 
3923 	memset(&scb_val, 0, sizeof(scb_val));
3924 	if ((res = dev_wlc_ioctl(dev, WLC_GET_RSSI, &scb_val, sizeof(scb_val_t)))) {
3925 		WL_TRACE(("WLC_GET_RSSI error=%d\n", res));
3926 		goto done;
3927 	}
3928 
3929 	rssi = dtoh32(scb_val.val);
3930 	rssi = MIN(rssi, RSSI_MAXVAL);
3931 	WL_TRACE(("wl_iw_get_wireless_stats rssi=%d ****** \n", rssi));
3932 	if (rssi <= WL_IW_RSSI_NO_SIGNAL)
3933 		wstats->qual.qual = 0;
3934 	else if (rssi <= WL_IW_RSSI_VERY_LOW)
3935 		wstats->qual.qual = 1;
3936 	else if (rssi <= WL_IW_RSSI_LOW)
3937 		wstats->qual.qual = 2;
3938 	else if (rssi <= WL_IW_RSSI_GOOD)
3939 		wstats->qual.qual = 3;
3940 	else if (rssi <= WL_IW_RSSI_VERY_GOOD)
3941 		wstats->qual.qual = 4;
3942 	else
3943 		wstats->qual.qual = 5;
3944 
3945 	/* Wraps to 0 if RSSI is 0 */
3946 	wstats->qual.level = 0x100 + rssi;
3947 	wstats->qual.noise = 0x100 + phy_noise;
3948 #if WIRELESS_EXT > 18
3949 	wstats->qual.updated |= (IW_QUAL_ALL_UPDATED | IW_QUAL_DBM);
3950 #else
3951 	wstats->qual.updated |= 7;
3952 #endif /* WIRELESS_EXT > 18 */
3953 
3954 #if WIRELESS_EXT > 11
3955 #ifdef WL_NAN
3956 	WL_TRACE(("wl_iw_get_wireless_stats counters\n *****"));
3957 
3958 	cntbuf = kmalloc(MAX_WLIW_IOCTL_LEN, GFP_KERNEL);
3959 	if (!cntbuf) {
3960 		res = BCME_NOMEM;
3961 		goto done;
3962 	}
3963 
3964 	memset(cntbuf, 0, MAX_WLIW_IOCTL_LEN);
3965 	res = dev_wlc_bufvar_get(dev, "counters", cntbuf, MAX_WLIW_IOCTL_LEN);
3966 	if (res)
3967 	{
3968 		WL_ERROR(("wl_iw_get_wireless_stats counters failed error=%d ****** \n", res));
3969 		goto done;
3970 	}
3971 
3972 	cntinfo = (wl_cnt_info_t *)cntbuf;
3973 	cntinfo->version = dtoh16(cntinfo->version);
3974 	cntinfo->datalen = dtoh16(cntinfo->datalen);
3975 	ver = cntinfo->version;
3976 	CHK_CNTBUF_DATALEN(cntbuf, MAX_WLIW_IOCTL_LEN);
3977 	if (ver > WL_CNT_VERSION_XTLV) {
3978 		WL_TRACE(("\tIncorrect version of counters struct: expected %d; got %d\n",
3979 			WL_CNT_VERSION_XTLV, ver));
3980 		res = BCME_VERSION;
3981 		goto done;
3982 	}
3983 
3984 	if (ver == WL_CNT_VERSION_11) {
3985 		wlc_rev_info_t revinfo;
3986 		memset(&revinfo, 0, sizeof(revinfo));
3987 		res = dev_wlc_ioctl(dev, WLC_GET_REVINFO, &revinfo, sizeof(revinfo));
3988 		if (res) {
3989 			WL_ERROR(("WLC_GET_REVINFO failed %d\n", res));
3990 			goto done;
3991 		}
3992 		corerev = dtoh32(revinfo.corerev);
3993 	}
3994 
3995 	res = wl_cntbuf_to_xtlv_format(NULL, cntinfo, MAX_WLIW_IOCTL_LEN, corerev);
3996 	if (res) {
3997 		WL_ERROR(("wl_cntbuf_to_xtlv_format failed %d\n", res));
3998 		goto done;
3999 	}
4000 
4001 	if ((res = bcm_unpack_xtlv_buf(wstats, cntinfo->data, cntinfo->datalen,
4002 		BCM_XTLV_OPTION_ALIGN32, wl_iw_get_wireless_stats_cbfn))) {
4003 		goto done;
4004 	}
4005 #endif
4006 #endif /* WIRELESS_EXT > 11 */
4007 
4008 done:
4009 #if WIRELESS_EXT > 11
4010 #ifdef WL_NAN
4011 	if (cntbuf) {
4012 		kfree(cntbuf);
4013 	}
4014 #endif
4015 #endif /* WIRELESS_EXT > 11 */
4016 	return res;
4017 }
4018 
4019 #ifndef WL_ESCAN
4020 static void
wl_iw_timerfunc(ulong data)4021 wl_iw_timerfunc(ulong data)
4022 {
4023 	iscan_info_t *iscan = (iscan_info_t *)data;
4024 	iscan->timer_on = 0;
4025 	if (iscan->iscan_state != ISCAN_STATE_IDLE) {
4026 		WL_TRACE(("timer trigger\n"));
4027 		up(&iscan->sysioc_sem);
4028 	}
4029 }
4030 
4031 static void
wl_iw_set_event_mask(struct net_device * dev)4032 wl_iw_set_event_mask(struct net_device *dev)
4033 {
4034 	char eventmask[WL_EVENTING_MASK_LEN];
4035 	char iovbuf[WL_EVENTING_MASK_LEN + 12];	/* Room for "event_msgs" + '\0' + bitvec */
4036 
4037 	dev_iw_iovar_getbuf(dev, "event_msgs", "", 0, iovbuf, sizeof(iovbuf));
4038 	bcopy(iovbuf, eventmask, WL_EVENTING_MASK_LEN);
4039 	setbit(eventmask, WLC_E_SCAN_COMPLETE);
4040 	dev_iw_iovar_setbuf(dev, "event_msgs", eventmask, WL_EVENTING_MASK_LEN,
4041 		iovbuf, sizeof(iovbuf));
4042 
4043 }
4044 
4045 static int
wl_iw_iscan_prep(wl_scan_params_v1_t * params,wlc_ssid_t * ssid)4046 wl_iw_iscan_prep(wl_scan_params_v1_t *params, wlc_ssid_t *ssid)
4047 {
4048 	int err = 0;
4049 
4050 	memcpy(&params->bssid, &ether_bcast, ETHER_ADDR_LEN);
4051 	params->bss_type = DOT11_BSSTYPE_ANY;
4052 	params->scan_type = 0;
4053 	params->nprobes = -1;
4054 	params->active_time = -1;
4055 	params->passive_time = -1;
4056 	params->home_time = -1;
4057 	params->channel_num = 0;
4058 
4059 	params->nprobes = htod32(params->nprobes);
4060 	params->active_time = htod32(params->active_time);
4061 	params->passive_time = htod32(params->passive_time);
4062 	params->home_time = htod32(params->home_time);
4063 	if (ssid && ssid->SSID_len)
4064 		memcpy(&params->ssid, ssid, sizeof(wlc_ssid_t));
4065 
4066 	return err;
4067 }
4068 
4069 static int
wl_iw_iscan(iscan_info_t * iscan,wlc_ssid_t * ssid,uint16 action)4070 wl_iw_iscan(iscan_info_t *iscan, wlc_ssid_t *ssid, uint16 action)
4071 {
4072 	int params_size = (WL_SCAN_PARAMS_V1_FIXED_SIZE + OFFSETOF(wl_iscan_params_v1_t, params));
4073 	wl_iscan_params_v1_t *params;
4074 	int err = 0;
4075 
4076 	if (ssid && ssid->SSID_len) {
4077 		params_size += sizeof(wlc_ssid_t);
4078 	}
4079 	params = (wl_iscan_params_v1_t*)kmalloc(params_size, GFP_KERNEL);
4080 	if (params == NULL) {
4081 		return -ENOMEM;
4082 	}
4083 	memset(params, 0, params_size);
4084 	ASSERT(params_size < WLC_IOCTL_SMLEN);
4085 
4086 	err = wl_iw_iscan_prep(&params->params, ssid);
4087 
4088 	if (!err) {
4089 		params->version = htod32(ISCAN_REQ_VERSION);
4090 		params->action = htod16(action);
4091 		params->scan_duration = htod16(0);
4092 
4093 		/* params_size += OFFSETOF(wl_iscan_params_v1_t, params); */
4094 		(void) dev_iw_iovar_setbuf(iscan->dev, "iscan", params, params_size,
4095 			iscan->ioctlbuf, WLC_IOCTL_SMLEN);
4096 	}
4097 
4098 	kfree(params);
4099 	return err;
4100 }
4101 
4102 static uint32
wl_iw_iscan_get(iscan_info_t * iscan)4103 wl_iw_iscan_get(iscan_info_t *iscan)
4104 {
4105 	iscan_buf_t * buf;
4106 	iscan_buf_t * ptr;
4107 	wl_iscan_results_t * list_buf;
4108 	wl_iscan_results_t list;
4109 	wl_scan_results_v109_t *results;
4110 	uint32 status;
4111 
4112 	/* buffers are allocated on demand */
4113 	if (iscan->list_cur) {
4114 		buf = iscan->list_cur;
4115 		iscan->list_cur = buf->next;
4116 	}
4117 	else {
4118 		buf = kmalloc(sizeof(iscan_buf_t), GFP_KERNEL);
4119 		if (!buf)
4120 			return WL_SCAN_RESULTS_ABORTED;
4121 		buf->next = NULL;
4122 		if (!iscan->list_hdr)
4123 			iscan->list_hdr = buf;
4124 		else {
4125 			ptr = iscan->list_hdr;
4126 			while (ptr->next) {
4127 				ptr = ptr->next;
4128 			}
4129 			ptr->next = buf;
4130 		}
4131 	}
4132 	memset(buf->iscan_buf, 0, WLC_IW_ISCAN_MAXLEN);
4133 	list_buf = (wl_iscan_results_t*)buf->iscan_buf;
4134 	results = &list_buf->results;
4135 	results->buflen = WL_ISCAN_RESULTS_FIXED_SIZE;
4136 	results->version = 0;
4137 	results->count = 0;
4138 
4139 	memset(&list, 0, sizeof(list));
4140 	list.results.buflen = htod32(WLC_IW_ISCAN_MAXLEN);
4141 	(void) dev_iw_iovar_getbuf(
4142 		iscan->dev,
4143 		"iscanresults",
4144 		&list,
4145 		WL_ISCAN_RESULTS_FIXED_SIZE,
4146 		buf->iscan_buf,
4147 		WLC_IW_ISCAN_MAXLEN);
4148 	results->buflen = dtoh32(results->buflen);
4149 	results->version = dtoh32(results->version);
4150 	results->count = dtoh32(results->count);
4151 	WL_TRACE(("results->count = %d\n", results->count));
4152 
4153 	WL_TRACE(("results->buflen = %d\n", results->buflen));
4154 	status = dtoh32(list_buf->status);
4155 	return status;
4156 }
4157 
wl_iw_send_scan_complete(iscan_info_t * iscan)4158 static void wl_iw_send_scan_complete(iscan_info_t *iscan)
4159 {
4160 	union iwreq_data wrqu;
4161 
4162 	memset(&wrqu, 0, sizeof(wrqu));
4163 
4164 	/* wext expects to get no data for SIOCGIWSCAN Event  */
4165 	wireless_send_event(iscan->dev, SIOCGIWSCAN, &wrqu, NULL);
4166 }
4167 
4168 static int
_iscan_sysioc_thread(void * data)4169 _iscan_sysioc_thread(void *data)
4170 {
4171 	uint32 status;
4172 	iscan_info_t *iscan = (iscan_info_t *)data;
4173 
4174 	WL_MSG("wlan", "thread Enter\n");
4175 	DAEMONIZE("iscan_sysioc");
4176 
4177 	status = WL_SCAN_RESULTS_PARTIAL;
4178 	while (down_interruptible(&iscan->sysioc_sem) == 0) {
4179 		if (iscan->timer_on) {
4180 			del_timer(&iscan->timer);
4181 			iscan->timer_on = 0;
4182 		}
4183 
4184 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4185 		rtnl_lock();
4186 #endif
4187 		status = wl_iw_iscan_get(iscan);
4188 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4189 		rtnl_unlock();
4190 #endif
4191 
4192 		switch (status) {
4193 			case WL_SCAN_RESULTS_PARTIAL:
4194 				WL_TRACE(("iscanresults incomplete\n"));
4195 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4196 				rtnl_lock();
4197 #endif
4198 				/* make sure our buffer size is enough before going next round */
4199 				wl_iw_iscan(iscan, NULL, WL_SCAN_ACTION_CONTINUE);
4200 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4201 				rtnl_unlock();
4202 #endif
4203 				/* Reschedule the timer */
4204 				iscan->timer.expires = jiffies + msecs_to_jiffies(iscan->timer_ms);
4205 				add_timer(&iscan->timer);
4206 				iscan->timer_on = 1;
4207 				break;
4208 			case WL_SCAN_RESULTS_SUCCESS:
4209 				WL_TRACE(("iscanresults complete\n"));
4210 				iscan->iscan_state = ISCAN_STATE_IDLE;
4211 				wl_iw_send_scan_complete(iscan);
4212 				break;
4213 			case WL_SCAN_RESULTS_PENDING:
4214 				WL_TRACE(("iscanresults pending\n"));
4215 				/* Reschedule the timer */
4216 				iscan->timer.expires = jiffies + msecs_to_jiffies(iscan->timer_ms);
4217 				add_timer(&iscan->timer);
4218 				iscan->timer_on = 1;
4219 				break;
4220 			case WL_SCAN_RESULTS_ABORTED:
4221 				WL_TRACE(("iscanresults aborted\n"));
4222 				iscan->iscan_state = ISCAN_STATE_IDLE;
4223 				wl_iw_send_scan_complete(iscan);
4224 				break;
4225 			default:
4226 				WL_TRACE(("iscanresults returned unknown status %d\n", status));
4227 				break;
4228 		 }
4229 	}
4230 	WL_MSG("wlan", "was terminated\n");
4231 	complete_and_exit(&iscan->sysioc_exited, 0);
4232 }
4233 #endif /* !WL_ESCAN */
4234 
4235 void
wl_iw_detach(struct net_device * dev)4236 wl_iw_detach(struct net_device *dev)
4237 {
4238 	struct dhd_pub *dhdp = dhd_get_pub(dev);
4239 	wl_wext_info_t *wext_info = dhdp->wext_info;
4240 #ifndef WL_ESCAN
4241 	iscan_buf_t  *buf;
4242 	iscan_info_t *iscan;
4243 #endif
4244 	if (!wext_info)
4245 		return;
4246 
4247 #ifndef WL_ESCAN
4248 	iscan = &wext_info->iscan;
4249 	if (iscan->sysioc_pid >= 0) {
4250 		KILL_PROC(iscan->sysioc_pid, SIGTERM);
4251 		wait_for_completion(&iscan->sysioc_exited);
4252 	}
4253 
4254 	while (iscan->list_hdr) {
4255 		buf = iscan->list_hdr->next;
4256 		kfree(iscan->list_hdr);
4257 		iscan->list_hdr = buf;
4258 	}
4259 #endif
4260 	wl_ext_event_deregister(dev, dhdp, WLC_E_LAST, wl_iw_event);
4261 	if (wext_info) {
4262 		kfree(wext_info);
4263 		dhdp->wext_info = NULL;
4264 	}
4265 }
4266 
4267 int
wl_iw_attach(struct net_device * dev)4268 wl_iw_attach(struct net_device *dev)
4269 {
4270 	struct dhd_pub *dhdp = dhd_get_pub(dev);
4271 	wl_wext_info_t *wext_info = NULL;
4272 	int ret = 0;
4273 #ifndef WL_ESCAN
4274 	iscan_info_t *iscan = NULL;
4275 #endif
4276 
4277 	if (!dev)
4278 		return 0;
4279 	WL_TRACE(("Enter\n"));
4280 
4281 	wext_info = (void *)kzalloc(sizeof(struct wl_wext_info), GFP_KERNEL);
4282 	if (!wext_info)
4283 		return -ENOMEM;
4284 	memset(wext_info, 0, sizeof(wl_wext_info_t));
4285 	wext_info->dev = dev;
4286 	wext_info->dhd = dhdp;
4287 	wext_info->conn_info.bssidx = 0;
4288 	dhdp->wext_info = (void *)wext_info;
4289 
4290 #ifndef WL_ESCAN
4291 	iscan = &wext_info->iscan;
4292 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
4293 	iscan->kthread = NULL;
4294 #endif
4295 	iscan->sysioc_pid = -1;
4296 	/* we only care about main interface so save a global here */
4297 	iscan->dev = dev;
4298 	iscan->iscan_state = ISCAN_STATE_IDLE;
4299 
4300 	/* Set up the timer */
4301 	iscan->timer_ms    = 2000;
4302 	init_timer_compat(&iscan->timer, wl_iw_timerfunc, iscan);
4303 
4304 	sema_init(&iscan->sysioc_sem, 0);
4305 	init_completion(&iscan->sysioc_exited);
4306 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
4307 	iscan->kthread = kthread_run(_iscan_sysioc_thread, iscan, "iscan_sysioc");
4308 	iscan->sysioc_pid = iscan->kthread->pid;
4309 #else
4310 	iscan->sysioc_pid = kernel_thread(_iscan_sysioc_thread, iscan, 0);
4311 #endif
4312 	if (iscan->sysioc_pid < 0) {
4313 		ret = -ENOMEM;
4314 		goto exit;
4315 	}
4316 #endif
4317 	ret = wl_ext_event_register(dev, dhdp, WLC_E_LAST, wl_iw_event, dhdp->wext_info,
4318 		PRIO_EVENT_WEXT);
4319 	if (ret) {
4320 		WL_ERROR(("wl_ext_event_register err %d\n", ret));
4321 		goto exit;
4322 	}
4323 
4324 	return ret;
4325 exit:
4326 	wl_iw_detach(dev);
4327 	return ret;
4328 }
4329 
4330 s32
wl_iw_autochannel(struct net_device * dev,char * command,int total_len)4331 wl_iw_autochannel(struct net_device *dev, char* command, int total_len)
4332 {
4333 	struct dhd_pub *dhd = dhd_get_pub(dev);
4334 	int ret = 0;
4335 #ifdef WL_ESCAN
4336 	int bytes_written = -1;
4337 #endif
4338 
4339 	DHD_CHECK(dhd, dev);
4340 #ifdef WL_ESCAN
4341 	sscanf(command, "%*s %d", &dhd->escan->autochannel);
4342 	if (dhd->escan->autochannel == 0) {
4343 		dhd->escan->best_2g_ch = 0;
4344 		dhd->escan->best_5g_ch = 0;
4345 	} else if (dhd->escan->autochannel == 2) {
4346 		bytes_written = snprintf(command, total_len, "2g=%d 5g=%d",
4347 			dhd->escan->best_2g_ch, dhd->escan->best_5g_ch);
4348 		WL_TRACE(("command result is %s\n", command));
4349 		ret = bytes_written;
4350 	}
4351 #endif
4352 
4353 	return ret;
4354 }
4355 
4356 #endif /* USE_IW */
4357