ath6kl: Check sme state before delivering disconnect event to cfg80211
[pandora-kernel.git] / drivers / net / wireless / ath / ath6kl / cfg80211.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
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
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "core.h"
18 #include "cfg80211.h"
19 #include "debug.h"
20
21 #define RATETAB_ENT(_rate, _rateid, _flags) {   \
22         .bitrate    = (_rate),                  \
23         .flags      = (_flags),                 \
24         .hw_value   = (_rateid),                \
25 }
26
27 #define CHAN2G(_channel, _freq, _flags) {   \
28         .band           = IEEE80211_BAND_2GHZ,  \
29         .hw_value       = (_channel),           \
30         .center_freq    = (_freq),              \
31         .flags          = (_flags),             \
32         .max_antenna_gain   = 0,                \
33         .max_power      = 30,                   \
34 }
35
36 #define CHAN5G(_channel, _flags) {                  \
37         .band           = IEEE80211_BAND_5GHZ,      \
38         .hw_value       = (_channel),               \
39         .center_freq    = 5000 + (5 * (_channel)),  \
40         .flags          = (_flags),                 \
41         .max_antenna_gain   = 0,                    \
42         .max_power      = 30,                       \
43 }
44
45 static struct ieee80211_rate ath6kl_rates[] = {
46         RATETAB_ENT(10, 0x1, 0),
47         RATETAB_ENT(20, 0x2, 0),
48         RATETAB_ENT(55, 0x4, 0),
49         RATETAB_ENT(110, 0x8, 0),
50         RATETAB_ENT(60, 0x10, 0),
51         RATETAB_ENT(90, 0x20, 0),
52         RATETAB_ENT(120, 0x40, 0),
53         RATETAB_ENT(180, 0x80, 0),
54         RATETAB_ENT(240, 0x100, 0),
55         RATETAB_ENT(360, 0x200, 0),
56         RATETAB_ENT(480, 0x400, 0),
57         RATETAB_ENT(540, 0x800, 0),
58 };
59
60 #define ath6kl_a_rates     (ath6kl_rates + 4)
61 #define ath6kl_a_rates_size    8
62 #define ath6kl_g_rates     (ath6kl_rates + 0)
63 #define ath6kl_g_rates_size    12
64
65 static struct ieee80211_channel ath6kl_2ghz_channels[] = {
66         CHAN2G(1, 2412, 0),
67         CHAN2G(2, 2417, 0),
68         CHAN2G(3, 2422, 0),
69         CHAN2G(4, 2427, 0),
70         CHAN2G(5, 2432, 0),
71         CHAN2G(6, 2437, 0),
72         CHAN2G(7, 2442, 0),
73         CHAN2G(8, 2447, 0),
74         CHAN2G(9, 2452, 0),
75         CHAN2G(10, 2457, 0),
76         CHAN2G(11, 2462, 0),
77         CHAN2G(12, 2467, 0),
78         CHAN2G(13, 2472, 0),
79         CHAN2G(14, 2484, 0),
80 };
81
82 static struct ieee80211_channel ath6kl_5ghz_a_channels[] = {
83         CHAN5G(34, 0), CHAN5G(36, 0),
84         CHAN5G(38, 0), CHAN5G(40, 0),
85         CHAN5G(42, 0), CHAN5G(44, 0),
86         CHAN5G(46, 0), CHAN5G(48, 0),
87         CHAN5G(52, 0), CHAN5G(56, 0),
88         CHAN5G(60, 0), CHAN5G(64, 0),
89         CHAN5G(100, 0), CHAN5G(104, 0),
90         CHAN5G(108, 0), CHAN5G(112, 0),
91         CHAN5G(116, 0), CHAN5G(120, 0),
92         CHAN5G(124, 0), CHAN5G(128, 0),
93         CHAN5G(132, 0), CHAN5G(136, 0),
94         CHAN5G(140, 0), CHAN5G(149, 0),
95         CHAN5G(153, 0), CHAN5G(157, 0),
96         CHAN5G(161, 0), CHAN5G(165, 0),
97         CHAN5G(184, 0), CHAN5G(188, 0),
98         CHAN5G(192, 0), CHAN5G(196, 0),
99         CHAN5G(200, 0), CHAN5G(204, 0),
100         CHAN5G(208, 0), CHAN5G(212, 0),
101         CHAN5G(216, 0),
102 };
103
104 static struct ieee80211_supported_band ath6kl_band_2ghz = {
105         .n_channels = ARRAY_SIZE(ath6kl_2ghz_channels),
106         .channels = ath6kl_2ghz_channels,
107         .n_bitrates = ath6kl_g_rates_size,
108         .bitrates = ath6kl_g_rates,
109 };
110
111 static struct ieee80211_supported_band ath6kl_band_5ghz = {
112         .n_channels = ARRAY_SIZE(ath6kl_5ghz_a_channels),
113         .channels = ath6kl_5ghz_a_channels,
114         .n_bitrates = ath6kl_a_rates_size,
115         .bitrates = ath6kl_a_rates,
116 };
117
118 static int ath6kl_set_wpa_version(struct ath6kl *ar,
119                                   enum nl80211_wpa_versions wpa_version)
120 {
121         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: %u\n", __func__, wpa_version);
122
123         if (!wpa_version) {
124                 ar->auth_mode = NONE_AUTH;
125         } else if (wpa_version & NL80211_WPA_VERSION_2) {
126                 ar->auth_mode = WPA2_AUTH;
127         } else if (wpa_version & NL80211_WPA_VERSION_1) {
128                 ar->auth_mode = WPA_AUTH;
129         } else {
130                 ath6kl_err("%s: %u not supported\n", __func__, wpa_version);
131                 return -ENOTSUPP;
132         }
133
134         return 0;
135 }
136
137 static int ath6kl_set_auth_type(struct ath6kl *ar,
138                                 enum nl80211_auth_type auth_type)
139 {
140
141         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, auth_type);
142
143         switch (auth_type) {
144         case NL80211_AUTHTYPE_OPEN_SYSTEM:
145                 ar->dot11_auth_mode = OPEN_AUTH;
146                 break;
147         case NL80211_AUTHTYPE_SHARED_KEY:
148                 ar->dot11_auth_mode = SHARED_AUTH;
149                 break;
150         case NL80211_AUTHTYPE_NETWORK_EAP:
151                 ar->dot11_auth_mode = LEAP_AUTH;
152                 break;
153
154         case NL80211_AUTHTYPE_AUTOMATIC:
155                 ar->dot11_auth_mode = OPEN_AUTH;
156                 ar->auto_auth_stage = AUTH_OPEN_IN_PROGRESS;
157                 break;
158
159         default:
160                 ath6kl_err("%s: 0x%x not spported\n", __func__, auth_type);
161                 return -ENOTSUPP;
162         }
163
164         return 0;
165 }
166
167 static int ath6kl_set_cipher(struct ath6kl *ar, u32 cipher, bool ucast)
168 {
169         u8 *ar_cipher = ucast ? &ar->prwise_crypto : &ar->grp_crypto;
170         u8 *ar_cipher_len = ucast ? &ar->prwise_crypto_len : &ar->grp_crpto_len;
171
172         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: cipher 0x%x, ucast %u\n",
173                    __func__, cipher, ucast);
174
175         switch (cipher) {
176         case 0:
177                 /* our own hack to use value 0 as no crypto used */
178                 *ar_cipher = NONE_CRYPT;
179                 *ar_cipher_len = 0;
180                 break;
181         case WLAN_CIPHER_SUITE_WEP40:
182                 *ar_cipher = WEP_CRYPT;
183                 *ar_cipher_len = 5;
184                 break;
185         case WLAN_CIPHER_SUITE_WEP104:
186                 *ar_cipher = WEP_CRYPT;
187                 *ar_cipher_len = 13;
188                 break;
189         case WLAN_CIPHER_SUITE_TKIP:
190                 *ar_cipher = TKIP_CRYPT;
191                 *ar_cipher_len = 0;
192                 break;
193         case WLAN_CIPHER_SUITE_CCMP:
194                 *ar_cipher = AES_CRYPT;
195                 *ar_cipher_len = 0;
196                 break;
197         default:
198                 ath6kl_err("cipher 0x%x not supported\n", cipher);
199                 return -ENOTSUPP;
200         }
201
202         return 0;
203 }
204
205 static void ath6kl_set_key_mgmt(struct ath6kl *ar, u32 key_mgmt)
206 {
207         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, key_mgmt);
208
209         if (key_mgmt == WLAN_AKM_SUITE_PSK) {
210                 if (ar->auth_mode == WPA_AUTH)
211                         ar->auth_mode = WPA_PSK_AUTH;
212                 else if (ar->auth_mode == WPA2_AUTH)
213                         ar->auth_mode = WPA2_PSK_AUTH;
214         } else if (key_mgmt != WLAN_AKM_SUITE_8021X) {
215                 ar->auth_mode = NONE_AUTH;
216         }
217 }
218
219 static bool ath6kl_cfg80211_ready(struct ath6kl *ar)
220 {
221         if (!test_bit(WMI_READY, &ar->flag)) {
222                 ath6kl_err("wmi is not ready\n");
223                 return false;
224         }
225
226         if (!test_bit(WLAN_ENABLED, &ar->flag)) {
227                 ath6kl_err("wlan disabled\n");
228                 return false;
229         }
230
231         return true;
232 }
233
234 static int ath6kl_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
235                                    struct cfg80211_connect_params *sme)
236 {
237         struct ath6kl *ar = ath6kl_priv(dev);
238         int status;
239
240         ar->sme_state = SME_CONNECTING;
241
242         if (!ath6kl_cfg80211_ready(ar))
243                 return -EIO;
244
245         if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
246                 ath6kl_err("destroy in progress\n");
247                 return -EBUSY;
248         }
249
250         if (test_bit(SKIP_SCAN, &ar->flag) &&
251             ((sme->channel && sme->channel->center_freq == 0) ||
252              (sme->bssid && is_zero_ether_addr(sme->bssid)))) {
253                 ath6kl_err("SkipScan: channel or bssid invalid\n");
254                 return -EINVAL;
255         }
256
257         if (down_interruptible(&ar->sem)) {
258                 ath6kl_err("busy, couldn't get access\n");
259                 return -ERESTARTSYS;
260         }
261
262         if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
263                 ath6kl_err("busy, destroy in progress\n");
264                 up(&ar->sem);
265                 return -EBUSY;
266         }
267
268         if (ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)]) {
269                 /*
270                  * sleep until the command queue drains
271                  */
272                 wait_event_interruptible_timeout(ar->event_wq,
273                         ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)] == 0,
274                         WMI_TIMEOUT);
275                 if (signal_pending(current)) {
276                         ath6kl_err("cmd queue drain timeout\n");
277                         up(&ar->sem);
278                         return -EINTR;
279                 }
280         }
281
282         if (test_bit(CONNECTED, &ar->flag) &&
283             ar->ssid_len == sme->ssid_len &&
284             !memcmp(ar->ssid, sme->ssid, ar->ssid_len)) {
285                 ar->reconnect_flag = true;
286                 status = ath6kl_wmi_reconnect_cmd(ar->wmi, ar->req_bssid,
287                                                   ar->ch_hint);
288
289                 up(&ar->sem);
290                 if (status) {
291                         ath6kl_err("wmi_reconnect_cmd failed\n");
292                         return -EIO;
293                 }
294                 return 0;
295         } else if (ar->ssid_len == sme->ssid_len &&
296                    !memcmp(ar->ssid, sme->ssid, ar->ssid_len)) {
297                 ath6kl_disconnect(ar);
298         }
299
300         memset(ar->ssid, 0, sizeof(ar->ssid));
301         ar->ssid_len = sme->ssid_len;
302         memcpy(ar->ssid, sme->ssid, sme->ssid_len);
303
304         if (sme->channel)
305                 ar->ch_hint = sme->channel->center_freq;
306
307         memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
308         if (sme->bssid && !is_broadcast_ether_addr(sme->bssid))
309                 memcpy(ar->req_bssid, sme->bssid, sizeof(ar->req_bssid));
310
311         ath6kl_set_wpa_version(ar, sme->crypto.wpa_versions);
312
313         status = ath6kl_set_auth_type(ar, sme->auth_type);
314         if (status) {
315                 up(&ar->sem);
316                 return status;
317         }
318
319         if (sme->crypto.n_ciphers_pairwise)
320                 ath6kl_set_cipher(ar, sme->crypto.ciphers_pairwise[0], true);
321         else
322                 ath6kl_set_cipher(ar, 0, true);
323
324         ath6kl_set_cipher(ar, sme->crypto.cipher_group, false);
325
326         if (sme->crypto.n_akm_suites)
327                 ath6kl_set_key_mgmt(ar, sme->crypto.akm_suites[0]);
328
329         if ((sme->key_len) &&
330             (ar->auth_mode == NONE_AUTH) && (ar->prwise_crypto == WEP_CRYPT)) {
331                 struct ath6kl_key *key = NULL;
332
333                 if (sme->key_idx < WMI_MIN_KEY_INDEX ||
334                     sme->key_idx > WMI_MAX_KEY_INDEX) {
335                         ath6kl_err("key index %d out of bounds\n",
336                                    sme->key_idx);
337                         up(&ar->sem);
338                         return -ENOENT;
339                 }
340
341                 key = &ar->keys[sme->key_idx];
342                 key->key_len = sme->key_len;
343                 memcpy(key->key, sme->key, key->key_len);
344                 key->cipher = ar->prwise_crypto;
345                 ar->def_txkey_index = sme->key_idx;
346
347                 ath6kl_wmi_addkey_cmd(ar->wmi, sme->key_idx,
348                                       ar->prwise_crypto,
349                                       GROUP_USAGE | TX_USAGE,
350                                       key->key_len,
351                                       NULL,
352                                       key->key, KEY_OP_INIT_VAL, NULL,
353                                       NO_SYNC_WMIFLAG);
354         }
355
356         if (!ar->usr_bss_filter) {
357                 if (ath6kl_wmi_bssfilter_cmd(ar->wmi, ALL_BSS_FILTER, 0) != 0) {
358                         ath6kl_err("couldn't set bss filtering\n");
359                         up(&ar->sem);
360                         return -EIO;
361                 }
362         }
363
364         ar->nw_type = ar->next_mode;
365
366         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
367                    "%s: connect called with authmode %d dot11 auth %d"
368                    " PW crypto %d PW crypto len %d GRP crypto %d"
369                    " GRP crypto len %d channel hint %u\n",
370                    __func__,
371                    ar->auth_mode, ar->dot11_auth_mode, ar->prwise_crypto,
372                    ar->prwise_crypto_len, ar->grp_crypto,
373                    ar->grp_crpto_len, ar->ch_hint);
374
375         ar->reconnect_flag = 0;
376         status = ath6kl_wmi_connect_cmd(ar->wmi, ar->nw_type,
377                                         ar->dot11_auth_mode, ar->auth_mode,
378                                         ar->prwise_crypto,
379                                         ar->prwise_crypto_len,
380                                         ar->grp_crypto, ar->grp_crpto_len,
381                                         ar->ssid_len, ar->ssid,
382                                         ar->req_bssid, ar->ch_hint,
383                                         ar->connect_ctrl_flags);
384
385         up(&ar->sem);
386
387         if (status == -EINVAL) {
388                 memset(ar->ssid, 0, sizeof(ar->ssid));
389                 ar->ssid_len = 0;
390                 ath6kl_err("invalid request\n");
391                 return -ENOENT;
392         } else if (status) {
393                 ath6kl_err("ath6kl_wmi_connect_cmd failed\n");
394                 return -EIO;
395         }
396
397         if ((!(ar->connect_ctrl_flags & CONNECT_DO_WPA_OFFLOAD)) &&
398             ((ar->auth_mode == WPA_PSK_AUTH)
399              || (ar->auth_mode == WPA2_PSK_AUTH))) {
400                 mod_timer(&ar->disconnect_timer,
401                           jiffies + msecs_to_jiffies(DISCON_TIMER_INTVAL));
402         }
403
404         ar->connect_ctrl_flags &= ~CONNECT_DO_WPA_OFFLOAD;
405         set_bit(CONNECT_PEND, &ar->flag);
406
407         return 0;
408 }
409
410 void ath6kl_cfg80211_connect_event(struct ath6kl *ar, u16 channel,
411                                    u8 *bssid, u16 listen_intvl,
412                                    u16 beacon_intvl,
413                                    enum network_type nw_type,
414                                    u8 beacon_ie_len, u8 assoc_req_len,
415                                    u8 assoc_resp_len, u8 *assoc_info)
416 {
417         u16 size = 0;
418         u16 capability = 0;
419         struct cfg80211_bss *bss = NULL;
420         struct ieee80211_mgmt *mgmt = NULL;
421         struct ieee80211_channel *ibss_ch = NULL;
422         s32 signal = 50 * 100;
423         u8 ie_buf_len = 0;
424         unsigned char ie_buf[256];
425         unsigned char *ptr_ie_buf = ie_buf;
426         unsigned char *ieeemgmtbuf = NULL;
427         u8 source_mac[ETH_ALEN];
428
429         /* capinfo + listen interval */
430         u8 assoc_req_ie_offset = sizeof(u16) + sizeof(u16);
431
432         /* capinfo + status code +  associd */
433         u8 assoc_resp_ie_offset = sizeof(u16) + sizeof(u16) + sizeof(u16);
434
435         u8 *assoc_req_ie = assoc_info + beacon_ie_len + assoc_req_ie_offset;
436         u8 *assoc_resp_ie = assoc_info + beacon_ie_len + assoc_req_len +
437             assoc_resp_ie_offset;
438
439         assoc_req_len -= assoc_req_ie_offset;
440         assoc_resp_len -= assoc_resp_ie_offset;
441
442         ar->auto_auth_stage = AUTH_IDLE;
443
444         if (nw_type & ADHOC_NETWORK) {
445                 if (ar->wdev->iftype != NL80211_IFTYPE_ADHOC) {
446                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
447                                    "%s: ath6k not in ibss mode\n", __func__);
448                         return;
449                 }
450         }
451
452         if (nw_type & INFRA_NETWORK) {
453                 if (ar->wdev->iftype != NL80211_IFTYPE_STATION) {
454                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
455                                    "%s: ath6k not in station mode\n", __func__);
456                         return;
457                 }
458         }
459
460         /*
461          * Earlier we were updating the cfg about bss by making a beacon frame
462          * only if the entry for bss is not there. This can have some issue if
463          * ROAM event is generated and a heavy traffic is ongoing. The ROAM
464          * event is handled through a work queue and by the time it really gets
465          * handled, BSS would have been aged out. So it is better to update the
466          * cfg about BSS irrespective of its entry being present right now or
467          * not.
468          */
469
470         if (nw_type & ADHOC_NETWORK) {
471                 /* construct 802.11 mgmt beacon */
472                 if (ptr_ie_buf) {
473                         *ptr_ie_buf++ = WLAN_EID_SSID;
474                         *ptr_ie_buf++ = ar->ssid_len;
475                         memcpy(ptr_ie_buf, ar->ssid, ar->ssid_len);
476                         ptr_ie_buf += ar->ssid_len;
477
478                         *ptr_ie_buf++ = WLAN_EID_IBSS_PARAMS;
479                         *ptr_ie_buf++ = 2;      /* length */
480                         *ptr_ie_buf++ = 0;      /* ATIM window */
481                         *ptr_ie_buf++ = 0;      /* ATIM window */
482
483                         /* TODO: update ibss params and include supported rates,
484                          * DS param set, extened support rates, wmm. */
485
486                         ie_buf_len = ptr_ie_buf - ie_buf;
487                 }
488
489                 capability |= WLAN_CAPABILITY_IBSS;
490
491                 if (ar->prwise_crypto == WEP_CRYPT)
492                         capability |= WLAN_CAPABILITY_PRIVACY;
493
494                 memcpy(source_mac, ar->net_dev->dev_addr, ETH_ALEN);
495                 ptr_ie_buf = ie_buf;
496         } else {
497                 capability = *(u16 *) (&assoc_info[beacon_ie_len]);
498                 memcpy(source_mac, bssid, ETH_ALEN);
499                 ptr_ie_buf = assoc_req_ie;
500                 ie_buf_len = assoc_req_len;
501         }
502
503         size = offsetof(struct ieee80211_mgmt, u)
504         + sizeof(mgmt->u.beacon)
505         + ie_buf_len;
506
507         ieeemgmtbuf = kzalloc(size, GFP_ATOMIC);
508         if (!ieeemgmtbuf) {
509                 ath6kl_err("ieee mgmt buf alloc error\n");
510                 return;
511         }
512
513         mgmt = (struct ieee80211_mgmt *)ieeemgmtbuf;
514         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
515                                           IEEE80211_STYPE_BEACON);
516         memset(mgmt->da, 0xff, ETH_ALEN);       /* broadcast addr */
517         memcpy(mgmt->sa, source_mac, ETH_ALEN);
518         memcpy(mgmt->bssid, bssid, ETH_ALEN);
519         mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_intvl);
520         mgmt->u.beacon.capab_info = cpu_to_le16(capability);
521         memcpy(mgmt->u.beacon.variable, ptr_ie_buf, ie_buf_len);
522
523         ibss_ch = ieee80211_get_channel(ar->wdev->wiphy, (int)channel);
524
525         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
526                    "%s: inform bss with bssid %pM channel %d beacon_intvl %d capability 0x%x\n",
527                    __func__, mgmt->bssid, ibss_ch->hw_value,
528                    beacon_intvl, capability);
529
530         bss = cfg80211_inform_bss_frame(ar->wdev->wiphy,
531                                         ibss_ch, mgmt,
532                                         size, signal, GFP_KERNEL);
533         kfree(ieeemgmtbuf);
534         cfg80211_put_bss(bss);
535
536         if (nw_type & ADHOC_NETWORK) {
537                 cfg80211_ibss_joined(ar->net_dev, bssid, GFP_KERNEL);
538                 return;
539         }
540
541         if (ar->sme_state == SME_CONNECTING) {
542                 /* inform connect result to cfg80211 */
543                 ar->sme_state = SME_CONNECTED;
544                 cfg80211_connect_result(ar->net_dev, bssid,
545                                         assoc_req_ie, assoc_req_len,
546                                         assoc_resp_ie, assoc_resp_len,
547                                         WLAN_STATUS_SUCCESS, GFP_KERNEL);
548         } else if (ar->sme_state == SME_CONNECTED) {
549                 /* inform roam event to cfg80211 */
550                 cfg80211_roamed(ar->net_dev, ibss_ch, bssid,
551                                 assoc_req_ie, assoc_req_len,
552                                 assoc_resp_ie, assoc_resp_len, GFP_KERNEL);
553         }
554 }
555
556 static int ath6kl_cfg80211_disconnect(struct wiphy *wiphy,
557                                       struct net_device *dev, u16 reason_code)
558 {
559         struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(dev);
560
561         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: reason=%u\n", __func__,
562                    reason_code);
563
564         if (!ath6kl_cfg80211_ready(ar))
565                 return -EIO;
566
567         if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
568                 ath6kl_err("busy, destroy in progress\n");
569                 return -EBUSY;
570         }
571
572         if (down_interruptible(&ar->sem)) {
573                 ath6kl_err("busy, couldn't get access\n");
574                 return -ERESTARTSYS;
575         }
576
577         ar->reconnect_flag = 0;
578         ath6kl_disconnect(ar);
579         memset(ar->ssid, 0, sizeof(ar->ssid));
580         ar->ssid_len = 0;
581
582         if (!test_bit(SKIP_SCAN, &ar->flag))
583                 memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
584
585         up(&ar->sem);
586
587         return 0;
588 }
589
590 void ath6kl_cfg80211_disconnect_event(struct ath6kl *ar, u8 reason,
591                                       u8 *bssid, u8 assoc_resp_len,
592                                       u8 *assoc_info, u16 proto_reason)
593 {
594         struct ath6kl_key *key = NULL;
595         u16 status;
596
597         if (ar->scan_req) {
598                 cfg80211_scan_done(ar->scan_req, true);
599                 ar->scan_req = NULL;
600         }
601
602         if (ar->nw_type & ADHOC_NETWORK) {
603                 if (ar->wdev->iftype != NL80211_IFTYPE_ADHOC) {
604                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
605                                    "%s: ath6k not in ibss mode\n", __func__);
606                         return;
607                 }
608                 memset(bssid, 0, ETH_ALEN);
609                 cfg80211_ibss_joined(ar->net_dev, bssid, GFP_KERNEL);
610                 return;
611         }
612
613         if (ar->nw_type & INFRA_NETWORK) {
614                 if (ar->wdev->iftype != NL80211_IFTYPE_STATION) {
615                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
616                                    "%s: ath6k not in station mode\n", __func__);
617                         return;
618                 }
619         }
620
621         if (!test_bit(CONNECT_PEND, &ar->flag)) {
622                 if (reason != DISCONNECT_CMD)
623                         ath6kl_wmi_disconnect_cmd(ar->wmi);
624
625                 return;
626         }
627
628         if (reason == NO_NETWORK_AVAIL) {
629                 /* connect cmd failed */
630                 ath6kl_wmi_disconnect_cmd(ar->wmi);
631                 return;
632         }
633
634         if (reason != DISCONNECT_CMD)
635                 return;
636
637         if (!ar->auto_auth_stage) {
638                 clear_bit(CONNECT_PEND, &ar->flag);
639
640                 if (ar->sme_state == SME_CONNECTING) {
641                         cfg80211_connect_result(ar->net_dev,
642                                                 bssid, NULL, 0,
643                                                 NULL, 0,
644                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
645                                                 GFP_KERNEL);
646                 } else if (ar->sme_state == SME_CONNECTED) {
647                         cfg80211_disconnected(ar->net_dev, reason,
648                                               NULL, 0, GFP_KERNEL);
649                 }
650
651                 ar->sme_state = SME_DISCONNECTED;
652                 return;
653         }
654
655         if (ar->dot11_auth_mode != OPEN_AUTH)
656                 return;
657
658         /*
659          * If the current auth algorithm is open, try shared and
660          * make autoAuthStage idle. We do not make it leap for now
661          * being.
662          */
663         key = &ar->keys[ar->def_txkey_index];
664         if (down_interruptible(&ar->sem)) {
665                 ath6kl_err("busy, couldn't get access\n");
666                 return;
667         }
668
669         ar->dot11_auth_mode = SHARED_AUTH;
670         ar->auto_auth_stage = AUTH_IDLE;
671
672         ath6kl_wmi_addkey_cmd(ar->wmi,
673                               ar->def_txkey_index,
674                               ar->prwise_crypto,
675                               GROUP_USAGE | TX_USAGE,
676                               key->key_len, NULL,
677                               key->key,
678                               KEY_OP_INIT_VAL, NULL,
679                               NO_SYNC_WMIFLAG);
680
681         status = ath6kl_wmi_connect_cmd(ar->wmi,
682                                         ar->nw_type,
683                                         ar->dot11_auth_mode,
684                                         ar->auth_mode,
685                                         ar->prwise_crypto,
686                                         ar->prwise_crypto_len,
687                                         ar->grp_crypto,
688                                         ar->grp_crpto_len,
689                                         ar->ssid_len,
690                                         ar->ssid,
691                                         ar->req_bssid,
692                                         ar->ch_hint,
693                                         ar->connect_ctrl_flags);
694         up(&ar->sem);
695 }
696
697 static inline bool is_ch_11a(u16 ch)
698 {
699         return (!((ch >= 2412) && (ch <= 2484)));
700 }
701
702 /* struct ath6kl_node_table::nt_nodelock is locked when calling this */
703 void ath6kl_cfg80211_scan_node(struct wiphy *wiphy, struct bss *ni)
704 {
705         struct ieee80211_mgmt *mgmt;
706         struct ieee80211_channel *channel;
707         struct ieee80211_supported_band *band;
708         struct ath6kl_common_ie *cie;
709         s32 signal;
710         int freq;
711
712         cie = &ni->ni_cie;
713
714         if (is_ch_11a(cie->ie_chan))
715                 band = wiphy->bands[IEEE80211_BAND_5GHZ]; /* 11a */
716         else if ((cie->ie_erp) || (cie->ie_xrates))
717                 band = wiphy->bands[IEEE80211_BAND_2GHZ]; /* 11g */
718         else
719                 band = wiphy->bands[IEEE80211_BAND_2GHZ]; /* 11b */
720
721         freq = cie->ie_chan;
722         channel = ieee80211_get_channel(wiphy, freq);
723         signal = ni->ni_snr * 100;
724
725         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
726                    "%s: bssid %pM ch %d freq %d size %d\n", __func__,
727                    ni->ni_macaddr, channel->hw_value, freq, ni->ni_framelen);
728         /*
729          * Both Beacon and Probe Response frames have same payload structure,
730          * so it is fine to share the parser for both.
731          */
732         if (ni->ni_framelen < 8 + 2 + 2)
733                 return;
734         mgmt = (struct ieee80211_mgmt *) (ni->ni_buf -
735                                           offsetof(struct ieee80211_mgmt, u));
736         cfg80211_inform_bss(wiphy, channel, ni->ni_macaddr,
737                             le64_to_cpu(mgmt->u.beacon.timestamp),
738                             le16_to_cpu(mgmt->u.beacon.capab_info),
739                             le16_to_cpu(mgmt->u.beacon.beacon_int),
740                             mgmt->u.beacon.variable,
741                             ni->ni_buf + ni->ni_framelen -
742                             mgmt->u.beacon.variable,
743                             signal, GFP_ATOMIC);
744 }
745
746 static int ath6kl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
747                                 struct cfg80211_scan_request *request)
748 {
749         struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
750         int ret = 0;
751
752         if (!ath6kl_cfg80211_ready(ar))
753                 return -EIO;
754
755         if (!ar->usr_bss_filter) {
756                 if (ath6kl_wmi_bssfilter_cmd(ar->wmi,
757                                              (test_bit(CONNECTED, &ar->flag) ?
758                                              ALL_BUT_BSS_FILTER :
759                                              ALL_BSS_FILTER), 0) != 0) {
760                         ath6kl_err("couldn't set bss filtering\n");
761                         return -EIO;
762                 }
763         }
764
765         if (request->n_ssids && request->ssids[0].ssid_len) {
766                 u8 i;
767
768                 if (request->n_ssids > (MAX_PROBED_SSID_INDEX - 1))
769                         request->n_ssids = MAX_PROBED_SSID_INDEX - 1;
770
771                 for (i = 0; i < request->n_ssids; i++)
772                         ath6kl_wmi_probedssid_cmd(ar->wmi, i + 1,
773                                                   SPECIFIC_SSID_FLAG,
774                                                   request->ssids[i].ssid_len,
775                                                   request->ssids[i].ssid);
776         }
777
778         if (ath6kl_wmi_startscan_cmd(ar->wmi, WMI_LONG_SCAN, 0,
779                                      false, 0, 0, 0, NULL) != 0) {
780                 ath6kl_err("wmi_startscan_cmd failed\n");
781                 ret = -EIO;
782         }
783
784         ar->scan_req = request;
785
786         return ret;
787 }
788
789 void ath6kl_cfg80211_scan_complete_event(struct ath6kl *ar, int status)
790 {
791         int i;
792
793         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: status %d\n", __func__, status);
794
795         if (!ar->scan_req)
796                 return;
797
798         if ((status == -ECANCELED) || (status == -EBUSY)) {
799                 cfg80211_scan_done(ar->scan_req, true);
800                 goto out;
801         }
802
803         /* Translate data to cfg80211 mgmt format */
804         wlan_iterate_nodes(&ar->scan_table, ar->wdev->wiphy);
805
806         cfg80211_scan_done(ar->scan_req, false);
807
808         if (ar->scan_req->n_ssids && ar->scan_req->ssids[0].ssid_len) {
809                 for (i = 0; i < ar->scan_req->n_ssids; i++) {
810                         ath6kl_wmi_probedssid_cmd(ar->wmi, i + 1,
811                                                   DISABLE_SSID_FLAG,
812                                                   0, NULL);
813                 }
814         }
815
816 out:
817         ar->scan_req = NULL;
818 }
819
820 static int ath6kl_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
821                                    u8 key_index, bool pairwise,
822                                    const u8 *mac_addr,
823                                    struct key_params *params)
824 {
825         struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
826         struct ath6kl_key *key = NULL;
827         u8 key_usage;
828         u8 key_type;
829         int status = 0;
830
831         if (!ath6kl_cfg80211_ready(ar))
832                 return -EIO;
833
834         if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
835                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
836                            "%s: key index %d out of bounds\n", __func__,
837                            key_index);
838                 return -ENOENT;
839         }
840
841         key = &ar->keys[key_index];
842         memset(key, 0, sizeof(struct ath6kl_key));
843
844         if (pairwise)
845                 key_usage = PAIRWISE_USAGE;
846         else
847                 key_usage = GROUP_USAGE;
848
849         if (params) {
850                 if (params->key_len > WLAN_MAX_KEY_LEN ||
851                     params->seq_len > sizeof(key->seq))
852                         return -EINVAL;
853
854                 key->key_len = params->key_len;
855                 memcpy(key->key, params->key, key->key_len);
856                 key->seq_len = params->seq_len;
857                 memcpy(key->seq, params->seq, key->seq_len);
858                 key->cipher = params->cipher;
859         }
860
861         switch (key->cipher) {
862         case WLAN_CIPHER_SUITE_WEP40:
863         case WLAN_CIPHER_SUITE_WEP104:
864                 key_type = WEP_CRYPT;
865                 break;
866
867         case WLAN_CIPHER_SUITE_TKIP:
868                 key_type = TKIP_CRYPT;
869                 break;
870
871         case WLAN_CIPHER_SUITE_CCMP:
872                 key_type = AES_CRYPT;
873                 break;
874
875         default:
876                 return -ENOTSUPP;
877         }
878
879         if (((ar->auth_mode == WPA_PSK_AUTH)
880              || (ar->auth_mode == WPA2_PSK_AUTH))
881             && (key_usage & GROUP_USAGE))
882                 del_timer(&ar->disconnect_timer);
883
884         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
885                    "%s: index %d, key_len %d, key_type 0x%x, key_usage 0x%x, seq_len %d\n",
886                    __func__, key_index, key->key_len, key_type,
887                    key_usage, key->seq_len);
888
889         ar->def_txkey_index = key_index;
890         status = ath6kl_wmi_addkey_cmd(ar->wmi, ar->def_txkey_index,
891                                        key_type, key_usage, key->key_len,
892                                        key->seq, key->key, KEY_OP_INIT_VAL,
893                                        (u8 *) mac_addr, SYNC_BOTH_WMIFLAG);
894
895         if (status)
896                 return -EIO;
897
898         return 0;
899 }
900
901 static int ath6kl_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
902                                    u8 key_index, bool pairwise,
903                                    const u8 *mac_addr)
904 {
905         struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
906
907         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
908
909         if (!ath6kl_cfg80211_ready(ar))
910                 return -EIO;
911
912         if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
913                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
914                            "%s: key index %d out of bounds\n", __func__,
915                            key_index);
916                 return -ENOENT;
917         }
918
919         if (!ar->keys[key_index].key_len) {
920                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
921                            "%s: index %d is empty\n", __func__, key_index);
922                 return 0;
923         }
924
925         ar->keys[key_index].key_len = 0;
926
927         return ath6kl_wmi_deletekey_cmd(ar->wmi, key_index);
928 }
929
930 static int ath6kl_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
931                                    u8 key_index, bool pairwise,
932                                    const u8 *mac_addr, void *cookie,
933                                    void (*callback) (void *cookie,
934                                                      struct key_params *))
935 {
936         struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
937         struct ath6kl_key *key = NULL;
938         struct key_params params;
939
940         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
941
942         if (!ath6kl_cfg80211_ready(ar))
943                 return -EIO;
944
945         if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
946                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
947                            "%s: key index %d out of bounds\n", __func__,
948                            key_index);
949                 return -ENOENT;
950         }
951
952         key = &ar->keys[key_index];
953         memset(&params, 0, sizeof(params));
954         params.cipher = key->cipher;
955         params.key_len = key->key_len;
956         params.seq_len = key->seq_len;
957         params.seq = key->seq;
958         params.key = key->key;
959
960         callback(cookie, &params);
961
962         return key->key_len ? 0 : -ENOENT;
963 }
964
965 static int ath6kl_cfg80211_set_default_key(struct wiphy *wiphy,
966                                            struct net_device *ndev,
967                                            u8 key_index, bool unicast,
968                                            bool multicast)
969 {
970         struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
971         struct ath6kl_key *key = NULL;
972         int status = 0;
973         u8 key_usage;
974
975         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
976
977         if (!ath6kl_cfg80211_ready(ar))
978                 return -EIO;
979
980         if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
981                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
982                            "%s: key index %d out of bounds\n",
983                            __func__, key_index);
984                 return -ENOENT;
985         }
986
987         if (!ar->keys[key_index].key_len) {
988                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: invalid key index %d\n",
989                            __func__, key_index);
990                 return -EINVAL;
991         }
992
993         ar->def_txkey_index = key_index;
994         key = &ar->keys[ar->def_txkey_index];
995         key_usage = GROUP_USAGE;
996         if (ar->prwise_crypto == WEP_CRYPT)
997                 key_usage |= TX_USAGE;
998
999         status = ath6kl_wmi_addkey_cmd(ar->wmi, ar->def_txkey_index,
1000                                        ar->prwise_crypto, key_usage,
1001                                        key->key_len, key->seq, key->key,
1002                                        KEY_OP_INIT_VAL, NULL,
1003                                        SYNC_BOTH_WMIFLAG);
1004         if (status)
1005                 return -EIO;
1006
1007         return 0;
1008 }
1009
1010 void ath6kl_cfg80211_tkip_micerr_event(struct ath6kl *ar, u8 keyid,
1011                                        bool ismcast)
1012 {
1013         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1014                    "%s: keyid %d, ismcast %d\n", __func__, keyid, ismcast);
1015
1016         cfg80211_michael_mic_failure(ar->net_dev, ar->bssid,
1017                                      (ismcast ? NL80211_KEYTYPE_GROUP :
1018                                       NL80211_KEYTYPE_PAIRWISE), keyid, NULL,
1019                                      GFP_KERNEL);
1020 }
1021
1022 static int ath6kl_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1023 {
1024         struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1025         int ret;
1026
1027         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: changed 0x%x\n", __func__,
1028                    changed);
1029
1030         if (!ath6kl_cfg80211_ready(ar))
1031                 return -EIO;
1032
1033         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1034                 ret = ath6kl_wmi_set_rts_cmd(ar->wmi, wiphy->rts_threshold);
1035                 if (ret != 0) {
1036                         ath6kl_err("ath6kl_wmi_set_rts_cmd failed\n");
1037                         return -EIO;
1038                 }
1039         }
1040
1041         return 0;
1042 }
1043
1044 /*
1045  * The type nl80211_tx_power_setting replaces the following
1046  * data type from 2.6.36 onwards
1047 */
1048 static int ath6kl_cfg80211_set_txpower(struct wiphy *wiphy,
1049                                        enum nl80211_tx_power_setting type,
1050                                        int dbm)
1051 {
1052         struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1053         u8 ath6kl_dbm;
1054
1055         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x, dbm %d\n", __func__,
1056                    type, dbm);
1057
1058         if (!ath6kl_cfg80211_ready(ar))
1059                 return -EIO;
1060
1061         switch (type) {
1062         case NL80211_TX_POWER_AUTOMATIC:
1063                 return 0;
1064         case NL80211_TX_POWER_LIMITED:
1065                 ar->tx_pwr = ath6kl_dbm = dbm;
1066                 break;
1067         default:
1068                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x not supported\n",
1069                            __func__, type);
1070                 return -EOPNOTSUPP;
1071         }
1072
1073         ath6kl_wmi_set_tx_pwr_cmd(ar->wmi, ath6kl_dbm);
1074
1075         return 0;
1076 }
1077
1078 static int ath6kl_cfg80211_get_txpower(struct wiphy *wiphy, int *dbm)
1079 {
1080         struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1081
1082         if (!ath6kl_cfg80211_ready(ar))
1083                 return -EIO;
1084
1085         if (test_bit(CONNECTED, &ar->flag)) {
1086                 ar->tx_pwr = 0;
1087
1088                 if (ath6kl_wmi_get_tx_pwr_cmd(ar->wmi) != 0) {
1089                         ath6kl_err("ath6kl_wmi_get_tx_pwr_cmd failed\n");
1090                         return -EIO;
1091                 }
1092
1093                 wait_event_interruptible_timeout(ar->event_wq, ar->tx_pwr != 0,
1094                                                  5 * HZ);
1095
1096                 if (signal_pending(current)) {
1097                         ath6kl_err("target did not respond\n");
1098                         return -EINTR;
1099                 }
1100         }
1101
1102         *dbm = ar->tx_pwr;
1103         return 0;
1104 }
1105
1106 static int ath6kl_cfg80211_set_power_mgmt(struct wiphy *wiphy,
1107                                           struct net_device *dev,
1108                                           bool pmgmt, int timeout)
1109 {
1110         struct ath6kl *ar = ath6kl_priv(dev);
1111         struct wmi_power_mode_cmd mode;
1112
1113         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: pmgmt %d, timeout %d\n",
1114                    __func__, pmgmt, timeout);
1115
1116         if (!ath6kl_cfg80211_ready(ar))
1117                 return -EIO;
1118
1119         if (pmgmt) {
1120                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: max perf\n", __func__);
1121                 mode.pwr_mode = REC_POWER;
1122         } else {
1123                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: rec power\n", __func__);
1124                 mode.pwr_mode = MAX_PERF_POWER;
1125         }
1126
1127         if (ath6kl_wmi_powermode_cmd(ar->wmi, mode.pwr_mode) != 0) {
1128                 ath6kl_err("wmi_powermode_cmd failed\n");
1129                 return -EIO;
1130         }
1131
1132         return 0;
1133 }
1134
1135 static int ath6kl_cfg80211_change_iface(struct wiphy *wiphy,
1136                                         struct net_device *ndev,
1137                                         enum nl80211_iftype type, u32 *flags,
1138                                         struct vif_params *params)
1139 {
1140         struct ath6kl *ar = ath6kl_priv(ndev);
1141         struct wireless_dev *wdev = ar->wdev;
1142
1143         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type %u\n", __func__, type);
1144
1145         if (!ath6kl_cfg80211_ready(ar))
1146                 return -EIO;
1147
1148         switch (type) {
1149         case NL80211_IFTYPE_STATION:
1150                 ar->next_mode = INFRA_NETWORK;
1151                 break;
1152         case NL80211_IFTYPE_ADHOC:
1153                 ar->next_mode = ADHOC_NETWORK;
1154                 break;
1155         default:
1156                 ath6kl_err("invalid interface type %u\n", type);
1157                 return -EOPNOTSUPP;
1158         }
1159
1160         wdev->iftype = type;
1161
1162         return 0;
1163 }
1164
1165 static int ath6kl_cfg80211_join_ibss(struct wiphy *wiphy,
1166                                      struct net_device *dev,
1167                                      struct cfg80211_ibss_params *ibss_param)
1168 {
1169         struct ath6kl *ar = ath6kl_priv(dev);
1170         int status;
1171
1172         if (!ath6kl_cfg80211_ready(ar))
1173                 return -EIO;
1174
1175         ar->ssid_len = ibss_param->ssid_len;
1176         memcpy(ar->ssid, ibss_param->ssid, ar->ssid_len);
1177
1178         if (ibss_param->channel)
1179                 ar->ch_hint = ibss_param->channel->center_freq;
1180
1181         if (ibss_param->channel_fixed) {
1182                 /*
1183                  * TODO: channel_fixed: The channel should be fixed, do not
1184                  * search for IBSSs to join on other channels. Target
1185                  * firmware does not support this feature, needs to be
1186                  * updated.
1187                  */
1188                 return -EOPNOTSUPP;
1189         }
1190
1191         memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
1192         if (ibss_param->bssid && !is_broadcast_ether_addr(ibss_param->bssid))
1193                 memcpy(ar->req_bssid, ibss_param->bssid, sizeof(ar->req_bssid));
1194
1195         ath6kl_set_wpa_version(ar, 0);
1196
1197         status = ath6kl_set_auth_type(ar, NL80211_AUTHTYPE_OPEN_SYSTEM);
1198         if (status)
1199                 return status;
1200
1201         if (ibss_param->privacy) {
1202                 ath6kl_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, true);
1203                 ath6kl_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, false);
1204         } else {
1205                 ath6kl_set_cipher(ar, 0, true);
1206                 ath6kl_set_cipher(ar, 0, false);
1207         }
1208
1209         ar->nw_type = ar->next_mode;
1210
1211         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1212                    "%s: connect called with authmode %d dot11 auth %d"
1213                    " PW crypto %d PW crypto len %d GRP crypto %d"
1214                    " GRP crypto len %d channel hint %u\n",
1215                    __func__,
1216                    ar->auth_mode, ar->dot11_auth_mode, ar->prwise_crypto,
1217                    ar->prwise_crypto_len, ar->grp_crypto,
1218                    ar->grp_crpto_len, ar->ch_hint);
1219
1220         status = ath6kl_wmi_connect_cmd(ar->wmi, ar->nw_type,
1221                                         ar->dot11_auth_mode, ar->auth_mode,
1222                                         ar->prwise_crypto,
1223                                         ar->prwise_crypto_len,
1224                                         ar->grp_crypto, ar->grp_crpto_len,
1225                                         ar->ssid_len, ar->ssid,
1226                                         ar->req_bssid, ar->ch_hint,
1227                                         ar->connect_ctrl_flags);
1228         set_bit(CONNECT_PEND, &ar->flag);
1229
1230         return 0;
1231 }
1232
1233 static int ath6kl_cfg80211_leave_ibss(struct wiphy *wiphy,
1234                                       struct net_device *dev)
1235 {
1236         struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(dev);
1237
1238         if (!ath6kl_cfg80211_ready(ar))
1239                 return -EIO;
1240
1241         ath6kl_disconnect(ar);
1242         memset(ar->ssid, 0, sizeof(ar->ssid));
1243         ar->ssid_len = 0;
1244
1245         return 0;
1246 }
1247
1248 static const u32 cipher_suites[] = {
1249         WLAN_CIPHER_SUITE_WEP40,
1250         WLAN_CIPHER_SUITE_WEP104,
1251         WLAN_CIPHER_SUITE_TKIP,
1252         WLAN_CIPHER_SUITE_CCMP,
1253 };
1254
1255 static bool is_rate_legacy(s32 rate)
1256 {
1257         static const s32 legacy[] = { 1000, 2000, 5500, 11000,
1258                 6000, 9000, 12000, 18000, 24000,
1259                 36000, 48000, 54000
1260         };
1261         u8 i;
1262
1263         for (i = 0; i < ARRAY_SIZE(legacy); i++)
1264                 if (rate == legacy[i])
1265                         return true;
1266
1267         return false;
1268 }
1269
1270 static bool is_rate_ht20(s32 rate, u8 *mcs, bool *sgi)
1271 {
1272         static const s32 ht20[] = { 6500, 13000, 19500, 26000, 39000,
1273                 52000, 58500, 65000, 72200
1274         };
1275         u8 i;
1276
1277         for (i = 0; i < ARRAY_SIZE(ht20); i++) {
1278                 if (rate == ht20[i]) {
1279                         if (i == ARRAY_SIZE(ht20) - 1)
1280                                 /* last rate uses sgi */
1281                                 *sgi = true;
1282                         else
1283                                 *sgi = false;
1284
1285                         *mcs = i;
1286                         return true;
1287                 }
1288         }
1289         return false;
1290 }
1291
1292 static bool is_rate_ht40(s32 rate, u8 *mcs, bool *sgi)
1293 {
1294         static const s32 ht40[] = { 13500, 27000, 40500, 54000,
1295                 81000, 108000, 121500, 135000,
1296                 150000
1297         };
1298         u8 i;
1299
1300         for (i = 0; i < ARRAY_SIZE(ht40); i++) {
1301                 if (rate == ht40[i]) {
1302                         if (i == ARRAY_SIZE(ht40) - 1)
1303                                 /* last rate uses sgi */
1304                                 *sgi = true;
1305                         else
1306                                 *sgi = false;
1307
1308                         *mcs = i;
1309                         return true;
1310                 }
1311         }
1312
1313         return false;
1314 }
1315
1316 static int ath6kl_get_station(struct wiphy *wiphy, struct net_device *dev,
1317                               u8 *mac, struct station_info *sinfo)
1318 {
1319         struct ath6kl *ar = ath6kl_priv(dev);
1320         long left;
1321         bool sgi;
1322         s32 rate;
1323         int ret;
1324         u8 mcs;
1325
1326         if (memcmp(mac, ar->bssid, ETH_ALEN) != 0)
1327                 return -ENOENT;
1328
1329         if (down_interruptible(&ar->sem))
1330                 return -EBUSY;
1331
1332         set_bit(STATS_UPDATE_PEND, &ar->flag);
1333
1334         ret = ath6kl_wmi_get_stats_cmd(ar->wmi);
1335
1336         if (ret != 0) {
1337                 up(&ar->sem);
1338                 return -EIO;
1339         }
1340
1341         left = wait_event_interruptible_timeout(ar->event_wq,
1342                                                 !test_bit(STATS_UPDATE_PEND,
1343                                                           &ar->flag),
1344                                                 WMI_TIMEOUT);
1345
1346         up(&ar->sem);
1347
1348         if (left == 0)
1349                 return -ETIMEDOUT;
1350         else if (left < 0)
1351                 return left;
1352
1353         if (ar->target_stats.rx_byte) {
1354                 sinfo->rx_bytes = ar->target_stats.rx_byte;
1355                 sinfo->filled |= STATION_INFO_RX_BYTES;
1356                 sinfo->rx_packets = ar->target_stats.rx_pkt;
1357                 sinfo->filled |= STATION_INFO_RX_PACKETS;
1358         }
1359
1360         if (ar->target_stats.tx_byte) {
1361                 sinfo->tx_bytes = ar->target_stats.tx_byte;
1362                 sinfo->filled |= STATION_INFO_TX_BYTES;
1363                 sinfo->tx_packets = ar->target_stats.tx_pkt;
1364                 sinfo->filled |= STATION_INFO_TX_PACKETS;
1365         }
1366
1367         sinfo->signal = ar->target_stats.cs_rssi;
1368         sinfo->filled |= STATION_INFO_SIGNAL;
1369
1370         rate = ar->target_stats.tx_ucast_rate;
1371
1372         if (is_rate_legacy(rate)) {
1373                 sinfo->txrate.legacy = rate / 100;
1374         } else if (is_rate_ht20(rate, &mcs, &sgi)) {
1375                 if (sgi) {
1376                         sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1377                         sinfo->txrate.mcs = mcs - 1;
1378                 } else {
1379                         sinfo->txrate.mcs = mcs;
1380                 }
1381
1382                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1383         } else if (is_rate_ht40(rate, &mcs, &sgi)) {
1384                 if (sgi) {
1385                         sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1386                         sinfo->txrate.mcs = mcs - 1;
1387                 } else {
1388                         sinfo->txrate.mcs = mcs;
1389                 }
1390
1391                 sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
1392                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1393         } else {
1394                 ath6kl_warn("invalid rate: %d\n", rate);
1395                 return 0;
1396         }
1397
1398         sinfo->filled |= STATION_INFO_TX_BITRATE;
1399
1400         return 0;
1401 }
1402
1403 static int ath6kl_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1404                             struct cfg80211_pmksa *pmksa)
1405 {
1406         struct ath6kl *ar = ath6kl_priv(netdev);
1407         return ath6kl_wmi_setpmkid_cmd(ar->wmi, pmksa->bssid,
1408                                        pmksa->pmkid, true);
1409 }
1410
1411 static int ath6kl_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1412                             struct cfg80211_pmksa *pmksa)
1413 {
1414         struct ath6kl *ar = ath6kl_priv(netdev);
1415         return ath6kl_wmi_setpmkid_cmd(ar->wmi, pmksa->bssid,
1416                                        pmksa->pmkid, false);
1417 }
1418
1419 static int ath6kl_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
1420 {
1421         struct ath6kl *ar = ath6kl_priv(netdev);
1422         if (test_bit(CONNECTED, &ar->flag))
1423                 return ath6kl_wmi_setpmkid_cmd(ar->wmi, ar->bssid, NULL, false);
1424         return 0;
1425 }
1426
1427 static struct cfg80211_ops ath6kl_cfg80211_ops = {
1428         .change_virtual_intf = ath6kl_cfg80211_change_iface,
1429         .scan = ath6kl_cfg80211_scan,
1430         .connect = ath6kl_cfg80211_connect,
1431         .disconnect = ath6kl_cfg80211_disconnect,
1432         .add_key = ath6kl_cfg80211_add_key,
1433         .get_key = ath6kl_cfg80211_get_key,
1434         .del_key = ath6kl_cfg80211_del_key,
1435         .set_default_key = ath6kl_cfg80211_set_default_key,
1436         .set_wiphy_params = ath6kl_cfg80211_set_wiphy_params,
1437         .set_tx_power = ath6kl_cfg80211_set_txpower,
1438         .get_tx_power = ath6kl_cfg80211_get_txpower,
1439         .set_power_mgmt = ath6kl_cfg80211_set_power_mgmt,
1440         .join_ibss = ath6kl_cfg80211_join_ibss,
1441         .leave_ibss = ath6kl_cfg80211_leave_ibss,
1442         .get_station = ath6kl_get_station,
1443         .set_pmksa = ath6kl_set_pmksa,
1444         .del_pmksa = ath6kl_del_pmksa,
1445         .flush_pmksa = ath6kl_flush_pmksa,
1446 };
1447
1448 struct wireless_dev *ath6kl_cfg80211_init(struct device *dev)
1449 {
1450         int ret = 0;
1451         struct wireless_dev *wdev;
1452
1453         wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
1454         if (!wdev) {
1455                 ath6kl_err("couldn't allocate wireless device\n");
1456                 return NULL;
1457         }
1458
1459         /* create a new wiphy for use with cfg80211 */
1460         wdev->wiphy = wiphy_new(&ath6kl_cfg80211_ops, sizeof(struct ath6kl));
1461         if (!wdev->wiphy) {
1462                 ath6kl_err("couldn't allocate wiphy device\n");
1463                 kfree(wdev);
1464                 return NULL;
1465         }
1466
1467         /* set device pointer for wiphy */
1468         set_wiphy_dev(wdev->wiphy, dev);
1469
1470         wdev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1471             BIT(NL80211_IFTYPE_ADHOC);
1472         /* max num of ssids that can be probed during scanning */
1473         wdev->wiphy->max_scan_ssids = MAX_PROBED_SSID_INDEX;
1474         wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &ath6kl_band_2ghz;
1475         wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &ath6kl_band_5ghz;
1476         wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1477
1478         wdev->wiphy->cipher_suites = cipher_suites;
1479         wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
1480
1481         ret = wiphy_register(wdev->wiphy);
1482         if (ret < 0) {
1483                 ath6kl_err("couldn't register wiphy device\n");
1484                 wiphy_free(wdev->wiphy);
1485                 kfree(wdev);
1486                 return NULL;
1487         }
1488
1489         return wdev;
1490 }
1491
1492 void ath6kl_cfg80211_deinit(struct ath6kl *ar)
1493 {
1494         struct wireless_dev *wdev = ar->wdev;
1495
1496         if (ar->scan_req) {
1497                 cfg80211_scan_done(ar->scan_req, true);
1498                 ar->scan_req = NULL;
1499         }
1500
1501         if (!wdev)
1502                 return;
1503
1504         wiphy_unregister(wdev->wiphy);
1505         wiphy_free(wdev->wiphy);
1506         kfree(wdev);
1507 }