OSDN Git Service

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