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