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