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