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