OSDN Git Service

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