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