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