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