]> Pileus Git - ~andy/linux/blob - drivers/staging/ath6kl/os/linux/cfg80211.c
Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[~andy/linux] / drivers / staging / ath6kl / os / linux / cfg80211.c
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2004-2010 Atheros Communications Inc.
3 // All rights reserved.
4 //
5 // 
6 //
7 // Permission to use, copy, modify, and/or distribute this software for any
8 // purpose with or without fee is hereby granted, provided that the above
9 // copyright notice and this permission notice appear in all copies.
10 //
11 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 //
19 //
20 //
21 // Author(s): ="Atheros"
22 //------------------------------------------------------------------------------
23
24 #include <linux/wireless.h>
25 #include <linux/ieee80211.h>
26 #include <net/cfg80211.h>
27 #include <net/netlink.h>
28
29 #include "ar6000_drv.h"
30
31
32 extern A_WAITQUEUE_HEAD arEvent;
33 extern unsigned int wmitimeout;
34 extern int reconnect_flag;
35
36
37 #define RATETAB_ENT(_rate, _rateid, _flags) {   \
38     .bitrate    = (_rate),                  \
39     .flags      = (_flags),                 \
40     .hw_value   = (_rateid),                \
41 }
42
43 #define CHAN2G(_channel, _freq, _flags) {   \
44     .band           = IEEE80211_BAND_2GHZ,  \
45     .hw_value       = (_channel),           \
46     .center_freq    = (_freq),              \
47     .flags          = (_flags),             \
48     .max_antenna_gain   = 0,                \
49     .max_power      = 30,                   \
50 }
51
52 #define CHAN5G(_channel, _flags) {              \
53     .band           = IEEE80211_BAND_5GHZ,      \
54     .hw_value       = (_channel),               \
55     .center_freq    = 5000 + (5 * (_channel)),  \
56     .flags          = (_flags),                 \
57     .max_antenna_gain   = 0,                    \
58     .max_power      = 30,                       \
59 }
60
61 static struct
62 ieee80211_rate ar6k_rates[] = {
63     RATETAB_ENT(10,  0x1,   0),
64     RATETAB_ENT(20,  0x2,   0),
65     RATETAB_ENT(55,  0x4,   0),
66     RATETAB_ENT(110, 0x8,   0),
67     RATETAB_ENT(60,  0x10,  0),
68     RATETAB_ENT(90,  0x20,  0),
69     RATETAB_ENT(120, 0x40,  0),
70     RATETAB_ENT(180, 0x80,  0),
71     RATETAB_ENT(240, 0x100, 0),
72     RATETAB_ENT(360, 0x200, 0),
73     RATETAB_ENT(480, 0x400, 0),
74     RATETAB_ENT(540, 0x800, 0),
75 };
76
77 #define ar6k_a_rates     (ar6k_rates + 4)
78 #define ar6k_a_rates_size    8
79 #define ar6k_g_rates     (ar6k_rates + 0)
80 #define ar6k_g_rates_size    12
81
82 static struct
83 ieee80211_channel ar6k_2ghz_channels[] = {
84     CHAN2G(1, 2412, 0),
85     CHAN2G(2, 2417, 0),
86     CHAN2G(3, 2422, 0),
87     CHAN2G(4, 2427, 0),
88     CHAN2G(5, 2432, 0),
89     CHAN2G(6, 2437, 0),
90     CHAN2G(7, 2442, 0),
91     CHAN2G(8, 2447, 0),
92     CHAN2G(9, 2452, 0),
93     CHAN2G(10, 2457, 0),
94     CHAN2G(11, 2462, 0),
95     CHAN2G(12, 2467, 0),
96     CHAN2G(13, 2472, 0),
97     CHAN2G(14, 2484, 0),
98 };
99
100 static struct
101 ieee80211_channel ar6k_5ghz_a_channels[] = {
102     CHAN5G(34, 0),      CHAN5G(36, 0),
103     CHAN5G(38, 0),      CHAN5G(40, 0),
104     CHAN5G(42, 0),      CHAN5G(44, 0),
105     CHAN5G(46, 0),      CHAN5G(48, 0),
106     CHAN5G(52, 0),      CHAN5G(56, 0),
107     CHAN5G(60, 0),      CHAN5G(64, 0),
108     CHAN5G(100, 0),     CHAN5G(104, 0),
109     CHAN5G(108, 0),     CHAN5G(112, 0),
110     CHAN5G(116, 0),     CHAN5G(120, 0),
111     CHAN5G(124, 0),     CHAN5G(128, 0),
112     CHAN5G(132, 0),     CHAN5G(136, 0),
113     CHAN5G(140, 0),     CHAN5G(149, 0),
114     CHAN5G(153, 0),     CHAN5G(157, 0),
115     CHAN5G(161, 0),     CHAN5G(165, 0),
116     CHAN5G(184, 0),     CHAN5G(188, 0),
117     CHAN5G(192, 0),     CHAN5G(196, 0),
118     CHAN5G(200, 0),     CHAN5G(204, 0),
119     CHAN5G(208, 0),     CHAN5G(212, 0),
120     CHAN5G(216, 0),
121 };
122
123 static struct
124 ieee80211_supported_band ar6k_band_2ghz = {
125     .n_channels = ARRAY_SIZE(ar6k_2ghz_channels),
126     .channels = ar6k_2ghz_channels,
127     .n_bitrates = ar6k_g_rates_size,
128     .bitrates = ar6k_g_rates,
129 };
130
131 static struct
132 ieee80211_supported_band ar6k_band_5ghz = {
133     .n_channels = ARRAY_SIZE(ar6k_5ghz_a_channels),
134     .channels = ar6k_5ghz_a_channels,
135     .n_bitrates = ar6k_a_rates_size,
136     .bitrates = ar6k_a_rates,
137 };
138
139 static int
140 ar6k_set_wpa_version(struct ar6_softc *ar, enum nl80211_wpa_versions wpa_version)
141 {
142
143     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: %u\n", __func__, wpa_version));
144
145     if (!wpa_version) {
146         ar->arAuthMode = NONE_AUTH;
147     } else if (wpa_version & NL80211_WPA_VERSION_1) {
148         ar->arAuthMode = WPA_AUTH;
149     } else if (wpa_version & NL80211_WPA_VERSION_2) {
150         ar->arAuthMode = WPA2_AUTH;
151     } else {
152         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
153                         ("%s: %u not spported\n", __func__, wpa_version));
154         return -ENOTSUPP;
155     }
156
157     return 0;
158 }
159
160 static int
161 ar6k_set_auth_type(struct ar6_softc *ar, enum nl80211_auth_type auth_type)
162 {
163
164     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: 0x%x\n", __func__, auth_type));
165
166     switch (auth_type) {
167     case NL80211_AUTHTYPE_OPEN_SYSTEM:
168         ar->arDot11AuthMode = OPEN_AUTH;
169         break;
170     case NL80211_AUTHTYPE_SHARED_KEY:
171         ar->arDot11AuthMode = SHARED_AUTH;
172         break;
173     case NL80211_AUTHTYPE_NETWORK_EAP:
174         ar->arDot11AuthMode = LEAP_AUTH;
175         break;
176
177     case NL80211_AUTHTYPE_AUTOMATIC:
178         ar->arDot11AuthMode = OPEN_AUTH;
179         ar->arAutoAuthStage = AUTH_OPEN_IN_PROGRESS;
180         break;
181
182     default:
183         ar->arDot11AuthMode = OPEN_AUTH;
184         AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
185                         ("%s: 0x%x not spported\n", __func__, auth_type));
186         return -ENOTSUPP;
187     }
188
189     return 0;
190 }
191
192 static int
193 ar6k_set_cipher(struct ar6_softc *ar, u32 cipher, bool ucast)
194 {
195     u8 *ar_cipher = ucast ? &ar->arPairwiseCrypto :
196                                 &ar->arGroupCrypto;
197     u8 *ar_cipher_len = ucast ? &ar->arPairwiseCryptoLen :
198                                     &ar->arGroupCryptoLen;
199
200     AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
201                     ("%s: cipher 0x%x, ucast %u\n", __func__, cipher, ucast));
202
203     switch (cipher) {
204     case 0:
205     case IW_AUTH_CIPHER_NONE:
206         *ar_cipher = NONE_CRYPT;
207         *ar_cipher_len = 0;
208         break;
209     case WLAN_CIPHER_SUITE_WEP40:
210         *ar_cipher = WEP_CRYPT;
211         *ar_cipher_len = 5;
212         break;
213     case WLAN_CIPHER_SUITE_WEP104:
214         *ar_cipher = WEP_CRYPT;
215         *ar_cipher_len = 13;
216         break;
217     case WLAN_CIPHER_SUITE_TKIP:
218         *ar_cipher = TKIP_CRYPT;
219         *ar_cipher_len = 0;
220         break;
221     case WLAN_CIPHER_SUITE_CCMP:
222         *ar_cipher = AES_CRYPT;
223         *ar_cipher_len = 0;
224         break;
225     default:
226         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
227                         ("%s: cipher 0x%x not supported\n", __func__, cipher));
228         return -ENOTSUPP;
229     }
230
231     return 0;
232 }
233
234 static void
235 ar6k_set_key_mgmt(struct ar6_softc *ar, u32 key_mgmt)
236 {
237     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: 0x%x\n", __func__, key_mgmt));
238
239     if (WLAN_AKM_SUITE_PSK == key_mgmt) {
240         if (WPA_AUTH == ar->arAuthMode) {
241             ar->arAuthMode = WPA_PSK_AUTH;
242         } else if (WPA2_AUTH == ar->arAuthMode) {
243             ar->arAuthMode = WPA2_PSK_AUTH;
244         }
245     } else if (WLAN_AKM_SUITE_8021X != key_mgmt) {
246         ar->arAuthMode = NONE_AUTH;
247     }
248 }
249
250 static int
251 ar6k_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
252                       struct cfg80211_connect_params *sme)
253 {
254     struct ar6_softc *ar = ar6k_priv(dev);
255     int status;
256
257     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
258     ar->smeState = SME_CONNECTING;
259
260     if(ar->arWmiReady == false) {
261         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready yet\n", __func__));
262         return -EIO;
263     }
264
265     if(ar->arWlanState == WLAN_DISABLED) {
266         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
267         return -EIO;
268     }
269
270     if(ar->bIsDestroyProgress) {
271         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: destroy in progress\n", __func__));
272         return -EBUSY;
273     }
274
275     if(!sme->ssid_len || IEEE80211_MAX_SSID_LEN < sme->ssid_len) {
276         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: ssid invalid\n", __func__));
277         return -EINVAL;
278     }
279
280     if(ar->arSkipScan == true &&
281        ((sme->channel && sme->channel->center_freq == 0) ||
282         (sme->bssid && !sme->bssid[0] && !sme->bssid[1] && !sme->bssid[2] &&
283          !sme->bssid[3] && !sme->bssid[4] && !sme->bssid[5])))
284     {
285         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s:SkipScan: channel or bssid invalid\n", __func__));
286         return -EINVAL;
287     }
288
289     if(down_interruptible(&ar->arSem)) {
290         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: busy, couldn't get access\n", __func__));
291         return -ERESTARTSYS;
292     }
293
294     if(ar->bIsDestroyProgress) {
295         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: busy, destroy in progress\n", __func__));
296         up(&ar->arSem);
297         return -EBUSY;
298     }
299
300     if(ar->arTxPending[wmi_get_control_ep(ar->arWmi)]) {
301         /*
302         * sleep until the command queue drains
303         */
304         wait_event_interruptible_timeout(arEvent,
305         ar->arTxPending[wmi_get_control_ep(ar->arWmi)] == 0, wmitimeout * HZ);
306         if (signal_pending(current)) {
307             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: cmd queue drain timeout\n", __func__));
308             up(&ar->arSem);
309             return -EINTR;
310         }
311     }
312
313     if(ar->arConnected == true &&
314        ar->arSsidLen == sme->ssid_len &&
315        !memcmp(ar->arSsid, sme->ssid, ar->arSsidLen)) {
316         reconnect_flag = true;
317         status = wmi_reconnect_cmd(ar->arWmi,
318                                    ar->arReqBssid,
319                                    ar->arChannelHint);
320
321         up(&ar->arSem);
322         if (status) {
323             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: wmi_reconnect_cmd failed\n", __func__));
324             return -EIO;
325         }
326         return 0;
327     } else if(ar->arSsidLen == sme->ssid_len &&
328               !memcmp(ar->arSsid, sme->ssid, ar->arSsidLen)) {
329             ar6000_disconnect(ar);
330     }
331
332     A_MEMZERO(ar->arSsid, sizeof(ar->arSsid));
333     ar->arSsidLen = sme->ssid_len;
334     memcpy(ar->arSsid, sme->ssid, sme->ssid_len);
335
336     if(sme->channel){
337         ar->arChannelHint = sme->channel->center_freq;
338     }
339
340     A_MEMZERO(ar->arReqBssid, sizeof(ar->arReqBssid));
341     if(sme->bssid){
342         if(memcmp(&sme->bssid, bcast_mac, AR6000_ETH_ADDR_LEN)) {
343             memcpy(ar->arReqBssid, sme->bssid, sizeof(ar->arReqBssid));
344         }
345     }
346
347     ar6k_set_wpa_version(ar, sme->crypto.wpa_versions);
348     ar6k_set_auth_type(ar, sme->auth_type);
349
350     if(sme->crypto.n_ciphers_pairwise) {
351         ar6k_set_cipher(ar, sme->crypto.ciphers_pairwise[0], true);
352     } else {
353         ar6k_set_cipher(ar, IW_AUTH_CIPHER_NONE, true);
354     }
355     ar6k_set_cipher(ar, sme->crypto.cipher_group, false);
356
357     if(sme->crypto.n_akm_suites) {
358         ar6k_set_key_mgmt(ar, sme->crypto.akm_suites[0]);
359     }
360
361     if((sme->key_len) &&
362        (NONE_AUTH == ar->arAuthMode) &&
363         (WEP_CRYPT == ar->arPairwiseCrypto)) {
364         struct ar_key *key = NULL;
365
366         if(sme->key_idx < WMI_MIN_KEY_INDEX || sme->key_idx > WMI_MAX_KEY_INDEX) {
367             AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
368                             ("%s: key index %d out of bounds\n", __func__, sme->key_idx));
369             up(&ar->arSem);
370             return -ENOENT;
371         }
372
373         key = &ar->keys[sme->key_idx];
374         key->key_len = sme->key_len;
375         memcpy(key->key, sme->key, key->key_len);
376         key->cipher = ar->arPairwiseCrypto;
377         ar->arDefTxKeyIndex = sme->key_idx;
378
379         wmi_addKey_cmd(ar->arWmi, sme->key_idx,
380                     ar->arPairwiseCrypto,
381                     GROUP_USAGE | TX_USAGE,
382                     key->key_len,
383                     NULL,
384                     key->key, KEY_OP_INIT_VAL, NULL,
385                     NO_SYNC_WMIFLAG);
386     }
387
388     if (!ar->arUserBssFilter) {
389         if (wmi_bssfilter_cmd(ar->arWmi, ALL_BSS_FILTER, 0) != 0) {
390             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Couldn't set bss filtering\n", __func__));
391             up(&ar->arSem);
392             return -EIO;
393         }
394     }
395
396     ar->arNetworkType = ar->arNextMode;
397
398     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: Connect called with authmode %d dot11 auth %d"\
399                     " PW crypto %d PW crypto Len %d GRP crypto %d"\
400                     " GRP crypto Len %d channel hint %u\n",
401                     __func__, ar->arAuthMode, ar->arDot11AuthMode,
402                     ar->arPairwiseCrypto, ar->arPairwiseCryptoLen,
403                     ar->arGroupCrypto, ar->arGroupCryptoLen, ar->arChannelHint));
404
405     reconnect_flag = 0;
406     status = wmi_connect_cmd(ar->arWmi, ar->arNetworkType,
407                             ar->arDot11AuthMode, ar->arAuthMode,
408                             ar->arPairwiseCrypto, ar->arPairwiseCryptoLen,
409                             ar->arGroupCrypto,ar->arGroupCryptoLen,
410                             ar->arSsidLen, ar->arSsid,
411                             ar->arReqBssid, ar->arChannelHint,
412                             ar->arConnectCtrlFlags);
413
414     up(&ar->arSem);
415
416     if (A_EINVAL == status) {
417         A_MEMZERO(ar->arSsid, sizeof(ar->arSsid));
418         ar->arSsidLen = 0;
419         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Invalid request\n", __func__));
420         return -ENOENT;
421     } else if (status) {
422         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: wmi_connect_cmd failed\n", __func__));
423         return -EIO;
424     }
425
426     if ((!(ar->arConnectCtrlFlags & CONNECT_DO_WPA_OFFLOAD)) &&
427         ((WPA_PSK_AUTH == ar->arAuthMode) || (WPA2_PSK_AUTH == ar->arAuthMode)))
428     {
429         A_TIMEOUT_MS(&ar->disconnect_timer, A_DISCONNECT_TIMER_INTERVAL, 0);
430     }
431
432     ar->arConnectCtrlFlags &= ~CONNECT_DO_WPA_OFFLOAD;
433     ar->arConnectPending = true;
434
435     return 0;
436 }
437
438 void
439 ar6k_cfg80211_connect_event(struct ar6_softc *ar, u16 channel,
440                 u8 *bssid, u16 listenInterval,
441                 u16 beaconInterval,NETWORK_TYPE networkType,
442                 u8 beaconIeLen, u8 assocReqLen,
443                 u8 assocRespLen, u8 *assocInfo)
444 {
445     u16 size = 0;
446     u16 capability = 0;
447     struct cfg80211_bss *bss = NULL;
448     struct ieee80211_mgmt *mgmt = NULL;
449     struct ieee80211_channel *ibss_channel = NULL;
450     s32 signal = 50 * 100;
451     u8 ie_buf_len = 0;
452     unsigned char ie_buf[256];
453     unsigned char *ptr_ie_buf = ie_buf;
454     unsigned char *ieeemgmtbuf = NULL;
455     u8 source_mac[ATH_MAC_LEN];
456
457     u8 assocReqIeOffset = sizeof(u16)  +  /* capinfo*/
458                                sizeof(u16);    /* listen interval */
459     u8 assocRespIeOffset = sizeof(u16) +  /* capinfo*/
460                                 sizeof(u16) +  /* status Code */
461                                 sizeof(u16);   /* associd */
462     u8 *assocReqIe = assocInfo + beaconIeLen + assocReqIeOffset;
463     u8 *assocRespIe = assocInfo + beaconIeLen + assocReqLen + assocRespIeOffset;
464
465     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
466
467     assocReqLen -= assocReqIeOffset;
468     assocRespLen -= assocRespIeOffset;
469
470     ar->arAutoAuthStage = AUTH_IDLE;
471
472     if((ADHOC_NETWORK & networkType)) {
473         if(NL80211_IFTYPE_ADHOC != ar->wdev->iftype) {
474             AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
475                             ("%s: ath6k not in ibss mode\n", __func__));
476             return;
477         }
478     }
479
480     if((INFRA_NETWORK & networkType)) {
481         if(NL80211_IFTYPE_STATION != ar->wdev->iftype) {
482             AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
483                             ("%s: ath6k not in station mode\n", __func__));
484             return;
485         }
486     }
487
488     /* Before informing the join/connect event, make sure that
489      * bss entry is present in scan list, if it not present
490      * construct and insert into scan list, otherwise that
491      * event will be dropped on the way by cfg80211, due to
492      * this keys will not be plumbed in case of WEP and
493      * application will not be aware of join/connect status. */
494     bss = cfg80211_get_bss(ar->wdev->wiphy, NULL, bssid,
495                            ar->wdev->ssid, ar->wdev->ssid_len,
496                            ((ADHOC_NETWORK & networkType) ? WLAN_CAPABILITY_IBSS : WLAN_CAPABILITY_ESS),
497                            ((ADHOC_NETWORK & networkType) ? WLAN_CAPABILITY_IBSS : WLAN_CAPABILITY_ESS));
498
499     /*
500      * Earlier we were updating the cfg about bss by making a beacon frame
501      * only if the entry for bss is not there. This can have some issue if
502      * ROAM event is generated and a heavy traffic is ongoing. The ROAM
503      * event is handled through a work queue and by the time it really gets
504      * handled, BSS would have been aged out. So it is better to update the
505      * cfg about BSS irrespective of its entry being present right now or
506      * not.
507      */
508
509     if (ADHOC_NETWORK & networkType) {
510             /* construct 802.11 mgmt beacon */
511             if(ptr_ie_buf) {
512                     *ptr_ie_buf++ = WLAN_EID_SSID;
513                     *ptr_ie_buf++ = ar->arSsidLen;
514                     memcpy(ptr_ie_buf, ar->arSsid, ar->arSsidLen);
515                     ptr_ie_buf +=ar->arSsidLen;
516
517                     *ptr_ie_buf++ = WLAN_EID_IBSS_PARAMS;
518                     *ptr_ie_buf++ = 2; /* length */
519                     *ptr_ie_buf++ = 0; /* ATIM window */
520                     *ptr_ie_buf++ = 0; /* ATIM window */
521
522                     /* TODO: update ibss params and include supported rates,
523                      * DS param set, extened support rates, wmm. */
524
525                     ie_buf_len = ptr_ie_buf - ie_buf;
526             }
527
528             capability |= IEEE80211_CAPINFO_IBSS;
529             if(WEP_CRYPT == ar->arPairwiseCrypto) {
530                     capability |= IEEE80211_CAPINFO_PRIVACY;
531             }
532             memcpy(source_mac, ar->arNetDev->dev_addr, ATH_MAC_LEN);
533             ptr_ie_buf = ie_buf;
534     } else {
535             capability = *(u16 *)(&assocInfo[beaconIeLen]);
536             memcpy(source_mac, bssid, ATH_MAC_LEN);
537             ptr_ie_buf = assocReqIe;
538             ie_buf_len = assocReqLen;
539     }
540
541     size = offsetof(struct ieee80211_mgmt, u)
542             + sizeof(mgmt->u.beacon)
543             + ie_buf_len;
544
545     ieeemgmtbuf = A_MALLOC_NOWAIT(size);
546     if(!ieeemgmtbuf) {
547             AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
548                             ("%s: ieeeMgmtbuf alloc error\n", __func__));
549             cfg80211_put_bss(bss);
550             return;
551     }
552
553     A_MEMZERO(ieeemgmtbuf, size);
554     mgmt = (struct ieee80211_mgmt *)ieeemgmtbuf;
555     mgmt->frame_control = (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
556     memcpy(mgmt->da, bcast_mac, ATH_MAC_LEN);
557     memcpy(mgmt->sa, source_mac, ATH_MAC_LEN);
558     memcpy(mgmt->bssid, bssid, ATH_MAC_LEN);
559     mgmt->u.beacon.beacon_int = beaconInterval;
560     mgmt->u.beacon.capab_info = capability;
561     memcpy(mgmt->u.beacon.variable, ptr_ie_buf, ie_buf_len);
562
563     ibss_channel = ieee80211_get_channel(ar->wdev->wiphy, (int)channel);
564
565     AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
566                     ("%s: inform bss with bssid %pM channel %d beaconInterval %d "
567                      "capability 0x%x\n", __func__, mgmt->bssid,
568                      ibss_channel->hw_value, beaconInterval, capability));
569
570     bss = cfg80211_inform_bss_frame(ar->wdev->wiphy,
571                                     ibss_channel, mgmt,
572                                     le16_to_cpu(size),
573                                     signal, GFP_KERNEL);
574     kfree(ieeemgmtbuf);
575     cfg80211_put_bss(bss);
576
577     if((ADHOC_NETWORK & networkType)) {
578         cfg80211_ibss_joined(ar->arNetDev, bssid, GFP_KERNEL);
579         return;
580     }
581
582     if (false == ar->arConnected) {
583         /* inform connect result to cfg80211 */
584         ar->smeState = SME_DISCONNECTED;
585         cfg80211_connect_result(ar->arNetDev, bssid,
586                                 assocReqIe, assocReqLen,
587                                 assocRespIe, assocRespLen,
588                                 WLAN_STATUS_SUCCESS, GFP_KERNEL);
589     } else {
590         /* inform roam event to cfg80211 */
591         cfg80211_roamed(ar->arNetDev, ibss_channel, bssid,
592                         assocReqIe, assocReqLen,
593                         assocRespIe, assocRespLen,
594                         GFP_KERNEL);
595     }
596 }
597
598 static int
599 ar6k_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
600                         u16 reason_code)
601 {
602     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(dev);
603
604     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: reason=%u\n", __func__, reason_code));
605
606     if(ar->arWmiReady == false) {
607         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
608         return -EIO;
609     }
610
611     if(ar->arWlanState == WLAN_DISABLED) {
612         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
613         return -EIO;
614     }
615
616     if(ar->bIsDestroyProgress) {
617         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: busy, destroy in progress\n", __func__));
618         return -EBUSY;
619     }
620
621     if(down_interruptible(&ar->arSem)) {
622         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: busy, couldn't get access\n", __func__));
623         return -ERESTARTSYS;
624     }
625
626     reconnect_flag = 0;
627     ar6000_disconnect(ar);
628     A_MEMZERO(ar->arSsid, sizeof(ar->arSsid));
629     ar->arSsidLen = 0;
630
631     if (ar->arSkipScan == false) {
632         A_MEMZERO(ar->arReqBssid, sizeof(ar->arReqBssid));
633     }
634
635     up(&ar->arSem);
636
637     return 0;
638 }
639
640 void
641 ar6k_cfg80211_disconnect_event(struct ar6_softc *ar, u8 reason,
642                                u8 *bssid, u8 assocRespLen,
643                                u8 *assocInfo, u16 protocolReasonStatus)
644 {
645
646     u16 status;
647
648     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: reason=%u\n", __func__, reason));
649
650     if (ar->scan_request) {
651         cfg80211_scan_done(ar->scan_request, true);
652         ar->scan_request = NULL;
653     }
654     if((ADHOC_NETWORK & ar->arNetworkType)) {
655         if(NL80211_IFTYPE_ADHOC != ar->wdev->iftype) {
656             AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
657                             ("%s: ath6k not in ibss mode\n", __func__));
658             return;
659         }
660         A_MEMZERO(bssid, ETH_ALEN);
661         cfg80211_ibss_joined(ar->arNetDev, bssid, GFP_KERNEL);
662         return;
663     }
664
665     if((INFRA_NETWORK & ar->arNetworkType)) {
666         if(NL80211_IFTYPE_STATION != ar->wdev->iftype) {
667             AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
668                             ("%s: ath6k not in station mode\n", __func__));
669             return;
670         }
671     }
672
673     if(true == ar->arConnectPending) {
674         if(NO_NETWORK_AVAIL == reason) {
675             /* connect cmd failed */
676             wmi_disconnect_cmd(ar->arWmi);
677         } else if (reason == DISCONNECT_CMD) {
678                 if (ar->arAutoAuthStage) {
679                         /*
680                          * If the current auth algorithm is open try shared
681                          * and make autoAuthStage idle. We do not make it
682                          * leap for now being.
683                          */
684                         if (ar->arDot11AuthMode == OPEN_AUTH) {
685                                 struct ar_key *key = NULL;
686                                 key = &ar->keys[ar->arDefTxKeyIndex];
687                                 if (down_interruptible(&ar->arSem)) {
688                                         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: busy, couldn't get access\n", __func__));
689                                         return;
690                                 }
691
692
693                                 ar->arDot11AuthMode = SHARED_AUTH;
694                                 ar->arAutoAuthStage = AUTH_IDLE;
695
696                                 wmi_addKey_cmd(ar->arWmi, ar->arDefTxKeyIndex,
697                                                 ar->arPairwiseCrypto,
698                                                 GROUP_USAGE | TX_USAGE,
699                                                 key->key_len,
700                                                 NULL,
701                                                 key->key, KEY_OP_INIT_VAL, NULL,
702                                                 NO_SYNC_WMIFLAG);
703
704                                 status = wmi_connect_cmd(ar->arWmi,
705                                                          ar->arNetworkType,
706                                                          ar->arDot11AuthMode,
707                                                          ar->arAuthMode,
708                                                          ar->arPairwiseCrypto,
709                                                          ar->arPairwiseCryptoLen,
710                                                          ar->arGroupCrypto,
711                                                          ar->arGroupCryptoLen,
712                                                          ar->arSsidLen,
713                                                          ar->arSsid,
714                                                          ar->arReqBssid,
715                                                          ar->arChannelHint,
716                                                          ar->arConnectCtrlFlags);
717                                 up(&ar->arSem);
718
719                         } else if (ar->arDot11AuthMode == SHARED_AUTH) {
720                                 /* should not reach here */
721                         }
722                 } else {
723                         ar->arConnectPending = false;
724                         if (ar->smeState == SME_CONNECTING) {
725                                 cfg80211_connect_result(ar->arNetDev, bssid,
726                                                         NULL, 0,
727                                                         NULL, 0,
728                                                         WLAN_STATUS_UNSPECIFIED_FAILURE,
729                                                         GFP_KERNEL);
730                         } else {
731                                 cfg80211_disconnected(ar->arNetDev,
732                                                       reason,
733                                                       NULL, 0,
734                                                       GFP_KERNEL);
735                         }
736                         ar->smeState = SME_DISCONNECTED;
737                 }
738         }
739     } else {
740             if (reason != DISCONNECT_CMD)
741                     wmi_disconnect_cmd(ar->arWmi);
742     }
743 }
744
745 void
746 ar6k_cfg80211_scan_node(void *arg, bss_t *ni)
747 {
748     struct wiphy *wiphy = (struct wiphy *)arg;
749     u16 size;
750     unsigned char *ieeemgmtbuf = NULL;
751     struct ieee80211_mgmt *mgmt;
752     struct ieee80211_channel *channel;
753     struct ieee80211_supported_band *band;
754     struct ieee80211_common_ie  *cie;
755     s32 signal;
756     int freq;
757
758     cie = &ni->ni_cie;
759
760 #define CHAN_IS_11A(x)  (!((x >= 2412) && (x <= 2484)))
761     if(CHAN_IS_11A(cie->ie_chan)) {
762         /* 11a */
763         band = wiphy->bands[IEEE80211_BAND_5GHZ];
764     } else if((cie->ie_erp) || (cie->ie_xrates)) {
765         /* 11g */
766         band = wiphy->bands[IEEE80211_BAND_2GHZ];
767     } else {
768         /* 11b */
769         band = wiphy->bands[IEEE80211_BAND_2GHZ];
770     }
771
772     size = ni->ni_framelen + offsetof(struct ieee80211_mgmt, u);
773     ieeemgmtbuf = A_MALLOC_NOWAIT(size);
774     if(!ieeemgmtbuf)
775     {
776         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: ieeeMgmtbuf alloc error\n", __func__));
777         return;
778     }
779
780     /* Note:
781        TODO: Update target to include 802.11 mac header while sending bss info.
782        Target removes 802.11 mac header while sending the bss info to host,
783        cfg80211 needs it, for time being just filling the da, sa and bssid fields alone.
784     */
785     mgmt = (struct ieee80211_mgmt *)ieeemgmtbuf;
786     memcpy(mgmt->da, bcast_mac, ATH_MAC_LEN);
787     memcpy(mgmt->sa, ni->ni_macaddr, ATH_MAC_LEN);
788     memcpy(mgmt->bssid, ni->ni_macaddr, ATH_MAC_LEN);
789     memcpy(ieeemgmtbuf + offsetof(struct ieee80211_mgmt, u),
790              ni->ni_buf, ni->ni_framelen);
791
792     freq    = cie->ie_chan;
793     channel = ieee80211_get_channel(wiphy, freq);
794     signal  = ni->ni_snr * 100;
795
796         AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
797                 ("%s: bssid %pM channel %d freq %d size %d\n", __func__,
798                         mgmt->bssid, channel->hw_value, freq, size));
799     cfg80211_inform_bss_frame(wiphy, channel, mgmt,
800                               le16_to_cpu(size),
801                               signal, GFP_KERNEL);
802
803     kfree (ieeemgmtbuf);
804 }
805
806 static int
807 ar6k_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
808                    struct cfg80211_scan_request *request)
809 {
810     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(ndev);
811     int ret = 0;
812     u32 forceFgScan = 0;
813
814     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
815
816     if(ar->arWmiReady == false) {
817         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
818         return -EIO;
819     }
820
821     if(ar->arWlanState == WLAN_DISABLED) {
822         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
823         return -EIO;
824     }
825
826     if (!ar->arUserBssFilter) {
827         if (wmi_bssfilter_cmd(ar->arWmi,
828                              (ar->arConnected ? ALL_BUT_BSS_FILTER : ALL_BSS_FILTER),
829                              0) != 0) {
830             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Couldn't set bss filtering\n", __func__));
831             return -EIO;
832         }
833     }
834
835     if(request->n_ssids &&
836        request->ssids[0].ssid_len) {
837         u8 i;
838
839         if(request->n_ssids > (MAX_PROBED_SSID_INDEX - 1)) {
840             request->n_ssids = MAX_PROBED_SSID_INDEX - 1;
841         }
842
843         for (i = 0; i < request->n_ssids; i++) {
844             wmi_probedSsid_cmd(ar->arWmi, i+1, SPECIFIC_SSID_FLAG,
845                                request->ssids[i].ssid_len,
846                                request->ssids[i].ssid);
847         }
848     }
849
850     if(ar->arConnected) {
851         forceFgScan = 1;
852     }
853
854     if(wmi_startscan_cmd(ar->arWmi, WMI_LONG_SCAN, forceFgScan, false, \
855                          0, 0, 0, NULL) != 0) {
856         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: wmi_startscan_cmd failed\n", __func__));
857         ret = -EIO;
858     }
859
860     ar->scan_request = request;
861
862     return ret;
863 }
864
865 void
866 ar6k_cfg80211_scanComplete_event(struct ar6_softc *ar, int status)
867 {
868
869     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: status %d\n", __func__, status));
870
871     if (!ar->scan_request)
872             return;
873
874     if ((status == A_ECANCELED) || (status == A_EBUSY)) {
875             cfg80211_scan_done(ar->scan_request, true);
876             goto out;
877     }
878
879     /* Translate data to cfg80211 mgmt format */
880     wmi_iterate_nodes(ar->arWmi, ar6k_cfg80211_scan_node, ar->wdev->wiphy);
881
882     cfg80211_scan_done(ar->scan_request, false);
883
884     if(ar->scan_request->n_ssids &&
885        ar->scan_request->ssids[0].ssid_len) {
886             u8 i;
887
888             for (i = 0; i < ar->scan_request->n_ssids; i++) {
889                     wmi_probedSsid_cmd(ar->arWmi, i+1, DISABLE_SSID_FLAG,
890                                        0, NULL);
891             }
892     }
893
894 out:
895     ar->scan_request = NULL;
896 }
897
898 static int
899 ar6k_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
900                       u8 key_index, bool pairwise, const u8 *mac_addr,
901                       struct key_params *params)
902 {
903     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(ndev);
904     struct ar_key *key = NULL;
905     u8 key_usage;
906     u8 key_type;
907     int status = 0;
908
909     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s:\n", __func__));
910
911     if(ar->arWmiReady == false) {
912         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
913         return -EIO;
914     }
915
916     if(ar->arWlanState == WLAN_DISABLED) {
917         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
918         return -EIO;
919     }
920
921     if(key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
922         AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
923                         ("%s: key index %d out of bounds\n", __func__, key_index));
924         return -ENOENT;
925     }
926
927     key = &ar->keys[key_index];
928     A_MEMZERO(key, sizeof(struct ar_key));
929
930     if(!mac_addr || is_broadcast_ether_addr(mac_addr)) {
931         key_usage = GROUP_USAGE;
932     } else {
933         key_usage = PAIRWISE_USAGE;
934     }
935
936     if(params) {
937         if(params->key_len > WLAN_MAX_KEY_LEN ||
938             params->seq_len > IW_ENCODE_SEQ_MAX_SIZE)
939             return -EINVAL;
940
941         key->key_len = params->key_len;
942         memcpy(key->key, params->key, key->key_len);
943         key->seq_len = params->seq_len;
944         memcpy(key->seq, params->seq, key->seq_len);
945         key->cipher = params->cipher;
946     }
947
948     switch (key->cipher) {
949     case WLAN_CIPHER_SUITE_WEP40:
950     case WLAN_CIPHER_SUITE_WEP104:
951         key_type = WEP_CRYPT;
952         break;
953
954     case WLAN_CIPHER_SUITE_TKIP:
955         key_type = TKIP_CRYPT;
956         break;
957
958     case WLAN_CIPHER_SUITE_CCMP:
959         key_type = AES_CRYPT;
960         break;
961
962     default:
963         return -ENOTSUPP;
964     }
965
966     if (((WPA_PSK_AUTH == ar->arAuthMode) || (WPA2_PSK_AUTH == ar->arAuthMode)) &&
967         (GROUP_USAGE & key_usage))
968     {
969         A_UNTIMEOUT(&ar->disconnect_timer);
970     }
971
972     AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
973                     ("%s: index %d, key_len %d, key_type 0x%x,"\
974                     " key_usage 0x%x, seq_len %d\n",
975                     __func__, key_index, key->key_len, key_type,
976                     key_usage, key->seq_len));
977
978     ar->arDefTxKeyIndex = key_index;
979     status = wmi_addKey_cmd(ar->arWmi, ar->arDefTxKeyIndex, key_type, key_usage,
980                     key->key_len, key->seq, key->key, KEY_OP_INIT_VAL,
981                     (u8 *)mac_addr, SYNC_BOTH_WMIFLAG);
982
983
984     if (status) {
985         return -EIO;
986     }
987
988     return 0;
989 }
990
991 static int
992 ar6k_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
993                       u8 key_index, bool pairwise, const u8 *mac_addr)
994 {
995     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(ndev);
996
997     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: index %d\n", __func__, key_index));
998
999     if(ar->arWmiReady == false) {
1000         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1001         return -EIO;
1002     }
1003
1004     if(ar->arWlanState == WLAN_DISABLED) {
1005         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1006         return -EIO;
1007     }
1008
1009     if(key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
1010         AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
1011                         ("%s: key index %d out of bounds\n", __func__, key_index));
1012         return -ENOENT;
1013     }
1014
1015     if(!ar->keys[key_index].key_len) {
1016         AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: index %d is empty\n", __func__, key_index));
1017         return 0;
1018     }
1019
1020     ar->keys[key_index].key_len = 0;
1021
1022     return wmi_deleteKey_cmd(ar->arWmi, key_index);
1023 }
1024
1025
1026 static int
1027 ar6k_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
1028                       u8 key_index, bool pairwise, const u8 *mac_addr,
1029                       void *cookie,
1030                       void (*callback)(void *cookie, struct key_params*))
1031 {
1032     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(ndev);
1033     struct ar_key *key = NULL;
1034     struct key_params params;
1035
1036     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: index %d\n", __func__, key_index));
1037
1038     if(ar->arWmiReady == false) {
1039         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1040         return -EIO;
1041     }
1042
1043     if(ar->arWlanState == WLAN_DISABLED) {
1044         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1045         return -EIO;
1046     }
1047
1048     if(key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
1049         AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
1050                         ("%s: key index %d out of bounds\n", __func__, key_index));
1051         return -ENOENT;
1052     }
1053
1054     key = &ar->keys[key_index];
1055     A_MEMZERO(&params, sizeof(params));
1056     params.cipher = key->cipher;
1057     params.key_len = key->key_len;
1058     params.seq_len = key->seq_len;
1059     params.seq = key->seq;
1060     params.key = key->key;
1061
1062     callback(cookie, &params);
1063
1064     return key->key_len ? 0 : -ENOENT;
1065 }
1066
1067
1068 static int
1069 ar6k_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *ndev,
1070                               u8 key_index, bool unicast, bool multicast)
1071 {
1072     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(ndev);
1073     struct ar_key *key = NULL;
1074     int status = 0;
1075     u8 key_usage;
1076
1077     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: index %d\n", __func__, key_index));
1078
1079     if(ar->arWmiReady == false) {
1080         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1081         return -EIO;
1082     }
1083
1084     if(ar->arWlanState == WLAN_DISABLED) {
1085         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1086         return -EIO;
1087     }
1088
1089     if(key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
1090         AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
1091                         ("%s: key index %d out of bounds\n",
1092                         __func__, key_index));
1093         return -ENOENT;
1094     }
1095
1096     if(!ar->keys[key_index].key_len) {
1097         AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: invalid key index %d\n",
1098                         __func__, key_index));
1099         return -EINVAL;
1100     }
1101
1102     ar->arDefTxKeyIndex = key_index;
1103     key = &ar->keys[ar->arDefTxKeyIndex];
1104     key_usage = GROUP_USAGE;
1105     if (WEP_CRYPT == ar->arPairwiseCrypto) {
1106         key_usage |= TX_USAGE;
1107     }
1108
1109     status = wmi_addKey_cmd(ar->arWmi, ar->arDefTxKeyIndex,
1110                             ar->arPairwiseCrypto, key_usage,
1111                             key->key_len, key->seq, key->key, KEY_OP_INIT_VAL,
1112                             NULL, SYNC_BOTH_WMIFLAG);
1113     if (status) {
1114         return -EIO;
1115     }
1116
1117     return 0;
1118 }
1119
1120 static int
1121 ar6k_cfg80211_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *ndev,
1122                                    u8 key_index)
1123 {
1124     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(ndev);
1125
1126     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: index %d\n", __func__, key_index));
1127
1128     if(ar->arWmiReady == false) {
1129         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1130         return -EIO;
1131     }
1132
1133     if(ar->arWlanState == WLAN_DISABLED) {
1134         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1135         return -EIO;
1136     }
1137
1138     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: not supported\n", __func__));
1139     return -ENOTSUPP;
1140 }
1141
1142 void
1143 ar6k_cfg80211_tkip_micerr_event(struct ar6_softc *ar, u8 keyid, bool ismcast)
1144 {
1145     AR_DEBUG_PRINTF(ATH_DEBUG_INFO,
1146                     ("%s: keyid %d, ismcast %d\n", __func__, keyid, ismcast));
1147
1148     cfg80211_michael_mic_failure(ar->arNetDev, ar->arBssid,
1149                                  (ismcast ? NL80211_KEYTYPE_GROUP : NL80211_KEYTYPE_PAIRWISE),
1150                                  keyid, NULL, GFP_KERNEL);
1151 }
1152
1153 static int
1154 ar6k_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1155 {
1156     struct ar6_softc *ar = (struct ar6_softc *)wiphy_priv(wiphy);
1157
1158     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: changed 0x%x\n", __func__, changed));
1159
1160     if(ar->arWmiReady == false) {
1161         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1162         return -EIO;
1163     }
1164
1165     if(ar->arWlanState == WLAN_DISABLED) {
1166         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1167         return -EIO;
1168     }
1169
1170     if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1171         if (wmi_set_rts_cmd(ar->arWmi,wiphy->rts_threshold) != 0){
1172             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: wmi_set_rts_cmd failed\n", __func__));
1173             return -EIO;
1174         }
1175     }
1176
1177     return 0;
1178 }
1179
1180 static int
1181 ar6k_cfg80211_set_bitrate_mask(struct wiphy *wiphy, struct net_device *dev,
1182                                const u8 *peer,
1183                                const struct cfg80211_bitrate_mask *mask)
1184 {
1185     AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("Setting rates: Not supported\n"));
1186     return -EIO;
1187 }
1188
1189 /* The type nl80211_tx_power_setting replaces the following data type from 2.6.36 onwards */
1190 static int
1191 ar6k_cfg80211_set_txpower(struct wiphy *wiphy, enum nl80211_tx_power_setting type, int dbm)
1192 {
1193     struct ar6_softc *ar = (struct ar6_softc *)wiphy_priv(wiphy);
1194     u8 ar_dbm;
1195
1196     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: type 0x%x, dbm %d\n", __func__, type, dbm));
1197
1198     if(ar->arWmiReady == false) {
1199         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1200         return -EIO;
1201     }
1202
1203     if(ar->arWlanState == WLAN_DISABLED) {
1204         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1205         return -EIO;
1206     }
1207
1208     ar->arTxPwrSet = false;
1209     switch(type) {
1210     case NL80211_TX_POWER_AUTOMATIC:
1211         return 0;
1212     case NL80211_TX_POWER_LIMITED:
1213         ar->arTxPwr = ar_dbm = dbm;
1214         ar->arTxPwrSet = true;
1215         break;
1216     default:
1217         AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: type 0x%x not supported\n", __func__, type));
1218         return -EOPNOTSUPP;
1219     }
1220
1221     wmi_set_txPwr_cmd(ar->arWmi, ar_dbm);
1222
1223     return 0;
1224 }
1225
1226 static int
1227 ar6k_cfg80211_get_txpower(struct wiphy *wiphy, int *dbm)
1228 {
1229     struct ar6_softc *ar = (struct ar6_softc *)wiphy_priv(wiphy);
1230
1231     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
1232
1233     if(ar->arWmiReady == false) {
1234         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1235         return -EIO;
1236     }
1237
1238     if(ar->arWlanState == WLAN_DISABLED) {
1239         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1240         return -EIO;
1241     }
1242
1243     if((ar->arConnected == true)) {
1244         ar->arTxPwr = 0;
1245
1246         if(wmi_get_txPwr_cmd(ar->arWmi) != 0) {
1247             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: wmi_get_txPwr_cmd failed\n", __func__));
1248             return -EIO;
1249         }
1250
1251         wait_event_interruptible_timeout(arEvent, ar->arTxPwr != 0, 5 * HZ);
1252
1253         if(signal_pending(current)) {
1254             AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Target did not respond\n", __func__));
1255             return -EINTR;
1256         }
1257     }
1258
1259     *dbm = ar->arTxPwr;
1260     return 0;
1261 }
1262
1263 static int
1264 ar6k_cfg80211_set_power_mgmt(struct wiphy *wiphy,
1265                              struct net_device *dev,
1266                              bool pmgmt, int timeout)
1267 {
1268     struct ar6_softc *ar = ar6k_priv(dev);
1269     WMI_POWER_MODE_CMD pwrMode;
1270
1271     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: pmgmt %d, timeout %d\n", __func__, pmgmt, timeout));
1272
1273     if(ar->arWmiReady == false) {
1274         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1275         return -EIO;
1276     }
1277
1278     if(ar->arWlanState == WLAN_DISABLED) {
1279         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1280         return -EIO;
1281     }
1282
1283     if(pmgmt) {
1284         AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: Max Perf\n", __func__));
1285         pwrMode.powerMode = REC_POWER;
1286     } else {
1287         AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: Rec Power\n", __func__));
1288         pwrMode.powerMode = MAX_PERF_POWER;
1289     }
1290
1291     if(wmi_powermode_cmd(ar->arWmi, pwrMode.powerMode) != 0) {
1292         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: wmi_powermode_cmd failed\n", __func__));
1293         return -EIO;
1294     }
1295
1296     return 0;
1297 }
1298
1299 static struct net_device *
1300 ar6k_cfg80211_add_virtual_intf(struct wiphy *wiphy, char *name,
1301                                             enum nl80211_iftype type, u32 *flags,
1302                                             struct vif_params *params)
1303 {
1304
1305     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: not supported\n", __func__));
1306
1307     /* Multiple virtual interface is not supported.
1308      * The default interface supports STA and IBSS type
1309      */
1310     return ERR_PTR(-EOPNOTSUPP);
1311 }
1312
1313 static int
1314 ar6k_cfg80211_del_virtual_intf(struct wiphy *wiphy, struct net_device *dev)
1315 {
1316
1317     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: not supported\n", __func__));
1318
1319     /* Multiple virtual interface is not supported.
1320      * The default interface supports STA and IBSS type
1321      */
1322     return -EOPNOTSUPP;
1323 }
1324
1325 static int
1326 ar6k_cfg80211_change_iface(struct wiphy *wiphy, struct net_device *ndev,
1327                            enum nl80211_iftype type, u32 *flags,
1328                            struct vif_params *params)
1329 {
1330     struct ar6_softc *ar = ar6k_priv(ndev);
1331     struct wireless_dev *wdev = ar->wdev;
1332
1333     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: type %u\n", __func__, type));
1334
1335     if(ar->arWmiReady == false) {
1336         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1337         return -EIO;
1338     }
1339
1340     if(ar->arWlanState == WLAN_DISABLED) {
1341         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1342         return -EIO;
1343     }
1344
1345     switch (type) {
1346     case NL80211_IFTYPE_STATION:
1347         ar->arNextMode = INFRA_NETWORK;
1348         break;
1349     case NL80211_IFTYPE_ADHOC:
1350         ar->arNextMode = ADHOC_NETWORK;
1351         break;
1352     default:
1353         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: type %u\n", __func__, type));
1354         return -EOPNOTSUPP;
1355     }
1356
1357     wdev->iftype = type;
1358
1359     return 0;
1360 }
1361
1362 static int
1363 ar6k_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1364                         struct cfg80211_ibss_params *ibss_param)
1365 {
1366     struct ar6_softc *ar = ar6k_priv(dev);
1367     int status;
1368
1369     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
1370
1371     if(ar->arWmiReady == false) {
1372         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1373         return -EIO;
1374     }
1375
1376     if(ar->arWlanState == WLAN_DISABLED) {
1377         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1378         return -EIO;
1379     }
1380
1381     if(!ibss_param->ssid_len || IEEE80211_MAX_SSID_LEN < ibss_param->ssid_len) {
1382         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: ssid invalid\n", __func__));
1383         return -EINVAL;
1384     }
1385
1386     ar->arSsidLen = ibss_param->ssid_len;
1387     memcpy(ar->arSsid, ibss_param->ssid, ar->arSsidLen);
1388
1389     if(ibss_param->channel) {
1390         ar->arChannelHint = ibss_param->channel->center_freq;
1391     }
1392
1393     if(ibss_param->channel_fixed) {
1394         /* TODO: channel_fixed: The channel should be fixed, do not search for
1395          * IBSSs to join on other channels. Target firmware does not support this
1396          * feature, needs to be updated.*/
1397     }
1398
1399     A_MEMZERO(ar->arReqBssid, sizeof(ar->arReqBssid));
1400     if(ibss_param->bssid) {
1401         if(memcmp(&ibss_param->bssid, bcast_mac, AR6000_ETH_ADDR_LEN)) {
1402             memcpy(ar->arReqBssid, ibss_param->bssid, sizeof(ar->arReqBssid));
1403         }
1404     }
1405
1406     ar6k_set_wpa_version(ar, 0);
1407     ar6k_set_auth_type(ar, NL80211_AUTHTYPE_OPEN_SYSTEM);
1408
1409     if(ibss_param->privacy) {
1410         ar6k_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, true);
1411         ar6k_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, false);
1412     } else {
1413         ar6k_set_cipher(ar, IW_AUTH_CIPHER_NONE, true);
1414         ar6k_set_cipher(ar, IW_AUTH_CIPHER_NONE, false);
1415     }
1416
1417     ar->arNetworkType = ar->arNextMode;
1418
1419     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: Connect called with authmode %d dot11 auth %d"\
1420                     " PW crypto %d PW crypto Len %d GRP crypto %d"\
1421                     " GRP crypto Len %d channel hint %u\n",
1422                     __func__, ar->arAuthMode, ar->arDot11AuthMode,
1423                     ar->arPairwiseCrypto, ar->arPairwiseCryptoLen,
1424                     ar->arGroupCrypto, ar->arGroupCryptoLen, ar->arChannelHint));
1425
1426     status = wmi_connect_cmd(ar->arWmi, ar->arNetworkType,
1427                             ar->arDot11AuthMode, ar->arAuthMode,
1428                             ar->arPairwiseCrypto, ar->arPairwiseCryptoLen,
1429                             ar->arGroupCrypto,ar->arGroupCryptoLen,
1430                             ar->arSsidLen, ar->arSsid,
1431                             ar->arReqBssid, ar->arChannelHint,
1432                             ar->arConnectCtrlFlags);
1433     ar->arConnectPending = true;
1434
1435     return 0;
1436 }
1437
1438 static int
1439 ar6k_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1440 {
1441     struct ar6_softc *ar = (struct ar6_softc *)ar6k_priv(dev);
1442
1443     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
1444
1445     if(ar->arWmiReady == false) {
1446         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wmi not ready\n", __func__));
1447         return -EIO;
1448     }
1449
1450     if(ar->arWlanState == WLAN_DISABLED) {
1451         AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("%s: Wlan disabled\n", __func__));
1452         return -EIO;
1453     }
1454
1455     ar6000_disconnect(ar);
1456     A_MEMZERO(ar->arSsid, sizeof(ar->arSsid));
1457     ar->arSsidLen = 0;
1458
1459     return 0;
1460 }
1461
1462 #ifdef CONFIG_NL80211_TESTMODE
1463 enum ar6k_testmode_attr {
1464         __AR6K_TM_ATTR_INVALID  = 0,
1465         AR6K_TM_ATTR_CMD        = 1,
1466         AR6K_TM_ATTR_DATA       = 2,
1467
1468         /* keep last */
1469         __AR6K_TM_ATTR_AFTER_LAST,
1470         AR6K_TM_ATTR_MAX        = __AR6K_TM_ATTR_AFTER_LAST - 1
1471 };
1472
1473 enum ar6k_testmode_cmd {
1474         AR6K_TM_CMD_TCMD                = 0,
1475         AR6K_TM_CMD_RX_REPORT           = 1,
1476 };
1477
1478 #define AR6K_TM_DATA_MAX_LEN 5000
1479
1480 static const struct nla_policy ar6k_testmode_policy[AR6K_TM_ATTR_MAX + 1] = {
1481         [AR6K_TM_ATTR_CMD] = { .type = NLA_U32 },
1482         [AR6K_TM_ATTR_DATA] = { .type = NLA_BINARY,
1483                                 .len = AR6K_TM_DATA_MAX_LEN },
1484 };
1485
1486 void ar6000_testmode_rx_report_event(struct ar6_softc *ar, void *buf,
1487                                      int buf_len)
1488 {
1489         if (down_interruptible(&ar->arSem))
1490                 return;
1491
1492         kfree(ar->tcmd_rx_report);
1493
1494         ar->tcmd_rx_report = kmemdup(buf, buf_len, GFP_KERNEL);
1495         ar->tcmd_rx_report_len = buf_len;
1496
1497         up(&ar->arSem);
1498
1499         wake_up(&arEvent);
1500 }
1501
1502 static int ar6000_testmode_rx_report(struct ar6_softc *ar, void *buf,
1503                                      int buf_len, struct sk_buff *skb)
1504 {
1505         int ret = 0;
1506         long left;
1507
1508         if (down_interruptible(&ar->arSem))
1509                 return -ERESTARTSYS;
1510
1511         if (ar->arWmiReady == false) {
1512                 ret = -EIO;
1513                 goto out;
1514         }
1515
1516         if (ar->bIsDestroyProgress) {
1517                 ret = -EBUSY;
1518                 goto out;
1519         }
1520
1521         WARN_ON(ar->tcmd_rx_report != NULL);
1522         WARN_ON(ar->tcmd_rx_report_len > 0);
1523
1524         if (wmi_test_cmd(ar->arWmi, buf, buf_len) < 0) {
1525                 up(&ar->arSem);
1526                 return -EIO;
1527         }
1528
1529         left = wait_event_interruptible_timeout(arEvent,
1530                                                ar->tcmd_rx_report != NULL,
1531                                                wmitimeout * HZ);
1532
1533         if (left == 0) {
1534                 ret = -ETIMEDOUT;
1535                 goto out;
1536         } else if (left < 0) {
1537                 ret = left;
1538                 goto out;
1539         }
1540
1541         if (ar->tcmd_rx_report == NULL || ar->tcmd_rx_report_len == 0) {
1542                 ret = -EINVAL;
1543                 goto out;
1544         }
1545
1546         NLA_PUT(skb, AR6K_TM_ATTR_DATA, ar->tcmd_rx_report_len,
1547                 ar->tcmd_rx_report);
1548
1549         kfree(ar->tcmd_rx_report);
1550         ar->tcmd_rx_report = NULL;
1551
1552 out:
1553         up(&ar->arSem);
1554
1555         return ret;
1556
1557 nla_put_failure:
1558         ret = -ENOBUFS;
1559         goto out;
1560 }
1561
1562 static int ar6k_testmode_cmd(struct wiphy *wiphy, void *data, int len)
1563 {
1564         struct ar6_softc *ar = wiphy_priv(wiphy);
1565         struct nlattr *tb[AR6K_TM_ATTR_MAX + 1];
1566         int err, buf_len, reply_len;
1567         struct sk_buff *skb;
1568         void *buf;
1569
1570         err = nla_parse(tb, AR6K_TM_ATTR_MAX, data, len,
1571                         ar6k_testmode_policy);
1572         if (err)
1573                 return err;
1574
1575         if (!tb[AR6K_TM_ATTR_CMD])
1576                 return -EINVAL;
1577
1578         switch (nla_get_u32(tb[AR6K_TM_ATTR_CMD])) {
1579         case AR6K_TM_CMD_TCMD:
1580                 if (!tb[AR6K_TM_ATTR_DATA])
1581                         return -EINVAL;
1582
1583                 buf = nla_data(tb[AR6K_TM_ATTR_DATA]);
1584                 buf_len = nla_len(tb[AR6K_TM_ATTR_DATA]);
1585
1586                 wmi_test_cmd(ar->arWmi, buf, buf_len);
1587
1588                 return 0;
1589
1590                 break;
1591         case AR6K_TM_CMD_RX_REPORT:
1592                 if (!tb[AR6K_TM_ATTR_DATA])
1593                         return -EINVAL;
1594
1595                 buf = nla_data(tb[AR6K_TM_ATTR_DATA]);
1596                 buf_len = nla_len(tb[AR6K_TM_ATTR_DATA]);
1597
1598                 reply_len = nla_total_size(AR6K_TM_DATA_MAX_LEN);
1599                 skb = cfg80211_testmode_alloc_reply_skb(wiphy, reply_len);
1600                 if (!skb)
1601                         return -ENOMEM;
1602
1603                 err = ar6000_testmode_rx_report(ar, buf, buf_len, skb);
1604                 if (err < 0) {
1605                         kfree_skb(skb);
1606                         return err;
1607                 }
1608
1609                 return cfg80211_testmode_reply(skb);
1610         default:
1611                 return -EOPNOTSUPP;
1612         }
1613 }
1614 #endif
1615
1616 static const
1617 u32 cipher_suites[] = {
1618     WLAN_CIPHER_SUITE_WEP40,
1619     WLAN_CIPHER_SUITE_WEP104,
1620     WLAN_CIPHER_SUITE_TKIP,
1621     WLAN_CIPHER_SUITE_CCMP,
1622 };
1623
1624 bool is_rate_legacy(s32 rate)
1625 {
1626         static const s32 legacy[] = { 1000, 2000, 5500, 11000,
1627                                       6000, 9000, 12000, 18000, 24000,
1628                                       36000, 48000, 54000 };
1629         u8 i;
1630
1631         for (i = 0; i < ARRAY_SIZE(legacy); i++) {
1632                 if (rate == legacy[i])
1633                         return true;
1634         }
1635
1636         return false;
1637 }
1638
1639 bool is_rate_ht20(s32 rate, u8 *mcs, bool *sgi)
1640 {
1641         static const s32 ht20[] = { 6500, 13000, 19500, 26000, 39000,
1642                                     52000, 58500, 65000, 72200 };
1643         u8 i;
1644
1645         for (i = 0; i < ARRAY_SIZE(ht20); i++) {
1646                 if (rate == ht20[i]) {
1647                         if (i == ARRAY_SIZE(ht20) - 1)
1648                                 /* last rate uses sgi */
1649                                 *sgi = true;
1650                         else
1651                                 *sgi = false;
1652
1653                         *mcs = i;
1654                         return true;
1655                 }
1656         }
1657         return false;
1658 }
1659
1660 bool is_rate_ht40(s32 rate, u8 *mcs, bool *sgi)
1661 {
1662         static const s32 ht40[] = { 13500, 27000, 40500, 54000,
1663                                     81000, 108000, 121500, 135000,
1664                                     150000 };
1665         u8 i;
1666
1667         for (i = 0; i < ARRAY_SIZE(ht40); i++) {
1668                 if (rate == ht40[i]) {
1669                         if (i == ARRAY_SIZE(ht40) - 1)
1670                                 /* last rate uses sgi */
1671                                 *sgi = true;
1672                         else
1673                                 *sgi = false;
1674
1675                         *mcs = i;
1676                         return true;
1677                 }
1678         }
1679
1680         return false;
1681 }
1682
1683 static int ar6k_get_station(struct wiphy *wiphy, struct net_device *dev,
1684                             u8 *mac, struct station_info *sinfo)
1685 {
1686         struct ar6_softc *ar = ar6k_priv(dev);
1687         long left;
1688         bool sgi;
1689         s32 rate;
1690         int ret;
1691         u8 mcs;
1692
1693         if (memcmp(mac, ar->arBssid, ETH_ALEN) != 0)
1694                 return -ENOENT;
1695
1696         if (down_interruptible(&ar->arSem))
1697                 return -EBUSY;
1698
1699         ar->statsUpdatePending = true;
1700
1701         ret = wmi_get_stats_cmd(ar->arWmi);
1702
1703         if (ret != 0) {
1704                 up(&ar->arSem);
1705                 return -EIO;
1706         }
1707
1708         left = wait_event_interruptible_timeout(arEvent,
1709                                                 ar->statsUpdatePending == false,
1710                                                 wmitimeout * HZ);
1711
1712         up(&ar->arSem);
1713
1714         if (left == 0)
1715                 return -ETIMEDOUT;
1716         else if (left < 0)
1717                 return left;
1718
1719         if (ar->arTargetStats.rx_bytes) {
1720                 sinfo->rx_bytes = ar->arTargetStats.rx_bytes;
1721                 sinfo->filled |= STATION_INFO_RX_BYTES;
1722                 sinfo->rx_packets = ar->arTargetStats.rx_packets;
1723                 sinfo->filled |= STATION_INFO_RX_PACKETS;
1724         }
1725
1726         if (ar->arTargetStats.tx_bytes) {
1727                 sinfo->tx_bytes = ar->arTargetStats.tx_bytes;
1728                 sinfo->filled |= STATION_INFO_TX_BYTES;
1729                 sinfo->tx_packets = ar->arTargetStats.tx_packets;
1730                 sinfo->filled |= STATION_INFO_TX_PACKETS;
1731         }
1732
1733         sinfo->signal = ar->arTargetStats.cs_rssi;
1734         sinfo->filled |= STATION_INFO_SIGNAL;
1735
1736         rate = ar->arTargetStats.tx_unicast_rate;
1737
1738         if (is_rate_legacy(rate)) {
1739                 sinfo->txrate.legacy = rate / 100;
1740         } else if (is_rate_ht20(rate, &mcs, &sgi)) {
1741                 if (sgi) {
1742                         sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1743                         sinfo->txrate.mcs = mcs - 1;
1744                 } else {
1745                         sinfo->txrate.mcs = mcs;
1746                 }
1747
1748                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1749         } else if (is_rate_ht40(rate, &mcs, &sgi)) {
1750                 if (sgi) {
1751                         sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1752                         sinfo->txrate.mcs = mcs - 1;
1753                 } else {
1754                         sinfo->txrate.mcs = mcs;
1755                 }
1756
1757                 sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
1758                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1759         } else {
1760                 WARN(1, "invalid rate: %d", rate);
1761                 return 0;
1762         }
1763
1764         sinfo->filled |= STATION_INFO_TX_BITRATE;
1765
1766         return 0;
1767 }
1768
1769 static int ar6k_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1770                           struct cfg80211_pmksa *pmksa)
1771 {
1772         struct ar6_softc *ar = ar6k_priv(netdev);
1773         return wmi_setPmkid_cmd(ar->arWmi, pmksa->bssid, pmksa->pmkid, true);
1774 }
1775
1776 static int ar6k_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1777                           struct cfg80211_pmksa *pmksa)
1778 {
1779         struct ar6_softc *ar = ar6k_priv(netdev);
1780         return wmi_setPmkid_cmd(ar->arWmi, pmksa->bssid, pmksa->pmkid, false);
1781 }
1782
1783 static int ar6k_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
1784 {
1785         struct ar6_softc *ar = ar6k_priv(netdev);
1786         if (ar->arConnected)
1787                 return wmi_setPmkid_cmd(ar->arWmi, ar->arBssid, NULL, false);
1788         return 0;
1789 }
1790
1791 static struct
1792 cfg80211_ops ar6k_cfg80211_ops = {
1793     .change_virtual_intf = ar6k_cfg80211_change_iface,
1794     .add_virtual_intf = ar6k_cfg80211_add_virtual_intf,
1795     .del_virtual_intf = ar6k_cfg80211_del_virtual_intf,
1796     .scan = ar6k_cfg80211_scan,
1797     .connect = ar6k_cfg80211_connect,
1798     .disconnect = ar6k_cfg80211_disconnect,
1799     .add_key = ar6k_cfg80211_add_key,
1800     .get_key = ar6k_cfg80211_get_key,
1801     .del_key = ar6k_cfg80211_del_key,
1802     .set_default_key = ar6k_cfg80211_set_default_key,
1803     .set_default_mgmt_key = ar6k_cfg80211_set_default_mgmt_key,
1804     .set_wiphy_params = ar6k_cfg80211_set_wiphy_params,
1805     .set_bitrate_mask = ar6k_cfg80211_set_bitrate_mask,
1806     .set_tx_power = ar6k_cfg80211_set_txpower,
1807     .get_tx_power = ar6k_cfg80211_get_txpower,
1808     .set_power_mgmt = ar6k_cfg80211_set_power_mgmt,
1809     .join_ibss = ar6k_cfg80211_join_ibss,
1810     .leave_ibss = ar6k_cfg80211_leave_ibss,
1811     .get_station = ar6k_get_station,
1812     .set_pmksa = ar6k_set_pmksa,
1813     .del_pmksa = ar6k_del_pmksa,
1814     .flush_pmksa = ar6k_flush_pmksa,
1815     CFG80211_TESTMODE_CMD(ar6k_testmode_cmd)
1816 };
1817
1818 struct wireless_dev *
1819 ar6k_cfg80211_init(struct device *dev)
1820 {
1821     int ret = 0;
1822     struct wireless_dev *wdev;
1823
1824     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
1825
1826     wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
1827     if(!wdev) {
1828         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1829                         ("%s: Couldn't allocate wireless device\n", __func__));
1830         return ERR_PTR(-ENOMEM);
1831     }
1832
1833     /* create a new wiphy for use with cfg80211 */
1834     wdev->wiphy = wiphy_new(&ar6k_cfg80211_ops, sizeof(struct ar6_softc));
1835     if(!wdev->wiphy) {
1836         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1837                         ("%s: Couldn't allocate wiphy device\n", __func__));
1838         kfree(wdev);
1839         return ERR_PTR(-ENOMEM);
1840     }
1841
1842     /* set device pointer for wiphy */
1843     set_wiphy_dev(wdev->wiphy, dev);
1844
1845     wdev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1846                                    BIT(NL80211_IFTYPE_ADHOC);
1847     /* max num of ssids that can be probed during scanning */
1848     wdev->wiphy->max_scan_ssids = MAX_PROBED_SSID_INDEX;
1849     wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar6k_band_2ghz;
1850     wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar6k_band_5ghz;
1851     wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1852
1853     wdev->wiphy->cipher_suites = cipher_suites;
1854     wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
1855
1856     ret = wiphy_register(wdev->wiphy);
1857     if(ret < 0) {
1858         AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
1859                         ("%s: Couldn't register wiphy device\n", __func__));
1860         wiphy_free(wdev->wiphy);
1861         return ERR_PTR(ret);
1862     }
1863
1864     return wdev;
1865 }
1866
1867 void
1868 ar6k_cfg80211_deinit(struct ar6_softc *ar)
1869 {
1870     struct wireless_dev *wdev = ar->wdev;
1871
1872     AR_DEBUG_PRINTF(ATH_DEBUG_INFO, ("%s: \n", __func__));
1873
1874     if(ar->scan_request) {
1875         cfg80211_scan_done(ar->scan_request, true);
1876         ar->scan_request = NULL;
1877     }
1878
1879     if(!wdev)
1880         return;
1881
1882     wiphy_unregister(wdev->wiphy);
1883     wiphy_free(wdev->wiphy);
1884     kfree(wdev);
1885 }
1886
1887
1888
1889
1890
1891
1892