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