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