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