ath6kl: Remove useless initialization in ath6kl_read_fwlogs()
[cascardo/linux.git] / drivers / net / wireless / mwifiex / sta_ioctl.c
1 /*
2  * Marvell Wireless LAN device driver: functions for station ioctl
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27 #include "cfg80211.h"
28
29 /*
30  * Copies the multicast address list from device to driver.
31  *
32  * This function does not validate the destination memory for
33  * size, and the calling function must ensure enough memory is
34  * available.
35  */
36 int mwifiex_copy_mcast_addr(struct mwifiex_multicast_list *mlist,
37                             struct net_device *dev)
38 {
39         int i = 0;
40         struct netdev_hw_addr *ha;
41
42         netdev_for_each_mc_addr(ha, dev)
43                 memcpy(&mlist->mac_list[i++], ha->addr, ETH_ALEN);
44
45         return i;
46 }
47
48 /*
49  * Wait queue completion handler.
50  *
51  * This function waits on a cmd wait queue. It also cancels the pending
52  * request after waking up, in case of errors.
53  */
54 int mwifiex_wait_queue_complete(struct mwifiex_adapter *adapter)
55 {
56         bool cancel_flag = false;
57         int status = adapter->cmd_wait_q.status;
58         struct cmd_ctrl_node *cmd_queued = adapter->cmd_queued;
59
60         adapter->cmd_queued = NULL;
61         dev_dbg(adapter->dev, "cmd pending\n");
62         atomic_inc(&adapter->cmd_pending);
63
64         /* Status pending, wake up main process */
65         queue_work(adapter->workqueue, &adapter->main_work);
66
67         /* Wait for completion */
68         wait_event_interruptible(adapter->cmd_wait_q.wait,
69                                         *(cmd_queued->condition));
70         if (!*(cmd_queued->condition))
71                 cancel_flag = true;
72
73         if (cancel_flag) {
74                 mwifiex_cancel_pending_ioctl(adapter);
75                 dev_dbg(adapter->dev, "cmd cancel\n");
76         }
77         adapter->cmd_wait_q.status = 0;
78
79         return status;
80 }
81
82 /*
83  * This function prepares the correct firmware command and
84  * issues it to set the multicast list.
85  *
86  * This function can be used to enable promiscuous mode, or enable all
87  * multicast packets, or to enable selective multicast.
88  */
89 int mwifiex_request_set_multicast_list(struct mwifiex_private *priv,
90                                 struct mwifiex_multicast_list *mcast_list)
91 {
92         int ret = 0;
93         u16 old_pkt_filter;
94
95         old_pkt_filter = priv->curr_pkt_filter;
96
97         if (mcast_list->mode == MWIFIEX_PROMISC_MODE) {
98                 dev_dbg(priv->adapter->dev, "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_MULTICAST_MODE) {
106                         dev_dbg(priv->adapter->dev,
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                         if (mcast_list->num_multicast_addr) {
114                                 dev_dbg(priv->adapter->dev,
115                                         "info: Set multicast list=%d\n",
116                                        mcast_list->num_multicast_addr);
117                                 /* Set multicast addresses to firmware */
118                                 if (old_pkt_filter == priv->curr_pkt_filter) {
119                                         /* Send request to firmware */
120                                         ret = mwifiex_send_cmd_async(priv,
121                                                 HostCmd_CMD_MAC_MULTICAST_ADR,
122                                                 HostCmd_ACT_GEN_SET, 0,
123                                                 mcast_list);
124                                 } else {
125                                         /* Send request to firmware */
126                                         ret = mwifiex_send_cmd_async(priv,
127                                                 HostCmd_CMD_MAC_MULTICAST_ADR,
128                                                 HostCmd_ACT_GEN_SET, 0,
129                                                 mcast_list);
130                                 }
131                         }
132                 }
133         }
134         dev_dbg(priv->adapter->dev,
135                 "info: old_pkt_filter=%#x, curr_pkt_filter=%#x\n",
136                old_pkt_filter, priv->curr_pkt_filter);
137         if (old_pkt_filter != priv->curr_pkt_filter) {
138                 ret = mwifiex_send_cmd_async(priv, HostCmd_CMD_MAC_CONTROL,
139                                              HostCmd_ACT_GEN_SET,
140                                              0, &priv->curr_pkt_filter);
141         }
142
143         return ret;
144 }
145
146 /*
147  * This function fills bss descriptor structure using provided
148  * information.
149  */
150 int mwifiex_fill_new_bss_desc(struct mwifiex_private *priv,
151                               u8 *bssid, s32 rssi, u8 *ie_buf,
152                               size_t ie_len, u16 beacon_period,
153                               u16 cap_info_bitmap, u8 band,
154                               struct mwifiex_bssdescriptor *bss_desc)
155 {
156         int ret;
157
158         memcpy(bss_desc->mac_address, bssid, ETH_ALEN);
159         bss_desc->rssi = rssi;
160         bss_desc->beacon_buf = ie_buf;
161         bss_desc->beacon_buf_size = ie_len;
162         bss_desc->beacon_period = beacon_period;
163         bss_desc->cap_info_bitmap = cap_info_bitmap;
164         bss_desc->bss_band = band;
165         if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_PRIVACY) {
166                 dev_dbg(priv->adapter->dev, "info: InterpretIE: AP WEP enabled\n");
167                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
168         } else {
169                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
170         }
171         if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_IBSS)
172                 bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
173         else
174                 bss_desc->bss_mode = NL80211_IFTYPE_STATION;
175
176         ret = mwifiex_update_bss_desc_with_ie(priv->adapter, bss_desc,
177                                               ie_buf, ie_len);
178
179         return ret;
180 }
181
182 /*
183  * In Ad-Hoc mode, the IBSS is created if not found in scan list.
184  * In both Ad-Hoc and infra mode, an deauthentication is performed
185  * first.
186  */
187 int mwifiex_bss_start(struct mwifiex_private *priv, struct cfg80211_bss *bss,
188                       struct mwifiex_802_11_ssid *req_ssid)
189 {
190         int ret;
191         struct mwifiex_adapter *adapter = priv->adapter;
192         struct mwifiex_bssdescriptor *bss_desc = NULL;
193         u8 *beacon_ie = NULL;
194
195         priv->scan_block = false;
196
197         if (bss) {
198                 /* Allocate and fill new bss descriptor */
199                 bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor),
200                                 GFP_KERNEL);
201                 if (!bss_desc) {
202                         dev_err(priv->adapter->dev, " failed to alloc bss_desc\n");
203                         return -ENOMEM;
204                 }
205
206                 beacon_ie = kmemdup(bss->information_elements,
207                                         bss->len_beacon_ies, GFP_KERNEL);
208                 if (!beacon_ie) {
209                         kfree(bss_desc);
210                         dev_err(priv->adapter->dev, " failed to alloc beacon_ie\n");
211                         return -ENOMEM;
212                 }
213
214                 ret = mwifiex_fill_new_bss_desc(priv, bss->bssid, bss->signal,
215                                                 beacon_ie, bss->len_beacon_ies,
216                                                 bss->beacon_interval,
217                                                 bss->capability,
218                                                 *(u8 *)bss->priv, bss_desc);
219                 if (ret)
220                         goto done;
221         }
222
223         if (priv->bss_mode == NL80211_IFTYPE_STATION) {
224                 /* Infra mode */
225                 ret = mwifiex_deauthenticate(priv, NULL);
226                 if (ret)
227                         goto done;
228
229                 ret = mwifiex_check_network_compatibility(priv, bss_desc);
230                 if (ret)
231                         goto done;
232
233                 dev_dbg(adapter->dev, "info: SSID found in scan list ... "
234                                       "associating...\n");
235
236                 if (!netif_queue_stopped(priv->netdev))
237                         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
238
239                 /* Clear any past association response stored for
240                  * application retrieval */
241                 priv->assoc_rsp_size = 0;
242                 ret = mwifiex_associate(priv, bss_desc);
243                 if (bss)
244                         cfg80211_put_bss(bss);
245         } else {
246                 /* Adhoc mode */
247                 /* If the requested SSID matches current SSID, return */
248                 if (bss_desc && bss_desc->ssid.ssid_len &&
249                     (!mwifiex_ssid_cmp
250                      (&priv->curr_bss_params.bss_descriptor.ssid,
251                       &bss_desc->ssid))) {
252                         kfree(bss_desc);
253                         kfree(beacon_ie);
254                         return 0;
255                 }
256
257                 /* Exit Adhoc mode first */
258                 dev_dbg(adapter->dev, "info: Sending Adhoc Stop\n");
259                 ret = mwifiex_deauthenticate(priv, NULL);
260                 if (ret)
261                         goto done;
262
263                 priv->adhoc_is_link_sensed = false;
264
265                 ret = mwifiex_check_network_compatibility(priv, bss_desc);
266
267                 if (!netif_queue_stopped(priv->netdev))
268                         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
269
270                 if (!ret) {
271                         dev_dbg(adapter->dev, "info: network found in scan"
272                                                         " list. Joining...\n");
273                         ret = mwifiex_adhoc_join(priv, bss_desc);
274                         if (bss)
275                                 cfg80211_put_bss(bss);
276                 } else {
277                         dev_dbg(adapter->dev, "info: Network not found in "
278                                 "the list, creating adhoc with ssid = %s\n",
279                                 req_ssid->ssid);
280                         ret = mwifiex_adhoc_start(priv, req_ssid);
281                 }
282         }
283
284 done:
285         kfree(bss_desc);
286         kfree(beacon_ie);
287         return ret;
288 }
289
290 /*
291  * IOCTL request handler to set host sleep configuration.
292  *
293  * This function prepares the correct firmware command and
294  * issues it.
295  */
296 static int mwifiex_set_hs_params(struct mwifiex_private *priv, u16 action,
297                                  int cmd_type, struct mwifiex_ds_hs_cfg *hs_cfg)
298
299 {
300         struct mwifiex_adapter *adapter = priv->adapter;
301         int status = 0;
302         u32 prev_cond = 0;
303
304         if (!hs_cfg)
305                 return -ENOMEM;
306
307         switch (action) {
308         case HostCmd_ACT_GEN_SET:
309                 if (adapter->pps_uapsd_mode) {
310                         dev_dbg(adapter->dev, "info: Host Sleep IOCTL"
311                                 " is blocked in UAPSD/PPS mode\n");
312                         status = -1;
313                         break;
314                 }
315                 if (hs_cfg->is_invoke_hostcmd) {
316                         if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL) {
317                                 if (!adapter->is_hs_configured)
318                                         /* Already cancelled */
319                                         break;
320                                 /* Save previous condition */
321                                 prev_cond = le32_to_cpu(adapter->hs_cfg
322                                                         .conditions);
323                                 adapter->hs_cfg.conditions =
324                                                 cpu_to_le32(hs_cfg->conditions);
325                         } else if (hs_cfg->conditions) {
326                                 adapter->hs_cfg.conditions =
327                                                 cpu_to_le32(hs_cfg->conditions);
328                                 adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
329                                 if (hs_cfg->gap)
330                                         adapter->hs_cfg.gap = (u8)hs_cfg->gap;
331                         } else if (adapter->hs_cfg.conditions ==
332                                                 cpu_to_le32(
333                                                 HOST_SLEEP_CFG_CANCEL)) {
334                                 /* Return failure if no parameters for HS
335                                    enable */
336                                 status = -1;
337                                 break;
338                         }
339                         if (cmd_type == MWIFIEX_SYNC_CMD)
340                                 status = mwifiex_send_cmd_sync(priv,
341                                                 HostCmd_CMD_802_11_HS_CFG_ENH,
342                                                 HostCmd_ACT_GEN_SET, 0,
343                                                 &adapter->hs_cfg);
344                         else
345                                 status = mwifiex_send_cmd_async(priv,
346                                                 HostCmd_CMD_802_11_HS_CFG_ENH,
347                                                 HostCmd_ACT_GEN_SET, 0,
348                                                 &adapter->hs_cfg);
349                         if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL)
350                                 /* Restore previous condition */
351                                 adapter->hs_cfg.conditions =
352                                                 cpu_to_le32(prev_cond);
353                 } else {
354                         adapter->hs_cfg.conditions =
355                                 cpu_to_le32(hs_cfg->conditions);
356                         adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
357                         adapter->hs_cfg.gap = (u8)hs_cfg->gap;
358                 }
359                 break;
360         case HostCmd_ACT_GEN_GET:
361                 hs_cfg->conditions = le32_to_cpu(adapter->hs_cfg.conditions);
362                 hs_cfg->gpio = adapter->hs_cfg.gpio;
363                 hs_cfg->gap = adapter->hs_cfg.gap;
364                 break;
365         default:
366                 status = -1;
367                 break;
368         }
369
370         return status;
371 }
372
373 /*
374  * Sends IOCTL request to cancel the existing Host Sleep configuration.
375  *
376  * This function allocates the IOCTL request buffer, fills it
377  * with requisite parameters and calls the IOCTL handler.
378  */
379 int mwifiex_cancel_hs(struct mwifiex_private *priv, int cmd_type)
380 {
381         struct mwifiex_ds_hs_cfg hscfg;
382
383         hscfg.conditions = HOST_SLEEP_CFG_CANCEL;
384         hscfg.is_invoke_hostcmd = true;
385
386         return mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
387                                     cmd_type, &hscfg);
388 }
389 EXPORT_SYMBOL_GPL(mwifiex_cancel_hs);
390
391 /*
392  * Sends IOCTL request to cancel the existing Host Sleep configuration.
393  *
394  * This function allocates the IOCTL request buffer, fills it
395  * with requisite parameters and calls the IOCTL handler.
396  */
397 int mwifiex_enable_hs(struct mwifiex_adapter *adapter)
398 {
399         struct mwifiex_ds_hs_cfg hscfg;
400
401         if (adapter->hs_activated) {
402                 dev_dbg(adapter->dev, "cmd: HS Already actived\n");
403                 return true;
404         }
405
406         adapter->hs_activate_wait_q_woken = false;
407
408         memset(&hscfg, 0, sizeof(struct mwifiex_hs_config_param));
409         hscfg.is_invoke_hostcmd = true;
410
411         if (mwifiex_set_hs_params(mwifiex_get_priv(adapter,
412                                                        MWIFIEX_BSS_ROLE_STA),
413                                   HostCmd_ACT_GEN_SET, MWIFIEX_SYNC_CMD,
414                                   &hscfg)) {
415                 dev_err(adapter->dev, "IOCTL request HS enable failed\n");
416                 return false;
417         }
418
419         wait_event_interruptible(adapter->hs_activate_wait_q,
420                         adapter->hs_activate_wait_q_woken);
421
422         return true;
423 }
424 EXPORT_SYMBOL_GPL(mwifiex_enable_hs);
425
426 /*
427  * IOCTL request handler to get BSS information.
428  *
429  * This function collates the information from different driver structures
430  * to send to the user.
431  */
432 int mwifiex_get_bss_info(struct mwifiex_private *priv,
433                          struct mwifiex_bss_info *info)
434 {
435         struct mwifiex_adapter *adapter = priv->adapter;
436         struct mwifiex_bssdescriptor *bss_desc;
437
438         if (!info)
439                 return -1;
440
441         bss_desc = &priv->curr_bss_params.bss_descriptor;
442
443         info->bss_mode = priv->bss_mode;
444
445         memcpy(&info->ssid, &bss_desc->ssid,
446                sizeof(struct mwifiex_802_11_ssid));
447
448         memcpy(&info->bssid, &bss_desc->mac_address, ETH_ALEN);
449
450         info->bss_chan = bss_desc->channel;
451
452         info->region_code = adapter->region_code;
453
454         info->media_connected = priv->media_connected;
455
456         info->max_power_level = priv->max_tx_power_level;
457         info->min_power_level = priv->min_tx_power_level;
458
459         info->adhoc_state = priv->adhoc_state;
460
461         info->bcn_nf_last = priv->bcn_nf_last;
462
463         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED)
464                 info->wep_status = true;
465         else
466                 info->wep_status = false;
467
468         info->is_hs_configured = adapter->is_hs_configured;
469         info->is_deep_sleep = adapter->is_deep_sleep;
470
471         return 0;
472 }
473
474 /*
475  * The function sets band configurations.
476  *
477  * it performs extra checks to make sure the Ad-Hoc
478  * band and channel are compatible. Otherwise it returns an error.
479  *
480  */
481 int mwifiex_set_radio_band_cfg(struct mwifiex_private *priv,
482                                struct mwifiex_ds_band_cfg *radio_cfg)
483 {
484         struct mwifiex_adapter *adapter = priv->adapter;
485         u8 infra_band, adhoc_band;
486         u32 adhoc_channel;
487
488         infra_band = (u8) radio_cfg->config_bands;
489         adhoc_band = (u8) radio_cfg->adhoc_start_band;
490         adhoc_channel = radio_cfg->adhoc_channel;
491
492         /* SET Infra band */
493         if ((infra_band | adapter->fw_bands) & ~adapter->fw_bands)
494                 return -1;
495
496         adapter->config_bands = infra_band;
497
498         /* SET Ad-hoc Band */
499         if ((adhoc_band | adapter->fw_bands) & ~adapter->fw_bands)
500                 return -1;
501
502         if (adhoc_band)
503                 adapter->adhoc_start_band = adhoc_band;
504         adapter->chan_offset = (u8) radio_cfg->sec_chan_offset;
505         /*
506          * If no adhoc_channel is supplied verify if the existing adhoc
507          * channel compiles with new adhoc_band
508          */
509         if (!adhoc_channel) {
510                 if (!mwifiex_get_cfp_by_band_and_channel_from_cfg80211
511                      (priv, adapter->adhoc_start_band,
512                      priv->adhoc_channel)) {
513                         /* Pass back the default channel */
514                         radio_cfg->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
515                         if ((adapter->adhoc_start_band & BAND_A)
516                             || (adapter->adhoc_start_band & BAND_AN))
517                                 radio_cfg->adhoc_channel =
518                                         DEFAULT_AD_HOC_CHANNEL_A;
519                 }
520         } else {        /* Retrurn error if adhoc_band and
521                            adhoc_channel combination is invalid */
522                 if (!mwifiex_get_cfp_by_band_and_channel_from_cfg80211
523                     (priv, adapter->adhoc_start_band, (u16) adhoc_channel))
524                         return -1;
525                 priv->adhoc_channel = (u8) adhoc_channel;
526         }
527         if ((adhoc_band & BAND_GN) || (adhoc_band & BAND_AN))
528                 adapter->adhoc_11n_enabled = true;
529         else
530                 adapter->adhoc_11n_enabled = false;
531
532         return 0;
533 }
534
535 /*
536  * The function disables auto deep sleep mode.
537  */
538 int mwifiex_disable_auto_ds(struct mwifiex_private *priv)
539 {
540         struct mwifiex_ds_auto_ds auto_ds;
541
542         auto_ds.auto_ds = DEEP_SLEEP_OFF;
543
544         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
545                                      DIS_AUTO_PS, BITMAP_AUTO_DS, &auto_ds);
546 }
547 EXPORT_SYMBOL_GPL(mwifiex_disable_auto_ds);
548
549 /*
550  * IOCTL request handler to set/get active channel.
551  *
552  * This function performs validity checking on channel/frequency
553  * compatibility and returns failure if not valid.
554  */
555 int mwifiex_bss_set_channel(struct mwifiex_private *priv,
556                             struct mwifiex_chan_freq_power *chan)
557 {
558         struct mwifiex_adapter *adapter = priv->adapter;
559         struct mwifiex_chan_freq_power *cfp = NULL;
560
561         if (!chan)
562                 return -1;
563
564         if (!chan->channel && !chan->freq)
565                 return -1;
566         if (adapter->adhoc_start_band & BAND_AN)
567                 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
568         else if (adapter->adhoc_start_band & BAND_A)
569                 adapter->adhoc_start_band = BAND_G | BAND_B;
570         if (chan->channel) {
571                 if (chan->channel <= MAX_CHANNEL_BAND_BG)
572                         cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211
573                                         (priv, 0, (u16) chan->channel);
574                 if (!cfp) {
575                         cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211
576                                         (priv, BAND_A, (u16) chan->channel);
577                         if (cfp) {
578                                 if (adapter->adhoc_11n_enabled)
579                                         adapter->adhoc_start_band = BAND_A
580                                                 | BAND_AN;
581                                 else
582                                         adapter->adhoc_start_band = BAND_A;
583                         }
584                 }
585         } else {
586                 if (chan->freq <= MAX_FREQUENCY_BAND_BG)
587                         cfp = mwifiex_get_cfp_by_band_and_freq_from_cfg80211(
588                                                         priv, 0, chan->freq);
589                 if (!cfp) {
590                         cfp = mwifiex_get_cfp_by_band_and_freq_from_cfg80211
591                                                   (priv, BAND_A, chan->freq);
592                         if (cfp) {
593                                 if (adapter->adhoc_11n_enabled)
594                                         adapter->adhoc_start_band = BAND_A
595                                                 | BAND_AN;
596                                 else
597                                         adapter->adhoc_start_band = BAND_A;
598                         }
599                 }
600         }
601         if (!cfp || !cfp->channel) {
602                 dev_err(adapter->dev, "invalid channel/freq\n");
603                 return -1;
604         }
605         priv->adhoc_channel = (u8) cfp->channel;
606         chan->channel = cfp->channel;
607         chan->freq = cfp->freq;
608
609         return 0;
610 }
611
612 /*
613  * IOCTL request handler to set/get Ad-Hoc channel.
614  *
615  * This function prepares the correct firmware command and
616  * issues it to set or get the ad-hoc channel.
617  */
618 static int mwifiex_bss_ioctl_ibss_channel(struct mwifiex_private *priv,
619                                           u16 action, u16 *channel)
620 {
621         if (action == HostCmd_ACT_GEN_GET) {
622                 if (!priv->media_connected) {
623                         *channel = priv->adhoc_channel;
624                         return 0;
625                 }
626         } else {
627                 priv->adhoc_channel = (u8) *channel;
628         }
629
630         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_RF_CHANNEL,
631                                     action, 0, channel);
632 }
633
634 /*
635  * IOCTL request handler to change Ad-Hoc channel.
636  *
637  * This function allocates the IOCTL request buffer, fills it
638  * with requisite parameters and calls the IOCTL handler.
639  *
640  * The function follows the following steps to perform the change -
641  *      - Get current IBSS information
642  *      - Get current channel
643  *      - If no change is required, return
644  *      - If not connected, change channel and return
645  *      - If connected,
646  *          - Disconnect
647  *          - Change channel
648  *          - Perform specific SSID scan with same SSID
649  *          - Start/Join the IBSS
650  */
651 int
652 mwifiex_drv_change_adhoc_chan(struct mwifiex_private *priv, int channel)
653 {
654         int ret;
655         struct mwifiex_bss_info bss_info;
656         struct mwifiex_ssid_bssid ssid_bssid;
657         u16 curr_chan = 0;
658         struct cfg80211_bss *bss = NULL;
659         struct ieee80211_channel *chan;
660         enum ieee80211_band band;
661
662         memset(&bss_info, 0, sizeof(bss_info));
663
664         /* Get BSS information */
665         if (mwifiex_get_bss_info(priv, &bss_info))
666                 return -1;
667
668         /* Get current channel */
669         ret = mwifiex_bss_ioctl_ibss_channel(priv, HostCmd_ACT_GEN_GET,
670                                              &curr_chan);
671
672         if (curr_chan == channel) {
673                 ret = 0;
674                 goto done;
675         }
676         dev_dbg(priv->adapter->dev, "cmd: updating channel from %d to %d\n",
677                         curr_chan, channel);
678
679         if (!bss_info.media_connected) {
680                 ret = 0;
681                 goto done;
682         }
683
684         /* Do disonnect */
685         memset(&ssid_bssid, 0, ETH_ALEN);
686         ret = mwifiex_deauthenticate(priv, ssid_bssid.bssid);
687
688         ret = mwifiex_bss_ioctl_ibss_channel(priv, HostCmd_ACT_GEN_SET,
689                                              (u16 *) &channel);
690
691         /* Do specific SSID scanning */
692         if (mwifiex_request_scan(priv, &bss_info.ssid)) {
693                 ret = -1;
694                 goto done;
695         }
696
697         band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
698         chan = __ieee80211_get_channel(priv->wdev->wiphy,
699                         ieee80211_channel_to_frequency(channel, band));
700
701         /* Find the BSS we want using available scan results */
702         bss = cfg80211_get_bss(priv->wdev->wiphy, chan, bss_info.bssid,
703                                bss_info.ssid.ssid, bss_info.ssid.ssid_len,
704                                WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
705         if (!bss)
706                 wiphy_warn(priv->wdev->wiphy, "assoc: bss %pM not in scan results\n",
707                           bss_info.bssid);
708
709         ret = mwifiex_bss_start(priv, bss, &bss_info.ssid);
710 done:
711         return ret;
712 }
713
714 /*
715  * IOCTL request handler to get rate.
716  *
717  * This function prepares the correct firmware command and
718  * issues it to get the current rate if it is connected,
719  * otherwise, the function returns the lowest supported rate
720  * for the band.
721  */
722 static int mwifiex_rate_ioctl_get_rate_value(struct mwifiex_private *priv,
723                                              struct mwifiex_rate_cfg *rate_cfg)
724 {
725         rate_cfg->is_rate_auto = priv->is_data_rate_auto;
726         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_TX_RATE_QUERY,
727                                      HostCmd_ACT_GEN_GET, 0, NULL);
728 }
729
730 /*
731  * IOCTL request handler to set rate.
732  *
733  * This function prepares the correct firmware command and
734  * issues it to set the current rate.
735  *
736  * The function also performs validation checking on the supplied value.
737  */
738 static int mwifiex_rate_ioctl_set_rate_value(struct mwifiex_private *priv,
739                                              struct mwifiex_rate_cfg *rate_cfg)
740 {
741         u8 rates[MWIFIEX_SUPPORTED_RATES];
742         u8 *rate;
743         int rate_index, ret;
744         u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
745         u32 i;
746         struct mwifiex_adapter *adapter = priv->adapter;
747
748         if (rate_cfg->is_rate_auto) {
749                 memset(bitmap_rates, 0, sizeof(bitmap_rates));
750                 /* Support all HR/DSSS rates */
751                 bitmap_rates[0] = 0x000F;
752                 /* Support all OFDM rates */
753                 bitmap_rates[1] = 0x00FF;
754                 /* Support all HT-MCSs rate */
755                 for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates) - 3; i++)
756                         bitmap_rates[i + 2] = 0xFFFF;
757                 bitmap_rates[9] = 0x3FFF;
758         } else {
759                 memset(rates, 0, sizeof(rates));
760                 mwifiex_get_active_data_rates(priv, rates);
761                 rate = rates;
762                 for (i = 0; (rate[i] && i < MWIFIEX_SUPPORTED_RATES); i++) {
763                         dev_dbg(adapter->dev, "info: rate=%#x wanted=%#x\n",
764                                 rate[i], rate_cfg->rate);
765                         if ((rate[i] & 0x7f) == (rate_cfg->rate & 0x7f))
766                                 break;
767                 }
768                 if ((i == MWIFIEX_SUPPORTED_RATES) || !rate[i]) {
769                         dev_err(adapter->dev, "fixed data rate %#x is out "
770                                "of range\n", rate_cfg->rate);
771                         return -1;
772                 }
773                 memset(bitmap_rates, 0, sizeof(bitmap_rates));
774
775                 rate_index = mwifiex_data_rate_to_index(rate_cfg->rate);
776
777                 /* Only allow b/g rates to be set */
778                 if (rate_index >= MWIFIEX_RATE_INDEX_HRDSSS0 &&
779                     rate_index <= MWIFIEX_RATE_INDEX_HRDSSS3) {
780                         bitmap_rates[0] = 1 << rate_index;
781                 } else {
782                         rate_index -= 1; /* There is a 0x00 in the table */
783                         if (rate_index >= MWIFIEX_RATE_INDEX_OFDM0 &&
784                             rate_index <= MWIFIEX_RATE_INDEX_OFDM7)
785                                 bitmap_rates[1] = 1 << (rate_index -
786                                                    MWIFIEX_RATE_INDEX_OFDM0);
787                 }
788         }
789
790         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_TX_RATE_CFG,
791                                     HostCmd_ACT_GEN_SET, 0, bitmap_rates);
792
793         return ret;
794 }
795
796 /*
797  * IOCTL request handler to set/get rate.
798  *
799  * This function can be used to set/get either the rate value or the
800  * rate index.
801  */
802 static int mwifiex_rate_ioctl_cfg(struct mwifiex_private *priv,
803                                   struct mwifiex_rate_cfg *rate_cfg)
804 {
805         int status;
806
807         if (!rate_cfg)
808                 return -1;
809
810         if (rate_cfg->action == HostCmd_ACT_GEN_GET)
811                 status = mwifiex_rate_ioctl_get_rate_value(priv, rate_cfg);
812         else
813                 status = mwifiex_rate_ioctl_set_rate_value(priv, rate_cfg);
814
815         return status;
816 }
817
818 /*
819  * Sends IOCTL request to get the data rate.
820  *
821  * This function allocates the IOCTL request buffer, fills it
822  * with requisite parameters and calls the IOCTL handler.
823  */
824 int mwifiex_drv_get_data_rate(struct mwifiex_private *priv,
825                               struct mwifiex_rate_cfg *rate)
826 {
827         int ret;
828
829         memset(rate, 0, sizeof(struct mwifiex_rate_cfg));
830         rate->action = HostCmd_ACT_GEN_GET;
831         ret = mwifiex_rate_ioctl_cfg(priv, rate);
832
833         if (!ret) {
834                 if (rate->is_rate_auto)
835                         rate->rate = mwifiex_index_to_data_rate(priv,
836                                         priv->tx_rate, priv->tx_htinfo);
837                 else
838                         rate->rate = priv->data_rate;
839         } else {
840                 ret = -1;
841         }
842
843         return ret;
844 }
845
846 /*
847  * IOCTL request handler to set tx power configuration.
848  *
849  * This function prepares the correct firmware command and
850  * issues it.
851  *
852  * For non-auto power mode, all the following power groups are set -
853  *      - Modulation class HR/DSSS
854  *      - Modulation class OFDM
855  *      - Modulation class HTBW20
856  *      - Modulation class HTBW40
857  */
858 int mwifiex_set_tx_power(struct mwifiex_private *priv,
859                          struct mwifiex_power_cfg *power_cfg)
860 {
861         int ret;
862         struct host_cmd_ds_txpwr_cfg *txp_cfg;
863         struct mwifiex_types_power_group *pg_tlv;
864         struct mwifiex_power_group *pg;
865         u8 *buf;
866         u16 dbm = 0;
867
868         if (!power_cfg->is_power_auto) {
869                 dbm = (u16) power_cfg->power_level;
870                 if ((dbm < priv->min_tx_power_level) ||
871                     (dbm > priv->max_tx_power_level)) {
872                         dev_err(priv->adapter->dev, "txpower value %d dBm"
873                                         " is out of range (%d dBm-%d dBm)\n",
874                                         dbm, priv->min_tx_power_level,
875                                         priv->max_tx_power_level);
876                         return -1;
877                 }
878         }
879         buf = kzalloc(MWIFIEX_SIZE_OF_CMD_BUFFER, GFP_KERNEL);
880         if (!buf) {
881                 dev_err(priv->adapter->dev, "%s: failed to alloc cmd buffer\n",
882                                 __func__);
883                 return -ENOMEM;
884         }
885
886         txp_cfg = (struct host_cmd_ds_txpwr_cfg *) buf;
887         txp_cfg->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
888         if (!power_cfg->is_power_auto) {
889                 txp_cfg->mode = cpu_to_le32(1);
890                 pg_tlv = (struct mwifiex_types_power_group *) (buf +
891                                 sizeof(struct host_cmd_ds_txpwr_cfg));
892                 pg_tlv->type = TLV_TYPE_POWER_GROUP;
893                 pg_tlv->length = 4 * sizeof(struct mwifiex_power_group);
894                 pg = (struct mwifiex_power_group *) (buf +
895                                 sizeof(struct host_cmd_ds_txpwr_cfg) +
896                                 sizeof(struct mwifiex_types_power_group));
897                 /* Power group for modulation class HR/DSSS */
898                 pg->first_rate_code = 0x00;
899                 pg->last_rate_code = 0x03;
900                 pg->modulation_class = MOD_CLASS_HR_DSSS;
901                 pg->power_step = 0;
902                 pg->power_min = (s8) dbm;
903                 pg->power_max = (s8) dbm;
904                 pg++;
905                 /* Power group for modulation class OFDM */
906                 pg->first_rate_code = 0x00;
907                 pg->last_rate_code = 0x07;
908                 pg->modulation_class = MOD_CLASS_OFDM;
909                 pg->power_step = 0;
910                 pg->power_min = (s8) dbm;
911                 pg->power_max = (s8) dbm;
912                 pg++;
913                 /* Power group for modulation class HTBW20 */
914                 pg->first_rate_code = 0x00;
915                 pg->last_rate_code = 0x20;
916                 pg->modulation_class = MOD_CLASS_HT;
917                 pg->power_step = 0;
918                 pg->power_min = (s8) dbm;
919                 pg->power_max = (s8) dbm;
920                 pg->ht_bandwidth = HT_BW_20;
921                 pg++;
922                 /* Power group for modulation class HTBW40 */
923                 pg->first_rate_code = 0x00;
924                 pg->last_rate_code = 0x20;
925                 pg->modulation_class = MOD_CLASS_HT;
926                 pg->power_step = 0;
927                 pg->power_min = (s8) dbm;
928                 pg->power_max = (s8) dbm;
929                 pg->ht_bandwidth = HT_BW_40;
930         }
931         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_TXPWR_CFG,
932                                     HostCmd_ACT_GEN_SET, 0, buf);
933
934         kfree(buf);
935         return ret;
936 }
937
938 /*
939  * IOCTL request handler to get power save mode.
940  *
941  * This function prepares the correct firmware command and
942  * issues it.
943  */
944 int mwifiex_drv_set_power(struct mwifiex_private *priv, u32 *ps_mode)
945 {
946         int ret;
947         struct mwifiex_adapter *adapter = priv->adapter;
948         u16 sub_cmd;
949
950         if (*ps_mode)
951                 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
952         else
953                 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
954         sub_cmd = (*ps_mode) ? EN_AUTO_PS : DIS_AUTO_PS;
955         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
956                                     sub_cmd, BITMAP_STA_PS, NULL);
957         if ((!ret) && (sub_cmd == DIS_AUTO_PS))
958                 ret = mwifiex_send_cmd_async(priv,
959                                 HostCmd_CMD_802_11_PS_MODE_ENH, GET_PS,
960                                 0, NULL);
961
962         return ret;
963 }
964
965 /*
966  * IOCTL request handler to set/reset WPA IE.
967  *
968  * The supplied WPA IE is treated as a opaque buffer. Only the first field
969  * is checked to determine WPA version. If buffer length is zero, the existing
970  * WPA IE is reset.
971  */
972 static int mwifiex_set_wpa_ie_helper(struct mwifiex_private *priv,
973                                      u8 *ie_data_ptr, u16 ie_len)
974 {
975         if (ie_len) {
976                 if (ie_len > sizeof(priv->wpa_ie)) {
977                         dev_err(priv->adapter->dev,
978                                 "failed to copy WPA IE, too big\n");
979                         return -1;
980                 }
981                 memcpy(priv->wpa_ie, ie_data_ptr, ie_len);
982                 priv->wpa_ie_len = (u8) ie_len;
983                 dev_dbg(priv->adapter->dev, "cmd: Set Wpa_ie_len=%d IE=%#x\n",
984                                 priv->wpa_ie_len, priv->wpa_ie[0]);
985
986                 if (priv->wpa_ie[0] == WLAN_EID_WPA) {
987                         priv->sec_info.wpa_enabled = true;
988                 } else if (priv->wpa_ie[0] == WLAN_EID_RSN) {
989                         priv->sec_info.wpa2_enabled = true;
990                 } else {
991                         priv->sec_info.wpa_enabled = false;
992                         priv->sec_info.wpa2_enabled = false;
993                 }
994         } else {
995                 memset(priv->wpa_ie, 0, sizeof(priv->wpa_ie));
996                 priv->wpa_ie_len = 0;
997                 dev_dbg(priv->adapter->dev, "info: reset wpa_ie_len=%d IE=%#x\n",
998                         priv->wpa_ie_len, priv->wpa_ie[0]);
999                 priv->sec_info.wpa_enabled = false;
1000                 priv->sec_info.wpa2_enabled = false;
1001         }
1002
1003         return 0;
1004 }
1005
1006 /*
1007  * IOCTL request handler to set/reset WAPI IE.
1008  *
1009  * The supplied WAPI IE is treated as a opaque buffer. Only the first field
1010  * is checked to internally enable WAPI. If buffer length is zero, the existing
1011  * WAPI IE is reset.
1012  */
1013 static int mwifiex_set_wapi_ie(struct mwifiex_private *priv,
1014                                u8 *ie_data_ptr, u16 ie_len)
1015 {
1016         if (ie_len) {
1017                 if (ie_len > sizeof(priv->wapi_ie)) {
1018                         dev_dbg(priv->adapter->dev,
1019                                 "info: failed to copy WAPI IE, too big\n");
1020                         return -1;
1021                 }
1022                 memcpy(priv->wapi_ie, ie_data_ptr, ie_len);
1023                 priv->wapi_ie_len = ie_len;
1024                 dev_dbg(priv->adapter->dev, "cmd: Set wapi_ie_len=%d IE=%#x\n",
1025                                 priv->wapi_ie_len, priv->wapi_ie[0]);
1026
1027                 if (priv->wapi_ie[0] == WLAN_EID_BSS_AC_ACCESS_DELAY)
1028                         priv->sec_info.wapi_enabled = true;
1029         } else {
1030                 memset(priv->wapi_ie, 0, sizeof(priv->wapi_ie));
1031                 priv->wapi_ie_len = ie_len;
1032                 dev_dbg(priv->adapter->dev,
1033                         "info: Reset wapi_ie_len=%d IE=%#x\n",
1034                        priv->wapi_ie_len, priv->wapi_ie[0]);
1035                 priv->sec_info.wapi_enabled = false;
1036         }
1037         return 0;
1038 }
1039
1040 /*
1041  * IOCTL request handler to set WAPI key.
1042  *
1043  * This function prepares the correct firmware command and
1044  * issues it.
1045  */
1046 static int mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private *priv,
1047                                struct mwifiex_ds_encrypt_key *encrypt_key)
1048 {
1049
1050         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1051                                     HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1052                                     encrypt_key);
1053 }
1054
1055 /*
1056  * IOCTL request handler to set WEP network key.
1057  *
1058  * This function prepares the correct firmware command and
1059  * issues it, after validation checks.
1060  */
1061 static int mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private *priv,
1062                               struct mwifiex_ds_encrypt_key *encrypt_key)
1063 {
1064         int ret;
1065         struct mwifiex_wep_key *wep_key;
1066         int index;
1067
1068         if (priv->wep_key_curr_index >= NUM_WEP_KEYS)
1069                 priv->wep_key_curr_index = 0;
1070         wep_key = &priv->wep_key[priv->wep_key_curr_index];
1071         index = encrypt_key->key_index;
1072         if (encrypt_key->key_disable) {
1073                 priv->sec_info.wep_status = MWIFIEX_802_11_WEP_DISABLED;
1074         } else if (!encrypt_key->key_len) {
1075                 /* Copy the required key as the current key */
1076                 wep_key = &priv->wep_key[index];
1077                 if (!wep_key->key_length) {
1078                         dev_err(priv->adapter->dev,
1079                                 "key not set, so cannot enable it\n");
1080                         return -1;
1081                 }
1082                 priv->wep_key_curr_index = (u16) index;
1083                 priv->sec_info.wep_status = MWIFIEX_802_11_WEP_ENABLED;
1084         } else {
1085                 wep_key = &priv->wep_key[index];
1086                 memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
1087                 /* Copy the key in the driver */
1088                 memcpy(wep_key->key_material,
1089                        encrypt_key->key_material,
1090                        encrypt_key->key_len);
1091                 wep_key->key_index = index;
1092                 wep_key->key_length = encrypt_key->key_len;
1093                 priv->sec_info.wep_status = MWIFIEX_802_11_WEP_ENABLED;
1094         }
1095         if (wep_key->key_length) {
1096                 /* Send request to firmware */
1097                 ret = mwifiex_send_cmd_async(priv,
1098                                              HostCmd_CMD_802_11_KEY_MATERIAL,
1099                                              HostCmd_ACT_GEN_SET, 0, NULL);
1100                 if (ret)
1101                         return ret;
1102         }
1103         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED)
1104                 priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
1105         else
1106                 priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
1107
1108         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_MAC_CONTROL,
1109                                     HostCmd_ACT_GEN_SET, 0,
1110                                     &priv->curr_pkt_filter);
1111
1112         return ret;
1113 }
1114
1115 /*
1116  * IOCTL request handler to set WPA key.
1117  *
1118  * This function prepares the correct firmware command and
1119  * issues it, after validation checks.
1120  *
1121  * Current driver only supports key length of up to 32 bytes.
1122  *
1123  * This function can also be used to disable a currently set key.
1124  */
1125 static int mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private *priv,
1126                               struct mwifiex_ds_encrypt_key *encrypt_key)
1127 {
1128         int ret;
1129         u8 remove_key = false;
1130         struct host_cmd_ds_802_11_key_material *ibss_key;
1131
1132         /* Current driver only supports key length of up to 32 bytes */
1133         if (encrypt_key->key_len > WLAN_MAX_KEY_LEN) {
1134                 dev_err(priv->adapter->dev, "key length too long\n");
1135                 return -1;
1136         }
1137
1138         if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
1139                 /*
1140                  * IBSS/WPA-None uses only one key (Group) for both receiving
1141                  * and sending unicast and multicast packets.
1142                  */
1143                 /* Send the key as PTK to firmware */
1144                 encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1145                 ret = mwifiex_send_cmd_async(priv,
1146                                         HostCmd_CMD_802_11_KEY_MATERIAL,
1147                                         HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1148                                         encrypt_key);
1149                 if (ret)
1150                         return ret;
1151
1152                 ibss_key = &priv->aes_key;
1153                 memset(ibss_key, 0,
1154                        sizeof(struct host_cmd_ds_802_11_key_material));
1155                 /* Copy the key in the driver */
1156                 memcpy(ibss_key->key_param_set.key, encrypt_key->key_material,
1157                        encrypt_key->key_len);
1158                 memcpy(&ibss_key->key_param_set.key_len, &encrypt_key->key_len,
1159                        sizeof(ibss_key->key_param_set.key_len));
1160                 ibss_key->key_param_set.key_type_id
1161                         = cpu_to_le16(KEY_TYPE_ID_TKIP);
1162                 ibss_key->key_param_set.key_info = cpu_to_le16(KEY_ENABLED);
1163
1164                 /* Send the key as GTK to firmware */
1165                 encrypt_key->key_index = ~MWIFIEX_KEY_INDEX_UNICAST;
1166         }
1167
1168         if (!encrypt_key->key_index)
1169                 encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1170
1171         if (remove_key)
1172                 ret = mwifiex_send_cmd_sync(priv,
1173                                        HostCmd_CMD_802_11_KEY_MATERIAL,
1174                                        HostCmd_ACT_GEN_SET, !(KEY_INFO_ENABLED),
1175                                        encrypt_key);
1176         else
1177                 ret = mwifiex_send_cmd_sync(priv,
1178                                         HostCmd_CMD_802_11_KEY_MATERIAL,
1179                                         HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1180                                         encrypt_key);
1181
1182         return ret;
1183 }
1184
1185 /*
1186  * IOCTL request handler to set/get network keys.
1187  *
1188  * This is a generic key handling function which supports WEP, WPA
1189  * and WAPI.
1190  */
1191 static int
1192 mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private *priv,
1193                               struct mwifiex_ds_encrypt_key *encrypt_key)
1194 {
1195         int status;
1196
1197         if (encrypt_key->is_wapi_key)
1198                 status = mwifiex_sec_ioctl_set_wapi_key(priv, encrypt_key);
1199         else if (encrypt_key->key_len > WLAN_KEY_LEN_WEP104)
1200                 status = mwifiex_sec_ioctl_set_wpa_key(priv, encrypt_key);
1201         else
1202                 status = mwifiex_sec_ioctl_set_wep_key(priv, encrypt_key);
1203         return status;
1204 }
1205
1206 /*
1207  * This function returns the driver version.
1208  */
1209 int
1210 mwifiex_drv_get_driver_version(struct mwifiex_adapter *adapter, char *version,
1211                                int max_len)
1212 {
1213         union {
1214                 u32 l;
1215                 u8 c[4];
1216         } ver;
1217         char fw_ver[32];
1218
1219         ver.l = adapter->fw_release_number;
1220         sprintf(fw_ver, "%u.%u.%u.p%u", ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
1221
1222         snprintf(version, max_len, driver_version, fw_ver);
1223
1224         dev_dbg(adapter->dev, "info: MWIFIEX VERSION: %s\n", version);
1225
1226         return 0;
1227 }
1228
1229 /*
1230  * Sends IOCTL request to get signal information.
1231  *
1232  * This function allocates the IOCTL request buffer, fills it
1233  * with requisite parameters and calls the IOCTL handler.
1234  */
1235 int mwifiex_get_signal_info(struct mwifiex_private *priv,
1236                             struct mwifiex_ds_get_signal *signal)
1237 {
1238         int status;
1239
1240         signal->selector = ALL_RSSI_INFO_MASK;
1241
1242         /* Signal info can be obtained only if connected */
1243         if (!priv->media_connected) {
1244                 dev_dbg(priv->adapter->dev,
1245                         "info: Can not get signal in disconnected state\n");
1246                 return -1;
1247         }
1248
1249         status = mwifiex_send_cmd_sync(priv, HostCmd_CMD_RSSI_INFO,
1250                                        HostCmd_ACT_GEN_GET, 0, signal);
1251
1252         if (!status) {
1253                 if (signal->selector & BCN_RSSI_AVG_MASK)
1254                         priv->qual_level = signal->bcn_rssi_avg;
1255                 if (signal->selector & BCN_NF_AVG_MASK)
1256                         priv->qual_noise = signal->bcn_nf_avg;
1257         }
1258
1259         return status;
1260 }
1261
1262 /*
1263  * Sends IOCTL request to set encoding parameters.
1264  *
1265  * This function allocates the IOCTL request buffer, fills it
1266  * with requisite parameters and calls the IOCTL handler.
1267  */
1268 int mwifiex_set_encode(struct mwifiex_private *priv, const u8 *key,
1269                         int key_len, u8 key_index, int disable)
1270 {
1271         struct mwifiex_ds_encrypt_key encrypt_key;
1272
1273         memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
1274         encrypt_key.key_len = key_len;
1275         if (!disable) {
1276                 encrypt_key.key_index = key_index;
1277                 if (key_len)
1278                         memcpy(encrypt_key.key_material, key, key_len);
1279         } else {
1280                 encrypt_key.key_disable = true;
1281         }
1282
1283         return mwifiex_sec_ioctl_encrypt_key(priv, &encrypt_key);
1284 }
1285
1286 /*
1287  * Sends IOCTL request to get extended version.
1288  *
1289  * This function allocates the IOCTL request buffer, fills it
1290  * with requisite parameters and calls the IOCTL handler.
1291  */
1292 int
1293 mwifiex_get_ver_ext(struct mwifiex_private *priv)
1294 {
1295         struct mwifiex_ver_ext ver_ext;
1296
1297         memset(&ver_ext, 0, sizeof(struct host_cmd_ds_version_ext));
1298         if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_VERSION_EXT,
1299                                     HostCmd_ACT_GEN_GET, 0, &ver_ext))
1300                 return -1;
1301
1302         return 0;
1303 }
1304
1305 /*
1306  * Sends IOCTL request to get statistics information.
1307  *
1308  * This function allocates the IOCTL request buffer, fills it
1309  * with requisite parameters and calls the IOCTL handler.
1310  */
1311 int
1312 mwifiex_get_stats_info(struct mwifiex_private *priv,
1313                        struct mwifiex_ds_get_stats *log)
1314 {
1315         return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_GET_LOG,
1316                                     HostCmd_ACT_GEN_GET, 0, log);
1317 }
1318
1319 /*
1320  * IOCTL request handler to read/write register.
1321  *
1322  * This function prepares the correct firmware command and
1323  * issues it.
1324  *
1325  * Access to the following registers are supported -
1326  *      - MAC
1327  *      - BBP
1328  *      - RF
1329  *      - PMIC
1330  *      - CAU
1331  */
1332 static int mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private *priv,
1333                                         struct mwifiex_ds_reg_rw *reg_rw,
1334                                         u16 action)
1335 {
1336         u16 cmd_no;
1337
1338         switch (le32_to_cpu(reg_rw->type)) {
1339         case MWIFIEX_REG_MAC:
1340                 cmd_no = HostCmd_CMD_MAC_REG_ACCESS;
1341                 break;
1342         case MWIFIEX_REG_BBP:
1343                 cmd_no = HostCmd_CMD_BBP_REG_ACCESS;
1344                 break;
1345         case MWIFIEX_REG_RF:
1346                 cmd_no = HostCmd_CMD_RF_REG_ACCESS;
1347                 break;
1348         case MWIFIEX_REG_PMIC:
1349                 cmd_no = HostCmd_CMD_PMIC_REG_ACCESS;
1350                 break;
1351         case MWIFIEX_REG_CAU:
1352                 cmd_no = HostCmd_CMD_CAU_REG_ACCESS;
1353                 break;
1354         default:
1355                 return -1;
1356         }
1357
1358         return mwifiex_send_cmd_sync(priv, cmd_no, action, 0, reg_rw);
1359
1360 }
1361
1362 /*
1363  * Sends IOCTL request to write to a register.
1364  *
1365  * This function allocates the IOCTL request buffer, fills it
1366  * with requisite parameters and calls the IOCTL handler.
1367  */
1368 int
1369 mwifiex_reg_write(struct mwifiex_private *priv, u32 reg_type,
1370                   u32 reg_offset, u32 reg_value)
1371 {
1372         struct mwifiex_ds_reg_rw reg_rw;
1373
1374         reg_rw.type = cpu_to_le32(reg_type);
1375         reg_rw.offset = cpu_to_le32(reg_offset);
1376         reg_rw.value = cpu_to_le32(reg_value);
1377
1378         return mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_SET);
1379 }
1380
1381 /*
1382  * Sends IOCTL request to read from a register.
1383  *
1384  * This function allocates the IOCTL request buffer, fills it
1385  * with requisite parameters and calls the IOCTL handler.
1386  */
1387 int
1388 mwifiex_reg_read(struct mwifiex_private *priv, u32 reg_type,
1389                  u32 reg_offset, u32 *value)
1390 {
1391         int ret;
1392         struct mwifiex_ds_reg_rw reg_rw;
1393
1394         reg_rw.type = cpu_to_le32(reg_type);
1395         reg_rw.offset = cpu_to_le32(reg_offset);
1396         ret = mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_GET);
1397
1398         if (ret)
1399                 goto done;
1400
1401         *value = le32_to_cpu(reg_rw.value);
1402
1403 done:
1404         return ret;
1405 }
1406
1407 /*
1408  * Sends IOCTL request to read from EEPROM.
1409  *
1410  * This function allocates the IOCTL request buffer, fills it
1411  * with requisite parameters and calls the IOCTL handler.
1412  */
1413 int
1414 mwifiex_eeprom_read(struct mwifiex_private *priv, u16 offset, u16 bytes,
1415                     u8 *value)
1416 {
1417         int ret;
1418         struct mwifiex_ds_read_eeprom rd_eeprom;
1419
1420         rd_eeprom.offset = cpu_to_le16((u16) offset);
1421         rd_eeprom.byte_count = cpu_to_le16((u16) bytes);
1422
1423         /* Send request to firmware */
1424         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_EEPROM_ACCESS,
1425                                     HostCmd_ACT_GEN_GET, 0, &rd_eeprom);
1426
1427         if (!ret)
1428                 memcpy(value, rd_eeprom.value, MAX_EEPROM_DATA);
1429         return ret;
1430 }
1431
1432 /*
1433  * This function sets a generic IE. In addition to generic IE, it can
1434  * also handle WPA, WPA2 and WAPI IEs.
1435  */
1436 static int
1437 mwifiex_set_gen_ie_helper(struct mwifiex_private *priv, u8 *ie_data_ptr,
1438                           u16 ie_len)
1439 {
1440         int ret = 0;
1441         struct ieee_types_vendor_header *pvendor_ie;
1442         const u8 wpa_oui[] = { 0x00, 0x50, 0xf2, 0x01 };
1443         const u8 wps_oui[] = { 0x00, 0x50, 0xf2, 0x04 };
1444
1445         /* If the passed length is zero, reset the buffer */
1446         if (!ie_len) {
1447                 priv->gen_ie_buf_len = 0;
1448                 priv->wps.session_enable = false;
1449
1450                 return 0;
1451         } else if (!ie_data_ptr) {
1452                 return -1;
1453         }
1454         pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1455         /* Test to see if it is a WPA IE, if not, then it is a gen IE */
1456         if (((pvendor_ie->element_id == WLAN_EID_WPA)
1457              && (!memcmp(pvendor_ie->oui, wpa_oui, sizeof(wpa_oui))))
1458                         || (pvendor_ie->element_id == WLAN_EID_RSN)) {
1459
1460                 /* IE is a WPA/WPA2 IE so call set_wpa function */
1461                 ret = mwifiex_set_wpa_ie_helper(priv, ie_data_ptr, ie_len);
1462                 priv->wps.session_enable = false;
1463
1464                 return ret;
1465         } else if (pvendor_ie->element_id == WLAN_EID_BSS_AC_ACCESS_DELAY) {
1466                 /* IE is a WAPI IE so call set_wapi function */
1467                 ret = mwifiex_set_wapi_ie(priv, ie_data_ptr, ie_len);
1468
1469                 return ret;
1470         }
1471         /*
1472          * Verify that the passed length is not larger than the
1473          * available space remaining in the buffer
1474          */
1475         if (ie_len < (sizeof(priv->gen_ie_buf) - priv->gen_ie_buf_len)) {
1476
1477                 /* Test to see if it is a WPS IE, if so, enable
1478                  * wps session flag
1479                  */
1480                 pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1481                 if ((pvendor_ie->element_id == WLAN_EID_VENDOR_SPECIFIC)
1482                                 && (!memcmp(pvendor_ie->oui, wps_oui,
1483                                                 sizeof(wps_oui)))) {
1484                         priv->wps.session_enable = true;
1485                         dev_dbg(priv->adapter->dev,
1486                                 "info: WPS Session Enabled.\n");
1487                 }
1488
1489                 /* Append the passed data to the end of the
1490                    genIeBuffer */
1491                 memcpy(priv->gen_ie_buf + priv->gen_ie_buf_len, ie_data_ptr,
1492                                                                         ie_len);
1493                 /* Increment the stored buffer length by the
1494                    size passed */
1495                 priv->gen_ie_buf_len += ie_len;
1496         } else {
1497                 /* Passed data does not fit in the remaining
1498                    buffer space */
1499                 ret = -1;
1500         }
1501
1502         /* Return 0, or -1 for error case */
1503         return ret;
1504 }
1505
1506 /*
1507  * IOCTL request handler to set/get generic IE.
1508  *
1509  * In addition to various generic IEs, this function can also be
1510  * used to set the ARP filter.
1511  */
1512 static int mwifiex_misc_ioctl_gen_ie(struct mwifiex_private *priv,
1513                                      struct mwifiex_ds_misc_gen_ie *gen_ie,
1514                                      u16 action)
1515 {
1516         struct mwifiex_adapter *adapter = priv->adapter;
1517
1518         switch (gen_ie->type) {
1519         case MWIFIEX_IE_TYPE_GEN_IE:
1520                 if (action == HostCmd_ACT_GEN_GET) {
1521                         gen_ie->len = priv->wpa_ie_len;
1522                         memcpy(gen_ie->ie_data, priv->wpa_ie, gen_ie->len);
1523                 } else {
1524                         mwifiex_set_gen_ie_helper(priv, gen_ie->ie_data,
1525                                                   (u16) gen_ie->len);
1526                 }
1527                 break;
1528         case MWIFIEX_IE_TYPE_ARP_FILTER:
1529                 memset(adapter->arp_filter, 0, sizeof(adapter->arp_filter));
1530                 if (gen_ie->len > ARP_FILTER_MAX_BUF_SIZE) {
1531                         adapter->arp_filter_size = 0;
1532                         dev_err(adapter->dev, "invalid ARP filter size\n");
1533                         return -1;
1534                 } else {
1535                         memcpy(adapter->arp_filter, gen_ie->ie_data,
1536                                                                 gen_ie->len);
1537                         adapter->arp_filter_size = gen_ie->len;
1538                 }
1539                 break;
1540         default:
1541                 dev_err(adapter->dev, "invalid IE type\n");
1542                 return -1;
1543         }
1544         return 0;
1545 }
1546
1547 /*
1548  * Sends IOCTL request to set a generic IE.
1549  *
1550  * This function allocates the IOCTL request buffer, fills it
1551  * with requisite parameters and calls the IOCTL handler.
1552  */
1553 int
1554 mwifiex_set_gen_ie(struct mwifiex_private *priv, u8 *ie, int ie_len)
1555 {
1556         struct mwifiex_ds_misc_gen_ie gen_ie;
1557
1558         if (ie_len > IEEE_MAX_IE_SIZE)
1559                 return -EFAULT;
1560
1561         gen_ie.type = MWIFIEX_IE_TYPE_GEN_IE;
1562         gen_ie.len = ie_len;
1563         memcpy(gen_ie.ie_data, ie, ie_len);
1564         if (mwifiex_misc_ioctl_gen_ie(priv, &gen_ie, HostCmd_ACT_GEN_SET))
1565                 return -EFAULT;
1566
1567         return 0;
1568 }