wl12xx: don't fail on AP scan
[deliverable/linux.git] / drivers / net / wireless / wl12xx / main.c
CommitLineData
f1d63a59 1
f5fc0f86
LC
2/*
3 * This file is part of wl1271
4 *
8bf29b0e 5 * Copyright (C) 2008-2010 Nokia Corporation
f5fc0f86
LC
6 *
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/module.h>
f5fc0f86
LC
26#include <linux/firmware.h>
27#include <linux/delay.h>
f5fc0f86
LC
28#include <linux/spi/spi.h>
29#include <linux/crc32.h>
30#include <linux/etherdevice.h>
1fba4974 31#include <linux/vmalloc.h>
a1dd8187 32#include <linux/platform_device.h>
5a0e3ad6 33#include <linux/slab.h>
341b7cde 34#include <linux/wl12xx.h>
95dac04f 35#include <linux/sched.h>
a390e85c 36#include <linux/interrupt.h>
f5fc0f86 37
00d20100 38#include "wl12xx.h"
0f4e3122 39#include "debug.h"
f5fc0f86 40#include "wl12xx_80211.h"
00d20100
SL
41#include "reg.h"
42#include "io.h"
43#include "event.h"
44#include "tx.h"
45#include "rx.h"
46#include "ps.h"
47#include "init.h"
48#include "debugfs.h"
49#include "cmd.h"
50#include "boot.h"
51#include "testmode.h"
52#include "scan.h"
f5fc0f86 53
9ccd9217
JO
54#define WL1271_BOOT_RETRIES 3
55
8a08048a
JO
56static struct conf_drv_settings default_conf = {
57 .sg = {
3be4112c
EP
58 .params = {
59 [CONF_SG_ACL_BT_MASTER_MIN_BR] = 10,
60 [CONF_SG_ACL_BT_MASTER_MAX_BR] = 180,
61 [CONF_SG_ACL_BT_SLAVE_MIN_BR] = 10,
62 [CONF_SG_ACL_BT_SLAVE_MAX_BR] = 180,
63 [CONF_SG_ACL_BT_MASTER_MIN_EDR] = 10,
64 [CONF_SG_ACL_BT_MASTER_MAX_EDR] = 80,
65 [CONF_SG_ACL_BT_SLAVE_MIN_EDR] = 10,
66 [CONF_SG_ACL_BT_SLAVE_MAX_EDR] = 80,
67 [CONF_SG_ACL_WLAN_PS_MASTER_BR] = 8,
68 [CONF_SG_ACL_WLAN_PS_SLAVE_BR] = 8,
69 [CONF_SG_ACL_WLAN_PS_MASTER_EDR] = 20,
70 [CONF_SG_ACL_WLAN_PS_SLAVE_EDR] = 20,
71 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MIN_BR] = 20,
72 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MAX_BR] = 35,
73 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MIN_BR] = 16,
74 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MAX_BR] = 35,
75 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MIN_EDR] = 32,
76 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MAX_EDR] = 50,
77 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MIN_EDR] = 28,
78 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MAX_EDR] = 50,
79 [CONF_SG_ACL_ACTIVE_SCAN_WLAN_BR] = 10,
80 [CONF_SG_ACL_ACTIVE_SCAN_WLAN_EDR] = 20,
81 [CONF_SG_ACL_PASSIVE_SCAN_BT_BR] = 75,
82 [CONF_SG_ACL_PASSIVE_SCAN_WLAN_BR] = 15,
83 [CONF_SG_ACL_PASSIVE_SCAN_BT_EDR] = 27,
84 [CONF_SG_ACL_PASSIVE_SCAN_WLAN_EDR] = 17,
85 /* active scan params */
86 [CONF_SG_AUTO_SCAN_PROBE_REQ] = 170,
87 [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_HV3] = 50,
88 [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_A2DP] = 100,
89 /* passive scan params */
90 [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP_BR] = 800,
91 [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP_EDR] = 200,
92 [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_HV3] = 200,
93 /* passive scan in dual antenna params */
94 [CONF_SG_CONSECUTIVE_HV3_IN_PASSIVE_SCAN] = 0,
95 [CONF_SG_BCN_HV3_COLLISION_THRESH_IN_PASSIVE_SCAN] = 0,
96 [CONF_SG_TX_RX_PROTECTION_BWIDTH_IN_PASSIVE_SCAN] = 0,
97 /* general params */
98 [CONF_SG_STA_FORCE_PS_IN_BT_SCO] = 1,
99 [CONF_SG_ANTENNA_CONFIGURATION] = 0,
100 [CONF_SG_BEACON_MISS_PERCENT] = 60,
101 [CONF_SG_DHCP_TIME] = 5000,
102 [CONF_SG_RXT] = 1200,
103 [CONF_SG_TXT] = 1000,
104 [CONF_SG_ADAPTIVE_RXT_TXT] = 1,
105 [CONF_SG_GENERAL_USAGE_BIT_MAP] = 3,
106 [CONF_SG_HV3_MAX_SERVED] = 6,
107 [CONF_SG_PS_POLL_TIMEOUT] = 10,
108 [CONF_SG_UPSD_TIMEOUT] = 10,
109 [CONF_SG_CONSECUTIVE_CTS_THRESHOLD] = 2,
110 [CONF_SG_STA_RX_WINDOW_AFTER_DTIM] = 5,
111 [CONF_SG_STA_CONNECTION_PROTECTION_TIME] = 30,
112 /* AP params */
113 [CONF_AP_BEACON_MISS_TX] = 3,
114 [CONF_AP_RX_WINDOW_AFTER_BEACON] = 10,
115 [CONF_AP_BEACON_WINDOW_INTERVAL] = 2,
116 [CONF_AP_CONNECTION_PROTECTION_TIME] = 0,
117 [CONF_AP_BT_ACL_VAL_BT_SERVE_TIME] = 25,
118 [CONF_AP_BT_ACL_VAL_WL_SERVE_TIME] = 25,
26612c43
EP
119 /* CTS Diluting params */
120 [CONF_SG_CTS_DILUTED_BAD_RX_PACKETS_TH] = 0,
121 [CONF_SG_CTS_CHOP_IN_DUAL_ANT_SCO_MASTER] = 0,
801f870b 122 },
1b00f546 123 .state = CONF_SG_PROTECTIVE,
8a08048a
JO
124 },
125 .rx = {
126 .rx_msdu_life_time = 512000,
127 .packet_detection_threshold = 0,
128 .ps_poll_timeout = 15,
129 .upsd_timeout = 15,
5f704d18 130 .rts_threshold = IEEE80211_MAX_RTS_THRESHOLD,
3ed8f2c6
LC
131 .rx_cca_threshold = 0,
132 .irq_blk_threshold = 0xFFFF,
133 .irq_pkt_threshold = 0,
134 .irq_timeout = 600,
8a08048a
JO
135 .queue_type = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
136 },
137 .tx = {
138 .tx_energy_detection = 0,
1e05a818 139 .sta_rc_conf = {
ebba60c6 140 .enabled_rates = 0,
8a08048a
JO
141 .short_retry_limit = 10,
142 .long_retry_limit = 10,
1e05a818 143 .aflags = 0,
45b531a8 144 },
8a08048a
JO
145 .ac_conf_count = 4,
146 .ac_conf = {
9987a9da 147 [CONF_TX_AC_BE] = {
8a08048a
JO
148 .ac = CONF_TX_AC_BE,
149 .cw_min = 15,
150 .cw_max = 63,
151 .aifsn = 3,
152 .tx_op_limit = 0,
45b531a8 153 },
9987a9da 154 [CONF_TX_AC_BK] = {
8a08048a
JO
155 .ac = CONF_TX_AC_BK,
156 .cw_min = 15,
157 .cw_max = 63,
158 .aifsn = 7,
159 .tx_op_limit = 0,
45b531a8 160 },
9987a9da 161 [CONF_TX_AC_VI] = {
8a08048a
JO
162 .ac = CONF_TX_AC_VI,
163 .cw_min = 15,
164 .cw_max = 63,
165 .aifsn = CONF_TX_AIFS_PIFS,
166 .tx_op_limit = 3008,
167 },
9987a9da 168 [CONF_TX_AC_VO] = {
8a08048a
JO
169 .ac = CONF_TX_AC_VO,
170 .cw_min = 15,
171 .cw_max = 63,
172 .aifsn = CONF_TX_AIFS_PIFS,
173 .tx_op_limit = 1504,
45b531a8 174 },
51f2be24 175 },
3618f30f
AN
176 .max_tx_retries = 100,
177 .ap_aging_period = 300,
9987a9da 178 .tid_conf_count = 4,
8a08048a 179 .tid_conf = {
9987a9da
JO
180 [CONF_TX_AC_BE] = {
181 .queue_id = CONF_TX_AC_BE,
182 .channel_type = CONF_CHANNEL_TYPE_EDCF,
8a08048a
JO
183 .tsid = CONF_TX_AC_BE,
184 .ps_scheme = CONF_PS_SCHEME_LEGACY,
185 .ack_policy = CONF_ACK_POLICY_LEGACY,
186 .apsd_conf = {0, 0},
51f2be24 187 },
9987a9da
JO
188 [CONF_TX_AC_BK] = {
189 .queue_id = CONF_TX_AC_BK,
190 .channel_type = CONF_CHANNEL_TYPE_EDCF,
191 .tsid = CONF_TX_AC_BK,
8a08048a
JO
192 .ps_scheme = CONF_PS_SCHEME_LEGACY,
193 .ack_policy = CONF_ACK_POLICY_LEGACY,
194 .apsd_conf = {0, 0},
195 },
9987a9da
JO
196 [CONF_TX_AC_VI] = {
197 .queue_id = CONF_TX_AC_VI,
198 .channel_type = CONF_CHANNEL_TYPE_EDCF,
199 .tsid = CONF_TX_AC_VI,
8a08048a
JO
200 .ps_scheme = CONF_PS_SCHEME_LEGACY,
201 .ack_policy = CONF_ACK_POLICY_LEGACY,
202 .apsd_conf = {0, 0},
203 },
9987a9da
JO
204 [CONF_TX_AC_VO] = {
205 .queue_id = CONF_TX_AC_VO,
206 .channel_type = CONF_CHANNEL_TYPE_EDCF,
207 .tsid = CONF_TX_AC_VO,
8a08048a
JO
208 .ps_scheme = CONF_PS_SCHEME_LEGACY,
209 .ack_policy = CONF_ACK_POLICY_LEGACY,
210 .apsd_conf = {0, 0},
211 },
8a08048a
JO
212 },
213 .frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD,
3ed8f2c6 214 .tx_compl_timeout = 700,
ebba60c6
JO
215 .tx_compl_threshold = 4,
216 .basic_rate = CONF_HW_BIT_RATE_1MBPS,
217 .basic_rate_5 = CONF_HW_BIT_RATE_6MBPS,
1e05a818
AN
218 .tmpl_short_retry_limit = 10,
219 .tmpl_long_retry_limit = 10,
8a08048a
JO
220 },
221 .conn = {
222 .wake_up_event = CONF_WAKE_UP_EVENT_DTIM,
50c500ad 223 .listen_interval = 1,
dae728fe
ES
224 .suspend_wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM,
225 .suspend_listen_interval = 3,
8a08048a 226 .bcn_filt_mode = CONF_BCN_FILT_MODE_ENABLED,
bc76b940 227 .bcn_filt_ie_count = 2,
8a08048a
JO
228 .bcn_filt_ie = {
229 [0] = {
230 .ie = WLAN_EID_CHANNEL_SWITCH,
231 .rule = CONF_BCN_RULE_PASS_ON_APPEARANCE,
bc76b940
SL
232 },
233 [1] = {
234 .ie = WLAN_EID_HT_INFORMATION,
235 .rule = CONF_BCN_RULE_PASS_ON_CHANGE,
236 },
47fab7d5 237 },
3ed8f2c6 238 .synch_fail_thold = 10,
8a08048a
JO
239 .bss_lose_timeout = 100,
240 .beacon_rx_timeout = 10000,
241 .broadcast_timeout = 20000,
242 .rx_broadcast_in_ps = 1,
90494a90 243 .ps_poll_threshold = 10,
11f70f97 244 .bet_enable = CONF_BET_MODE_ENABLE,
958b20e0 245 .bet_max_consecutive = 50,
a879ed79 246 .psm_entry_retries = 8,
2370841b 247 .psm_exit_retries = 16,
8eab7b47 248 .psm_entry_nullfunc_retries = 3,
f1d63a59 249 .dynamic_ps_timeout = 100,
5c0dc2fc 250 .forced_ps = false,
50c500ad
JO
251 .keep_alive_interval = 55000,
252 .max_listen_interval = 20,
8a08048a 253 },
6e92b416
LC
254 .itrim = {
255 .enable = false,
256 .timeout = 50000,
38ad2d87
JO
257 },
258 .pm_config = {
259 .host_clk_settling_time = 5000,
260 .host_fast_wakeup_support = false
00236aed
JO
261 },
262 .roam_trigger = {
00236aed
JO
263 .trigger_pacing = 1,
264 .avg_weight_rssi_beacon = 20,
265 .avg_weight_rssi_data = 10,
266 .avg_weight_snr_beacon = 20,
4b7fac77 267 .avg_weight_snr_data = 10,
bea39d6a
JO
268 },
269 .scan = {
270 .min_dwell_time_active = 7500,
271 .max_dwell_time_active = 30000,
ea45b2cb
JO
272 .min_dwell_time_passive = 100000,
273 .max_dwell_time_passive = 100000,
bea39d6a
JO
274 .num_probe_reqs = 2,
275 },
3a9d60e5
LC
276 .sched_scan = {
277 /* sched_scan requires dwell times in TU instead of TU/1000 */
221737d2
LC
278 .min_dwell_time_active = 30,
279 .max_dwell_time_active = 60,
3a9d60e5 280 .dwell_time_passive = 100,
50a66d7f 281 .dwell_time_dfs = 150,
3a9d60e5
LC
282 .num_probe_reqs = 2,
283 .rssi_threshold = -90,
284 .snr_threshold = 0,
285 },
644a4860
JO
286 .rf = {
287 .tx_per_channel_power_compensation_2 = {
288 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 },
290 .tx_per_channel_power_compensation_5 = {
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 },
295 },
4b7fac77 296 .ht = {
0f9c8250 297 .rx_ba_win_size = 8,
4b7fac77
LS
298 .tx_ba_win_size = 64,
299 .inactivity_timeout = 10000,
0f9c8250 300 .tx_ba_tid_bitmap = CONF_TX_BA_ENABLED_TID_BITMAP,
4b7fac77 301 },
13b107dd 302 .mem_wl127x = {
fe5ef090
EP
303 .num_stations = 1,
304 .ssid_profiles = 1,
305 .rx_block_num = 70,
306 .tx_min_block_num = 40,
4cf557fc 307 .dynamic_memory = 1,
b16d4b68 308 .min_req_tx_blocks = 100,
c8bde243
EP
309 .min_req_rx_blocks = 22,
310 .tx_min = 27,
13b107dd
SL
311 },
312 .mem_wl128x = {
313 .num_stations = 1,
314 .ssid_profiles = 1,
315 .rx_block_num = 40,
316 .tx_min_block_num = 40,
317 .dynamic_memory = 1,
318 .min_req_tx_blocks = 45,
319 .min_req_rx_blocks = 22,
320 .tx_min = 27,
321 },
ff86843d
SL
322 .fm_coex = {
323 .enable = true,
324 .swallow_period = 5,
325 .n_divider_fref_set_1 = 0xff, /* default */
326 .n_divider_fref_set_2 = 12,
327 .m_divider_fref_set_1 = 148,
328 .m_divider_fref_set_2 = 0xffff, /* default */
329 .coex_pll_stabilization_time = 0xffffffff, /* default */
330 .ldo_stabilization_time = 0xffff, /* default */
331 .fm_disturbed_band_margin = 0xff, /* default */
332 .swallow_clk_diff = 0xff, /* default */
333 },
f84673d5
EP
334 .rx_streaming = {
335 .duration = 150,
336 .queues = 0x1,
337 .interval = 20,
77ddaa10 338 .always = 0,
f84673d5 339 },
95dac04f
IY
340 .fwlog = {
341 .mode = WL12XX_FWLOG_ON_DEMAND,
342 .mem_blocks = 2,
343 .severity = 0,
344 .timestamp = WL12XX_FWLOG_TIMESTAMP_DISABLED,
345 .output = WL12XX_FWLOG_OUTPUT_HOST,
346 .threshold = 0,
347 },
afb7d3cd 348 .hci_io_ds = HCI_IO_DS_6MA,
fa6ad9f0
EP
349 .rate = {
350 .rate_retry_score = 32000,
351 .per_add = 8192,
352 .per_th1 = 2048,
353 .per_th2 = 4096,
354 .max_per = 8100,
355 .inverse_curiosity_factor = 5,
356 .tx_fail_low_th = 4,
357 .tx_fail_high_th = 10,
358 .per_alpha_shift = 4,
359 .per_add_shift = 13,
360 .per_beta1_shift = 10,
361 .per_beta2_shift = 8,
362 .rate_check_up = 2,
363 .rate_check_down = 12,
364 .rate_retry_policy = {
365 0x00, 0x00, 0x00, 0x00, 0x00,
366 0x00, 0x00, 0x00, 0x00, 0x00,
367 0x00, 0x00, 0x00,
368 },
369 },
9487775c
EP
370 .hangover = {
371 .recover_time = 0,
372 .hangover_period = 20,
373 .dynamic_mode = 1,
374 .early_termination_mode = 1,
375 .max_period = 20,
376 .min_period = 1,
377 .increase_delta = 1,
378 .decrease_delta = 2,
379 .quiet_time = 4,
380 .increase_time = 1,
381 .window_size = 16,
382 },
8a08048a
JO
383};
384
95dac04f 385static char *fwlog_param;
2a5bff09 386static bool bug_on_recovery;
95dac04f 387
7dece1c8 388static void __wl1271_op_remove_interface(struct wl1271 *wl,
536129c8 389 struct ieee80211_vif *vif,
7dece1c8 390 bool reset_tx_queues);
f0277434 391static void wl1271_op_stop(struct ieee80211_hw *hw);
170d0e67 392static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
52b0e7a6 393
f9f774c1 394static DEFINE_MUTEX(wl_list_mutex);
01c09162
JO
395static LIST_HEAD(wl_list);
396
ba8447f6
EP
397static int wl1271_check_operstate(struct wl1271 *wl, struct wl12xx_vif *wlvif,
398 unsigned char operstate)
ef4b29e9
EP
399{
400 int ret;
0603d891 401
ef4b29e9
EP
402 if (operstate != IF_OPER_UP)
403 return 0;
404
8181aecc 405 if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
ef4b29e9
EP
406 return 0;
407
154da67c 408 ret = wl12xx_cmd_set_peer_state(wl, wlvif->sta.hlid);
ef4b29e9
EP
409 if (ret < 0)
410 return ret;
411
0603d891 412 wl12xx_croc(wl, wlvif->role_id);
251c177f 413
ef4b29e9
EP
414 wl1271_info("Association completed.");
415 return 0;
416}
c2c192ac
JO
417static int wl1271_dev_notify(struct notifier_block *me, unsigned long what,
418 void *arg)
419{
420 struct net_device *dev = arg;
421 struct wireless_dev *wdev;
422 struct wiphy *wiphy;
423 struct ieee80211_hw *hw;
424 struct wl1271 *wl;
425 struct wl1271 *wl_temp;
ba8447f6 426 struct wl12xx_vif *wlvif;
c2c192ac
JO
427 int ret = 0;
428
429 /* Check that this notification is for us. */
430 if (what != NETDEV_CHANGE)
431 return NOTIFY_DONE;
432
433 wdev = dev->ieee80211_ptr;
434 if (wdev == NULL)
435 return NOTIFY_DONE;
436
437 wiphy = wdev->wiphy;
438 if (wiphy == NULL)
439 return NOTIFY_DONE;
440
441 hw = wiphy_priv(wiphy);
442 if (hw == NULL)
443 return NOTIFY_DONE;
444
445 wl_temp = hw->priv;
f9f774c1 446 mutex_lock(&wl_list_mutex);
c2c192ac
JO
447 list_for_each_entry(wl, &wl_list, list) {
448 if (wl == wl_temp)
449 break;
450 }
f9f774c1 451 mutex_unlock(&wl_list_mutex);
c2c192ac
JO
452 if (wl != wl_temp)
453 return NOTIFY_DONE;
454
455 mutex_lock(&wl->mutex);
456
457 if (wl->state == WL1271_STATE_OFF)
458 goto out;
459
6ab70916
EP
460 if (dev->operstate != IF_OPER_UP)
461 goto out;
462 /*
463 * The correct behavior should be just getting the appropriate wlvif
464 * from the given dev, but currently we don't have a mac80211
465 * interface for it.
466 */
ba8447f6 467 wl12xx_for_each_wlvif_sta(wl, wlvif) {
6ab70916
EP
468 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
469
ba8447f6
EP
470 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
471 continue;
c2c192ac 472
ba8447f6
EP
473 ret = wl1271_ps_elp_wakeup(wl);
474 if (ret < 0)
475 goto out;
c2c192ac 476
6ab70916
EP
477 wl1271_check_operstate(wl, wlvif,
478 ieee80211_get_operstate(vif));
c2c192ac 479
ba8447f6
EP
480 wl1271_ps_elp_sleep(wl);
481 }
c2c192ac
JO
482out:
483 mutex_unlock(&wl->mutex);
484
485 return NOTIFY_OK;
486}
487
b7417d93 488static int wl1271_reg_notify(struct wiphy *wiphy,
573c67cf
LC
489 struct regulatory_request *request)
490{
b7417d93
JO
491 struct ieee80211_supported_band *band;
492 struct ieee80211_channel *ch;
493 int i;
494
495 band = wiphy->bands[IEEE80211_BAND_5GHZ];
496 for (i = 0; i < band->n_channels; i++) {
497 ch = &band->channels[i];
498 if (ch->flags & IEEE80211_CHAN_DISABLED)
499 continue;
500
501 if (ch->flags & IEEE80211_CHAN_RADAR)
502 ch->flags |= IEEE80211_CHAN_NO_IBSS |
503 IEEE80211_CHAN_PASSIVE_SCAN;
504
505 }
506
507 return 0;
508}
509
9eb599e9
EP
510static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
511 bool enable)
77ddaa10
EP
512{
513 int ret = 0;
514
515 /* we should hold wl->mutex */
9eb599e9 516 ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
77ddaa10
EP
517 if (ret < 0)
518 goto out;
519
520 if (enable)
0744bdb6 521 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
77ddaa10 522 else
0744bdb6 523 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
77ddaa10
EP
524out:
525 return ret;
526}
527
528/*
529 * this function is being called when the rx_streaming interval
530 * has beed changed or rx_streaming should be disabled
531 */
9eb599e9 532int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
77ddaa10
EP
533{
534 int ret = 0;
535 int period = wl->conf.rx_streaming.interval;
536
537 /* don't reconfigure if rx_streaming is disabled */
0744bdb6 538 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
77ddaa10
EP
539 goto out;
540
541 /* reconfigure/disable according to new streaming_period */
542 if (period &&
ba8447f6 543 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
77ddaa10
EP
544 (wl->conf.rx_streaming.always ||
545 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
9eb599e9 546 ret = wl1271_set_rx_streaming(wl, wlvif, true);
77ddaa10 547 else {
9eb599e9 548 ret = wl1271_set_rx_streaming(wl, wlvif, false);
77ddaa10 549 /* don't cancel_work_sync since we might deadlock */
9eb599e9 550 del_timer_sync(&wlvif->rx_streaming_timer);
77ddaa10
EP
551 }
552out:
553 return ret;
554}
555
556static void wl1271_rx_streaming_enable_work(struct work_struct *work)
557{
558 int ret;
9eb599e9
EP
559 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
560 rx_streaming_enable_work);
561 struct wl1271 *wl = wlvif->wl;
77ddaa10
EP
562
563 mutex_lock(&wl->mutex);
564
0744bdb6 565 if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
ba8447f6 566 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
77ddaa10
EP
567 (!wl->conf.rx_streaming.always &&
568 !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
569 goto out;
570
571 if (!wl->conf.rx_streaming.interval)
572 goto out;
573
574 ret = wl1271_ps_elp_wakeup(wl);
575 if (ret < 0)
576 goto out;
577
9eb599e9 578 ret = wl1271_set_rx_streaming(wl, wlvif, true);
77ddaa10
EP
579 if (ret < 0)
580 goto out_sleep;
581
582 /* stop it after some time of inactivity */
9eb599e9 583 mod_timer(&wlvif->rx_streaming_timer,
77ddaa10
EP
584 jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
585
586out_sleep:
587 wl1271_ps_elp_sleep(wl);
588out:
589 mutex_unlock(&wl->mutex);
590}
591
592static void wl1271_rx_streaming_disable_work(struct work_struct *work)
593{
594 int ret;
9eb599e9
EP
595 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
596 rx_streaming_disable_work);
597 struct wl1271 *wl = wlvif->wl;
77ddaa10
EP
598
599 mutex_lock(&wl->mutex);
600
0744bdb6 601 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
77ddaa10
EP
602 goto out;
603
604 ret = wl1271_ps_elp_wakeup(wl);
605 if (ret < 0)
606 goto out;
607
9eb599e9 608 ret = wl1271_set_rx_streaming(wl, wlvif, false);
77ddaa10
EP
609 if (ret)
610 goto out_sleep;
611
612out_sleep:
613 wl1271_ps_elp_sleep(wl);
614out:
615 mutex_unlock(&wl->mutex);
616}
617
618static void wl1271_rx_streaming_timer(unsigned long data)
619{
9eb599e9
EP
620 struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
621 struct wl1271 *wl = wlvif->wl;
622 ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
77ddaa10
EP
623}
624
8a08048a
JO
625static void wl1271_conf_init(struct wl1271 *wl)
626{
2b60100b
JO
627
628 /*
629 * This function applies the default configuration to the driver. This
630 * function is invoked upon driver load (spi probe.)
631 *
632 * The configuration is stored in a run-time structure in order to
633 * facilitate for run-time adjustment of any of the parameters. Making
634 * changes to the configuration structure will apply the new values on
635 * the next interface up (wl1271_op_start.)
636 */
637
638 /* apply driver default configuration */
8a08048a 639 memcpy(&wl->conf, &default_conf, sizeof(default_conf));
2b60100b 640
95dac04f
IY
641 /* Adjust settings according to optional module parameters */
642 if (fwlog_param) {
643 if (!strcmp(fwlog_param, "continuous")) {
644 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
645 } else if (!strcmp(fwlog_param, "ondemand")) {
646 wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
647 } else if (!strcmp(fwlog_param, "dbgpins")) {
648 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
649 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
650 } else if (!strcmp(fwlog_param, "disable")) {
651 wl->conf.fwlog.mem_blocks = 0;
652 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
653 } else {
654 wl1271_error("Unknown fwlog parameter %s", fwlog_param);
655 }
656 }
657}
2b60100b 658
f5fc0f86
LC
659static int wl1271_plt_init(struct wl1271 *wl)
660{
188e7f54 661 int ret;
f5fc0f86 662
49d750ca
SL
663 if (wl->chip.id == CHIP_ID_1283_PG20)
664 ret = wl128x_cmd_general_parms(wl);
665 else
666 ret = wl1271_cmd_general_parms(wl);
4a90406b 667 if (ret < 0)
cc7defa3
LC
668 return ret;
669
49d750ca
SL
670 if (wl->chip.id == CHIP_ID_1283_PG20)
671 ret = wl128x_cmd_radio_parms(wl);
672 else
673 ret = wl1271_cmd_radio_parms(wl);
4a90406b 674 if (ret < 0)
cc7defa3
LC
675 return ret;
676
49d750ca
SL
677 if (wl->chip.id != CHIP_ID_1283_PG20) {
678 ret = wl1271_cmd_ext_radio_parms(wl);
679 if (ret < 0)
680 return ret;
681 }
644a4860
JO
682 if (ret < 0)
683 return ret;
684
48a61477
SL
685 /* Chip-specific initializations */
686 ret = wl1271_chip_specific_init(wl);
687 if (ret < 0)
688 return ret;
689
f5fc0f86
LC
690 ret = wl1271_acx_init_mem_config(wl);
691 if (ret < 0)
692 return ret;
693
7f097988 694 ret = wl12xx_acx_mem_cfg(wl);
1ec610eb
GK
695 if (ret < 0)
696 goto out_free_memmap;
697
12419cce 698 /* Enable data path */
94210897 699 ret = wl1271_cmd_data_path(wl, 1);
f5fc0f86 700 if (ret < 0)
12419cce
LC
701 goto out_free_memmap;
702
703 /* Configure for CAM power saving (ie. always active) */
704 ret = wl1271_acx_sleep_auth(wl, WL1271_PSM_CAM);
705 if (ret < 0)
706 goto out_free_memmap;
707
708 /* configure PM */
709 ret = wl1271_acx_pm_config(wl);
710 if (ret < 0)
711 goto out_free_memmap;
f5fc0f86
LC
712
713 return 0;
12419cce
LC
714
715 out_free_memmap:
716 kfree(wl->target_mem_map);
717 wl->target_mem_map = NULL;
718
719 return ret;
f5fc0f86
LC
720}
721
6e8cd331
EP
722static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
723 struct wl12xx_vif *wlvif,
724 u8 hlid, u8 tx_pkts)
b622d992 725{
da03209e 726 bool fw_ps, single_sta;
b622d992 727
b622d992 728 fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
da03209e 729 single_sta = (wl->active_sta_count == 1);
b622d992
AN
730
731 /*
732 * Wake up from high level PS if the STA is asleep with too little
9b17f1b3 733 * packets in FW or if the STA is awake.
b622d992 734 */
9b17f1b3 735 if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
6e8cd331 736 wl12xx_ps_link_end(wl, wlvif, hlid);
b622d992 737
da03209e
AN
738 /*
739 * Start high-level PS if the STA is asleep with enough blocks in FW.
740 * Make an exception if this is the only connected station. In this
741 * case FW-memory congestion is not a problem.
742 */
743 else if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
6e8cd331 744 wl12xx_ps_link_start(wl, wlvif, hlid, true);
b622d992
AN
745}
746
9b17f1b3 747static void wl12xx_irq_update_links_status(struct wl1271 *wl,
c7ffb902 748 struct wl12xx_vif *wlvif,
9b17f1b3 749 struct wl12xx_fw_status *status)
b622d992 750{
c7ffb902 751 struct wl1271_link *lnk;
b622d992 752 u32 cur_fw_ps_map;
9b17f1b3
AN
753 u8 hlid, cnt;
754
755 /* TODO: also use link_fast_bitmap here */
b622d992
AN
756
757 cur_fw_ps_map = le32_to_cpu(status->link_ps_bitmap);
758 if (wl->ap_fw_ps_map != cur_fw_ps_map) {
759 wl1271_debug(DEBUG_PSM,
760 "link ps prev 0x%x cur 0x%x changed 0x%x",
761 wl->ap_fw_ps_map, cur_fw_ps_map,
762 wl->ap_fw_ps_map ^ cur_fw_ps_map);
763
764 wl->ap_fw_ps_map = cur_fw_ps_map;
765 }
766
c7ffb902
EP
767 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, WL12XX_MAX_LINKS) {
768 lnk = &wl->links[hlid];
769 cnt = status->tx_lnk_free_pkts[hlid] - lnk->prev_freed_pkts;
b622d992 770
c7ffb902
EP
771 lnk->prev_freed_pkts = status->tx_lnk_free_pkts[hlid];
772 lnk->allocated_pkts -= cnt;
9b17f1b3 773
6e8cd331
EP
774 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
775 lnk->allocated_pkts);
b622d992
AN
776 }
777}
778
4d56ad9c
EP
779static void wl12xx_fw_status(struct wl1271 *wl,
780 struct wl12xx_fw_status *status)
f5fc0f86 781{
6e8cd331 782 struct wl12xx_vif *wlvif;
ac5e1e39 783 struct timespec ts;
13b107dd 784 u32 old_tx_blk_count = wl->tx_blocks_available;
4d56ad9c 785 int avail, freed_blocks;
bf54e301 786 int i;
f5fc0f86 787
4d56ad9c 788 wl1271_raw_read(wl, FW_STATUS_ADDR, status, sizeof(*status), false);
13b107dd 789
f5fc0f86
LC
790 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
791 "drv_rx_counter = %d, tx_results_counter = %d)",
792 status->intr,
793 status->fw_rx_counter,
794 status->drv_rx_counter,
795 status->tx_results_counter);
796
bf54e301
AN
797 for (i = 0; i < NUM_TX_QUEUES; i++) {
798 /* prevent wrap-around in freed-packets counter */
742246f8 799 wl->tx_allocated_pkts[i] -=
bf54e301
AN
800 (status->tx_released_pkts[i] -
801 wl->tx_pkts_freed[i]) & 0xff;
802
803 wl->tx_pkts_freed[i] = status->tx_released_pkts[i];
804 }
805
bdf91cfa
AN
806 /* prevent wrap-around in total blocks counter */
807 if (likely(wl->tx_blocks_freed <=
808 le32_to_cpu(status->total_released_blks)))
809 freed_blocks = le32_to_cpu(status->total_released_blks) -
810 wl->tx_blocks_freed;
811 else
812 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
813 le32_to_cpu(status->total_released_blks);
814
4d56ad9c 815 wl->tx_blocks_freed = le32_to_cpu(status->total_released_blks);
13b107dd 816
7bb5d6ce
AN
817 wl->tx_allocated_blocks -= freed_blocks;
818
4d56ad9c 819 avail = le32_to_cpu(status->tx_total) - wl->tx_allocated_blocks;
13b107dd 820
4d56ad9c
EP
821 /*
822 * The FW might change the total number of TX memblocks before
823 * we get a notification about blocks being released. Thus, the
824 * available blocks calculation might yield a temporary result
825 * which is lower than the actual available blocks. Keeping in
826 * mind that only blocks that were allocated can be moved from
827 * TX to RX, tx_blocks_available should never decrease here.
828 */
829 wl->tx_blocks_available = max((int)wl->tx_blocks_available,
830 avail);
f5fc0f86 831
a522550a 832 /* if more blocks are available now, tx work can be scheduled */
13b107dd 833 if (wl->tx_blocks_available > old_tx_blk_count)
a522550a 834 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
f5fc0f86 835
4d56ad9c 836 /* for AP update num of allocated TX blocks per link and ps status */
6e8cd331 837 wl12xx_for_each_wlvif_ap(wl, wlvif) {
c7ffb902 838 wl12xx_irq_update_links_status(wl, wlvif, status);
6e8cd331 839 }
4d56ad9c 840
f5fc0f86 841 /* update the host-chipset time offset */
ac5e1e39
JO
842 getnstimeofday(&ts);
843 wl->time_offset = (timespec_to_ns(&ts) >> 10) -
844 (s64)le32_to_cpu(status->fw_localtime);
f5fc0f86
LC
845}
846
a620865e
IY
847static void wl1271_flush_deferred_work(struct wl1271 *wl)
848{
849 struct sk_buff *skb;
850
851 /* Pass all received frames to the network stack */
852 while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
853 ieee80211_rx_ni(wl->hw, skb);
854
855 /* Return sent skbs to the network stack */
856 while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
c27d3acc 857 ieee80211_tx_status_ni(wl->hw, skb);
a620865e
IY
858}
859
860static void wl1271_netstack_work(struct work_struct *work)
861{
862 struct wl1271 *wl =
863 container_of(work, struct wl1271, netstack_work);
864
865 do {
866 wl1271_flush_deferred_work(wl);
867 } while (skb_queue_len(&wl->deferred_rx_queue));
868}
1e73eb62 869
a620865e
IY
870#define WL1271_IRQ_MAX_LOOPS 256
871
4b32a2c9 872static irqreturn_t wl1271_irq(int irq, void *cookie)
f5fc0f86 873{
f5fc0f86 874 int ret;
c15f63bf 875 u32 intr;
1e73eb62 876 int loopcount = WL1271_IRQ_MAX_LOOPS;
a620865e
IY
877 struct wl1271 *wl = (struct wl1271 *)cookie;
878 bool done = false;
879 unsigned int defer_count;
b07d4037
IY
880 unsigned long flags;
881
882 /* TX might be handled here, avoid redundant work */
883 set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
884 cancel_work_sync(&wl->tx_work);
f5fc0f86 885
341b7cde
IY
886 /*
887 * In case edge triggered interrupt must be used, we cannot iterate
888 * more than once without introducing race conditions with the hardirq.
889 */
890 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
891 loopcount = 1;
892
f5fc0f86
LC
893 mutex_lock(&wl->mutex);
894
895 wl1271_debug(DEBUG_IRQ, "IRQ work");
896
1e73eb62 897 if (unlikely(wl->state == WL1271_STATE_OFF))
f5fc0f86
LC
898 goto out;
899
a620865e 900 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
901 if (ret < 0)
902 goto out;
903
a620865e
IY
904 while (!done && loopcount--) {
905 /*
906 * In order to avoid a race with the hardirq, clear the flag
907 * before acknowledging the chip. Since the mutex is held,
908 * wl1271_ps_elp_wakeup cannot be called concurrently.
909 */
910 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
911 smp_mb__after_clear_bit();
1e73eb62 912
4d56ad9c
EP
913 wl12xx_fw_status(wl, wl->fw_status);
914 intr = le32_to_cpu(wl->fw_status->intr);
a620865e 915 intr &= WL1271_INTR_MASK;
1e73eb62 916 if (!intr) {
a620865e 917 done = true;
1e73eb62
JO
918 continue;
919 }
f5fc0f86 920
ccc83b04
EP
921 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
922 wl1271_error("watchdog interrupt received! "
923 "starting recovery.");
baacb9ae 924 wl12xx_queue_recovery_work(wl);
ccc83b04
EP
925
926 /* restarting the chip. ignore any other interrupt. */
927 goto out;
928 }
929
a620865e 930 if (likely(intr & WL1271_ACX_INTR_DATA)) {
1e73eb62 931 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
1fd2794f 932
4d56ad9c 933 wl12xx_rx(wl, wl->fw_status);
f5fc0f86 934
a522550a 935 /* Check if any tx blocks were freed */
b07d4037 936 spin_lock_irqsave(&wl->wl_lock, flags);
a522550a 937 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
f1a46384 938 wl1271_tx_total_queue_count(wl) > 0) {
b07d4037 939 spin_unlock_irqrestore(&wl->wl_lock, flags);
a522550a
IY
940 /*
941 * In order to avoid starvation of the TX path,
942 * call the work function directly.
943 */
a32d0cdf 944 wl1271_tx_work_locked(wl);
b07d4037
IY
945 } else {
946 spin_unlock_irqrestore(&wl->wl_lock, flags);
a522550a
IY
947 }
948
8aad2464 949 /* check for tx results */
4d56ad9c 950 if (wl->fw_status->tx_results_counter !=
8aad2464
IY
951 (wl->tx_results_count & 0xff))
952 wl1271_tx_complete(wl);
a620865e
IY
953
954 /* Make sure the deferred queues don't get too long */
955 defer_count = skb_queue_len(&wl->deferred_tx_queue) +
956 skb_queue_len(&wl->deferred_rx_queue);
957 if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
958 wl1271_flush_deferred_work(wl);
1e73eb62 959 }
f5fc0f86 960
1e73eb62
JO
961 if (intr & WL1271_ACX_INTR_EVENT_A) {
962 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
963 wl1271_event_handle(wl, 0);
964 }
f5fc0f86 965
1e73eb62
JO
966 if (intr & WL1271_ACX_INTR_EVENT_B) {
967 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
968 wl1271_event_handle(wl, 1);
969 }
f5fc0f86 970
1e73eb62
JO
971 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
972 wl1271_debug(DEBUG_IRQ,
973 "WL1271_ACX_INTR_INIT_COMPLETE");
f5fc0f86 974
1e73eb62
JO
975 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
976 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
c15f63bf 977 }
f5fc0f86 978
f5fc0f86
LC
979 wl1271_ps_elp_sleep(wl);
980
981out:
b07d4037
IY
982 spin_lock_irqsave(&wl->wl_lock, flags);
983 /* In case TX was not handled here, queue TX work */
984 clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
985 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
f1a46384 986 wl1271_tx_total_queue_count(wl) > 0)
b07d4037
IY
987 ieee80211_queue_work(wl->hw, &wl->tx_work);
988 spin_unlock_irqrestore(&wl->wl_lock, flags);
989
f5fc0f86 990 mutex_unlock(&wl->mutex);
a620865e
IY
991
992 return IRQ_HANDLED;
f5fc0f86
LC
993}
994
f5fc0f86
LC
995static int wl1271_fetch_firmware(struct wl1271 *wl)
996{
997 const struct firmware *fw;
166d504e 998 const char *fw_name;
f5fc0f86
LC
999 int ret;
1000
c302b2c9
AN
1001 if (wl->chip.id == CHIP_ID_1283_PG20)
1002 fw_name = WL128X_FW_NAME;
1003 else
1004 fw_name = WL127X_FW_NAME;
166d504e
AN
1005
1006 wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
1007
a390e85c 1008 ret = request_firmware(&fw, fw_name, wl->dev);
f5fc0f86
LC
1009
1010 if (ret < 0) {
35898935 1011 wl1271_error("could not get firmware %s: %d", fw_name, ret);
f5fc0f86
LC
1012 return ret;
1013 }
1014
1015 if (fw->size % 4) {
1016 wl1271_error("firmware size is not multiple of 32 bits: %zu",
1017 fw->size);
1018 ret = -EILSEQ;
1019 goto out;
1020 }
1021
166d504e 1022 vfree(wl->fw);
f5fc0f86 1023 wl->fw_len = fw->size;
1fba4974 1024 wl->fw = vmalloc(wl->fw_len);
f5fc0f86
LC
1025
1026 if (!wl->fw) {
1027 wl1271_error("could not allocate memory for the firmware");
1028 ret = -ENOMEM;
1029 goto out;
1030 }
1031
1032 memcpy(wl->fw, fw->data, wl->fw_len);
f5fc0f86
LC
1033 ret = 0;
1034
1035out:
1036 release_firmware(fw);
1037
1038 return ret;
1039}
1040
1041static int wl1271_fetch_nvs(struct wl1271 *wl)
1042{
1043 const struct firmware *fw;
1044 int ret;
1045
a390e85c 1046 ret = request_firmware(&fw, WL12XX_NVS_NAME, wl->dev);
f5fc0f86
LC
1047
1048 if (ret < 0) {
35898935
PF
1049 wl1271_error("could not get nvs file %s: %d", WL12XX_NVS_NAME,
1050 ret);
f5fc0f86
LC
1051 return ret;
1052 }
1053
bc765bf3 1054 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
f5fc0f86
LC
1055
1056 if (!wl->nvs) {
1057 wl1271_error("could not allocate memory for the nvs file");
1058 ret = -ENOMEM;
1059 goto out;
1060 }
1061
02fabb0e
JO
1062 wl->nvs_len = fw->size;
1063
f5fc0f86
LC
1064out:
1065 release_firmware(fw);
1066
1067 return ret;
1068}
1069
baacb9ae
IY
1070void wl12xx_queue_recovery_work(struct wl1271 *wl)
1071{
1072 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1073 ieee80211_queue_work(wl->hw, &wl->recovery_work);
1074}
1075
95dac04f
IY
1076size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
1077{
1078 size_t len = 0;
1079
1080 /* The FW log is a length-value list, find where the log end */
1081 while (len < maxlen) {
1082 if (memblock[len] == 0)
1083 break;
1084 if (len + memblock[len] + 1 > maxlen)
1085 break;
1086 len += memblock[len] + 1;
1087 }
1088
1089 /* Make sure we have enough room */
1090 len = min(len, (size_t)(PAGE_SIZE - wl->fwlog_size));
1091
1092 /* Fill the FW log file, consumed by the sysfs fwlog entry */
1093 memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
1094 wl->fwlog_size += len;
1095
1096 return len;
1097}
1098
1099static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
1100{
1101 u32 addr;
1102 u32 first_addr;
1103 u8 *block;
1104
1105 if ((wl->quirks & WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
1106 (wl->conf.fwlog.mode != WL12XX_FWLOG_ON_DEMAND) ||
1107 (wl->conf.fwlog.mem_blocks == 0))
1108 return;
1109
1110 wl1271_info("Reading FW panic log");
1111
1112 block = kmalloc(WL12XX_HW_BLOCK_SIZE, GFP_KERNEL);
1113 if (!block)
1114 return;
1115
1116 /*
1117 * Make sure the chip is awake and the logger isn't active.
1118 * This might fail if the firmware hanged.
1119 */
1120 if (!wl1271_ps_elp_wakeup(wl))
1121 wl12xx_cmd_stop_fwlog(wl);
1122
1123 /* Read the first memory block address */
4d56ad9c
EP
1124 wl12xx_fw_status(wl, wl->fw_status);
1125 first_addr = le32_to_cpu(wl->fw_status->log_start_addr);
95dac04f
IY
1126 if (!first_addr)
1127 goto out;
1128
1129 /* Traverse the memory blocks linked list */
1130 addr = first_addr;
1131 do {
1132 memset(block, 0, WL12XX_HW_BLOCK_SIZE);
1133 wl1271_read_hwaddr(wl, addr, block, WL12XX_HW_BLOCK_SIZE,
1134 false);
1135
1136 /*
1137 * Memory blocks are linked to one another. The first 4 bytes
1138 * of each memory block hold the hardware address of the next
1139 * one. The last memory block points to the first one.
1140 */
4d56ad9c 1141 addr = le32_to_cpup((__le32 *)block);
95dac04f
IY
1142 if (!wl12xx_copy_fwlog(wl, block + sizeof(addr),
1143 WL12XX_HW_BLOCK_SIZE - sizeof(addr)))
1144 break;
1145 } while (addr && (addr != first_addr));
1146
1147 wake_up_interruptible(&wl->fwlog_waitq);
1148
1149out:
1150 kfree(block);
1151}
1152
52b0e7a6
JO
1153static void wl1271_recovery_work(struct work_struct *work)
1154{
1155 struct wl1271 *wl =
1156 container_of(work, struct wl1271, recovery_work);
48e93e40 1157 struct wl12xx_vif *wlvif;
6e8cd331 1158 struct ieee80211_vif *vif;
52b0e7a6
JO
1159
1160 mutex_lock(&wl->mutex);
1161
1162 if (wl->state != WL1271_STATE_ON)
f0277434 1163 goto out_unlock;
52b0e7a6 1164
baacb9ae
IY
1165 /* Avoid a recursive recovery */
1166 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1167
95dac04f
IY
1168 wl12xx_read_fwlog_panic(wl);
1169
52dcaf57
AN
1170 wl1271_info("Hardware recovery in progress. FW ver: %s pc: 0x%x",
1171 wl->chip.fw_ver_str, wl1271_read32(wl, SCR_PAD4));
52b0e7a6 1172
2a5bff09
EP
1173 BUG_ON(bug_on_recovery);
1174
b992c682
OK
1175 /*
1176 * Advance security sequence number to overcome potential progress
1177 * in the firmware during recovery. This doens't hurt if the network is
1178 * not encrypted.
1179 */
48e93e40 1180 wl12xx_for_each_wlvif(wl, wlvif) {
ba8447f6 1181 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
53d40d0b 1182 test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
48e93e40
EP
1183 wlvif->tx_security_seq +=
1184 WL1271_TX_SQN_POST_RECOVERY_PADDING;
1185 }
b992c682 1186
7dece1c8
AN
1187 /* Prevent spurious TX during FW restart */
1188 ieee80211_stop_queues(wl->hw);
1189
33c2c06c
LC
1190 if (wl->sched_scanning) {
1191 ieee80211_sched_scan_stopped(wl->hw);
1192 wl->sched_scanning = false;
1193 }
1194
52b0e7a6 1195 /* reboot the chipset */
6e8cd331
EP
1196 while (!list_empty(&wl->wlvif_list)) {
1197 wlvif = list_first_entry(&wl->wlvif_list,
1198 struct wl12xx_vif, list);
1199 vif = wl12xx_wlvif_to_vif(wlvif);
1200 __wl1271_op_remove_interface(wl, vif, false);
1201 }
f0277434
EP
1202 mutex_unlock(&wl->mutex);
1203 wl1271_op_stop(wl->hw);
baacb9ae
IY
1204
1205 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1206
52b0e7a6
JO
1207 ieee80211_restart_hw(wl->hw);
1208
7dece1c8
AN
1209 /*
1210 * Its safe to enable TX now - the queues are stopped after a request
1211 * to restart the HW.
1212 */
1213 ieee80211_wake_queues(wl->hw);
f0277434
EP
1214 return;
1215out_unlock:
52b0e7a6
JO
1216 mutex_unlock(&wl->mutex);
1217}
1218
f5fc0f86
LC
1219static void wl1271_fw_wakeup(struct wl1271 *wl)
1220{
1221 u32 elp_reg;
1222
1223 elp_reg = ELPCTRL_WAKE_UP;
74621417 1224 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
f5fc0f86
LC
1225}
1226
1227static int wl1271_setup(struct wl1271 *wl)
1228{
1229 wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
1230 if (!wl->fw_status)
1231 return -ENOMEM;
1232
1233 wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
1234 if (!wl->tx_res_if) {
1235 kfree(wl->fw_status);
1236 return -ENOMEM;
1237 }
1238
f5fc0f86
LC
1239 return 0;
1240}
1241
30c5dbd1 1242static int wl12xx_set_power_on(struct wl1271 *wl)
f5fc0f86 1243{
30c5dbd1 1244 int ret;
f5fc0f86 1245
01ac17ec 1246 msleep(WL1271_PRE_POWER_ON_SLEEP);
2cc78ff7
OBC
1247 ret = wl1271_power_on(wl);
1248 if (ret < 0)
1249 goto out;
f5fc0f86 1250 msleep(WL1271_POWER_ON_SLEEP);
9b280722
TP
1251 wl1271_io_reset(wl);
1252 wl1271_io_init(wl);
f5fc0f86 1253
30c5dbd1 1254 wl1271_set_partition(wl, &wl12xx_part_table[PART_DOWN]);
f5fc0f86
LC
1255
1256 /* ELP module wake up */
1257 wl1271_fw_wakeup(wl);
1258
30c5dbd1
LC
1259out:
1260 return ret;
1261}
f5fc0f86 1262
30c5dbd1
LC
1263static int wl1271_chip_wakeup(struct wl1271 *wl)
1264{
1265 int ret = 0;
1266
1267 ret = wl12xx_set_power_on(wl);
1268 if (ret < 0)
1269 goto out;
f5fc0f86 1270
e62c9ce4
LC
1271 /*
1272 * For wl127x based devices we could use the default block
1273 * size (512 bytes), but due to a bug in the sdio driver, we
1274 * need to set it explicitly after the chip is powered on. To
1275 * simplify the code and since the performance impact is
1276 * negligible, we use the same block size for all different
1277 * chip types.
1278 */
1279 if (!wl1271_set_block_size(wl))
1280 wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
f5fc0f86
LC
1281
1282 switch (wl->chip.id) {
1283 case CHIP_ID_1271_PG10:
1284 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
1285 wl->chip.id);
1286
1287 ret = wl1271_setup(wl);
1288 if (ret < 0)
9ccd9217 1289 goto out;
ce39defb 1290 wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
f5fc0f86 1291 break;
ce39defb 1292
f5fc0f86
LC
1293 case CHIP_ID_1271_PG20:
1294 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
1295 wl->chip.id);
1296
1297 ret = wl1271_setup(wl);
1298 if (ret < 0)
9ccd9217 1299 goto out;
ce39defb 1300 wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
f5fc0f86 1301 break;
ce39defb 1302
0830ceed
SL
1303 case CHIP_ID_1283_PG20:
1304 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1283 PG20)",
1305 wl->chip.id);
1306
1307 ret = wl1271_setup(wl);
1308 if (ret < 0)
1309 goto out;
1310 break;
1311 case CHIP_ID_1283_PG10:
f5fc0f86 1312 default:
9ccd9217 1313 wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
f5fc0f86 1314 ret = -ENODEV;
9ccd9217 1315 goto out;
f5fc0f86
LC
1316 }
1317
c302b2c9 1318 if (wl->fw == NULL) {
f5fc0f86
LC
1319 ret = wl1271_fetch_firmware(wl);
1320 if (ret < 0)
9ccd9217 1321 goto out;
f5fc0f86
LC
1322 }
1323
1324 /* No NVS from netlink, try to get it from the filesystem */
1325 if (wl->nvs == NULL) {
1326 ret = wl1271_fetch_nvs(wl);
1327 if (ret < 0)
9ccd9217 1328 goto out;
f5fc0f86
LC
1329 }
1330
1331out:
1332 return ret;
1333}
1334
f5fc0f86
LC
1335int wl1271_plt_start(struct wl1271 *wl)
1336{
9ccd9217 1337 int retries = WL1271_BOOT_RETRIES;
6f07b72a 1338 struct wiphy *wiphy = wl->hw->wiphy;
f5fc0f86
LC
1339 int ret;
1340
1341 mutex_lock(&wl->mutex);
1342
1343 wl1271_notice("power up");
1344
1345 if (wl->state != WL1271_STATE_OFF) {
1346 wl1271_error("cannot go into PLT state because not "
1347 "in off state: %d", wl->state);
1348 ret = -EBUSY;
1349 goto out;
1350 }
1351
9ccd9217
JO
1352 while (retries) {
1353 retries--;
1354 ret = wl1271_chip_wakeup(wl);
1355 if (ret < 0)
1356 goto power_off;
f5fc0f86 1357
9ccd9217
JO
1358 ret = wl1271_boot(wl);
1359 if (ret < 0)
1360 goto power_off;
eb5b28d0 1361
9ccd9217
JO
1362 ret = wl1271_plt_init(wl);
1363 if (ret < 0)
1364 goto irq_disable;
bd5ea18f 1365
9ccd9217
JO
1366 wl->state = WL1271_STATE_PLT;
1367 wl1271_notice("firmware booted in PLT mode (%s)",
4b7fac77 1368 wl->chip.fw_ver_str);
e7ddf549 1369
6f07b72a
GK
1370 /* update hw/fw version info in wiphy struct */
1371 wiphy->hw_version = wl->chip.id;
1372 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1373 sizeof(wiphy->fw_version));
1374
9ccd9217 1375 goto out;
eb5b28d0 1376
9ccd9217 1377irq_disable:
9ccd9217
JO
1378 mutex_unlock(&wl->mutex);
1379 /* Unlocking the mutex in the middle of handling is
1380 inherently unsafe. In this case we deem it safe to do,
1381 because we need to let any possibly pending IRQ out of
1382 the system (and while we are WL1271_STATE_OFF the IRQ
1383 work function will not do anything.) Also, any other
1384 possible concurrent operations will fail due to the
1385 current state, hence the wl1271 struct should be safe. */
a620865e
IY
1386 wl1271_disable_interrupts(wl);
1387 wl1271_flush_deferred_work(wl);
1388 cancel_work_sync(&wl->netstack_work);
9ccd9217
JO
1389 mutex_lock(&wl->mutex);
1390power_off:
1391 wl1271_power_off(wl);
1392 }
f5fc0f86 1393
9ccd9217
JO
1394 wl1271_error("firmware boot in PLT mode failed despite %d retries",
1395 WL1271_BOOT_RETRIES);
f5fc0f86
LC
1396out:
1397 mutex_unlock(&wl->mutex);
1398
1399 return ret;
1400}
1401
f3df1331 1402int wl1271_plt_stop(struct wl1271 *wl)
f5fc0f86
LC
1403{
1404 int ret = 0;
1405
f5fc0f86
LC
1406 wl1271_notice("power down");
1407
46b0cc9f
IY
1408 /*
1409 * Interrupts must be disabled before setting the state to OFF.
1410 * Otherwise, the interrupt handler might be called and exit without
1411 * reading the interrupt status.
1412 */
1413 wl1271_disable_interrupts(wl);
f3df1331 1414 mutex_lock(&wl->mutex);
f5fc0f86 1415 if (wl->state != WL1271_STATE_PLT) {
f3df1331 1416 mutex_unlock(&wl->mutex);
46b0cc9f
IY
1417
1418 /*
1419 * This will not necessarily enable interrupts as interrupts
1420 * may have been disabled when op_stop was called. It will,
1421 * however, balance the above call to disable_interrupts().
1422 */
1423 wl1271_enable_interrupts(wl);
1424
f5fc0f86
LC
1425 wl1271_error("cannot power down because not in PLT "
1426 "state: %d", wl->state);
1427 ret = -EBUSY;
1428 goto out;
1429 }
1430
f5fc0f86 1431 mutex_unlock(&wl->mutex);
f3df1331 1432
a620865e
IY
1433 wl1271_flush_deferred_work(wl);
1434 cancel_work_sync(&wl->netstack_work);
52b0e7a6 1435 cancel_work_sync(&wl->recovery_work);
f6fbeccd 1436 cancel_delayed_work_sync(&wl->elp_work);
a454969e
IY
1437
1438 mutex_lock(&wl->mutex);
1439 wl1271_power_off(wl);
f6fbeccd
LC
1440 wl->flags = 0;
1441 wl->state = WL1271_STATE_OFF;
1442 wl->rx_counter = 0;
a454969e
IY
1443 mutex_unlock(&wl->mutex);
1444
4ae3fa87
JO
1445out:
1446 return ret;
1447}
1448
7bb45683 1449static void wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
f5fc0f86
LC
1450{
1451 struct wl1271 *wl = hw->priv;
a8ab39a4
EP
1452 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1453 struct ieee80211_vif *vif = info->control.vif;
0f168014 1454 struct wl12xx_vif *wlvif = NULL;
830fb67b 1455 unsigned long flags;
708bb3cf 1456 int q, mapping;
d6a3cc2e 1457 u8 hlid;
f5fc0f86 1458
0f168014
EP
1459 if (vif)
1460 wlvif = wl12xx_vif_to_data(vif);
1461
708bb3cf
AN
1462 mapping = skb_get_queue_mapping(skb);
1463 q = wl1271_tx_get_queue(mapping);
b07d4037 1464
d6a3cc2e 1465 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
b07d4037 1466
830fb67b 1467 spin_lock_irqsave(&wl->wl_lock, flags);
b07d4037 1468
830fb67b 1469 /* queue the packet */
d6a3cc2e 1470 if (hlid == WL12XX_INVALID_LINK_ID ||
0f168014 1471 (wlvif && !test_bit(hlid, wlvif->links_map))) {
d6a3cc2e 1472 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
5de8eef4 1473 ieee80211_free_txskb(hw, skb);
d6a3cc2e 1474 goto out;
a8c0ddb5 1475 }
f5fc0f86 1476
d6a3cc2e
EP
1477 wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d", hlid, q);
1478 skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1479
04b4d69c
AN
1480 wl->tx_queue_count[q]++;
1481
1482 /*
1483 * The workqueue is slow to process the tx_queue and we need stop
1484 * the queue here, otherwise the queue will get too long.
1485 */
1486 if (wl->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK) {
1487 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1488 ieee80211_stop_queue(wl->hw, mapping);
1489 set_bit(q, &wl->stopped_queues_map);
1490 }
1491
f5fc0f86
LC
1492 /*
1493 * The chip specific setup must run before the first TX packet -
1494 * before that, the tx_work will not be initialized!
1495 */
1496
b07d4037
IY
1497 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1498 !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
a522550a 1499 ieee80211_queue_work(wl->hw, &wl->tx_work);
b07d4037 1500
04216da3 1501out:
b07d4037 1502 spin_unlock_irqrestore(&wl->wl_lock, flags);
f5fc0f86
LC
1503}
1504
ae47c45f
SL
1505int wl1271_tx_dummy_packet(struct wl1271 *wl)
1506{
990f5de7 1507 unsigned long flags;
14623787
AN
1508 int q;
1509
1510 /* no need to queue a new dummy packet if one is already pending */
1511 if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1512 return 0;
1513
1514 q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
990f5de7
IY
1515
1516 spin_lock_irqsave(&wl->wl_lock, flags);
1517 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
f1a46384 1518 wl->tx_queue_count[q]++;
990f5de7
IY
1519 spin_unlock_irqrestore(&wl->wl_lock, flags);
1520
1521 /* The FW is low on RX memory blocks, so send the dummy packet asap */
1522 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
a32d0cdf 1523 wl1271_tx_work_locked(wl);
990f5de7
IY
1524
1525 /*
1526 * If the FW TX is busy, TX work will be scheduled by the threaded
1527 * interrupt handler function
1528 */
1529 return 0;
1530}
1531
1532/*
1533 * The size of the dummy packet should be at least 1400 bytes. However, in
1534 * order to minimize the number of bus transactions, aligning it to 512 bytes
1535 * boundaries could be beneficial, performance wise
1536 */
1537#define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1538
cf27d867 1539static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
990f5de7
IY
1540{
1541 struct sk_buff *skb;
ae47c45f 1542 struct ieee80211_hdr_3addr *hdr;
990f5de7
IY
1543 unsigned int dummy_packet_size;
1544
1545 dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1546 sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
ae47c45f 1547
990f5de7 1548 skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
ae47c45f 1549 if (!skb) {
990f5de7
IY
1550 wl1271_warning("Failed to allocate a dummy packet skb");
1551 return NULL;
ae47c45f
SL
1552 }
1553
1554 skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1555
1556 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1557 memset(hdr, 0, sizeof(*hdr));
1558 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
990f5de7
IY
1559 IEEE80211_STYPE_NULLFUNC |
1560 IEEE80211_FCTL_TODS);
ae47c45f 1561
990f5de7 1562 memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
ae47c45f 1563
18b92ffa
LC
1564 /* Dummy packets require the TID to be management */
1565 skb->priority = WL1271_TID_MGMT;
ae47c45f 1566
990f5de7 1567 /* Initialize all fields that might be used */
86c438f4 1568 skb_set_queue_mapping(skb, 0);
990f5de7 1569 memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
ae47c45f 1570
990f5de7 1571 return skb;
ae47c45f
SL
1572}
1573
990f5de7 1574
c2c192ac
JO
1575static struct notifier_block wl1271_dev_notifier = {
1576 .notifier_call = wl1271_dev_notify,
1577};
1578
f634a4e7 1579#ifdef CONFIG_PM
dae728fe
ES
1580static int wl1271_configure_suspend_sta(struct wl1271 *wl,
1581 struct wl12xx_vif *wlvif)
1582{
1583 int ret = 0;
1584
1585 mutex_lock(&wl->mutex);
1586
1587 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1588 goto out_unlock;
1589
1590 ret = wl1271_ps_elp_wakeup(wl);
1591 if (ret < 0)
1592 goto out_unlock;
1593
1594 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1595 wl->conf.conn.suspend_wake_up_event,
1596 wl->conf.conn.suspend_listen_interval);
1597
1598 if (ret < 0)
1599 wl1271_error("suspend: set wake up conditions failed: %d", ret);
1600
1601
1602 wl1271_ps_elp_sleep(wl);
1603
1604out_unlock:
1605 mutex_unlock(&wl->mutex);
1606 return ret;
1607
1608}
9439064c 1609
0603d891
EP
1610static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1611 struct wl12xx_vif *wlvif)
8a7cf3fe 1612{
e85d1629 1613 int ret = 0;
8a7cf3fe
EP
1614
1615 mutex_lock(&wl->mutex);
1616
53d40d0b 1617 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
e85d1629
EP
1618 goto out_unlock;
1619
8a7cf3fe
EP
1620 ret = wl1271_ps_elp_wakeup(wl);
1621 if (ret < 0)
1622 goto out_unlock;
1623
0603d891 1624 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
8a7cf3fe
EP
1625
1626 wl1271_ps_elp_sleep(wl);
1627out_unlock:
1628 mutex_unlock(&wl->mutex);
1629 return ret;
1630
1631}
1632
d2d66c56
EP
1633static int wl1271_configure_suspend(struct wl1271 *wl,
1634 struct wl12xx_vif *wlvif)
8a7cf3fe 1635{
dae728fe
ES
1636 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1637 return wl1271_configure_suspend_sta(wl, wlvif);
536129c8 1638 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
0603d891 1639 return wl1271_configure_suspend_ap(wl, wlvif);
8a7cf3fe
EP
1640 return 0;
1641}
1642
d2d66c56
EP
1643static void wl1271_configure_resume(struct wl1271 *wl,
1644 struct wl12xx_vif *wlvif)
9439064c 1645{
dae728fe 1646 int ret = 0;
536129c8 1647 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
dae728fe 1648 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
9439064c 1649
dae728fe 1650 if ((!is_ap) && (!is_sta))
9439064c
EP
1651 return;
1652
1653 mutex_lock(&wl->mutex);
1654 ret = wl1271_ps_elp_wakeup(wl);
1655 if (ret < 0)
1656 goto out;
1657
dae728fe
ES
1658 if (is_sta) {
1659 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1660 wl->conf.conn.wake_up_event,
1661 wl->conf.conn.listen_interval);
1662
1663 if (ret < 0)
1664 wl1271_error("resume: wake up conditions failed: %d",
1665 ret);
1666
1667 } else if (is_ap) {
1668 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1669 }
9439064c
EP
1670
1671 wl1271_ps_elp_sleep(wl);
1672out:
1673 mutex_unlock(&wl->mutex);
1674}
1675
402e4861
EP
1676static int wl1271_op_suspend(struct ieee80211_hw *hw,
1677 struct cfg80211_wowlan *wow)
1678{
1679 struct wl1271 *wl = hw->priv;
6e8cd331 1680 struct wl12xx_vif *wlvif;
4a859df8
EP
1681 int ret;
1682
402e4861 1683 wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
4a859df8 1684 WARN_ON(!wow || !wow->any);
f44e5868 1685
4a859df8 1686 wl->wow_enabled = true;
6e8cd331
EP
1687 wl12xx_for_each_wlvif(wl, wlvif) {
1688 ret = wl1271_configure_suspend(wl, wlvif);
1689 if (ret < 0) {
1690 wl1271_warning("couldn't prepare device to suspend");
1691 return ret;
1692 }
4a859df8
EP
1693 }
1694 /* flush any remaining work */
1695 wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
f44e5868 1696
4a859df8
EP
1697 /*
1698 * disable and re-enable interrupts in order to flush
1699 * the threaded_irq
1700 */
1701 wl1271_disable_interrupts(wl);
1702
1703 /*
1704 * set suspended flag to avoid triggering a new threaded_irq
1705 * work. no need for spinlock as interrupts are disabled.
1706 */
1707 set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1708
1709 wl1271_enable_interrupts(wl);
1710 flush_work(&wl->tx_work);
4a859df8 1711 flush_delayed_work(&wl->elp_work);
f44e5868 1712
402e4861
EP
1713 return 0;
1714}
1715
1716static int wl1271_op_resume(struct ieee80211_hw *hw)
1717{
1718 struct wl1271 *wl = hw->priv;
6e8cd331 1719 struct wl12xx_vif *wlvif;
4a859df8
EP
1720 unsigned long flags;
1721 bool run_irq_work = false;
1722
402e4861
EP
1723 wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1724 wl->wow_enabled);
4a859df8 1725 WARN_ON(!wl->wow_enabled);
f44e5868
EP
1726
1727 /*
1728 * re-enable irq_work enqueuing, and call irq_work directly if
1729 * there is a pending work.
1730 */
4a859df8
EP
1731 spin_lock_irqsave(&wl->wl_lock, flags);
1732 clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1733 if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1734 run_irq_work = true;
1735 spin_unlock_irqrestore(&wl->wl_lock, flags);
9439064c 1736
4a859df8
EP
1737 if (run_irq_work) {
1738 wl1271_debug(DEBUG_MAC80211,
1739 "run postponed irq_work directly");
1740 wl1271_irq(0, wl);
1741 wl1271_enable_interrupts(wl);
f44e5868 1742 }
6e8cd331
EP
1743 wl12xx_for_each_wlvif(wl, wlvif) {
1744 wl1271_configure_resume(wl, wlvif);
1745 }
ff91afc9 1746 wl->wow_enabled = false;
f44e5868 1747
402e4861
EP
1748 return 0;
1749}
f634a4e7 1750#endif
402e4861 1751
f5fc0f86 1752static int wl1271_op_start(struct ieee80211_hw *hw)
1b72aecd
JO
1753{
1754 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1755
1756 /*
1757 * We have to delay the booting of the hardware because
1758 * we need to know the local MAC address before downloading and
1759 * initializing the firmware. The MAC address cannot be changed
1760 * after boot, and without the proper MAC address, the firmware
1761 * will not function properly.
1762 *
1763 * The MAC address is first known when the corresponding interface
1764 * is added. That is where we will initialize the hardware.
1765 */
1766
d18da7fc 1767 return 0;
1b72aecd
JO
1768}
1769
1770static void wl1271_op_stop(struct ieee80211_hw *hw)
1771{
baf6277a
EP
1772 struct wl1271 *wl = hw->priv;
1773 int i;
1774
1b72aecd 1775 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
baf6277a 1776
46b0cc9f
IY
1777 /*
1778 * Interrupts must be disabled before setting the state to OFF.
1779 * Otherwise, the interrupt handler might be called and exit without
1780 * reading the interrupt status.
1781 */
1782 wl1271_disable_interrupts(wl);
10c8cd01
EP
1783 mutex_lock(&wl->mutex);
1784 if (wl->state == WL1271_STATE_OFF) {
1785 mutex_unlock(&wl->mutex);
46b0cc9f
IY
1786
1787 /*
1788 * This will not necessarily enable interrupts as interrupts
1789 * may have been disabled when op_stop was called. It will,
1790 * however, balance the above call to disable_interrupts().
1791 */
1792 wl1271_enable_interrupts(wl);
10c8cd01
EP
1793 return;
1794 }
46b0cc9f 1795
baf6277a
EP
1796 /*
1797 * this must be before the cancel_work calls below, so that the work
1798 * functions don't perform further work.
1799 */
1800 wl->state = WL1271_STATE_OFF;
10c8cd01
EP
1801 mutex_unlock(&wl->mutex);
1802
1803 mutex_lock(&wl_list_mutex);
1804 list_del(&wl->list);
baf6277a
EP
1805 mutex_unlock(&wl_list_mutex);
1806
baf6277a
EP
1807 wl1271_flush_deferred_work(wl);
1808 cancel_delayed_work_sync(&wl->scan_complete_work);
1809 cancel_work_sync(&wl->netstack_work);
1810 cancel_work_sync(&wl->tx_work);
baf6277a
EP
1811 cancel_delayed_work_sync(&wl->elp_work);
1812
1813 /* let's notify MAC80211 about the remaining pending TX frames */
1814 wl12xx_tx_reset(wl, true);
1815 mutex_lock(&wl->mutex);
1816
1817 wl1271_power_off(wl);
1818
1819 wl->band = IEEE80211_BAND_2GHZ;
1820
1821 wl->rx_counter = 0;
1822 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1823 wl->tx_blocks_available = 0;
1824 wl->tx_allocated_blocks = 0;
1825 wl->tx_results_count = 0;
1826 wl->tx_packets_count = 0;
1827 wl->time_offset = 0;
baf6277a
EP
1828 wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
1829 wl->ap_fw_ps_map = 0;
1830 wl->ap_ps_map = 0;
1831 wl->sched_scanning = false;
1832 memset(wl->roles_map, 0, sizeof(wl->roles_map));
1833 memset(wl->links_map, 0, sizeof(wl->links_map));
1834 memset(wl->roc_map, 0, sizeof(wl->roc_map));
1835 wl->active_sta_count = 0;
1836
1837 /* The system link is always allocated */
1838 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1839
1840 /*
1841 * this is performed after the cancel_work calls and the associated
1842 * mutex_lock, so that wl1271_op_add_interface does not accidentally
1843 * get executed before all these vars have been reset.
1844 */
1845 wl->flags = 0;
1846
1847 wl->tx_blocks_freed = 0;
1848
1849 for (i = 0; i < NUM_TX_QUEUES; i++) {
1850 wl->tx_pkts_freed[i] = 0;
1851 wl->tx_allocated_pkts[i] = 0;
1852 }
1853
1854 wl1271_debugfs_reset(wl);
1855
1856 kfree(wl->fw_status);
1857 wl->fw_status = NULL;
1858 kfree(wl->tx_res_if);
1859 wl->tx_res_if = NULL;
1860 kfree(wl->target_mem_map);
1861 wl->target_mem_map = NULL;
1862
1863 mutex_unlock(&wl->mutex);
1b72aecd
JO
1864}
1865
e5a359f8
EP
1866static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
1867{
1868 u8 policy = find_first_zero_bit(wl->rate_policies_map,
1869 WL12XX_MAX_RATE_POLICIES);
1870 if (policy >= WL12XX_MAX_RATE_POLICIES)
1871 return -EBUSY;
1872
1873 __set_bit(policy, wl->rate_policies_map);
1874 *idx = policy;
1875 return 0;
1876}
1877
1878static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
1879{
1880 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
1881 return;
1882
1883 __clear_bit(*idx, wl->rate_policies_map);
1884 *idx = WL12XX_MAX_RATE_POLICIES;
1885}
1886
536129c8 1887static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
b78b47eb 1888{
536129c8 1889 switch (wlvif->bss_type) {
b78b47eb 1890 case BSS_TYPE_AP_BSS:
fb0e707c 1891 if (wlvif->p2p)
045c745f
EP
1892 return WL1271_ROLE_P2P_GO;
1893 else
1894 return WL1271_ROLE_AP;
b78b47eb
EP
1895
1896 case BSS_TYPE_STA_BSS:
fb0e707c 1897 if (wlvif->p2p)
045c745f
EP
1898 return WL1271_ROLE_P2P_CL;
1899 else
1900 return WL1271_ROLE_STA;
b78b47eb 1901
227e81e1
EP
1902 case BSS_TYPE_IBSS:
1903 return WL1271_ROLE_IBSS;
1904
b78b47eb 1905 default:
536129c8 1906 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
b78b47eb
EP
1907 }
1908 return WL12XX_INVALID_ROLE_TYPE;
1909}
1910
83587505 1911static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
87fbcb0f 1912{
e936bbe0 1913 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e5a359f8 1914 int i;
e936bbe0 1915
48e93e40
EP
1916 /* clear everything but the persistent data */
1917 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
e936bbe0
EP
1918
1919 switch (ieee80211_vif_type_p2p(vif)) {
1920 case NL80211_IFTYPE_P2P_CLIENT:
1921 wlvif->p2p = 1;
1922 /* fall-through */
1923 case NL80211_IFTYPE_STATION:
1924 wlvif->bss_type = BSS_TYPE_STA_BSS;
1925 break;
1926 case NL80211_IFTYPE_ADHOC:
1927 wlvif->bss_type = BSS_TYPE_IBSS;
1928 break;
1929 case NL80211_IFTYPE_P2P_GO:
1930 wlvif->p2p = 1;
1931 /* fall-through */
1932 case NL80211_IFTYPE_AP:
1933 wlvif->bss_type = BSS_TYPE_AP_BSS;
1934 break;
1935 default:
1936 wlvif->bss_type = MAX_BSS_TYPE;
1937 return -EOPNOTSUPP;
1938 }
1939
0603d891 1940 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
7edebf56 1941 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
afaf8bdb 1942 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
a8ab39a4 1943
e936bbe0
EP
1944 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
1945 wlvif->bss_type == BSS_TYPE_IBSS) {
1946 /* init sta/ibss data */
1947 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
1948 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
1949 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
1950 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
e936bbe0
EP
1951 } else {
1952 /* init ap data */
1953 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
1954 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
1955 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
1956 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
1957 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
1958 wl12xx_allocate_rate_policy(wl,
1959 &wlvif->ap.ucast_rate_idx[i]);
e936bbe0 1960 }
a8ab39a4 1961
83587505
EP
1962 wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
1963 wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
87fbcb0f 1964 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
d2d66c56 1965 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
30d0c8fd 1966 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
6a899796
EP
1967 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
1968
1b92f15e
EP
1969 /*
1970 * mac80211 configures some values globally, while we treat them
1971 * per-interface. thus, on init, we have to copy them from wl
1972 */
1973 wlvif->band = wl->band;
61f845f4 1974 wlvif->channel = wl->channel;
6bd65029 1975 wlvif->power_level = wl->power_level;
1b92f15e 1976
9eb599e9
EP
1977 INIT_WORK(&wlvif->rx_streaming_enable_work,
1978 wl1271_rx_streaming_enable_work);
1979 INIT_WORK(&wlvif->rx_streaming_disable_work,
1980 wl1271_rx_streaming_disable_work);
87627214 1981 INIT_LIST_HEAD(&wlvif->list);
252efa4f 1982
9eb599e9
EP
1983 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
1984 (unsigned long) wlvif);
e936bbe0 1985 return 0;
87fbcb0f
EP
1986}
1987
1d095475 1988static bool wl12xx_init_fw(struct wl1271 *wl)
f5fc0f86 1989{
9ccd9217 1990 int retries = WL1271_BOOT_RETRIES;
71125abd 1991 bool booted = false;
1d095475
EP
1992 struct wiphy *wiphy = wl->hw->wiphy;
1993 int ret;
f5fc0f86 1994
9ccd9217
JO
1995 while (retries) {
1996 retries--;
1997 ret = wl1271_chip_wakeup(wl);
1998 if (ret < 0)
1999 goto power_off;
f5fc0f86 2000
9ccd9217
JO
2001 ret = wl1271_boot(wl);
2002 if (ret < 0)
2003 goto power_off;
f5fc0f86 2004
92c77c73
EP
2005 ret = wl1271_hw_init(wl);
2006 if (ret < 0)
2007 goto irq_disable;
2008
71125abd
EP
2009 booted = true;
2010 break;
eb5b28d0 2011
9ccd9217 2012irq_disable:
9ccd9217
JO
2013 mutex_unlock(&wl->mutex);
2014 /* Unlocking the mutex in the middle of handling is
2015 inherently unsafe. In this case we deem it safe to do,
2016 because we need to let any possibly pending IRQ out of
2017 the system (and while we are WL1271_STATE_OFF the IRQ
2018 work function will not do anything.) Also, any other
2019 possible concurrent operations will fail due to the
2020 current state, hence the wl1271 struct should be safe. */
a620865e
IY
2021 wl1271_disable_interrupts(wl);
2022 wl1271_flush_deferred_work(wl);
2023 cancel_work_sync(&wl->netstack_work);
9ccd9217
JO
2024 mutex_lock(&wl->mutex);
2025power_off:
2026 wl1271_power_off(wl);
2027 }
eb5b28d0 2028
71125abd
EP
2029 if (!booted) {
2030 wl1271_error("firmware boot failed despite %d retries",
2031 WL1271_BOOT_RETRIES);
2032 goto out;
2033 }
2034
4b7fac77 2035 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
71125abd
EP
2036
2037 /* update hw/fw version info in wiphy struct */
2038 wiphy->hw_version = wl->chip.id;
4b7fac77 2039 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
71125abd
EP
2040 sizeof(wiphy->fw_version));
2041
fb6a6819
LC
2042 /*
2043 * Now we know if 11a is supported (info from the NVS), so disable
2044 * 11a channels if not supported
2045 */
2046 if (!wl->enable_11a)
2047 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2048
2049 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2050 wl->enable_11a ? "" : "not ");
2051
1d095475
EP
2052 wl->state = WL1271_STATE_ON;
2053out:
2054 return booted;
2055}
2056
92e712da
EP
2057static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2058{
2059 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2060}
2061
1d095475
EP
2062static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2063 struct ieee80211_vif *vif)
2064{
2065 struct wl1271 *wl = hw->priv;
2066 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2067 int ret = 0;
2068 u8 role_type;
2069 bool booted = false;
2070
ea086359
JB
2071 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2072 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
c1288b12 2073
1d095475
EP
2074 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2075 ieee80211_vif_type_p2p(vif), vif->addr);
2076
2077 mutex_lock(&wl->mutex);
f750c820
EP
2078 ret = wl1271_ps_elp_wakeup(wl);
2079 if (ret < 0)
2080 goto out_unlock;
2081
1d095475
EP
2082 if (wl->vif) {
2083 wl1271_debug(DEBUG_MAC80211,
2084 "multiple vifs are not supported yet");
2085 ret = -EBUSY;
2086 goto out;
2087 }
2088
2089 /*
2090 * in some very corner case HW recovery scenarios its possible to
2091 * get here before __wl1271_op_remove_interface is complete, so
2092 * opt out if that is the case.
2093 */
10c8cd01
EP
2094 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2095 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
1d095475
EP
2096 ret = -EBUSY;
2097 goto out;
2098 }
2099
83587505 2100 ret = wl12xx_init_vif_data(wl, vif);
1d095475
EP
2101 if (ret < 0)
2102 goto out;
2103
2104 wlvif->wl = wl;
2105 role_type = wl12xx_get_role_type(wl, wlvif);
2106 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2107 ret = -EINVAL;
2108 goto out;
2109 }
2110
2111 /*
2112 * TODO: after the nvs issue will be solved, move this block
2113 * to start(), and make sure here the driver is ON.
2114 */
2115 if (wl->state == WL1271_STATE_OFF) {
2116 /*
2117 * we still need this in order to configure the fw
2118 * while uploading the nvs
2119 */
5e037e74 2120 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
1d095475
EP
2121
2122 booted = wl12xx_init_fw(wl);
2123 if (!booted) {
2124 ret = -EINVAL;
2125 goto out;
2126 }
2127 }
2128
2129 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2130 wlvif->bss_type == BSS_TYPE_IBSS) {
2131 /*
2132 * The device role is a special role used for
2133 * rx and tx frames prior to association (as
2134 * the STA role can get packets only from
2135 * its associated bssid)
2136 */
2137 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2138 WL1271_ROLE_DEVICE,
2139 &wlvif->dev_role_id);
2140 if (ret < 0)
2141 goto out;
2142 }
2143
2144 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2145 role_type, &wlvif->role_id);
2146 if (ret < 0)
2147 goto out;
2148
2149 ret = wl1271_init_vif_specific(wl, vif);
2150 if (ret < 0)
2151 goto out;
2152
2153 wl->vif = vif;
87627214 2154 list_add(&wlvif->list, &wl->wlvif_list);
10c8cd01 2155 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
a4e4130d
EP
2156
2157 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2158 wl->ap_count++;
2159 else
2160 wl->sta_count++;
eb5b28d0 2161out:
f750c820
EP
2162 wl1271_ps_elp_sleep(wl);
2163out_unlock:
f5fc0f86
LC
2164 mutex_unlock(&wl->mutex);
2165
f9f774c1 2166 mutex_lock(&wl_list_mutex);
eb887dfd 2167 if (!ret)
01c09162 2168 list_add(&wl->list, &wl_list);
f9f774c1 2169 mutex_unlock(&wl_list_mutex);
01c09162 2170
f5fc0f86
LC
2171 return ret;
2172}
2173
7dece1c8 2174static void __wl1271_op_remove_interface(struct wl1271 *wl,
536129c8 2175 struct ieee80211_vif *vif,
7dece1c8 2176 bool reset_tx_queues)
f5fc0f86 2177{
536129c8 2178 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e5a359f8 2179 int i, ret;
f5fc0f86 2180
1b72aecd 2181 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
f5fc0f86 2182
10c8cd01
EP
2183 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2184 return;
2185
2f8e81ad
EP
2186 wl->vif = NULL;
2187
13026dec
JO
2188 /* because of hardware recovery, we may get here twice */
2189 if (wl->state != WL1271_STATE_ON)
2190 return;
2191
1b72aecd 2192 wl1271_info("down");
f5fc0f86 2193
baf6277a
EP
2194 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2195 wl->scan_vif == vif) {
08688d6b 2196 wl->scan.state = WL1271_SCAN_STATE_IDLE;
4a31c11c 2197 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
784f694d 2198 wl->scan_vif = NULL;
b739a42c 2199 wl->scan.req = NULL;
76a029fb 2200 ieee80211_scan_completed(wl->hw, true);
f5fc0f86
LC
2201 }
2202
b78b47eb
EP
2203 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2204 /* disable active roles */
2205 ret = wl1271_ps_elp_wakeup(wl);
2206 if (ret < 0)
2207 goto deinit;
2208
b890f4c3
EP
2209 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2210 wlvif->bss_type == BSS_TYPE_IBSS) {
2211 if (wl12xx_dev_role_started(wlvif))
2212 wl12xx_stop_dev(wl, wlvif);
2213
7edebf56 2214 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
04e8079c
EP
2215 if (ret < 0)
2216 goto deinit;
2217 }
2218
0603d891 2219 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
b78b47eb
EP
2220 if (ret < 0)
2221 goto deinit;
2222
2223 wl1271_ps_elp_sleep(wl);
2224 }
2225deinit:
e51ae9be 2226 /* clear all hlids (except system_hlid) */
afaf8bdb 2227 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2228
2229 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2230 wlvif->bss_type == BSS_TYPE_IBSS) {
2231 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2232 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2233 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2234 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2235 } else {
2236 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2237 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2238 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2239 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2240 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2241 wl12xx_free_rate_policy(wl,
2242 &wlvif->ap.ucast_rate_idx[i]);
2243 }
b78b47eb 2244
d6a3cc2e 2245 wl12xx_tx_reset_wlvif(wl, wlvif);
170d0e67 2246 wl1271_free_ap_keys(wl, wlvif);
e4120df9
EP
2247 if (wl->last_wlvif == wlvif)
2248 wl->last_wlvif = NULL;
87627214 2249 list_del(&wlvif->list);
c7ffb902 2250 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
0603d891 2251 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
7edebf56 2252 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
d6e19d13 2253
a4e4130d
EP
2254 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2255 wl->ap_count--;
2256 else
2257 wl->sta_count--;
2258
baf6277a 2259 mutex_unlock(&wl->mutex);
d6bf9ada 2260
9eb599e9
EP
2261 del_timer_sync(&wlvif->rx_streaming_timer);
2262 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2263 cancel_work_sync(&wlvif->rx_streaming_disable_work);
bd9dc49c 2264
baf6277a 2265 mutex_lock(&wl->mutex);
52a2a375 2266}
bd9dc49c 2267
52a2a375
JO
2268static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2269 struct ieee80211_vif *vif)
2270{
2271 struct wl1271 *wl = hw->priv;
10c8cd01 2272 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
6e8cd331 2273 struct wl12xx_vif *iter;
52a2a375
JO
2274
2275 mutex_lock(&wl->mutex);
10c8cd01
EP
2276
2277 if (wl->state == WL1271_STATE_OFF ||
2278 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2279 goto out;
2280
67353299
JO
2281 /*
2282 * wl->vif can be null here if someone shuts down the interface
2283 * just when hardware recovery has been started.
2284 */
6e8cd331
EP
2285 wl12xx_for_each_wlvif(wl, iter) {
2286 if (iter != wlvif)
2287 continue;
2288
536129c8 2289 __wl1271_op_remove_interface(wl, vif, true);
6e8cd331 2290 break;
67353299 2291 }
6e8cd331 2292 WARN_ON(iter != wlvif);
10c8cd01 2293out:
67353299 2294 mutex_unlock(&wl->mutex);
52b0e7a6 2295 cancel_work_sync(&wl->recovery_work);
f5fc0f86
LC
2296}
2297
c0fad1b7
EP
2298static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2299 struct ieee80211_vif *vif,
2300 enum nl80211_iftype new_type, bool p2p)
2301{
2302 wl1271_op_remove_interface(hw, vif);
2303
2304 vif->type = ieee80211_iftype_p2p(new_type, p2p);
2305 vif->p2p = p2p;
2306 return wl1271_op_add_interface(hw, vif);
2307}
2308
87fbcb0f
EP
2309static int wl1271_join(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2310 bool set_assoc)
82429d32
JO
2311{
2312 int ret;
536129c8 2313 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
82429d32 2314
69e5434c
JO
2315 /*
2316 * One of the side effects of the JOIN command is that is clears
2317 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2318 * to a WPA/WPA2 access point will therefore kill the data-path.
8bf69aae
OBC
2319 * Currently the only valid scenario for JOIN during association
2320 * is on roaming, in which case we will also be given new keys.
2321 * Keep the below message for now, unless it starts bothering
2322 * users who really like to roam a lot :)
69e5434c 2323 */
ba8447f6 2324 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
69e5434c
JO
2325 wl1271_info("JOIN while associated.");
2326
5ec8a448
EP
2327 /* clear encryption type */
2328 wlvif->encryption_type = KEY_NONE;
2329
69e5434c 2330 if (set_assoc)
ba8447f6 2331 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
69e5434c 2332
227e81e1 2333 if (is_ibss)
87fbcb0f 2334 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
227e81e1 2335 else
87fbcb0f 2336 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
82429d32
JO
2337 if (ret < 0)
2338 goto out;
2339
ba8447f6 2340 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
82429d32
JO
2341 goto out;
2342
2343 /*
2344 * The join command disable the keep-alive mode, shut down its process,
2345 * and also clear the template config, so we need to reset it all after
2346 * the join. The acx_aid starts the keep-alive process, and the order
2347 * of the commands below is relevant.
2348 */
0603d891 2349 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
82429d32
JO
2350 if (ret < 0)
2351 goto out;
2352
0603d891 2353 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
82429d32
JO
2354 if (ret < 0)
2355 goto out;
2356
d2d66c56 2357 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
82429d32
JO
2358 if (ret < 0)
2359 goto out;
2360
0603d891
EP
2361 ret = wl1271_acx_keep_alive_config(wl, wlvif,
2362 CMD_TEMPL_KLV_IDX_NULL_DATA,
82429d32
JO
2363 ACX_KEEP_ALIVE_TPL_VALID);
2364 if (ret < 0)
2365 goto out;
2366
2367out:
2368 return ret;
2369}
2370
0603d891 2371static int wl1271_unjoin(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c7f43e45
LC
2372{
2373 int ret;
2374
52630c5d 2375 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
6e8cd331
EP
2376 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2377
6d158ff3 2378 wl12xx_cmd_stop_channel_switch(wl);
6e8cd331 2379 ieee80211_chswitch_done(vif, false);
6d158ff3
SL
2380 }
2381
c7f43e45 2382 /* to stop listening to a channel, we disconnect */
0603d891 2383 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
c7f43e45
LC
2384 if (ret < 0)
2385 goto out;
2386
b992c682 2387 /* reset TX security counters on a clean disconnect */
48e93e40
EP
2388 wlvif->tx_security_last_seq_lsb = 0;
2389 wlvif->tx_security_seq = 0;
b992c682 2390
c7f43e45
LC
2391out:
2392 return ret;
2393}
2394
87fbcb0f 2395static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
ebba60c6 2396{
1b92f15e 2397 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
30d0c8fd 2398 wlvif->rate_set = wlvif->basic_rate_set;
ebba60c6
JO
2399}
2400
87fbcb0f
EP
2401static int wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2402 bool idle)
0d58cbff
JO
2403{
2404 int ret;
a0c7b782
EP
2405 bool cur_idle = !test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2406
2407 if (idle == cur_idle)
2408 return 0;
0d58cbff
JO
2409
2410 if (idle) {
251c177f 2411 /* no need to croc if we weren't busy (e.g. during boot) */
92e712da 2412 if (wl12xx_dev_role_started(wlvif)) {
679a6734 2413 ret = wl12xx_stop_dev(wl, wlvif);
0d58cbff
JO
2414 if (ret < 0)
2415 goto out;
2416 }
30d0c8fd
EP
2417 wlvif->rate_set =
2418 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2419 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
0d58cbff
JO
2420 if (ret < 0)
2421 goto out;
2422 ret = wl1271_acx_keep_alive_config(
0603d891 2423 wl, wlvif, CMD_TEMPL_KLV_IDX_NULL_DATA,
0d58cbff
JO
2424 ACX_KEEP_ALIVE_TPL_INVALID);
2425 if (ret < 0)
2426 goto out;
a0c7b782 2427 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
0d58cbff 2428 } else {
33c2c06c
LC
2429 /* The current firmware only supports sched_scan in idle */
2430 if (wl->sched_scanning) {
2431 wl1271_scan_sched_scan_stop(wl);
2432 ieee80211_sched_scan_stopped(wl->hw);
2433 }
2434
679a6734 2435 ret = wl12xx_start_dev(wl, wlvif);
0d58cbff
JO
2436 if (ret < 0)
2437 goto out;
a0c7b782 2438 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
0d58cbff
JO
2439 }
2440
2441out:
2442 return ret;
2443}
2444
9f259c4e
EP
2445static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2446 struct ieee80211_conf *conf, u32 changed)
f5fc0f86 2447{
9f259c4e
EP
2448 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2449 int channel, ret;
f5fc0f86
LC
2450
2451 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2452
ebba60c6 2453 /* if the channel changes while joined, join again */
69e5434c 2454 if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
1b92f15e 2455 ((wlvif->band != conf->channel->band) ||
61f845f4 2456 (wlvif->channel != channel))) {
c6930b07 2457 /* send all pending packets */
a32d0cdf 2458 wl1271_tx_work_locked(wl);
61f845f4
EP
2459 wlvif->band = conf->channel->band;
2460 wlvif->channel = channel;
ebba60c6 2461
bee0ffec
AN
2462 if (!is_ap) {
2463 /*
2464 * FIXME: the mac80211 should really provide a fixed
2465 * rate to use here. for now, just use the smallest
2466 * possible rate for the band as a fixed rate for
2467 * association frames and other control messages.
2468 */
ba8447f6 2469 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
87fbcb0f 2470 wl1271_set_band_rate(wl, wlvif);
bee0ffec 2471
d2d66c56 2472 wlvif->basic_rate =
87fbcb0f
EP
2473 wl1271_tx_min_rate_get(wl,
2474 wlvif->basic_rate_set);
30d0c8fd 2475 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
ebba60c6 2476 if (ret < 0)
bee0ffec 2477 wl1271_warning("rate policy for channel "
ebba60c6 2478 "failed %d", ret);
bee0ffec 2479
ba8447f6
EP
2480 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED,
2481 &wlvif->flags)) {
92e712da 2482 if (wl12xx_dev_role_started(wlvif)) {
251c177f 2483 /* roaming */
7edebf56
EP
2484 ret = wl12xx_croc(wl,
2485 wlvif->dev_role_id);
251c177f 2486 if (ret < 0)
9f259c4e 2487 return ret;
251c177f 2488 }
87fbcb0f 2489 ret = wl1271_join(wl, wlvif, false);
bee0ffec
AN
2490 if (ret < 0)
2491 wl1271_warning("cmd join on channel "
2492 "failed %d", ret);
251c177f
EP
2493 } else {
2494 /*
2495 * change the ROC channel. do it only if we are
2496 * not idle. otherwise, CROC will be called
2497 * anyway.
2498 */
92e712da 2499 if (wl12xx_dev_role_started(wlvif) &&
251c177f 2500 !(conf->flags & IEEE80211_CONF_IDLE)) {
679a6734 2501 ret = wl12xx_stop_dev(wl, wlvif);
251c177f 2502 if (ret < 0)
9f259c4e 2503 return ret;
251c177f 2504
679a6734 2505 ret = wl12xx_start_dev(wl, wlvif);
251c177f 2506 if (ret < 0)
679a6734 2507 return ret;
251c177f 2508 }
bee0ffec 2509 }
ebba60c6
JO
2510 }
2511 }
2512
d18da7fc
ES
2513 if ((changed & IEEE80211_CONF_CHANGE_PS) && !is_ap) {
2514
2515 if ((conf->flags & IEEE80211_CONF_PS) &&
2516 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
5c0dc2fc 2517 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
d18da7fc 2518
5c0dc2fc
ES
2519 int ps_mode;
2520 char *ps_mode_str;
2521
2522 if (wl->conf.conn.forced_ps) {
2523 ps_mode = STATION_POWER_SAVE_MODE;
2524 ps_mode_str = "forced";
2525 } else {
2526 ps_mode = STATION_AUTO_PS_MODE;
2527 ps_mode_str = "auto";
2528 }
2529
2530 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
2531
2532 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
f5fc0f86 2533
d18da7fc 2534 if (ret < 0)
5c0dc2fc
ES
2535 wl1271_warning("enter %s ps failed %d",
2536 ps_mode_str, ret);
f5fc0f86 2537
d18da7fc 2538 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
5c0dc2fc 2539 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
d18da7fc
ES
2540
2541 wl1271_debug(DEBUG_PSM, "auto ps disabled");
f5fc0f86 2542
0603d891 2543 ret = wl1271_ps_set_mode(wl, wlvif,
248a0018 2544 STATION_ACTIVE_MODE);
d18da7fc
ES
2545 if (ret < 0)
2546 wl1271_warning("exit auto ps failed %d", ret);
2547 }
f5fc0f86
LC
2548 }
2549
6bd65029 2550 if (conf->power_level != wlvif->power_level) {
0603d891 2551 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
f5fc0f86 2552 if (ret < 0)
9f259c4e 2553 return ret;
f5fc0f86 2554
6bd65029 2555 wlvif->power_level = conf->power_level;
f5fc0f86
LC
2556 }
2557
9f259c4e
EP
2558 return 0;
2559}
2560
2561static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2562{
2563 struct wl1271 *wl = hw->priv;
2564 struct wl12xx_vif *wlvif;
2565 struct ieee80211_conf *conf = &hw->conf;
2566 int channel, ret = 0;
2567
2568 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2569
2570 wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d %s"
2571 " changed 0x%x",
2572 channel,
2573 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2574 conf->power_level,
2575 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2576 changed);
2577
2578 /*
2579 * mac80211 will go to idle nearly immediately after transmitting some
2580 * frames, such as the deauth. To make sure those frames reach the air,
2581 * wait here until the TX queue is fully flushed.
2582 */
2583 if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
2584 (conf->flags & IEEE80211_CONF_IDLE))
2585 wl1271_tx_flush(wl);
2586
2587 mutex_lock(&wl->mutex);
2588
2589 /* we support configuring the channel and band even while off */
2590 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2591 wl->band = conf->channel->band;
2592 wl->channel = channel;
2593 }
2594
2595 if (changed & IEEE80211_CONF_CHANGE_POWER)
2596 wl->power_level = conf->power_level;
2597
2598 if (unlikely(wl->state == WL1271_STATE_OFF))
2599 goto out;
2600
2601 ret = wl1271_ps_elp_wakeup(wl);
2602 if (ret < 0)
2603 goto out;
2604
2605 /* configure each interface */
2606 wl12xx_for_each_wlvif(wl, wlvif) {
2607 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2608 if (ret < 0)
2609 goto out_sleep;
2610 }
2611
f5fc0f86
LC
2612out_sleep:
2613 wl1271_ps_elp_sleep(wl);
2614
2615out:
2616 mutex_unlock(&wl->mutex);
2617
2618 return ret;
2619}
2620
b54853f1
JO
2621struct wl1271_filter_params {
2622 bool enabled;
2623 int mc_list_length;
2624 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2625};
2626
22bedad3
JP
2627static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2628 struct netdev_hw_addr_list *mc_list)
c87dec9f 2629{
c87dec9f 2630 struct wl1271_filter_params *fp;
22bedad3 2631 struct netdev_hw_addr *ha;
2c10bb9c 2632 struct wl1271 *wl = hw->priv;
c87dec9f 2633
2c10bb9c
SD
2634 if (unlikely(wl->state == WL1271_STATE_OFF))
2635 return 0;
c87dec9f 2636
74441130 2637 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
c87dec9f
JO
2638 if (!fp) {
2639 wl1271_error("Out of memory setting filters.");
2640 return 0;
2641 }
2642
2643 /* update multicast filtering parameters */
c87dec9f 2644 fp->mc_list_length = 0;
22bedad3
JP
2645 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2646 fp->enabled = false;
2647 } else {
2648 fp->enabled = true;
2649 netdev_hw_addr_list_for_each(ha, mc_list) {
c87dec9f 2650 memcpy(fp->mc_list[fp->mc_list_length],
22bedad3 2651 ha->addr, ETH_ALEN);
c87dec9f 2652 fp->mc_list_length++;
22bedad3 2653 }
c87dec9f
JO
2654 }
2655
b54853f1 2656 return (u64)(unsigned long)fp;
c87dec9f 2657}
f5fc0f86 2658
b54853f1
JO
2659#define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2660 FIF_ALLMULTI | \
2661 FIF_FCSFAIL | \
2662 FIF_BCN_PRBRESP_PROMISC | \
2663 FIF_CONTROL | \
2664 FIF_OTHER_BSS)
2665
f5fc0f86
LC
2666static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2667 unsigned int changed,
c87dec9f 2668 unsigned int *total, u64 multicast)
f5fc0f86 2669{
b54853f1 2670 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
f5fc0f86 2671 struct wl1271 *wl = hw->priv;
6e8cd331 2672 struct wl12xx_vif *wlvif;
536129c8 2673
b54853f1 2674 int ret;
f5fc0f86 2675
7d057869
AN
2676 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2677 " total %x", changed, *total);
f5fc0f86 2678
b54853f1
JO
2679 mutex_lock(&wl->mutex);
2680
2c10bb9c
SD
2681 *total &= WL1271_SUPPORTED_FILTERS;
2682 changed &= WL1271_SUPPORTED_FILTERS;
2683
2684 if (unlikely(wl->state == WL1271_STATE_OFF))
b54853f1
JO
2685 goto out;
2686
a620865e 2687 ret = wl1271_ps_elp_wakeup(wl);
b54853f1
JO
2688 if (ret < 0)
2689 goto out;
2690
6e8cd331
EP
2691 wl12xx_for_each_wlvif(wl, wlvif) {
2692 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2693 if (*total & FIF_ALLMULTI)
2694 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2695 false,
2696 NULL, 0);
2697 else if (fp)
2698 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2699 fp->enabled,
2700 fp->mc_list,
2701 fp->mc_list_length);
2702 if (ret < 0)
2703 goto out_sleep;
2704 }
7d057869 2705 }
f5fc0f86 2706
08c1d1c7
EP
2707 /*
2708 * the fw doesn't provide an api to configure the filters. instead,
2709 * the filters configuration is based on the active roles / ROC
2710 * state.
2711 */
b54853f1
JO
2712
2713out_sleep:
2714 wl1271_ps_elp_sleep(wl);
2715
2716out:
2717 mutex_unlock(&wl->mutex);
14b228a0 2718 kfree(fp);
f5fc0f86
LC
2719}
2720
170d0e67
EP
2721static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2722 u8 id, u8 key_type, u8 key_size,
2723 const u8 *key, u8 hlid, u32 tx_seq_32,
2724 u16 tx_seq_16)
7f179b46
AN
2725{
2726 struct wl1271_ap_key *ap_key;
2727 int i;
2728
2729 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2730
2731 if (key_size > MAX_KEY_SIZE)
2732 return -EINVAL;
2733
2734 /*
2735 * Find next free entry in ap_keys. Also check we are not replacing
2736 * an existing key.
2737 */
2738 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67 2739 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
2740 break;
2741
170d0e67 2742 if (wlvif->ap.recorded_keys[i]->id == id) {
7f179b46
AN
2743 wl1271_warning("trying to record key replacement");
2744 return -EINVAL;
2745 }
2746 }
2747
2748 if (i == MAX_NUM_KEYS)
2749 return -EBUSY;
2750
2751 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2752 if (!ap_key)
2753 return -ENOMEM;
2754
2755 ap_key->id = id;
2756 ap_key->key_type = key_type;
2757 ap_key->key_size = key_size;
2758 memcpy(ap_key->key, key, key_size);
2759 ap_key->hlid = hlid;
2760 ap_key->tx_seq_32 = tx_seq_32;
2761 ap_key->tx_seq_16 = tx_seq_16;
2762
170d0e67 2763 wlvif->ap.recorded_keys[i] = ap_key;
7f179b46
AN
2764 return 0;
2765}
2766
170d0e67 2767static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
2768{
2769 int i;
2770
2771 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67
EP
2772 kfree(wlvif->ap.recorded_keys[i]);
2773 wlvif->ap.recorded_keys[i] = NULL;
7f179b46
AN
2774 }
2775}
2776
a8ab39a4 2777static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
2778{
2779 int i, ret = 0;
2780 struct wl1271_ap_key *key;
2781 bool wep_key_added = false;
2782
2783 for (i = 0; i < MAX_NUM_KEYS; i++) {
7f97b487 2784 u8 hlid;
170d0e67 2785 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
2786 break;
2787
170d0e67 2788 key = wlvif->ap.recorded_keys[i];
7f97b487
EP
2789 hlid = key->hlid;
2790 if (hlid == WL12XX_INVALID_LINK_ID)
a8ab39a4 2791 hlid = wlvif->ap.bcast_hlid;
7f97b487 2792
a8ab39a4 2793 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
2794 key->id, key->key_type,
2795 key->key_size, key->key,
7f97b487 2796 hlid, key->tx_seq_32,
7f179b46
AN
2797 key->tx_seq_16);
2798 if (ret < 0)
2799 goto out;
2800
2801 if (key->key_type == KEY_WEP)
2802 wep_key_added = true;
2803 }
2804
2805 if (wep_key_added) {
f75c753f 2806 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
a8ab39a4 2807 wlvif->ap.bcast_hlid);
7f179b46
AN
2808 if (ret < 0)
2809 goto out;
2810 }
2811
2812out:
170d0e67 2813 wl1271_free_ap_keys(wl, wlvif);
7f179b46
AN
2814 return ret;
2815}
2816
536129c8
EP
2817static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2818 u16 action, u8 id, u8 key_type,
7f179b46
AN
2819 u8 key_size, const u8 *key, u32 tx_seq_32,
2820 u16 tx_seq_16, struct ieee80211_sta *sta)
2821{
2822 int ret;
536129c8 2823 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
7f179b46
AN
2824
2825 if (is_ap) {
2826 struct wl1271_station *wl_sta;
2827 u8 hlid;
2828
2829 if (sta) {
2830 wl_sta = (struct wl1271_station *)sta->drv_priv;
2831 hlid = wl_sta->hlid;
2832 } else {
a8ab39a4 2833 hlid = wlvif->ap.bcast_hlid;
7f179b46
AN
2834 }
2835
53d40d0b 2836 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
7f179b46
AN
2837 /*
2838 * We do not support removing keys after AP shutdown.
2839 * Pretend we do to make mac80211 happy.
2840 */
2841 if (action != KEY_ADD_OR_REPLACE)
2842 return 0;
2843
170d0e67 2844 ret = wl1271_record_ap_key(wl, wlvif, id,
7f179b46
AN
2845 key_type, key_size,
2846 key, hlid, tx_seq_32,
2847 tx_seq_16);
2848 } else {
a8ab39a4 2849 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
7f179b46
AN
2850 id, key_type, key_size,
2851 key, hlid, tx_seq_32,
2852 tx_seq_16);
2853 }
2854
2855 if (ret < 0)
2856 return ret;
2857 } else {
2858 const u8 *addr;
2859 static const u8 bcast_addr[ETH_ALEN] = {
2860 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2861 };
2862
e9eb8cbe
GE
2863 /*
2864 * A STA set to GEM cipher requires 2 tx spare blocks.
2865 * Return to default value when GEM cipher key is removed
2866 */
2867 if (key_type == KEY_GEM) {
2868 if (action == KEY_ADD_OR_REPLACE)
2869 wl->tx_spare_blocks = 2;
2870 else if (action == KEY_REMOVE)
2871 wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
2872 }
2873
7f179b46
AN
2874 addr = sta ? sta->addr : bcast_addr;
2875
2876 if (is_zero_ether_addr(addr)) {
2877 /* We dont support TX only encryption */
2878 return -EOPNOTSUPP;
2879 }
2880
2881 /* The wl1271 does not allow to remove unicast keys - they
2882 will be cleared automatically on next CMD_JOIN. Ignore the
2883 request silently, as we dont want the mac80211 to emit
2884 an error message. */
2885 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
2886 return 0;
2887
010d3d30
EP
2888 /* don't remove key if hlid was already deleted */
2889 if (action == KEY_REMOVE &&
154da67c 2890 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
010d3d30
EP
2891 return 0;
2892
a8ab39a4 2893 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
7f179b46
AN
2894 id, key_type, key_size,
2895 key, addr, tx_seq_32,
2896 tx_seq_16);
2897 if (ret < 0)
2898 return ret;
2899
2900 /* the default WEP key needs to be configured at least once */
2901 if (key_type == KEY_WEP) {
c690ec81 2902 ret = wl12xx_cmd_set_default_wep_key(wl,
f75c753f
EP
2903 wlvif->default_key,
2904 wlvif->sta.hlid);
7f179b46
AN
2905 if (ret < 0)
2906 return ret;
2907 }
2908 }
2909
2910 return 0;
2911}
2912
f5fc0f86
LC
2913static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2914 struct ieee80211_vif *vif,
2915 struct ieee80211_sta *sta,
2916 struct ieee80211_key_conf *key_conf)
2917{
2918 struct wl1271 *wl = hw->priv;
536129c8 2919 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
f5fc0f86 2920 int ret;
ac4e4ce5
JO
2921 u32 tx_seq_32 = 0;
2922 u16 tx_seq_16 = 0;
f5fc0f86
LC
2923 u8 key_type;
2924
f5fc0f86
LC
2925 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
2926
7f179b46 2927 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
f5fc0f86 2928 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
97359d12 2929 key_conf->cipher, key_conf->keyidx,
f5fc0f86
LC
2930 key_conf->keylen, key_conf->flags);
2931 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
2932
f5fc0f86
LC
2933 mutex_lock(&wl->mutex);
2934
f8d9802f
JO
2935 if (unlikely(wl->state == WL1271_STATE_OFF)) {
2936 ret = -EAGAIN;
2937 goto out_unlock;
2938 }
2939
a620865e 2940 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
2941 if (ret < 0)
2942 goto out_unlock;
2943
97359d12
JB
2944 switch (key_conf->cipher) {
2945 case WLAN_CIPHER_SUITE_WEP40:
2946 case WLAN_CIPHER_SUITE_WEP104:
f5fc0f86
LC
2947 key_type = KEY_WEP;
2948
2949 key_conf->hw_key_idx = key_conf->keyidx;
2950 break;
97359d12 2951 case WLAN_CIPHER_SUITE_TKIP:
f5fc0f86
LC
2952 key_type = KEY_TKIP;
2953
2954 key_conf->hw_key_idx = key_conf->keyidx;
48e93e40
EP
2955 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2956 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
f5fc0f86 2957 break;
97359d12 2958 case WLAN_CIPHER_SUITE_CCMP:
f5fc0f86
LC
2959 key_type = KEY_AES;
2960
12d4b975 2961 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
48e93e40
EP
2962 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2963 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
f5fc0f86 2964 break;
7a55724e
JO
2965 case WL1271_CIPHER_SUITE_GEM:
2966 key_type = KEY_GEM;
48e93e40
EP
2967 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2968 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
7a55724e 2969 break;
f5fc0f86 2970 default:
97359d12 2971 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
f5fc0f86
LC
2972
2973 ret = -EOPNOTSUPP;
2974 goto out_sleep;
2975 }
2976
2977 switch (cmd) {
2978 case SET_KEY:
536129c8 2979 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
2980 key_conf->keyidx, key_type,
2981 key_conf->keylen, key_conf->key,
2982 tx_seq_32, tx_seq_16, sta);
f5fc0f86
LC
2983 if (ret < 0) {
2984 wl1271_error("Could not add or replace key");
2985 goto out_sleep;
2986 }
5ec8a448
EP
2987
2988 /*
2989 * reconfiguring arp response if the unicast (or common)
2990 * encryption key type was changed
2991 */
2992 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
2993 (sta || key_type == KEY_WEP) &&
2994 wlvif->encryption_type != key_type) {
2995 wlvif->encryption_type = key_type;
2996 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
2997 if (ret < 0) {
2998 wl1271_warning("build arp rsp failed: %d", ret);
2999 goto out_sleep;
3000 }
3001 }
f5fc0f86
LC
3002 break;
3003
3004 case DISABLE_KEY:
536129c8 3005 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
7f179b46
AN
3006 key_conf->keyidx, key_type,
3007 key_conf->keylen, key_conf->key,
3008 0, 0, sta);
f5fc0f86
LC
3009 if (ret < 0) {
3010 wl1271_error("Could not remove key");
3011 goto out_sleep;
3012 }
3013 break;
3014
3015 default:
3016 wl1271_error("Unsupported key cmd 0x%x", cmd);
3017 ret = -EOPNOTSUPP;
f5fc0f86
LC
3018 break;
3019 }
3020
3021out_sleep:
3022 wl1271_ps_elp_sleep(wl);
3023
3024out_unlock:
3025 mutex_unlock(&wl->mutex);
3026
f5fc0f86
LC
3027 return ret;
3028}
3029
3030static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
a060bbfe 3031 struct ieee80211_vif *vif,
f5fc0f86
LC
3032 struct cfg80211_scan_request *req)
3033{
3034 struct wl1271 *wl = hw->priv;
7edebf56
EP
3035 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3036
f5fc0f86
LC
3037 int ret;
3038 u8 *ssid = NULL;
abb0b3bf 3039 size_t len = 0;
f5fc0f86
LC
3040
3041 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3042
3043 if (req->n_ssids) {
3044 ssid = req->ssids[0].ssid;
abb0b3bf 3045 len = req->ssids[0].ssid_len;
f5fc0f86
LC
3046 }
3047
3048 mutex_lock(&wl->mutex);
3049
b739a42c
JO
3050 if (wl->state == WL1271_STATE_OFF) {
3051 /*
3052 * We cannot return -EBUSY here because cfg80211 will expect
3053 * a call to ieee80211_scan_completed if we do - in this case
3054 * there won't be any call.
3055 */
3056 ret = -EAGAIN;
3057 goto out;
3058 }
3059
a620865e 3060 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3061 if (ret < 0)
3062 goto out;
3063
92e712da
EP
3064 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
3065 test_bit(wlvif->role_id, wl->roc_map)) {
3066 /* don't allow scanning right now */
3067 ret = -EBUSY;
3068 goto out_sleep;
3069 }
3070
784f694d 3071 ret = wl1271_scan(hw->priv, vif, ssid, len, req);
251c177f 3072out_sleep:
f5fc0f86 3073 wl1271_ps_elp_sleep(wl);
f5fc0f86
LC
3074out:
3075 mutex_unlock(&wl->mutex);
3076
3077 return ret;
3078}
3079
73ecce31
EP
3080static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3081 struct ieee80211_vif *vif)
3082{
3083 struct wl1271 *wl = hw->priv;
3084 int ret;
3085
3086 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3087
3088 mutex_lock(&wl->mutex);
3089
3090 if (wl->state == WL1271_STATE_OFF)
3091 goto out;
3092
3093 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3094 goto out;
3095
3096 ret = wl1271_ps_elp_wakeup(wl);
3097 if (ret < 0)
3098 goto out;
3099
3100 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3101 ret = wl1271_scan_stop(wl);
3102 if (ret < 0)
3103 goto out_sleep;
3104 }
3105 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3106 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
784f694d 3107 wl->scan_vif = NULL;
73ecce31
EP
3108 wl->scan.req = NULL;
3109 ieee80211_scan_completed(wl->hw, true);
3110
3111out_sleep:
3112 wl1271_ps_elp_sleep(wl);
3113out:
3114 mutex_unlock(&wl->mutex);
3115
3116 cancel_delayed_work_sync(&wl->scan_complete_work);
3117}
3118
33c2c06c
LC
3119static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3120 struct ieee80211_vif *vif,
3121 struct cfg80211_sched_scan_request *req,
3122 struct ieee80211_sched_scan_ies *ies)
3123{
3124 struct wl1271 *wl = hw->priv;
536129c8 3125 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3126 int ret;
3127
3128 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3129
3130 mutex_lock(&wl->mutex);
3131
9e0dc890
PF
3132 if (wl->state == WL1271_STATE_OFF) {
3133 ret = -EAGAIN;
3134 goto out;
3135 }
3136
33c2c06c
LC
3137 ret = wl1271_ps_elp_wakeup(wl);
3138 if (ret < 0)
3139 goto out;
3140
536129c8 3141 ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
33c2c06c
LC
3142 if (ret < 0)
3143 goto out_sleep;
3144
536129c8 3145 ret = wl1271_scan_sched_scan_start(wl, wlvif);
33c2c06c
LC
3146 if (ret < 0)
3147 goto out_sleep;
3148
3149 wl->sched_scanning = true;
3150
3151out_sleep:
3152 wl1271_ps_elp_sleep(wl);
3153out:
3154 mutex_unlock(&wl->mutex);
3155 return ret;
3156}
3157
3158static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3159 struct ieee80211_vif *vif)
3160{
3161 struct wl1271 *wl = hw->priv;
3162 int ret;
3163
3164 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3165
3166 mutex_lock(&wl->mutex);
3167
9e0dc890
PF
3168 if (wl->state == WL1271_STATE_OFF)
3169 goto out;
3170
33c2c06c
LC
3171 ret = wl1271_ps_elp_wakeup(wl);
3172 if (ret < 0)
3173 goto out;
3174
3175 wl1271_scan_sched_scan_stop(wl);
3176
3177 wl1271_ps_elp_sleep(wl);
3178out:
3179 mutex_unlock(&wl->mutex);
3180}
3181
68d069c4
AN
3182static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3183{
3184 struct wl1271 *wl = hw->priv;
3185 int ret = 0;
3186
3187 mutex_lock(&wl->mutex);
3188
3189 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3190 ret = -EAGAIN;
3191 goto out;
3192 }
3193
a620865e 3194 ret = wl1271_ps_elp_wakeup(wl);
68d069c4
AN
3195 if (ret < 0)
3196 goto out;
3197
5f704d18 3198 ret = wl1271_acx_frag_threshold(wl, value);
68d069c4
AN
3199 if (ret < 0)
3200 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3201
3202 wl1271_ps_elp_sleep(wl);
3203
3204out:
3205 mutex_unlock(&wl->mutex);
3206
3207 return ret;
3208}
3209
f5fc0f86
LC
3210static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3211{
3212 struct wl1271 *wl = hw->priv;
6e8cd331 3213 struct wl12xx_vif *wlvif;
aecb0565 3214 int ret = 0;
f5fc0f86
LC
3215
3216 mutex_lock(&wl->mutex);
3217
f8d9802f
JO
3218 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3219 ret = -EAGAIN;
aecb0565 3220 goto out;
f8d9802f 3221 }
aecb0565 3222
a620865e 3223 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3224 if (ret < 0)
3225 goto out;
3226
6e8cd331
EP
3227 wl12xx_for_each_wlvif(wl, wlvif) {
3228 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3229 if (ret < 0)
3230 wl1271_warning("set rts threshold failed: %d", ret);
3231 }
f5fc0f86
LC
3232 wl1271_ps_elp_sleep(wl);
3233
3234out:
3235 mutex_unlock(&wl->mutex);
3236
3237 return ret;
3238}
3239
1fe9f161 3240static int wl1271_ssid_set(struct ieee80211_vif *vif, struct sk_buff *skb,
2f6724b2 3241 int offset)
30240fc7 3242{
1fe9f161 3243 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
889cb360
EP
3244 u8 ssid_len;
3245 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
3246 skb->len - offset);
30240fc7 3247
889cb360
EP
3248 if (!ptr) {
3249 wl1271_error("No SSID in IEs!");
3250 return -ENOENT;
3251 }
3252
3253 ssid_len = ptr[1];
3254 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
3255 wl1271_error("SSID is too long!");
3256 return -EINVAL;
30240fc7 3257 }
e78a287a 3258
1fe9f161
EP
3259 wlvif->ssid_len = ssid_len;
3260 memcpy(wlvif->ssid, ptr+2, ssid_len);
889cb360 3261 return 0;
30240fc7
JO
3262}
3263
d48055d9
EP
3264static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3265{
3266 int len;
3267 const u8 *next, *end = skb->data + skb->len;
3268 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3269 skb->len - ieoffset);
3270 if (!ie)
3271 return;
3272 len = ie[1] + 2;
3273 next = ie + len;
3274 memmove(ie, next, end - next);
3275 skb_trim(skb, skb->len - len);
3276}
3277
26b4bf2e
EP
3278static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3279 unsigned int oui, u8 oui_type,
3280 int ieoffset)
3281{
3282 int len;
3283 const u8 *next, *end = skb->data + skb->len;
3284 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3285 skb->data + ieoffset,
3286 skb->len - ieoffset);
3287 if (!ie)
3288 return;
3289 len = ie[1] + 2;
3290 next = ie + len;
3291 memmove(ie, next, end - next);
3292 skb_trim(skb, skb->len - len);
3293}
3294
341f2c11
AN
3295static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3296 struct ieee80211_vif *vif)
560f0024 3297{
cdaac628 3298 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
560f0024
AN
3299 struct sk_buff *skb;
3300 int ret;
3301
341f2c11 3302 skb = ieee80211_proberesp_get(wl->hw, vif);
560f0024 3303 if (!skb)
341f2c11 3304 return -EOPNOTSUPP;
560f0024 3305
cdaac628 3306 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
560f0024
AN
3307 CMD_TEMPL_AP_PROBE_RESPONSE,
3308 skb->data,
3309 skb->len, 0,
3310 rates);
3311
3312 dev_kfree_skb(skb);
3313 return ret;
3314}
3315
3316static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3317 struct ieee80211_vif *vif,
3318 u8 *probe_rsp_data,
3319 size_t probe_rsp_len,
3320 u32 rates)
68eaaf6e 3321{
1fe9f161
EP
3322 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3323 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
68eaaf6e
AN
3324 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3325 int ssid_ie_offset, ie_offset, templ_len;
3326 const u8 *ptr;
3327
3328 /* no need to change probe response if the SSID is set correctly */
1fe9f161 3329 if (wlvif->ssid_len > 0)
cdaac628 3330 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3331 CMD_TEMPL_AP_PROBE_RESPONSE,
3332 probe_rsp_data,
3333 probe_rsp_len, 0,
3334 rates);
3335
3336 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3337 wl1271_error("probe_rsp template too big");
3338 return -EINVAL;
3339 }
3340
3341 /* start searching from IE offset */
3342 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3343
3344 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3345 probe_rsp_len - ie_offset);
3346 if (!ptr) {
3347 wl1271_error("No SSID in beacon!");
3348 return -EINVAL;
3349 }
3350
3351 ssid_ie_offset = ptr - probe_rsp_data;
3352 ptr += (ptr[1] + 2);
3353
3354 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3355
3356 /* insert SSID from bss_conf */
3357 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3358 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3359 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3360 bss_conf->ssid, bss_conf->ssid_len);
3361 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3362
3363 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3364 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3365 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3366
cdaac628 3367 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3368 CMD_TEMPL_AP_PROBE_RESPONSE,
3369 probe_rsp_templ,
3370 templ_len, 0,
3371 rates);
3372}
3373
e78a287a 3374static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
0603d891 3375 struct ieee80211_vif *vif,
f5fc0f86
LC
3376 struct ieee80211_bss_conf *bss_conf,
3377 u32 changed)
3378{
0603d891 3379 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3380 int ret = 0;
f5fc0f86 3381
e78a287a
AN
3382 if (changed & BSS_CHANGED_ERP_SLOT) {
3383 if (bss_conf->use_short_slot)
0603d891 3384 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
e78a287a 3385 else
0603d891 3386 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
e78a287a
AN
3387 if (ret < 0) {
3388 wl1271_warning("Set slot time failed %d", ret);
3389 goto out;
3390 }
3391 }
f5fc0f86 3392
e78a287a
AN
3393 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3394 if (bss_conf->use_short_preamble)
0603d891 3395 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
e78a287a 3396 else
0603d891 3397 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
e78a287a 3398 }
f5fc0f86 3399
e78a287a
AN
3400 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3401 if (bss_conf->use_cts_prot)
0603d891
EP
3402 ret = wl1271_acx_cts_protect(wl, wlvif,
3403 CTSPROTECT_ENABLE);
e78a287a 3404 else
0603d891
EP
3405 ret = wl1271_acx_cts_protect(wl, wlvif,
3406 CTSPROTECT_DISABLE);
e78a287a
AN
3407 if (ret < 0) {
3408 wl1271_warning("Set ctsprotect failed %d", ret);
3409 goto out;
3410 }
3411 }
f8d9802f 3412
e78a287a
AN
3413out:
3414 return ret;
3415}
f5fc0f86 3416
e78a287a
AN
3417static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3418 struct ieee80211_vif *vif,
3419 struct ieee80211_bss_conf *bss_conf,
3420 u32 changed)
3421{
87fbcb0f 3422 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
536129c8 3423 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
3424 int ret = 0;
3425
3426 if ((changed & BSS_CHANGED_BEACON_INT)) {
3427 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
60e84c2e
JO
3428 bss_conf->beacon_int);
3429
6a899796 3430 wlvif->beacon_int = bss_conf->beacon_int;
60e84c2e
JO
3431 }
3432
560f0024
AN
3433 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3434 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
341f2c11
AN
3435 if (!wl1271_ap_set_probe_resp_tmpl(wl, rate, vif)) {
3436 wl1271_debug(DEBUG_AP, "probe response updated");
3437 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3438 }
560f0024
AN
3439 }
3440
e78a287a
AN
3441 if ((changed & BSS_CHANGED_BEACON)) {
3442 struct ieee80211_hdr *hdr;
af7fbb28 3443 u32 min_rate;
e78a287a
AN
3444 int ieoffset = offsetof(struct ieee80211_mgmt,
3445 u.beacon.variable);
3446 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3447 u16 tmpl_id;
3448
560f0024
AN
3449 if (!beacon) {
3450 ret = -EINVAL;
e78a287a 3451 goto out;
560f0024 3452 }
e78a287a
AN
3453
3454 wl1271_debug(DEBUG_MASTER, "beacon updated");
3455
1fe9f161 3456 ret = wl1271_ssid_set(vif, beacon, ieoffset);
e78a287a
AN
3457 if (ret < 0) {
3458 dev_kfree_skb(beacon);
3459 goto out;
3460 }
87fbcb0f 3461 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
e78a287a
AN
3462 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3463 CMD_TEMPL_BEACON;
cdaac628 3464 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
e78a287a
AN
3465 beacon->data,
3466 beacon->len, 0,
af7fbb28 3467 min_rate);
e78a287a
AN
3468 if (ret < 0) {
3469 dev_kfree_skb(beacon);
3470 goto out;
3471 }
3472
560f0024
AN
3473 /*
3474 * In case we already have a probe-resp beacon set explicitly
3475 * by usermode, don't use the beacon data.
3476 */
3477 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3478 goto end_bcn;
3479
d48055d9
EP
3480 /* remove TIM ie from probe response */
3481 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3482
26b4bf2e
EP
3483 /*
3484 * remove p2p ie from probe response.
3485 * the fw reponds to probe requests that don't include
3486 * the p2p ie. probe requests with p2p ie will be passed,
3487 * and will be responded by the supplicant (the spec
3488 * forbids including the p2p ie when responding to probe
3489 * requests that didn't include it).
3490 */
3491 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3492 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3493
e78a287a
AN
3494 hdr = (struct ieee80211_hdr *) beacon->data;
3495 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3496 IEEE80211_STYPE_PROBE_RESP);
68eaaf6e 3497 if (is_ap)
560f0024 3498 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
68eaaf6e
AN
3499 beacon->data,
3500 beacon->len,
af7fbb28 3501 min_rate);
68eaaf6e 3502 else
cdaac628 3503 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3504 CMD_TEMPL_PROBE_RESPONSE,
3505 beacon->data,
3506 beacon->len, 0,
af7fbb28 3507 min_rate);
560f0024 3508end_bcn:
e78a287a
AN
3509 dev_kfree_skb(beacon);
3510 if (ret < 0)
3511 goto out;
3512 }
3513
3514out:
560f0024
AN
3515 if (ret != 0)
3516 wl1271_error("beacon info change failed: %d", ret);
e78a287a
AN
3517 return ret;
3518}
3519
3520/* AP mode changes */
3521static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3522 struct ieee80211_vif *vif,
3523 struct ieee80211_bss_conf *bss_conf,
3524 u32 changed)
3525{
87fbcb0f 3526 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3527 int ret = 0;
e0d8bbf0 3528
e78a287a
AN
3529 if ((changed & BSS_CHANGED_BASIC_RATES)) {
3530 u32 rates = bss_conf->basic_rates;
5da11dcd 3531
87fbcb0f 3532 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3533 wlvif->band);
d2d66c56 3534 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
87fbcb0f 3535 wlvif->basic_rate_set);
70f47424 3536
87fbcb0f 3537 ret = wl1271_init_ap_rates(wl, wlvif);
e78a287a 3538 if (ret < 0) {
70f47424 3539 wl1271_error("AP rate policy change failed %d", ret);
e78a287a
AN
3540 goto out;
3541 }
c45a85b5 3542
784f694d 3543 ret = wl1271_ap_init_templates(wl, vif);
c45a85b5
AN
3544 if (ret < 0)
3545 goto out;
e78a287a 3546 }
2f6724b2 3547
e78a287a
AN
3548 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3549 if (ret < 0)
3550 goto out;
30240fc7 3551
e78a287a
AN
3552 if ((changed & BSS_CHANGED_BEACON_ENABLED)) {
3553 if (bss_conf->enable_beacon) {
53d40d0b 3554 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
87fbcb0f 3555 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
e78a287a
AN
3556 if (ret < 0)
3557 goto out;
e0d8bbf0 3558
a8ab39a4 3559 ret = wl1271_ap_init_hwenc(wl, wlvif);
7f179b46
AN
3560 if (ret < 0)
3561 goto out;
cf42039f 3562
53d40d0b 3563 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
cf42039f 3564 wl1271_debug(DEBUG_AP, "started AP");
e0d8bbf0 3565 }
e78a287a 3566 } else {
53d40d0b 3567 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
0603d891 3568 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
e78a287a
AN
3569 if (ret < 0)
3570 goto out;
e0d8bbf0 3571
53d40d0b 3572 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
560f0024
AN
3573 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3574 &wlvif->flags);
e78a287a
AN
3575 wl1271_debug(DEBUG_AP, "stopped AP");
3576 }
3577 }
3578 }
e0d8bbf0 3579
0603d891 3580 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
3581 if (ret < 0)
3582 goto out;
0b932ab9
AN
3583
3584 /* Handle HT information change */
3585 if ((changed & BSS_CHANGED_HT) &&
3586 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
0603d891 3587 ret = wl1271_acx_set_ht_information(wl, wlvif,
0b932ab9
AN
3588 bss_conf->ht_operation_mode);
3589 if (ret < 0) {
3590 wl1271_warning("Set ht information failed %d", ret);
3591 goto out;
3592 }
3593 }
3594
e78a287a
AN
3595out:
3596 return;
3597}
8bf29b0e 3598
e78a287a
AN
3599/* STA/IBSS mode changes */
3600static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3601 struct ieee80211_vif *vif,
3602 struct ieee80211_bss_conf *bss_conf,
3603 u32 changed)
3604{
87fbcb0f 3605 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3606 bool do_join = false, set_assoc = false;
536129c8 3607 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
227e81e1 3608 bool ibss_joined = false;
72c2d9e5 3609 u32 sta_rate_set = 0;
e78a287a 3610 int ret;
2d6e4e76 3611 struct ieee80211_sta *sta;
a100885d
AN
3612 bool sta_exists = false;
3613 struct ieee80211_sta_ht_cap sta_ht_cap;
e78a287a
AN
3614
3615 if (is_ibss) {
3616 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3617 changed);
3618 if (ret < 0)
3619 goto out;
e0d8bbf0
JO
3620 }
3621
227e81e1
EP
3622 if (changed & BSS_CHANGED_IBSS) {
3623 if (bss_conf->ibss_joined) {
eee514e3 3624 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
227e81e1
EP
3625 ibss_joined = true;
3626 } else {
eee514e3
EP
3627 if (test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED,
3628 &wlvif->flags)) {
0603d891 3629 wl1271_unjoin(wl, wlvif);
679a6734 3630 wl12xx_start_dev(wl, wlvif);
227e81e1
EP
3631 }
3632 }
3633 }
3634
3635 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
e78a287a
AN
3636 do_join = true;
3637
3638 /* Need to update the SSID (for filtering etc) */
227e81e1 3639 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
e78a287a
AN
3640 do_join = true;
3641
227e81e1 3642 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
5da11dcd
JO
3643 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3644 bss_conf->enable_beacon ? "enabled" : "disabled");
3645
5da11dcd
JO
3646 do_join = true;
3647 }
3648
c31e4946
EP
3649 if (changed & BSS_CHANGED_IDLE) {
3650 ret = wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
3651 if (ret < 0)
3652 wl1271_warning("idle mode change failed %d", ret);
3653 }
3654
e78a287a 3655 if ((changed & BSS_CHANGED_CQM)) {
00236aed
JO
3656 bool enable = false;
3657 if (bss_conf->cqm_rssi_thold)
3658 enable = true;
0603d891 3659 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
00236aed
JO
3660 bss_conf->cqm_rssi_thold,
3661 bss_conf->cqm_rssi_hyst);
3662 if (ret < 0)
3663 goto out;
04324d99 3664 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
00236aed
JO
3665 }
3666
7db4ee6d
EP
3667 if (changed & BSS_CHANGED_BSSID &&
3668 (is_ibss || bss_conf->assoc))
cdf09495 3669 if (!is_zero_ether_addr(bss_conf->bssid)) {
d2d66c56 3670 ret = wl12xx_cmd_build_null_data(wl, wlvif);
fa287b8f
EP
3671 if (ret < 0)
3672 goto out;
30240fc7 3673
784f694d 3674 ret = wl1271_build_qos_null_data(wl, vif);
fa287b8f
EP
3675 if (ret < 0)
3676 goto out;
30240fc7 3677
fa287b8f
EP
3678 /* Need to update the BSSID (for filtering etc) */
3679 do_join = true;
3680 }
30240fc7 3681
0f9c8250
AN
3682 if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_HT)) {
3683 rcu_read_lock();
3684 sta = ieee80211_find_sta(vif, bss_conf->bssid);
3685 if (!sta)
3686 goto sta_not_found;
3687
72c2d9e5
EP
3688 /* save the supp_rates of the ap */
3689 sta_rate_set = sta->supp_rates[wl->hw->conf.channel->band];
3690 if (sta->ht_cap.ht_supported)
3691 sta_rate_set |=
3692 (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET);
a100885d
AN
3693 sta_ht_cap = sta->ht_cap;
3694 sta_exists = true;
72c2d9e5 3695
0f9c8250
AN
3696sta_not_found:
3697 rcu_read_unlock();
72c2d9e5 3698 }
72c2d9e5 3699
e78a287a 3700 if ((changed & BSS_CHANGED_ASSOC)) {
f5fc0f86 3701 if (bss_conf->assoc) {
ebba60c6 3702 u32 rates;
2f6724b2 3703 int ieoffset;
6840e37a 3704 wlvif->aid = bss_conf->aid;
69e5434c 3705 set_assoc = true;
f5fc0f86 3706
ebba60c6
JO
3707 /*
3708 * use basic rates from AP, and determine lowest rate
3709 * to use with control frames.
3710 */
3711 rates = bss_conf->basic_rates;
87fbcb0f 3712 wlvif->basic_rate_set =
af7fbb28 3713 wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3714 wlvif->band);
d2d66c56 3715 wlvif->basic_rate =
87fbcb0f
EP
3716 wl1271_tx_min_rate_get(wl,
3717 wlvif->basic_rate_set);
72c2d9e5 3718 if (sta_rate_set)
30d0c8fd
EP
3719 wlvif->rate_set =
3720 wl1271_tx_enabled_rates_get(wl,
af7fbb28 3721 sta_rate_set,
1b92f15e 3722 wlvif->band);
30d0c8fd 3723 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
ebba60c6 3724 if (ret < 0)
e78a287a 3725 goto out;
ebba60c6 3726
ae751bab
LC
3727 /*
3728 * with wl1271, we don't need to update the
3729 * beacon_int and dtim_period, because the firmware
3730 * updates it by itself when the first beacon is
3731 * received after a join.
3732 */
6840e37a 3733 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
f5fc0f86 3734 if (ret < 0)
e78a287a 3735 goto out;
f5fc0f86 3736
c2b2d99b 3737 /*
2f6724b2 3738 * Get a template for hardware connection maintenance
c2b2d99b 3739 */
bddb29b8
EP
3740 dev_kfree_skb(wlvif->probereq);
3741 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
83587505 3742 wlvif,
bddb29b8 3743 NULL);
2f6724b2
JO
3744 ieoffset = offsetof(struct ieee80211_mgmt,
3745 u.probe_req.variable);
bddb29b8 3746 wl1271_ssid_set(vif, wlvif->probereq, ieoffset);
c2b2d99b 3747
6ccbb92e 3748 /* enable the connection monitoring feature */
0603d891 3749 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
f5fc0f86 3750 if (ret < 0)
e78a287a 3751 goto out;
d94cd297
JO
3752 } else {
3753 /* use defaults when not associated */
30df14d0 3754 bool was_assoc =
ba8447f6
EP
3755 !!test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED,
3756 &wlvif->flags);
251c177f 3757 bool was_ifup =
8181aecc
EP
3758 !!test_and_clear_bit(WLVIF_FLAG_STA_STATE_SENT,
3759 &wlvif->flags);
6840e37a 3760 wlvif->aid = 0;
6ccbb92e 3761
2f6724b2 3762 /* free probe-request template */
bddb29b8
EP
3763 dev_kfree_skb(wlvif->probereq);
3764 wlvif->probereq = NULL;
2f6724b2 3765
ebba60c6 3766 /* revert back to minimum rates for the current band */
87fbcb0f 3767 wl1271_set_band_rate(wl, wlvif);
d2d66c56 3768 wlvif->basic_rate =
87fbcb0f
EP
3769 wl1271_tx_min_rate_get(wl,
3770 wlvif->basic_rate_set);
30d0c8fd 3771 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
ebba60c6 3772 if (ret < 0)
e78a287a 3773 goto out;
ebba60c6 3774
6ccbb92e 3775 /* disable connection monitor features */
0603d891 3776 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
c1899554
JO
3777
3778 /* Disable the keep-alive feature */
0603d891 3779 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
6ccbb92e 3780 if (ret < 0)
e78a287a 3781 goto out;
b84a7d3d
JO
3782
3783 /* restore the bssid filter and go to dummy bssid */
30df14d0 3784 if (was_assoc) {
251c177f
EP
3785 u32 conf_flags = wl->hw->conf.flags;
3786 /*
3787 * we might have to disable roc, if there was
3788 * no IF_OPER_UP notification.
3789 */
3790 if (!was_ifup) {
0603d891 3791 ret = wl12xx_croc(wl, wlvif->role_id);
251c177f
EP
3792 if (ret < 0)
3793 goto out;
3794 }
3795 /*
3796 * (we also need to disable roc in case of
3797 * roaming on the same channel. until we will
3798 * have a better flow...)
3799 */
7edebf56
EP
3800 if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
3801 ret = wl12xx_croc(wl,
3802 wlvif->dev_role_id);
251c177f
EP
3803 if (ret < 0)
3804 goto out;
3805 }
3806
0603d891 3807 wl1271_unjoin(wl, wlvif);
679a6734
EP
3808 if (!(conf_flags & IEEE80211_CONF_IDLE))
3809 wl12xx_start_dev(wl, wlvif);
30df14d0 3810 }
f5fc0f86 3811 }
f5fc0f86
LC
3812 }
3813
d192d268
EP
3814 if (changed & BSS_CHANGED_IBSS) {
3815 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
3816 bss_conf->ibss_joined);
3817
3818 if (bss_conf->ibss_joined) {
3819 u32 rates = bss_conf->basic_rates;
87fbcb0f 3820 wlvif->basic_rate_set =
af7fbb28 3821 wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3822 wlvif->band);
d2d66c56 3823 wlvif->basic_rate =
87fbcb0f
EP
3824 wl1271_tx_min_rate_get(wl,
3825 wlvif->basic_rate_set);
d192d268 3826
06b660e1 3827 /* by default, use 11b + OFDM rates */
30d0c8fd
EP
3828 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
3829 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
d192d268
EP
3830 if (ret < 0)
3831 goto out;
3832 }
3833 }
3834
0603d891 3835 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
3836 if (ret < 0)
3837 goto out;
f5fc0f86 3838
8bf29b0e 3839 if (do_join) {
87fbcb0f 3840 ret = wl1271_join(wl, wlvif, set_assoc);
8bf29b0e
JO
3841 if (ret < 0) {
3842 wl1271_warning("cmd join failed %d", ret);
e78a287a 3843 goto out;
8bf29b0e 3844 }
251c177f
EP
3845
3846 /* ROC until connected (after EAPOL exchange) */
3847 if (!is_ibss) {
1b92f15e 3848 ret = wl12xx_roc(wl, wlvif, wlvif->role_id);
251c177f
EP
3849 if (ret < 0)
3850 goto out;
3851
ba8447f6 3852 wl1271_check_operstate(wl, wlvif,
251c177f
EP
3853 ieee80211_get_operstate(vif));
3854 }
3855 /*
3856 * stop device role if started (we might already be in
92e712da 3857 * STA/IBSS role).
251c177f 3858 */
92e712da 3859 if (wl12xx_dev_role_started(wlvif)) {
679a6734 3860 ret = wl12xx_stop_dev(wl, wlvif);
251c177f
EP
3861 if (ret < 0)
3862 goto out;
3863 }
c1899554
JO
3864 }
3865
0b932ab9 3866 /* Handle new association with HT. Do this after join. */
0f9c8250
AN
3867 if (sta_exists) {
3868 if ((changed & BSS_CHANGED_HT) &&
3869 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
0b932ab9
AN
3870 ret = wl1271_acx_set_ht_capabilities(wl,
3871 &sta_ht_cap,
3872 true,
154da67c 3873 wlvif->sta.hlid);
0f9c8250
AN
3874 if (ret < 0) {
3875 wl1271_warning("Set ht cap true failed %d",
3876 ret);
3877 goto out;
3878 }
3879 }
3880 /* handle new association without HT and disassociation */
3881 else if (changed & BSS_CHANGED_ASSOC) {
0b932ab9
AN
3882 ret = wl1271_acx_set_ht_capabilities(wl,
3883 &sta_ht_cap,
3884 false,
154da67c 3885 wlvif->sta.hlid);
0f9c8250
AN
3886 if (ret < 0) {
3887 wl1271_warning("Set ht cap false failed %d",
3888 ret);
3889 goto out;
3890 }
3891 }
3892 }
3893
0b932ab9
AN
3894 /* Handle HT information change. Done after join. */
3895 if ((changed & BSS_CHANGED_HT) &&
0f9c8250 3896 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
0603d891 3897 ret = wl1271_acx_set_ht_information(wl, wlvif,
0f9c8250
AN
3898 bss_conf->ht_operation_mode);
3899 if (ret < 0) {
3900 wl1271_warning("Set ht information failed %d", ret);
3901 goto out;
3902 }
3903 }
3904
76a74c8a
EP
3905 /* Handle arp filtering. Done after join. */
3906 if ((changed & BSS_CHANGED_ARP_FILTER) ||
3907 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
3908 __be32 addr = bss_conf->arp_addr_list[0];
3909 wlvif->sta.qos = bss_conf->qos;
3910 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
3911
3912 if (bss_conf->arp_addr_cnt == 1 &&
3913 bss_conf->arp_filter_enabled) {
3914 wlvif->ip_addr = addr;
3915 /*
3916 * The template should have been configured only upon
3917 * association. however, it seems that the correct ip
3918 * isn't being set (when sending), so we have to
3919 * reconfigure the template upon every ip change.
3920 */
3921 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3922 if (ret < 0) {
3923 wl1271_warning("build arp rsp failed: %d", ret);
3924 goto out;
3925 }
3926
3927 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
3928 (ACX_ARP_FILTER_ARP_FILTERING |
3929 ACX_ARP_FILTER_AUTO_ARP),
3930 addr);
3931 } else {
3932 wlvif->ip_addr = 0;
3933 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
3934 }
3935
3936 if (ret < 0)
3937 goto out;
3938 }
3939
e78a287a
AN
3940out:
3941 return;
3942}
3943
3944static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
3945 struct ieee80211_vif *vif,
3946 struct ieee80211_bss_conf *bss_conf,
3947 u32 changed)
3948{
3949 struct wl1271 *wl = hw->priv;
536129c8
EP
3950 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3951 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
3952 int ret;
3953
3954 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed 0x%x",
3955 (int)changed);
3956
3957 mutex_lock(&wl->mutex);
3958
3959 if (unlikely(wl->state == WL1271_STATE_OFF))
3960 goto out;
3961
10c8cd01
EP
3962 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
3963 goto out;
3964
a620865e 3965 ret = wl1271_ps_elp_wakeup(wl);
e78a287a
AN
3966 if (ret < 0)
3967 goto out;
3968
3969 if (is_ap)
3970 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
3971 else
3972 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
3973
f5fc0f86
LC
3974 wl1271_ps_elp_sleep(wl);
3975
3976out:
3977 mutex_unlock(&wl->mutex);
3978}
3979
8a3a3c85
EP
3980static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
3981 struct ieee80211_vif *vif, u16 queue,
c6999d83
KV
3982 const struct ieee80211_tx_queue_params *params)
3983{
3984 struct wl1271 *wl = hw->priv;
0603d891 3985 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4695dc91 3986 u8 ps_scheme;
488fc540 3987 int ret = 0;
c6999d83
KV
3988
3989 mutex_lock(&wl->mutex);
3990
3991 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
3992
4695dc91
KV
3993 if (params->uapsd)
3994 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
3995 else
3996 ps_scheme = CONF_PS_SCHEME_LEGACY;
3997
5b37ddfe 3998 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
c1b193eb 3999 goto out;
488fc540 4000
c1b193eb
EP
4001 ret = wl1271_ps_elp_wakeup(wl);
4002 if (ret < 0)
4003 goto out;
488fc540 4004
c1b193eb
EP
4005 /*
4006 * the txop is confed in units of 32us by the mac80211,
4007 * we need us
4008 */
0603d891 4009 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4010 params->cw_min, params->cw_max,
4011 params->aifs, params->txop << 5);
4012 if (ret < 0)
4013 goto out_sleep;
4014
0603d891 4015 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4016 CONF_CHANNEL_TYPE_EDCF,
4017 wl1271_tx_get_queue(queue),
4018 ps_scheme, CONF_ACK_POLICY_LEGACY,
4019 0, 0);
c82c1dde
KV
4020
4021out_sleep:
c1b193eb 4022 wl1271_ps_elp_sleep(wl);
c6999d83
KV
4023
4024out:
4025 mutex_unlock(&wl->mutex);
4026
4027 return ret;
4028}
4029
37a41b4a
EP
4030static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4031 struct ieee80211_vif *vif)
bbbb538e
JO
4032{
4033
4034 struct wl1271 *wl = hw->priv;
9c531149 4035 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbbb538e
JO
4036 u64 mactime = ULLONG_MAX;
4037 int ret;
4038
4039 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4040
4041 mutex_lock(&wl->mutex);
4042
f8d9802f
JO
4043 if (unlikely(wl->state == WL1271_STATE_OFF))
4044 goto out;
4045
a620865e 4046 ret = wl1271_ps_elp_wakeup(wl);
bbbb538e
JO
4047 if (ret < 0)
4048 goto out;
4049
9c531149 4050 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
bbbb538e
JO
4051 if (ret < 0)
4052 goto out_sleep;
4053
4054out_sleep:
4055 wl1271_ps_elp_sleep(wl);
4056
4057out:
4058 mutex_unlock(&wl->mutex);
4059 return mactime;
4060}
f5fc0f86 4061
ece550d0
JL
4062static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4063 struct survey_info *survey)
4064{
4065 struct wl1271 *wl = hw->priv;
4066 struct ieee80211_conf *conf = &hw->conf;
b739a42c 4067
ece550d0
JL
4068 if (idx != 0)
4069 return -ENOENT;
b739a42c 4070
ece550d0
JL
4071 survey->channel = conf->channel;
4072 survey->filled = SURVEY_INFO_NOISE_DBM;
4073 survey->noise = wl->noise;
b739a42c 4074
ece550d0
JL
4075 return 0;
4076}
4077
409622ec 4078static int wl1271_allocate_sta(struct wl1271 *wl,
c7ffb902
EP
4079 struct wl12xx_vif *wlvif,
4080 struct ieee80211_sta *sta)
f84f7d78
AN
4081{
4082 struct wl1271_station *wl_sta;
c7ffb902 4083 int ret;
f84f7d78 4084
c7ffb902
EP
4085
4086 if (wl->active_sta_count >= AP_MAX_STATIONS) {
f84f7d78
AN
4087 wl1271_warning("could not allocate HLID - too much stations");
4088 return -EBUSY;
4089 }
4090
4091 wl_sta = (struct wl1271_station *)sta->drv_priv;
c7ffb902
EP
4092 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4093 if (ret < 0) {
4094 wl1271_warning("could not allocate HLID - too many links");
4095 return -EBUSY;
4096 }
4097
4098 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
b622d992 4099 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
da03209e 4100 wl->active_sta_count++;
f84f7d78
AN
4101 return 0;
4102}
4103
c7ffb902 4104void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
f84f7d78 4105{
c7ffb902 4106 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
f1acea9a
AN
4107 return;
4108
c7ffb902 4109 clear_bit(hlid, wlvif->ap.sta_hlid_map);
b622d992 4110 memset(wl->links[hlid].addr, 0, ETH_ALEN);
0f9c8250 4111 wl->links[hlid].ba_bitmap = 0;
a8c0ddb5 4112 wl1271_tx_reset_link_queues(wl, hlid);
b622d992
AN
4113 __clear_bit(hlid, &wl->ap_ps_map);
4114 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
c7ffb902 4115 wl12xx_free_link(wl, wlvif, &hlid);
da03209e 4116 wl->active_sta_count--;
f84f7d78
AN
4117}
4118
4119static int wl1271_op_sta_add(struct ieee80211_hw *hw,
4120 struct ieee80211_vif *vif,
4121 struct ieee80211_sta *sta)
4122{
4123 struct wl1271 *wl = hw->priv;
536129c8 4124 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
c7ffb902 4125 struct wl1271_station *wl_sta;
f84f7d78
AN
4126 int ret = 0;
4127 u8 hlid;
4128
4129 mutex_lock(&wl->mutex);
4130
4131 if (unlikely(wl->state == WL1271_STATE_OFF))
4132 goto out;
4133
536129c8 4134 if (wlvif->bss_type != BSS_TYPE_AP_BSS)
f84f7d78
AN
4135 goto out;
4136
4137 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4138
c7ffb902 4139 ret = wl1271_allocate_sta(wl, wlvif, sta);
f84f7d78
AN
4140 if (ret < 0)
4141 goto out;
4142
c7ffb902
EP
4143 wl_sta = (struct wl1271_station *)sta->drv_priv;
4144 hlid = wl_sta->hlid;
4145
a620865e 4146 ret = wl1271_ps_elp_wakeup(wl);
f84f7d78 4147 if (ret < 0)
409622ec 4148 goto out_free_sta;
f84f7d78 4149
1b92f15e 4150 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
f84f7d78
AN
4151 if (ret < 0)
4152 goto out_sleep;
4153
b67476ef
EP
4154 ret = wl12xx_cmd_set_peer_state(wl, hlid);
4155 if (ret < 0)
4156 goto out_sleep;
4157
0b932ab9
AN
4158 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true, hlid);
4159 if (ret < 0)
4160 goto out_sleep;
4161
f84f7d78
AN
4162out_sleep:
4163 wl1271_ps_elp_sleep(wl);
4164
409622ec
AN
4165out_free_sta:
4166 if (ret < 0)
c7ffb902 4167 wl1271_free_sta(wl, wlvif, hlid);
409622ec 4168
f84f7d78
AN
4169out:
4170 mutex_unlock(&wl->mutex);
4171 return ret;
4172}
4173
4174static int wl1271_op_sta_remove(struct ieee80211_hw *hw,
4175 struct ieee80211_vif *vif,
4176 struct ieee80211_sta *sta)
4177{
4178 struct wl1271 *wl = hw->priv;
536129c8 4179 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
f84f7d78
AN
4180 struct wl1271_station *wl_sta;
4181 int ret = 0, id;
4182
4183 mutex_lock(&wl->mutex);
4184
4185 if (unlikely(wl->state == WL1271_STATE_OFF))
4186 goto out;
4187
536129c8 4188 if (wlvif->bss_type != BSS_TYPE_AP_BSS)
f84f7d78
AN
4189 goto out;
4190
4191 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4192
4193 wl_sta = (struct wl1271_station *)sta->drv_priv;
c7ffb902
EP
4194 id = wl_sta->hlid;
4195 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
f84f7d78
AN
4196 goto out;
4197
a620865e 4198 ret = wl1271_ps_elp_wakeup(wl);
f84f7d78
AN
4199 if (ret < 0)
4200 goto out;
4201
c690ec81 4202 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
f84f7d78
AN
4203 if (ret < 0)
4204 goto out_sleep;
4205
c7ffb902 4206 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
f84f7d78
AN
4207
4208out_sleep:
4209 wl1271_ps_elp_sleep(wl);
4210
4211out:
4212 mutex_unlock(&wl->mutex);
4213 return ret;
4214}
4215
4623ec7d
LC
4216static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4217 struct ieee80211_vif *vif,
4218 enum ieee80211_ampdu_mlme_action action,
4219 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4220 u8 buf_size)
bbba3e68
LS
4221{
4222 struct wl1271 *wl = hw->priv;
536129c8 4223 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbba3e68 4224 int ret;
0f9c8250
AN
4225 u8 hlid, *ba_bitmap;
4226
4227 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4228 tid);
4229
4230 /* sanity check - the fields in FW are only 8bits wide */
4231 if (WARN_ON(tid > 0xFF))
4232 return -ENOTSUPP;
bbba3e68
LS
4233
4234 mutex_lock(&wl->mutex);
4235
4236 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4237 ret = -EAGAIN;
4238 goto out;
4239 }
4240
536129c8 4241 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
154da67c 4242 hlid = wlvif->sta.hlid;
d0802abd 4243 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
536129c8 4244 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
0f9c8250
AN
4245 struct wl1271_station *wl_sta;
4246
4247 wl_sta = (struct wl1271_station *)sta->drv_priv;
4248 hlid = wl_sta->hlid;
4249 ba_bitmap = &wl->links[hlid].ba_bitmap;
4250 } else {
4251 ret = -EINVAL;
4252 goto out;
4253 }
4254
a620865e 4255 ret = wl1271_ps_elp_wakeup(wl);
bbba3e68
LS
4256 if (ret < 0)
4257 goto out;
4258
70559a06
SL
4259 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4260 tid, action);
4261
bbba3e68
LS
4262 switch (action) {
4263 case IEEE80211_AMPDU_RX_START:
d0802abd 4264 if (!wlvif->ba_support || !wlvif->ba_allowed) {
bbba3e68 4265 ret = -ENOTSUPP;
0f9c8250
AN
4266 break;
4267 }
4268
4269 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4270 ret = -EBUSY;
4271 wl1271_error("exceeded max RX BA sessions");
4272 break;
4273 }
4274
4275 if (*ba_bitmap & BIT(tid)) {
4276 ret = -EINVAL;
4277 wl1271_error("cannot enable RX BA session on active "
4278 "tid: %d", tid);
4279 break;
4280 }
4281
4282 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4283 hlid);
4284 if (!ret) {
4285 *ba_bitmap |= BIT(tid);
4286 wl->ba_rx_session_count++;
bbba3e68
LS
4287 }
4288 break;
4289
4290 case IEEE80211_AMPDU_RX_STOP:
0f9c8250
AN
4291 if (!(*ba_bitmap & BIT(tid))) {
4292 ret = -EINVAL;
4293 wl1271_error("no active RX BA session on tid: %d",
4294 tid);
4295 break;
4296 }
4297
4298 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4299 hlid);
4300 if (!ret) {
4301 *ba_bitmap &= ~BIT(tid);
4302 wl->ba_rx_session_count--;
4303 }
bbba3e68
LS
4304 break;
4305
4306 /*
4307 * The BA initiator session management in FW independently.
4308 * Falling break here on purpose for all TX APDU commands.
4309 */
4310 case IEEE80211_AMPDU_TX_START:
4311 case IEEE80211_AMPDU_TX_STOP:
4312 case IEEE80211_AMPDU_TX_OPERATIONAL:
4313 ret = -EINVAL;
4314 break;
4315
4316 default:
4317 wl1271_error("Incorrect ampdu action id=%x\n", action);
4318 ret = -EINVAL;
4319 }
4320
4321 wl1271_ps_elp_sleep(wl);
4322
4323out:
4324 mutex_unlock(&wl->mutex);
4325
4326 return ret;
4327}
4328
af7fbb28
EP
4329static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4330 struct ieee80211_vif *vif,
4331 const struct cfg80211_bitrate_mask *mask)
4332{
83587505 4333 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
af7fbb28 4334 struct wl1271 *wl = hw->priv;
d6fa37c9 4335 int i, ret = 0;
af7fbb28
EP
4336
4337 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4338 mask->control[NL80211_BAND_2GHZ].legacy,
4339 mask->control[NL80211_BAND_5GHZ].legacy);
4340
4341 mutex_lock(&wl->mutex);
4342
4343 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
83587505 4344 wlvif->bitrate_masks[i] =
af7fbb28
EP
4345 wl1271_tx_enabled_rates_get(wl,
4346 mask->control[i].legacy,
4347 i);
d6fa37c9
EP
4348
4349 if (unlikely(wl->state == WL1271_STATE_OFF))
4350 goto out;
4351
4352 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4353 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4354
4355 ret = wl1271_ps_elp_wakeup(wl);
4356 if (ret < 0)
4357 goto out;
4358
4359 wl1271_set_band_rate(wl, wlvif);
4360 wlvif->basic_rate =
4361 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4362 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4363
4364 wl1271_ps_elp_sleep(wl);
4365 }
4366out:
af7fbb28
EP
4367 mutex_unlock(&wl->mutex);
4368
d6fa37c9 4369 return ret;
af7fbb28
EP
4370}
4371
6d158ff3
SL
4372static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4373 struct ieee80211_channel_switch *ch_switch)
4374{
4375 struct wl1271 *wl = hw->priv;
52630c5d 4376 struct wl12xx_vif *wlvif;
6d158ff3
SL
4377 int ret;
4378
4379 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4380
4381 mutex_lock(&wl->mutex);
4382
4383 if (unlikely(wl->state == WL1271_STATE_OFF)) {
6e8cd331
EP
4384 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4385 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4386 ieee80211_chswitch_done(vif, false);
4387 }
4388 goto out;
6d158ff3
SL
4389 }
4390
4391 ret = wl1271_ps_elp_wakeup(wl);
4392 if (ret < 0)
4393 goto out;
4394
52630c5d
EP
4395 /* TODO: change mac80211 to pass vif as param */
4396 wl12xx_for_each_wlvif_sta(wl, wlvif) {
8332f0f6 4397 ret = wl12xx_cmd_channel_switch(wl, wlvif, ch_switch);
6d158ff3 4398
52630c5d
EP
4399 if (!ret)
4400 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4401 }
6d158ff3
SL
4402
4403 wl1271_ps_elp_sleep(wl);
4404
4405out:
4406 mutex_unlock(&wl->mutex);
4407}
4408
33437893
AN
4409static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
4410{
4411 struct wl1271 *wl = hw->priv;
4412 bool ret = false;
4413
4414 mutex_lock(&wl->mutex);
4415
4416 if (unlikely(wl->state == WL1271_STATE_OFF))
4417 goto out;
4418
4419 /* packets are considered pending if in the TX queue or the FW */
f1a46384 4420 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
33437893
AN
4421out:
4422 mutex_unlock(&wl->mutex);
4423
4424 return ret;
4425}
4426
f5fc0f86
LC
4427/* can't be const, mac80211 writes to this */
4428static struct ieee80211_rate wl1271_rates[] = {
4429 { .bitrate = 10,
2b60100b
JO
4430 .hw_value = CONF_HW_BIT_RATE_1MBPS,
4431 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
f5fc0f86 4432 { .bitrate = 20,
2b60100b
JO
4433 .hw_value = CONF_HW_BIT_RATE_2MBPS,
4434 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
f5fc0f86
LC
4435 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4436 { .bitrate = 55,
2b60100b
JO
4437 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
4438 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
f5fc0f86
LC
4439 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4440 { .bitrate = 110,
2b60100b
JO
4441 .hw_value = CONF_HW_BIT_RATE_11MBPS,
4442 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
f5fc0f86
LC
4443 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4444 { .bitrate = 60,
2b60100b
JO
4445 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4446 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
f5fc0f86 4447 { .bitrate = 90,
2b60100b
JO
4448 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4449 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
f5fc0f86 4450 { .bitrate = 120,
2b60100b
JO
4451 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4452 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
f5fc0f86 4453 { .bitrate = 180,
2b60100b
JO
4454 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4455 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
f5fc0f86 4456 { .bitrate = 240,
2b60100b
JO
4457 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4458 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
f5fc0f86 4459 { .bitrate = 360,
2b60100b
JO
4460 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4461 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
f5fc0f86 4462 { .bitrate = 480,
2b60100b
JO
4463 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4464 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
f5fc0f86 4465 { .bitrate = 540,
2b60100b
JO
4466 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4467 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
f5fc0f86
LC
4468};
4469
fa97f46b 4470/* can't be const, mac80211 writes to this */
f5fc0f86 4471static struct ieee80211_channel wl1271_channels[] = {
a2d0e3f1 4472 { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
fa21c7a9 4473 { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
fa97f46b
JO
4474 { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
4475 { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
4476 { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
fa21c7a9 4477 { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
fa97f46b
JO
4478 { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
4479 { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
4480 { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
fa21c7a9 4481 { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
fa97f46b
JO
4482 { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
4483 { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
4484 { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
6c89b7b2 4485 { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
f5fc0f86
LC
4486};
4487
f876bb9a 4488/* mapping to indexes for wl1271_rates */
a0ea9493 4489static const u8 wl1271_rate_to_idx_2ghz[] = {
f876bb9a 4490 /* MCS rates are used only with 11n */
defe02c7 4491 7, /* CONF_HW_RXTX_RATE_MCS7_SGI */
18357850
SL
4492 7, /* CONF_HW_RXTX_RATE_MCS7 */
4493 6, /* CONF_HW_RXTX_RATE_MCS6 */
4494 5, /* CONF_HW_RXTX_RATE_MCS5 */
4495 4, /* CONF_HW_RXTX_RATE_MCS4 */
4496 3, /* CONF_HW_RXTX_RATE_MCS3 */
4497 2, /* CONF_HW_RXTX_RATE_MCS2 */
4498 1, /* CONF_HW_RXTX_RATE_MCS1 */
4499 0, /* CONF_HW_RXTX_RATE_MCS0 */
f876bb9a
JO
4500
4501 11, /* CONF_HW_RXTX_RATE_54 */
4502 10, /* CONF_HW_RXTX_RATE_48 */
4503 9, /* CONF_HW_RXTX_RATE_36 */
4504 8, /* CONF_HW_RXTX_RATE_24 */
4505
4506 /* TI-specific rate */
4507 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22 */
4508
4509 7, /* CONF_HW_RXTX_RATE_18 */
4510 6, /* CONF_HW_RXTX_RATE_12 */
4511 3, /* CONF_HW_RXTX_RATE_11 */
4512 5, /* CONF_HW_RXTX_RATE_9 */
4513 4, /* CONF_HW_RXTX_RATE_6 */
4514 2, /* CONF_HW_RXTX_RATE_5_5 */
4515 1, /* CONF_HW_RXTX_RATE_2 */
4516 0 /* CONF_HW_RXTX_RATE_1 */
4517};
4518
e8b03a2b
SL
4519/* 11n STA capabilities */
4520#define HW_RX_HIGHEST_RATE 72
4521
00d20100 4522#define WL12XX_HT_CAP { \
871d0c3b
SL
4523 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 | \
4524 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT), \
e8b03a2b
SL
4525 .ht_supported = true, \
4526 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K, \
4527 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
4528 .mcs = { \
4529 .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, \
4530 .rx_highest = cpu_to_le16(HW_RX_HIGHEST_RATE), \
4531 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
4532 }, \
4533}
4534
f5fc0f86
LC
4535/* can't be const, mac80211 writes to this */
4536static struct ieee80211_supported_band wl1271_band_2ghz = {
4537 .channels = wl1271_channels,
4538 .n_channels = ARRAY_SIZE(wl1271_channels),
4539 .bitrates = wl1271_rates,
4540 .n_bitrates = ARRAY_SIZE(wl1271_rates),
00d20100 4541 .ht_cap = WL12XX_HT_CAP,
f5fc0f86
LC
4542};
4543
1ebec3d7
TP
4544/* 5 GHz data rates for WL1273 */
4545static struct ieee80211_rate wl1271_rates_5ghz[] = {
4546 { .bitrate = 60,
4547 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4548 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4549 { .bitrate = 90,
4550 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4551 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4552 { .bitrate = 120,
4553 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4554 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4555 { .bitrate = 180,
4556 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4557 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4558 { .bitrate = 240,
4559 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4560 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4561 { .bitrate = 360,
4562 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4563 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4564 { .bitrate = 480,
4565 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4566 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4567 { .bitrate = 540,
4568 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4569 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4570};
4571
fa97f46b 4572/* 5 GHz band channels for WL1273 */
1ebec3d7 4573static struct ieee80211_channel wl1271_channels_5ghz[] = {
6cfa5cff
AN
4574 { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
4575 { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
4576 { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
4577 { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
4578 { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
4579 { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
4580 { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
4581 { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
4582 { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
4583 { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
4584 { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
4585 { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
4586 { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
4587 { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
4588 { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
4589 { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
4590 { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
4591 { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
4592 { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
4593 { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
4594 { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
4595 { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
4596 { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
4597 { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
4598 { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
4599 { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
4600 { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
4601 { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
4602 { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
4603 { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
4604 { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
4605 { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
4606 { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
4607 { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
1ebec3d7
TP
4608};
4609
f876bb9a 4610/* mapping to indexes for wl1271_rates_5ghz */
a0ea9493 4611static const u8 wl1271_rate_to_idx_5ghz[] = {
f876bb9a 4612 /* MCS rates are used only with 11n */
defe02c7 4613 7, /* CONF_HW_RXTX_RATE_MCS7_SGI */
18357850
SL
4614 7, /* CONF_HW_RXTX_RATE_MCS7 */
4615 6, /* CONF_HW_RXTX_RATE_MCS6 */
4616 5, /* CONF_HW_RXTX_RATE_MCS5 */
4617 4, /* CONF_HW_RXTX_RATE_MCS4 */
4618 3, /* CONF_HW_RXTX_RATE_MCS3 */
4619 2, /* CONF_HW_RXTX_RATE_MCS2 */
4620 1, /* CONF_HW_RXTX_RATE_MCS1 */
4621 0, /* CONF_HW_RXTX_RATE_MCS0 */
f876bb9a
JO
4622
4623 7, /* CONF_HW_RXTX_RATE_54 */
4624 6, /* CONF_HW_RXTX_RATE_48 */
4625 5, /* CONF_HW_RXTX_RATE_36 */
4626 4, /* CONF_HW_RXTX_RATE_24 */
4627
4628 /* TI-specific rate */
4629 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22 */
4630
4631 3, /* CONF_HW_RXTX_RATE_18 */
4632 2, /* CONF_HW_RXTX_RATE_12 */
4633 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_11 */
4634 1, /* CONF_HW_RXTX_RATE_9 */
4635 0, /* CONF_HW_RXTX_RATE_6 */
4636 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_5_5 */
4637 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_2 */
4638 CONF_HW_RXTX_RATE_UNSUPPORTED /* CONF_HW_RXTX_RATE_1 */
4639};
1ebec3d7
TP
4640
4641static struct ieee80211_supported_band wl1271_band_5ghz = {
4642 .channels = wl1271_channels_5ghz,
4643 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
4644 .bitrates = wl1271_rates_5ghz,
4645 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
00d20100 4646 .ht_cap = WL12XX_HT_CAP,
1ebec3d7
TP
4647};
4648
a0ea9493 4649static const u8 *wl1271_band_rate_to_idx[] = {
f876bb9a
JO
4650 [IEEE80211_BAND_2GHZ] = wl1271_rate_to_idx_2ghz,
4651 [IEEE80211_BAND_5GHZ] = wl1271_rate_to_idx_5ghz
4652};
4653
f5fc0f86
LC
4654static const struct ieee80211_ops wl1271_ops = {
4655 .start = wl1271_op_start,
4656 .stop = wl1271_op_stop,
4657 .add_interface = wl1271_op_add_interface,
4658 .remove_interface = wl1271_op_remove_interface,
c0fad1b7 4659 .change_interface = wl12xx_op_change_interface,
f634a4e7 4660#ifdef CONFIG_PM
402e4861
EP
4661 .suspend = wl1271_op_suspend,
4662 .resume = wl1271_op_resume,
f634a4e7 4663#endif
f5fc0f86 4664 .config = wl1271_op_config,
c87dec9f 4665 .prepare_multicast = wl1271_op_prepare_multicast,
f5fc0f86
LC
4666 .configure_filter = wl1271_op_configure_filter,
4667 .tx = wl1271_op_tx,
4668 .set_key = wl1271_op_set_key,
4669 .hw_scan = wl1271_op_hw_scan,
73ecce31 4670 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
33c2c06c
LC
4671 .sched_scan_start = wl1271_op_sched_scan_start,
4672 .sched_scan_stop = wl1271_op_sched_scan_stop,
f5fc0f86 4673 .bss_info_changed = wl1271_op_bss_info_changed,
68d069c4 4674 .set_frag_threshold = wl1271_op_set_frag_threshold,
f5fc0f86 4675 .set_rts_threshold = wl1271_op_set_rts_threshold,
c6999d83 4676 .conf_tx = wl1271_op_conf_tx,
bbbb538e 4677 .get_tsf = wl1271_op_get_tsf,
ece550d0 4678 .get_survey = wl1271_op_get_survey,
f84f7d78
AN
4679 .sta_add = wl1271_op_sta_add,
4680 .sta_remove = wl1271_op_sta_remove,
bbba3e68 4681 .ampdu_action = wl1271_op_ampdu_action,
33437893 4682 .tx_frames_pending = wl1271_tx_frames_pending,
af7fbb28 4683 .set_bitrate_mask = wl12xx_set_bitrate_mask,
6d158ff3 4684 .channel_switch = wl12xx_op_channel_switch,
c8c90873 4685 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
f5fc0f86
LC
4686};
4687
f876bb9a 4688
6a2de93b 4689u8 wl1271_rate_to_idx(int rate, enum ieee80211_band band)
f876bb9a
JO
4690{
4691 u8 idx;
4692
6a2de93b 4693 BUG_ON(band >= sizeof(wl1271_band_rate_to_idx)/sizeof(u8 *));
f876bb9a
JO
4694
4695 if (unlikely(rate >= CONF_HW_RXTX_RATE_MAX)) {
4696 wl1271_error("Illegal RX rate from HW: %d", rate);
4697 return 0;
4698 }
4699
6a2de93b 4700 idx = wl1271_band_rate_to_idx[band][rate];
f876bb9a
JO
4701 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
4702 wl1271_error("Unsupported RX rate from HW: %d", rate);
4703 return 0;
4704 }
4705
4706 return idx;
4707}
4708
7fc3a864
JO
4709static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
4710 struct device_attribute *attr,
4711 char *buf)
4712{
4713 struct wl1271 *wl = dev_get_drvdata(dev);
4714 ssize_t len;
4715
2f63b011 4716 len = PAGE_SIZE;
7fc3a864
JO
4717
4718 mutex_lock(&wl->mutex);
4719 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
4720 wl->sg_enabled);
4721 mutex_unlock(&wl->mutex);
4722
4723 return len;
4724
4725}
4726
4727static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
4728 struct device_attribute *attr,
4729 const char *buf, size_t count)
4730{
4731 struct wl1271 *wl = dev_get_drvdata(dev);
4732 unsigned long res;
4733 int ret;
4734
6277ed65 4735 ret = kstrtoul(buf, 10, &res);
7fc3a864
JO
4736 if (ret < 0) {
4737 wl1271_warning("incorrect value written to bt_coex_mode");
4738 return count;
4739 }
4740
4741 mutex_lock(&wl->mutex);
4742
4743 res = !!res;
4744
4745 if (res == wl->sg_enabled)
4746 goto out;
4747
4748 wl->sg_enabled = res;
4749
4750 if (wl->state == WL1271_STATE_OFF)
4751 goto out;
4752
a620865e 4753 ret = wl1271_ps_elp_wakeup(wl);
7fc3a864
JO
4754 if (ret < 0)
4755 goto out;
4756
4757 wl1271_acx_sg_enable(wl, wl->sg_enabled);
4758 wl1271_ps_elp_sleep(wl);
4759
4760 out:
4761 mutex_unlock(&wl->mutex);
4762 return count;
4763}
4764
4765static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
4766 wl1271_sysfs_show_bt_coex_state,
4767 wl1271_sysfs_store_bt_coex_state);
4768
d717fd61
JO
4769static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
4770 struct device_attribute *attr,
4771 char *buf)
4772{
4773 struct wl1271 *wl = dev_get_drvdata(dev);
4774 ssize_t len;
4775
2f63b011 4776 len = PAGE_SIZE;
d717fd61
JO
4777
4778 mutex_lock(&wl->mutex);
4779 if (wl->hw_pg_ver >= 0)
4780 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
4781 else
4782 len = snprintf(buf, len, "n/a\n");
4783 mutex_unlock(&wl->mutex);
4784
4785 return len;
4786}
4787
6f07b72a 4788static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
d717fd61
JO
4789 wl1271_sysfs_show_hw_pg_ver, NULL);
4790
95dac04f
IY
4791static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
4792 struct bin_attribute *bin_attr,
4793 char *buffer, loff_t pos, size_t count)
4794{
4795 struct device *dev = container_of(kobj, struct device, kobj);
4796 struct wl1271 *wl = dev_get_drvdata(dev);
4797 ssize_t len;
4798 int ret;
4799
4800 ret = mutex_lock_interruptible(&wl->mutex);
4801 if (ret < 0)
4802 return -ERESTARTSYS;
4803
4804 /* Let only one thread read the log at a time, blocking others */
4805 while (wl->fwlog_size == 0) {
4806 DEFINE_WAIT(wait);
4807
4808 prepare_to_wait_exclusive(&wl->fwlog_waitq,
4809 &wait,
4810 TASK_INTERRUPTIBLE);
4811
4812 if (wl->fwlog_size != 0) {
4813 finish_wait(&wl->fwlog_waitq, &wait);
4814 break;
4815 }
4816
4817 mutex_unlock(&wl->mutex);
4818
4819 schedule();
4820 finish_wait(&wl->fwlog_waitq, &wait);
4821
4822 if (signal_pending(current))
4823 return -ERESTARTSYS;
4824
4825 ret = mutex_lock_interruptible(&wl->mutex);
4826 if (ret < 0)
4827 return -ERESTARTSYS;
4828 }
4829
4830 /* Check if the fwlog is still valid */
4831 if (wl->fwlog_size < 0) {
4832 mutex_unlock(&wl->mutex);
4833 return 0;
4834 }
4835
4836 /* Seeking is not supported - old logs are not kept. Disregard pos. */
4837 len = min(count, (size_t)wl->fwlog_size);
4838 wl->fwlog_size -= len;
4839 memcpy(buffer, wl->fwlog, len);
4840
4841 /* Make room for new messages */
4842 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
4843
4844 mutex_unlock(&wl->mutex);
4845
4846 return len;
4847}
4848
4849static struct bin_attribute fwlog_attr = {
4850 .attr = {.name = "fwlog", .mode = S_IRUSR},
4851 .read = wl1271_sysfs_read_fwlog,
4852};
4853
5e037e74
LC
4854static bool wl12xx_mac_in_fuse(struct wl1271 *wl)
4855{
4856 bool supported = false;
4857 u8 major, minor;
4858
4859 if (wl->chip.id == CHIP_ID_1283_PG20) {
4860 major = WL128X_PG_GET_MAJOR(wl->hw_pg_ver);
4861 minor = WL128X_PG_GET_MINOR(wl->hw_pg_ver);
4862
4863 /* in wl128x we have the MAC address if the PG is >= (2, 1) */
4864 if (major > 2 || (major == 2 && minor >= 1))
4865 supported = true;
4866 } else {
4867 major = WL127X_PG_GET_MAJOR(wl->hw_pg_ver);
4868 minor = WL127X_PG_GET_MINOR(wl->hw_pg_ver);
4869
4870 /* in wl127x we have the MAC address if the PG is >= (3, 1) */
4871 if (major == 3 && minor >= 1)
4872 supported = true;
4873 }
4874
4875 wl1271_debug(DEBUG_PROBE,
4876 "PG Ver major = %d minor = %d, MAC %s present",
4877 major, minor, supported ? "is" : "is not");
4878
4879 return supported;
4880}
4881
4882static void wl12xx_derive_mac_addresses(struct wl1271 *wl,
4883 u32 oui, u32 nic, int n)
4884{
4885 int i;
4886
4887 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x, n %d",
4888 oui, nic, n);
4889
4890 if (nic + n - 1 > 0xffffff)
4891 wl1271_warning("NIC part of the MAC address wraps around!");
4892
4893 for (i = 0; i < n; i++) {
4894 wl->addresses[i].addr[0] = (u8)(oui >> 16);
4895 wl->addresses[i].addr[1] = (u8)(oui >> 8);
4896 wl->addresses[i].addr[2] = (u8) oui;
4897 wl->addresses[i].addr[3] = (u8)(nic >> 16);
4898 wl->addresses[i].addr[4] = (u8)(nic >> 8);
4899 wl->addresses[i].addr[5] = (u8) nic;
4900 nic++;
4901 }
4902
4903 wl->hw->wiphy->n_addresses = n;
4904 wl->hw->wiphy->addresses = wl->addresses;
4905}
4906
4907static void wl12xx_get_fuse_mac(struct wl1271 *wl)
4908{
4909 u32 mac1, mac2;
4910
4911 wl1271_set_partition(wl, &wl12xx_part_table[PART_DRPW]);
4912
4913 mac1 = wl1271_read32(wl, WL12XX_REG_FUSE_BD_ADDR_1);
4914 mac2 = wl1271_read32(wl, WL12XX_REG_FUSE_BD_ADDR_2);
4915
4916 /* these are the two parts of the BD_ADDR */
4917 wl->fuse_oui_addr = ((mac2 & 0xffff) << 8) +
4918 ((mac1 & 0xff000000) >> 24);
4919 wl->fuse_nic_addr = mac1 & 0xffffff;
4920
4921 wl1271_set_partition(wl, &wl12xx_part_table[PART_DOWN]);
4922}
4923
30c5dbd1
LC
4924static int wl12xx_get_hw_info(struct wl1271 *wl)
4925{
4926 int ret;
4927 u32 die_info;
4928
4929 ret = wl12xx_set_power_on(wl);
4930 if (ret < 0)
4931 goto out;
4932
4933 wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
4934
4935 if (wl->chip.id == CHIP_ID_1283_PG20)
4936 die_info = wl1271_top_reg_read(wl, WL128X_REG_FUSE_DATA_2_1);
4937 else
4938 die_info = wl1271_top_reg_read(wl, WL127X_REG_FUSE_DATA_2_1);
4939
4940 wl->hw_pg_ver = (s8) (die_info & PG_VER_MASK) >> PG_VER_OFFSET;
4941
5e037e74
LC
4942 if (!wl12xx_mac_in_fuse(wl)) {
4943 wl->fuse_oui_addr = 0;
4944 wl->fuse_nic_addr = 0;
4945 } else {
4946 wl12xx_get_fuse_mac(wl);
4947 }
4948
30c5dbd1
LC
4949 wl1271_power_off(wl);
4950out:
4951 return ret;
4952}
4953
4b32a2c9 4954static int wl1271_register_hw(struct wl1271 *wl)
f5fc0f86
LC
4955{
4956 int ret;
5e037e74 4957 u32 oui_addr = 0, nic_addr = 0;
f5fc0f86
LC
4958
4959 if (wl->mac80211_registered)
4960 return 0;
4961
30c5dbd1
LC
4962 ret = wl12xx_get_hw_info(wl);
4963 if (ret < 0) {
4964 wl1271_error("couldn't get hw info");
4965 goto out;
4966 }
4967
31d26ec6
AN
4968 ret = wl1271_fetch_nvs(wl);
4969 if (ret == 0) {
bc765bf3
SL
4970 /* NOTE: The wl->nvs->nvs element must be first, in
4971 * order to simplify the casting, we assume it is at
4972 * the beginning of the wl->nvs structure.
4973 */
4974 u8 *nvs_ptr = (u8 *)wl->nvs;
31d26ec6 4975
5e037e74
LC
4976 oui_addr =
4977 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
4978 nic_addr =
4979 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
4980 }
4981
4982 /* if the MAC address is zeroed in the NVS derive from fuse */
4983 if (oui_addr == 0 && nic_addr == 0) {
4984 oui_addr = wl->fuse_oui_addr;
4985 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
4986 nic_addr = wl->fuse_nic_addr + 1;
31d26ec6
AN
4987 }
4988
5e037e74 4989 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr, 2);
f5fc0f86
LC
4990
4991 ret = ieee80211_register_hw(wl->hw);
4992 if (ret < 0) {
4993 wl1271_error("unable to register mac80211 hw: %d", ret);
30c5dbd1 4994 goto out;
f5fc0f86
LC
4995 }
4996
4997 wl->mac80211_registered = true;
4998
d60080ae
EP
4999 wl1271_debugfs_init(wl);
5000
c2c192ac
JO
5001 register_netdevice_notifier(&wl1271_dev_notifier);
5002
f5fc0f86
LC
5003 wl1271_notice("loaded");
5004
30c5dbd1
LC
5005out:
5006 return ret;
f5fc0f86
LC
5007}
5008
4b32a2c9 5009static void wl1271_unregister_hw(struct wl1271 *wl)
3b56dd6a 5010{
4ae3fa87 5011 if (wl->state == WL1271_STATE_PLT)
f3df1331 5012 wl1271_plt_stop(wl);
4ae3fa87 5013
c2c192ac 5014 unregister_netdevice_notifier(&wl1271_dev_notifier);
3b56dd6a
TP
5015 ieee80211_unregister_hw(wl->hw);
5016 wl->mac80211_registered = false;
5017
5018}
3b56dd6a 5019
4b32a2c9 5020static int wl1271_init_ieee80211(struct wl1271 *wl)
f5fc0f86 5021{
7a55724e
JO
5022 static const u32 cipher_suites[] = {
5023 WLAN_CIPHER_SUITE_WEP40,
5024 WLAN_CIPHER_SUITE_WEP104,
5025 WLAN_CIPHER_SUITE_TKIP,
5026 WLAN_CIPHER_SUITE_CCMP,
5027 WL1271_CIPHER_SUITE_GEM,
5028 };
5029
1e2b7976 5030 /* The tx descriptor buffer and the TKIP space. */
5ec8a448 5031 wl->hw->extra_tx_headroom = WL1271_EXTRA_SPACE_TKIP +
1e2b7976 5032 sizeof(struct wl1271_tx_hw_descr);
f5fc0f86
LC
5033
5034 /* unit us */
5035 /* FIXME: find a proper value */
5036 wl->hw->channel_change_time = 10000;
50c500ad 5037 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
f5fc0f86
LC
5038
5039 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
0a34332f 5040 IEEE80211_HW_SUPPORTS_PS |
f1d63a59 5041 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4695dc91 5042 IEEE80211_HW_SUPPORTS_UAPSD |
a9af092b 5043 IEEE80211_HW_HAS_RATE_CONTROL |
00236aed 5044 IEEE80211_HW_CONNECTION_MONITOR |
25eaea30 5045 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
fcd23b63 5046 IEEE80211_HW_SPECTRUM_MGMT |
93f8c8e0
AN
5047 IEEE80211_HW_AP_LINK_PS |
5048 IEEE80211_HW_AMPDU_AGGREGATION |
79aba1ba
EP
5049 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5050 IEEE80211_HW_SCAN_WHILE_IDLE;
f5fc0f86 5051
7a55724e
JO
5052 wl->hw->wiphy->cipher_suites = cipher_suites;
5053 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5054
e0d8bbf0 5055 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
045c745f
EP
5056 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5057 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
f5fc0f86 5058 wl->hw->wiphy->max_scan_ssids = 1;
221737d2
LC
5059 wl->hw->wiphy->max_sched_scan_ssids = 16;
5060 wl->hw->wiphy->max_match_sets = 16;
ea559b46
GE
5061 /*
5062 * Maximum length of elements in scanning probe request templates
5063 * should be the maximum length possible for a template, without
5064 * the IEEE80211 header of the template
5065 */
154037d1 5066 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_DFLT_SIZE -
ea559b46 5067 sizeof(struct ieee80211_header);
a8aaaf53 5068
c9e79a47
LC
5069 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_DFLT_SIZE -
5070 sizeof(struct ieee80211_header);
5071
1ec23f7f
EP
5072 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5073
4a31c11c
LC
5074 /* make sure all our channels fit in the scanned_ch bitmask */
5075 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5076 ARRAY_SIZE(wl1271_channels_5ghz) >
5077 WL1271_MAX_CHANNELS);
a8aaaf53
LC
5078 /*
5079 * We keep local copies of the band structs because we need to
5080 * modify them on a per-device basis.
5081 */
5082 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5083 sizeof(wl1271_band_2ghz));
5084 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5085 sizeof(wl1271_band_5ghz));
5086
5087 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5088 &wl->bands[IEEE80211_BAND_2GHZ];
5089 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5090 &wl->bands[IEEE80211_BAND_5GHZ];
1ebec3d7 5091
12bd8949 5092 wl->hw->queues = 4;
31627dc5 5093 wl->hw->max_rates = 1;
12bd8949 5094
b7417d93
JO
5095 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5096
9c1b190b
AN
5097 /* the FW answers probe-requests in AP-mode */
5098 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5099 wl->hw->wiphy->probe_resp_offload =
5100 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5101 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5102 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5103
a390e85c 5104 SET_IEEE80211_DEV(wl->hw, wl->dev);
f5fc0f86 5105
f84f7d78 5106 wl->hw->sta_data_size = sizeof(struct wl1271_station);
87fbcb0f 5107 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
f84f7d78 5108
4c9cfa78
LC
5109 wl->hw->max_rx_aggregation_subframes = 8;
5110
f5fc0f86
LC
5111 return 0;
5112}
5113
f5fc0f86 5114#define WL1271_DEFAULT_CHANNEL 0
c332a4b8 5115
4b32a2c9 5116static struct ieee80211_hw *wl1271_alloc_hw(void)
f5fc0f86 5117{
f5fc0f86
LC
5118 struct ieee80211_hw *hw;
5119 struct wl1271 *wl;
a8c0ddb5 5120 int i, j, ret;
1f37cbc9 5121 unsigned int order;
f5fc0f86 5122
c7ffb902 5123 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
f80c2d12 5124
f5fc0f86
LC
5125 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5126 if (!hw) {
5127 wl1271_error("could not alloc ieee80211_hw");
a1dd8187 5128 ret = -ENOMEM;
3b56dd6a
TP
5129 goto err_hw_alloc;
5130 }
5131
f5fc0f86
LC
5132 wl = hw->priv;
5133 memset(wl, 0, sizeof(*wl));
5134
01c09162 5135 INIT_LIST_HEAD(&wl->list);
87627214 5136 INIT_LIST_HEAD(&wl->wlvif_list);
01c09162 5137
f5fc0f86 5138 wl->hw = hw;
f5fc0f86 5139
a8c0ddb5 5140 for (i = 0; i < NUM_TX_QUEUES; i++)
c7ffb902 5141 for (j = 0; j < WL12XX_MAX_LINKS; j++)
a8c0ddb5
AN
5142 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5143
a620865e
IY
5144 skb_queue_head_init(&wl->deferred_rx_queue);
5145 skb_queue_head_init(&wl->deferred_tx_queue);
5146
37b70a81 5147 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
a620865e 5148 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
117b38d0
JO
5149 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5150 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5151 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
77ddaa10 5152
92ef8960
EP
5153 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5154 if (!wl->freezable_wq) {
5155 ret = -ENOMEM;
5156 goto err_hw;
5157 }
5158
f5fc0f86 5159 wl->channel = WL1271_DEFAULT_CHANNEL;
f5fc0f86 5160 wl->rx_counter = 0;
f5fc0f86 5161 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
8a5a37a6 5162 wl->band = IEEE80211_BAND_2GHZ;
b771eee5 5163 wl->vif = NULL;
830fb67b 5164 wl->flags = 0;
7fc3a864 5165 wl->sg_enabled = true;
d717fd61 5166 wl->hw_pg_ver = -1;
b622d992
AN
5167 wl->ap_ps_map = 0;
5168 wl->ap_fw_ps_map = 0;
606ea9fa 5169 wl->quirks = 0;
341b7cde 5170 wl->platform_quirks = 0;
33c2c06c 5171 wl->sched_scanning = false;
e9eb8cbe 5172 wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
f4df1bd5 5173 wl->system_hlid = WL12XX_SYSTEM_HLID;
da03209e 5174 wl->active_sta_count = 0;
95dac04f
IY
5175 wl->fwlog_size = 0;
5176 init_waitqueue_head(&wl->fwlog_waitq);
f5fc0f86 5177
f4df1bd5
EP
5178 /* The system link is always allocated */
5179 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5180
25eeb9e3 5181 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
be7078c2 5182 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
f5fc0f86
LC
5183 wl->tx_frames[i] = NULL;
5184
5185 spin_lock_init(&wl->wl_lock);
5186
f5fc0f86
LC
5187 wl->state = WL1271_STATE_OFF;
5188 mutex_init(&wl->mutex);
5189
c332a4b8
TP
5190 /* Apply default driver configuration. */
5191 wl1271_conf_init(wl);
5192
1f37cbc9
IY
5193 order = get_order(WL1271_AGGR_BUFFER_SIZE);
5194 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5195 if (!wl->aggr_buf) {
5196 ret = -ENOMEM;
92ef8960 5197 goto err_wq;
1f37cbc9
IY
5198 }
5199
990f5de7
IY
5200 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5201 if (!wl->dummy_packet) {
5202 ret = -ENOMEM;
5203 goto err_aggr;
5204 }
5205
95dac04f
IY
5206 /* Allocate one page for the FW log */
5207 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5208 if (!wl->fwlog) {
5209 ret = -ENOMEM;
5210 goto err_dummy_packet;
5211 }
5212
c332a4b8 5213 return hw;
a1dd8187 5214
990f5de7
IY
5215err_dummy_packet:
5216 dev_kfree_skb(wl->dummy_packet);
5217
1f37cbc9
IY
5218err_aggr:
5219 free_pages((unsigned long)wl->aggr_buf, order);
5220
92ef8960
EP
5221err_wq:
5222 destroy_workqueue(wl->freezable_wq);
5223
a1dd8187 5224err_hw:
3b56dd6a 5225 wl1271_debugfs_exit(wl);
3b56dd6a
TP
5226 ieee80211_free_hw(hw);
5227
5228err_hw_alloc:
a1dd8187 5229
a1dd8187 5230 return ERR_PTR(ret);
c332a4b8
TP
5231}
5232
4b32a2c9 5233static int wl1271_free_hw(struct wl1271 *wl)
c332a4b8 5234{
95dac04f
IY
5235 /* Unblock any fwlog readers */
5236 mutex_lock(&wl->mutex);
5237 wl->fwlog_size = -1;
5238 wake_up_interruptible_all(&wl->fwlog_waitq);
5239 mutex_unlock(&wl->mutex);
5240
f79f890c 5241 device_remove_bin_file(wl->dev, &fwlog_attr);
6f07b72a 5242
f79f890c 5243 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
6f07b72a 5244
f79f890c 5245 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
95dac04f 5246 free_page((unsigned long)wl->fwlog);
990f5de7 5247 dev_kfree_skb(wl->dummy_packet);
1f37cbc9
IY
5248 free_pages((unsigned long)wl->aggr_buf,
5249 get_order(WL1271_AGGR_BUFFER_SIZE));
c332a4b8
TP
5250
5251 wl1271_debugfs_exit(wl);
5252
c332a4b8
TP
5253 vfree(wl->fw);
5254 wl->fw = NULL;
5255 kfree(wl->nvs);
5256 wl->nvs = NULL;
5257
5258 kfree(wl->fw_status);
5259 kfree(wl->tx_res_if);
92ef8960 5260 destroy_workqueue(wl->freezable_wq);
c332a4b8
TP
5261
5262 ieee80211_free_hw(wl->hw);
5263
5264 return 0;
5265}
50b3eb4b 5266
a390e85c
FB
5267static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5268{
5269 struct wl1271 *wl = cookie;
5270 unsigned long flags;
5271
5272 wl1271_debug(DEBUG_IRQ, "IRQ");
5273
5274 /* complete the ELP completion */
5275 spin_lock_irqsave(&wl->wl_lock, flags);
5276 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5277 if (wl->elp_compl) {
5278 complete(wl->elp_compl);
5279 wl->elp_compl = NULL;
5280 }
5281
5282 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5283 /* don't enqueue a work right now. mark it as pending */
5284 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5285 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5286 disable_irq_nosync(wl->irq);
5287 pm_wakeup_event(wl->dev, 0);
5288 spin_unlock_irqrestore(&wl->wl_lock, flags);
5289 return IRQ_HANDLED;
5290 }
5291 spin_unlock_irqrestore(&wl->wl_lock, flags);
5292
5293 return IRQ_WAKE_THREAD;
5294}
5295
ce2a217c
FB
5296static int __devinit wl12xx_probe(struct platform_device *pdev)
5297{
a390e85c
FB
5298 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
5299 struct ieee80211_hw *hw;
5300 struct wl1271 *wl;
5301 unsigned long irqflags;
5302 int ret = -ENODEV;
5303
5304 hw = wl1271_alloc_hw();
5305 if (IS_ERR(hw)) {
5306 wl1271_error("can't allocate hw");
5307 ret = PTR_ERR(hw);
5308 goto out;
5309 }
5310
5311 wl = hw->priv;
5312 wl->irq = platform_get_irq(pdev, 0);
5313 wl->ref_clock = pdata->board_ref_clock;
5314 wl->tcxo_clock = pdata->board_tcxo_clock;
5315 wl->platform_quirks = pdata->platform_quirks;
5316 wl->set_power = pdata->set_power;
5317 wl->dev = &pdev->dev;
5318 wl->if_ops = pdata->ops;
5319
5320 platform_set_drvdata(pdev, wl);
5321
5322 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5323 irqflags = IRQF_TRIGGER_RISING;
5324 else
5325 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5326
5327 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wl1271_irq,
5328 irqflags,
5329 pdev->name, wl);
5330 if (ret < 0) {
5331 wl1271_error("request_irq() failed: %d", ret);
5332 goto out_free_hw;
5333 }
5334
5335 ret = enable_irq_wake(wl->irq);
5336 if (!ret) {
5337 wl->irq_wake_enabled = true;
5338 device_init_wakeup(wl->dev, 1);
5339 if (pdata->pwr_in_suspend)
5340 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
5341
5342 }
5343 disable_irq(wl->irq);
5344
5345 ret = wl1271_init_ieee80211(wl);
5346 if (ret)
5347 goto out_irq;
5348
5349 ret = wl1271_register_hw(wl);
5350 if (ret)
5351 goto out_irq;
5352
f79f890c
FB
5353 /* Create sysfs file to control bt coex state */
5354 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5355 if (ret < 0) {
5356 wl1271_error("failed to create sysfs file bt_coex_state");
5357 goto out_irq;
5358 }
5359
5360 /* Create sysfs file to get HW PG version */
5361 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5362 if (ret < 0) {
5363 wl1271_error("failed to create sysfs file hw_pg_ver");
5364 goto out_bt_coex_state;
5365 }
5366
5367 /* Create sysfs file for the FW log */
5368 ret = device_create_bin_file(wl->dev, &fwlog_attr);
5369 if (ret < 0) {
5370 wl1271_error("failed to create sysfs file fwlog");
5371 goto out_hw_pg_ver;
5372 }
5373
ce2a217c 5374 return 0;
a390e85c 5375
f79f890c
FB
5376out_hw_pg_ver:
5377 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5378
5379out_bt_coex_state:
5380 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5381
a390e85c
FB
5382out_irq:
5383 free_irq(wl->irq, wl);
5384
5385out_free_hw:
5386 wl1271_free_hw(wl);
5387
5388out:
5389 return ret;
ce2a217c
FB
5390}
5391
5392static int __devexit wl12xx_remove(struct platform_device *pdev)
5393{
a390e85c
FB
5394 struct wl1271 *wl = platform_get_drvdata(pdev);
5395
5396 if (wl->irq_wake_enabled) {
5397 device_init_wakeup(wl->dev, 0);
5398 disable_irq_wake(wl->irq);
5399 }
5400 wl1271_unregister_hw(wl);
5401 free_irq(wl->irq, wl);
5402 wl1271_free_hw(wl);
5403
ce2a217c
FB
5404 return 0;
5405}
5406
5407static const struct platform_device_id wl12xx_id_table[] __devinitconst = {
ccb62000 5408 { "wl12xx", 0 },
ce2a217c
FB
5409 { } /* Terminating Entry */
5410};
5411MODULE_DEVICE_TABLE(platform, wl12xx_id_table);
5412
5413static struct platform_driver wl12xx_driver = {
5414 .probe = wl12xx_probe,
5415 .remove = __devexit_p(wl12xx_remove),
5416 .id_table = wl12xx_id_table,
5417 .driver = {
ccb62000 5418 .name = "wl12xx_driver",
ce2a217c
FB
5419 .owner = THIS_MODULE,
5420 }
5421};
5422
5423static int __init wl12xx_init(void)
5424{
5425 return platform_driver_register(&wl12xx_driver);
5426}
5427module_init(wl12xx_init);
5428
5429static void __exit wl12xx_exit(void)
5430{
5431 platform_driver_unregister(&wl12xx_driver);
5432}
5433module_exit(wl12xx_exit);
5434
491bbd6b 5435u32 wl12xx_debug_level = DEBUG_NONE;
17c1755c 5436EXPORT_SYMBOL_GPL(wl12xx_debug_level);
491bbd6b 5437module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
17c1755c
EP
5438MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
5439
95dac04f 5440module_param_named(fwlog, fwlog_param, charp, 0);
2c882fa4 5441MODULE_PARM_DESC(fwlog,
95dac04f
IY
5442 "FW logger options: continuous, ondemand, dbgpins or disable");
5443
2a5bff09
EP
5444module_param(bug_on_recovery, bool, S_IRUSR | S_IWUSR);
5445MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
5446
50b3eb4b 5447MODULE_LICENSE("GPL");
b1a48cab 5448MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
50b3eb4b 5449MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
This page took 0.99842 seconds and 5 git commands to generate.