20ed12547838a7a58ce3a6c58de7ce8b35c94fbf
[cascardo/linux.git] / drivers / net / wireless / mwifiex / sta_ioctl.c
1 /*
2  * Marvell Wireless LAN device driver: functions for station ioctl
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 "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27 #include "cfg80211.h"
28
29 static int disconnect_on_suspend = 1;
30 module_param(disconnect_on_suspend, int, 0644);
31
32 /*
33  * Copies the multicast address list from device to driver.
34  *
35  * This function does not validate the destination memory for
36  * size, and the calling function must ensure enough memory is
37  * available.
38  */
39 int mwifiex_copy_mcast_addr(struct mwifiex_multicast_list *mlist,
40                             struct net_device *dev)
41 {
42         int i = 0;
43         struct netdev_hw_addr *ha;
44
45         netdev_for_each_mc_addr(ha, dev)
46                 memcpy(&mlist->mac_list[i++], ha->addr, ETH_ALEN);
47
48         return i;
49 }
50
51 /*
52  * Wait queue completion handler.
53  *
54  * This function waits on a cmd wait queue. It also cancels the pending
55  * request after waking up, in case of errors.
56  */
57 int mwifiex_wait_queue_complete(struct mwifiex_adapter *adapter)
58 {
59         int status;
60         struct cmd_ctrl_node *cmd_queued;
61
62         if (!adapter->cmd_queued)
63                 return 0;
64
65         cmd_queued = adapter->cmd_queued;
66         adapter->cmd_queued = NULL;
67
68         dev_dbg(adapter->dev, "cmd pending\n");
69         atomic_inc(&adapter->cmd_pending);
70
71         /* Wait for completion */
72         status = wait_event_interruptible(adapter->cmd_wait_q.wait,
73                                           *(cmd_queued->condition));
74         if (status) {
75                 dev_err(adapter->dev, "cmd_wait_q terminated: %d\n", status);
76                 return status;
77         }
78
79         status = adapter->cmd_wait_q.status;
80         adapter->cmd_wait_q.status = 0;
81
82         return status;
83 }
84
85 /*
86  * This function prepares the correct firmware command and
87  * issues it to set the multicast list.
88  *
89  * This function can be used to enable promiscuous mode, or enable all
90  * multicast packets, or to enable selective multicast.
91  */
92 int mwifiex_request_set_multicast_list(struct mwifiex_private *priv,
93                                 struct mwifiex_multicast_list *mcast_list)
94 {
95         int ret = 0;
96         u16 old_pkt_filter;
97
98         old_pkt_filter = priv->curr_pkt_filter;
99
100         if (mcast_list->mode == MWIFIEX_PROMISC_MODE) {
101                 dev_dbg(priv->adapter->dev, "info: Enable Promiscuous mode\n");
102                 priv->curr_pkt_filter |= HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
103                 priv->curr_pkt_filter &=
104                         ~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
105         } else {
106                 /* Multicast */
107                 priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
108                 if (mcast_list->mode == MWIFIEX_MULTICAST_MODE) {
109                         dev_dbg(priv->adapter->dev,
110                                 "info: Enabling All Multicast!\n");
111                         priv->curr_pkt_filter |=
112                                 HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
113                 } else {
114                         priv->curr_pkt_filter &=
115                                 ~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
116                         if (mcast_list->num_multicast_addr) {
117                                 dev_dbg(priv->adapter->dev,
118                                         "info: Set multicast list=%d\n",
119                                        mcast_list->num_multicast_addr);
120                                 /* Set multicast addresses to firmware */
121                                 if (old_pkt_filter == priv->curr_pkt_filter) {
122                                         /* Send request to firmware */
123                                         ret = mwifiex_send_cmd_async(priv,
124                                                 HostCmd_CMD_MAC_MULTICAST_ADR,
125                                                 HostCmd_ACT_GEN_SET, 0,
126                                                 mcast_list);
127                                 } else {
128                                         /* Send request to firmware */
129                                         ret = mwifiex_send_cmd_async(priv,
130                                                 HostCmd_CMD_MAC_MULTICAST_ADR,
131                                                 HostCmd_ACT_GEN_SET, 0,
132                                                 mcast_list);
133                                 }
134                         }
135                 }
136         }
137         dev_dbg(priv->adapter->dev,
138                 "info: old_pkt_filter=%#x, curr_pkt_filter=%#x\n",
139                old_pkt_filter, priv->curr_pkt_filter);
140         if (old_pkt_filter != priv->curr_pkt_filter) {
141                 ret = mwifiex_send_cmd_async(priv, HostCmd_CMD_MAC_CONTROL,
142                                              HostCmd_ACT_GEN_SET,
143                                              0, &priv->curr_pkt_filter);
144         }
145
146         return ret;
147 }
148
149 /*
150  * This function fills bss descriptor structure using provided
151  * information.
152  */
153 int mwifiex_fill_new_bss_desc(struct mwifiex_private *priv,
154                               struct cfg80211_bss *bss,
155                               struct mwifiex_bssdescriptor *bss_desc,
156                               u8 *beacon_ie, size_t beacon_ie_len)
157 {
158         struct mwifiex_bss_priv *bss_priv = (void *)bss->priv;
159
160         memcpy(bss_desc->mac_address, bss->bssid, ETH_ALEN);
161         bss_desc->rssi = bss->signal;
162         bss_desc->beacon_buf = beacon_ie;
163         bss_desc->beacon_buf_size = beacon_ie_len;
164         bss_desc->beacon_period = bss->beacon_interval;
165         bss_desc->cap_info_bitmap = bss->capability;
166         bss_desc->bss_band = bss_priv->band;
167         bss_desc->fw_tsf = bss_priv->fw_tsf;
168         bss_desc->timestamp = bss->tsf;
169         if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_PRIVACY) {
170                 dev_dbg(priv->adapter->dev, "info: InterpretIE: AP WEP enabled\n");
171                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
172         } else {
173                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
174         }
175         if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_IBSS)
176                 bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
177         else
178                 bss_desc->bss_mode = NL80211_IFTYPE_STATION;
179
180         return mwifiex_update_bss_desc_with_ie(priv->adapter, bss_desc);
181 }
182
183 static int mwifiex_process_country_ie(struct mwifiex_private *priv,
184                                       struct cfg80211_bss *bss)
185 {
186         u8 *country_ie, country_ie_len;
187         struct mwifiex_802_11d_domain_reg *domain_info =
188                                         &priv->adapter->domain_reg;
189
190         country_ie = (u8 *)ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
191
192         if (!country_ie)
193                 return 0;
194
195         country_ie_len = country_ie[1];
196         if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
197                 return 0;
198
199         domain_info->country_code[0] = country_ie[2];
200         domain_info->country_code[1] = country_ie[3];
201         domain_info->country_code[2] = ' ';
202
203         country_ie_len -= IEEE80211_COUNTRY_STRING_LEN;
204
205         domain_info->no_of_triplet =
206                 country_ie_len / sizeof(struct ieee80211_country_ie_triplet);
207
208         memcpy((u8 *)domain_info->triplet,
209                &country_ie[2] + IEEE80211_COUNTRY_STRING_LEN, country_ie_len);
210
211         if (mwifiex_send_cmd_async(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
212                                    HostCmd_ACT_GEN_SET, 0, NULL)) {
213                 wiphy_err(priv->wdev->wiphy,
214                           "11D: setting domain info in FW\n");
215                 return -1;
216         }
217
218         return 0;
219 }
220
221 /*
222  * In Ad-Hoc mode, the IBSS is created if not found in scan list.
223  * In both Ad-Hoc and infra mode, an deauthentication is performed
224  * first.
225  */
226 int mwifiex_bss_start(struct mwifiex_private *priv, struct cfg80211_bss *bss,
227                       struct cfg80211_ssid *req_ssid)
228 {
229         int ret;
230         struct mwifiex_adapter *adapter = priv->adapter;
231         struct mwifiex_bssdescriptor *bss_desc = NULL;
232         u8 *beacon_ie = NULL;
233         size_t beacon_ie_len = bss->len_information_elements;
234
235         priv->scan_block = false;
236
237         if (bss) {
238                 mwifiex_process_country_ie(priv, bss);
239
240                 /* Allocate and fill new bss descriptor */
241                 bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor),
242                                 GFP_KERNEL);
243                 if (!bss_desc) {
244                         dev_err(priv->adapter->dev, " failed to alloc bss_desc\n");
245                         return -ENOMEM;
246                 }
247
248                 beacon_ie = kmemdup(bss->information_elements, beacon_ie_len,
249                                     GFP_KERNEL);
250                 if (!beacon_ie) {
251                         kfree(bss_desc);
252                         return -ENOMEM;
253                 }
254
255                 ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc, beacon_ie,
256                                                 beacon_ie_len);
257                 if (ret)
258                         goto done;
259         }
260
261         if (priv->bss_mode == NL80211_IFTYPE_STATION) {
262                 /* Infra mode */
263                 ret = mwifiex_deauthenticate(priv, NULL);
264                 if (ret)
265                         goto done;
266
267                 ret = mwifiex_check_network_compatibility(priv, bss_desc);
268                 if (ret)
269                         goto done;
270
271                 dev_dbg(adapter->dev, "info: SSID found in scan list ... "
272                                       "associating...\n");
273
274                 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
275                 if (netif_carrier_ok(priv->netdev))
276                         netif_carrier_off(priv->netdev);
277
278                 /* Clear any past association response stored for
279                  * application retrieval */
280                 priv->assoc_rsp_size = 0;
281                 ret = mwifiex_associate(priv, bss_desc);
282
283                 /* If auth type is auto and association fails using open mode,
284                  * try to connect using shared mode */
285                 if (ret == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
286                     priv->sec_info.is_authtype_auto &&
287                     priv->sec_info.wep_enabled) {
288                         priv->sec_info.authentication_mode =
289                                                 NL80211_AUTHTYPE_SHARED_KEY;
290                         ret = mwifiex_associate(priv, bss_desc);
291                 }
292
293                 if (bss)
294                         cfg80211_put_bss(bss);
295         } else {
296                 /* Adhoc mode */
297                 /* If the requested SSID matches current SSID, return */
298                 if (bss_desc && bss_desc->ssid.ssid_len &&
299                     (!mwifiex_ssid_cmp(&priv->curr_bss_params.bss_descriptor.
300                                        ssid, &bss_desc->ssid))) {
301                         ret = 0;
302                         goto done;
303                 }
304
305                 /* Exit Adhoc mode first */
306                 dev_dbg(adapter->dev, "info: Sending Adhoc Stop\n");
307                 ret = mwifiex_deauthenticate(priv, NULL);
308                 if (ret)
309                         goto done;
310
311                 priv->adhoc_is_link_sensed = false;
312
313                 ret = mwifiex_check_network_compatibility(priv, bss_desc);
314
315                 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
316                 if (netif_carrier_ok(priv->netdev))
317                         netif_carrier_off(priv->netdev);
318
319                 if (!ret) {
320                         dev_dbg(adapter->dev, "info: network found in scan"
321                                                         " list. Joining...\n");
322                         ret = mwifiex_adhoc_join(priv, bss_desc);
323                         if (bss)
324                                 cfg80211_put_bss(bss);
325                 } else {
326                         dev_dbg(adapter->dev, "info: Network not found in "
327                                 "the list, creating adhoc with ssid = %s\n",
328                                 req_ssid->ssid);
329                         ret = mwifiex_adhoc_start(priv, req_ssid);
330                 }
331         }
332
333 done:
334         kfree(beacon_ie);
335         kfree(bss_desc);
336         return ret;
337 }
338
339 /*
340  * IOCTL request handler to set host sleep configuration.
341  *
342  * This function prepares the correct firmware command and
343  * issues it.
344  */
345 static int mwifiex_set_hs_params(struct mwifiex_private *priv, u16 action,
346                                  int cmd_type, struct mwifiex_ds_hs_cfg *hs_cfg)
347
348 {
349         struct mwifiex_adapter *adapter = priv->adapter;
350         int status = 0;
351         u32 prev_cond = 0;
352
353         if (!hs_cfg)
354                 return -ENOMEM;
355
356         switch (action) {
357         case HostCmd_ACT_GEN_SET:
358                 if (adapter->pps_uapsd_mode) {
359                         dev_dbg(adapter->dev, "info: Host Sleep IOCTL"
360                                 " is blocked in UAPSD/PPS mode\n");
361                         status = -1;
362                         break;
363                 }
364                 if (hs_cfg->is_invoke_hostcmd) {
365                         if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL) {
366                                 if (!adapter->is_hs_configured)
367                                         /* Already cancelled */
368                                         break;
369                                 /* Save previous condition */
370                                 prev_cond = le32_to_cpu(adapter->hs_cfg
371                                                         .conditions);
372                                 adapter->hs_cfg.conditions =
373                                                 cpu_to_le32(hs_cfg->conditions);
374                         } else if (hs_cfg->conditions) {
375                                 adapter->hs_cfg.conditions =
376                                                 cpu_to_le32(hs_cfg->conditions);
377                                 adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
378                                 if (hs_cfg->gap)
379                                         adapter->hs_cfg.gap = (u8)hs_cfg->gap;
380                         } else if (adapter->hs_cfg.conditions
381                                    == cpu_to_le32(HOST_SLEEP_CFG_CANCEL)) {
382                                 /* Return failure if no parameters for HS
383                                    enable */
384                                 status = -1;
385                                 break;
386                         }
387                         if (cmd_type == MWIFIEX_SYNC_CMD)
388                                 status = mwifiex_send_cmd_sync(priv,
389                                                 HostCmd_CMD_802_11_HS_CFG_ENH,
390                                                 HostCmd_ACT_GEN_SET, 0,
391                                                 &adapter->hs_cfg);
392                         else
393                                 status = mwifiex_send_cmd_async(priv,
394                                                 HostCmd_CMD_802_11_HS_CFG_ENH,
395                                                 HostCmd_ACT_GEN_SET, 0,
396                                                 &adapter->hs_cfg);
397                         if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL)
398                                 /* Restore previous condition */
399                                 adapter->hs_cfg.conditions =
400                                                 cpu_to_le32(prev_cond);
401                 } else {
402                         adapter->hs_cfg.conditions =
403                                                 cpu_to_le32(hs_cfg->conditions);
404                         adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
405                         adapter->hs_cfg.gap = (u8)hs_cfg->gap;
406                 }
407                 break;
408         case HostCmd_ACT_GEN_GET:
409                 hs_cfg->conditions = le32_to_cpu(adapter->hs_cfg.conditions);
410                 hs_cfg->gpio = adapter->hs_cfg.gpio;
411                 hs_cfg->gap = adapter->hs_cfg.gap;
412                 break;
413         default:
414                 status = -1;
415                 break;
416         }
417
418         return status;
419 }
420
421 /*
422  * Sends IOCTL request to cancel the existing Host Sleep configuration.
423  *
424  * This function allocates the IOCTL request buffer, fills it
425  * with requisite parameters and calls the IOCTL handler.
426  */
427 int mwifiex_cancel_hs(struct mwifiex_private *priv, int cmd_type)
428 {
429         struct mwifiex_ds_hs_cfg hscfg;
430
431         hscfg.conditions = HOST_SLEEP_CFG_CANCEL;
432         hscfg.is_invoke_hostcmd = true;
433
434         return mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
435                                     cmd_type, &hscfg);
436 }
437 EXPORT_SYMBOL_GPL(mwifiex_cancel_hs);
438
439 /*
440  * Sends IOCTL request to cancel the existing Host Sleep configuration.
441  *
442  * This function allocates the IOCTL request buffer, fills it
443  * with requisite parameters and calls the IOCTL handler.
444  */
445 int mwifiex_enable_hs(struct mwifiex_adapter *adapter)
446 {
447         struct mwifiex_ds_hs_cfg hscfg;
448         struct mwifiex_private *priv;
449         int i;
450
451         if (disconnect_on_suspend) {
452                 for (i = 0; i < adapter->priv_num; i++) {
453                         priv = adapter->priv[i];
454                         if (priv && priv->media_connected)
455                                 mwifiex_deauthenticate(priv, NULL);
456                 }
457         }
458
459         if (adapter->hs_activated) {
460                 dev_dbg(adapter->dev, "cmd: HS Already actived\n");
461                 return true;
462         }
463
464         adapter->hs_activate_wait_q_woken = false;
465
466         memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
467         hscfg.is_invoke_hostcmd = true;
468
469         if (mwifiex_set_hs_params(mwifiex_get_priv(adapter,
470                                                    MWIFIEX_BSS_ROLE_STA),
471                                   HostCmd_ACT_GEN_SET, MWIFIEX_SYNC_CMD,
472                                   &hscfg)) {
473                 dev_err(adapter->dev, "IOCTL request HS enable failed\n");
474                 return false;
475         }
476
477         if (wait_event_interruptible(adapter->hs_activate_wait_q,
478                                      adapter->hs_activate_wait_q_woken)) {
479                 dev_err(adapter->dev, "hs_activate_wait_q terminated\n");
480                 return false;
481         }
482
483         return true;
484 }
485 EXPORT_SYMBOL_GPL(mwifiex_enable_hs);
486
487 /*
488  * IOCTL request handler to get BSS information.
489  *
490  * This function collates the information from different driver structures
491  * to send to the user.
492  */
493 int mwifiex_get_bss_info(struct mwifiex_private *priv,
494                          struct mwifiex_bss_info *info)
495 {
496         struct mwifiex_adapter *adapter = priv->adapter;
497         struct mwifiex_bssdescriptor *bss_desc;
498
499         if (!info)
500                 return -1;
501
502         bss_desc = &priv->curr_bss_params.bss_descriptor;
503
504         info->bss_mode = priv->bss_mode;
505
506         memcpy(&info->ssid, &bss_desc->ssid, sizeof(struct cfg80211_ssid));
507
508         memcpy(&info->bssid, &bss_desc->mac_address, ETH_ALEN);
509
510         info->bss_chan = bss_desc->channel;
511
512         memcpy(info->country_code, priv->country_code,
513                IEEE80211_COUNTRY_STRING_LEN);
514
515         info->media_connected = priv->media_connected;
516
517         info->max_power_level = priv->max_tx_power_level;
518         info->min_power_level = priv->min_tx_power_level;
519
520         info->adhoc_state = priv->adhoc_state;
521
522         info->bcn_nf_last = priv->bcn_nf_last;
523
524         if (priv->sec_info.wep_enabled)
525                 info->wep_status = true;
526         else
527                 info->wep_status = false;
528
529         info->is_hs_configured = adapter->is_hs_configured;
530         info->is_deep_sleep = adapter->is_deep_sleep;
531
532         return 0;
533 }
534
535 /*
536  * The function disables auto deep sleep mode.
537  */
538 int mwifiex_disable_auto_ds(struct mwifiex_private *priv)
539 {
540         struct mwifiex_ds_auto_ds auto_ds;
541
542         auto_ds.auto_ds = DEEP_SLEEP_OFF;
543
544         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
545                                      DIS_AUTO_PS, BITMAP_AUTO_DS, &auto_ds);
546 }
547 EXPORT_SYMBOL_GPL(mwifiex_disable_auto_ds);
548
549 /*
550  * IOCTL request handler to set/get active channel.
551  *
552  * This function performs validity checking on channel/frequency
553  * compatibility and returns failure if not valid.
554  */
555 int mwifiex_bss_set_channel(struct mwifiex_private *priv,
556                             struct mwifiex_chan_freq_power *chan)
557 {
558         struct mwifiex_adapter *adapter = priv->adapter;
559         struct mwifiex_chan_freq_power *cfp = NULL;
560
561         if (!chan)
562                 return -1;
563
564         if (!chan->channel && !chan->freq)
565                 return -1;
566         if (adapter->adhoc_start_band & BAND_AN)
567                 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
568         else if (adapter->adhoc_start_band & BAND_A)
569                 adapter->adhoc_start_band = BAND_G | BAND_B;
570         if (chan->channel) {
571                 if (chan->channel <= MAX_CHANNEL_BAND_BG)
572                         cfp = mwifiex_get_cfp(priv, 0, (u16) chan->channel, 0);
573                 if (!cfp) {
574                         cfp = mwifiex_get_cfp(priv, BAND_A,
575                                               (u16) chan->channel, 0);
576                         if (cfp) {
577                                 if (adapter->adhoc_11n_enabled)
578                                         adapter->adhoc_start_band = BAND_A
579                                                                     | BAND_AN;
580                                 else
581                                         adapter->adhoc_start_band = BAND_A;
582                         }
583                 }
584         } else {
585                 if (chan->freq <= MAX_FREQUENCY_BAND_BG)
586                         cfp = mwifiex_get_cfp(priv, 0, 0, chan->freq);
587                 if (!cfp) {
588                         cfp = mwifiex_get_cfp(priv, BAND_A, 0, chan->freq);
589                         if (cfp) {
590                                 if (adapter->adhoc_11n_enabled)
591                                         adapter->adhoc_start_band = BAND_A
592                                                                     | BAND_AN;
593                                 else
594                                         adapter->adhoc_start_band = BAND_A;
595                         }
596                 }
597         }
598         if (!cfp || !cfp->channel) {
599                 dev_err(adapter->dev, "invalid channel/freq\n");
600                 return -1;
601         }
602         priv->adhoc_channel = (u8) cfp->channel;
603         chan->channel = cfp->channel;
604         chan->freq = cfp->freq;
605
606         return 0;
607 }
608
609 /*
610  * IOCTL request handler to set/get Ad-Hoc channel.
611  *
612  * This function prepares the correct firmware command and
613  * issues it to set or get the ad-hoc channel.
614  */
615 static int mwifiex_bss_ioctl_ibss_channel(struct mwifiex_private *priv,
616                                           u16 action, u16 *channel)
617 {
618         if (action == HostCmd_ACT_GEN_GET) {
619                 if (!priv->media_connected) {
620                         *channel = priv->adhoc_channel;
621                         return 0;
622                 }
623         } else {
624                 priv->adhoc_channel = (u8) *channel;
625         }
626
627         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_RF_CHANNEL,
628                                      action, 0, channel);
629 }
630
631 /*
632  * IOCTL request handler to change Ad-Hoc channel.
633  *
634  * This function allocates the IOCTL request buffer, fills it
635  * with requisite parameters and calls the IOCTL handler.
636  *
637  * The function follows the following steps to perform the change -
638  *      - Get current IBSS information
639  *      - Get current channel
640  *      - If no change is required, return
641  *      - If not connected, change channel and return
642  *      - If connected,
643  *          - Disconnect
644  *          - Change channel
645  *          - Perform specific SSID scan with same SSID
646  *          - Start/Join the IBSS
647  */
648 int
649 mwifiex_drv_change_adhoc_chan(struct mwifiex_private *priv, u16 channel)
650 {
651         int ret;
652         struct mwifiex_bss_info bss_info;
653         struct mwifiex_ssid_bssid ssid_bssid;
654         u16 curr_chan = 0;
655         struct cfg80211_bss *bss = NULL;
656         struct ieee80211_channel *chan;
657         enum ieee80211_band band;
658
659         memset(&bss_info, 0, sizeof(bss_info));
660
661         /* Get BSS information */
662         if (mwifiex_get_bss_info(priv, &bss_info))
663                 return -1;
664
665         /* Get current channel */
666         ret = mwifiex_bss_ioctl_ibss_channel(priv, HostCmd_ACT_GEN_GET,
667                                              &curr_chan);
668
669         if (curr_chan == channel) {
670                 ret = 0;
671                 goto done;
672         }
673         dev_dbg(priv->adapter->dev, "cmd: updating channel from %d to %d\n",
674                 curr_chan, channel);
675
676         if (!bss_info.media_connected) {
677                 ret = 0;
678                 goto done;
679         }
680
681         /* Do disonnect */
682         memset(&ssid_bssid, 0, ETH_ALEN);
683         ret = mwifiex_deauthenticate(priv, ssid_bssid.bssid);
684
685         ret = mwifiex_bss_ioctl_ibss_channel(priv, HostCmd_ACT_GEN_SET,
686                                              &channel);
687
688         /* Do specific SSID scanning */
689         if (mwifiex_request_scan(priv, &bss_info.ssid)) {
690                 ret = -1;
691                 goto done;
692         }
693
694         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
695         chan = __ieee80211_get_channel(priv->wdev->wiphy,
696                                        ieee80211_channel_to_frequency(channel,
697                                                                       band));
698
699         /* Find the BSS we want using available scan results */
700         bss = cfg80211_get_bss(priv->wdev->wiphy, chan, bss_info.bssid,
701                                bss_info.ssid.ssid, bss_info.ssid.ssid_len,
702                                WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
703         if (!bss)
704                 wiphy_warn(priv->wdev->wiphy, "assoc: bss %pM not in scan results\n",
705                            bss_info.bssid);
706
707         ret = mwifiex_bss_start(priv, bss, &bss_info.ssid);
708 done:
709         return ret;
710 }
711
712 /*
713  * IOCTL request handler to get rate.
714  *
715  * This function prepares the correct firmware command and
716  * issues it to get the current rate if it is connected,
717  * otherwise, the function returns the lowest supported rate
718  * for the band.
719  */
720 static int mwifiex_rate_ioctl_get_rate_value(struct mwifiex_private *priv,
721                                              struct mwifiex_rate_cfg *rate_cfg)
722 {
723         rate_cfg->is_rate_auto = priv->is_data_rate_auto;
724         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_TX_RATE_QUERY,
725                                      HostCmd_ACT_GEN_GET, 0, NULL);
726 }
727
728 /*
729  * IOCTL request handler to set rate.
730  *
731  * This function prepares the correct firmware command and
732  * issues it to set the current rate.
733  *
734  * The function also performs validation checking on the supplied value.
735  */
736 static int mwifiex_rate_ioctl_set_rate_value(struct mwifiex_private *priv,
737                                              struct mwifiex_rate_cfg *rate_cfg)
738 {
739         u8 rates[MWIFIEX_SUPPORTED_RATES];
740         u8 *rate;
741         int rate_index, ret;
742         u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
743         u32 i;
744         struct mwifiex_adapter *adapter = priv->adapter;
745
746         if (rate_cfg->is_rate_auto) {
747                 memset(bitmap_rates, 0, sizeof(bitmap_rates));
748                 /* Support all HR/DSSS rates */
749                 bitmap_rates[0] = 0x000F;
750                 /* Support all OFDM rates */
751                 bitmap_rates[1] = 0x00FF;
752                 /* Support all HT-MCSs rate */
753                 for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates) - 3; i++)
754                         bitmap_rates[i + 2] = 0xFFFF;
755                 bitmap_rates[9] = 0x3FFF;
756         } else {
757                 memset(rates, 0, sizeof(rates));
758                 mwifiex_get_active_data_rates(priv, rates);
759                 rate = rates;
760                 for (i = 0; (rate[i] && i < MWIFIEX_SUPPORTED_RATES); i++) {
761                         dev_dbg(adapter->dev, "info: rate=%#x wanted=%#x\n",
762                                 rate[i], rate_cfg->rate);
763                         if ((rate[i] & 0x7f) == (rate_cfg->rate & 0x7f))
764                                 break;
765                 }
766                 if ((i == MWIFIEX_SUPPORTED_RATES) || !rate[i]) {
767                         dev_err(adapter->dev, "fixed data rate %#x is out "
768                                "of range\n", rate_cfg->rate);
769                         return -1;
770                 }
771                 memset(bitmap_rates, 0, sizeof(bitmap_rates));
772
773                 rate_index = mwifiex_data_rate_to_index(rate_cfg->rate);
774
775                 /* Only allow b/g rates to be set */
776                 if (rate_index >= MWIFIEX_RATE_INDEX_HRDSSS0 &&
777                     rate_index <= MWIFIEX_RATE_INDEX_HRDSSS3) {
778                         bitmap_rates[0] = 1 << rate_index;
779                 } else {
780                         rate_index -= 1; /* There is a 0x00 in the table */
781                         if (rate_index >= MWIFIEX_RATE_INDEX_OFDM0 &&
782                             rate_index <= MWIFIEX_RATE_INDEX_OFDM7)
783                                 bitmap_rates[1] = 1 << (rate_index -
784                                                    MWIFIEX_RATE_INDEX_OFDM0);
785                 }
786         }
787
788         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_TX_RATE_CFG,
789                                     HostCmd_ACT_GEN_SET, 0, bitmap_rates);
790
791         return ret;
792 }
793
794 /*
795  * IOCTL request handler to set/get rate.
796  *
797  * This function can be used to set/get either the rate value or the
798  * rate index.
799  */
800 static int mwifiex_rate_ioctl_cfg(struct mwifiex_private *priv,
801                                   struct mwifiex_rate_cfg *rate_cfg)
802 {
803         int status;
804
805         if (!rate_cfg)
806                 return -1;
807
808         if (rate_cfg->action == HostCmd_ACT_GEN_GET)
809                 status = mwifiex_rate_ioctl_get_rate_value(priv, rate_cfg);
810         else
811                 status = mwifiex_rate_ioctl_set_rate_value(priv, rate_cfg);
812
813         return status;
814 }
815
816 /*
817  * Sends IOCTL request to get the data rate.
818  *
819  * This function allocates the IOCTL request buffer, fills it
820  * with requisite parameters and calls the IOCTL handler.
821  */
822 int mwifiex_drv_get_data_rate(struct mwifiex_private *priv,
823                               struct mwifiex_rate_cfg *rate)
824 {
825         int ret;
826
827         memset(rate, 0, sizeof(struct mwifiex_rate_cfg));
828         rate->action = HostCmd_ACT_GEN_GET;
829         ret = mwifiex_rate_ioctl_cfg(priv, rate);
830
831         if (!ret) {
832                 if (rate->is_rate_auto)
833                         rate->rate = mwifiex_index_to_data_rate(priv,
834                                                                 priv->tx_rate,
835                                                                 priv->tx_htinfo
836                                                                 );
837                 else
838                         rate->rate = priv->data_rate;
839         } else {
840                 ret = -1;
841         }
842
843         return ret;
844 }
845
846 /*
847  * IOCTL request handler to set tx power configuration.
848  *
849  * This function prepares the correct firmware command and
850  * issues it.
851  *
852  * For non-auto power mode, all the following power groups are set -
853  *      - Modulation class HR/DSSS
854  *      - Modulation class OFDM
855  *      - Modulation class HTBW20
856  *      - Modulation class HTBW40
857  */
858 int mwifiex_set_tx_power(struct mwifiex_private *priv,
859                          struct mwifiex_power_cfg *power_cfg)
860 {
861         int ret;
862         struct host_cmd_ds_txpwr_cfg *txp_cfg;
863         struct mwifiex_types_power_group *pg_tlv;
864         struct mwifiex_power_group *pg;
865         u8 *buf;
866         u16 dbm = 0;
867
868         if (!power_cfg->is_power_auto) {
869                 dbm = (u16) power_cfg->power_level;
870                 if ((dbm < priv->min_tx_power_level) ||
871                     (dbm > priv->max_tx_power_level)) {
872                         dev_err(priv->adapter->dev, "txpower value %d dBm"
873                                 " is out of range (%d dBm-%d dBm)\n",
874                                 dbm, priv->min_tx_power_level,
875                                 priv->max_tx_power_level);
876                         return -1;
877                 }
878         }
879         buf = kzalloc(MWIFIEX_SIZE_OF_CMD_BUFFER, GFP_KERNEL);
880         if (!buf) {
881                 dev_err(priv->adapter->dev, "%s: failed to alloc cmd buffer\n",
882                         __func__);
883                 return -ENOMEM;
884         }
885
886         txp_cfg = (struct host_cmd_ds_txpwr_cfg *) buf;
887         txp_cfg->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
888         if (!power_cfg->is_power_auto) {
889                 txp_cfg->mode = cpu_to_le32(1);
890                 pg_tlv = (struct mwifiex_types_power_group *)
891                          (buf + sizeof(struct host_cmd_ds_txpwr_cfg));
892                 pg_tlv->type = TLV_TYPE_POWER_GROUP;
893                 pg_tlv->length = 4 * sizeof(struct mwifiex_power_group);
894                 pg = (struct mwifiex_power_group *)
895                      (buf + sizeof(struct host_cmd_ds_txpwr_cfg)
896                       + sizeof(struct mwifiex_types_power_group));
897                 /* Power group for modulation class HR/DSSS */
898                 pg->first_rate_code = 0x00;
899                 pg->last_rate_code = 0x03;
900                 pg->modulation_class = MOD_CLASS_HR_DSSS;
901                 pg->power_step = 0;
902                 pg->power_min = (s8) dbm;
903                 pg->power_max = (s8) dbm;
904                 pg++;
905                 /* Power group for modulation class OFDM */
906                 pg->first_rate_code = 0x00;
907                 pg->last_rate_code = 0x07;
908                 pg->modulation_class = MOD_CLASS_OFDM;
909                 pg->power_step = 0;
910                 pg->power_min = (s8) dbm;
911                 pg->power_max = (s8) dbm;
912                 pg++;
913                 /* Power group for modulation class HTBW20 */
914                 pg->first_rate_code = 0x00;
915                 pg->last_rate_code = 0x20;
916                 pg->modulation_class = MOD_CLASS_HT;
917                 pg->power_step = 0;
918                 pg->power_min = (s8) dbm;
919                 pg->power_max = (s8) dbm;
920                 pg->ht_bandwidth = HT_BW_20;
921                 pg++;
922                 /* Power group for modulation class HTBW40 */
923                 pg->first_rate_code = 0x00;
924                 pg->last_rate_code = 0x20;
925                 pg->modulation_class = MOD_CLASS_HT;
926                 pg->power_step = 0;
927                 pg->power_min = (s8) dbm;
928                 pg->power_max = (s8) dbm;
929                 pg->ht_bandwidth = HT_BW_40;
930         }
931         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_TXPWR_CFG,
932                                     HostCmd_ACT_GEN_SET, 0, buf);
933
934         kfree(buf);
935         return ret;
936 }
937
938 /*
939  * IOCTL request handler to get power save mode.
940  *
941  * This function prepares the correct firmware command and
942  * issues it.
943  */
944 int mwifiex_drv_set_power(struct mwifiex_private *priv, u32 *ps_mode)
945 {
946         int ret;
947         struct mwifiex_adapter *adapter = priv->adapter;
948         u16 sub_cmd;
949
950         if (*ps_mode)
951                 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
952         else
953                 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
954         sub_cmd = (*ps_mode) ? EN_AUTO_PS : DIS_AUTO_PS;
955         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
956                                     sub_cmd, BITMAP_STA_PS, NULL);
957         if ((!ret) && (sub_cmd == DIS_AUTO_PS))
958                 ret = mwifiex_send_cmd_async(priv,
959                                              HostCmd_CMD_802_11_PS_MODE_ENH,
960                                              GET_PS, 0, NULL);
961
962         return ret;
963 }
964
965 /*
966  * IOCTL request handler to set/reset WPA IE.
967  *
968  * The supplied WPA IE is treated as a opaque buffer. Only the first field
969  * is checked to determine WPA version. If buffer length is zero, the existing
970  * WPA IE is reset.
971  */
972 static int mwifiex_set_wpa_ie_helper(struct mwifiex_private *priv,
973                                      u8 *ie_data_ptr, u16 ie_len)
974 {
975         if (ie_len) {
976                 if (ie_len > sizeof(priv->wpa_ie)) {
977                         dev_err(priv->adapter->dev,
978                                 "failed to copy WPA IE, too big\n");
979                         return -1;
980                 }
981                 memcpy(priv->wpa_ie, ie_data_ptr, ie_len);
982                 priv->wpa_ie_len = (u8) ie_len;
983                 dev_dbg(priv->adapter->dev, "cmd: Set Wpa_ie_len=%d IE=%#x\n",
984                         priv->wpa_ie_len, priv->wpa_ie[0]);
985
986                 if (priv->wpa_ie[0] == WLAN_EID_WPA) {
987                         priv->sec_info.wpa_enabled = true;
988                 } else if (priv->wpa_ie[0] == WLAN_EID_RSN) {
989                         priv->sec_info.wpa2_enabled = true;
990                 } else {
991                         priv->sec_info.wpa_enabled = false;
992                         priv->sec_info.wpa2_enabled = false;
993                 }
994         } else {
995                 memset(priv->wpa_ie, 0, sizeof(priv->wpa_ie));
996                 priv->wpa_ie_len = 0;
997                 dev_dbg(priv->adapter->dev, "info: reset wpa_ie_len=%d IE=%#x\n",
998                         priv->wpa_ie_len, priv->wpa_ie[0]);
999                 priv->sec_info.wpa_enabled = false;
1000                 priv->sec_info.wpa2_enabled = false;
1001         }
1002
1003         return 0;
1004 }
1005
1006 /*
1007  * IOCTL request handler to set/reset WAPI IE.
1008  *
1009  * The supplied WAPI IE is treated as a opaque buffer. Only the first field
1010  * is checked to internally enable WAPI. If buffer length is zero, the existing
1011  * WAPI IE is reset.
1012  */
1013 static int mwifiex_set_wapi_ie(struct mwifiex_private *priv,
1014                                u8 *ie_data_ptr, u16 ie_len)
1015 {
1016         if (ie_len) {
1017                 if (ie_len > sizeof(priv->wapi_ie)) {
1018                         dev_dbg(priv->adapter->dev,
1019                                 "info: failed to copy WAPI IE, too big\n");
1020                         return -1;
1021                 }
1022                 memcpy(priv->wapi_ie, ie_data_ptr, ie_len);
1023                 priv->wapi_ie_len = ie_len;
1024                 dev_dbg(priv->adapter->dev, "cmd: Set wapi_ie_len=%d IE=%#x\n",
1025                         priv->wapi_ie_len, priv->wapi_ie[0]);
1026
1027                 if (priv->wapi_ie[0] == WLAN_EID_BSS_AC_ACCESS_DELAY)
1028                         priv->sec_info.wapi_enabled = true;
1029         } else {
1030                 memset(priv->wapi_ie, 0, sizeof(priv->wapi_ie));
1031                 priv->wapi_ie_len = ie_len;
1032                 dev_dbg(priv->adapter->dev,
1033                         "info: Reset wapi_ie_len=%d IE=%#x\n",
1034                        priv->wapi_ie_len, priv->wapi_ie[0]);
1035                 priv->sec_info.wapi_enabled = false;
1036         }
1037         return 0;
1038 }
1039
1040 /*
1041  * IOCTL request handler to set/reset WPS IE.
1042  *
1043  * The supplied WPS IE is treated as a opaque buffer. Only the first field
1044  * is checked to internally enable WPS. If buffer length is zero, the existing
1045  * WPS IE is reset.
1046  */
1047 static int mwifiex_set_wps_ie(struct mwifiex_private *priv,
1048                                u8 *ie_data_ptr, u16 ie_len)
1049 {
1050         if (ie_len) {
1051                 priv->wps_ie = kzalloc(MWIFIEX_MAX_VSIE_LEN, GFP_KERNEL);
1052                 if (!priv->wps_ie)
1053                         return -ENOMEM;
1054                 if (ie_len > sizeof(priv->wps_ie)) {
1055                         dev_dbg(priv->adapter->dev,
1056                                 "info: failed to copy WPS IE, too big\n");
1057                         kfree(priv->wps_ie);
1058                         return -1;
1059                 }
1060                 memcpy(priv->wps_ie, ie_data_ptr, ie_len);
1061                 priv->wps_ie_len = ie_len;
1062                 dev_dbg(priv->adapter->dev, "cmd: Set wps_ie_len=%d IE=%#x\n",
1063                         priv->wps_ie_len, priv->wps_ie[0]);
1064         } else {
1065                 kfree(priv->wps_ie);
1066                 priv->wps_ie_len = ie_len;
1067                 dev_dbg(priv->adapter->dev,
1068                         "info: Reset wps_ie_len=%d\n", priv->wps_ie_len);
1069         }
1070         return 0;
1071 }
1072
1073 /*
1074  * IOCTL request handler to set WAPI key.
1075  *
1076  * This function prepares the correct firmware command and
1077  * issues it.
1078  */
1079 static int mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private *priv,
1080                                struct mwifiex_ds_encrypt_key *encrypt_key)
1081 {
1082
1083         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1084                                      HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1085                                      encrypt_key);
1086 }
1087
1088 /*
1089  * IOCTL request handler to set WEP network key.
1090  *
1091  * This function prepares the correct firmware command and
1092  * issues it, after validation checks.
1093  */
1094 static int mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private *priv,
1095                               struct mwifiex_ds_encrypt_key *encrypt_key)
1096 {
1097         int ret;
1098         struct mwifiex_wep_key *wep_key;
1099         int index;
1100
1101         if (priv->wep_key_curr_index >= NUM_WEP_KEYS)
1102                 priv->wep_key_curr_index = 0;
1103         wep_key = &priv->wep_key[priv->wep_key_curr_index];
1104         index = encrypt_key->key_index;
1105         if (encrypt_key->key_disable) {
1106                 priv->sec_info.wep_enabled = 0;
1107         } else if (!encrypt_key->key_len) {
1108                 /* Copy the required key as the current key */
1109                 wep_key = &priv->wep_key[index];
1110                 if (!wep_key->key_length) {
1111                         dev_err(priv->adapter->dev,
1112                                 "key not set, so cannot enable it\n");
1113                         return -1;
1114                 }
1115                 priv->wep_key_curr_index = (u16) index;
1116                 priv->sec_info.wep_enabled = 1;
1117         } else {
1118                 wep_key = &priv->wep_key[index];
1119                 memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
1120                 /* Copy the key in the driver */
1121                 memcpy(wep_key->key_material,
1122                        encrypt_key->key_material,
1123                        encrypt_key->key_len);
1124                 wep_key->key_index = index;
1125                 wep_key->key_length = encrypt_key->key_len;
1126                 priv->sec_info.wep_enabled = 1;
1127         }
1128         if (wep_key->key_length) {
1129                 /* Send request to firmware */
1130                 ret = mwifiex_send_cmd_async(priv,
1131                                              HostCmd_CMD_802_11_KEY_MATERIAL,
1132                                              HostCmd_ACT_GEN_SET, 0, NULL);
1133                 if (ret)
1134                         return ret;
1135         }
1136         if (priv->sec_info.wep_enabled)
1137                 priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
1138         else
1139                 priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
1140
1141         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_MAC_CONTROL,
1142                                     HostCmd_ACT_GEN_SET, 0,
1143                                     &priv->curr_pkt_filter);
1144
1145         return ret;
1146 }
1147
1148 /*
1149  * IOCTL request handler to set WPA key.
1150  *
1151  * This function prepares the correct firmware command and
1152  * issues it, after validation checks.
1153  *
1154  * Current driver only supports key length of up to 32 bytes.
1155  *
1156  * This function can also be used to disable a currently set key.
1157  */
1158 static int mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private *priv,
1159                               struct mwifiex_ds_encrypt_key *encrypt_key)
1160 {
1161         int ret;
1162         u8 remove_key = false;
1163         struct host_cmd_ds_802_11_key_material *ibss_key;
1164
1165         /* Current driver only supports key length of up to 32 bytes */
1166         if (encrypt_key->key_len > WLAN_MAX_KEY_LEN) {
1167                 dev_err(priv->adapter->dev, "key length too long\n");
1168                 return -1;
1169         }
1170
1171         if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
1172                 /*
1173                  * IBSS/WPA-None uses only one key (Group) for both receiving
1174                  * and sending unicast and multicast packets.
1175                  */
1176                 /* Send the key as PTK to firmware */
1177                 encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1178                 ret = mwifiex_send_cmd_async(priv,
1179                                              HostCmd_CMD_802_11_KEY_MATERIAL,
1180                                              HostCmd_ACT_GEN_SET,
1181                                              KEY_INFO_ENABLED, encrypt_key);
1182                 if (ret)
1183                         return ret;
1184
1185                 ibss_key = &priv->aes_key;
1186                 memset(ibss_key, 0,
1187                        sizeof(struct host_cmd_ds_802_11_key_material));
1188                 /* Copy the key in the driver */
1189                 memcpy(ibss_key->key_param_set.key, encrypt_key->key_material,
1190                        encrypt_key->key_len);
1191                 memcpy(&ibss_key->key_param_set.key_len, &encrypt_key->key_len,
1192                        sizeof(ibss_key->key_param_set.key_len));
1193                 ibss_key->key_param_set.key_type_id
1194                         = cpu_to_le16(KEY_TYPE_ID_TKIP);
1195                 ibss_key->key_param_set.key_info = cpu_to_le16(KEY_ENABLED);
1196
1197                 /* Send the key as GTK to firmware */
1198                 encrypt_key->key_index = ~MWIFIEX_KEY_INDEX_UNICAST;
1199         }
1200
1201         if (!encrypt_key->key_index)
1202                 encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1203
1204         if (remove_key)
1205                 ret = mwifiex_send_cmd_sync(priv,
1206                                             HostCmd_CMD_802_11_KEY_MATERIAL,
1207                                             HostCmd_ACT_GEN_SET,
1208                                             !KEY_INFO_ENABLED, encrypt_key);
1209         else
1210                 ret = mwifiex_send_cmd_sync(priv,
1211                                             HostCmd_CMD_802_11_KEY_MATERIAL,
1212                                             HostCmd_ACT_GEN_SET,
1213                                             KEY_INFO_ENABLED, encrypt_key);
1214
1215         return ret;
1216 }
1217
1218 /*
1219  * IOCTL request handler to set/get network keys.
1220  *
1221  * This is a generic key handling function which supports WEP, WPA
1222  * and WAPI.
1223  */
1224 static int
1225 mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private *priv,
1226                               struct mwifiex_ds_encrypt_key *encrypt_key)
1227 {
1228         int status;
1229
1230         if (encrypt_key->is_wapi_key)
1231                 status = mwifiex_sec_ioctl_set_wapi_key(priv, encrypt_key);
1232         else if (encrypt_key->key_len > WLAN_KEY_LEN_WEP104)
1233                 status = mwifiex_sec_ioctl_set_wpa_key(priv, encrypt_key);
1234         else
1235                 status = mwifiex_sec_ioctl_set_wep_key(priv, encrypt_key);
1236         return status;
1237 }
1238
1239 /*
1240  * This function returns the driver version.
1241  */
1242 int
1243 mwifiex_drv_get_driver_version(struct mwifiex_adapter *adapter, char *version,
1244                                int max_len)
1245 {
1246         union {
1247                 u32 l;
1248                 u8 c[4];
1249         } ver;
1250         char fw_ver[32];
1251
1252         ver.l = adapter->fw_release_number;
1253         sprintf(fw_ver, "%u.%u.%u.p%u", ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
1254
1255         snprintf(version, max_len, driver_version, fw_ver);
1256
1257         dev_dbg(adapter->dev, "info: MWIFIEX VERSION: %s\n", version);
1258
1259         return 0;
1260 }
1261
1262 /*
1263  * Sends IOCTL request to set encoding parameters.
1264  *
1265  * This function allocates the IOCTL request buffer, fills it
1266  * with requisite parameters and calls the IOCTL handler.
1267  */
1268 int mwifiex_set_encode(struct mwifiex_private *priv, const u8 *key,
1269                         int key_len, u8 key_index, int disable)
1270 {
1271         struct mwifiex_ds_encrypt_key encrypt_key;
1272
1273         memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
1274         encrypt_key.key_len = key_len;
1275         if (!disable) {
1276                 encrypt_key.key_index = key_index;
1277                 if (key_len)
1278                         memcpy(encrypt_key.key_material, key, key_len);
1279         } else {
1280                 encrypt_key.key_disable = true;
1281         }
1282
1283         return mwifiex_sec_ioctl_encrypt_key(priv, &encrypt_key);
1284 }
1285
1286 /*
1287  * Sends IOCTL request to get extended version.
1288  *
1289  * This function allocates the IOCTL request buffer, fills it
1290  * with requisite parameters and calls the IOCTL handler.
1291  */
1292 int
1293 mwifiex_get_ver_ext(struct mwifiex_private *priv)
1294 {
1295         struct mwifiex_ver_ext ver_ext;
1296
1297         memset(&ver_ext, 0, sizeof(struct host_cmd_ds_version_ext));
1298         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_VERSION_EXT,
1299                                   HostCmd_ACT_GEN_GET, 0, &ver_ext))
1300                 return -1;
1301
1302         return 0;
1303 }
1304
1305 /*
1306  * Sends IOCTL request to get statistics information.
1307  *
1308  * This function allocates the IOCTL request buffer, fills it
1309  * with requisite parameters and calls the IOCTL handler.
1310  */
1311 int
1312 mwifiex_get_stats_info(struct mwifiex_private *priv,
1313                        struct mwifiex_ds_get_stats *log)
1314 {
1315         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_GET_LOG,
1316                                      HostCmd_ACT_GEN_GET, 0, log);
1317 }
1318
1319 /*
1320  * IOCTL request handler to read/write register.
1321  *
1322  * This function prepares the correct firmware command and
1323  * issues it.
1324  *
1325  * Access to the following registers are supported -
1326  *      - MAC
1327  *      - BBP
1328  *      - RF
1329  *      - PMIC
1330  *      - CAU
1331  */
1332 static int mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private *priv,
1333                                         struct mwifiex_ds_reg_rw *reg_rw,
1334                                         u16 action)
1335 {
1336         u16 cmd_no;
1337
1338         switch (le32_to_cpu(reg_rw->type)) {
1339         case MWIFIEX_REG_MAC:
1340                 cmd_no = HostCmd_CMD_MAC_REG_ACCESS;
1341                 break;
1342         case MWIFIEX_REG_BBP:
1343                 cmd_no = HostCmd_CMD_BBP_REG_ACCESS;
1344                 break;
1345         case MWIFIEX_REG_RF:
1346                 cmd_no = HostCmd_CMD_RF_REG_ACCESS;
1347                 break;
1348         case MWIFIEX_REG_PMIC:
1349                 cmd_no = HostCmd_CMD_PMIC_REG_ACCESS;
1350                 break;
1351         case MWIFIEX_REG_CAU:
1352                 cmd_no = HostCmd_CMD_CAU_REG_ACCESS;
1353                 break;
1354         default:
1355                 return -1;
1356         }
1357
1358         return mwifiex_send_cmd_sync(priv, cmd_no, action, 0, reg_rw);
1359
1360 }
1361
1362 /*
1363  * Sends IOCTL request to write to a register.
1364  *
1365  * This function allocates the IOCTL request buffer, fills it
1366  * with requisite parameters and calls the IOCTL handler.
1367  */
1368 int
1369 mwifiex_reg_write(struct mwifiex_private *priv, u32 reg_type,
1370                   u32 reg_offset, u32 reg_value)
1371 {
1372         struct mwifiex_ds_reg_rw reg_rw;
1373
1374         reg_rw.type = cpu_to_le32(reg_type);
1375         reg_rw.offset = cpu_to_le32(reg_offset);
1376         reg_rw.value = cpu_to_le32(reg_value);
1377
1378         return mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_SET);
1379 }
1380
1381 /*
1382  * Sends IOCTL request to read from a register.
1383  *
1384  * This function allocates the IOCTL request buffer, fills it
1385  * with requisite parameters and calls the IOCTL handler.
1386  */
1387 int
1388 mwifiex_reg_read(struct mwifiex_private *priv, u32 reg_type,
1389                  u32 reg_offset, u32 *value)
1390 {
1391         int ret;
1392         struct mwifiex_ds_reg_rw reg_rw;
1393
1394         reg_rw.type = cpu_to_le32(reg_type);
1395         reg_rw.offset = cpu_to_le32(reg_offset);
1396         ret = mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_GET);
1397
1398         if (ret)
1399                 goto done;
1400
1401         *value = le32_to_cpu(reg_rw.value);
1402
1403 done:
1404         return ret;
1405 }
1406
1407 /*
1408  * Sends IOCTL request to read from EEPROM.
1409  *
1410  * This function allocates the IOCTL request buffer, fills it
1411  * with requisite parameters and calls the IOCTL handler.
1412  */
1413 int
1414 mwifiex_eeprom_read(struct mwifiex_private *priv, u16 offset, u16 bytes,
1415                     u8 *value)
1416 {
1417         int ret;
1418         struct mwifiex_ds_read_eeprom rd_eeprom;
1419
1420         rd_eeprom.offset = cpu_to_le16((u16) offset);
1421         rd_eeprom.byte_count = cpu_to_le16((u16) bytes);
1422
1423         /* Send request to firmware */
1424         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_EEPROM_ACCESS,
1425                                     HostCmd_ACT_GEN_GET, 0, &rd_eeprom);
1426
1427         if (!ret)
1428                 memcpy(value, rd_eeprom.value, MAX_EEPROM_DATA);
1429         return ret;
1430 }
1431
1432 /*
1433  * This function sets a generic IE. In addition to generic IE, it can
1434  * also handle WPA, WPA2 and WAPI IEs.
1435  */
1436 static int
1437 mwifiex_set_gen_ie_helper(struct mwifiex_private *priv, u8 *ie_data_ptr,
1438                           u16 ie_len)
1439 {
1440         int ret = 0;
1441         struct ieee_types_vendor_header *pvendor_ie;
1442         const u8 wpa_oui[] = { 0x00, 0x50, 0xf2, 0x01 };
1443         const u8 wps_oui[] = { 0x00, 0x50, 0xf2, 0x04 };
1444
1445         /* If the passed length is zero, reset the buffer */
1446         if (!ie_len) {
1447                 priv->gen_ie_buf_len = 0;
1448                 priv->wps.session_enable = false;
1449
1450                 return 0;
1451         } else if (!ie_data_ptr) {
1452                 return -1;
1453         }
1454         pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1455         /* Test to see if it is a WPA IE, if not, then it is a gen IE */
1456         if (((pvendor_ie->element_id == WLAN_EID_WPA) &&
1457              (!memcmp(pvendor_ie->oui, wpa_oui, sizeof(wpa_oui)))) ||
1458             (pvendor_ie->element_id == WLAN_EID_RSN)) {
1459
1460                 /* IE is a WPA/WPA2 IE so call set_wpa function */
1461                 ret = mwifiex_set_wpa_ie_helper(priv, ie_data_ptr, ie_len);
1462                 priv->wps.session_enable = false;
1463
1464                 return ret;
1465         } else if (pvendor_ie->element_id == WLAN_EID_BSS_AC_ACCESS_DELAY) {
1466                 /* IE is a WAPI IE so call set_wapi function */
1467                 ret = mwifiex_set_wapi_ie(priv, ie_data_ptr, ie_len);
1468
1469                 return ret;
1470         }
1471         /*
1472          * Verify that the passed length is not larger than the
1473          * available space remaining in the buffer
1474          */
1475         if (ie_len < (sizeof(priv->gen_ie_buf) - priv->gen_ie_buf_len)) {
1476
1477                 /* Test to see if it is a WPS IE, if so, enable
1478                  * wps session flag
1479                  */
1480                 pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1481                 if ((pvendor_ie->element_id == WLAN_EID_VENDOR_SPECIFIC) &&
1482                     (!memcmp(pvendor_ie->oui, wps_oui, sizeof(wps_oui)))) {
1483                         priv->wps.session_enable = true;
1484                         dev_dbg(priv->adapter->dev,
1485                                 "info: WPS Session Enabled.\n");
1486                         ret = mwifiex_set_wps_ie(priv, ie_data_ptr, ie_len);
1487                 }
1488
1489                 /* Append the passed data to the end of the
1490                    genIeBuffer */
1491                 memcpy(priv->gen_ie_buf + priv->gen_ie_buf_len, ie_data_ptr,
1492                        ie_len);
1493                 /* Increment the stored buffer length by the
1494                    size passed */
1495                 priv->gen_ie_buf_len += ie_len;
1496         } else {
1497                 /* Passed data does not fit in the remaining
1498                    buffer space */
1499                 ret = -1;
1500         }
1501
1502         /* Return 0, or -1 for error case */
1503         return ret;
1504 }
1505
1506 /*
1507  * IOCTL request handler to set/get generic IE.
1508  *
1509  * In addition to various generic IEs, this function can also be
1510  * used to set the ARP filter.
1511  */
1512 static int mwifiex_misc_ioctl_gen_ie(struct mwifiex_private *priv,
1513                                      struct mwifiex_ds_misc_gen_ie *gen_ie,
1514                                      u16 action)
1515 {
1516         struct mwifiex_adapter *adapter = priv->adapter;
1517
1518         switch (gen_ie->type) {
1519         case MWIFIEX_IE_TYPE_GEN_IE:
1520                 if (action == HostCmd_ACT_GEN_GET) {
1521                         gen_ie->len = priv->wpa_ie_len;
1522                         memcpy(gen_ie->ie_data, priv->wpa_ie, gen_ie->len);
1523                 } else {
1524                         mwifiex_set_gen_ie_helper(priv, gen_ie->ie_data,
1525                                                   (u16) gen_ie->len);
1526                 }
1527                 break;
1528         case MWIFIEX_IE_TYPE_ARP_FILTER:
1529                 memset(adapter->arp_filter, 0, sizeof(adapter->arp_filter));
1530                 if (gen_ie->len > ARP_FILTER_MAX_BUF_SIZE) {
1531                         adapter->arp_filter_size = 0;
1532                         dev_err(adapter->dev, "invalid ARP filter size\n");
1533                         return -1;
1534                 } else {
1535                         memcpy(adapter->arp_filter, gen_ie->ie_data,
1536                                gen_ie->len);
1537                         adapter->arp_filter_size = gen_ie->len;
1538                 }
1539                 break;
1540         default:
1541                 dev_err(adapter->dev, "invalid IE type\n");
1542                 return -1;
1543         }
1544         return 0;
1545 }
1546
1547 /*
1548  * Sends IOCTL request to set a generic IE.
1549  *
1550  * This function allocates the IOCTL request buffer, fills it
1551  * with requisite parameters and calls the IOCTL handler.
1552  */
1553 int
1554 mwifiex_set_gen_ie(struct mwifiex_private *priv, u8 *ie, int ie_len)
1555 {
1556         struct mwifiex_ds_misc_gen_ie gen_ie;
1557
1558         if (ie_len > IEEE_MAX_IE_SIZE)
1559                 return -EFAULT;
1560
1561         gen_ie.type = MWIFIEX_IE_TYPE_GEN_IE;
1562         gen_ie.len = ie_len;
1563         memcpy(gen_ie.ie_data, ie, ie_len);
1564         if (mwifiex_misc_ioctl_gen_ie(priv, &gen_ie, HostCmd_ACT_GEN_SET))
1565                 return -EFAULT;
1566
1567         return 0;
1568 }