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