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