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