GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / net / wireless / ath / wcn36xx / main.c
1 /*
2  * Copyright (c) 2013 Eugene Krasnikov <k.eugene.e@gmail.com>
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 ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/firmware.h>
21 #include <linux/platform_device.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include "wcn36xx.h"
25
26 unsigned int wcn36xx_dbg_mask;
27 module_param_named(debug_mask, wcn36xx_dbg_mask, uint, 0644);
28 MODULE_PARM_DESC(debug_mask, "Debugging mask");
29
30 #define CHAN2G(_freq, _idx) { \
31         .band = NL80211_BAND_2GHZ, \
32         .center_freq = (_freq), \
33         .hw_value = (_idx), \
34         .max_power = 25, \
35 }
36
37 #define CHAN5G(_freq, _idx) { \
38         .band = NL80211_BAND_5GHZ, \
39         .center_freq = (_freq), \
40         .hw_value = (_idx), \
41         .max_power = 25, \
42 }
43
44 /* The wcn firmware expects channel values to matching
45  * their mnemonic values. So use these for .hw_value. */
46 static struct ieee80211_channel wcn_2ghz_channels[] = {
47         CHAN2G(2412, 1), /* Channel 1 */
48         CHAN2G(2417, 2), /* Channel 2 */
49         CHAN2G(2422, 3), /* Channel 3 */
50         CHAN2G(2427, 4), /* Channel 4 */
51         CHAN2G(2432, 5), /* Channel 5 */
52         CHAN2G(2437, 6), /* Channel 6 */
53         CHAN2G(2442, 7), /* Channel 7 */
54         CHAN2G(2447, 8), /* Channel 8 */
55         CHAN2G(2452, 9), /* Channel 9 */
56         CHAN2G(2457, 10), /* Channel 10 */
57         CHAN2G(2462, 11), /* Channel 11 */
58         CHAN2G(2467, 12), /* Channel 12 */
59         CHAN2G(2472, 13), /* Channel 13 */
60         CHAN2G(2484, 14)  /* Channel 14 */
61
62 };
63
64 static struct ieee80211_channel wcn_5ghz_channels[] = {
65         CHAN5G(5180, 36),
66         CHAN5G(5200, 40),
67         CHAN5G(5220, 44),
68         CHAN5G(5240, 48),
69         CHAN5G(5260, 52),
70         CHAN5G(5280, 56),
71         CHAN5G(5300, 60),
72         CHAN5G(5320, 64),
73         CHAN5G(5500, 100),
74         CHAN5G(5520, 104),
75         CHAN5G(5540, 108),
76         CHAN5G(5560, 112),
77         CHAN5G(5580, 116),
78         CHAN5G(5600, 120),
79         CHAN5G(5620, 124),
80         CHAN5G(5640, 128),
81         CHAN5G(5660, 132),
82         CHAN5G(5700, 140),
83         CHAN5G(5745, 149),
84         CHAN5G(5765, 153),
85         CHAN5G(5785, 157),
86         CHAN5G(5805, 161),
87         CHAN5G(5825, 165)
88 };
89
90 #define RATE(_bitrate, _hw_rate, _flags) { \
91         .bitrate        = (_bitrate),                   \
92         .flags          = (_flags),                     \
93         .hw_value       = (_hw_rate),                   \
94         .hw_value_short = (_hw_rate)  \
95 }
96
97 static struct ieee80211_rate wcn_2ghz_rates[] = {
98         RATE(10, HW_RATE_INDEX_1MBPS, 0),
99         RATE(20, HW_RATE_INDEX_2MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
100         RATE(55, HW_RATE_INDEX_5_5MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
101         RATE(110, HW_RATE_INDEX_11MBPS, IEEE80211_RATE_SHORT_PREAMBLE),
102         RATE(60, HW_RATE_INDEX_6MBPS, 0),
103         RATE(90, HW_RATE_INDEX_9MBPS, 0),
104         RATE(120, HW_RATE_INDEX_12MBPS, 0),
105         RATE(180, HW_RATE_INDEX_18MBPS, 0),
106         RATE(240, HW_RATE_INDEX_24MBPS, 0),
107         RATE(360, HW_RATE_INDEX_36MBPS, 0),
108         RATE(480, HW_RATE_INDEX_48MBPS, 0),
109         RATE(540, HW_RATE_INDEX_54MBPS, 0)
110 };
111
112 static struct ieee80211_rate wcn_5ghz_rates[] = {
113         RATE(60, HW_RATE_INDEX_6MBPS, 0),
114         RATE(90, HW_RATE_INDEX_9MBPS, 0),
115         RATE(120, HW_RATE_INDEX_12MBPS, 0),
116         RATE(180, HW_RATE_INDEX_18MBPS, 0),
117         RATE(240, HW_RATE_INDEX_24MBPS, 0),
118         RATE(360, HW_RATE_INDEX_36MBPS, 0),
119         RATE(480, HW_RATE_INDEX_48MBPS, 0),
120         RATE(540, HW_RATE_INDEX_54MBPS, 0)
121 };
122
123 static struct ieee80211_supported_band wcn_band_2ghz = {
124         .channels       = wcn_2ghz_channels,
125         .n_channels     = ARRAY_SIZE(wcn_2ghz_channels),
126         .bitrates       = wcn_2ghz_rates,
127         .n_bitrates     = ARRAY_SIZE(wcn_2ghz_rates),
128         .ht_cap         = {
129                 .cap =  IEEE80211_HT_CAP_GRN_FLD |
130                         IEEE80211_HT_CAP_SGI_20 |
131                         IEEE80211_HT_CAP_DSSSCCK40 |
132                         IEEE80211_HT_CAP_LSIG_TXOP_PROT |
133                         IEEE80211_HT_CAP_SGI_40 |
134                         IEEE80211_HT_CAP_SUP_WIDTH_20_40,
135                 .ht_supported = true,
136                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
137                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_16,
138                 .mcs = {
139                         .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
140                         .rx_highest = cpu_to_le16(72),
141                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
142                 }
143         }
144 };
145
146 static struct ieee80211_supported_band wcn_band_5ghz = {
147         .channels       = wcn_5ghz_channels,
148         .n_channels     = ARRAY_SIZE(wcn_5ghz_channels),
149         .bitrates       = wcn_5ghz_rates,
150         .n_bitrates     = ARRAY_SIZE(wcn_5ghz_rates),
151         .ht_cap         = {
152                 .cap =  IEEE80211_HT_CAP_GRN_FLD |
153                         IEEE80211_HT_CAP_SGI_20 |
154                         IEEE80211_HT_CAP_DSSSCCK40 |
155                         IEEE80211_HT_CAP_LSIG_TXOP_PROT |
156                         IEEE80211_HT_CAP_SGI_40 |
157                         IEEE80211_HT_CAP_SUP_WIDTH_20_40,
158                 .ht_supported = true,
159                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
160                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_16,
161                 .mcs = {
162                         .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
163                         .rx_highest = cpu_to_le16(150),
164                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
165                 }
166         }
167 };
168
169 #ifdef CONFIG_PM
170
171 static const struct wiphy_wowlan_support wowlan_support = {
172         .flags = WIPHY_WOWLAN_ANY
173 };
174
175 #endif
176
177 static inline u8 get_sta_index(struct ieee80211_vif *vif,
178                                struct wcn36xx_sta *sta_priv)
179 {
180         return NL80211_IFTYPE_STATION == vif->type ?
181                sta_priv->bss_sta_index :
182                sta_priv->sta_index;
183 }
184
185 static const char * const wcn36xx_caps_names[] = {
186         "MCC",                          /* 0 */
187         "P2P",                          /* 1 */
188         "DOT11AC",                      /* 2 */
189         "SLM_SESSIONIZATION",           /* 3 */
190         "DOT11AC_OPMODE",               /* 4 */
191         "SAP32STA",                     /* 5 */
192         "TDLS",                         /* 6 */
193         "P2P_GO_NOA_DECOUPLE_INIT_SCAN",/* 7 */
194         "WLANACTIVE_OFFLOAD",           /* 8 */
195         "BEACON_OFFLOAD",               /* 9 */
196         "SCAN_OFFLOAD",                 /* 10 */
197         "ROAM_OFFLOAD",                 /* 11 */
198         "BCN_MISS_OFFLOAD",             /* 12 */
199         "STA_POWERSAVE",                /* 13 */
200         "STA_ADVANCED_PWRSAVE",         /* 14 */
201         "AP_UAPSD",                     /* 15 */
202         "AP_DFS",                       /* 16 */
203         "BLOCKACK",                     /* 17 */
204         "PHY_ERR",                      /* 18 */
205         "BCN_FILTER",                   /* 19 */
206         "RTT",                          /* 20 */
207         "RATECTRL",                     /* 21 */
208         "WOW",                          /* 22 */
209         "WLAN_ROAM_SCAN_OFFLOAD",       /* 23 */
210         "SPECULATIVE_PS_POLL",          /* 24 */
211         "SCAN_SCH",                     /* 25 */
212         "IBSS_HEARTBEAT_OFFLOAD",       /* 26 */
213         "WLAN_SCAN_OFFLOAD",            /* 27 */
214         "WLAN_PERIODIC_TX_PTRN",        /* 28 */
215         "ADVANCE_TDLS",                 /* 29 */
216         "BATCH_SCAN",                   /* 30 */
217         "FW_IN_TX_PATH",                /* 31 */
218         "EXTENDED_NSOFFLOAD_SLOT",      /* 32 */
219         "CH_SWITCH_V1",                 /* 33 */
220         "HT40_OBSS_SCAN",               /* 34 */
221         "UPDATE_CHANNEL_LIST",          /* 35 */
222         "WLAN_MCADDR_FLT",              /* 36 */
223         "WLAN_CH144",                   /* 37 */
224         "NAN",                          /* 38 */
225         "TDLS_SCAN_COEXISTENCE",        /* 39 */
226         "LINK_LAYER_STATS_MEAS",        /* 40 */
227         "MU_MIMO",                      /* 41 */
228         "EXTENDED_SCAN",                /* 42 */
229         "DYNAMIC_WMM_PS",               /* 43 */
230         "MAC_SPOOFED_SCAN",             /* 44 */
231         "BMU_ERROR_GENERIC_RECOVERY",   /* 45 */
232         "DISA",                         /* 46 */
233         "FW_STATS",                     /* 47 */
234         "WPS_PRBRSP_TMPL",              /* 48 */
235         "BCN_IE_FLT_DELTA",             /* 49 */
236         "TDLS_OFF_CHANNEL",             /* 51 */
237         "RTT3",                         /* 52 */
238         "MGMT_FRAME_LOGGING",           /* 53 */
239         "ENHANCED_TXBD_COMPLETION",     /* 54 */
240         "LOGGING_ENHANCEMENT",          /* 55 */
241         "EXT_SCAN_ENHANCED",            /* 56 */
242         "MEMORY_DUMP_SUPPORTED",        /* 57 */
243         "PER_PKT_STATS_SUPPORTED",      /* 58 */
244         "EXT_LL_STAT",                  /* 60 */
245         "WIFI_CONFIG",                  /* 61 */
246         "ANTENNA_DIVERSITY_SELECTION",  /* 62 */
247 };
248
249 static const char *wcn36xx_get_cap_name(enum place_holder_in_cap_bitmap x)
250 {
251         if (x >= ARRAY_SIZE(wcn36xx_caps_names))
252                 return "UNKNOWN";
253         return wcn36xx_caps_names[x];
254 }
255
256 static void wcn36xx_feat_caps_info(struct wcn36xx *wcn)
257 {
258         int i;
259
260         for (i = 0; i < MAX_FEATURE_SUPPORTED; i++) {
261                 if (get_feat_caps(wcn->fw_feat_caps, i))
262                         wcn36xx_info("FW Cap %s\n", wcn36xx_get_cap_name(i));
263         }
264 }
265
266 static int wcn36xx_start(struct ieee80211_hw *hw)
267 {
268         struct wcn36xx *wcn = hw->priv;
269         int ret;
270
271         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac start\n");
272
273         /* SMD initialization */
274         ret = wcn36xx_smd_open(wcn);
275         if (ret) {
276                 wcn36xx_err("Failed to open smd channel: %d\n", ret);
277                 goto out_err;
278         }
279
280         /* Allocate memory pools for Mgmt BD headers and Data BD headers */
281         ret = wcn36xx_dxe_allocate_mem_pools(wcn);
282         if (ret) {
283                 wcn36xx_err("Failed to alloc DXE mempool: %d\n", ret);
284                 goto out_smd_close;
285         }
286
287         ret = wcn36xx_dxe_alloc_ctl_blks(wcn);
288         if (ret) {
289                 wcn36xx_err("Failed to alloc DXE ctl blocks: %d\n", ret);
290                 goto out_free_dxe_pool;
291         }
292
293         wcn->hal_buf = kmalloc(WCN36XX_HAL_BUF_SIZE, GFP_KERNEL);
294         if (!wcn->hal_buf) {
295                 wcn36xx_err("Failed to allocate smd buf\n");
296                 ret = -ENOMEM;
297                 goto out_free_dxe_ctl;
298         }
299
300         ret = wcn36xx_smd_load_nv(wcn);
301         if (ret) {
302                 wcn36xx_err("Failed to push NV to chip\n");
303                 goto out_free_smd_buf;
304         }
305
306         ret = wcn36xx_smd_start(wcn);
307         if (ret) {
308                 wcn36xx_err("Failed to start chip\n");
309                 goto out_free_smd_buf;
310         }
311
312         if (!wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
313                 ret = wcn36xx_smd_feature_caps_exchange(wcn);
314                 if (ret)
315                         wcn36xx_warn("Exchange feature caps failed\n");
316                 else
317                         wcn36xx_feat_caps_info(wcn);
318         }
319
320         /* DMA channel initialization */
321         ret = wcn36xx_dxe_init(wcn);
322         if (ret) {
323                 wcn36xx_err("DXE init failed\n");
324                 goto out_smd_stop;
325         }
326
327         wcn36xx_debugfs_init(wcn);
328
329         INIT_LIST_HEAD(&wcn->vif_list);
330         spin_lock_init(&wcn->dxe_lock);
331
332         return 0;
333
334 out_smd_stop:
335         wcn36xx_smd_stop(wcn);
336 out_free_smd_buf:
337         kfree(wcn->hal_buf);
338 out_free_dxe_pool:
339         wcn36xx_dxe_free_mem_pools(wcn);
340 out_free_dxe_ctl:
341         wcn36xx_dxe_free_ctl_blks(wcn);
342 out_smd_close:
343         wcn36xx_smd_close(wcn);
344 out_err:
345         return ret;
346 }
347
348 static void wcn36xx_stop(struct ieee80211_hw *hw)
349 {
350         struct wcn36xx *wcn = hw->priv;
351
352         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac stop\n");
353
354         wcn36xx_debugfs_exit(wcn);
355         wcn36xx_smd_stop(wcn);
356         wcn36xx_dxe_deinit(wcn);
357         wcn36xx_smd_close(wcn);
358
359         wcn36xx_dxe_free_mem_pools(wcn);
360         wcn36xx_dxe_free_ctl_blks(wcn);
361
362         kfree(wcn->hal_buf);
363 }
364
365 static int wcn36xx_config(struct ieee80211_hw *hw, u32 changed)
366 {
367         struct wcn36xx *wcn = hw->priv;
368         struct ieee80211_vif *vif = NULL;
369         struct wcn36xx_vif *tmp;
370
371         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac config changed 0x%08x\n", changed);
372
373         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
374                 int ch = WCN36XX_HW_CHANNEL(wcn);
375                 wcn36xx_dbg(WCN36XX_DBG_MAC, "wcn36xx_config channel switch=%d\n",
376                             ch);
377                 list_for_each_entry(tmp, &wcn->vif_list, list) {
378                         vif = wcn36xx_priv_to_vif(tmp);
379                         wcn36xx_smd_switch_channel(wcn, vif, ch);
380                 }
381         }
382
383         return 0;
384 }
385
386 static void wcn36xx_configure_filter(struct ieee80211_hw *hw,
387                                      unsigned int changed,
388                                      unsigned int *total, u64 multicast)
389 {
390         struct wcn36xx_hal_rcv_flt_mc_addr_list_type *fp;
391         struct wcn36xx *wcn = hw->priv;
392         struct wcn36xx_vif *tmp;
393         struct ieee80211_vif *vif = NULL;
394
395         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac configure filter\n");
396
397         *total &= FIF_ALLMULTI;
398
399         fp = (void *)(unsigned long)multicast;
400         list_for_each_entry(tmp, &wcn->vif_list, list) {
401                 vif = wcn36xx_priv_to_vif(tmp);
402
403                 /* FW handles MC filtering only when connected as STA */
404                 if (*total & FIF_ALLMULTI)
405                         wcn36xx_smd_set_mc_list(wcn, vif, NULL);
406                 else if (NL80211_IFTYPE_STATION == vif->type && tmp->sta_assoc)
407                         wcn36xx_smd_set_mc_list(wcn, vif, fp);
408         }
409         kfree(fp);
410 }
411
412 static u64 wcn36xx_prepare_multicast(struct ieee80211_hw *hw,
413                                      struct netdev_hw_addr_list *mc_list)
414 {
415         struct wcn36xx_hal_rcv_flt_mc_addr_list_type *fp;
416         struct netdev_hw_addr *ha;
417
418         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac prepare multicast list\n");
419         fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
420         if (!fp) {
421                 wcn36xx_err("Out of memory setting filters.\n");
422                 return 0;
423         }
424
425         fp->mc_addr_count = 0;
426         /* update multicast filtering parameters */
427         if (netdev_hw_addr_list_count(mc_list) <=
428             WCN36XX_HAL_MAX_NUM_MULTICAST_ADDRESS) {
429                 netdev_hw_addr_list_for_each(ha, mc_list) {
430                         memcpy(fp->mc_addr[fp->mc_addr_count],
431                                         ha->addr, ETH_ALEN);
432                         fp->mc_addr_count++;
433                 }
434         }
435
436         return (u64)(unsigned long)fp;
437 }
438
439 static void wcn36xx_tx(struct ieee80211_hw *hw,
440                        struct ieee80211_tx_control *control,
441                        struct sk_buff *skb)
442 {
443         struct wcn36xx *wcn = hw->priv;
444         struct wcn36xx_sta *sta_priv = NULL;
445
446         if (control->sta)
447                 sta_priv = wcn36xx_sta_to_priv(control->sta);
448
449         if (wcn36xx_start_tx(wcn, sta_priv, skb))
450                 ieee80211_free_txskb(wcn->hw, skb);
451 }
452
453 static int wcn36xx_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
454                            struct ieee80211_vif *vif,
455                            struct ieee80211_sta *sta,
456                            struct ieee80211_key_conf *key_conf)
457 {
458         struct wcn36xx *wcn = hw->priv;
459         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
460         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
461         int ret = 0;
462         u8 key[WLAN_MAX_KEY_LEN];
463
464         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac80211 set key\n");
465         wcn36xx_dbg(WCN36XX_DBG_MAC, "Key: cmd=0x%x algo:0x%x, id:%d, len:%d flags 0x%x\n",
466                     cmd, key_conf->cipher, key_conf->keyidx,
467                     key_conf->keylen, key_conf->flags);
468         wcn36xx_dbg_dump(WCN36XX_DBG_MAC, "KEY: ",
469                          key_conf->key,
470                          key_conf->keylen);
471
472         switch (key_conf->cipher) {
473         case WLAN_CIPHER_SUITE_WEP40:
474                 vif_priv->encrypt_type = WCN36XX_HAL_ED_WEP40;
475                 break;
476         case WLAN_CIPHER_SUITE_WEP104:
477                 vif_priv->encrypt_type = WCN36XX_HAL_ED_WEP40;
478                 break;
479         case WLAN_CIPHER_SUITE_CCMP:
480                 vif_priv->encrypt_type = WCN36XX_HAL_ED_CCMP;
481                 break;
482         case WLAN_CIPHER_SUITE_TKIP:
483                 vif_priv->encrypt_type = WCN36XX_HAL_ED_TKIP;
484                 break;
485         default:
486                 wcn36xx_err("Unsupported key type 0x%x\n",
487                               key_conf->cipher);
488                 ret = -EOPNOTSUPP;
489                 goto out;
490         }
491
492         switch (cmd) {
493         case SET_KEY:
494                 if (WCN36XX_HAL_ED_TKIP == vif_priv->encrypt_type) {
495                         /*
496                          * Supplicant is sending key in the wrong order:
497                          * Temporal Key (16 b) - TX MIC (8 b) - RX MIC (8 b)
498                          * but HW expects it to be in the order as described in
499                          * IEEE 802.11 spec (see chapter 11.7) like this:
500                          * Temporal Key (16 b) - RX MIC (8 b) - TX MIC (8 b)
501                          */
502                         memcpy(key, key_conf->key, 16);
503                         memcpy(key + 16, key_conf->key + 24, 8);
504                         memcpy(key + 24, key_conf->key + 16, 8);
505                 } else {
506                         memcpy(key, key_conf->key, key_conf->keylen);
507                 }
508
509                 if (IEEE80211_KEY_FLAG_PAIRWISE & key_conf->flags) {
510                         sta_priv->is_data_encrypted = true;
511                         /* Reconfigure bss with encrypt_type */
512                         if (NL80211_IFTYPE_STATION == vif->type)
513                                 wcn36xx_smd_config_bss(wcn,
514                                                        vif,
515                                                        sta,
516                                                        sta->addr,
517                                                        true);
518
519                         wcn36xx_smd_set_stakey(wcn,
520                                 vif_priv->encrypt_type,
521                                 key_conf->keyidx,
522                                 key_conf->keylen,
523                                 key,
524                                 get_sta_index(vif, sta_priv));
525                 } else {
526                         wcn36xx_smd_set_bsskey(wcn,
527                                 vif_priv->encrypt_type,
528                                 key_conf->keyidx,
529                                 key_conf->keylen,
530                                 key);
531                         if ((WLAN_CIPHER_SUITE_WEP40 == key_conf->cipher) ||
532                             (WLAN_CIPHER_SUITE_WEP104 == key_conf->cipher)) {
533                                 sta_priv->is_data_encrypted = true;
534                                 wcn36xx_smd_set_stakey(wcn,
535                                         vif_priv->encrypt_type,
536                                         key_conf->keyidx,
537                                         key_conf->keylen,
538                                         key,
539                                         get_sta_index(vif, sta_priv));
540                         }
541                 }
542                 break;
543         case DISABLE_KEY:
544                 if (!(IEEE80211_KEY_FLAG_PAIRWISE & key_conf->flags)) {
545                         vif_priv->encrypt_type = WCN36XX_HAL_ED_NONE;
546                         wcn36xx_smd_remove_bsskey(wcn,
547                                 vif_priv->encrypt_type,
548                                 key_conf->keyidx);
549                 } else {
550                         sta_priv->is_data_encrypted = false;
551                         /* do not remove key if disassociated */
552                         if (sta_priv->aid)
553                                 wcn36xx_smd_remove_stakey(wcn,
554                                         vif_priv->encrypt_type,
555                                         key_conf->keyidx,
556                                         get_sta_index(vif, sta_priv));
557                 }
558                 break;
559         default:
560                 wcn36xx_err("Unsupported key cmd 0x%x\n", cmd);
561                 ret = -EOPNOTSUPP;
562                 goto out;
563         }
564
565 out:
566         return ret;
567 }
568
569 static void wcn36xx_sw_scan_start(struct ieee80211_hw *hw,
570                                   struct ieee80211_vif *vif,
571                                   const u8 *mac_addr)
572 {
573         struct wcn36xx *wcn = hw->priv;
574
575         wcn36xx_smd_init_scan(wcn, HAL_SYS_MODE_SCAN);
576         wcn36xx_smd_start_scan(wcn);
577 }
578
579 static void wcn36xx_sw_scan_complete(struct ieee80211_hw *hw,
580                                      struct ieee80211_vif *vif)
581 {
582         struct wcn36xx *wcn = hw->priv;
583
584         wcn36xx_smd_end_scan(wcn);
585         wcn36xx_smd_finish_scan(wcn, HAL_SYS_MODE_SCAN);
586 }
587
588 static void wcn36xx_update_allowed_rates(struct ieee80211_sta *sta,
589                                          enum nl80211_band band)
590 {
591         int i, size;
592         u16 *rates_table;
593         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
594         u32 rates = sta->supp_rates[band];
595
596         memset(&sta_priv->supported_rates, 0,
597                 sizeof(sta_priv->supported_rates));
598         sta_priv->supported_rates.op_rate_mode = STA_11n;
599
600         size = ARRAY_SIZE(sta_priv->supported_rates.dsss_rates);
601         rates_table = sta_priv->supported_rates.dsss_rates;
602         if (band == NL80211_BAND_2GHZ) {
603                 for (i = 0; i < size; i++) {
604                         if (rates & 0x01) {
605                                 rates_table[i] = wcn_2ghz_rates[i].hw_value;
606                                 rates = rates >> 1;
607                         }
608                 }
609         }
610
611         size = ARRAY_SIZE(sta_priv->supported_rates.ofdm_rates);
612         rates_table = sta_priv->supported_rates.ofdm_rates;
613         for (i = 0; i < size; i++) {
614                 if (rates & 0x01) {
615                         rates_table[i] = wcn_5ghz_rates[i].hw_value;
616                         rates = rates >> 1;
617                 }
618         }
619
620         if (sta->ht_cap.ht_supported) {
621                 BUILD_BUG_ON(sizeof(sta->ht_cap.mcs.rx_mask) >
622                         sizeof(sta_priv->supported_rates.supported_mcs_set));
623                 memcpy(sta_priv->supported_rates.supported_mcs_set,
624                        sta->ht_cap.mcs.rx_mask,
625                        sizeof(sta->ht_cap.mcs.rx_mask));
626         }
627 }
628 void wcn36xx_set_default_rates(struct wcn36xx_hal_supported_rates *rates)
629 {
630         u16 ofdm_rates[WCN36XX_HAL_NUM_OFDM_RATES] = {
631                 HW_RATE_INDEX_6MBPS,
632                 HW_RATE_INDEX_9MBPS,
633                 HW_RATE_INDEX_12MBPS,
634                 HW_RATE_INDEX_18MBPS,
635                 HW_RATE_INDEX_24MBPS,
636                 HW_RATE_INDEX_36MBPS,
637                 HW_RATE_INDEX_48MBPS,
638                 HW_RATE_INDEX_54MBPS
639         };
640         u16 dsss_rates[WCN36XX_HAL_NUM_DSSS_RATES] = {
641                 HW_RATE_INDEX_1MBPS,
642                 HW_RATE_INDEX_2MBPS,
643                 HW_RATE_INDEX_5_5MBPS,
644                 HW_RATE_INDEX_11MBPS
645         };
646
647         rates->op_rate_mode = STA_11n;
648         memcpy(rates->dsss_rates, dsss_rates,
649                 sizeof(*dsss_rates) * WCN36XX_HAL_NUM_DSSS_RATES);
650         memcpy(rates->ofdm_rates, ofdm_rates,
651                 sizeof(*ofdm_rates) * WCN36XX_HAL_NUM_OFDM_RATES);
652         rates->supported_mcs_set[0] = 0xFF;
653 }
654 static void wcn36xx_bss_info_changed(struct ieee80211_hw *hw,
655                                      struct ieee80211_vif *vif,
656                                      struct ieee80211_bss_conf *bss_conf,
657                                      u32 changed)
658 {
659         struct wcn36xx *wcn = hw->priv;
660         struct sk_buff *skb = NULL;
661         u16 tim_off, tim_len;
662         enum wcn36xx_hal_link_state link_state;
663         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
664
665         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss info changed vif %p changed 0x%08x\n",
666                     vif, changed);
667
668         if (changed & BSS_CHANGED_BEACON_INFO) {
669                 wcn36xx_dbg(WCN36XX_DBG_MAC,
670                             "mac bss changed dtim period %d\n",
671                             bss_conf->dtim_period);
672
673                 vif_priv->dtim_period = bss_conf->dtim_period;
674         }
675
676         if (changed & BSS_CHANGED_PS) {
677                 wcn36xx_dbg(WCN36XX_DBG_MAC,
678                             "mac bss PS set %d\n",
679                             bss_conf->ps);
680                 if (bss_conf->ps) {
681                         wcn36xx_pmc_enter_bmps_state(wcn, vif);
682                 } else {
683                         wcn36xx_pmc_exit_bmps_state(wcn, vif);
684                 }
685         }
686
687         if (changed & BSS_CHANGED_BSSID) {
688                 wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss changed_bssid %pM\n",
689                             bss_conf->bssid);
690
691                 if (!is_zero_ether_addr(bss_conf->bssid)) {
692                         vif_priv->is_joining = true;
693                         vif_priv->bss_index = WCN36XX_HAL_BSS_INVALID_IDX;
694                         wcn36xx_smd_join(wcn, bss_conf->bssid,
695                                          vif->addr, WCN36XX_HW_CHANNEL(wcn));
696                         wcn36xx_smd_config_bss(wcn, vif, NULL,
697                                                bss_conf->bssid, false);
698                 } else {
699                         vif_priv->is_joining = false;
700                         wcn36xx_smd_delete_bss(wcn, vif);
701                         vif_priv->encrypt_type = WCN36XX_HAL_ED_NONE;
702                 }
703         }
704
705         if (changed & BSS_CHANGED_SSID) {
706                 wcn36xx_dbg(WCN36XX_DBG_MAC,
707                             "mac bss changed ssid\n");
708                 wcn36xx_dbg_dump(WCN36XX_DBG_MAC, "ssid ",
709                                  bss_conf->ssid, bss_conf->ssid_len);
710
711                 vif_priv->ssid.length = bss_conf->ssid_len;
712                 memcpy(&vif_priv->ssid.ssid,
713                        bss_conf->ssid,
714                        bss_conf->ssid_len);
715         }
716
717         if (changed & BSS_CHANGED_ASSOC) {
718                 vif_priv->is_joining = false;
719                 if (bss_conf->assoc) {
720                         struct ieee80211_sta *sta;
721                         struct wcn36xx_sta *sta_priv;
722
723                         wcn36xx_dbg(WCN36XX_DBG_MAC,
724                                     "mac assoc bss %pM vif %pM AID=%d\n",
725                                      bss_conf->bssid,
726                                      vif->addr,
727                                      bss_conf->aid);
728
729                         vif_priv->sta_assoc = true;
730                         rcu_read_lock();
731                         sta = ieee80211_find_sta(vif, bss_conf->bssid);
732                         if (!sta) {
733                                 wcn36xx_err("sta %pM is not found\n",
734                                               bss_conf->bssid);
735                                 rcu_read_unlock();
736                                 goto out;
737                         }
738                         sta_priv = wcn36xx_sta_to_priv(sta);
739
740                         wcn36xx_update_allowed_rates(sta, WCN36XX_BAND(wcn));
741
742                         wcn36xx_smd_set_link_st(wcn, bss_conf->bssid,
743                                 vif->addr,
744                                 WCN36XX_HAL_LINK_POSTASSOC_STATE);
745                         wcn36xx_smd_config_bss(wcn, vif, sta,
746                                                bss_conf->bssid,
747                                                true);
748                         sta_priv->aid = bss_conf->aid;
749                         /*
750                          * config_sta must be called from  because this is the
751                          * place where AID is available.
752                          */
753                         wcn36xx_smd_config_sta(wcn, vif, sta);
754                         rcu_read_unlock();
755                 } else {
756                         wcn36xx_dbg(WCN36XX_DBG_MAC,
757                                     "disassociated bss %pM vif %pM AID=%d\n",
758                                     bss_conf->bssid,
759                                     vif->addr,
760                                     bss_conf->aid);
761                         vif_priv->sta_assoc = false;
762                         wcn36xx_smd_set_link_st(wcn,
763                                                 bss_conf->bssid,
764                                                 vif->addr,
765                                                 WCN36XX_HAL_LINK_IDLE_STATE);
766                 }
767         }
768
769         if (changed & BSS_CHANGED_AP_PROBE_RESP) {
770                 wcn36xx_dbg(WCN36XX_DBG_MAC, "mac bss changed ap probe resp\n");
771                 skb = ieee80211_proberesp_get(hw, vif);
772                 if (!skb) {
773                         wcn36xx_err("failed to alloc probereq skb\n");
774                         goto out;
775                 }
776
777                 wcn36xx_smd_update_proberesp_tmpl(wcn, vif, skb);
778                 dev_kfree_skb(skb);
779         }
780
781         if (changed & BSS_CHANGED_BEACON_ENABLED ||
782             changed & BSS_CHANGED_BEACON) {
783                 wcn36xx_dbg(WCN36XX_DBG_MAC,
784                             "mac bss changed beacon enabled %d\n",
785                             bss_conf->enable_beacon);
786
787                 if (bss_conf->enable_beacon) {
788                         vif_priv->dtim_period = bss_conf->dtim_period;
789                         vif_priv->bss_index = WCN36XX_HAL_BSS_INVALID_IDX;
790                         wcn36xx_smd_config_bss(wcn, vif, NULL,
791                                                vif->addr, false);
792                         skb = ieee80211_beacon_get_tim(hw, vif, &tim_off,
793                                                        &tim_len);
794                         if (!skb) {
795                                 wcn36xx_err("failed to alloc beacon skb\n");
796                                 goto out;
797                         }
798                         wcn36xx_smd_send_beacon(wcn, vif, skb, tim_off, 0);
799                         dev_kfree_skb(skb);
800
801                         if (vif->type == NL80211_IFTYPE_ADHOC ||
802                             vif->type == NL80211_IFTYPE_MESH_POINT)
803                                 link_state = WCN36XX_HAL_LINK_IBSS_STATE;
804                         else
805                                 link_state = WCN36XX_HAL_LINK_AP_STATE;
806
807                         wcn36xx_smd_set_link_st(wcn, vif->addr, vif->addr,
808                                                 link_state);
809                 } else {
810                         wcn36xx_smd_delete_bss(wcn, vif);
811                         wcn36xx_smd_set_link_st(wcn, vif->addr, vif->addr,
812                                                 WCN36XX_HAL_LINK_IDLE_STATE);
813                 }
814         }
815 out:
816         return;
817 }
818
819 /* this is required when using IEEE80211_HW_HAS_RATE_CONTROL */
820 static int wcn36xx_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
821 {
822         struct wcn36xx *wcn = hw->priv;
823         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac set RTS threshold %d\n", value);
824
825         wcn36xx_smd_update_cfg(wcn, WCN36XX_HAL_CFG_RTS_THRESHOLD, value);
826         return 0;
827 }
828
829 static void wcn36xx_remove_interface(struct ieee80211_hw *hw,
830                                      struct ieee80211_vif *vif)
831 {
832         struct wcn36xx *wcn = hw->priv;
833         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
834         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac remove interface vif %p\n", vif);
835
836         list_del(&vif_priv->list);
837         wcn36xx_smd_delete_sta_self(wcn, vif->addr);
838 }
839
840 static int wcn36xx_add_interface(struct ieee80211_hw *hw,
841                                  struct ieee80211_vif *vif)
842 {
843         struct wcn36xx *wcn = hw->priv;
844         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
845
846         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac add interface vif %p type %d\n",
847                     vif, vif->type);
848
849         if (!(NL80211_IFTYPE_STATION == vif->type ||
850               NL80211_IFTYPE_AP == vif->type ||
851               NL80211_IFTYPE_ADHOC == vif->type ||
852               NL80211_IFTYPE_MESH_POINT == vif->type)) {
853                 wcn36xx_warn("Unsupported interface type requested: %d\n",
854                              vif->type);
855                 return -EOPNOTSUPP;
856         }
857
858         list_add(&vif_priv->list, &wcn->vif_list);
859         wcn36xx_smd_add_sta_self(wcn, vif);
860
861         return 0;
862 }
863
864 static int wcn36xx_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
865                            struct ieee80211_sta *sta)
866 {
867         struct wcn36xx *wcn = hw->priv;
868         struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
869         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
870         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac sta add vif %p sta %pM\n",
871                     vif, sta->addr);
872
873         spin_lock_init(&sta_priv->ampdu_lock);
874         sta_priv->vif = vif_priv;
875         /*
876          * For STA mode HW will be configured on BSS_CHANGED_ASSOC because
877          * at this stage AID is not available yet.
878          */
879         if (NL80211_IFTYPE_STATION != vif->type) {
880                 wcn36xx_update_allowed_rates(sta, WCN36XX_BAND(wcn));
881                 sta_priv->aid = sta->aid;
882                 wcn36xx_smd_config_sta(wcn, vif, sta);
883         }
884         return 0;
885 }
886
887 static int wcn36xx_sta_remove(struct ieee80211_hw *hw,
888                               struct ieee80211_vif *vif,
889                               struct ieee80211_sta *sta)
890 {
891         struct wcn36xx *wcn = hw->priv;
892         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
893
894         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac sta remove vif %p sta %pM index %d\n",
895                     vif, sta->addr, sta_priv->sta_index);
896
897         wcn36xx_smd_delete_sta(wcn, sta_priv->sta_index);
898         sta_priv->vif = NULL;
899         return 0;
900 }
901
902 #ifdef CONFIG_PM
903
904 static int wcn36xx_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wow)
905 {
906         struct wcn36xx *wcn = hw->priv;
907
908         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac suspend\n");
909
910         flush_workqueue(wcn->hal_ind_wq);
911         wcn36xx_smd_set_power_params(wcn, true);
912         return 0;
913 }
914
915 static int wcn36xx_resume(struct ieee80211_hw *hw)
916 {
917         struct wcn36xx *wcn = hw->priv;
918
919         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac resume\n");
920
921         flush_workqueue(wcn->hal_ind_wq);
922         wcn36xx_smd_set_power_params(wcn, false);
923         return 0;
924 }
925
926 #endif
927
928 static int wcn36xx_ampdu_action(struct ieee80211_hw *hw,
929                     struct ieee80211_vif *vif,
930                     struct ieee80211_ampdu_params *params)
931 {
932         struct wcn36xx *wcn = hw->priv;
933         struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(params->sta);
934         struct ieee80211_sta *sta = params->sta;
935         enum ieee80211_ampdu_mlme_action action = params->action;
936         u16 tid = params->tid;
937         u16 *ssn = &params->ssn;
938
939         wcn36xx_dbg(WCN36XX_DBG_MAC, "mac ampdu action action %d tid %d\n",
940                     action, tid);
941
942         switch (action) {
943         case IEEE80211_AMPDU_RX_START:
944                 sta_priv->tid = tid;
945                 wcn36xx_smd_add_ba_session(wcn, sta, tid, ssn, 0,
946                         get_sta_index(vif, sta_priv));
947                 wcn36xx_smd_add_ba(wcn);
948                 wcn36xx_smd_trigger_ba(wcn, get_sta_index(vif, sta_priv));
949                 break;
950         case IEEE80211_AMPDU_RX_STOP:
951                 wcn36xx_smd_del_ba(wcn, tid, get_sta_index(vif, sta_priv));
952                 break;
953         case IEEE80211_AMPDU_TX_START:
954                 spin_lock_bh(&sta_priv->ampdu_lock);
955                 sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_START;
956                 spin_unlock_bh(&sta_priv->ampdu_lock);
957
958                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
959                 break;
960         case IEEE80211_AMPDU_TX_OPERATIONAL:
961                 spin_lock_bh(&sta_priv->ampdu_lock);
962                 sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_OPERATIONAL;
963                 spin_unlock_bh(&sta_priv->ampdu_lock);
964
965                 wcn36xx_smd_add_ba_session(wcn, sta, tid, ssn, 1,
966                         get_sta_index(vif, sta_priv));
967                 break;
968         case IEEE80211_AMPDU_TX_STOP_FLUSH:
969         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
970         case IEEE80211_AMPDU_TX_STOP_CONT:
971                 spin_lock_bh(&sta_priv->ampdu_lock);
972                 sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_NONE;
973                 spin_unlock_bh(&sta_priv->ampdu_lock);
974
975                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
976                 break;
977         default:
978                 wcn36xx_err("Unknown AMPDU action\n");
979         }
980
981         return 0;
982 }
983
984 static const struct ieee80211_ops wcn36xx_ops = {
985         .start                  = wcn36xx_start,
986         .stop                   = wcn36xx_stop,
987         .add_interface          = wcn36xx_add_interface,
988         .remove_interface       = wcn36xx_remove_interface,
989 #ifdef CONFIG_PM
990         .suspend                = wcn36xx_suspend,
991         .resume                 = wcn36xx_resume,
992 #endif
993         .config                 = wcn36xx_config,
994         .prepare_multicast      = wcn36xx_prepare_multicast,
995         .configure_filter       = wcn36xx_configure_filter,
996         .tx                     = wcn36xx_tx,
997         .set_key                = wcn36xx_set_key,
998         .sw_scan_start          = wcn36xx_sw_scan_start,
999         .sw_scan_complete       = wcn36xx_sw_scan_complete,
1000         .bss_info_changed       = wcn36xx_bss_info_changed,
1001         .set_rts_threshold      = wcn36xx_set_rts_threshold,
1002         .sta_add                = wcn36xx_sta_add,
1003         .sta_remove             = wcn36xx_sta_remove,
1004         .ampdu_action           = wcn36xx_ampdu_action,
1005 };
1006
1007 static int wcn36xx_init_ieee80211(struct wcn36xx *wcn)
1008 {
1009         int ret = 0;
1010
1011         static const u32 cipher_suites[] = {
1012                 WLAN_CIPHER_SUITE_WEP40,
1013                 WLAN_CIPHER_SUITE_WEP104,
1014                 WLAN_CIPHER_SUITE_TKIP,
1015                 WLAN_CIPHER_SUITE_CCMP,
1016         };
1017
1018         ieee80211_hw_set(wcn->hw, TIMING_BEACON_ONLY);
1019         ieee80211_hw_set(wcn->hw, AMPDU_AGGREGATION);
1020         ieee80211_hw_set(wcn->hw, CONNECTION_MONITOR);
1021         ieee80211_hw_set(wcn->hw, SUPPORTS_PS);
1022         ieee80211_hw_set(wcn->hw, SIGNAL_DBM);
1023         ieee80211_hw_set(wcn->hw, HAS_RATE_CONTROL);
1024
1025         wcn->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1026                 BIT(NL80211_IFTYPE_AP) |
1027                 BIT(NL80211_IFTYPE_ADHOC) |
1028                 BIT(NL80211_IFTYPE_MESH_POINT);
1029
1030         wcn->hw->wiphy->bands[NL80211_BAND_2GHZ] = &wcn_band_2ghz;
1031         wcn->hw->wiphy->bands[NL80211_BAND_5GHZ] = &wcn_band_5ghz;
1032
1033         wcn->hw->wiphy->cipher_suites = cipher_suites;
1034         wcn->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
1035
1036         wcn->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1037
1038 #ifdef CONFIG_PM
1039         wcn->hw->wiphy->wowlan = &wowlan_support;
1040 #endif
1041
1042         wcn->hw->max_listen_interval = 200;
1043
1044         wcn->hw->queues = 4;
1045
1046         SET_IEEE80211_DEV(wcn->hw, wcn->dev);
1047
1048         wcn->hw->sta_data_size = sizeof(struct wcn36xx_sta);
1049         wcn->hw->vif_data_size = sizeof(struct wcn36xx_vif);
1050
1051         return ret;
1052 }
1053
1054 static int wcn36xx_platform_get_resources(struct wcn36xx *wcn,
1055                                           struct platform_device *pdev)
1056 {
1057         struct device_node *mmio_node;
1058         struct resource *res;
1059         int index;
1060         int ret;
1061
1062         /* Set TX IRQ */
1063         res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1064                                            "wcnss_wlantx_irq");
1065         if (!res) {
1066                 wcn36xx_err("failed to get tx_irq\n");
1067                 return -ENOENT;
1068         }
1069         wcn->tx_irq = res->start;
1070
1071         /* Set RX IRQ */
1072         res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1073                                            "wcnss_wlanrx_irq");
1074         if (!res) {
1075                 wcn36xx_err("failed to get rx_irq\n");
1076                 return -ENOENT;
1077         }
1078         wcn->rx_irq = res->start;
1079
1080         mmio_node = of_parse_phandle(pdev->dev.parent->of_node, "qcom,mmio", 0);
1081         if (!mmio_node) {
1082                 wcn36xx_err("failed to acquire qcom,mmio reference\n");
1083                 return -EINVAL;
1084         }
1085
1086         wcn->is_pronto = !!of_device_is_compatible(mmio_node, "qcom,pronto");
1087
1088         /* Map the CCU memory */
1089         index = of_property_match_string(mmio_node, "reg-names", "ccu");
1090         wcn->ccu_base = of_iomap(mmio_node, index);
1091         if (!wcn->ccu_base) {
1092                 wcn36xx_err("failed to map ccu memory\n");
1093                 ret = -ENOMEM;
1094                 goto put_mmio_node;
1095         }
1096
1097         /* Map the DXE memory */
1098         index = of_property_match_string(mmio_node, "reg-names", "dxe");
1099         wcn->dxe_base = of_iomap(mmio_node, index);
1100         if (!wcn->dxe_base) {
1101                 wcn36xx_err("failed to map dxe memory\n");
1102                 ret = -ENOMEM;
1103                 goto unmap_ccu;
1104         }
1105
1106         of_node_put(mmio_node);
1107         return 0;
1108
1109 unmap_ccu:
1110         iounmap(wcn->ccu_base);
1111 put_mmio_node:
1112         of_node_put(mmio_node);
1113         return ret;
1114 }
1115
1116 static int wcn36xx_probe(struct platform_device *pdev)
1117 {
1118         struct ieee80211_hw *hw;
1119         struct wcn36xx *wcn;
1120         int ret;
1121         u8 addr[ETH_ALEN];
1122
1123         wcn36xx_dbg(WCN36XX_DBG_MAC, "platform probe\n");
1124
1125         hw = ieee80211_alloc_hw(sizeof(struct wcn36xx), &wcn36xx_ops);
1126         if (!hw) {
1127                 wcn36xx_err("failed to alloc hw\n");
1128                 ret = -ENOMEM;
1129                 goto out_err;
1130         }
1131         platform_set_drvdata(pdev, hw);
1132         wcn = hw->priv;
1133         wcn->hw = hw;
1134         wcn->dev = &pdev->dev;
1135         wcn->ctrl_ops = pdev->dev.platform_data;
1136
1137         mutex_init(&wcn->hal_mutex);
1138
1139         if (!wcn->ctrl_ops->get_hw_mac(addr)) {
1140                 wcn36xx_info("mac address: %pM\n", addr);
1141                 SET_IEEE80211_PERM_ADDR(wcn->hw, addr);
1142         }
1143
1144         ret = wcn36xx_platform_get_resources(wcn, pdev);
1145         if (ret)
1146                 goto out_wq;
1147
1148         wcn36xx_init_ieee80211(wcn);
1149         ret = ieee80211_register_hw(wcn->hw);
1150         if (ret)
1151                 goto out_unmap;
1152
1153         return 0;
1154
1155 out_unmap:
1156         iounmap(wcn->ccu_base);
1157         iounmap(wcn->dxe_base);
1158 out_wq:
1159         ieee80211_free_hw(hw);
1160 out_err:
1161         return ret;
1162 }
1163 static int wcn36xx_remove(struct platform_device *pdev)
1164 {
1165         struct ieee80211_hw *hw = platform_get_drvdata(pdev);
1166         struct wcn36xx *wcn = hw->priv;
1167         wcn36xx_dbg(WCN36XX_DBG_MAC, "platform remove\n");
1168
1169         release_firmware(wcn->nv);
1170
1171         ieee80211_unregister_hw(hw);
1172         iounmap(wcn->dxe_base);
1173         iounmap(wcn->ccu_base);
1174
1175         mutex_destroy(&wcn->hal_mutex);
1176         ieee80211_free_hw(hw);
1177
1178         return 0;
1179 }
1180 static const struct platform_device_id wcn36xx_platform_id_table[] = {
1181         {
1182                 .name = "wcn36xx",
1183                 .driver_data = 0
1184         },
1185         {}
1186 };
1187 MODULE_DEVICE_TABLE(platform, wcn36xx_platform_id_table);
1188
1189 static struct platform_driver wcn36xx_driver = {
1190         .probe      = wcn36xx_probe,
1191         .remove     = wcn36xx_remove,
1192         .driver         = {
1193                 .name   = "wcn36xx",
1194         },
1195         .id_table    = wcn36xx_platform_id_table,
1196 };
1197
1198 static int __init wcn36xx_init(void)
1199 {
1200         platform_driver_register(&wcn36xx_driver);
1201         return 0;
1202 }
1203 module_init(wcn36xx_init);
1204
1205 static void __exit wcn36xx_exit(void)
1206 {
1207         platform_driver_unregister(&wcn36xx_driver);
1208 }
1209 module_exit(wcn36xx_exit);
1210
1211 MODULE_LICENSE("Dual BSD/GPL");
1212 MODULE_AUTHOR("Eugene Krasnikov k.eugene.e@gmail.com");
1213 /*(DEBLOBBED)*/