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