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