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