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