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