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