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