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