Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac802...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / mwifiex / cfg80211.c
1 /*
2  * Marvell Wireless LAN device driver: CFG80211
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "cfg80211.h"
21 #include "main.h"
22
23 static const struct ieee80211_iface_limit mwifiex_ap_sta_limits[] = {
24         {
25                 .max = 1, .types = BIT(NL80211_IFTYPE_STATION),
26         },
27         {
28                 .max = 1, .types = BIT(NL80211_IFTYPE_AP),
29         },
30 };
31
32 static const struct ieee80211_iface_combination mwifiex_iface_comb_ap_sta = {
33         .limits = mwifiex_ap_sta_limits,
34         .num_different_channels = 1,
35         .n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
36         .max_interfaces = MWIFIEX_MAX_BSS_NUM,
37         .beacon_int_infra_match = true,
38 };
39
40 static const struct ieee80211_regdomain mwifiex_world_regdom_custom = {
41         .n_reg_rules = 7,
42         .alpha2 =  "99",
43         .reg_rules = {
44                 /* Channel 1 - 11 */
45                 REG_RULE(2412-10, 2462+10, 40, 3, 20, 0),
46                 /* Channel 12 - 13 */
47                 REG_RULE(2467-10, 2472+10, 20, 3, 20,
48                          NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
49                 /* Channel 14 */
50                 REG_RULE(2484-10, 2484+10, 20, 3, 20,
51                          NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS |
52                          NL80211_RRF_NO_OFDM),
53                 /* Channel 36 - 48 */
54                 REG_RULE(5180-10, 5240+10, 40, 3, 20,
55                          NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
56                 /* Channel 149 - 165 */
57                 REG_RULE(5745-10, 5825+10, 40, 3, 20,
58                          NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
59                 /* Channel 52 - 64 */
60                 REG_RULE(5260-10, 5320+10, 40, 3, 30,
61                          NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS |
62                          NL80211_RRF_DFS),
63                 /* Channel 100 - 140 */
64                 REG_RULE(5500-10, 5700+10, 40, 3, 30,
65                          NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS |
66                          NL80211_RRF_DFS),
67         }
68 };
69
70 /*
71  * This function maps the nl802.11 channel type into driver channel type.
72  *
73  * The mapping is as follows -
74  *      NL80211_CHAN_NO_HT     -> IEEE80211_HT_PARAM_CHA_SEC_NONE
75  *      NL80211_CHAN_HT20      -> IEEE80211_HT_PARAM_CHA_SEC_NONE
76  *      NL80211_CHAN_HT40PLUS  -> IEEE80211_HT_PARAM_CHA_SEC_ABOVE
77  *      NL80211_CHAN_HT40MINUS -> IEEE80211_HT_PARAM_CHA_SEC_BELOW
78  *      Others                 -> IEEE80211_HT_PARAM_CHA_SEC_NONE
79  */
80 static u8
81 mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
82 {
83         switch (chan_type) {
84         case NL80211_CHAN_NO_HT:
85         case NL80211_CHAN_HT20:
86                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
87         case NL80211_CHAN_HT40PLUS:
88                 return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
89         case NL80211_CHAN_HT40MINUS:
90                 return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
91         default:
92                 return IEEE80211_HT_PARAM_CHA_SEC_NONE;
93         }
94 }
95
96 /*
97  * This function checks whether WEP is set.
98  */
99 static int
100 mwifiex_is_alg_wep(u32 cipher)
101 {
102         switch (cipher) {
103         case WLAN_CIPHER_SUITE_WEP40:
104         case WLAN_CIPHER_SUITE_WEP104:
105                 return 1;
106         default:
107                 break;
108         }
109
110         return 0;
111 }
112
113 /*
114  * This function retrieves the private structure from kernel wiphy structure.
115  */
116 static void *mwifiex_cfg80211_get_adapter(struct wiphy *wiphy)
117 {
118         return (void *) (*(unsigned long *) wiphy_priv(wiphy));
119 }
120
121 /*
122  * CFG802.11 operation handler to delete a network key.
123  */
124 static int
125 mwifiex_cfg80211_del_key(struct wiphy *wiphy, struct net_device *netdev,
126                          u8 key_index, bool pairwise, const u8 *mac_addr)
127 {
128         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
129         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
130         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
131
132         if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1)) {
133                 wiphy_err(wiphy, "deleting the crypto keys\n");
134                 return -EFAULT;
135         }
136
137         wiphy_dbg(wiphy, "info: crypto keys deleted\n");
138         return 0;
139 }
140
141 /*
142  * CFG802.11 operation handler to set Tx power.
143  */
144 static int
145 mwifiex_cfg80211_set_tx_power(struct wiphy *wiphy,
146                               enum nl80211_tx_power_setting type,
147                               int mbm)
148 {
149         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
150         struct mwifiex_private *priv;
151         struct mwifiex_power_cfg power_cfg;
152         int dbm = MBM_TO_DBM(mbm);
153
154         if (type == NL80211_TX_POWER_FIXED) {
155                 power_cfg.is_power_auto = 0;
156                 power_cfg.power_level = dbm;
157         } else {
158                 power_cfg.is_power_auto = 1;
159         }
160
161         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
162
163         return mwifiex_set_tx_power(priv, &power_cfg);
164 }
165
166 /*
167  * CFG802.11 operation handler to set Power Save option.
168  *
169  * The timeout value, if provided, is currently ignored.
170  */
171 static int
172 mwifiex_cfg80211_set_power_mgmt(struct wiphy *wiphy,
173                                 struct net_device *dev,
174                                 bool enabled, int timeout)
175 {
176         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
177         u32 ps_mode;
178
179         if (timeout)
180                 wiphy_dbg(wiphy,
181                           "info: ignore timeout value for IEEE Power Save\n");
182
183         ps_mode = enabled;
184
185         return mwifiex_drv_set_power(priv, &ps_mode);
186 }
187
188 /*
189  * CFG802.11 operation handler to set the default network key.
190  */
191 static int
192 mwifiex_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
193                                  u8 key_index, bool unicast,
194                                  bool multicast)
195 {
196         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
197
198         /* Return if WEP key not configured */
199         if (!priv->sec_info.wep_enabled)
200                 return 0;
201
202         if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP) {
203                 priv->wep_key_curr_index = key_index;
204         } else if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index,
205                                       NULL, 0)) {
206                 wiphy_err(wiphy, "set default Tx key index\n");
207                 return -EFAULT;
208         }
209
210         return 0;
211 }
212
213 /*
214  * CFG802.11 operation handler to add a network key.
215  */
216 static int
217 mwifiex_cfg80211_add_key(struct wiphy *wiphy, struct net_device *netdev,
218                          u8 key_index, bool pairwise, const u8 *mac_addr,
219                          struct key_params *params)
220 {
221         struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
222         struct mwifiex_wep_key *wep_key;
223         const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
224         const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
225
226         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
227             (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
228              params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
229                 if (params->key && params->key_len) {
230                         wep_key = &priv->wep_key[key_index];
231                         memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
232                         memcpy(wep_key->key_material, params->key,
233                                params->key_len);
234                         wep_key->key_index = key_index;
235                         wep_key->key_length = params->key_len;
236                         priv->sec_info.wep_enabled = 1;
237                 }
238                 return 0;
239         }
240
241         if (mwifiex_set_encode(priv, params, params->key, params->key_len,
242                                key_index, peer_mac, 0)) {
243                 wiphy_err(wiphy, "crypto keys added\n");
244                 return -EFAULT;
245         }
246
247         return 0;
248 }
249
250 /*
251  * This function sends domain information to the firmware.
252  *
253  * The following information are passed to the firmware -
254  *      - Country codes
255  *      - Sub bands (first channel, number of channels, maximum Tx power)
256  */
257 static int mwifiex_send_domain_info_cmd_fw(struct wiphy *wiphy)
258 {
259         u8 no_of_triplet = 0;
260         struct ieee80211_country_ie_triplet *t;
261         u8 no_of_parsed_chan = 0;
262         u8 first_chan = 0, next_chan = 0, max_pwr = 0;
263         u8 i, flag = 0;
264         enum ieee80211_band band;
265         struct ieee80211_supported_band *sband;
266         struct ieee80211_channel *ch;
267         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
268         struct mwifiex_private *priv;
269         struct mwifiex_802_11d_domain_reg *domain_info = &adapter->domain_reg;
270
271         /* Set country code */
272         domain_info->country_code[0] = adapter->country_code[0];
273         domain_info->country_code[1] = adapter->country_code[1];
274         domain_info->country_code[2] = ' ';
275
276         band = mwifiex_band_to_radio_type(adapter->config_bands);
277         if (!wiphy->bands[band]) {
278                 wiphy_err(wiphy, "11D: setting domain info in FW\n");
279                 return -1;
280         }
281
282         sband = wiphy->bands[band];
283
284         for (i = 0; i < sband->n_channels ; i++) {
285                 ch = &sband->channels[i];
286                 if (ch->flags & IEEE80211_CHAN_DISABLED)
287                         continue;
288
289                 if (!flag) {
290                         flag = 1;
291                         first_chan = (u32) ch->hw_value;
292                         next_chan = first_chan;
293                         max_pwr = ch->max_reg_power;
294                         no_of_parsed_chan = 1;
295                         continue;
296                 }
297
298                 if (ch->hw_value == next_chan + 1 &&
299                     ch->max_reg_power == max_pwr) {
300                         next_chan++;
301                         no_of_parsed_chan++;
302                 } else {
303                         t = &domain_info->triplet[no_of_triplet];
304                         t->chans.first_channel = first_chan;
305                         t->chans.num_channels = no_of_parsed_chan;
306                         t->chans.max_power = max_pwr;
307                         no_of_triplet++;
308                         first_chan = (u32) ch->hw_value;
309                         next_chan = first_chan;
310                         max_pwr = ch->max_reg_power;
311                         no_of_parsed_chan = 1;
312                 }
313         }
314
315         if (flag) {
316                 t = &domain_info->triplet[no_of_triplet];
317                 t->chans.first_channel = first_chan;
318                 t->chans.num_channels = no_of_parsed_chan;
319                 t->chans.max_power = max_pwr;
320                 no_of_triplet++;
321         }
322
323         domain_info->no_of_triplet = no_of_triplet;
324
325         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
326
327         if (mwifiex_send_cmd_async(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
328                                    HostCmd_ACT_GEN_SET, 0, NULL)) {
329                 wiphy_err(wiphy, "11D: setting domain info in FW\n");
330                 return -1;
331         }
332
333         return 0;
334 }
335
336 /*
337  * CFG802.11 regulatory domain callback function.
338  *
339  * This function is called when the regulatory domain is changed due to the
340  * following reasons -
341  *      - Set by driver
342  *      - Set by system core
343  *      - Set by user
344  *      - Set bt Country IE
345  */
346 static int mwifiex_reg_notifier(struct wiphy *wiphy,
347                                 struct regulatory_request *request)
348 {
349         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
350
351         wiphy_dbg(wiphy, "info: cfg80211 regulatory domain callback for %c%c\n",
352                   request->alpha2[0], request->alpha2[1]);
353
354         memcpy(adapter->country_code, request->alpha2, sizeof(request->alpha2));
355
356         switch (request->initiator) {
357         case NL80211_REGDOM_SET_BY_DRIVER:
358         case NL80211_REGDOM_SET_BY_CORE:
359         case NL80211_REGDOM_SET_BY_USER:
360                 break;
361                 /* Todo: apply driver specific changes in channel flags based
362                    on the request initiator if necessary. */
363         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
364                 break;
365         }
366         mwifiex_send_domain_info_cmd_fw(wiphy);
367
368         return 0;
369 }
370
371 /*
372  * This function sets the fragmentation threshold.
373  *
374  * The fragmentation threshold value must lie between MWIFIEX_FRAG_MIN_VALUE
375  * and MWIFIEX_FRAG_MAX_VALUE.
376  */
377 static int
378 mwifiex_set_frag(struct mwifiex_private *priv, u32 frag_thr)
379 {
380         if (frag_thr < MWIFIEX_FRAG_MIN_VALUE ||
381             frag_thr > MWIFIEX_FRAG_MAX_VALUE)
382                 frag_thr = MWIFIEX_FRAG_MAX_VALUE;
383
384         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_SNMP_MIB,
385                                      HostCmd_ACT_GEN_SET, FRAG_THRESH_I,
386                                      &frag_thr);
387 }
388
389 /*
390  * This function sets the RTS threshold.
391
392  * The rts value must lie between MWIFIEX_RTS_MIN_VALUE
393  * and MWIFIEX_RTS_MAX_VALUE.
394  */
395 static int
396 mwifiex_set_rts(struct mwifiex_private *priv, u32 rts_thr)
397 {
398         if (rts_thr < MWIFIEX_RTS_MIN_VALUE || rts_thr > MWIFIEX_RTS_MAX_VALUE)
399                 rts_thr = MWIFIEX_RTS_MAX_VALUE;
400
401         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_SNMP_MIB,
402                                     HostCmd_ACT_GEN_SET, RTS_THRESH_I,
403                                     &rts_thr);
404 }
405
406 /*
407  * CFG802.11 operation handler to set wiphy parameters.
408  *
409  * This function can be used to set the RTS threshold and the
410  * Fragmentation threshold of the driver.
411  */
412 static int
413 mwifiex_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
414 {
415         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
416         struct mwifiex_private *priv;
417         struct mwifiex_uap_bss_param *bss_cfg;
418         int ret, bss_started, i;
419
420         for (i = 0; i < adapter->priv_num; i++) {
421                 priv = adapter->priv[i];
422
423                 switch (priv->bss_role) {
424                 case MWIFIEX_BSS_ROLE_UAP:
425                         bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param),
426                                           GFP_KERNEL);
427                         if (!bss_cfg)
428                                 return -ENOMEM;
429
430                         mwifiex_set_sys_config_invalid_data(bss_cfg);
431
432                         if (changed & WIPHY_PARAM_RTS_THRESHOLD)
433                                 bss_cfg->rts_threshold = wiphy->rts_threshold;
434                         if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
435                                 bss_cfg->frag_threshold = wiphy->frag_threshold;
436                         if (changed & WIPHY_PARAM_RETRY_LONG)
437                                 bss_cfg->retry_limit = wiphy->retry_long;
438
439                         bss_started = priv->bss_started;
440
441                         ret = mwifiex_send_cmd_sync(priv,
442                                                     HostCmd_CMD_UAP_BSS_STOP,
443                                                     HostCmd_ACT_GEN_SET, 0,
444                                                     NULL);
445                         if (ret) {
446                                 wiphy_err(wiphy, "Failed to stop the BSS\n");
447                                 kfree(bss_cfg);
448                                 return ret;
449                         }
450
451                         ret = mwifiex_send_cmd_async(priv,
452                                                      HostCmd_CMD_UAP_SYS_CONFIG,
453                                                      HostCmd_ACT_GEN_SET,
454                                                      UAP_BSS_PARAMS_I, bss_cfg);
455
456                         kfree(bss_cfg);
457
458                         if (ret) {
459                                 wiphy_err(wiphy, "Failed to set bss config\n");
460                                 return ret;
461                         }
462
463                         if (!bss_started)
464                                 break;
465
466                         ret = mwifiex_send_cmd_async(priv,
467                                                      HostCmd_CMD_UAP_BSS_START,
468                                                      HostCmd_ACT_GEN_SET, 0,
469                                                      NULL);
470                         if (ret) {
471                                 wiphy_err(wiphy, "Failed to start BSS\n");
472                                 return ret;
473                         }
474
475                         break;
476                 case MWIFIEX_BSS_ROLE_STA:
477                         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
478                                 ret = mwifiex_set_rts(priv,
479                                                       wiphy->rts_threshold);
480                                 if (ret)
481                                         return ret;
482                         }
483                         if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
484                                 ret = mwifiex_set_frag(priv,
485                                                        wiphy->frag_threshold);
486                                 if (ret)
487                                         return ret;
488                         }
489                         break;
490                 }
491         }
492
493         return 0;
494 }
495
496 /*
497  * CFG802.11 operation handler to change interface type.
498  */
499 static int
500 mwifiex_cfg80211_change_virtual_intf(struct wiphy *wiphy,
501                                      struct net_device *dev,
502                                      enum nl80211_iftype type, u32 *flags,
503                                      struct vif_params *params)
504 {
505         int ret;
506         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
507
508         switch (dev->ieee80211_ptr->iftype) {
509         case NL80211_IFTYPE_ADHOC:
510                 switch (type) {
511                 case NL80211_IFTYPE_STATION:
512                         break;
513                 case NL80211_IFTYPE_UNSPECIFIED:
514                         wiphy_warn(wiphy, "%s: kept type as IBSS\n", dev->name);
515                 case NL80211_IFTYPE_ADHOC:      /* This shouldn't happen */
516                         return 0;
517                 case NL80211_IFTYPE_AP:
518                 default:
519                         wiphy_err(wiphy, "%s: changing to %d not supported\n",
520                                   dev->name, type);
521                         return -EOPNOTSUPP;
522                 }
523                 break;
524         case NL80211_IFTYPE_STATION:
525                 switch (type) {
526                 case NL80211_IFTYPE_ADHOC:
527                         break;
528                 case NL80211_IFTYPE_UNSPECIFIED:
529                         wiphy_warn(wiphy, "%s: kept type as STA\n", dev->name);
530                 case NL80211_IFTYPE_STATION:    /* This shouldn't happen */
531                         return 0;
532                 case NL80211_IFTYPE_AP:
533                 default:
534                         wiphy_err(wiphy, "%s: changing to %d not supported\n",
535                                   dev->name, type);
536                         return -EOPNOTSUPP;
537                 }
538                 break;
539         case NL80211_IFTYPE_AP:
540                 switch (type) {
541                 case NL80211_IFTYPE_UNSPECIFIED:
542                         wiphy_warn(wiphy, "%s: kept type as AP\n", dev->name);
543                 case NL80211_IFTYPE_AP:         /* This shouldn't happen */
544                         return 0;
545                 case NL80211_IFTYPE_ADHOC:
546                 case NL80211_IFTYPE_STATION:
547                 default:
548                         wiphy_err(wiphy, "%s: changing to %d not supported\n",
549                                   dev->name, type);
550                         return -EOPNOTSUPP;
551                 }
552                 break;
553         default:
554                 wiphy_err(wiphy, "%s: unknown iftype: %d\n",
555                           dev->name, dev->ieee80211_ptr->iftype);
556                 return -EOPNOTSUPP;
557         }
558
559         dev->ieee80211_ptr->iftype = type;
560         priv->bss_mode = type;
561         mwifiex_deauthenticate(priv, NULL);
562
563         priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
564
565         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_SET_BSS_MODE,
566                                     HostCmd_ACT_GEN_SET, 0, NULL);
567
568         return ret;
569 }
570
571 /*
572  * This function dumps the station information on a buffer.
573  *
574  * The following information are shown -
575  *      - Total bytes transmitted
576  *      - Total bytes received
577  *      - Total packets transmitted
578  *      - Total packets received
579  *      - Signal quality level
580  *      - Transmission rate
581  */
582 static int
583 mwifiex_dump_station_info(struct mwifiex_private *priv,
584                           struct station_info *sinfo)
585 {
586         u32 rate;
587
588         sinfo->filled = STATION_INFO_RX_BYTES | STATION_INFO_TX_BYTES |
589                         STATION_INFO_RX_PACKETS | STATION_INFO_TX_PACKETS |
590                         STATION_INFO_TX_BITRATE |
591                         STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
592
593         /* Get signal information from the firmware */
594         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_RSSI_INFO,
595                                   HostCmd_ACT_GEN_GET, 0, NULL)) {
596                 dev_err(priv->adapter->dev, "failed to get signal information\n");
597                 return -EFAULT;
598         }
599
600         if (mwifiex_drv_get_data_rate(priv, &rate)) {
601                 dev_err(priv->adapter->dev, "getting data rate\n");
602                 return -EFAULT;
603         }
604
605         /* Get DTIM period information from firmware */
606         mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_SNMP_MIB,
607                               HostCmd_ACT_GEN_GET, DTIM_PERIOD_I,
608                               &priv->dtim_period);
609
610         /*
611          * Bit 0 in tx_htinfo indicates that current Tx rate is 11n rate. Valid
612          * MCS index values for us are 0 to 15.
613          */
614         if ((priv->tx_htinfo & BIT(0)) && (priv->tx_rate < 16)) {
615                 sinfo->txrate.mcs = priv->tx_rate;
616                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
617                 /* 40MHz rate */
618                 if (priv->tx_htinfo & BIT(1))
619                         sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
620                 /* SGI enabled */
621                 if (priv->tx_htinfo & BIT(2))
622                         sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
623         }
624
625         sinfo->signal_avg = priv->bcn_rssi_avg;
626         sinfo->rx_bytes = priv->stats.rx_bytes;
627         sinfo->tx_bytes = priv->stats.tx_bytes;
628         sinfo->rx_packets = priv->stats.rx_packets;
629         sinfo->tx_packets = priv->stats.tx_packets;
630         sinfo->signal = priv->bcn_rssi_avg;
631         /* bit rate is in 500 kb/s units. Convert it to 100kb/s units */
632         sinfo->txrate.legacy = rate * 5;
633
634         if (priv->bss_mode == NL80211_IFTYPE_STATION) {
635                 sinfo->filled |= STATION_INFO_BSS_PARAM;
636                 sinfo->bss_param.flags = 0;
637                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
638                                                 WLAN_CAPABILITY_SHORT_PREAMBLE)
639                         sinfo->bss_param.flags |=
640                                         BSS_PARAM_FLAGS_SHORT_PREAMBLE;
641                 if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
642                                                 WLAN_CAPABILITY_SHORT_SLOT_TIME)
643                         sinfo->bss_param.flags |=
644                                         BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
645                 sinfo->bss_param.dtim_period = priv->dtim_period;
646                 sinfo->bss_param.beacon_interval =
647                         priv->curr_bss_params.bss_descriptor.beacon_period;
648         }
649
650         return 0;
651 }
652
653 /*
654  * CFG802.11 operation handler to get station information.
655  *
656  * This function only works in connected mode, and dumps the
657  * requested station information, if available.
658  */
659 static int
660 mwifiex_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
661                              u8 *mac, struct station_info *sinfo)
662 {
663         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
664
665         if (!priv->media_connected)
666                 return -ENOENT;
667         if (memcmp(mac, priv->cfg_bssid, ETH_ALEN))
668                 return -ENOENT;
669
670         return mwifiex_dump_station_info(priv, sinfo);
671 }
672
673 /*
674  * CFG802.11 operation handler to dump station information.
675  */
676 static int
677 mwifiex_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
678                               int idx, u8 *mac, struct station_info *sinfo)
679 {
680         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
681
682         if (!priv->media_connected || idx)
683                 return -ENOENT;
684
685         memcpy(mac, priv->cfg_bssid, ETH_ALEN);
686
687         return mwifiex_dump_station_info(priv, sinfo);
688 }
689
690 /* Supported rates to be advertised to the cfg80211 */
691
692 static struct ieee80211_rate mwifiex_rates[] = {
693         {.bitrate = 10, .hw_value = 2, },
694         {.bitrate = 20, .hw_value = 4, },
695         {.bitrate = 55, .hw_value = 11, },
696         {.bitrate = 110, .hw_value = 22, },
697         {.bitrate = 60, .hw_value = 12, },
698         {.bitrate = 90, .hw_value = 18, },
699         {.bitrate = 120, .hw_value = 24, },
700         {.bitrate = 180, .hw_value = 36, },
701         {.bitrate = 240, .hw_value = 48, },
702         {.bitrate = 360, .hw_value = 72, },
703         {.bitrate = 480, .hw_value = 96, },
704         {.bitrate = 540, .hw_value = 108, },
705 };
706
707 /* Channel definitions to be advertised to cfg80211 */
708
709 static struct ieee80211_channel mwifiex_channels_2ghz[] = {
710         {.center_freq = 2412, .hw_value = 1, },
711         {.center_freq = 2417, .hw_value = 2, },
712         {.center_freq = 2422, .hw_value = 3, },
713         {.center_freq = 2427, .hw_value = 4, },
714         {.center_freq = 2432, .hw_value = 5, },
715         {.center_freq = 2437, .hw_value = 6, },
716         {.center_freq = 2442, .hw_value = 7, },
717         {.center_freq = 2447, .hw_value = 8, },
718         {.center_freq = 2452, .hw_value = 9, },
719         {.center_freq = 2457, .hw_value = 10, },
720         {.center_freq = 2462, .hw_value = 11, },
721         {.center_freq = 2467, .hw_value = 12, },
722         {.center_freq = 2472, .hw_value = 13, },
723         {.center_freq = 2484, .hw_value = 14, },
724 };
725
726 static struct ieee80211_supported_band mwifiex_band_2ghz = {
727         .channels = mwifiex_channels_2ghz,
728         .n_channels = ARRAY_SIZE(mwifiex_channels_2ghz),
729         .bitrates = mwifiex_rates,
730         .n_bitrates = ARRAY_SIZE(mwifiex_rates),
731 };
732
733 static struct ieee80211_channel mwifiex_channels_5ghz[] = {
734         {.center_freq = 5040, .hw_value = 8, },
735         {.center_freq = 5060, .hw_value = 12, },
736         {.center_freq = 5080, .hw_value = 16, },
737         {.center_freq = 5170, .hw_value = 34, },
738         {.center_freq = 5190, .hw_value = 38, },
739         {.center_freq = 5210, .hw_value = 42, },
740         {.center_freq = 5230, .hw_value = 46, },
741         {.center_freq = 5180, .hw_value = 36, },
742         {.center_freq = 5200, .hw_value = 40, },
743         {.center_freq = 5220, .hw_value = 44, },
744         {.center_freq = 5240, .hw_value = 48, },
745         {.center_freq = 5260, .hw_value = 52, },
746         {.center_freq = 5280, .hw_value = 56, },
747         {.center_freq = 5300, .hw_value = 60, },
748         {.center_freq = 5320, .hw_value = 64, },
749         {.center_freq = 5500, .hw_value = 100, },
750         {.center_freq = 5520, .hw_value = 104, },
751         {.center_freq = 5540, .hw_value = 108, },
752         {.center_freq = 5560, .hw_value = 112, },
753         {.center_freq = 5580, .hw_value = 116, },
754         {.center_freq = 5600, .hw_value = 120, },
755         {.center_freq = 5620, .hw_value = 124, },
756         {.center_freq = 5640, .hw_value = 128, },
757         {.center_freq = 5660, .hw_value = 132, },
758         {.center_freq = 5680, .hw_value = 136, },
759         {.center_freq = 5700, .hw_value = 140, },
760         {.center_freq = 5745, .hw_value = 149, },
761         {.center_freq = 5765, .hw_value = 153, },
762         {.center_freq = 5785, .hw_value = 157, },
763         {.center_freq = 5805, .hw_value = 161, },
764         {.center_freq = 5825, .hw_value = 165, },
765 };
766
767 static struct ieee80211_supported_band mwifiex_band_5ghz = {
768         .channels = mwifiex_channels_5ghz,
769         .n_channels = ARRAY_SIZE(mwifiex_channels_5ghz),
770         .bitrates = mwifiex_rates + 4,
771         .n_bitrates = ARRAY_SIZE(mwifiex_rates) - 4,
772 };
773
774
775 /* Supported crypto cipher suits to be advertised to cfg80211 */
776
777 static const u32 mwifiex_cipher_suites[] = {
778         WLAN_CIPHER_SUITE_WEP40,
779         WLAN_CIPHER_SUITE_WEP104,
780         WLAN_CIPHER_SUITE_TKIP,
781         WLAN_CIPHER_SUITE_CCMP,
782         WLAN_CIPHER_SUITE_AES_CMAC,
783 };
784
785 /*
786  * CFG802.11 operation handler for setting bit rates.
787  *
788  * Function configures data rates to firmware using bitrate mask
789  * provided by cfg80211.
790  */
791 static int mwifiex_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
792                                 struct net_device *dev,
793                                 const u8 *peer,
794                                 const struct cfg80211_bitrate_mask *mask)
795 {
796         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
797         u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
798         enum ieee80211_band band;
799
800         if (!priv->media_connected) {
801                 dev_err(priv->adapter->dev,
802                         "Can not set Tx data rate in disconnected state\n");
803                 return -EINVAL;
804         }
805
806         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
807
808         memset(bitmap_rates, 0, sizeof(bitmap_rates));
809
810         /* Fill HR/DSSS rates. */
811         if (band == IEEE80211_BAND_2GHZ)
812                 bitmap_rates[0] = mask->control[band].legacy & 0x000f;
813
814         /* Fill OFDM rates */
815         if (band == IEEE80211_BAND_2GHZ)
816                 bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
817         else
818                 bitmap_rates[1] = mask->control[band].legacy;
819
820         /* Fill MCS rates */
821         bitmap_rates[2] = mask->control[band].mcs[0];
822         if (priv->adapter->hw_dev_mcs_support == HT_STREAM_2X2)
823                 bitmap_rates[2] |= mask->control[band].mcs[1] << 8;
824
825         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_TX_RATE_CFG,
826                                      HostCmd_ACT_GEN_SET, 0, bitmap_rates);
827 }
828
829 /*
830  * CFG802.11 operation handler for connection quality monitoring.
831  *
832  * This function subscribes/unsubscribes HIGH_RSSI and LOW_RSSI
833  * events to FW.
834  */
835 static int mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
836                                                 struct net_device *dev,
837                                                 s32 rssi_thold, u32 rssi_hyst)
838 {
839         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
840         struct mwifiex_ds_misc_subsc_evt subsc_evt;
841
842         priv->cqm_rssi_thold = rssi_thold;
843         priv->cqm_rssi_hyst = rssi_hyst;
844
845         memset(&subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
846         subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
847
848         /* Subscribe/unsubscribe low and high rssi events */
849         if (rssi_thold && rssi_hyst) {
850                 subsc_evt.action = HostCmd_ACT_BITWISE_SET;
851                 subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
852                 subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
853                 subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
854                 subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
855                 return mwifiex_send_cmd_sync(priv,
856                                              HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
857                                              0, 0, &subsc_evt);
858         } else {
859                 subsc_evt.action = HostCmd_ACT_BITWISE_CLR;
860                 return mwifiex_send_cmd_sync(priv,
861                                              HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
862                                              0, 0, &subsc_evt);
863         }
864
865         return 0;
866 }
867
868 /* cfg80211 operation handler for change_beacon.
869  * Function retrieves and sets modified management IEs to FW.
870  */
871 static int mwifiex_cfg80211_change_beacon(struct wiphy *wiphy,
872                                           struct net_device *dev,
873                                           struct cfg80211_beacon_data *data)
874 {
875         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
876
877         if (priv->bss_type != MWIFIEX_BSS_TYPE_UAP) {
878                 wiphy_err(wiphy, "%s: bss_type mismatched\n", __func__);
879                 return -EINVAL;
880         }
881
882         if (!priv->bss_started) {
883                 wiphy_err(wiphy, "%s: bss not started\n", __func__);
884                 return -EINVAL;
885         }
886
887         if (mwifiex_set_mgmt_ies(priv, data)) {
888                 wiphy_err(wiphy, "%s: setting mgmt ies failed\n", __func__);
889                 return -EFAULT;
890         }
891
892         return 0;
893 }
894
895 static int
896 mwifiex_cfg80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
897 {
898         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
899         struct mwifiex_private *priv = mwifiex_get_priv(adapter,
900                                                         MWIFIEX_BSS_ROLE_ANY);
901         struct mwifiex_ds_ant_cfg ant_cfg;
902
903         if (!tx_ant || !rx_ant)
904                 return -EOPNOTSUPP;
905
906         if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
907                 /* Not a MIMO chip. User should provide specific antenna number
908                  * for Tx/Rx path or enable all antennas for diversity
909                  */
910                 if (tx_ant != rx_ant)
911                         return -EOPNOTSUPP;
912
913                 if ((tx_ant & (tx_ant - 1)) &&
914                     (tx_ant != BIT(adapter->number_of_antenna) - 1))
915                         return -EOPNOTSUPP;
916
917                 if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
918                     (priv->adapter->number_of_antenna > 1)) {
919                         tx_ant = RF_ANTENNA_AUTO;
920                         rx_ant = RF_ANTENNA_AUTO;
921                 }
922         }
923
924         ant_cfg.tx_ant = tx_ant;
925         ant_cfg.rx_ant = rx_ant;
926
927         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_RF_ANTENNA,
928                                      HostCmd_ACT_GEN_SET, 0, &ant_cfg);
929 }
930
931 /* cfg80211 operation handler for stop ap.
932  * Function stops BSS running at uAP interface.
933  */
934 static int mwifiex_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
935 {
936         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
937
938         if (mwifiex_del_mgmt_ies(priv))
939                 wiphy_err(wiphy, "Failed to delete mgmt IEs!\n");
940
941         priv->ap_11n_enabled = 0;
942
943         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_UAP_BSS_STOP,
944                                   HostCmd_ACT_GEN_SET, 0, NULL)) {
945                 wiphy_err(wiphy, "Failed to stop the BSS\n");
946                 return -1;
947         }
948
949         return 0;
950 }
951
952 /* cfg80211 operation handler for start_ap.
953  * Function sets beacon period, DTIM period, SSID and security into
954  * AP config structure.
955  * AP is configured with these settings and BSS is started.
956  */
957 static int mwifiex_cfg80211_start_ap(struct wiphy *wiphy,
958                                      struct net_device *dev,
959                                      struct cfg80211_ap_settings *params)
960 {
961         struct mwifiex_uap_bss_param *bss_cfg;
962         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
963         u8 config_bands = 0;
964
965         if (priv->bss_type != MWIFIEX_BSS_TYPE_UAP)
966                 return -1;
967         if (mwifiex_set_mgmt_ies(priv, &params->beacon))
968                 return -1;
969
970         bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param), GFP_KERNEL);
971         if (!bss_cfg)
972                 return -ENOMEM;
973
974         mwifiex_set_sys_config_invalid_data(bss_cfg);
975
976         if (params->beacon_interval)
977                 bss_cfg->beacon_period = params->beacon_interval;
978         if (params->dtim_period)
979                 bss_cfg->dtim_period = params->dtim_period;
980
981         if (params->ssid && params->ssid_len) {
982                 memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
983                 bss_cfg->ssid.ssid_len = params->ssid_len;
984         }
985
986         switch (params->hidden_ssid) {
987         case NL80211_HIDDEN_SSID_NOT_IN_USE:
988                 bss_cfg->bcast_ssid_ctl = 1;
989                 break;
990         case NL80211_HIDDEN_SSID_ZERO_LEN:
991                 bss_cfg->bcast_ssid_ctl = 0;
992                 break;
993         case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
994                 /* firmware doesn't support this type of hidden SSID */
995         default:
996                 kfree(bss_cfg);
997                 return -EINVAL;
998         }
999
1000         bss_cfg->channel =
1001             (u8)ieee80211_frequency_to_channel(params->channel->center_freq);
1002
1003         /* Set appropriate bands */
1004         if (params->channel->band == IEEE80211_BAND_2GHZ) {
1005                 bss_cfg->band_cfg = BAND_CONFIG_BG;
1006
1007                 if (params->channel_type == NL80211_CHAN_NO_HT)
1008                         config_bands = BAND_B | BAND_G;
1009                 else
1010                         config_bands = BAND_B | BAND_G | BAND_GN;
1011         } else {
1012                 bss_cfg->band_cfg = BAND_CONFIG_A;
1013
1014                 if (params->channel_type == NL80211_CHAN_NO_HT)
1015                         config_bands = BAND_A;
1016                 else
1017                         config_bands = BAND_AN | BAND_A;
1018         }
1019
1020         if (!((config_bands | priv->adapter->fw_bands) &
1021               ~priv->adapter->fw_bands))
1022                 priv->adapter->config_bands = config_bands;
1023
1024         mwifiex_set_uap_rates(bss_cfg, params);
1025         mwifiex_send_domain_info_cmd_fw(wiphy);
1026
1027         if (mwifiex_set_secure_params(priv, bss_cfg, params)) {
1028                 kfree(bss_cfg);
1029                 wiphy_err(wiphy, "Failed to parse secuirty parameters!\n");
1030                 return -1;
1031         }
1032
1033         mwifiex_set_ht_params(priv, bss_cfg, params);
1034
1035         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_UAP_BSS_STOP,
1036                                   HostCmd_ACT_GEN_SET, 0, NULL)) {
1037                 wiphy_err(wiphy, "Failed to stop the BSS\n");
1038                 kfree(bss_cfg);
1039                 return -1;
1040         }
1041
1042         if (mwifiex_send_cmd_async(priv, HostCmd_CMD_UAP_SYS_CONFIG,
1043                                    HostCmd_ACT_GEN_SET,
1044                                    UAP_BSS_PARAMS_I, bss_cfg)) {
1045                 wiphy_err(wiphy, "Failed to set the SSID\n");
1046                 kfree(bss_cfg);
1047                 return -1;
1048         }
1049
1050         kfree(bss_cfg);
1051
1052         if (mwifiex_send_cmd_async(priv, HostCmd_CMD_UAP_BSS_START,
1053                                    HostCmd_ACT_GEN_SET, 0, NULL)) {
1054                 wiphy_err(wiphy, "Failed to start the BSS\n");
1055                 return -1;
1056         }
1057
1058         if (priv->sec_info.wep_enabled)
1059                 priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
1060         else
1061                 priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
1062
1063         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_MAC_CONTROL,
1064                                   HostCmd_ACT_GEN_SET, 0,
1065                                   &priv->curr_pkt_filter))
1066                 return -1;
1067
1068         return 0;
1069 }
1070
1071 /*
1072  * CFG802.11 operation handler for disconnection request.
1073  *
1074  * This function does not work when there is already a disconnection
1075  * procedure going on.
1076  */
1077 static int
1078 mwifiex_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
1079                             u16 reason_code)
1080 {
1081         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1082
1083         if (mwifiex_deauthenticate(priv, NULL))
1084                 return -EFAULT;
1085
1086         wiphy_dbg(wiphy, "info: successfully disconnected from %pM:"
1087                 " reason code %d\n", priv->cfg_bssid, reason_code);
1088
1089         memset(priv->cfg_bssid, 0, ETH_ALEN);
1090
1091         return 0;
1092 }
1093
1094 /*
1095  * This function informs the CFG802.11 subsystem of a new IBSS.
1096  *
1097  * The following information are sent to the CFG802.11 subsystem
1098  * to register the new IBSS. If we do not register the new IBSS,
1099  * a kernel panic will result.
1100  *      - SSID
1101  *      - SSID length
1102  *      - BSSID
1103  *      - Channel
1104  */
1105 static int mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private *priv)
1106 {
1107         struct ieee80211_channel *chan;
1108         struct mwifiex_bss_info bss_info;
1109         struct cfg80211_bss *bss;
1110         int ie_len;
1111         u8 ie_buf[IEEE80211_MAX_SSID_LEN + sizeof(struct ieee_types_header)];
1112         enum ieee80211_band band;
1113
1114         if (mwifiex_get_bss_info(priv, &bss_info))
1115                 return -1;
1116
1117         ie_buf[0] = WLAN_EID_SSID;
1118         ie_buf[1] = bss_info.ssid.ssid_len;
1119
1120         memcpy(&ie_buf[sizeof(struct ieee_types_header)],
1121                &bss_info.ssid.ssid, bss_info.ssid.ssid_len);
1122         ie_len = ie_buf[1] + sizeof(struct ieee_types_header);
1123
1124         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1125         chan = __ieee80211_get_channel(priv->wdev->wiphy,
1126                         ieee80211_channel_to_frequency(bss_info.bss_chan,
1127                                                        band));
1128
1129         bss = cfg80211_inform_bss(priv->wdev->wiphy, chan,
1130                                   bss_info.bssid, 0, WLAN_CAPABILITY_IBSS,
1131                                   0, ie_buf, ie_len, 0, GFP_KERNEL);
1132         cfg80211_put_bss(bss);
1133         memcpy(priv->cfg_bssid, bss_info.bssid, ETH_ALEN);
1134
1135         return 0;
1136 }
1137
1138 /*
1139  * This function connects with a BSS.
1140  *
1141  * This function handles both Infra and Ad-Hoc modes. It also performs
1142  * validity checking on the provided parameters, disconnects from the
1143  * current BSS (if any), sets up the association/scan parameters,
1144  * including security settings, and performs specific SSID scan before
1145  * trying to connect.
1146  *
1147  * For Infra mode, the function returns failure if the specified SSID
1148  * is not found in scan table. However, for Ad-Hoc mode, it can create
1149  * the IBSS if it does not exist. On successful completion in either case,
1150  * the function notifies the CFG802.11 subsystem of the new BSS connection.
1151  */
1152 static int
1153 mwifiex_cfg80211_assoc(struct mwifiex_private *priv, size_t ssid_len, u8 *ssid,
1154                        u8 *bssid, int mode, struct ieee80211_channel *channel,
1155                        struct cfg80211_connect_params *sme, bool privacy)
1156 {
1157         struct cfg80211_ssid req_ssid;
1158         int ret, auth_type = 0;
1159         struct cfg80211_bss *bss = NULL;
1160         u8 is_scanning_required = 0, config_bands = 0;
1161
1162         memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
1163
1164         req_ssid.ssid_len = ssid_len;
1165         if (ssid_len > IEEE80211_MAX_SSID_LEN) {
1166                 dev_err(priv->adapter->dev, "invalid SSID - aborting\n");
1167                 return -EINVAL;
1168         }
1169
1170         memcpy(req_ssid.ssid, ssid, ssid_len);
1171         if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
1172                 dev_err(priv->adapter->dev, "invalid SSID - aborting\n");
1173                 return -EINVAL;
1174         }
1175
1176         /* disconnect before try to associate */
1177         mwifiex_deauthenticate(priv, NULL);
1178
1179         if (channel) {
1180                 if (mode == NL80211_IFTYPE_STATION) {
1181                         if (channel->band == IEEE80211_BAND_2GHZ)
1182                                 config_bands = BAND_B | BAND_G | BAND_GN;
1183                         else
1184                                 config_bands = BAND_A | BAND_AN;
1185
1186                         if (!((config_bands | priv->adapter->fw_bands) &
1187                               ~priv->adapter->fw_bands))
1188                                 priv->adapter->config_bands = config_bands;
1189                 }
1190         }
1191
1192         /* As this is new association, clear locally stored
1193          * keys and security related flags */
1194         priv->sec_info.wpa_enabled = false;
1195         priv->sec_info.wpa2_enabled = false;
1196         priv->wep_key_curr_index = 0;
1197         priv->sec_info.encryption_mode = 0;
1198         priv->sec_info.is_authtype_auto = 0;
1199         ret = mwifiex_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
1200
1201         if (mode == NL80211_IFTYPE_ADHOC) {
1202                 /* "privacy" is set only for ad-hoc mode */
1203                 if (privacy) {
1204                         /*
1205                          * Keep WLAN_CIPHER_SUITE_WEP104 for now so that
1206                          * the firmware can find a matching network from the
1207                          * scan. The cfg80211 does not give us the encryption
1208                          * mode at this stage so just setting it to WEP here.
1209                          */
1210                         priv->sec_info.encryption_mode =
1211                                         WLAN_CIPHER_SUITE_WEP104;
1212                         priv->sec_info.authentication_mode =
1213                                         NL80211_AUTHTYPE_OPEN_SYSTEM;
1214                 }
1215
1216                 goto done;
1217         }
1218
1219         /* Now handle infra mode. "sme" is valid for infra mode only */
1220         if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
1221                 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1222                 priv->sec_info.is_authtype_auto = 1;
1223         } else {
1224                 auth_type = sme->auth_type;
1225         }
1226
1227         if (sme->crypto.n_ciphers_pairwise) {
1228                 priv->sec_info.encryption_mode =
1229                                                 sme->crypto.ciphers_pairwise[0];
1230                 priv->sec_info.authentication_mode = auth_type;
1231         }
1232
1233         if (sme->crypto.cipher_group) {
1234                 priv->sec_info.encryption_mode = sme->crypto.cipher_group;
1235                 priv->sec_info.authentication_mode = auth_type;
1236         }
1237         if (sme->ie)
1238                 ret = mwifiex_set_gen_ie(priv, sme->ie, sme->ie_len);
1239
1240         if (sme->key) {
1241                 if (mwifiex_is_alg_wep(priv->sec_info.encryption_mode)) {
1242                         dev_dbg(priv->adapter->dev,
1243                                 "info: setting wep encryption"
1244                                 " with key len %d\n", sme->key_len);
1245                         priv->wep_key_curr_index = sme->key_idx;
1246                         ret = mwifiex_set_encode(priv, NULL, sme->key,
1247                                                  sme->key_len, sme->key_idx,
1248                                                  NULL, 0);
1249                 }
1250         }
1251 done:
1252         /*
1253          * Scan entries are valid for some time (15 sec). So we can save one
1254          * active scan time if we just try cfg80211_get_bss first. If it fails
1255          * then request scan and cfg80211_get_bss() again for final output.
1256          */
1257         while (1) {
1258                 if (is_scanning_required) {
1259                         /* Do specific SSID scanning */
1260                         if (mwifiex_request_scan(priv, &req_ssid)) {
1261                                 dev_err(priv->adapter->dev, "scan error\n");
1262                                 return -EFAULT;
1263                         }
1264                 }
1265
1266                 /* Find the BSS we want using available scan results */
1267                 if (mode == NL80211_IFTYPE_ADHOC)
1268                         bss = cfg80211_get_bss(priv->wdev->wiphy, channel,
1269                                                bssid, ssid, ssid_len,
1270                                                WLAN_CAPABILITY_IBSS,
1271                                                WLAN_CAPABILITY_IBSS);
1272                 else
1273                         bss = cfg80211_get_bss(priv->wdev->wiphy, channel,
1274                                                bssid, ssid, ssid_len,
1275                                                WLAN_CAPABILITY_ESS,
1276                                                WLAN_CAPABILITY_ESS);
1277
1278                 if (!bss) {
1279                         if (is_scanning_required) {
1280                                 dev_warn(priv->adapter->dev,
1281                                          "assoc: requested bss not found in scan results\n");
1282                                 break;
1283                         }
1284                         is_scanning_required = 1;
1285                 } else {
1286                         dev_dbg(priv->adapter->dev,
1287                                 "info: trying to associate to '%s' bssid %pM\n",
1288                                 (char *) req_ssid.ssid, bss->bssid);
1289                         memcpy(&priv->cfg_bssid, bss->bssid, ETH_ALEN);
1290                         break;
1291                 }
1292         }
1293
1294         if (mwifiex_bss_start(priv, bss, &req_ssid))
1295                 return -EFAULT;
1296
1297         if (mode == NL80211_IFTYPE_ADHOC) {
1298                 /* Inform the BSS information to kernel, otherwise
1299                  * kernel will give a panic after successful assoc */
1300                 if (mwifiex_cfg80211_inform_ibss_bss(priv))
1301                         return -EFAULT;
1302         }
1303
1304         return ret;
1305 }
1306
1307 /*
1308  * CFG802.11 operation handler for association request.
1309  *
1310  * This function does not work when the current mode is set to Ad-Hoc, or
1311  * when there is already an association procedure going on. The given BSS
1312  * information is used to associate.
1313  */
1314 static int
1315 mwifiex_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
1316                          struct cfg80211_connect_params *sme)
1317 {
1318         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1319         int ret = 0;
1320
1321         if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
1322                 wiphy_err(wiphy, "received infra assoc request "
1323                                 "when station is in ibss mode\n");
1324                 goto done;
1325         }
1326
1327         if (priv->bss_mode == NL80211_IFTYPE_AP) {
1328                 wiphy_err(wiphy, "skip association request for AP interface\n");
1329                 goto done;
1330         }
1331
1332         wiphy_dbg(wiphy, "info: Trying to associate to %s and bssid %pM\n",
1333                   (char *) sme->ssid, sme->bssid);
1334
1335         ret = mwifiex_cfg80211_assoc(priv, sme->ssid_len, sme->ssid, sme->bssid,
1336                                      priv->bss_mode, sme->channel, sme, 0);
1337 done:
1338         if (!ret) {
1339                 cfg80211_connect_result(priv->netdev, priv->cfg_bssid, NULL, 0,
1340                                         NULL, 0, WLAN_STATUS_SUCCESS,
1341                                         GFP_KERNEL);
1342                 dev_dbg(priv->adapter->dev,
1343                         "info: associated to bssid %pM successfully\n",
1344                         priv->cfg_bssid);
1345         } else {
1346                 dev_dbg(priv->adapter->dev,
1347                         "info: association to bssid %pM failed\n",
1348                         priv->cfg_bssid);
1349                 memset(priv->cfg_bssid, 0, ETH_ALEN);
1350         }
1351
1352         return ret;
1353 }
1354
1355 /*
1356  * This function sets following parameters for ibss network.
1357  *  -  channel
1358  *  -  start band
1359  *  -  11n flag
1360  *  -  secondary channel offset
1361  */
1362 static int mwifiex_set_ibss_params(struct mwifiex_private *priv,
1363                                    struct cfg80211_ibss_params *params)
1364 {
1365         struct wiphy *wiphy = priv->wdev->wiphy;
1366         struct mwifiex_adapter *adapter = priv->adapter;
1367         int index = 0, i;
1368         u8 config_bands = 0;
1369
1370         if (params->channel->band == IEEE80211_BAND_2GHZ) {
1371                 if (!params->basic_rates) {
1372                         config_bands = BAND_B | BAND_G;
1373                 } else {
1374                         for (i = 0; i < mwifiex_band_2ghz.n_bitrates; i++) {
1375                                 /*
1376                                  * Rates below 6 Mbps in the table are CCK
1377                                  * rates; 802.11b and from 6 they are OFDM;
1378                                  * 802.11G
1379                                  */
1380                                 if (mwifiex_rates[i].bitrate == 60) {
1381                                         index = 1 << i;
1382                                         break;
1383                                 }
1384                         }
1385
1386                         if (params->basic_rates < index) {
1387                                 config_bands = BAND_B;
1388                         } else {
1389                                 config_bands = BAND_G;
1390                                 if (params->basic_rates % index)
1391                                         config_bands |= BAND_B;
1392                         }
1393                 }
1394
1395                 if (params->channel_type != NL80211_CHAN_NO_HT)
1396                         config_bands |= BAND_GN;
1397         } else {
1398                 if (params->channel_type == NL80211_CHAN_NO_HT)
1399                         config_bands = BAND_A;
1400                 else
1401                         config_bands = BAND_AN | BAND_A;
1402         }
1403
1404         if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands)) {
1405                 adapter->config_bands = config_bands;
1406                 adapter->adhoc_start_band = config_bands;
1407
1408                 if ((config_bands & BAND_GN) || (config_bands & BAND_AN))
1409                         adapter->adhoc_11n_enabled = true;
1410                 else
1411                         adapter->adhoc_11n_enabled = false;
1412         }
1413
1414         adapter->sec_chan_offset =
1415                 mwifiex_chan_type_to_sec_chan_offset(params->channel_type);
1416         priv->adhoc_channel =
1417                 ieee80211_frequency_to_channel(params->channel->center_freq);
1418
1419         wiphy_dbg(wiphy, "info: set ibss band %d, chan %d, chan offset %d\n",
1420                   config_bands, priv->adhoc_channel, adapter->sec_chan_offset);
1421
1422         return 0;
1423 }
1424
1425 /*
1426  * CFG802.11 operation handler to join an IBSS.
1427  *
1428  * This function does not work in any mode other than Ad-Hoc, or if
1429  * a join operation is already in progress.
1430  */
1431 static int
1432 mwifiex_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1433                            struct cfg80211_ibss_params *params)
1434 {
1435         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1436         int ret = 0;
1437
1438         if (priv->bss_mode != NL80211_IFTYPE_ADHOC) {
1439                 wiphy_err(wiphy, "request to join ibss received "
1440                                 "when station is not in ibss mode\n");
1441                 goto done;
1442         }
1443
1444         wiphy_dbg(wiphy, "info: trying to join to %s and bssid %pM\n",
1445                   (char *) params->ssid, params->bssid);
1446
1447         mwifiex_set_ibss_params(priv, params);
1448
1449         ret = mwifiex_cfg80211_assoc(priv, params->ssid_len, params->ssid,
1450                                      params->bssid, priv->bss_mode,
1451                                      params->channel, NULL, params->privacy);
1452 done:
1453         if (!ret) {
1454                 cfg80211_ibss_joined(priv->netdev, priv->cfg_bssid, GFP_KERNEL);
1455                 dev_dbg(priv->adapter->dev,
1456                         "info: joined/created adhoc network with bssid"
1457                         " %pM successfully\n", priv->cfg_bssid);
1458         } else {
1459                 dev_dbg(priv->adapter->dev,
1460                         "info: failed creating/joining adhoc network\n");
1461         }
1462
1463         return ret;
1464 }
1465
1466 /*
1467  * CFG802.11 operation handler to leave an IBSS.
1468  *
1469  * This function does not work if a leave operation is
1470  * already in progress.
1471  */
1472 static int
1473 mwifiex_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1474 {
1475         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1476
1477         wiphy_dbg(wiphy, "info: disconnecting from essid %pM\n",
1478                   priv->cfg_bssid);
1479         if (mwifiex_deauthenticate(priv, NULL))
1480                 return -EFAULT;
1481
1482         memset(priv->cfg_bssid, 0, ETH_ALEN);
1483
1484         return 0;
1485 }
1486
1487 /*
1488  * CFG802.11 operation handler for scan request.
1489  *
1490  * This function issues a scan request to the firmware based upon
1491  * the user specified scan configuration. On successfull completion,
1492  * it also informs the results.
1493  */
1494 static int
1495 mwifiex_cfg80211_scan(struct wiphy *wiphy,
1496                       struct cfg80211_scan_request *request)
1497 {
1498         struct net_device *dev = request->wdev->netdev;
1499         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1500         int i;
1501         struct ieee80211_channel *chan;
1502
1503         wiphy_dbg(wiphy, "info: received scan request on %s\n", dev->name);
1504
1505         if (atomic_read(&priv->wmm.tx_pkts_queued) >=
1506             MWIFIEX_MIN_TX_PENDING_TO_CANCEL_SCAN) {
1507                 dev_dbg(priv->adapter->dev, "scan rejected due to traffic\n");
1508                 return -EBUSY;
1509         }
1510
1511         priv->scan_request = request;
1512
1513         priv->user_scan_cfg = kzalloc(sizeof(struct mwifiex_user_scan_cfg),
1514                                       GFP_KERNEL);
1515         if (!priv->user_scan_cfg) {
1516                 dev_err(priv->adapter->dev, "failed to alloc scan_req\n");
1517                 return -ENOMEM;
1518         }
1519
1520         priv->user_scan_cfg->num_ssids = request->n_ssids;
1521         priv->user_scan_cfg->ssid_list = request->ssids;
1522
1523         if (request->ie && request->ie_len) {
1524                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
1525                         if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
1526                                 continue;
1527                         priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_SCAN;
1528                         memcpy(&priv->vs_ie[i].ie, request->ie,
1529                                request->ie_len);
1530                         break;
1531                 }
1532         }
1533
1534         for (i = 0; i < request->n_channels; i++) {
1535                 chan = request->channels[i];
1536                 priv->user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
1537                 priv->user_scan_cfg->chan_list[i].radio_type = chan->band;
1538
1539                 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
1540                         priv->user_scan_cfg->chan_list[i].scan_type =
1541                                                 MWIFIEX_SCAN_TYPE_PASSIVE;
1542                 else
1543                         priv->user_scan_cfg->chan_list[i].scan_type =
1544                                                 MWIFIEX_SCAN_TYPE_ACTIVE;
1545
1546                 priv->user_scan_cfg->chan_list[i].scan_time = 0;
1547         }
1548         if (mwifiex_scan_networks(priv, priv->user_scan_cfg))
1549                 return -EFAULT;
1550
1551         if (request->ie && request->ie_len) {
1552                 for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
1553                         if (priv->vs_ie[i].mask == MWIFIEX_VSIE_MASK_SCAN) {
1554                                 priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_CLEAR;
1555                                 memset(&priv->vs_ie[i].ie, 0,
1556                                        MWIFIEX_MAX_VSIE_LEN);
1557                         }
1558                 }
1559         }
1560         return 0;
1561 }
1562
1563 /*
1564  * This function sets up the CFG802.11 specific HT capability fields
1565  * with default values.
1566  *
1567  * The following default values are set -
1568  *      - HT Supported = True
1569  *      - Maximum AMPDU length factor = IEEE80211_HT_MAX_AMPDU_64K
1570  *      - Minimum AMPDU spacing = IEEE80211_HT_MPDU_DENSITY_NONE
1571  *      - HT Capabilities supported by firmware
1572  *      - MCS information, Rx mask = 0xff
1573  *      - MCD information, Tx parameters = IEEE80211_HT_MCS_TX_DEFINED (0x01)
1574  */
1575 static void
1576 mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap *ht_info,
1577                       struct mwifiex_private *priv)
1578 {
1579         int rx_mcs_supp;
1580         struct ieee80211_mcs_info mcs_set;
1581         u8 *mcs = (u8 *)&mcs_set;
1582         struct mwifiex_adapter *adapter = priv->adapter;
1583
1584         ht_info->ht_supported = true;
1585         ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1586         ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
1587
1588         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
1589
1590         /* Fill HT capability information */
1591         if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
1592                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1593         else
1594                 ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1595
1596         if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
1597                 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
1598         else
1599                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
1600
1601         if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
1602                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
1603         else
1604                 ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
1605
1606         if (ISSUPP_RXSTBC(adapter->hw_dot_11n_dev_cap))
1607                 ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
1608         else
1609                 ht_info->cap &= ~(3 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
1610
1611         if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
1612                 ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
1613         else
1614                 ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
1615
1616         ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
1617         ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
1618
1619         rx_mcs_supp = GET_RXMCSSUPP(adapter->hw_dev_mcs_support);
1620         /* Set MCS for 1x1 */
1621         memset(mcs, 0xff, rx_mcs_supp);
1622         /* Clear all the other values */
1623         memset(&mcs[rx_mcs_supp], 0,
1624                sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
1625         if (priv->bss_mode == NL80211_IFTYPE_STATION ||
1626             ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
1627                 /* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
1628                 SETHT_MCS32(mcs_set.rx_mask);
1629
1630         memcpy((u8 *) &ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
1631
1632         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
1633 }
1634
1635 /*
1636  *  create a new virtual interface with the given name
1637  */
1638 struct wireless_dev *mwifiex_add_virtual_intf(struct wiphy *wiphy,
1639                                               const char *name,
1640                                               enum nl80211_iftype type,
1641                                               u32 *flags,
1642                                               struct vif_params *params)
1643 {
1644         struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1645         struct mwifiex_private *priv;
1646         struct net_device *dev;
1647         void *mdev_priv;
1648         struct wireless_dev *wdev;
1649
1650         if (!adapter)
1651                 return ERR_PTR(-EFAULT);
1652
1653         switch (type) {
1654         case NL80211_IFTYPE_UNSPECIFIED:
1655         case NL80211_IFTYPE_STATION:
1656         case NL80211_IFTYPE_ADHOC:
1657                 priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
1658                 if (priv->bss_mode) {
1659                         wiphy_err(wiphy,
1660                                   "cannot create multiple sta/adhoc ifaces\n");
1661                         return ERR_PTR(-EINVAL);
1662                 }
1663
1664                 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
1665                 if (!wdev)
1666                         return ERR_PTR(-ENOMEM);
1667
1668                 wdev->wiphy = wiphy;
1669                 priv->wdev = wdev;
1670                 wdev->iftype = NL80211_IFTYPE_STATION;
1671
1672                 if (type == NL80211_IFTYPE_UNSPECIFIED)
1673                         priv->bss_mode = NL80211_IFTYPE_STATION;
1674                 else
1675                         priv->bss_mode = type;
1676
1677                 priv->bss_type = MWIFIEX_BSS_TYPE_STA;
1678                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
1679                 priv->bss_priority = 0;
1680                 priv->bss_role = MWIFIEX_BSS_ROLE_STA;
1681                 priv->bss_num = 0;
1682
1683                 break;
1684         case NL80211_IFTYPE_AP:
1685                 priv = adapter->priv[MWIFIEX_BSS_TYPE_UAP];
1686
1687                 if (priv->bss_mode) {
1688                         wiphy_err(wiphy, "Can't create multiple AP interfaces");
1689                         return ERR_PTR(-EINVAL);
1690                 }
1691
1692                 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
1693                 if (!wdev)
1694                         return ERR_PTR(-ENOMEM);
1695
1696                 priv->wdev = wdev;
1697                 wdev->wiphy = wiphy;
1698                 wdev->iftype = NL80211_IFTYPE_AP;
1699
1700                 priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
1701                 priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
1702                 priv->bss_priority = 0;
1703                 priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
1704                 priv->bss_started = 0;
1705                 priv->bss_num = 0;
1706                 priv->bss_mode = type;
1707
1708                 break;
1709         default:
1710                 wiphy_err(wiphy, "type not supported\n");
1711                 return ERR_PTR(-EINVAL);
1712         }
1713
1714         dev = alloc_netdev_mq(sizeof(struct mwifiex_private *), name,
1715                               ether_setup, 1);
1716         if (!dev) {
1717                 wiphy_err(wiphy, "no memory available for netdevice\n");
1718                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
1719                 return ERR_PTR(-ENOMEM);
1720         }
1721
1722         mwifiex_init_priv_params(priv, dev);
1723         priv->netdev = dev;
1724
1725         mwifiex_setup_ht_caps(&wiphy->bands[IEEE80211_BAND_2GHZ]->ht_cap, priv);
1726
1727         if (adapter->config_bands & BAND_A)
1728                 mwifiex_setup_ht_caps(
1729                         &wiphy->bands[IEEE80211_BAND_5GHZ]->ht_cap, priv);
1730
1731         dev_net_set(dev, wiphy_net(wiphy));
1732         dev->ieee80211_ptr = priv->wdev;
1733         dev->ieee80211_ptr->iftype = priv->bss_mode;
1734         memcpy(dev->dev_addr, wiphy->perm_addr, ETH_ALEN);
1735         memcpy(dev->perm_addr, wiphy->perm_addr, ETH_ALEN);
1736         SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
1737
1738         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1739         dev->watchdog_timeo = MWIFIEX_DEFAULT_WATCHDOG_TIMEOUT;
1740         dev->hard_header_len += MWIFIEX_MIN_DATA_HEADER_LEN;
1741
1742         mdev_priv = netdev_priv(dev);
1743         *((unsigned long *) mdev_priv) = (unsigned long) priv;
1744
1745         SET_NETDEV_DEV(dev, adapter->dev);
1746
1747         /* Register network device */
1748         if (register_netdevice(dev)) {
1749                 wiphy_err(wiphy, "cannot register virtual network device\n");
1750                 free_netdev(dev);
1751                 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
1752                 return ERR_PTR(-EFAULT);
1753         }
1754
1755         sema_init(&priv->async_sem, 1);
1756         priv->scan_pending_on_block = false;
1757
1758         dev_dbg(adapter->dev, "info: %s: Marvell 802.11 Adapter\n", dev->name);
1759
1760 #ifdef CONFIG_DEBUG_FS
1761         mwifiex_dev_debugfs_init(priv);
1762 #endif
1763         return wdev;
1764 }
1765 EXPORT_SYMBOL_GPL(mwifiex_add_virtual_intf);
1766
1767 /*
1768  * del_virtual_intf: remove the virtual interface determined by dev
1769  */
1770 int mwifiex_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
1771 {
1772         struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
1773
1774 #ifdef CONFIG_DEBUG_FS
1775         mwifiex_dev_debugfs_remove(priv);
1776 #endif
1777
1778         if (!netif_queue_stopped(priv->netdev))
1779                 netif_stop_queue(priv->netdev);
1780
1781         if (netif_carrier_ok(priv->netdev))
1782                 netif_carrier_off(priv->netdev);
1783
1784         if (wdev->netdev->reg_state == NETREG_REGISTERED)
1785                 unregister_netdevice(wdev->netdev);
1786
1787         if (wdev->netdev->reg_state == NETREG_UNREGISTERED)
1788                 free_netdev(wdev->netdev);
1789
1790         /* Clear the priv in adapter */
1791         priv->netdev = NULL;
1792
1793         priv->media_connected = false;
1794
1795         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
1796
1797         return 0;
1798 }
1799 EXPORT_SYMBOL_GPL(mwifiex_del_virtual_intf);
1800
1801 /* station cfg80211 operations */
1802 static struct cfg80211_ops mwifiex_cfg80211_ops = {
1803         .add_virtual_intf = mwifiex_add_virtual_intf,
1804         .del_virtual_intf = mwifiex_del_virtual_intf,
1805         .change_virtual_intf = mwifiex_cfg80211_change_virtual_intf,
1806         .scan = mwifiex_cfg80211_scan,
1807         .connect = mwifiex_cfg80211_connect,
1808         .disconnect = mwifiex_cfg80211_disconnect,
1809         .get_station = mwifiex_cfg80211_get_station,
1810         .dump_station = mwifiex_cfg80211_dump_station,
1811         .set_wiphy_params = mwifiex_cfg80211_set_wiphy_params,
1812         .join_ibss = mwifiex_cfg80211_join_ibss,
1813         .leave_ibss = mwifiex_cfg80211_leave_ibss,
1814         .add_key = mwifiex_cfg80211_add_key,
1815         .del_key = mwifiex_cfg80211_del_key,
1816         .set_default_key = mwifiex_cfg80211_set_default_key,
1817         .set_power_mgmt = mwifiex_cfg80211_set_power_mgmt,
1818         .set_tx_power = mwifiex_cfg80211_set_tx_power,
1819         .set_bitrate_mask = mwifiex_cfg80211_set_bitrate_mask,
1820         .start_ap = mwifiex_cfg80211_start_ap,
1821         .stop_ap = mwifiex_cfg80211_stop_ap,
1822         .change_beacon = mwifiex_cfg80211_change_beacon,
1823         .set_cqm_rssi_config = mwifiex_cfg80211_set_cqm_rssi_config,
1824         .set_antenna = mwifiex_cfg80211_set_antenna,
1825 };
1826
1827 /*
1828  * This function registers the device with CFG802.11 subsystem.
1829  *
1830  * The function creates the wireless device/wiphy, populates it with
1831  * default parameters and handler function pointers, and finally
1832  * registers the device.
1833  */
1834
1835 int mwifiex_register_cfg80211(struct mwifiex_adapter *adapter)
1836 {
1837         int ret;
1838         void *wdev_priv;
1839         struct wiphy *wiphy;
1840         struct mwifiex_private *priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
1841         u8 *country_code;
1842
1843         /* create a new wiphy for use with cfg80211 */
1844         wiphy = wiphy_new(&mwifiex_cfg80211_ops,
1845                           sizeof(struct mwifiex_adapter *));
1846         if (!wiphy) {
1847                 dev_err(adapter->dev, "%s: creating new wiphy\n", __func__);
1848                 return -ENOMEM;
1849         }
1850         wiphy->max_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
1851         wiphy->max_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
1852         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1853                                  BIT(NL80211_IFTYPE_ADHOC) |
1854                                  BIT(NL80211_IFTYPE_AP);
1855
1856         wiphy->bands[IEEE80211_BAND_2GHZ] = &mwifiex_band_2ghz;
1857         if (adapter->config_bands & BAND_A)
1858                 wiphy->bands[IEEE80211_BAND_5GHZ] = &mwifiex_band_5ghz;
1859         else
1860                 wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
1861
1862         wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta;
1863         wiphy->n_iface_combinations = 1;
1864
1865         /* Initialize cipher suits */
1866         wiphy->cipher_suites = mwifiex_cipher_suites;
1867         wiphy->n_cipher_suites = ARRAY_SIZE(mwifiex_cipher_suites);
1868
1869         memcpy(wiphy->perm_addr, priv->curr_addr, ETH_ALEN);
1870         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1871         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1872                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
1873                         WIPHY_FLAG_CUSTOM_REGULATORY;
1874
1875         wiphy_apply_custom_regulatory(wiphy, &mwifiex_world_regdom_custom);
1876
1877         wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1878                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
1879
1880         wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
1881         wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
1882
1883         wiphy->features = NL80211_FEATURE_HT_IBSS;
1884
1885         /* Reserve space for mwifiex specific private data for BSS */
1886         wiphy->bss_priv_size = sizeof(struct mwifiex_bss_priv);
1887
1888         wiphy->reg_notifier = mwifiex_reg_notifier;
1889
1890         /* Set struct mwifiex_adapter pointer in wiphy_priv */
1891         wdev_priv = wiphy_priv(wiphy);
1892         *(unsigned long *)wdev_priv = (unsigned long)adapter;
1893
1894         set_wiphy_dev(wiphy, priv->adapter->dev);
1895
1896         ret = wiphy_register(wiphy);
1897         if (ret < 0) {
1898                 dev_err(adapter->dev,
1899                         "%s: wiphy_register failed: %d\n", __func__, ret);
1900                 wiphy_free(wiphy);
1901                 return ret;
1902         }
1903         country_code = mwifiex_11d_code_2_region(priv->adapter->region_code);
1904         if (country_code)
1905                 dev_info(adapter->dev,
1906                          "ignoring F/W country code %2.2s\n", country_code);
1907
1908         adapter->wiphy = wiphy;
1909         return ret;
1910 }