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