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