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