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