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