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