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