]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/mwifiex/sta_ioctl.c
mwifiex: remove unnecessary _set_auth functions
[~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 static int
37 mwifiex_copy_mcast_addr(struct mwifiex_multicast_list *mlist,
38                         struct net_device *dev)
39 {
40         int i = 0;
41         struct netdev_hw_addr *ha;
42
43         netdev_for_each_mc_addr(ha, dev)
44                 memcpy(&mlist->mac_list[i++], ha->addr, ETH_ALEN);
45
46         return i;
47 }
48
49 /*
50  * Allocate and fills a wait queue with proper parameters.
51  *
52  * This function needs to be called before an IOCTL request can be made.
53  * It can handle the following wait options:
54  *      MWIFIEX_NO_WAIT     - Waiting is disabled
55  *      MWIFIEX_IOCTL_WAIT  - Waiting is done on IOCTL wait queue
56  *      MWIFIEX_CMD_WAIT    - Waiting is done on command wait queue
57  *      MWIFIEX_WSTATS_WAIT - Waiting is done on stats wait queue
58  */
59 struct mwifiex_wait_queue *
60 mwifiex_alloc_fill_wait_queue(struct mwifiex_private *priv,
61                               u8 wait_option)
62 {
63         struct mwifiex_wait_queue *wait = NULL;
64
65         wait = (struct mwifiex_wait_queue *)
66                 kzalloc(sizeof(struct mwifiex_wait_queue), GFP_ATOMIC);
67         if (!wait) {
68                 dev_err(priv->adapter->dev, "%s: fail to alloc buffer\n",
69                                                 __func__);
70                 return wait;
71         }
72
73         wait->bss_index = priv->bss_index;
74
75         switch (wait_option) {
76         case MWIFIEX_NO_WAIT:
77                 wait->enabled = 0;
78                 break;
79         case MWIFIEX_IOCTL_WAIT:
80                 priv->ioctl_wait_q_woken = false;
81                 wait->start_time = jiffies;
82                 wait->wait = &priv->ioctl_wait_q;
83                 wait->condition = &priv->ioctl_wait_q_woken;
84                 wait->enabled = 1;
85                 break;
86         case MWIFIEX_CMD_WAIT:
87                 priv->cmd_wait_q_woken = false;
88                 wait->start_time = jiffies;
89                 wait->wait = &priv->cmd_wait_q;
90                 wait->condition = &priv->cmd_wait_q_woken;
91                 wait->enabled = 1;
92                 break;
93         case MWIFIEX_WSTATS_WAIT:
94                 priv->w_stats_wait_q_woken = false;
95                 wait->start_time = jiffies;
96                 wait->wait = &priv->w_stats_wait_q;
97                 wait->condition = &priv->w_stats_wait_q_woken;
98                 wait->enabled = 1;
99                 break;
100         }
101
102         return wait;
103 }
104
105 /*
106  * Wait queue completion handler.
107  *
108  * This function waits on a particular wait queue.
109  * For NO_WAIT option, it returns immediately. It also cancels the
110  * pending IOCTL request after waking up, in case of errors.
111  */
112 static void
113 mwifiex_wait_ioctl_complete(struct mwifiex_private *priv,
114                             struct mwifiex_wait_queue *wait,
115                             u8 wait_option)
116 {
117         bool cancel_flag = false;
118
119         switch (wait_option) {
120         case MWIFIEX_NO_WAIT:
121                 break;
122         case MWIFIEX_IOCTL_WAIT:
123                 wait_event_interruptible(priv->ioctl_wait_q,
124                                          priv->ioctl_wait_q_woken);
125                 if (!priv->ioctl_wait_q_woken)
126                         cancel_flag = true;
127                 break;
128         case MWIFIEX_CMD_WAIT:
129                 wait_event_interruptible(priv->cmd_wait_q,
130                                          priv->cmd_wait_q_woken);
131                 if (!priv->cmd_wait_q_woken)
132                         cancel_flag = true;
133                 break;
134         case MWIFIEX_WSTATS_WAIT:
135                 wait_event_interruptible(priv->w_stats_wait_q,
136                                          priv->w_stats_wait_q_woken);
137                 if (!priv->w_stats_wait_q_woken)
138                         cancel_flag = true;
139                 break;
140         }
141         if (cancel_flag) {
142                 mwifiex_cancel_pending_ioctl(priv->adapter, wait);
143                 dev_dbg(priv->adapter->dev, "cmd: IOCTL cancel: wait=%p, wait_option=%d\n",
144                         wait, wait_option);
145         }
146
147         return;
148 }
149
150 /*
151  * The function waits for the request to complete and issues the
152  * completion handler, if required.
153  */
154 int mwifiex_request_ioctl(struct mwifiex_private *priv,
155                           struct mwifiex_wait_queue *wait,
156                           int status, u8 wait_option)
157 {
158         switch (status) {
159         case -EINPROGRESS:
160                 dev_dbg(priv->adapter->dev, "cmd: IOCTL pending: wait=%p, wait_option=%d\n",
161                                 wait, wait_option);
162                 atomic_inc(&priv->adapter->ioctl_pending);
163                 /* Status pending, wake up main process */
164                 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
165
166                 /* Wait for completion */
167                 if (wait_option) {
168                         mwifiex_wait_ioctl_complete(priv, wait, wait_option);
169                         status = wait->status;
170                 }
171                 break;
172         case 0:
173         case -1:
174         case -EBUSY:
175         default:
176                 break;
177         }
178         return status;
179 }
180 EXPORT_SYMBOL_GPL(mwifiex_request_ioctl);
181
182 /*
183  * IOCTL request handler to set/get MAC address.
184  *
185  * This function prepares the correct firmware command and
186  * issues it to get the extended version information.
187  */
188 static int mwifiex_bss_ioctl_mac_address(struct mwifiex_private *priv,
189                                          struct mwifiex_wait_queue *wait,
190                                          u8 action, u8 *mac)
191 {
192         int ret = 0;
193
194         if ((action == HostCmd_ACT_GEN_GET) && mac) {
195                 memcpy(mac, priv->curr_addr, ETH_ALEN);
196                 return 0;
197         }
198
199         /* Send request to firmware */
200         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
201                                   action, 0, wait, mac);
202         if (!ret)
203                 ret = -EINPROGRESS;
204
205         return ret;
206 }
207
208 /*
209  * Sends IOCTL request to set MAC address.
210  *
211  * This function allocates the IOCTL request buffer, fills it
212  * with requisite parameters and calls the IOCTL handler.
213  */
214 int mwifiex_request_set_mac_address(struct mwifiex_private *priv)
215 {
216         struct mwifiex_wait_queue *wait = NULL;
217         int status = 0;
218         u8 wait_option = MWIFIEX_CMD_WAIT;
219
220         /* Allocate wait buffer */
221         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
222         if (!wait)
223                 return -ENOMEM;
224
225         status = mwifiex_bss_ioctl_mac_address(priv, wait, HostCmd_ACT_GEN_SET,
226                                                NULL);
227
228         status = mwifiex_request_ioctl(priv, wait, status, wait_option);
229         if (!status)
230                 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
231         else
232                 dev_err(priv->adapter->dev, "set mac address failed: status=%d"
233                                 " error_code=%#x\n", status, wait->status);
234
235         kfree(wait);
236         return status;
237 }
238
239 /*
240  * IOCTL request handler to set multicast list.
241  *
242  * This function prepares the correct firmware command and
243  * issues it to set the multicast list.
244  *
245  * This function can be used to enable promiscuous mode, or enable all
246  * multicast packets, or to enable selective multicast.
247  */
248 static int
249 mwifiex_bss_ioctl_multicast_list(struct mwifiex_private *priv,
250                                  struct mwifiex_wait_queue *wait,
251                                  u16 action,
252                                  struct mwifiex_multicast_list *mcast_list)
253 {
254         int ret = 0;
255         u16 old_pkt_filter;
256
257         old_pkt_filter = priv->curr_pkt_filter;
258         if (action == HostCmd_ACT_GEN_GET)
259                 return -1;
260
261         if (mcast_list->mode == MWIFIEX_PROMISC_MODE) {
262                 dev_dbg(priv->adapter->dev, "info: Enable Promiscuous mode\n");
263                 priv->curr_pkt_filter |= HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
264                 priv->curr_pkt_filter &=
265                         ~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
266         } else {
267                 /* Multicast */
268                 priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
269                 if (mcast_list->mode == MWIFIEX_MULTICAST_MODE) {
270                         dev_dbg(priv->adapter->dev,
271                                 "info: Enabling All Multicast!\n");
272                         priv->curr_pkt_filter |=
273                                 HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
274                 } else {
275                         priv->curr_pkt_filter &=
276                                 ~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
277                         if (mcast_list->num_multicast_addr) {
278                                 dev_dbg(priv->adapter->dev,
279                                         "info: Set multicast list=%d\n",
280                                        mcast_list->num_multicast_addr);
281                                 /* Set multicast addresses to firmware */
282                                 if (old_pkt_filter == priv->curr_pkt_filter) {
283                                         /* Send request to firmware */
284                                         ret = mwifiex_prepare_cmd(priv,
285                                                 HostCmd_CMD_MAC_MULTICAST_ADR,
286                                                 action, 0, wait, mcast_list);
287                                         if (!ret)
288                                                 ret = -EINPROGRESS;
289                                 } else {
290                                         /* Send request to firmware */
291                                         ret = mwifiex_prepare_cmd(priv,
292                                                 HostCmd_CMD_MAC_MULTICAST_ADR,
293                                                 action, 0, NULL,
294                                                 mcast_list);
295                                 }
296                         }
297                 }
298         }
299         dev_dbg(priv->adapter->dev,
300                 "info: old_pkt_filter=%#x, curr_pkt_filter=%#x\n",
301                old_pkt_filter, priv->curr_pkt_filter);
302         if (old_pkt_filter != priv->curr_pkt_filter) {
303                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL, action,
304                                           0, wait, &priv->curr_pkt_filter);
305                 if (!ret)
306                         ret = -EINPROGRESS;
307         }
308
309         return ret;
310 }
311
312 /*
313  * Sends IOCTL request to set multicast list.
314  *
315  * This function allocates the IOCTL request buffer, fills it
316  * with requisite parameters and calls the IOCTL handler.
317  */
318 void
319 mwifiex_request_set_multicast_list(struct mwifiex_private *priv,
320                                    struct net_device *dev)
321 {
322         struct mwifiex_wait_queue *wait = NULL;
323         struct mwifiex_multicast_list mcast_list;
324         u8 wait_option = MWIFIEX_NO_WAIT;
325         int status = 0;
326
327         /* Allocate wait buffer */
328         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
329         if (!wait)
330                 return;
331
332         if (dev->flags & IFF_PROMISC) {
333                 mcast_list.mode = MWIFIEX_PROMISC_MODE;
334         } else if (dev->flags & IFF_ALLMULTI ||
335                    netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
336                 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
337         } else {
338                 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
339                 if (netdev_mc_count(dev))
340                         mcast_list.num_multicast_addr =
341                                 mwifiex_copy_mcast_addr(&mcast_list, dev);
342         }
343         status = mwifiex_bss_ioctl_multicast_list(priv, wait,
344                                                   HostCmd_ACT_GEN_SET,
345                                                   &mcast_list);
346
347         status = mwifiex_request_ioctl(priv, wait, status, wait_option);
348         if (wait && status != -EINPROGRESS)
349                 kfree(wait);
350
351         return;
352 }
353
354 /*
355  * IOCTL request handler to disconnect from a BSS/IBSS.
356  */
357 static int mwifiex_bss_ioctl_stop(struct mwifiex_private *priv,
358                                   struct mwifiex_wait_queue *wait, u8 *mac)
359 {
360         return mwifiex_deauthenticate(priv, wait, mac);
361 }
362
363 /*
364  * Sends IOCTL request to disconnect from a BSS.
365  *
366  * This function allocates the IOCTL request buffer, fills it
367  * with requisite parameters and calls the IOCTL handler.
368  */
369 int mwifiex_disconnect(struct mwifiex_private *priv, u8 wait_option, u8 *mac)
370 {
371         struct mwifiex_wait_queue *wait = NULL;
372         int status = 0;
373
374         /* Allocate wait buffer */
375         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
376         if (!wait)
377                 return -ENOMEM;
378
379         status = mwifiex_bss_ioctl_stop(priv, wait, mac);
380
381         status = mwifiex_request_ioctl(priv, wait, status, wait_option);
382
383         kfree(wait);
384         return status;
385 }
386 EXPORT_SYMBOL_GPL(mwifiex_disconnect);
387
388 /*
389  * IOCTL request handler to join a BSS/IBSS.
390  *
391  * In Ad-Hoc mode, the IBSS is created if not found in scan list.
392  * In both Ad-Hoc and infra mode, an deauthentication is performed
393  * first.
394  */
395 static int mwifiex_bss_ioctl_start(struct mwifiex_private *priv,
396                                    struct mwifiex_wait_queue *wait,
397                                    struct mwifiex_ssid_bssid *ssid_bssid)
398 {
399         int ret = 0;
400         struct mwifiex_adapter *adapter = priv->adapter;
401         s32 i = -1;
402
403         priv->scan_block = false;
404         if (!ssid_bssid)
405                 return -1;
406
407         if (priv->bss_mode == MWIFIEX_BSS_MODE_INFRA) {
408                 /* Infra mode */
409                 ret = mwifiex_deauthenticate(priv, NULL, NULL);
410                 if (ret)
411                         return ret;
412
413                 /* Search for the requested SSID in the scan table */
414                 if (ssid_bssid->ssid.ssid_len)
415                         i = mwifiex_find_ssid_in_list(priv, &ssid_bssid->ssid,
416                                                 NULL, MWIFIEX_BSS_MODE_INFRA);
417                 else
418                         i = mwifiex_find_bssid_in_list(priv,
419                                                 (u8 *) &ssid_bssid->bssid,
420                                                 MWIFIEX_BSS_MODE_INFRA);
421                 if (i < 0)
422                         return -1;
423
424                 dev_dbg(adapter->dev,
425                         "info: SSID found in scan list ... associating...\n");
426
427                 /* Clear any past association response stored for
428                  * application retrieval */
429                 priv->assoc_rsp_size = 0;
430                 ret = mwifiex_associate(priv, wait, &adapter->scan_table[i]);
431                 if (ret)
432                         return ret;
433         } else {
434                 /* Adhoc mode */
435                 /* If the requested SSID matches current SSID, return */
436                 if (ssid_bssid->ssid.ssid_len &&
437                     (!mwifiex_ssid_cmp
438                      (&priv->curr_bss_params.bss_descriptor.ssid,
439                       &ssid_bssid->ssid)))
440                         return 0;
441
442                 /* Exit Adhoc mode first */
443                 dev_dbg(adapter->dev, "info: Sending Adhoc Stop\n");
444                 ret = mwifiex_deauthenticate(priv, NULL, NULL);
445                 if (ret)
446                         return ret;
447
448                 priv->adhoc_is_link_sensed = false;
449
450                 /* Search for the requested network in the scan table */
451                 if (ssid_bssid->ssid.ssid_len)
452                         i = mwifiex_find_ssid_in_list(priv,
453                                                       &ssid_bssid->ssid, NULL,
454                                                       MWIFIEX_BSS_MODE_IBSS);
455                 else
456                         i = mwifiex_find_bssid_in_list(priv,
457                                                        (u8 *)&ssid_bssid->bssid,
458                                                        MWIFIEX_BSS_MODE_IBSS);
459
460                 if (i >= 0) {
461                         dev_dbg(adapter->dev, "info: network found in scan"
462                                                         " list. Joining...\n");
463                         ret = mwifiex_adhoc_join(priv, wait,
464                                                  &adapter->scan_table[i]);
465                         if (ret)
466                                 return ret;
467                 } else {        /* i >= 0 */
468                         dev_dbg(adapter->dev, "info: Network not found in "
469                                 "the list, creating adhoc with ssid = %s\n",
470                                ssid_bssid->ssid.ssid);
471                         ret = mwifiex_adhoc_start(priv, wait,
472                                                   &ssid_bssid->ssid);
473                         if (ret)
474                                 return ret;
475                 }
476         }
477
478         if (!ret)
479                 ret = -EINPROGRESS;
480
481         return ret;
482 }
483
484 /*
485  * Sends IOCTL request to connect with a BSS.
486  *
487  * This function allocates the IOCTL request buffer, fills it
488  * with requisite parameters and calls the IOCTL handler.
489  */
490 int mwifiex_bss_start(struct mwifiex_private *priv, u8 wait_option,
491                       struct mwifiex_ssid_bssid *ssid_bssid)
492 {
493         struct mwifiex_wait_queue *wait = NULL;
494         struct mwifiex_ssid_bssid tmp_ssid_bssid;
495         int status = 0;
496
497         /* Stop the O.S. TX queue if needed */
498         if (!netif_queue_stopped(priv->netdev))
499                 netif_stop_queue(priv->netdev);
500
501         /* Allocate wait buffer */
502         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
503         if (!wait)
504                 return -ENOMEM;
505
506         if (ssid_bssid)
507                 memcpy(&tmp_ssid_bssid, ssid_bssid,
508                        sizeof(struct mwifiex_ssid_bssid));
509         status = mwifiex_bss_ioctl_start(priv, wait, &tmp_ssid_bssid);
510
511         status = mwifiex_request_ioctl(priv, wait, status, wait_option);
512
513         kfree(wait);
514         return status;
515 }
516
517 /*
518  * IOCTL request handler to set host sleep configuration.
519  *
520  * This function prepares the correct firmware command and
521  * issues it.
522  */
523 static int
524 mwifiex_pm_ioctl_hs_cfg(struct mwifiex_private *priv,
525                         struct mwifiex_wait_queue *wait,
526                         u16 action, struct mwifiex_ds_hs_cfg *hs_cfg)
527 {
528         struct mwifiex_adapter *adapter = priv->adapter;
529         int status = 0;
530         u32 prev_cond = 0;
531
532         switch (action) {
533         case HostCmd_ACT_GEN_SET:
534                 if (adapter->pps_uapsd_mode) {
535                         dev_dbg(adapter->dev, "info: Host Sleep IOCTL"
536                                 " is blocked in UAPSD/PPS mode\n");
537                         status = -1;
538                         break;
539                 }
540                 if (hs_cfg->is_invoke_hostcmd) {
541                         if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL) {
542                                 if (!adapter->is_hs_configured)
543                                         /* Already cancelled */
544                                         break;
545                                 /* Save previous condition */
546                                 prev_cond = le32_to_cpu(adapter->hs_cfg
547                                                         .conditions);
548                                 adapter->hs_cfg.conditions =
549                                                 cpu_to_le32(hs_cfg->conditions);
550                         } else if (hs_cfg->conditions) {
551                                 adapter->hs_cfg.conditions =
552                                                 cpu_to_le32(hs_cfg->conditions);
553                                 adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
554                                 if (hs_cfg->gap)
555                                         adapter->hs_cfg.gap = (u8)hs_cfg->gap;
556                         } else if (adapter->hs_cfg.conditions ==
557                                                 cpu_to_le32(
558                                                 HOST_SLEEP_CFG_CANCEL)) {
559                                 /* Return failure if no parameters for HS
560                                    enable */
561                                 status = -1;
562                                 break;
563                         }
564                         status = mwifiex_prepare_cmd(priv,
565                                         HostCmd_CMD_802_11_HS_CFG_ENH,
566                                         HostCmd_ACT_GEN_SET,
567                                         0, wait, &adapter->hs_cfg);
568                         if (!status)
569                                 status = -EINPROGRESS;
570                         if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL)
571                                 /* Restore previous condition */
572                                 adapter->hs_cfg.conditions =
573                                                 cpu_to_le32(prev_cond);
574                 } else {
575                         adapter->hs_cfg.conditions =
576                                 cpu_to_le32(hs_cfg->conditions);
577                         adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
578                         adapter->hs_cfg.gap = (u8)hs_cfg->gap;
579                 }
580                 break;
581         case HostCmd_ACT_GEN_GET:
582                 hs_cfg->conditions = le32_to_cpu(adapter->hs_cfg.conditions);
583                 hs_cfg->gpio = adapter->hs_cfg.gpio;
584                 hs_cfg->gap = adapter->hs_cfg.gap;
585                 break;
586         default:
587                 status = -1;
588                 break;
589         }
590
591         return status;
592 }
593
594 /*
595  * Sends IOCTL request to set Host Sleep parameters.
596  *
597  * This function allocates the IOCTL request buffer, fills it
598  * with requisite parameters and calls the IOCTL handler.
599  */
600 int mwifiex_set_hs_params(struct mwifiex_private *priv, u16 action,
601                               u8 wait_option,
602                               struct mwifiex_ds_hs_cfg *hscfg)
603 {
604         int ret = 0;
605         struct mwifiex_wait_queue *wait = NULL;
606
607         if (!hscfg)
608                 return -ENOMEM;
609
610         /* Allocate wait buffer */
611         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
612         if (!wait)
613                 return -ENOMEM;
614
615         ret = mwifiex_pm_ioctl_hs_cfg(priv, wait, action, hscfg);
616
617         ret = mwifiex_request_ioctl(priv, wait, ret, wait_option);
618
619         if (wait && (ret != -EINPROGRESS))
620                 kfree(wait);
621         return ret;
622 }
623
624 /*
625  * Sends IOCTL request to cancel the existing Host Sleep configuration.
626  *
627  * This function allocates the IOCTL request buffer, fills it
628  * with requisite parameters and calls the IOCTL handler.
629  */
630 int mwifiex_cancel_hs(struct mwifiex_private *priv, u8 wait_option)
631 {
632         int ret = 0;
633         struct mwifiex_ds_hs_cfg hscfg;
634
635         /* Cancel Host Sleep */
636         hscfg.conditions = HOST_SLEEP_CFG_CANCEL;
637         hscfg.is_invoke_hostcmd = true;
638         ret = mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
639                                         wait_option, &hscfg);
640
641         return ret;
642 }
643 EXPORT_SYMBOL_GPL(mwifiex_cancel_hs);
644
645 /*
646  * Sends IOCTL request to cancel the existing Host Sleep configuration.
647  *
648  * This function allocates the IOCTL request buffer, fills it
649  * with requisite parameters and calls the IOCTL handler.
650  */
651 int mwifiex_enable_hs(struct mwifiex_adapter *adapter)
652 {
653         struct mwifiex_ds_hs_cfg hscfg;
654
655         if (adapter->hs_activated) {
656                 dev_dbg(adapter->dev, "cmd: HS Already actived\n");
657                 return true;
658         }
659
660         /* Enable Host Sleep */
661         adapter->hs_activate_wait_q_woken = false;
662
663         memset(&hscfg, 0, sizeof(struct mwifiex_hs_config_param));
664         hscfg.is_invoke_hostcmd = true;
665
666         if (mwifiex_set_hs_params(mwifiex_get_priv(adapter,
667                                                        MWIFIEX_BSS_ROLE_STA),
668                                       HostCmd_ACT_GEN_SET,
669                                       MWIFIEX_IOCTL_WAIT, &hscfg)) {
670                 dev_err(adapter->dev, "IOCTL request HS enable failed\n");
671                 return false;
672         }
673
674         wait_event_interruptible(adapter->hs_activate_wait_q,
675                         adapter->hs_activate_wait_q_woken);
676
677         return true;
678 }
679 EXPORT_SYMBOL_GPL(mwifiex_enable_hs);
680
681 /*
682  * IOCTL request handler to get signal information.
683  *
684  * This function prepares the correct firmware command and
685  * issues it to get the signal (RSSI) information.
686  *
687  * This only works in the connected mode.
688  */
689 static int mwifiex_get_info_signal(struct mwifiex_private *priv,
690                                    struct mwifiex_wait_queue *wait,
691                                    struct mwifiex_ds_get_signal *signal)
692 {
693         int ret = 0;
694
695         if (!wait) {
696                 dev_err(priv->adapter->dev, "WAIT information is not present\n");
697                 return -1;
698         }
699
700         /* Signal info can be obtained only if connected */
701         if (!priv->media_connected) {
702                 dev_dbg(priv->adapter->dev,
703                         "info: Can not get signal in disconnected state\n");
704                 return -1;
705         }
706
707         /* Send request to firmware */
708         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_RSSI_INFO,
709                                   HostCmd_ACT_GEN_GET, 0, wait, signal);
710
711         if (!ret)
712                 ret = -EINPROGRESS;
713
714         return ret;
715 }
716
717 /*
718  * IOCTL request handler to get statistics.
719  *
720  * This function prepares the correct firmware command and
721  * issues it to get the statistics (RSSI) information.
722  */
723 static int mwifiex_get_info_stats(struct mwifiex_private *priv,
724                            struct mwifiex_wait_queue *wait,
725                            struct mwifiex_ds_get_stats *log)
726 {
727         int ret = 0;
728
729         if (!wait) {
730                 dev_err(priv->adapter->dev, "MWIFIEX IOCTL information is not present\n");
731                 return -1;
732         }
733
734         /* Send request to firmware */
735         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_GET_LOG,
736                                   HostCmd_ACT_GEN_GET, 0, wait, log);
737
738         if (!ret)
739                 ret = -EINPROGRESS;
740
741         return ret;
742 }
743
744 /*
745  * IOCTL request handler to get BSS information.
746  *
747  * This function collates the information from different driver structures
748  * to send to the user.
749  */
750 int mwifiex_get_bss_info(struct mwifiex_private *priv,
751                          struct mwifiex_bss_info *info)
752 {
753         struct mwifiex_adapter *adapter = priv->adapter;
754         struct mwifiex_bssdescriptor *bss_desc;
755         s32 tbl_idx = 0;
756
757         if (!info)
758                 return -1;
759
760         /* Get current BSS info */
761         bss_desc = &priv->curr_bss_params.bss_descriptor;
762
763         /* BSS mode */
764         info->bss_mode = priv->bss_mode;
765
766         /* SSID */
767         memcpy(&info->ssid, &bss_desc->ssid,
768                sizeof(struct mwifiex_802_11_ssid));
769
770         /* BSSID */
771         memcpy(&info->bssid, &bss_desc->mac_address, ETH_ALEN);
772
773         /* Channel */
774         info->bss_chan = bss_desc->channel;
775
776         /* Region code */
777         info->region_code = adapter->region_code;
778
779         /* Scan table index if connected */
780         info->scan_table_idx = 0;
781         if (priv->media_connected) {
782                 tbl_idx =
783                         mwifiex_find_ssid_in_list(priv, &bss_desc->ssid,
784                                                   bss_desc->mac_address,
785                                                   priv->bss_mode);
786                 if (tbl_idx >= 0)
787                         info->scan_table_idx = tbl_idx;
788         }
789
790         /* Connection status */
791         info->media_connected = priv->media_connected;
792
793         /* Radio status */
794         info->radio_on = adapter->radio_on;
795
796         /* Tx power information */
797         info->max_power_level = priv->max_tx_power_level;
798         info->min_power_level = priv->min_tx_power_level;
799
800         /* AdHoc state */
801         info->adhoc_state = priv->adhoc_state;
802
803         /* Last beacon NF */
804         info->bcn_nf_last = priv->bcn_nf_last;
805
806         /* wep status */
807         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED)
808                 info->wep_status = true;
809         else
810                 info->wep_status = false;
811
812         info->is_hs_configured = adapter->is_hs_configured;
813         info->is_deep_sleep = adapter->is_deep_sleep;
814
815         return 0;
816 }
817
818 /*
819  * IOCTL request handler to get extended version information.
820  *
821  * This function prepares the correct firmware command and
822  * issues it to get the extended version information.
823  */
824 static int mwifiex_get_info_ver_ext(struct mwifiex_private *priv,
825                                     struct mwifiex_wait_queue *wait,
826                                     struct mwifiex_ver_ext *ver_ext)
827 {
828         int ret = 0;
829
830         /* Send request to firmware */
831         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_VERSION_EXT,
832                                   HostCmd_ACT_GEN_GET, 0, wait, ver_ext);
833         if (!ret)
834                 ret = -EINPROGRESS;
835
836         return ret;
837 }
838
839 /*
840  * IOCTL request handler to set/get SNMP MIB parameters.
841  *
842  * This function prepares the correct firmware command and
843  * issues it.
844  *
845  * Currently the following parameters are supported -
846  *      Set/get RTS Threshold
847  *      Set/get fragmentation threshold
848  *      Set/get retry count
849  */
850 int mwifiex_snmp_mib_ioctl(struct mwifiex_private *priv,
851                            struct mwifiex_wait_queue *wait,
852                            u32 cmd_oid, u16 action, u32 *value)
853 {
854         int ret = 0;
855
856         if (!value)
857                 return -1;
858
859         /* Send request to firmware */
860         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
861                                   action, cmd_oid, wait, value);
862
863         if (!ret)
864                 ret = -EINPROGRESS;
865
866         return ret;
867 }
868
869 /*
870  * IOCTL request handler to set/get band configurations.
871  *
872  * For SET operation, it performs extra checks to make sure the Ad-Hoc
873  * band and channel are compatible. Otherwise it returns an error.
874  *
875  * For GET operation, this function retrieves the following information -
876  *      - Infra bands
877  *      - Ad-hoc band
878  *      - Ad-hoc channel
879  *      - Secondary channel offset
880  */
881 int mwifiex_radio_ioctl_band_cfg(struct mwifiex_private *priv,
882                                  u16 action,
883                                  struct mwifiex_ds_band_cfg *radio_cfg)
884 {
885         struct mwifiex_adapter *adapter = priv->adapter;
886         u8 infra_band = 0;
887         u8 adhoc_band = 0;
888         u32 adhoc_channel = 0;
889
890         if (action == HostCmd_ACT_GEN_GET) {
891                 /* Infra Bands */
892                 radio_cfg->config_bands = adapter->config_bands;
893                 /* Adhoc Band */
894                 radio_cfg->adhoc_start_band = adapter->adhoc_start_band;
895                 /* Adhoc channel */
896                 radio_cfg->adhoc_channel = priv->adhoc_channel;
897                 /* Secondary channel offset */
898                 radio_cfg->sec_chan_offset = adapter->chan_offset;
899                 return 0;
900         }
901
902         /* For action = SET */
903         infra_band = (u8) radio_cfg->config_bands;
904         adhoc_band = (u8) radio_cfg->adhoc_start_band;
905         adhoc_channel = radio_cfg->adhoc_channel;
906
907         /* SET Infra band */
908         if ((infra_band | adapter->fw_bands) & ~adapter->fw_bands)
909                 return -1;
910
911         adapter->config_bands = infra_band;
912
913         /* SET Ad-hoc Band */
914         if ((adhoc_band | adapter->fw_bands) & ~adapter->fw_bands)
915                 return -1;
916
917         if (adhoc_band)
918                 adapter->adhoc_start_band = adhoc_band;
919         adapter->chan_offset = (u8) radio_cfg->sec_chan_offset;
920         /*
921          * If no adhoc_channel is supplied verify if the existing adhoc
922          * channel compiles with new adhoc_band
923          */
924         if (!adhoc_channel) {
925                 if (!mwifiex_get_cfp_by_band_and_channel_from_cfg80211
926                      (priv, adapter->adhoc_start_band,
927                      priv->adhoc_channel)) {
928                         /* Pass back the default channel */
929                         radio_cfg->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
930                         if ((adapter->adhoc_start_band & BAND_A)
931                             || (adapter->adhoc_start_band & BAND_AN))
932                                 radio_cfg->adhoc_channel =
933                                         DEFAULT_AD_HOC_CHANNEL_A;
934                 }
935         } else {        /* Retrurn error if adhoc_band and
936                            adhoc_channel combination is invalid */
937                 if (!mwifiex_get_cfp_by_band_and_channel_from_cfg80211
938                     (priv, adapter->adhoc_start_band, (u16) adhoc_channel))
939                         return -1;
940                 priv->adhoc_channel = (u8) adhoc_channel;
941         }
942         if ((adhoc_band & BAND_GN) || (adhoc_band & BAND_AN))
943                 adapter->adhoc_11n_enabled = true;
944         else
945                 adapter->adhoc_11n_enabled = false;
946
947         return 0;
948 }
949
950 /*
951  * IOCTL request handler to set/get active channel.
952  *
953  * This function performs validity checking on channel/frequency
954  * compatibility and returns failure if not valid.
955  */
956 int mwifiex_bss_ioctl_channel(struct mwifiex_private *priv, u16 action,
957                               struct mwifiex_chan_freq_power *chan)
958 {
959         struct mwifiex_adapter *adapter = priv->adapter;
960         struct mwifiex_chan_freq_power *cfp = NULL;
961
962         if (!chan)
963                 return -1;
964
965         if (action == HostCmd_ACT_GEN_GET) {
966                 cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211(priv,
967                                 priv->curr_bss_params.band,
968                                 (u16) priv->curr_bss_params.bss_descriptor.
969                                         channel);
970                 chan->channel = cfp->channel;
971                 chan->freq = cfp->freq;
972
973                 return 0;
974         }
975         if (!chan->channel && !chan->freq)
976                 return -1;
977         if (adapter->adhoc_start_band & BAND_AN)
978                 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
979         else if (adapter->adhoc_start_band & BAND_A)
980                 adapter->adhoc_start_band = BAND_G | BAND_B;
981         if (chan->channel) {
982                 if (chan->channel <= MAX_CHANNEL_BAND_BG)
983                         cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211
984                                         (priv, 0, (u16) chan->channel);
985                 if (!cfp) {
986                         cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211
987                                         (priv, BAND_A, (u16) chan->channel);
988                         if (cfp) {
989                                 if (adapter->adhoc_11n_enabled)
990                                         adapter->adhoc_start_band = BAND_A
991                                                 | BAND_AN;
992                                 else
993                                         adapter->adhoc_start_band = BAND_A;
994                         }
995                 }
996         } else {
997                 if (chan->freq <= MAX_FREQUENCY_BAND_BG)
998                         cfp = mwifiex_get_cfp_by_band_and_freq_from_cfg80211(
999                                                         priv, 0, chan->freq);
1000                 if (!cfp) {
1001                         cfp = mwifiex_get_cfp_by_band_and_freq_from_cfg80211
1002                                                   (priv, BAND_A, chan->freq);
1003                         if (cfp) {
1004                                 if (adapter->adhoc_11n_enabled)
1005                                         adapter->adhoc_start_band = BAND_A
1006                                                 | BAND_AN;
1007                                 else
1008                                         adapter->adhoc_start_band = BAND_A;
1009                         }
1010                 }
1011         }
1012         if (!cfp || !cfp->channel) {
1013                 dev_err(adapter->dev, "invalid channel/freq\n");
1014                 return -1;
1015         }
1016         priv->adhoc_channel = (u8) cfp->channel;
1017         chan->channel = cfp->channel;
1018         chan->freq = cfp->freq;
1019
1020         return 0;
1021 }
1022
1023 /*
1024  * IOCTL request handler to set/get BSS mode.
1025  *
1026  * This function prepares the correct firmware command and
1027  * issues it to set or get the BSS mode.
1028  *
1029  * In case the mode is changed, a deauthentication is performed
1030  * first by the function automatically.
1031  */
1032 int mwifiex_bss_ioctl_mode(struct mwifiex_private *priv,
1033                            struct mwifiex_wait_queue *wait,
1034                            u16 action, int *mode)
1035 {
1036         int ret = 0;
1037
1038         if (!mode)
1039                 return -1;
1040
1041         if (action == HostCmd_ACT_GEN_GET) {
1042                 *mode = priv->bss_mode;
1043                 return 0;
1044         }
1045
1046         if ((priv->bss_mode == *mode) || (*mode == MWIFIEX_BSS_MODE_AUTO)) {
1047                 dev_dbg(priv->adapter->dev,
1048                         "info: Already set to required mode! No change!\n");
1049                 priv->bss_mode = *mode;
1050                 return 0;
1051         }
1052
1053         ret = mwifiex_deauthenticate(priv, wait, NULL);
1054
1055         priv->sec_info.authentication_mode = MWIFIEX_AUTH_MODE_OPEN;
1056         priv->bss_mode = *mode;
1057         if (priv->bss_mode != MWIFIEX_BSS_MODE_AUTO) {
1058                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1059                                           HostCmd_ACT_GEN_SET, 0, wait, NULL);
1060                 if (!ret)
1061                         ret = -EINPROGRESS;
1062         }
1063
1064         return ret;
1065 }
1066
1067 /*
1068  * IOCTL request handler to set/get Ad-Hoc channel.
1069  *
1070  * This function prepares the correct firmware command and
1071  * issues it to set or get the ad-hoc channel.
1072  */
1073 static int mwifiex_bss_ioctl_ibss_channel(struct mwifiex_private *priv,
1074                                           struct mwifiex_wait_queue *wait,
1075                                           u16 action, u16 *channel)
1076 {
1077         int ret = 0;
1078
1079         if (action == HostCmd_ACT_GEN_GET) {
1080                 if (!priv->media_connected) {
1081                         *channel = priv->adhoc_channel;
1082                         return ret;
1083                 }
1084         } else {
1085                 priv->adhoc_channel = (u8) *channel;
1086         }
1087
1088         /* Send request to firmware */
1089         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_RF_CHANNEL,
1090                                   action, 0, wait, channel);
1091         if (!ret)
1092                 ret = -EINPROGRESS;
1093
1094         return ret;
1095 }
1096
1097 /*
1098  * IOCTL request handler to find a particular BSS.
1099  *
1100  * The BSS can be searched with either a BSSID or a SSID. If none of
1101  * these are provided, just the best BSS (best RSSI) is returned.
1102  */
1103 int mwifiex_bss_ioctl_find_bss(struct mwifiex_private *priv,
1104                                struct mwifiex_wait_queue *wait,
1105                                struct mwifiex_ssid_bssid *ssid_bssid)
1106 {
1107         struct mwifiex_adapter *adapter = priv->adapter;
1108         int ret = 0;
1109         struct mwifiex_bssdescriptor *bss_desc;
1110         u8 zero_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
1111         u8 mac[ETH_ALEN];
1112         int i = 0;
1113
1114         if (memcmp(ssid_bssid->bssid, zero_mac, sizeof(zero_mac))) {
1115                 i = mwifiex_find_bssid_in_list(priv,
1116                                                (u8 *) ssid_bssid->bssid,
1117                                                priv->bss_mode);
1118                 if (i < 0) {
1119                         memcpy(mac, ssid_bssid->bssid, sizeof(mac));
1120                         dev_err(adapter->dev, "cannot find bssid %pM\n", mac);
1121                         return -1;
1122                 }
1123                 bss_desc = &adapter->scan_table[i];
1124                 memcpy(&ssid_bssid->ssid, &bss_desc->ssid,
1125                                 sizeof(struct mwifiex_802_11_ssid));
1126         } else if (ssid_bssid->ssid.ssid_len) {
1127                 i = mwifiex_find_ssid_in_list(priv, &ssid_bssid->ssid, NULL,
1128                                               priv->bss_mode);
1129                 if (i < 0) {
1130                         dev_err(adapter->dev, "cannot find ssid %s\n",
1131                                         ssid_bssid->ssid.ssid);
1132                         return -1;
1133                 }
1134                 bss_desc = &adapter->scan_table[i];
1135                 memcpy(ssid_bssid->bssid, bss_desc->mac_address, ETH_ALEN);
1136         } else {
1137                 ret = mwifiex_find_best_network(priv, ssid_bssid);
1138         }
1139
1140         return ret;
1141 }
1142
1143 /*
1144  * IOCTL request handler to change Ad-Hoc channel.
1145  *
1146  * This function allocates the IOCTL request buffer, fills it
1147  * with requisite parameters and calls the IOCTL handler.
1148  *
1149  * The function follows the following steps to perform the change -
1150  *      - Get current IBSS information
1151  *      - Get current channel
1152  *      - If no change is required, return
1153  *      - If not connected, change channel and return
1154  *      - If connected,
1155  *          - Disconnect
1156  *          - Change channel
1157  *          - Perform specific SSID scan with same SSID
1158  *          - Start/Join the IBSS
1159  */
1160 int
1161 mwifiex_drv_change_adhoc_chan(struct mwifiex_private *priv, int channel)
1162 {
1163         int ret = 0;
1164         int status = 0;
1165         struct mwifiex_bss_info bss_info;
1166         struct mwifiex_wait_queue *wait = NULL;
1167         u8 wait_option = MWIFIEX_IOCTL_WAIT;
1168         struct mwifiex_ssid_bssid ssid_bssid;
1169         u16 curr_chan = 0;
1170
1171         memset(&bss_info, 0, sizeof(bss_info));
1172
1173         /* Get BSS information */
1174         if (mwifiex_get_bss_info(priv, &bss_info))
1175                 return -1;
1176
1177         /* Allocate wait buffer */
1178         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
1179         if (!wait)
1180                 return -ENOMEM;
1181
1182         /* Get current channel */
1183         status = mwifiex_bss_ioctl_ibss_channel(priv, wait, HostCmd_ACT_GEN_GET,
1184                                                 &curr_chan);
1185
1186         if (mwifiex_request_ioctl(priv, wait, status, wait_option)) {
1187                 ret = -1;
1188                 goto done;
1189         }
1190         if (curr_chan == channel) {
1191                 ret = 0;
1192                 goto done;
1193         }
1194         dev_dbg(priv->adapter->dev, "cmd: updating channel from %d to %d\n",
1195                         curr_chan, channel);
1196
1197         if (!bss_info.media_connected) {
1198                 ret = 0;
1199                 goto done;
1200         }
1201
1202         /* Do disonnect */
1203         memset(&ssid_bssid, 0, ETH_ALEN);
1204         status = mwifiex_bss_ioctl_stop(priv, wait, ssid_bssid.bssid);
1205
1206         if (mwifiex_request_ioctl(priv, wait, status, wait_option)) {
1207                 ret = -1;
1208                 goto done;
1209         }
1210
1211         status = mwifiex_bss_ioctl_ibss_channel(priv, wait, HostCmd_ACT_GEN_SET,
1212                                                 (u16 *) &channel);
1213
1214         if (mwifiex_request_ioctl(priv, wait, status, wait_option)) {
1215                 ret = -1;
1216                 goto done;
1217         }
1218
1219         /* Do specific SSID scanning */
1220         if (mwifiex_request_scan(priv, wait_option, &bss_info.ssid)) {
1221                 ret = -1;
1222                 goto done;
1223         }
1224         /* Start/Join Adhoc network */
1225         memset(&ssid_bssid, 0, sizeof(struct mwifiex_ssid_bssid));
1226         memcpy(&ssid_bssid.ssid, &bss_info.ssid,
1227                sizeof(struct mwifiex_802_11_ssid));
1228
1229         status = mwifiex_bss_ioctl_start(priv, wait, &ssid_bssid);
1230
1231         if (mwifiex_request_ioctl(priv, wait, status, wait_option))
1232                 ret = -1;
1233
1234 done:
1235         kfree(wait);
1236         return ret;
1237 }
1238
1239 /*
1240  * IOCTL request handler to get current driver mode.
1241  *
1242  * This function allocates the IOCTL request buffer, fills it
1243  * with requisite parameters and calls the IOCTL handler.
1244  */
1245 int
1246 mwifiex_drv_get_mode(struct mwifiex_private *priv, u8 wait_option)
1247 {
1248         struct mwifiex_wait_queue *wait = NULL;
1249         int status = 0;
1250         int mode = -1;
1251
1252         /* Allocate wait buffer */
1253         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
1254         if (!wait)
1255                 return -1;
1256
1257         status = mwifiex_bss_ioctl_mode(priv, wait, HostCmd_ACT_GEN_GET, &mode);
1258
1259         status = mwifiex_request_ioctl(priv, wait, status, wait_option);
1260
1261         if (wait && (status != -EINPROGRESS))
1262                 kfree(wait);
1263         return mode;
1264 }
1265
1266 /*
1267  * IOCTL request handler to get rate.
1268  *
1269  * This function prepares the correct firmware command and
1270  * issues it to get the current rate if it is connected,
1271  * otherwise, the function returns the lowest supported rate
1272  * for the band.
1273  */
1274 static int mwifiex_rate_ioctl_get_rate_value(struct mwifiex_private *priv,
1275                                              struct mwifiex_wait_queue *wait,
1276                                              struct mwifiex_rate_cfg *rate_cfg)
1277 {
1278         struct mwifiex_adapter *adapter = priv->adapter;
1279         int ret = 0;
1280
1281         rate_cfg->is_rate_auto = priv->is_data_rate_auto;
1282         if (!priv->media_connected) {
1283                 switch (adapter->config_bands) {
1284                 case BAND_B:
1285                         /* Return the lowest supported rate for B band */
1286                         rate_cfg->rate = supported_rates_b[0] & 0x7f;
1287                         break;
1288                 case BAND_G:
1289                 case BAND_G | BAND_GN:
1290                         /* Return the lowest supported rate for G band */
1291                         rate_cfg->rate = supported_rates_g[0] & 0x7f;
1292                         break;
1293                 case BAND_B | BAND_G:
1294                 case BAND_A | BAND_B | BAND_G:
1295                 case BAND_A | BAND_B:
1296                 case BAND_A | BAND_B | BAND_G | BAND_AN | BAND_GN:
1297                 case BAND_B | BAND_G | BAND_GN:
1298                         /* Return the lowest supported rate for BG band */
1299                         rate_cfg->rate = supported_rates_bg[0] & 0x7f;
1300                         break;
1301                 case BAND_A:
1302                 case BAND_A | BAND_G:
1303                 case BAND_A | BAND_G | BAND_AN | BAND_GN:
1304                 case BAND_A | BAND_AN:
1305                         /* Return the lowest supported rate for A band */
1306                         rate_cfg->rate = supported_rates_a[0] & 0x7f;
1307                         break;
1308                 case BAND_GN:
1309                         /* Return the lowest supported rate for N band */
1310                         rate_cfg->rate = supported_rates_n[0] & 0x7f;
1311                         break;
1312                 default:
1313                         dev_warn(adapter->dev, "invalid band %#x\n",
1314                                adapter->config_bands);
1315                         break;
1316                 }
1317         } else {
1318                 /* Send request to firmware */
1319                 ret = mwifiex_prepare_cmd(priv,
1320                                           HostCmd_CMD_802_11_TX_RATE_QUERY,
1321                                           HostCmd_ACT_GEN_GET, 0, wait, NULL);
1322                 if (!ret)
1323                         ret = -EINPROGRESS;
1324         }
1325
1326         return ret;
1327 }
1328
1329 /*
1330  * IOCTL request handler to set rate.
1331  *
1332  * This function prepares the correct firmware command and
1333  * issues it to set the current rate.
1334  *
1335  * The function also performs validation checking on the supplied value.
1336  */
1337 static int mwifiex_rate_ioctl_set_rate_value(struct mwifiex_private *priv,
1338                                              struct mwifiex_wait_queue *wait,
1339                                              struct mwifiex_rate_cfg *rate_cfg)
1340 {
1341         u8 rates[MWIFIEX_SUPPORTED_RATES];
1342         u8 *rate = NULL;
1343         int rate_index = 0;
1344         u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1345         u32 i = 0;
1346         int ret = 0;
1347         struct mwifiex_adapter *adapter = priv->adapter;
1348
1349         if (rate_cfg->is_rate_auto) {
1350                 memset(bitmap_rates, 0, sizeof(bitmap_rates));
1351                 /* Support all HR/DSSS rates */
1352                 bitmap_rates[0] = 0x000F;
1353                 /* Support all OFDM rates */
1354                 bitmap_rates[1] = 0x00FF;
1355                 /* Support all HT-MCSs rate */
1356                 for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates) - 3; i++)
1357                         bitmap_rates[i + 2] = 0xFFFF;
1358                 bitmap_rates[9] = 0x3FFF;
1359         } else {
1360                 memset(rates, 0, sizeof(rates));
1361                 mwifiex_get_active_data_rates(priv, rates);
1362                 rate = rates;
1363                 for (i = 0; (rate[i] && i < MWIFIEX_SUPPORTED_RATES); i++) {
1364                         dev_dbg(adapter->dev, "info: rate=%#x wanted=%#x\n",
1365                                 rate[i], rate_cfg->rate);
1366                         if ((rate[i] & 0x7f) == (rate_cfg->rate & 0x7f))
1367                                 break;
1368                 }
1369                 if (!rate[i] || (i == MWIFIEX_SUPPORTED_RATES)) {
1370                         dev_err(adapter->dev, "fixed data rate %#x is out "
1371                                "of range\n", rate_cfg->rate);
1372                         return -1;
1373                 }
1374                 memset(bitmap_rates, 0, sizeof(bitmap_rates));
1375
1376                 rate_index =
1377                         mwifiex_data_rate_to_index(adapter, rate_cfg->rate);
1378
1379                 /* Only allow b/g rates to be set */
1380                 if (rate_index >= MWIFIEX_RATE_INDEX_HRDSSS0 &&
1381                     rate_index <= MWIFIEX_RATE_INDEX_HRDSSS3) {
1382                         bitmap_rates[0] = 1 << rate_index;
1383                 } else {
1384                         rate_index -= 1; /* There is a 0x00 in the table */
1385                         if (rate_index >= MWIFIEX_RATE_INDEX_OFDM0 &&
1386                             rate_index <= MWIFIEX_RATE_INDEX_OFDM7)
1387                                 bitmap_rates[1] = 1 << (rate_index -
1388                                                    MWIFIEX_RATE_INDEX_OFDM0);
1389                 }
1390         }
1391
1392         /* Send request to firmware */
1393         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
1394                                   HostCmd_ACT_GEN_SET, 0, wait, bitmap_rates);
1395         if (!ret)
1396                 ret = -EINPROGRESS;
1397
1398         return ret;
1399 }
1400
1401 /*
1402  * IOCTL request handler to set/get rate.
1403  *
1404  * This function can be used to set/get either the rate value or the
1405  * rate index.
1406  */
1407 static int mwifiex_rate_ioctl_cfg(struct mwifiex_private *priv,
1408                                   struct mwifiex_wait_queue *wait,
1409                                   struct mwifiex_rate_cfg *rate_cfg)
1410 {
1411         int status = 0;
1412
1413         if (!rate_cfg)
1414                 return -1;
1415
1416         if (rate_cfg->action == HostCmd_ACT_GEN_GET)
1417                 status = mwifiex_rate_ioctl_get_rate_value(
1418                                 priv, wait, rate_cfg);
1419         else
1420                 status = mwifiex_rate_ioctl_set_rate_value(
1421                                 priv, wait, rate_cfg);
1422
1423         return status;
1424 }
1425
1426 /*
1427  * Sends IOCTL request to get the data rate.
1428  *
1429  * This function allocates the IOCTL request buffer, fills it
1430  * with requisite parameters and calls the IOCTL handler.
1431  */
1432 int mwifiex_drv_get_data_rate(struct mwifiex_private *priv,
1433                               struct mwifiex_rate_cfg *rate)
1434 {
1435         int ret = 0;
1436         struct mwifiex_wait_queue *wait = NULL;
1437         u8 wait_option = MWIFIEX_IOCTL_WAIT;
1438
1439         /* Allocate wait buffer */
1440         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
1441         if (!wait)
1442                 return -ENOMEM;
1443
1444         memset(rate, 0, sizeof(struct mwifiex_rate_cfg));
1445         rate->action = HostCmd_ACT_GEN_GET;
1446         ret = mwifiex_rate_ioctl_cfg(priv, wait, rate);
1447
1448         ret = mwifiex_request_ioctl(priv, wait, ret, wait_option);
1449         if (!ret) {
1450                 if (rate && rate->is_rate_auto)
1451                         rate->rate = mwifiex_index_to_data_rate(priv->adapter,
1452                                         priv->tx_rate, priv->tx_htinfo);
1453                 else if (rate)
1454                         rate->rate = priv->data_rate;
1455         } else {
1456                 ret = -1;
1457         }
1458
1459         kfree(wait);
1460         return ret;
1461 }
1462
1463 /*
1464  * IOCTL request handler to set tx power configuration.
1465  *
1466  * This function prepares the correct firmware command and
1467  * issues it.
1468  *
1469  * For non-auto power mode, all the following power groups are set -
1470  *      - Modulation class HR/DSSS
1471  *      - Modulation class OFDM
1472  *      - Modulation class HTBW20
1473  *      - Modulation class HTBW40
1474  */
1475 static int mwifiex_power_ioctl_set_power(struct mwifiex_private *priv,
1476                                          struct mwifiex_wait_queue *wait,
1477                                          struct mwifiex_power_cfg *power_cfg)
1478 {
1479         int ret = 0;
1480         struct host_cmd_ds_txpwr_cfg *txp_cfg = NULL;
1481         struct mwifiex_types_power_group *pg_tlv = NULL;
1482         struct mwifiex_power_group *pg = NULL;
1483         u8 *buf = NULL;
1484         u16 dbm = 0;
1485
1486         if (!power_cfg->is_power_auto) {
1487                 dbm = (u16) power_cfg->power_level;
1488                 if ((dbm < priv->min_tx_power_level) ||
1489                     (dbm > priv->max_tx_power_level)) {
1490                         dev_err(priv->adapter->dev, "txpower value %d dBm"
1491                                         " is out of range (%d dBm-%d dBm)\n",
1492                                         dbm, priv->min_tx_power_level,
1493                                         priv->max_tx_power_level);
1494                         return -1;
1495                 }
1496         }
1497         buf = kzalloc(MWIFIEX_SIZE_OF_CMD_BUFFER, GFP_KERNEL);
1498         if (!buf) {
1499                 dev_err(priv->adapter->dev, "%s: failed to alloc cmd buffer\n",
1500                                 __func__);
1501                 return -1;
1502         }
1503
1504         txp_cfg = (struct host_cmd_ds_txpwr_cfg *) buf;
1505         txp_cfg->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
1506         if (!power_cfg->is_power_auto) {
1507                 txp_cfg->mode = cpu_to_le32(1);
1508                 pg_tlv = (struct mwifiex_types_power_group *) (buf +
1509                                 sizeof(struct host_cmd_ds_txpwr_cfg));
1510                 pg_tlv->type = TLV_TYPE_POWER_GROUP;
1511                 pg_tlv->length = 4 * sizeof(struct mwifiex_power_group);
1512                 pg = (struct mwifiex_power_group *) (buf +
1513                                 sizeof(struct host_cmd_ds_txpwr_cfg) +
1514                                 sizeof(struct mwifiex_types_power_group));
1515                 /* Power group for modulation class HR/DSSS */
1516                 pg->first_rate_code = 0x00;
1517                 pg->last_rate_code = 0x03;
1518                 pg->modulation_class = MOD_CLASS_HR_DSSS;
1519                 pg->power_step = 0;
1520                 pg->power_min = (s8) dbm;
1521                 pg->power_max = (s8) dbm;
1522                 pg++;
1523                 /* Power group for modulation class OFDM */
1524                 pg->first_rate_code = 0x00;
1525                 pg->last_rate_code = 0x07;
1526                 pg->modulation_class = MOD_CLASS_OFDM;
1527                 pg->power_step = 0;
1528                 pg->power_min = (s8) dbm;
1529                 pg->power_max = (s8) dbm;
1530                 pg++;
1531                 /* Power group for modulation class HTBW20 */
1532                 pg->first_rate_code = 0x00;
1533                 pg->last_rate_code = 0x20;
1534                 pg->modulation_class = MOD_CLASS_HT;
1535                 pg->power_step = 0;
1536                 pg->power_min = (s8) dbm;
1537                 pg->power_max = (s8) dbm;
1538                 pg->ht_bandwidth = HT_BW_20;
1539                 pg++;
1540                 /* Power group for modulation class HTBW40 */
1541                 pg->first_rate_code = 0x00;
1542                 pg->last_rate_code = 0x20;
1543                 pg->modulation_class = MOD_CLASS_HT;
1544                 pg->power_step = 0;
1545                 pg->power_min = (s8) dbm;
1546                 pg->power_max = (s8) dbm;
1547                 pg->ht_bandwidth = HT_BW_40;
1548         }
1549         /* Send request to firmware */
1550         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_TXPWR_CFG,
1551                                   HostCmd_ACT_GEN_SET, 0, wait, buf);
1552         if (!ret)
1553                 ret = -EINPROGRESS;
1554         kfree(buf);
1555
1556         return ret;
1557 }
1558
1559 /*
1560  * IOCTL request handler to get power save mode.
1561  *
1562  * This function prepares the correct firmware command and
1563  * issues it.
1564  */
1565 static int mwifiex_pm_ioctl_ps_mode(struct mwifiex_private *priv,
1566                                     struct mwifiex_wait_queue *wait,
1567                                     u32 *ps_mode, u16 action)
1568 {
1569         int ret = 0;
1570         struct mwifiex_adapter *adapter = priv->adapter;
1571         u16 sub_cmd;
1572
1573         if (action == HostCmd_ACT_GEN_SET) {
1574                 if (*ps_mode)
1575                         adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1576                 else
1577                         adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1578                 sub_cmd = (*ps_mode) ? EN_AUTO_PS : DIS_AUTO_PS;
1579                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
1580                                           sub_cmd, BITMAP_STA_PS, wait, NULL);
1581                 if ((!ret) && (sub_cmd == DIS_AUTO_PS))
1582                         ret = mwifiex_prepare_cmd(priv,
1583                                         HostCmd_CMD_802_11_PS_MODE_ENH, GET_PS,
1584                                         0, NULL, NULL);
1585         } else {
1586                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
1587                                           GET_PS, 0, wait, NULL);
1588         }
1589
1590         if (!ret)
1591                 ret = -EINPROGRESS;
1592
1593         return ret;
1594 }
1595
1596 /*
1597  * IOCTL request handler to set/reset WPA IE.
1598  *
1599  * The supplied WPA IE is treated as a opaque buffer. Only the first field
1600  * is checked to determine WPA version. If buffer length is zero, the existing
1601  * WPA IE is reset.
1602  */
1603 static int mwifiex_set_wpa_ie_helper(struct mwifiex_private *priv,
1604                                      u8 *ie_data_ptr, u16 ie_len)
1605 {
1606         if (ie_len) {
1607                 if (ie_len > sizeof(priv->wpa_ie)) {
1608                         dev_err(priv->adapter->dev,
1609                                 "failed to copy WPA IE, too big\n");
1610                         return -1;
1611                 }
1612                 memcpy(priv->wpa_ie, ie_data_ptr, ie_len);
1613                 priv->wpa_ie_len = (u8) ie_len;
1614                 dev_dbg(priv->adapter->dev, "cmd: Set Wpa_ie_len=%d IE=%#x\n",
1615                                 priv->wpa_ie_len, priv->wpa_ie[0]);
1616
1617                 if (priv->wpa_ie[0] == WLAN_EID_WPA) {
1618                         priv->sec_info.wpa_enabled = true;
1619                 } else if (priv->wpa_ie[0] == WLAN_EID_RSN) {
1620                         priv->sec_info.wpa2_enabled = true;
1621                 } else {
1622                         priv->sec_info.wpa_enabled = false;
1623                         priv->sec_info.wpa2_enabled = false;
1624                 }
1625         } else {
1626                 memset(priv->wpa_ie, 0, sizeof(priv->wpa_ie));
1627                 priv->wpa_ie_len = 0;
1628                 dev_dbg(priv->adapter->dev, "info: reset wpa_ie_len=%d IE=%#x\n",
1629                         priv->wpa_ie_len, priv->wpa_ie[0]);
1630                 priv->sec_info.wpa_enabled = false;
1631                 priv->sec_info.wpa2_enabled = false;
1632         }
1633
1634         return 0;
1635 }
1636
1637 /*
1638  * IOCTL request handler to set/reset WAPI IE.
1639  *
1640  * The supplied WAPI IE is treated as a opaque buffer. Only the first field
1641  * is checked to internally enable WAPI. If buffer length is zero, the existing
1642  * WAPI IE is reset.
1643  */
1644 static int mwifiex_set_wapi_ie(struct mwifiex_private *priv,
1645                                u8 *ie_data_ptr, u16 ie_len)
1646 {
1647         if (ie_len) {
1648                 if (ie_len > sizeof(priv->wapi_ie)) {
1649                         dev_dbg(priv->adapter->dev,
1650                                 "info: failed to copy WAPI IE, too big\n");
1651                         return -1;
1652                 }
1653                 memcpy(priv->wapi_ie, ie_data_ptr, ie_len);
1654                 priv->wapi_ie_len = ie_len;
1655                 dev_dbg(priv->adapter->dev, "cmd: Set wapi_ie_len=%d IE=%#x\n",
1656                                 priv->wapi_ie_len, priv->wapi_ie[0]);
1657
1658                 if (priv->wapi_ie[0] == WLAN_EID_BSS_AC_ACCESS_DELAY)
1659                         priv->sec_info.wapi_enabled = true;
1660         } else {
1661                 memset(priv->wapi_ie, 0, sizeof(priv->wapi_ie));
1662                 priv->wapi_ie_len = ie_len;
1663                 dev_dbg(priv->adapter->dev,
1664                         "info: Reset wapi_ie_len=%d IE=%#x\n",
1665                        priv->wapi_ie_len, priv->wapi_ie[0]);
1666                 priv->sec_info.wapi_enabled = false;
1667         }
1668         return 0;
1669 }
1670
1671 /*
1672  * IOCTL request handler to set WAPI key.
1673  *
1674  * This function prepares the correct firmware command and
1675  * issues it.
1676  */
1677 static int mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_adapter *adapter,
1678                                struct mwifiex_wait_queue *wait,
1679                                struct mwifiex_ds_encrypt_key *encrypt_key)
1680 {
1681         int ret = 0;
1682         struct mwifiex_private *priv = adapter->priv[wait->bss_index];
1683
1684         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1685                                   HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1686                                   wait, encrypt_key);
1687         if (!ret)
1688                 ret = -EINPROGRESS;
1689
1690         return ret;
1691 }
1692
1693 /*
1694  * IOCTL request handler to set WEP network key.
1695  *
1696  * This function prepares the correct firmware command and
1697  * issues it, after validation checks.
1698  */
1699 static int mwifiex_sec_ioctl_set_wep_key(struct mwifiex_adapter *adapter,
1700                               struct mwifiex_wait_queue *wait,
1701                               struct mwifiex_ds_encrypt_key *encrypt_key)
1702 {
1703         int ret = 0;
1704         struct mwifiex_private *priv = adapter->priv[wait->bss_index];
1705         struct mwifiex_wep_key *wep_key = NULL;
1706         int index;
1707
1708         if (priv->wep_key_curr_index >= NUM_WEP_KEYS)
1709                 priv->wep_key_curr_index = 0;
1710         wep_key = &priv->wep_key[priv->wep_key_curr_index];
1711         index = encrypt_key->key_index;
1712         if (encrypt_key->key_disable) {
1713                 priv->sec_info.wep_status = MWIFIEX_802_11_WEP_DISABLED;
1714         } else if (!encrypt_key->key_len) {
1715                 /* Copy the required key as the current key */
1716                 wep_key = &priv->wep_key[index];
1717                 if (!wep_key->key_length) {
1718                         dev_err(adapter->dev,
1719                                 "key not set, so cannot enable it\n");
1720                         return -1;
1721                 }
1722                 priv->wep_key_curr_index = (u16) index;
1723                 priv->sec_info.wep_status = MWIFIEX_802_11_WEP_ENABLED;
1724         } else {
1725                 wep_key = &priv->wep_key[index];
1726                 /* Cleanup */
1727                 memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
1728                 /* Copy the key in the driver */
1729                 memcpy(wep_key->key_material,
1730                        encrypt_key->key_material,
1731                        encrypt_key->key_len);
1732                 wep_key->key_index = index;
1733                 wep_key->key_length = encrypt_key->key_len;
1734                 priv->sec_info.wep_status = MWIFIEX_802_11_WEP_ENABLED;
1735         }
1736         if (wep_key->key_length) {
1737                 /* Send request to firmware */
1738                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1739                                           HostCmd_ACT_GEN_SET, 0, NULL, NULL);
1740                 if (ret)
1741                         return ret;
1742         }
1743         if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED)
1744                 priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
1745         else
1746                 priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
1747
1748         /* Send request to firmware */
1749         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1750                                   HostCmd_ACT_GEN_SET, 0, wait,
1751                                   &priv->curr_pkt_filter);
1752         if (!ret)
1753                 ret = -EINPROGRESS;
1754
1755         return ret;
1756 }
1757
1758 /*
1759  * IOCTL request handler to set WPA key.
1760  *
1761  * This function prepares the correct firmware command and
1762  * issues it, after validation checks.
1763  *
1764  * Current driver only supports key length of up to 32 bytes.
1765  *
1766  * This function can also be used to disable a currently set key.
1767  */
1768 static int mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_adapter *adapter,
1769                               struct mwifiex_wait_queue *wait,
1770                               struct mwifiex_ds_encrypt_key *encrypt_key)
1771 {
1772         int ret = 0;
1773         struct mwifiex_private *priv = adapter->priv[wait->bss_index];
1774         u8 remove_key = false;
1775         struct host_cmd_ds_802_11_key_material *ibss_key;
1776
1777         /* Current driver only supports key length of up to 32 bytes */
1778         if (encrypt_key->key_len > MWIFIEX_MAX_KEY_LENGTH) {
1779                 dev_err(adapter->dev, "key length too long\n");
1780                 return -1;
1781         }
1782
1783         if (priv->bss_mode == MWIFIEX_BSS_MODE_IBSS) {
1784                 /*
1785                  * IBSS/WPA-None uses only one key (Group) for both receiving
1786                  * and sending unicast and multicast packets.
1787                  */
1788                 /* Send the key as PTK to firmware */
1789                 encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1790                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1791                                           HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1792                                           NULL, encrypt_key);
1793                 if (ret)
1794                         return ret;
1795
1796                 ibss_key = &priv->aes_key;
1797                 memset(ibss_key, 0,
1798                        sizeof(struct host_cmd_ds_802_11_key_material));
1799                 /* Copy the key in the driver */
1800                 memcpy(ibss_key->key_param_set.key, encrypt_key->key_material,
1801                        encrypt_key->key_len);
1802                 memcpy(&ibss_key->key_param_set.key_len, &encrypt_key->key_len,
1803                        sizeof(ibss_key->key_param_set.key_len));
1804                 ibss_key->key_param_set.key_type_id
1805                         = cpu_to_le16(KEY_TYPE_ID_TKIP);
1806                 ibss_key->key_param_set.key_info
1807                         = cpu_to_le16(KEY_INFO_TKIP_ENABLED);
1808
1809                 /* Send the key as GTK to firmware */
1810                 encrypt_key->key_index = ~MWIFIEX_KEY_INDEX_UNICAST;
1811         }
1812
1813         if (!encrypt_key->key_index)
1814                 encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1815
1816         if (remove_key)
1817                 /* Send request to firmware */
1818                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1819                                           HostCmd_ACT_GEN_SET,
1820                                           !(KEY_INFO_ENABLED),
1821                                           wait, encrypt_key);
1822         else
1823                 /* Send request to firmware */
1824                 ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
1825                                           HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1826                                           wait, encrypt_key);
1827
1828         if (!ret)
1829                 ret = -EINPROGRESS;
1830
1831         return ret;
1832 }
1833
1834 /*
1835  * IOCTL request handler to set/get network keys.
1836  *
1837  * This is a generic key handling function which supports WEP, WPA
1838  * and WAPI.
1839  */
1840 static int
1841 mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private *priv,
1842                               struct mwifiex_wait_queue *wait,
1843                               struct mwifiex_ds_encrypt_key *encrypt_key)
1844 {
1845         int status = 0;
1846         struct mwifiex_adapter *adapter = priv->adapter;
1847
1848         if (encrypt_key->is_wapi_key)
1849                 status = mwifiex_sec_ioctl_set_wapi_key(adapter, wait,
1850                                                         encrypt_key);
1851         else if (encrypt_key->key_len > WLAN_KEY_LEN_WEP104)
1852                 status = mwifiex_sec_ioctl_set_wpa_key(adapter, wait,
1853                                                        encrypt_key);
1854         else
1855                 status = mwifiex_sec_ioctl_set_wep_key(adapter, wait,
1856                                                        encrypt_key);
1857         return status;
1858 }
1859
1860 /*
1861  * This function returns the driver version.
1862  */
1863 int
1864 mwifiex_drv_get_driver_version(struct mwifiex_adapter *adapter, char *version,
1865                                int max_len)
1866 {
1867         union {
1868                 u32 l;
1869                 u8 c[4];
1870         } ver;
1871         char fw_ver[32];
1872
1873         ver.l = adapter->fw_release_number;
1874         sprintf(fw_ver, "%u.%u.%u.p%u", ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
1875
1876         snprintf(version, max_len, driver_version, fw_ver);
1877
1878         dev_dbg(adapter->dev, "info: MWIFIEX VERSION: %s\n", version);
1879
1880         return 0;
1881 }
1882
1883 /*
1884  * Sends IOCTL request to set Tx power. It can be set to either auto
1885  * or a fixed value.
1886  *
1887  * This function allocates the IOCTL request buffer, fills it
1888  * with requisite parameters and calls the IOCTL handler.
1889  */
1890 int
1891 mwifiex_set_tx_power(struct mwifiex_private *priv, int type, int dbm)
1892 {
1893         struct mwifiex_power_cfg power_cfg;
1894         struct mwifiex_wait_queue *wait = NULL;
1895         int status = 0;
1896         int ret = 0;
1897
1898         wait = mwifiex_alloc_fill_wait_queue(priv, MWIFIEX_IOCTL_WAIT);
1899         if (!wait)
1900                 return -ENOMEM;
1901
1902         if (type == NL80211_TX_POWER_FIXED) {
1903                 power_cfg.is_power_auto = 0;
1904                 power_cfg.power_level = dbm;
1905         } else {
1906                 power_cfg.is_power_auto = 1;
1907         }
1908         status = mwifiex_power_ioctl_set_power(priv, wait, &power_cfg);
1909
1910         ret = mwifiex_request_ioctl(priv, wait, status, MWIFIEX_IOCTL_WAIT);
1911
1912         kfree(wait);
1913         return ret;
1914 }
1915
1916 /*
1917  * Sends IOCTL request to get scan table.
1918  *
1919  * This function allocates the IOCTL request buffer, fills it
1920  * with requisite parameters and calls the IOCTL handler.
1921  */
1922 int mwifiex_get_scan_table(struct mwifiex_private *priv, u8 wait_option,
1923                            struct mwifiex_scan_resp *scan_resp)
1924 {
1925         struct mwifiex_wait_queue *wait = NULL;
1926         struct mwifiex_scan_resp scan;
1927         int status = 0;
1928
1929         /* Allocate wait buffer */
1930         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
1931         if (!wait)
1932                 return -ENOMEM;
1933
1934         status = mwifiex_scan_networks(priv, wait, HostCmd_ACT_GEN_GET,
1935                                        NULL, &scan);
1936
1937         status = mwifiex_request_ioctl(priv, wait, status, wait_option);
1938         if (!status) {
1939                 if (scan_resp)
1940                         memcpy(scan_resp, &scan,
1941                                sizeof(struct mwifiex_scan_resp));
1942         }
1943
1944         if (wait && (status != -EINPROGRESS))
1945                 kfree(wait);
1946         return status;
1947 }
1948
1949 /*
1950  * Sends IOCTL request to get signal information.
1951  *
1952  * This function allocates the IOCTL request buffer, fills it
1953  * with requisite parameters and calls the IOCTL handler.
1954  */
1955 int mwifiex_get_signal_info(struct mwifiex_private *priv, u8 wait_option,
1956                             struct mwifiex_ds_get_signal *signal)
1957 {
1958         struct mwifiex_ds_get_signal info;
1959         struct mwifiex_wait_queue *wait = NULL;
1960         int status = 0;
1961
1962         /* Allocate wait buffer */
1963         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
1964         if (!wait)
1965                 return -ENOMEM;
1966
1967         info.selector = ALL_RSSI_INFO_MASK;
1968
1969         status = mwifiex_get_info_signal(priv, wait, &info);
1970
1971         status = mwifiex_request_ioctl(priv, wait, status, wait_option);
1972         if (!status) {
1973                 if (signal)
1974                         memcpy(signal, &info,
1975                                sizeof(struct mwifiex_ds_get_signal));
1976                 if (info.selector & BCN_RSSI_AVG_MASK)
1977                         priv->w_stats.qual.level = info.bcn_rssi_avg;
1978                 if (info.selector & BCN_NF_AVG_MASK)
1979                         priv->w_stats.qual.noise = info.bcn_nf_avg;
1980         }
1981
1982         if (wait && (status != -EINPROGRESS))
1983                 kfree(wait);
1984         return status;
1985 }
1986
1987 /*
1988  * Sends IOCTL request to set encoding parameters.
1989  *
1990  * This function allocates the IOCTL request buffer, fills it
1991  * with requisite parameters and calls the IOCTL handler.
1992  */
1993 int mwifiex_set_encode(struct mwifiex_private *priv, const u8 *key,
1994                         int key_len, u8 key_index, int disable)
1995 {
1996         struct mwifiex_wait_queue *wait = NULL;
1997         struct mwifiex_ds_encrypt_key encrypt_key;
1998         int status = 0;
1999         int ret = 0;
2000
2001         wait = mwifiex_alloc_fill_wait_queue(priv, MWIFIEX_IOCTL_WAIT);
2002         if (!wait)
2003                 return -ENOMEM;
2004
2005         memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
2006         encrypt_key.key_len = key_len;
2007         if (!disable) {
2008                 encrypt_key.key_index = key_index;
2009                 if (key_len)
2010                         memcpy(encrypt_key.key_material, key, key_len);
2011         } else {
2012                 encrypt_key.key_disable = true;
2013         }
2014
2015         status = mwifiex_sec_ioctl_encrypt_key(priv, wait, &encrypt_key);
2016
2017         if (mwifiex_request_ioctl(priv, wait, status, MWIFIEX_IOCTL_WAIT))
2018                 ret = -EFAULT;
2019
2020         kfree(wait);
2021         return ret;
2022 }
2023
2024 /*
2025  * Sends IOCTL request to set power management parameters.
2026  *
2027  * This function allocates the IOCTL request buffer, fills it
2028  * with requisite parameters and calls the IOCTL handler.
2029  */
2030 int
2031 mwifiex_drv_set_power(struct mwifiex_private *priv, bool power_on)
2032 {
2033         int ret = 0;
2034         int status = 0;
2035         struct mwifiex_wait_queue *wait = NULL;
2036         u32 ps_mode;
2037
2038         wait = mwifiex_alloc_fill_wait_queue(priv, MWIFIEX_IOCTL_WAIT);
2039         if (!wait)
2040                 return -ENOMEM;
2041
2042         ps_mode = power_on;
2043         status = mwifiex_pm_ioctl_ps_mode(priv, wait, &ps_mode,
2044                                           HostCmd_ACT_GEN_SET);
2045
2046         ret = mwifiex_request_ioctl(priv, wait, status, MWIFIEX_IOCTL_WAIT);
2047
2048         kfree(wait);
2049         return ret;
2050 }
2051
2052 /*
2053  * Sends IOCTL request to get extended version.
2054  *
2055  * This function allocates the IOCTL request buffer, fills it
2056  * with requisite parameters and calls the IOCTL handler.
2057  */
2058 int
2059 mwifiex_get_ver_ext(struct mwifiex_private *priv)
2060 {
2061         struct mwifiex_ver_ext ver_ext;
2062         struct mwifiex_wait_queue *wait = NULL;
2063         int status = 0;
2064         int ret = 0;
2065         u8 wait_option = MWIFIEX_IOCTL_WAIT;
2066
2067         /* Allocate wait buffer */
2068         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
2069         if (!wait)
2070                 return -ENOMEM;
2071
2072         /* get fw version */
2073         memset(&ver_ext, 0, sizeof(struct host_cmd_ds_version_ext));
2074         status = mwifiex_get_info_ver_ext(priv, wait, &ver_ext);
2075
2076         ret = mwifiex_request_ioctl(priv, wait, status, wait_option);
2077
2078         if (ret)
2079                 ret = -1;
2080
2081         kfree(wait);
2082         return ret;
2083 }
2084
2085 /*
2086  * Sends IOCTL request to get statistics information.
2087  *
2088  * This function allocates the IOCTL request buffer, fills it
2089  * with requisite parameters and calls the IOCTL handler.
2090  */
2091 int
2092 mwifiex_get_stats_info(struct mwifiex_private *priv,
2093                        struct mwifiex_ds_get_stats *log)
2094 {
2095         int ret = 0;
2096         int status = 0;
2097         struct mwifiex_wait_queue *wait = NULL;
2098         struct mwifiex_ds_get_stats get_log;
2099         u8 wait_option = MWIFIEX_IOCTL_WAIT;
2100
2101         /* Allocate wait buffer */
2102         wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
2103         if (!wait)
2104                 return -ENOMEM;
2105
2106         memset(&get_log, 0, sizeof(struct mwifiex_ds_get_stats));
2107         status = mwifiex_get_info_stats(priv, wait, &get_log);
2108
2109         /* Send IOCTL request to MWIFIEX */
2110         ret = mwifiex_request_ioctl(priv, wait, status, wait_option);
2111         if (!ret) {
2112                 if (log)
2113                         memcpy(log, &get_log, sizeof(struct
2114                                         mwifiex_ds_get_stats));
2115                 priv->w_stats.discard.fragment = get_log.fcs_error;
2116                 priv->w_stats.discard.retries = get_log.retry;
2117                 priv->w_stats.discard.misc = get_log.ack_failure;
2118         }
2119
2120         kfree(wait);
2121         return ret;
2122 }
2123
2124 /*
2125  * IOCTL request handler to read/write register.
2126  *
2127  * This function prepares the correct firmware command and
2128  * issues it.
2129  *
2130  * Access to the following registers are supported -
2131  *      - MAC
2132  *      - BBP
2133  *      - RF
2134  *      - PMIC
2135  *      - CAU
2136  */
2137 static int mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private *priv,
2138                                         struct mwifiex_wait_queue *wait,
2139                                         struct mwifiex_ds_reg_rw *reg_rw,
2140                                         u16 action)
2141 {
2142         int ret = 0;
2143         u16 cmd_no;
2144
2145         switch (le32_to_cpu(reg_rw->type)) {
2146         case MWIFIEX_REG_MAC:
2147                 cmd_no = HostCmd_CMD_MAC_REG_ACCESS;
2148                 break;
2149         case MWIFIEX_REG_BBP:
2150                 cmd_no = HostCmd_CMD_BBP_REG_ACCESS;
2151                 break;
2152         case MWIFIEX_REG_RF:
2153                 cmd_no = HostCmd_CMD_RF_REG_ACCESS;
2154                 break;
2155         case MWIFIEX_REG_PMIC:
2156                 cmd_no = HostCmd_CMD_PMIC_REG_ACCESS;
2157                 break;
2158         case MWIFIEX_REG_CAU:
2159                 cmd_no = HostCmd_CMD_CAU_REG_ACCESS;
2160                 break;
2161         default:
2162                 return -1;
2163         }
2164
2165         /* Send request to firmware */
2166         ret = mwifiex_prepare_cmd(priv, cmd_no, action, 0, wait, reg_rw);
2167
2168         if (!ret)
2169                 ret = -EINPROGRESS;
2170
2171         return ret;
2172 }
2173
2174 /*
2175  * Sends IOCTL request to write to a register.
2176  *
2177  * This function allocates the IOCTL request buffer, fills it
2178  * with requisite parameters and calls the IOCTL handler.
2179  */
2180 int
2181 mwifiex_reg_write(struct mwifiex_private *priv, u32 reg_type,
2182                   u32 reg_offset, u32 reg_value)
2183 {
2184         int ret = 0;
2185         int status = 0;
2186         struct mwifiex_wait_queue *wait = NULL;
2187         struct mwifiex_ds_reg_rw reg_rw;
2188
2189         wait = mwifiex_alloc_fill_wait_queue(priv, MWIFIEX_IOCTL_WAIT);
2190         if (!wait)
2191                 return -ENOMEM;
2192
2193         reg_rw.type = cpu_to_le32(reg_type);
2194         reg_rw.offset = cpu_to_le32(reg_offset);
2195         reg_rw.value = cpu_to_le32(reg_value);
2196         status = mwifiex_reg_mem_ioctl_reg_rw(priv, wait, &reg_rw,
2197                                               HostCmd_ACT_GEN_SET);
2198
2199         ret = mwifiex_request_ioctl(priv, wait, status, MWIFIEX_IOCTL_WAIT);
2200
2201         kfree(wait);
2202         return ret;
2203 }
2204
2205 /*
2206  * Sends IOCTL request to read from a register.
2207  *
2208  * This function allocates the IOCTL request buffer, fills it
2209  * with requisite parameters and calls the IOCTL handler.
2210  */
2211 int
2212 mwifiex_reg_read(struct mwifiex_private *priv, u32 reg_type,
2213                  u32 reg_offset, u32 *value)
2214 {
2215         int ret = 0;
2216         int status = 0;
2217         struct mwifiex_wait_queue *wait = NULL;
2218         struct mwifiex_ds_reg_rw reg_rw;
2219
2220         wait = mwifiex_alloc_fill_wait_queue(priv, MWIFIEX_IOCTL_WAIT);
2221         if (!wait)
2222                 return -ENOMEM;
2223
2224         reg_rw.type = cpu_to_le32(reg_type);
2225         reg_rw.offset = cpu_to_le32(reg_offset);
2226         status = mwifiex_reg_mem_ioctl_reg_rw(priv, wait, &reg_rw,
2227                                               HostCmd_ACT_GEN_GET);
2228
2229         ret = mwifiex_request_ioctl(priv, wait, status, MWIFIEX_IOCTL_WAIT);
2230         if (ret)
2231                 goto done;
2232
2233         *value = le32_to_cpu(reg_rw.value);
2234
2235 done:
2236         kfree(wait);
2237         return ret;
2238 }
2239
2240 /*
2241  * IOCTL request handler to read EEPROM.
2242  *
2243  * This function prepares the correct firmware command and
2244  * issues it.
2245  */
2246 static int
2247 mwifiex_reg_mem_ioctl_read_eeprom(struct mwifiex_private *priv,
2248                                   struct mwifiex_wait_queue *wait,
2249                                   struct mwifiex_ds_read_eeprom *rd_eeprom)
2250 {
2251         int ret = 0;
2252
2253         /* Send request to firmware */
2254         ret = mwifiex_prepare_cmd(priv, HostCmd_CMD_802_11_EEPROM_ACCESS,
2255                                   HostCmd_ACT_GEN_GET, 0, wait, rd_eeprom);
2256
2257         if (!ret)
2258                 ret = -EINPROGRESS;
2259
2260         return ret;
2261 }
2262
2263 /*
2264  * Sends IOCTL request to read from EEPROM.
2265  *
2266  * This function allocates the IOCTL request buffer, fills it
2267  * with requisite parameters and calls the IOCTL handler.
2268  */
2269 int
2270 mwifiex_eeprom_read(struct mwifiex_private *priv, u16 offset, u16 bytes,
2271                     u8 *value)
2272 {
2273         int ret = 0;
2274         int status = 0;
2275         struct mwifiex_wait_queue *wait = NULL;
2276         struct mwifiex_ds_read_eeprom rd_eeprom;
2277
2278         wait = mwifiex_alloc_fill_wait_queue(priv, MWIFIEX_IOCTL_WAIT);
2279         if (!wait)
2280                 return -ENOMEM;
2281
2282         rd_eeprom.offset = cpu_to_le16((u16) offset);
2283         rd_eeprom.byte_count = cpu_to_le16((u16) bytes);
2284         status = mwifiex_reg_mem_ioctl_read_eeprom(priv, wait, &rd_eeprom);
2285
2286         ret = mwifiex_request_ioctl(priv, wait, status, MWIFIEX_IOCTL_WAIT);
2287         if (ret)
2288                 goto done;
2289
2290         memcpy(value, rd_eeprom.value, MAX_EEPROM_DATA);
2291 done:
2292         kfree(wait);
2293         return ret;
2294 }
2295
2296 /*
2297  * This function sets a generic IE. In addition to generic IE, it can
2298  * also handle WPA, WPA2 and WAPI IEs.
2299  */
2300 static int
2301 mwifiex_set_gen_ie_helper(struct mwifiex_private *priv, u8 *ie_data_ptr,
2302                           u16 ie_len)
2303 {
2304         int ret = 0;
2305         struct ieee_types_vendor_header *pvendor_ie;
2306         const u8 wpa_oui[] = { 0x00, 0x50, 0xf2, 0x01 };
2307         const u8 wps_oui[] = { 0x00, 0x50, 0xf2, 0x04 };
2308
2309         /* If the passed length is zero, reset the buffer */
2310         if (!ie_len) {
2311                 priv->gen_ie_buf_len = 0;
2312                 priv->wps.session_enable = false;
2313
2314                 return 0;
2315         } else if (!ie_data_ptr) {
2316                 return -1;
2317         }
2318         pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
2319         /* Test to see if it is a WPA IE, if not, then it is a gen IE */
2320         if (((pvendor_ie->element_id == WLAN_EID_WPA)
2321              && (!memcmp(pvendor_ie->oui, wpa_oui, sizeof(wpa_oui))))
2322                         || (pvendor_ie->element_id == WLAN_EID_RSN)) {
2323
2324                 /* IE is a WPA/WPA2 IE so call set_wpa function */
2325                 ret = mwifiex_set_wpa_ie_helper(priv, ie_data_ptr, ie_len);
2326                 priv->wps.session_enable = false;
2327
2328                 return ret;
2329         } else if (pvendor_ie->element_id == WLAN_EID_BSS_AC_ACCESS_DELAY) {
2330                 /* IE is a WAPI IE so call set_wapi function */
2331                 ret = mwifiex_set_wapi_ie(priv, ie_data_ptr, ie_len);
2332
2333                 return ret;
2334         }
2335         /*
2336          * Verify that the passed length is not larger than the
2337          * available space remaining in the buffer
2338          */
2339         if (ie_len < (sizeof(priv->gen_ie_buf) - priv->gen_ie_buf_len)) {
2340
2341                 /* Test to see if it is a WPS IE, if so, enable
2342                  * wps session flag
2343                  */
2344                 pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
2345                 if ((pvendor_ie->element_id == WLAN_EID_VENDOR_SPECIFIC)
2346                                 && (!memcmp(pvendor_ie->oui, wps_oui,
2347                                                 sizeof(wps_oui)))) {
2348                         priv->wps.session_enable = true;
2349                         dev_dbg(priv->adapter->dev,
2350                                 "info: WPS Session Enabled.\n");
2351                 }
2352
2353                 /* Append the passed data to the end of the
2354                    genIeBuffer */
2355                 memcpy(priv->gen_ie_buf + priv->gen_ie_buf_len, ie_data_ptr,
2356                                                                         ie_len);
2357                 /* Increment the stored buffer length by the
2358                    size passed */
2359                 priv->gen_ie_buf_len += ie_len;
2360         } else {
2361                 /* Passed data does not fit in the remaining
2362                    buffer space */
2363                 ret = -1;
2364         }
2365
2366         /* Return 0, or -1 for error case */
2367         return ret;
2368 }
2369
2370 /*
2371  * IOCTL request handler to set/get generic IE.
2372  *
2373  * In addition to various generic IEs, this function can also be
2374  * used to set the ARP filter.
2375  */
2376 static int mwifiex_misc_ioctl_gen_ie(struct mwifiex_private *priv,
2377                                      struct mwifiex_ds_misc_gen_ie *gen_ie,
2378                                      u16 action)
2379 {
2380         struct mwifiex_adapter *adapter = priv->adapter;
2381
2382         switch (gen_ie->type) {
2383         case MWIFIEX_IE_TYPE_GEN_IE:
2384                 if (action == HostCmd_ACT_GEN_GET) {
2385                         gen_ie->len = priv->wpa_ie_len;
2386                         memcpy(gen_ie->ie_data, priv->wpa_ie, gen_ie->len);
2387                 } else {
2388                         mwifiex_set_gen_ie_helper(priv, gen_ie->ie_data,
2389                                                   (u16) gen_ie->len);
2390                 }
2391                 break;
2392         case MWIFIEX_IE_TYPE_ARP_FILTER:
2393                 memset(adapter->arp_filter, 0, sizeof(adapter->arp_filter));
2394                 if (gen_ie->len > ARP_FILTER_MAX_BUF_SIZE) {
2395                         adapter->arp_filter_size = 0;
2396                         dev_err(adapter->dev, "invalid ARP filter size\n");
2397                         return -1;
2398                 } else {
2399                         memcpy(adapter->arp_filter, gen_ie->ie_data,
2400                                                                 gen_ie->len);
2401                         adapter->arp_filter_size = gen_ie->len;
2402                 }
2403                 break;
2404         default:
2405                 dev_err(adapter->dev, "invalid IE type\n");
2406                 return -1;
2407         }
2408         return 0;
2409 }
2410
2411 /*
2412  * Sends IOCTL request to set a generic IE.
2413  *
2414  * This function allocates the IOCTL request buffer, fills it
2415  * with requisite parameters and calls the IOCTL handler.
2416  */
2417 int
2418 mwifiex_set_gen_ie(struct mwifiex_private *priv, u8 *ie, int ie_len)
2419 {
2420         struct mwifiex_ds_misc_gen_ie gen_ie;
2421         int status = 0;
2422
2423         if (ie_len > IW_CUSTOM_MAX)
2424                 return -EFAULT;
2425
2426         gen_ie.type = MWIFIEX_IE_TYPE_GEN_IE;
2427         gen_ie.len = ie_len;
2428         memcpy(gen_ie.ie_data, ie, ie_len);
2429         status = mwifiex_misc_ioctl_gen_ie(priv, &gen_ie, HostCmd_ACT_GEN_SET);
2430         if (status)
2431                 return -EFAULT;
2432
2433         return 0;
2434 }