3 * This file is part of wl1271
5 * Copyright (C) 2008-2010 Nokia Corporation
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
25 #include <linux/module.h>
26 #include <linux/firmware.h>
27 #include <linux/delay.h>
28 #include <linux/spi/spi.h>
29 #include <linux/crc32.h>
30 #include <linux/etherdevice.h>
31 #include <linux/vmalloc.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <linux/wl12xx.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
40 #include "wl12xx_80211.h"
54 #define WL1271_BOOT_RETRIES 3
56 static struct conf_drv_settings default_conf = {
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,
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,
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,
120 .state = CONF_SG_PROTECTIVE,
123 .rx_msdu_life_time = 512000,
124 .packet_detection_threshold = 0,
125 .ps_poll_timeout = 15,
127 .rts_threshold = IEEE80211_MAX_RTS_THRESHOLD,
128 .rx_cca_threshold = 0,
129 .irq_blk_threshold = 0xFFFF,
130 .irq_pkt_threshold = 0,
132 .queue_type = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
135 .tx_energy_detection = 0,
138 .short_retry_limit = 10,
139 .long_retry_limit = 10,
162 .aifsn = CONF_TX_AIFS_PIFS,
169 .aifsn = CONF_TX_AIFS_PIFS,
173 .max_tx_retries = 100,
174 .ap_aging_period = 300,
178 .queue_id = CONF_TX_AC_BE,
179 .channel_type = CONF_CHANNEL_TYPE_EDCF,
180 .tsid = CONF_TX_AC_BE,
181 .ps_scheme = CONF_PS_SCHEME_LEGACY,
182 .ack_policy = CONF_ACK_POLICY_LEGACY,
186 .queue_id = CONF_TX_AC_BK,
187 .channel_type = CONF_CHANNEL_TYPE_EDCF,
188 .tsid = CONF_TX_AC_BK,
189 .ps_scheme = CONF_PS_SCHEME_LEGACY,
190 .ack_policy = CONF_ACK_POLICY_LEGACY,
194 .queue_id = CONF_TX_AC_VI,
195 .channel_type = CONF_CHANNEL_TYPE_EDCF,
196 .tsid = CONF_TX_AC_VI,
197 .ps_scheme = CONF_PS_SCHEME_LEGACY,
198 .ack_policy = CONF_ACK_POLICY_LEGACY,
202 .queue_id = CONF_TX_AC_VO,
203 .channel_type = CONF_CHANNEL_TYPE_EDCF,
204 .tsid = CONF_TX_AC_VO,
205 .ps_scheme = CONF_PS_SCHEME_LEGACY,
206 .ack_policy = CONF_ACK_POLICY_LEGACY,
210 .frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD,
211 .tx_compl_timeout = 700,
212 .tx_compl_threshold = 4,
213 .basic_rate = CONF_HW_BIT_RATE_1MBPS,
214 .basic_rate_5 = CONF_HW_BIT_RATE_6MBPS,
215 .tmpl_short_retry_limit = 10,
216 .tmpl_long_retry_limit = 10,
219 .wake_up_event = CONF_WAKE_UP_EVENT_DTIM,
220 .listen_interval = 1,
221 .bcn_filt_mode = CONF_BCN_FILT_MODE_ENABLED,
222 .bcn_filt_ie_count = 2,
225 .ie = WLAN_EID_CHANNEL_SWITCH,
226 .rule = CONF_BCN_RULE_PASS_ON_APPEARANCE,
229 .ie = WLAN_EID_HT_INFORMATION,
230 .rule = CONF_BCN_RULE_PASS_ON_CHANGE,
233 .synch_fail_thold = 10,
234 .bss_lose_timeout = 100,
235 .beacon_rx_timeout = 10000,
236 .broadcast_timeout = 20000,
237 .rx_broadcast_in_ps = 1,
238 .ps_poll_threshold = 10,
239 .bet_enable = CONF_BET_MODE_ENABLE,
240 .bet_max_consecutive = 50,
241 .psm_entry_retries = 8,
242 .psm_exit_retries = 16,
243 .psm_entry_nullfunc_retries = 3,
244 .dynamic_ps_timeout = 100,
245 .keep_alive_interval = 55000,
246 .max_listen_interval = 20,
253 .host_clk_settling_time = 5000,
254 .host_fast_wakeup_support = false
258 .avg_weight_rssi_beacon = 20,
259 .avg_weight_rssi_data = 10,
260 .avg_weight_snr_beacon = 20,
261 .avg_weight_snr_data = 10,
264 .min_dwell_time_active = 7500,
265 .max_dwell_time_active = 30000,
266 .min_dwell_time_passive = 100000,
267 .max_dwell_time_passive = 100000,
271 /* sched_scan requires dwell times in TU instead of TU/1000 */
272 .min_dwell_time_active = 30,
273 .max_dwell_time_active = 60,
274 .dwell_time_passive = 100,
275 .dwell_time_dfs = 150,
277 .rssi_threshold = -90,
281 .tx_per_channel_power_compensation_2 = {
282 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284 .tx_per_channel_power_compensation_5 = {
285 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 .tx_ba_win_size = 64,
293 .inactivity_timeout = 10000,
294 .tx_ba_tid_bitmap = CONF_TX_BA_ENABLED_TID_BITMAP,
300 .tx_min_block_num = 40,
302 .min_req_tx_blocks = 100,
303 .min_req_rx_blocks = 22,
310 .tx_min_block_num = 40,
312 .min_req_tx_blocks = 45,
313 .min_req_rx_blocks = 22,
319 .n_divider_fref_set_1 = 0xff, /* default */
320 .n_divider_fref_set_2 = 12,
321 .m_divider_fref_set_1 = 148,
322 .m_divider_fref_set_2 = 0xffff, /* default */
323 .coex_pll_stabilization_time = 0xffffffff, /* default */
324 .ldo_stabilization_time = 0xffff, /* default */
325 .fm_disturbed_band_margin = 0xff, /* default */
326 .swallow_clk_diff = 0xff, /* default */
335 .mode = WL12XX_FWLOG_ON_DEMAND,
338 .timestamp = WL12XX_FWLOG_TIMESTAMP_DISABLED,
339 .output = WL12XX_FWLOG_OUTPUT_HOST,
342 .hci_io_ds = HCI_IO_DS_6MA,
344 .rate_retry_score = 32000,
349 .inverse_curiosity_factor = 5,
351 .tx_fail_high_th = 10,
352 .per_alpha_shift = 4,
354 .per_beta1_shift = 10,
355 .per_beta2_shift = 8,
357 .rate_check_down = 12,
358 .rate_retry_policy = {
359 0x00, 0x00, 0x00, 0x00, 0x00,
360 0x00, 0x00, 0x00, 0x00, 0x00,
366 .hangover_period = 20,
368 .early_termination_mode = 1,
379 static char *fwlog_param;
380 static bool bug_on_recovery;
382 static void __wl1271_op_remove_interface(struct wl1271 *wl,
383 struct ieee80211_vif *vif,
384 bool reset_tx_queues);
385 static void wl1271_op_stop(struct ieee80211_hw *hw);
386 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
388 static DEFINE_MUTEX(wl_list_mutex);
389 static LIST_HEAD(wl_list);
391 static int wl1271_check_operstate(struct wl1271 *wl, struct wl12xx_vif *wlvif,
392 unsigned char operstate)
396 if (operstate != IF_OPER_UP)
399 if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
402 ret = wl12xx_cmd_set_peer_state(wl, wlvif->sta.hlid);
406 wl12xx_croc(wl, wlvif->role_id);
408 wl1271_info("Association completed.");
411 static int wl1271_dev_notify(struct notifier_block *me, unsigned long what,
414 struct net_device *dev = arg;
415 struct wireless_dev *wdev;
417 struct ieee80211_hw *hw;
419 struct wl1271 *wl_temp;
420 struct wl12xx_vif *wlvif;
423 /* Check that this notification is for us. */
424 if (what != NETDEV_CHANGE)
427 wdev = dev->ieee80211_ptr;
435 hw = wiphy_priv(wiphy);
440 mutex_lock(&wl_list_mutex);
441 list_for_each_entry(wl, &wl_list, list) {
445 mutex_unlock(&wl_list_mutex);
449 mutex_lock(&wl->mutex);
451 if (wl->state == WL1271_STATE_OFF)
454 if (dev->operstate != IF_OPER_UP)
457 * The correct behavior should be just getting the appropriate wlvif
458 * from the given dev, but currently we don't have a mac80211
461 wl12xx_for_each_wlvif_sta(wl, wlvif) {
462 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
464 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
467 ret = wl1271_ps_elp_wakeup(wl);
471 wl1271_check_operstate(wl, wlvif,
472 ieee80211_get_operstate(vif));
474 wl1271_ps_elp_sleep(wl);
477 mutex_unlock(&wl->mutex);
482 static int wl1271_reg_notify(struct wiphy *wiphy,
483 struct regulatory_request *request)
485 struct ieee80211_supported_band *band;
486 struct ieee80211_channel *ch;
489 band = wiphy->bands[IEEE80211_BAND_5GHZ];
490 for (i = 0; i < band->n_channels; i++) {
491 ch = &band->channels[i];
492 if (ch->flags & IEEE80211_CHAN_DISABLED)
495 if (ch->flags & IEEE80211_CHAN_RADAR)
496 ch->flags |= IEEE80211_CHAN_NO_IBSS |
497 IEEE80211_CHAN_PASSIVE_SCAN;
504 static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
509 /* we should hold wl->mutex */
510 ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
515 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
517 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
523 * this function is being called when the rx_streaming interval
524 * has beed changed or rx_streaming should be disabled
526 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
529 int period = wl->conf.rx_streaming.interval;
531 /* don't reconfigure if rx_streaming is disabled */
532 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
535 /* reconfigure/disable according to new streaming_period */
537 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
538 (wl->conf.rx_streaming.always ||
539 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
540 ret = wl1271_set_rx_streaming(wl, wlvif, true);
542 ret = wl1271_set_rx_streaming(wl, wlvif, false);
543 /* don't cancel_work_sync since we might deadlock */
544 del_timer_sync(&wlvif->rx_streaming_timer);
550 static void wl1271_rx_streaming_enable_work(struct work_struct *work)
553 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
554 rx_streaming_enable_work);
555 struct wl1271 *wl = wlvif->wl;
557 mutex_lock(&wl->mutex);
559 if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
560 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
561 (!wl->conf.rx_streaming.always &&
562 !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
565 if (!wl->conf.rx_streaming.interval)
568 ret = wl1271_ps_elp_wakeup(wl);
572 ret = wl1271_set_rx_streaming(wl, wlvif, true);
576 /* stop it after some time of inactivity */
577 mod_timer(&wlvif->rx_streaming_timer,
578 jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
581 wl1271_ps_elp_sleep(wl);
583 mutex_unlock(&wl->mutex);
586 static void wl1271_rx_streaming_disable_work(struct work_struct *work)
589 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
590 rx_streaming_disable_work);
591 struct wl1271 *wl = wlvif->wl;
593 mutex_lock(&wl->mutex);
595 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
598 ret = wl1271_ps_elp_wakeup(wl);
602 ret = wl1271_set_rx_streaming(wl, wlvif, false);
607 wl1271_ps_elp_sleep(wl);
609 mutex_unlock(&wl->mutex);
612 static void wl1271_rx_streaming_timer(unsigned long data)
614 struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
615 struct wl1271 *wl = wlvif->wl;
616 ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
619 static void wl1271_conf_init(struct wl1271 *wl)
623 * This function applies the default configuration to the driver. This
624 * function is invoked upon driver load (spi probe.)
626 * The configuration is stored in a run-time structure in order to
627 * facilitate for run-time adjustment of any of the parameters. Making
628 * changes to the configuration structure will apply the new values on
629 * the next interface up (wl1271_op_start.)
632 /* apply driver default configuration */
633 memcpy(&wl->conf, &default_conf, sizeof(default_conf));
635 /* Adjust settings according to optional module parameters */
637 if (!strcmp(fwlog_param, "continuous")) {
638 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
639 } else if (!strcmp(fwlog_param, "ondemand")) {
640 wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
641 } else if (!strcmp(fwlog_param, "dbgpins")) {
642 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
643 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
644 } else if (!strcmp(fwlog_param, "disable")) {
645 wl->conf.fwlog.mem_blocks = 0;
646 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
648 wl1271_error("Unknown fwlog parameter %s", fwlog_param);
653 static int wl1271_plt_init(struct wl1271 *wl)
657 if (wl->chip.id == CHIP_ID_1283_PG20)
658 ret = wl128x_cmd_general_parms(wl);
660 ret = wl1271_cmd_general_parms(wl);
664 if (wl->chip.id == CHIP_ID_1283_PG20)
665 ret = wl128x_cmd_radio_parms(wl);
667 ret = wl1271_cmd_radio_parms(wl);
671 if (wl->chip.id != CHIP_ID_1283_PG20) {
672 ret = wl1271_cmd_ext_radio_parms(wl);
679 /* Chip-specific initializations */
680 ret = wl1271_chip_specific_init(wl);
684 ret = wl1271_acx_init_mem_config(wl);
688 ret = wl12xx_acx_mem_cfg(wl);
690 goto out_free_memmap;
692 /* Enable data path */
693 ret = wl1271_cmd_data_path(wl, 1);
695 goto out_free_memmap;
697 /* Configure for CAM power saving (ie. always active) */
698 ret = wl1271_acx_sleep_auth(wl, WL1271_PSM_CAM);
700 goto out_free_memmap;
703 ret = wl1271_acx_pm_config(wl);
705 goto out_free_memmap;
710 kfree(wl->target_mem_map);
711 wl->target_mem_map = NULL;
716 static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
717 struct wl12xx_vif *wlvif,
720 bool fw_ps, single_sta;
722 fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
723 single_sta = (wl->active_sta_count == 1);
726 * Wake up from high level PS if the STA is asleep with too little
727 * packets in FW or if the STA is awake.
729 if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
730 wl12xx_ps_link_end(wl, wlvif, hlid);
733 * Start high-level PS if the STA is asleep with enough blocks in FW.
734 * Make an exception if this is the only connected station. In this
735 * case FW-memory congestion is not a problem.
737 else if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
738 wl12xx_ps_link_start(wl, wlvif, hlid, true);
741 static void wl12xx_irq_update_links_status(struct wl1271 *wl,
742 struct wl12xx_vif *wlvif,
743 struct wl12xx_fw_status *status)
745 struct wl1271_link *lnk;
749 /* TODO: also use link_fast_bitmap here */
751 cur_fw_ps_map = le32_to_cpu(status->link_ps_bitmap);
752 if (wl->ap_fw_ps_map != cur_fw_ps_map) {
753 wl1271_debug(DEBUG_PSM,
754 "link ps prev 0x%x cur 0x%x changed 0x%x",
755 wl->ap_fw_ps_map, cur_fw_ps_map,
756 wl->ap_fw_ps_map ^ cur_fw_ps_map);
758 wl->ap_fw_ps_map = cur_fw_ps_map;
761 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, WL12XX_MAX_LINKS) {
762 lnk = &wl->links[hlid];
763 cnt = status->tx_lnk_free_pkts[hlid] - lnk->prev_freed_pkts;
765 lnk->prev_freed_pkts = status->tx_lnk_free_pkts[hlid];
766 lnk->allocated_pkts -= cnt;
768 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
769 lnk->allocated_pkts);
773 static void wl12xx_fw_status(struct wl1271 *wl,
774 struct wl12xx_fw_status *status)
776 struct wl12xx_vif *wlvif;
778 u32 old_tx_blk_count = wl->tx_blocks_available;
779 int avail, freed_blocks;
782 wl1271_raw_read(wl, FW_STATUS_ADDR, status, sizeof(*status), false);
784 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
785 "drv_rx_counter = %d, tx_results_counter = %d)",
787 status->fw_rx_counter,
788 status->drv_rx_counter,
789 status->tx_results_counter);
791 for (i = 0; i < NUM_TX_QUEUES; i++) {
792 /* prevent wrap-around in freed-packets counter */
793 wl->tx_allocated_pkts[i] -=
794 (status->tx_released_pkts[i] -
795 wl->tx_pkts_freed[i]) & 0xff;
797 wl->tx_pkts_freed[i] = status->tx_released_pkts[i];
800 /* prevent wrap-around in total blocks counter */
801 if (likely(wl->tx_blocks_freed <=
802 le32_to_cpu(status->total_released_blks)))
803 freed_blocks = le32_to_cpu(status->total_released_blks) -
806 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
807 le32_to_cpu(status->total_released_blks);
809 wl->tx_blocks_freed = le32_to_cpu(status->total_released_blks);
811 wl->tx_allocated_blocks -= freed_blocks;
813 avail = le32_to_cpu(status->tx_total) - wl->tx_allocated_blocks;
816 * The FW might change the total number of TX memblocks before
817 * we get a notification about blocks being released. Thus, the
818 * available blocks calculation might yield a temporary result
819 * which is lower than the actual available blocks. Keeping in
820 * mind that only blocks that were allocated can be moved from
821 * TX to RX, tx_blocks_available should never decrease here.
823 wl->tx_blocks_available = max((int)wl->tx_blocks_available,
826 /* if more blocks are available now, tx work can be scheduled */
827 if (wl->tx_blocks_available > old_tx_blk_count)
828 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
830 /* for AP update num of allocated TX blocks per link and ps status */
831 wl12xx_for_each_wlvif_ap(wl, wlvif) {
832 wl12xx_irq_update_links_status(wl, wlvif, status);
835 /* update the host-chipset time offset */
837 wl->time_offset = (timespec_to_ns(&ts) >> 10) -
838 (s64)le32_to_cpu(status->fw_localtime);
841 static void wl1271_flush_deferred_work(struct wl1271 *wl)
845 /* Pass all received frames to the network stack */
846 while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
847 ieee80211_rx_ni(wl->hw, skb);
849 /* Return sent skbs to the network stack */
850 while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
851 ieee80211_tx_status_ni(wl->hw, skb);
854 static void wl1271_netstack_work(struct work_struct *work)
857 container_of(work, struct wl1271, netstack_work);
860 wl1271_flush_deferred_work(wl);
861 } while (skb_queue_len(&wl->deferred_rx_queue));
864 #define WL1271_IRQ_MAX_LOOPS 256
866 static irqreturn_t wl1271_irq(int irq, void *cookie)
870 int loopcount = WL1271_IRQ_MAX_LOOPS;
871 struct wl1271 *wl = (struct wl1271 *)cookie;
873 unsigned int defer_count;
876 /* TX might be handled here, avoid redundant work */
877 set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
878 cancel_work_sync(&wl->tx_work);
881 * In case edge triggered interrupt must be used, we cannot iterate
882 * more than once without introducing race conditions with the hardirq.
884 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
887 mutex_lock(&wl->mutex);
889 wl1271_debug(DEBUG_IRQ, "IRQ work");
891 if (unlikely(wl->state == WL1271_STATE_OFF))
894 ret = wl1271_ps_elp_wakeup(wl);
898 while (!done && loopcount--) {
900 * In order to avoid a race with the hardirq, clear the flag
901 * before acknowledging the chip. Since the mutex is held,
902 * wl1271_ps_elp_wakeup cannot be called concurrently.
904 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
905 smp_mb__after_clear_bit();
907 wl12xx_fw_status(wl, wl->fw_status);
908 intr = le32_to_cpu(wl->fw_status->intr);
909 intr &= WL1271_INTR_MASK;
915 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
916 wl1271_error("watchdog interrupt received! "
917 "starting recovery.");
918 wl12xx_queue_recovery_work(wl);
920 /* restarting the chip. ignore any other interrupt. */
924 if (likely(intr & WL1271_ACX_INTR_DATA)) {
925 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
927 wl12xx_rx(wl, wl->fw_status);
929 /* Check if any tx blocks were freed */
930 spin_lock_irqsave(&wl->wl_lock, flags);
931 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
932 wl1271_tx_total_queue_count(wl) > 0) {
933 spin_unlock_irqrestore(&wl->wl_lock, flags);
935 * In order to avoid starvation of the TX path,
936 * call the work function directly.
938 wl1271_tx_work_locked(wl);
940 spin_unlock_irqrestore(&wl->wl_lock, flags);
943 /* check for tx results */
944 if (wl->fw_status->tx_results_counter !=
945 (wl->tx_results_count & 0xff))
946 wl1271_tx_complete(wl);
948 /* Make sure the deferred queues don't get too long */
949 defer_count = skb_queue_len(&wl->deferred_tx_queue) +
950 skb_queue_len(&wl->deferred_rx_queue);
951 if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
952 wl1271_flush_deferred_work(wl);
955 if (intr & WL1271_ACX_INTR_EVENT_A) {
956 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
957 wl1271_event_handle(wl, 0);
960 if (intr & WL1271_ACX_INTR_EVENT_B) {
961 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
962 wl1271_event_handle(wl, 1);
965 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
966 wl1271_debug(DEBUG_IRQ,
967 "WL1271_ACX_INTR_INIT_COMPLETE");
969 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
970 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
973 wl1271_ps_elp_sleep(wl);
976 spin_lock_irqsave(&wl->wl_lock, flags);
977 /* In case TX was not handled here, queue TX work */
978 clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
979 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
980 wl1271_tx_total_queue_count(wl) > 0)
981 ieee80211_queue_work(wl->hw, &wl->tx_work);
982 spin_unlock_irqrestore(&wl->wl_lock, flags);
984 mutex_unlock(&wl->mutex);
989 static int wl1271_fetch_firmware(struct wl1271 *wl)
991 const struct firmware *fw;
995 if (wl->chip.id == CHIP_ID_1283_PG20)
996 fw_name = WL128X_FW_NAME;
998 fw_name = WL127X_FW_NAME;
1000 wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
1002 ret = request_firmware(&fw, fw_name, wl->dev);
1005 wl1271_error("could not get firmware %s: %d", fw_name, ret);
1010 wl1271_error("firmware size is not multiple of 32 bits: %zu",
1017 wl->fw_len = fw->size;
1018 wl->fw = vmalloc(wl->fw_len);
1021 wl1271_error("could not allocate memory for the firmware");
1026 memcpy(wl->fw, fw->data, wl->fw_len);
1030 release_firmware(fw);
1035 static int wl1271_fetch_nvs(struct wl1271 *wl)
1037 const struct firmware *fw;
1040 ret = request_firmware(&fw, WL12XX_NVS_NAME, wl->dev);
1043 wl1271_error("could not get nvs file %s: %d", WL12XX_NVS_NAME,
1048 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
1051 wl1271_error("could not allocate memory for the nvs file");
1056 wl->nvs_len = fw->size;
1059 release_firmware(fw);
1064 void wl12xx_queue_recovery_work(struct wl1271 *wl)
1066 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1067 ieee80211_queue_work(wl->hw, &wl->recovery_work);
1070 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
1074 /* The FW log is a length-value list, find where the log end */
1075 while (len < maxlen) {
1076 if (memblock[len] == 0)
1078 if (len + memblock[len] + 1 > maxlen)
1080 len += memblock[len] + 1;
1083 /* Make sure we have enough room */
1084 len = min(len, (size_t)(PAGE_SIZE - wl->fwlog_size));
1086 /* Fill the FW log file, consumed by the sysfs fwlog entry */
1087 memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
1088 wl->fwlog_size += len;
1093 static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
1099 if ((wl->quirks & WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
1100 (wl->conf.fwlog.mode != WL12XX_FWLOG_ON_DEMAND) ||
1101 (wl->conf.fwlog.mem_blocks == 0))
1104 wl1271_info("Reading FW panic log");
1106 block = kmalloc(WL12XX_HW_BLOCK_SIZE, GFP_KERNEL);
1111 * Make sure the chip is awake and the logger isn't active.
1112 * This might fail if the firmware hanged.
1114 if (!wl1271_ps_elp_wakeup(wl))
1115 wl12xx_cmd_stop_fwlog(wl);
1117 /* Read the first memory block address */
1118 wl12xx_fw_status(wl, wl->fw_status);
1119 first_addr = le32_to_cpu(wl->fw_status->log_start_addr);
1123 /* Traverse the memory blocks linked list */
1126 memset(block, 0, WL12XX_HW_BLOCK_SIZE);
1127 wl1271_read_hwaddr(wl, addr, block, WL12XX_HW_BLOCK_SIZE,
1131 * Memory blocks are linked to one another. The first 4 bytes
1132 * of each memory block hold the hardware address of the next
1133 * one. The last memory block points to the first one.
1135 addr = le32_to_cpup((__le32 *)block);
1136 if (!wl12xx_copy_fwlog(wl, block + sizeof(addr),
1137 WL12XX_HW_BLOCK_SIZE - sizeof(addr)))
1139 } while (addr && (addr != first_addr));
1141 wake_up_interruptible(&wl->fwlog_waitq);
1147 static void wl1271_recovery_work(struct work_struct *work)
1150 container_of(work, struct wl1271, recovery_work);
1151 struct wl12xx_vif *wlvif;
1152 struct ieee80211_vif *vif;
1154 mutex_lock(&wl->mutex);
1156 if (wl->state != WL1271_STATE_ON)
1159 /* Avoid a recursive recovery */
1160 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1162 wl12xx_read_fwlog_panic(wl);
1164 wl1271_info("Hardware recovery in progress. FW ver: %s pc: 0x%x",
1165 wl->chip.fw_ver_str, wl1271_read32(wl, SCR_PAD4));
1167 BUG_ON(bug_on_recovery);
1170 * Advance security sequence number to overcome potential progress
1171 * in the firmware during recovery. This doens't hurt if the network is
1174 wl12xx_for_each_wlvif(wl, wlvif) {
1175 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
1176 test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1177 wlvif->tx_security_seq +=
1178 WL1271_TX_SQN_POST_RECOVERY_PADDING;
1181 /* Prevent spurious TX during FW restart */
1182 ieee80211_stop_queues(wl->hw);
1184 if (wl->sched_scanning) {
1185 ieee80211_sched_scan_stopped(wl->hw);
1186 wl->sched_scanning = false;
1189 /* reboot the chipset */
1190 while (!list_empty(&wl->wlvif_list)) {
1191 wlvif = list_first_entry(&wl->wlvif_list,
1192 struct wl12xx_vif, list);
1193 vif = wl12xx_wlvif_to_vif(wlvif);
1194 __wl1271_op_remove_interface(wl, vif, false);
1196 mutex_unlock(&wl->mutex);
1197 wl1271_op_stop(wl->hw);
1199 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1201 ieee80211_restart_hw(wl->hw);
1204 * Its safe to enable TX now - the queues are stopped after a request
1205 * to restart the HW.
1207 ieee80211_wake_queues(wl->hw);
1210 mutex_unlock(&wl->mutex);
1213 static void wl1271_fw_wakeup(struct wl1271 *wl)
1217 elp_reg = ELPCTRL_WAKE_UP;
1218 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
1221 static int wl1271_setup(struct wl1271 *wl)
1223 wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
1227 wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
1228 if (!wl->tx_res_if) {
1229 kfree(wl->fw_status);
1236 static int wl12xx_set_power_on(struct wl1271 *wl)
1240 msleep(WL1271_PRE_POWER_ON_SLEEP);
1241 ret = wl1271_power_on(wl);
1244 msleep(WL1271_POWER_ON_SLEEP);
1245 wl1271_io_reset(wl);
1248 wl1271_set_partition(wl, &wl12xx_part_table[PART_DOWN]);
1250 /* ELP module wake up */
1251 wl1271_fw_wakeup(wl);
1257 static int wl1271_chip_wakeup(struct wl1271 *wl)
1261 ret = wl12xx_set_power_on(wl);
1266 * For wl127x based devices we could use the default block
1267 * size (512 bytes), but due to a bug in the sdio driver, we
1268 * need to set it explicitly after the chip is powered on. To
1269 * simplify the code and since the performance impact is
1270 * negligible, we use the same block size for all different
1273 if (!wl1271_set_block_size(wl))
1274 wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
1276 switch (wl->chip.id) {
1277 case CHIP_ID_1271_PG10:
1278 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
1281 ret = wl1271_setup(wl);
1284 wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
1287 case CHIP_ID_1271_PG20:
1288 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
1291 ret = wl1271_setup(wl);
1294 wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
1297 case CHIP_ID_1283_PG20:
1298 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1283 PG20)",
1301 ret = wl1271_setup(wl);
1305 case CHIP_ID_1283_PG10:
1307 wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
1312 if (wl->fw == NULL) {
1313 ret = wl1271_fetch_firmware(wl);
1318 /* No NVS from netlink, try to get it from the filesystem */
1319 if (wl->nvs == NULL) {
1320 ret = wl1271_fetch_nvs(wl);
1329 int wl1271_plt_start(struct wl1271 *wl)
1331 int retries = WL1271_BOOT_RETRIES;
1332 struct wiphy *wiphy = wl->hw->wiphy;
1335 mutex_lock(&wl->mutex);
1337 wl1271_notice("power up");
1339 if (wl->state != WL1271_STATE_OFF) {
1340 wl1271_error("cannot go into PLT state because not "
1341 "in off state: %d", wl->state);
1348 ret = wl1271_chip_wakeup(wl);
1352 ret = wl1271_boot(wl);
1356 ret = wl1271_plt_init(wl);
1360 wl->state = WL1271_STATE_PLT;
1361 wl1271_notice("firmware booted in PLT mode (%s)",
1362 wl->chip.fw_ver_str);
1364 /* update hw/fw version info in wiphy struct */
1365 wiphy->hw_version = wl->chip.id;
1366 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1367 sizeof(wiphy->fw_version));
1372 mutex_unlock(&wl->mutex);
1373 /* Unlocking the mutex in the middle of handling is
1374 inherently unsafe. In this case we deem it safe to do,
1375 because we need to let any possibly pending IRQ out of
1376 the system (and while we are WL1271_STATE_OFF the IRQ
1377 work function will not do anything.) Also, any other
1378 possible concurrent operations will fail due to the
1379 current state, hence the wl1271 struct should be safe. */
1380 wl1271_disable_interrupts(wl);
1381 wl1271_flush_deferred_work(wl);
1382 cancel_work_sync(&wl->netstack_work);
1383 mutex_lock(&wl->mutex);
1385 wl1271_power_off(wl);
1388 wl1271_error("firmware boot in PLT mode failed despite %d retries",
1389 WL1271_BOOT_RETRIES);
1391 mutex_unlock(&wl->mutex);
1396 int wl1271_plt_stop(struct wl1271 *wl)
1400 wl1271_notice("power down");
1403 * Interrupts must be disabled before setting the state to OFF.
1404 * Otherwise, the interrupt handler might be called and exit without
1405 * reading the interrupt status.
1407 wl1271_disable_interrupts(wl);
1408 mutex_lock(&wl->mutex);
1409 if (wl->state != WL1271_STATE_PLT) {
1410 mutex_unlock(&wl->mutex);
1413 * This will not necessarily enable interrupts as interrupts
1414 * may have been disabled when op_stop was called. It will,
1415 * however, balance the above call to disable_interrupts().
1417 wl1271_enable_interrupts(wl);
1419 wl1271_error("cannot power down because not in PLT "
1420 "state: %d", wl->state);
1425 mutex_unlock(&wl->mutex);
1427 wl1271_flush_deferred_work(wl);
1428 cancel_work_sync(&wl->netstack_work);
1429 cancel_work_sync(&wl->recovery_work);
1430 cancel_delayed_work_sync(&wl->elp_work);
1432 mutex_lock(&wl->mutex);
1433 wl1271_power_off(wl);
1435 wl->state = WL1271_STATE_OFF;
1437 mutex_unlock(&wl->mutex);
1443 static void wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1445 struct wl1271 *wl = hw->priv;
1446 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1447 struct ieee80211_vif *vif = info->control.vif;
1448 struct wl12xx_vif *wlvif = NULL;
1449 unsigned long flags;
1454 wlvif = wl12xx_vif_to_data(vif);
1456 mapping = skb_get_queue_mapping(skb);
1457 q = wl1271_tx_get_queue(mapping);
1459 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
1461 spin_lock_irqsave(&wl->wl_lock, flags);
1463 /* queue the packet */
1464 if (hlid == WL12XX_INVALID_LINK_ID ||
1465 (wlvif && !test_bit(hlid, wlvif->links_map))) {
1466 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
1467 ieee80211_free_txskb(hw, skb);
1471 wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d", hlid, q);
1472 skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1474 wl->tx_queue_count[q]++;
1477 * The workqueue is slow to process the tx_queue and we need stop
1478 * the queue here, otherwise the queue will get too long.
1480 if (wl->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK) {
1481 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1482 ieee80211_stop_queue(wl->hw, mapping);
1483 set_bit(q, &wl->stopped_queues_map);
1487 * The chip specific setup must run before the first TX packet -
1488 * before that, the tx_work will not be initialized!
1491 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1492 !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
1493 ieee80211_queue_work(wl->hw, &wl->tx_work);
1496 spin_unlock_irqrestore(&wl->wl_lock, flags);
1499 int wl1271_tx_dummy_packet(struct wl1271 *wl)
1501 unsigned long flags;
1504 /* no need to queue a new dummy packet if one is already pending */
1505 if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1508 q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
1510 spin_lock_irqsave(&wl->wl_lock, flags);
1511 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
1512 wl->tx_queue_count[q]++;
1513 spin_unlock_irqrestore(&wl->wl_lock, flags);
1515 /* The FW is low on RX memory blocks, so send the dummy packet asap */
1516 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
1517 wl1271_tx_work_locked(wl);
1520 * If the FW TX is busy, TX work will be scheduled by the threaded
1521 * interrupt handler function
1527 * The size of the dummy packet should be at least 1400 bytes. However, in
1528 * order to minimize the number of bus transactions, aligning it to 512 bytes
1529 * boundaries could be beneficial, performance wise
1531 #define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1533 static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
1535 struct sk_buff *skb;
1536 struct ieee80211_hdr_3addr *hdr;
1537 unsigned int dummy_packet_size;
1539 dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1540 sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
1542 skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
1544 wl1271_warning("Failed to allocate a dummy packet skb");
1548 skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1550 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1551 memset(hdr, 0, sizeof(*hdr));
1552 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1553 IEEE80211_STYPE_NULLFUNC |
1554 IEEE80211_FCTL_TODS);
1556 memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
1558 /* Dummy packets require the TID to be management */
1559 skb->priority = WL1271_TID_MGMT;
1561 /* Initialize all fields that might be used */
1562 skb_set_queue_mapping(skb, 0);
1563 memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
1569 static struct notifier_block wl1271_dev_notifier = {
1570 .notifier_call = wl1271_dev_notify,
1575 static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1576 struct wl12xx_vif *wlvif)
1580 mutex_lock(&wl->mutex);
1582 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1585 ret = wl1271_ps_elp_wakeup(wl);
1589 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
1591 wl1271_ps_elp_sleep(wl);
1593 mutex_unlock(&wl->mutex);
1598 static int wl1271_configure_suspend(struct wl1271 *wl,
1599 struct wl12xx_vif *wlvif)
1601 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
1602 return wl1271_configure_suspend_ap(wl, wlvif);
1606 static void wl1271_configure_resume(struct wl1271 *wl,
1607 struct wl12xx_vif *wlvif)
1610 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
1615 mutex_lock(&wl->mutex);
1616 ret = wl1271_ps_elp_wakeup(wl);
1620 wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1622 wl1271_ps_elp_sleep(wl);
1624 mutex_unlock(&wl->mutex);
1627 static int wl1271_op_suspend(struct ieee80211_hw *hw,
1628 struct cfg80211_wowlan *wow)
1630 struct wl1271 *wl = hw->priv;
1631 struct wl12xx_vif *wlvif;
1634 wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
1635 WARN_ON(!wow || !wow->any);
1637 wl->wow_enabled = true;
1638 wl12xx_for_each_wlvif(wl, wlvif) {
1639 ret = wl1271_configure_suspend(wl, wlvif);
1641 wl1271_warning("couldn't prepare device to suspend");
1645 /* flush any remaining work */
1646 wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
1649 * disable and re-enable interrupts in order to flush
1652 wl1271_disable_interrupts(wl);
1655 * set suspended flag to avoid triggering a new threaded_irq
1656 * work. no need for spinlock as interrupts are disabled.
1658 set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1660 wl1271_enable_interrupts(wl);
1661 flush_work(&wl->tx_work);
1662 flush_delayed_work(&wl->elp_work);
1667 static int wl1271_op_resume(struct ieee80211_hw *hw)
1669 struct wl1271 *wl = hw->priv;
1670 struct wl12xx_vif *wlvif;
1671 unsigned long flags;
1672 bool run_irq_work = false;
1674 wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1676 WARN_ON(!wl->wow_enabled);
1679 * re-enable irq_work enqueuing, and call irq_work directly if
1680 * there is a pending work.
1682 spin_lock_irqsave(&wl->wl_lock, flags);
1683 clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1684 if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1685 run_irq_work = true;
1686 spin_unlock_irqrestore(&wl->wl_lock, flags);
1689 wl1271_debug(DEBUG_MAC80211,
1690 "run postponed irq_work directly");
1692 wl1271_enable_interrupts(wl);
1694 wl12xx_for_each_wlvif(wl, wlvif) {
1695 wl1271_configure_resume(wl, wlvif);
1697 wl->wow_enabled = false;
1703 static int wl1271_op_start(struct ieee80211_hw *hw)
1705 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1708 * We have to delay the booting of the hardware because
1709 * we need to know the local MAC address before downloading and
1710 * initializing the firmware. The MAC address cannot be changed
1711 * after boot, and without the proper MAC address, the firmware
1712 * will not function properly.
1714 * The MAC address is first known when the corresponding interface
1715 * is added. That is where we will initialize the hardware.
1718 wl1271_error("wl12xx is in an ustable state (fw api update is "
1719 "taking place). skip this commit when bisecting");
1723 static void wl1271_op_stop(struct ieee80211_hw *hw)
1725 struct wl1271 *wl = hw->priv;
1728 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
1731 * Interrupts must be disabled before setting the state to OFF.
1732 * Otherwise, the interrupt handler might be called and exit without
1733 * reading the interrupt status.
1735 wl1271_disable_interrupts(wl);
1736 mutex_lock(&wl->mutex);
1737 if (wl->state == WL1271_STATE_OFF) {
1738 mutex_unlock(&wl->mutex);
1741 * This will not necessarily enable interrupts as interrupts
1742 * may have been disabled when op_stop was called. It will,
1743 * however, balance the above call to disable_interrupts().
1745 wl1271_enable_interrupts(wl);
1750 * this must be before the cancel_work calls below, so that the work
1751 * functions don't perform further work.
1753 wl->state = WL1271_STATE_OFF;
1754 mutex_unlock(&wl->mutex);
1756 mutex_lock(&wl_list_mutex);
1757 list_del(&wl->list);
1758 mutex_unlock(&wl_list_mutex);
1760 wl1271_flush_deferred_work(wl);
1761 cancel_delayed_work_sync(&wl->scan_complete_work);
1762 cancel_work_sync(&wl->netstack_work);
1763 cancel_work_sync(&wl->tx_work);
1764 cancel_delayed_work_sync(&wl->elp_work);
1766 /* let's notify MAC80211 about the remaining pending TX frames */
1767 wl12xx_tx_reset(wl, true);
1768 mutex_lock(&wl->mutex);
1770 wl1271_power_off(wl);
1772 wl->band = IEEE80211_BAND_2GHZ;
1775 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1776 wl->tx_blocks_available = 0;
1777 wl->tx_allocated_blocks = 0;
1778 wl->tx_results_count = 0;
1779 wl->tx_packets_count = 0;
1780 wl->time_offset = 0;
1781 wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
1782 wl->ap_fw_ps_map = 0;
1784 wl->sched_scanning = false;
1785 memset(wl->roles_map, 0, sizeof(wl->roles_map));
1786 memset(wl->links_map, 0, sizeof(wl->links_map));
1787 memset(wl->roc_map, 0, sizeof(wl->roc_map));
1788 wl->active_sta_count = 0;
1790 /* The system link is always allocated */
1791 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1794 * this is performed after the cancel_work calls and the associated
1795 * mutex_lock, so that wl1271_op_add_interface does not accidentally
1796 * get executed before all these vars have been reset.
1800 wl->tx_blocks_freed = 0;
1802 for (i = 0; i < NUM_TX_QUEUES; i++) {
1803 wl->tx_pkts_freed[i] = 0;
1804 wl->tx_allocated_pkts[i] = 0;
1807 wl1271_debugfs_reset(wl);
1809 kfree(wl->fw_status);
1810 wl->fw_status = NULL;
1811 kfree(wl->tx_res_if);
1812 wl->tx_res_if = NULL;
1813 kfree(wl->target_mem_map);
1814 wl->target_mem_map = NULL;
1816 mutex_unlock(&wl->mutex);
1819 static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
1821 u8 policy = find_first_zero_bit(wl->rate_policies_map,
1822 WL12XX_MAX_RATE_POLICIES);
1823 if (policy >= WL12XX_MAX_RATE_POLICIES)
1826 __set_bit(policy, wl->rate_policies_map);
1831 static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
1833 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
1836 __clear_bit(*idx, wl->rate_policies_map);
1837 *idx = WL12XX_MAX_RATE_POLICIES;
1840 static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1842 switch (wlvif->bss_type) {
1843 case BSS_TYPE_AP_BSS:
1845 return WL1271_ROLE_P2P_GO;
1847 return WL1271_ROLE_AP;
1849 case BSS_TYPE_STA_BSS:
1851 return WL1271_ROLE_P2P_CL;
1853 return WL1271_ROLE_STA;
1856 return WL1271_ROLE_IBSS;
1859 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
1861 return WL12XX_INVALID_ROLE_TYPE;
1864 static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1866 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1869 /* clear everything but the persistent data */
1870 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
1872 switch (ieee80211_vif_type_p2p(vif)) {
1873 case NL80211_IFTYPE_P2P_CLIENT:
1876 case NL80211_IFTYPE_STATION:
1877 wlvif->bss_type = BSS_TYPE_STA_BSS;
1879 case NL80211_IFTYPE_ADHOC:
1880 wlvif->bss_type = BSS_TYPE_IBSS;
1882 case NL80211_IFTYPE_P2P_GO:
1885 case NL80211_IFTYPE_AP:
1886 wlvif->bss_type = BSS_TYPE_AP_BSS;
1889 wlvif->bss_type = MAX_BSS_TYPE;
1893 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
1894 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
1895 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
1897 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
1898 wlvif->bss_type == BSS_TYPE_IBSS) {
1899 /* init sta/ibss data */
1900 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
1901 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
1902 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
1903 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
1906 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
1907 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
1908 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
1909 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
1910 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
1911 wl12xx_allocate_rate_policy(wl,
1912 &wlvif->ap.ucast_rate_idx[i]);
1915 wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
1916 wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
1917 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
1918 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
1919 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
1920 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
1923 * mac80211 configures some values globally, while we treat them
1924 * per-interface. thus, on init, we have to copy them from wl
1926 wlvif->band = wl->band;
1927 wlvif->channel = wl->channel;
1928 wlvif->power_level = wl->power_level;
1930 INIT_WORK(&wlvif->rx_streaming_enable_work,
1931 wl1271_rx_streaming_enable_work);
1932 INIT_WORK(&wlvif->rx_streaming_disable_work,
1933 wl1271_rx_streaming_disable_work);
1934 INIT_LIST_HEAD(&wlvif->list);
1936 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
1937 (unsigned long) wlvif);
1941 static bool wl12xx_init_fw(struct wl1271 *wl)
1943 int retries = WL1271_BOOT_RETRIES;
1944 bool booted = false;
1945 struct wiphy *wiphy = wl->hw->wiphy;
1950 ret = wl1271_chip_wakeup(wl);
1954 ret = wl1271_boot(wl);
1958 ret = wl1271_hw_init(wl);
1966 mutex_unlock(&wl->mutex);
1967 /* Unlocking the mutex in the middle of handling is
1968 inherently unsafe. In this case we deem it safe to do,
1969 because we need to let any possibly pending IRQ out of
1970 the system (and while we are WL1271_STATE_OFF the IRQ
1971 work function will not do anything.) Also, any other
1972 possible concurrent operations will fail due to the
1973 current state, hence the wl1271 struct should be safe. */
1974 wl1271_disable_interrupts(wl);
1975 wl1271_flush_deferred_work(wl);
1976 cancel_work_sync(&wl->netstack_work);
1977 mutex_lock(&wl->mutex);
1979 wl1271_power_off(wl);
1983 wl1271_error("firmware boot failed despite %d retries",
1984 WL1271_BOOT_RETRIES);
1988 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
1990 /* update hw/fw version info in wiphy struct */
1991 wiphy->hw_version = wl->chip.id;
1992 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1993 sizeof(wiphy->fw_version));
1996 * Now we know if 11a is supported (info from the NVS), so disable
1997 * 11a channels if not supported
1999 if (!wl->enable_11a)
2000 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2002 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2003 wl->enable_11a ? "" : "not ");
2005 wl->state = WL1271_STATE_ON;
2010 static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2012 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2015 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2016 struct ieee80211_vif *vif)
2018 struct wl1271 *wl = hw->priv;
2019 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2022 bool booted = false;
2024 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2025 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
2027 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2028 ieee80211_vif_type_p2p(vif), vif->addr);
2030 mutex_lock(&wl->mutex);
2031 ret = wl1271_ps_elp_wakeup(wl);
2036 wl1271_debug(DEBUG_MAC80211,
2037 "multiple vifs are not supported yet");
2043 * in some very corner case HW recovery scenarios its possible to
2044 * get here before __wl1271_op_remove_interface is complete, so
2045 * opt out if that is the case.
2047 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2048 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
2053 ret = wl12xx_init_vif_data(wl, vif);
2058 role_type = wl12xx_get_role_type(wl, wlvif);
2059 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2065 * TODO: after the nvs issue will be solved, move this block
2066 * to start(), and make sure here the driver is ON.
2068 if (wl->state == WL1271_STATE_OFF) {
2070 * we still need this in order to configure the fw
2071 * while uploading the nvs
2073 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
2075 booted = wl12xx_init_fw(wl);
2082 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2083 wlvif->bss_type == BSS_TYPE_IBSS) {
2085 * The device role is a special role used for
2086 * rx and tx frames prior to association (as
2087 * the STA role can get packets only from
2088 * its associated bssid)
2090 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2092 &wlvif->dev_role_id);
2097 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2098 role_type, &wlvif->role_id);
2102 ret = wl1271_init_vif_specific(wl, vif);
2107 list_add(&wlvif->list, &wl->wlvif_list);
2108 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
2110 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2115 wl1271_ps_elp_sleep(wl);
2117 mutex_unlock(&wl->mutex);
2119 mutex_lock(&wl_list_mutex);
2121 list_add(&wl->list, &wl_list);
2122 mutex_unlock(&wl_list_mutex);
2127 static void __wl1271_op_remove_interface(struct wl1271 *wl,
2128 struct ieee80211_vif *vif,
2129 bool reset_tx_queues)
2131 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2134 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
2136 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2141 /* because of hardware recovery, we may get here twice */
2142 if (wl->state != WL1271_STATE_ON)
2145 wl1271_info("down");
2147 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2148 wl->scan_vif == vif) {
2149 wl->scan.state = WL1271_SCAN_STATE_IDLE;
2150 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
2151 wl->scan_vif = NULL;
2152 wl->scan.req = NULL;
2153 ieee80211_scan_completed(wl->hw, true);
2156 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2157 /* disable active roles */
2158 ret = wl1271_ps_elp_wakeup(wl);
2162 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2163 wlvif->bss_type == BSS_TYPE_IBSS) {
2164 if (wl12xx_dev_role_started(wlvif))
2165 wl12xx_stop_dev(wl, wlvif);
2167 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2172 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
2176 wl1271_ps_elp_sleep(wl);
2179 /* clear all hlids (except system_hlid) */
2180 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2182 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2183 wlvif->bss_type == BSS_TYPE_IBSS) {
2184 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2185 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2186 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2187 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2189 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2190 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2191 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2192 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2193 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2194 wl12xx_free_rate_policy(wl,
2195 &wlvif->ap.ucast_rate_idx[i]);
2198 wl12xx_tx_reset_wlvif(wl, wlvif);
2199 wl1271_free_ap_keys(wl, wlvif);
2200 if (wl->last_wlvif == wlvif)
2201 wl->last_wlvif = NULL;
2202 list_del(&wlvif->list);
2203 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
2204 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2205 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2207 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2212 mutex_unlock(&wl->mutex);
2214 del_timer_sync(&wlvif->rx_streaming_timer);
2215 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2216 cancel_work_sync(&wlvif->rx_streaming_disable_work);
2218 mutex_lock(&wl->mutex);
2221 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2222 struct ieee80211_vif *vif)
2224 struct wl1271 *wl = hw->priv;
2225 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2226 struct wl12xx_vif *iter;
2228 mutex_lock(&wl->mutex);
2230 if (wl->state == WL1271_STATE_OFF ||
2231 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2235 * wl->vif can be null here if someone shuts down the interface
2236 * just when hardware recovery has been started.
2238 wl12xx_for_each_wlvif(wl, iter) {
2242 __wl1271_op_remove_interface(wl, vif, true);
2245 WARN_ON(iter != wlvif);
2247 mutex_unlock(&wl->mutex);
2248 cancel_work_sync(&wl->recovery_work);
2251 static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2252 struct ieee80211_vif *vif,
2253 enum nl80211_iftype new_type, bool p2p)
2255 wl1271_op_remove_interface(hw, vif);
2257 vif->type = ieee80211_iftype_p2p(new_type, p2p);
2259 return wl1271_op_add_interface(hw, vif);
2262 static int wl1271_join(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2266 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
2269 * One of the side effects of the JOIN command is that is clears
2270 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2271 * to a WPA/WPA2 access point will therefore kill the data-path.
2272 * Currently the only valid scenario for JOIN during association
2273 * is on roaming, in which case we will also be given new keys.
2274 * Keep the below message for now, unless it starts bothering
2275 * users who really like to roam a lot :)
2277 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2278 wl1271_info("JOIN while associated.");
2281 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2284 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
2286 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
2290 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2294 * The join command disable the keep-alive mode, shut down its process,
2295 * and also clear the template config, so we need to reset it all after
2296 * the join. The acx_aid starts the keep-alive process, and the order
2297 * of the commands below is relevant.
2299 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
2303 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
2307 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
2311 ret = wl1271_acx_keep_alive_config(wl, wlvif,
2312 CMD_TEMPL_KLV_IDX_NULL_DATA,
2313 ACX_KEEP_ALIVE_TPL_VALID);
2321 static int wl1271_unjoin(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2325 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
2326 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2328 wl12xx_cmd_stop_channel_switch(wl);
2329 ieee80211_chswitch_done(vif, false);
2332 /* to stop listening to a channel, we disconnect */
2333 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
2337 /* reset TX security counters on a clean disconnect */
2338 wlvif->tx_security_last_seq_lsb = 0;
2339 wlvif->tx_security_seq = 0;
2345 static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2347 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
2348 wlvif->rate_set = wlvif->basic_rate_set;
2351 static int wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2355 bool cur_idle = !test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2357 if (idle == cur_idle)
2361 /* no need to croc if we weren't busy (e.g. during boot) */
2362 if (wl12xx_dev_role_started(wlvif)) {
2363 ret = wl12xx_stop_dev(wl, wlvif);
2368 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2369 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2372 ret = wl1271_acx_keep_alive_config(
2373 wl, wlvif, CMD_TEMPL_KLV_IDX_NULL_DATA,
2374 ACX_KEEP_ALIVE_TPL_INVALID);
2377 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2379 /* The current firmware only supports sched_scan in idle */
2380 if (wl->sched_scanning) {
2381 wl1271_scan_sched_scan_stop(wl);
2382 ieee80211_sched_scan_stopped(wl->hw);
2385 ret = wl12xx_start_dev(wl, wlvif);
2388 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2395 static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2396 struct ieee80211_conf *conf, u32 changed)
2398 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2401 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2403 /* if the channel changes while joined, join again */
2404 if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
2405 ((wlvif->band != conf->channel->band) ||
2406 (wlvif->channel != channel))) {
2407 /* send all pending packets */
2408 wl1271_tx_work_locked(wl);
2409 wlvif->band = conf->channel->band;
2410 wlvif->channel = channel;
2414 * FIXME: the mac80211 should really provide a fixed
2415 * rate to use here. for now, just use the smallest
2416 * possible rate for the band as a fixed rate for
2417 * association frames and other control messages.
2419 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2420 wl1271_set_band_rate(wl, wlvif);
2423 wl1271_tx_min_rate_get(wl,
2424 wlvif->basic_rate_set);
2425 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2427 wl1271_warning("rate policy for channel "
2430 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED,
2432 if (wl12xx_dev_role_started(wlvif)) {
2434 ret = wl12xx_croc(wl,
2435 wlvif->dev_role_id);
2439 ret = wl1271_join(wl, wlvif, false);
2441 wl1271_warning("cmd join on channel "
2445 * change the ROC channel. do it only if we are
2446 * not idle. otherwise, CROC will be called
2449 if (wl12xx_dev_role_started(wlvif) &&
2450 !(conf->flags & IEEE80211_CONF_IDLE)) {
2451 ret = wl12xx_stop_dev(wl, wlvif);
2455 ret = wl12xx_start_dev(wl, wlvif);
2463 if (conf->flags & IEEE80211_CONF_PS &&
2464 !test_bit(WLVIF_FLAG_PSM_REQUESTED, &wlvif->flags)) {
2465 set_bit(WLVIF_FLAG_PSM_REQUESTED, &wlvif->flags);
2468 * We enter PSM only if we're already associated.
2469 * If we're not, we'll enter it when joining an SSID,
2470 * through the bss_info_changed() hook.
2472 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
2473 wl1271_debug(DEBUG_PSM, "psm enabled");
2474 ret = wl1271_ps_set_mode(wl, wlvif,
2475 STATION_AUTO_PS_MODE);
2477 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
2478 test_bit(WLVIF_FLAG_PSM_REQUESTED, &wlvif->flags)) {
2479 wl1271_debug(DEBUG_PSM, "psm disabled");
2481 clear_bit(WLVIF_FLAG_PSM_REQUESTED, &wlvif->flags);
2483 if (test_bit(WLVIF_FLAG_PSM, &wlvif->flags))
2484 ret = wl1271_ps_set_mode(wl, wlvif,
2485 STATION_ACTIVE_MODE);
2488 if (conf->power_level != wlvif->power_level) {
2489 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
2493 wlvif->power_level = conf->power_level;
2499 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2501 struct wl1271 *wl = hw->priv;
2502 struct wl12xx_vif *wlvif;
2503 struct ieee80211_conf *conf = &hw->conf;
2504 int channel, ret = 0;
2506 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2508 wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d %s"
2511 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2513 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2517 * mac80211 will go to idle nearly immediately after transmitting some
2518 * frames, such as the deauth. To make sure those frames reach the air,
2519 * wait here until the TX queue is fully flushed.
2521 if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
2522 (conf->flags & IEEE80211_CONF_IDLE))
2523 wl1271_tx_flush(wl);
2525 mutex_lock(&wl->mutex);
2527 /* we support configuring the channel and band even while off */
2528 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2529 wl->band = conf->channel->band;
2530 wl->channel = channel;
2533 if (changed & IEEE80211_CONF_CHANGE_POWER)
2534 wl->power_level = conf->power_level;
2536 if (unlikely(wl->state == WL1271_STATE_OFF))
2539 ret = wl1271_ps_elp_wakeup(wl);
2543 /* configure each interface */
2544 wl12xx_for_each_wlvif(wl, wlvif) {
2545 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2551 wl1271_ps_elp_sleep(wl);
2554 mutex_unlock(&wl->mutex);
2559 struct wl1271_filter_params {
2562 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2565 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2566 struct netdev_hw_addr_list *mc_list)
2568 struct wl1271_filter_params *fp;
2569 struct netdev_hw_addr *ha;
2570 struct wl1271 *wl = hw->priv;
2572 if (unlikely(wl->state == WL1271_STATE_OFF))
2575 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
2577 wl1271_error("Out of memory setting filters.");
2581 /* update multicast filtering parameters */
2582 fp->mc_list_length = 0;
2583 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2584 fp->enabled = false;
2587 netdev_hw_addr_list_for_each(ha, mc_list) {
2588 memcpy(fp->mc_list[fp->mc_list_length],
2589 ha->addr, ETH_ALEN);
2590 fp->mc_list_length++;
2594 return (u64)(unsigned long)fp;
2597 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2600 FIF_BCN_PRBRESP_PROMISC | \
2604 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2605 unsigned int changed,
2606 unsigned int *total, u64 multicast)
2608 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
2609 struct wl1271 *wl = hw->priv;
2610 struct wl12xx_vif *wlvif;
2614 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2615 " total %x", changed, *total);
2617 mutex_lock(&wl->mutex);
2619 *total &= WL1271_SUPPORTED_FILTERS;
2620 changed &= WL1271_SUPPORTED_FILTERS;
2622 if (unlikely(wl->state == WL1271_STATE_OFF))
2625 ret = wl1271_ps_elp_wakeup(wl);
2629 wl12xx_for_each_wlvif(wl, wlvif) {
2630 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2631 if (*total & FIF_ALLMULTI)
2632 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2636 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2639 fp->mc_list_length);
2646 * the fw doesn't provide an api to configure the filters. instead,
2647 * the filters configuration is based on the active roles / ROC
2652 wl1271_ps_elp_sleep(wl);
2655 mutex_unlock(&wl->mutex);
2659 static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2660 u8 id, u8 key_type, u8 key_size,
2661 const u8 *key, u8 hlid, u32 tx_seq_32,
2664 struct wl1271_ap_key *ap_key;
2667 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2669 if (key_size > MAX_KEY_SIZE)
2673 * Find next free entry in ap_keys. Also check we are not replacing
2676 for (i = 0; i < MAX_NUM_KEYS; i++) {
2677 if (wlvif->ap.recorded_keys[i] == NULL)
2680 if (wlvif->ap.recorded_keys[i]->id == id) {
2681 wl1271_warning("trying to record key replacement");
2686 if (i == MAX_NUM_KEYS)
2689 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2694 ap_key->key_type = key_type;
2695 ap_key->key_size = key_size;
2696 memcpy(ap_key->key, key, key_size);
2697 ap_key->hlid = hlid;
2698 ap_key->tx_seq_32 = tx_seq_32;
2699 ap_key->tx_seq_16 = tx_seq_16;
2701 wlvif->ap.recorded_keys[i] = ap_key;
2705 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2709 for (i = 0; i < MAX_NUM_KEYS; i++) {
2710 kfree(wlvif->ap.recorded_keys[i]);
2711 wlvif->ap.recorded_keys[i] = NULL;
2715 static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2718 struct wl1271_ap_key *key;
2719 bool wep_key_added = false;
2721 for (i = 0; i < MAX_NUM_KEYS; i++) {
2723 if (wlvif->ap.recorded_keys[i] == NULL)
2726 key = wlvif->ap.recorded_keys[i];
2728 if (hlid == WL12XX_INVALID_LINK_ID)
2729 hlid = wlvif->ap.bcast_hlid;
2731 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
2732 key->id, key->key_type,
2733 key->key_size, key->key,
2734 hlid, key->tx_seq_32,
2739 if (key->key_type == KEY_WEP)
2740 wep_key_added = true;
2743 if (wep_key_added) {
2744 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
2745 wlvif->ap.bcast_hlid);
2751 wl1271_free_ap_keys(wl, wlvif);
2755 static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2756 u16 action, u8 id, u8 key_type,
2757 u8 key_size, const u8 *key, u32 tx_seq_32,
2758 u16 tx_seq_16, struct ieee80211_sta *sta)
2761 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2764 struct wl1271_station *wl_sta;
2768 wl_sta = (struct wl1271_station *)sta->drv_priv;
2769 hlid = wl_sta->hlid;
2771 hlid = wlvif->ap.bcast_hlid;
2774 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
2776 * We do not support removing keys after AP shutdown.
2777 * Pretend we do to make mac80211 happy.
2779 if (action != KEY_ADD_OR_REPLACE)
2782 ret = wl1271_record_ap_key(wl, wlvif, id,
2784 key, hlid, tx_seq_32,
2787 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
2788 id, key_type, key_size,
2789 key, hlid, tx_seq_32,
2797 static const u8 bcast_addr[ETH_ALEN] = {
2798 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2802 * A STA set to GEM cipher requires 2 tx spare blocks.
2803 * Return to default value when GEM cipher key is removed
2805 if (key_type == KEY_GEM) {
2806 if (action == KEY_ADD_OR_REPLACE)
2807 wl->tx_spare_blocks = 2;
2808 else if (action == KEY_REMOVE)
2809 wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
2812 addr = sta ? sta->addr : bcast_addr;
2814 if (is_zero_ether_addr(addr)) {
2815 /* We dont support TX only encryption */
2819 /* The wl1271 does not allow to remove unicast keys - they
2820 will be cleared automatically on next CMD_JOIN. Ignore the
2821 request silently, as we dont want the mac80211 to emit
2822 an error message. */
2823 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
2826 /* don't remove key if hlid was already deleted */
2827 if (action == KEY_REMOVE &&
2828 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
2831 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
2832 id, key_type, key_size,
2833 key, addr, tx_seq_32,
2838 /* the default WEP key needs to be configured at least once */
2839 if (key_type == KEY_WEP) {
2840 ret = wl12xx_cmd_set_default_wep_key(wl,
2851 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2852 struct ieee80211_vif *vif,
2853 struct ieee80211_sta *sta,
2854 struct ieee80211_key_conf *key_conf)
2856 struct wl1271 *wl = hw->priv;
2857 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2863 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
2865 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
2866 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
2867 key_conf->cipher, key_conf->keyidx,
2868 key_conf->keylen, key_conf->flags);
2869 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
2871 mutex_lock(&wl->mutex);
2873 if (unlikely(wl->state == WL1271_STATE_OFF)) {
2878 ret = wl1271_ps_elp_wakeup(wl);
2882 switch (key_conf->cipher) {
2883 case WLAN_CIPHER_SUITE_WEP40:
2884 case WLAN_CIPHER_SUITE_WEP104:
2887 key_conf->hw_key_idx = key_conf->keyidx;
2889 case WLAN_CIPHER_SUITE_TKIP:
2890 key_type = KEY_TKIP;
2892 key_conf->hw_key_idx = key_conf->keyidx;
2893 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2894 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
2896 case WLAN_CIPHER_SUITE_CCMP:
2899 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2900 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2901 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
2903 case WL1271_CIPHER_SUITE_GEM:
2905 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2906 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
2909 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
2917 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
2918 key_conf->keyidx, key_type,
2919 key_conf->keylen, key_conf->key,
2920 tx_seq_32, tx_seq_16, sta);
2922 wl1271_error("Could not add or replace key");
2928 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
2929 key_conf->keyidx, key_type,
2930 key_conf->keylen, key_conf->key,
2933 wl1271_error("Could not remove key");
2939 wl1271_error("Unsupported key cmd 0x%x", cmd);
2945 wl1271_ps_elp_sleep(wl);
2948 mutex_unlock(&wl->mutex);
2953 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
2954 struct ieee80211_vif *vif,
2955 struct cfg80211_scan_request *req)
2957 struct wl1271 *wl = hw->priv;
2958 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2964 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
2967 ssid = req->ssids[0].ssid;
2968 len = req->ssids[0].ssid_len;
2971 mutex_lock(&wl->mutex);
2973 if (wl->state == WL1271_STATE_OFF) {
2975 * We cannot return -EBUSY here because cfg80211 will expect
2976 * a call to ieee80211_scan_completed if we do - in this case
2977 * there won't be any call.
2983 ret = wl1271_ps_elp_wakeup(wl);
2987 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
2988 test_bit(wlvif->role_id, wl->roc_map)) {
2989 /* don't allow scanning right now */
2994 /* cancel ROC before scanning */
2995 if (wl12xx_dev_role_started(wlvif))
2996 wl12xx_croc(wl, wlvif->dev_role_id);
2998 ret = wl1271_scan(hw->priv, vif, ssid, len, req);
3000 wl1271_ps_elp_sleep(wl);
3002 mutex_unlock(&wl->mutex);
3007 static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3008 struct ieee80211_vif *vif)
3010 struct wl1271 *wl = hw->priv;
3013 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3015 mutex_lock(&wl->mutex);
3017 if (wl->state == WL1271_STATE_OFF)
3020 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3023 ret = wl1271_ps_elp_wakeup(wl);
3027 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3028 ret = wl1271_scan_stop(wl);
3032 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3033 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3034 wl->scan_vif = NULL;
3035 wl->scan.req = NULL;
3036 ieee80211_scan_completed(wl->hw, true);
3039 wl1271_ps_elp_sleep(wl);
3041 mutex_unlock(&wl->mutex);
3043 cancel_delayed_work_sync(&wl->scan_complete_work);
3046 static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3047 struct ieee80211_vif *vif,
3048 struct cfg80211_sched_scan_request *req,
3049 struct ieee80211_sched_scan_ies *ies)
3051 struct wl1271 *wl = hw->priv;
3052 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3055 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3057 mutex_lock(&wl->mutex);
3059 if (wl->state == WL1271_STATE_OFF) {
3064 ret = wl1271_ps_elp_wakeup(wl);
3068 ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
3072 ret = wl1271_scan_sched_scan_start(wl, wlvif);
3076 wl->sched_scanning = true;
3079 wl1271_ps_elp_sleep(wl);
3081 mutex_unlock(&wl->mutex);
3085 static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3086 struct ieee80211_vif *vif)
3088 struct wl1271 *wl = hw->priv;
3091 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3093 mutex_lock(&wl->mutex);
3095 if (wl->state == WL1271_STATE_OFF)
3098 ret = wl1271_ps_elp_wakeup(wl);
3102 wl1271_scan_sched_scan_stop(wl);
3104 wl1271_ps_elp_sleep(wl);
3106 mutex_unlock(&wl->mutex);
3109 static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3111 struct wl1271 *wl = hw->priv;
3114 mutex_lock(&wl->mutex);
3116 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3121 ret = wl1271_ps_elp_wakeup(wl);
3125 ret = wl1271_acx_frag_threshold(wl, value);
3127 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3129 wl1271_ps_elp_sleep(wl);
3132 mutex_unlock(&wl->mutex);
3137 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3139 struct wl1271 *wl = hw->priv;
3140 struct wl12xx_vif *wlvif;
3143 mutex_lock(&wl->mutex);
3145 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3150 ret = wl1271_ps_elp_wakeup(wl);
3154 wl12xx_for_each_wlvif(wl, wlvif) {
3155 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3157 wl1271_warning("set rts threshold failed: %d", ret);
3159 wl1271_ps_elp_sleep(wl);
3162 mutex_unlock(&wl->mutex);
3167 static int wl1271_ssid_set(struct ieee80211_vif *vif, struct sk_buff *skb,
3170 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3172 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
3176 wl1271_error("No SSID in IEs!");
3181 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
3182 wl1271_error("SSID is too long!");
3186 wlvif->ssid_len = ssid_len;
3187 memcpy(wlvif->ssid, ptr+2, ssid_len);
3191 static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3194 const u8 *next, *end = skb->data + skb->len;
3195 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3196 skb->len - ieoffset);
3201 memmove(ie, next, end - next);
3202 skb_trim(skb, skb->len - len);
3205 static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3206 unsigned int oui, u8 oui_type,
3210 const u8 *next, *end = skb->data + skb->len;
3211 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3212 skb->data + ieoffset,
3213 skb->len - ieoffset);
3218 memmove(ie, next, end - next);
3219 skb_trim(skb, skb->len - len);
3222 static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3223 struct ieee80211_vif *vif)
3225 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3226 struct sk_buff *skb;
3229 skb = ieee80211_proberesp_get(wl->hw, vif);
3233 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3234 CMD_TEMPL_AP_PROBE_RESPONSE,
3243 static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3244 struct ieee80211_vif *vif,
3246 size_t probe_rsp_len,
3249 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3250 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3251 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3252 int ssid_ie_offset, ie_offset, templ_len;
3255 /* no need to change probe response if the SSID is set correctly */
3256 if (wlvif->ssid_len > 0)
3257 return wl1271_cmd_template_set(wl, wlvif->role_id,
3258 CMD_TEMPL_AP_PROBE_RESPONSE,
3263 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3264 wl1271_error("probe_rsp template too big");
3268 /* start searching from IE offset */
3269 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3271 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3272 probe_rsp_len - ie_offset);
3274 wl1271_error("No SSID in beacon!");
3278 ssid_ie_offset = ptr - probe_rsp_data;
3279 ptr += (ptr[1] + 2);
3281 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3283 /* insert SSID from bss_conf */
3284 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3285 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3286 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3287 bss_conf->ssid, bss_conf->ssid_len);
3288 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3290 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3291 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3292 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3294 return wl1271_cmd_template_set(wl, wlvif->role_id,
3295 CMD_TEMPL_AP_PROBE_RESPONSE,
3301 static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
3302 struct ieee80211_vif *vif,
3303 struct ieee80211_bss_conf *bss_conf,
3306 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3309 if (changed & BSS_CHANGED_ERP_SLOT) {
3310 if (bss_conf->use_short_slot)
3311 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
3313 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
3315 wl1271_warning("Set slot time failed %d", ret);
3320 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3321 if (bss_conf->use_short_preamble)
3322 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
3324 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
3327 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3328 if (bss_conf->use_cts_prot)
3329 ret = wl1271_acx_cts_protect(wl, wlvif,
3332 ret = wl1271_acx_cts_protect(wl, wlvif,
3333 CTSPROTECT_DISABLE);
3335 wl1271_warning("Set ctsprotect failed %d", ret);
3344 static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3345 struct ieee80211_vif *vif,
3346 struct ieee80211_bss_conf *bss_conf,
3349 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3350 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3353 if ((changed & BSS_CHANGED_BEACON_INT)) {
3354 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
3355 bss_conf->beacon_int);
3357 wlvif->beacon_int = bss_conf->beacon_int;
3360 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3361 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3362 if (!wl1271_ap_set_probe_resp_tmpl(wl, rate, vif)) {
3363 wl1271_debug(DEBUG_AP, "probe response updated");
3364 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3368 if ((changed & BSS_CHANGED_BEACON)) {
3369 struct ieee80211_hdr *hdr;
3371 int ieoffset = offsetof(struct ieee80211_mgmt,
3373 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3381 wl1271_debug(DEBUG_MASTER, "beacon updated");
3383 ret = wl1271_ssid_set(vif, beacon, ieoffset);
3385 dev_kfree_skb(beacon);
3388 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3389 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3391 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3396 dev_kfree_skb(beacon);
3401 * In case we already have a probe-resp beacon set explicitly
3402 * by usermode, don't use the beacon data.
3404 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3407 /* remove TIM ie from probe response */
3408 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3411 * remove p2p ie from probe response.
3412 * the fw reponds to probe requests that don't include
3413 * the p2p ie. probe requests with p2p ie will be passed,
3414 * and will be responded by the supplicant (the spec
3415 * forbids including the p2p ie when responding to probe
3416 * requests that didn't include it).
3418 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3419 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3421 hdr = (struct ieee80211_hdr *) beacon->data;
3422 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3423 IEEE80211_STYPE_PROBE_RESP);
3425 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3430 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3431 CMD_TEMPL_PROBE_RESPONSE,
3436 dev_kfree_skb(beacon);
3443 wl1271_error("beacon info change failed: %d", ret);
3447 /* AP mode changes */
3448 static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3449 struct ieee80211_vif *vif,
3450 struct ieee80211_bss_conf *bss_conf,
3453 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3456 if ((changed & BSS_CHANGED_BASIC_RATES)) {
3457 u32 rates = bss_conf->basic_rates;
3459 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
3461 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
3462 wlvif->basic_rate_set);
3464 ret = wl1271_init_ap_rates(wl, wlvif);
3466 wl1271_error("AP rate policy change failed %d", ret);
3470 ret = wl1271_ap_init_templates(wl, vif);
3475 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3479 if ((changed & BSS_CHANGED_BEACON_ENABLED)) {
3480 if (bss_conf->enable_beacon) {
3481 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3482 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
3486 ret = wl1271_ap_init_hwenc(wl, wlvif);
3490 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3491 wl1271_debug(DEBUG_AP, "started AP");
3494 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3495 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
3499 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3500 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3502 wl1271_debug(DEBUG_AP, "stopped AP");
3507 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3511 /* Handle HT information change */
3512 if ((changed & BSS_CHANGED_HT) &&
3513 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3514 ret = wl1271_acx_set_ht_information(wl, wlvif,
3515 bss_conf->ht_operation_mode);
3517 wl1271_warning("Set ht information failed %d", ret);
3526 /* STA/IBSS mode changes */
3527 static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3528 struct ieee80211_vif *vif,
3529 struct ieee80211_bss_conf *bss_conf,
3532 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3533 bool do_join = false, set_assoc = false;
3534 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
3535 bool ibss_joined = false;
3536 u32 sta_rate_set = 0;
3538 struct ieee80211_sta *sta;
3539 bool sta_exists = false;
3540 struct ieee80211_sta_ht_cap sta_ht_cap;
3543 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3549 if (changed & BSS_CHANGED_IBSS) {
3550 if (bss_conf->ibss_joined) {
3551 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
3554 if (test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED,
3556 wl1271_unjoin(wl, wlvif);
3557 wl12xx_start_dev(wl, wlvif);
3562 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
3565 /* Need to update the SSID (for filtering etc) */
3566 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
3569 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
3570 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3571 bss_conf->enable_beacon ? "enabled" : "disabled");
3576 if (changed & BSS_CHANGED_IDLE) {
3577 ret = wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
3579 wl1271_warning("idle mode change failed %d", ret);
3582 if ((changed & BSS_CHANGED_CQM)) {
3583 bool enable = false;
3584 if (bss_conf->cqm_rssi_thold)
3586 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
3587 bss_conf->cqm_rssi_thold,
3588 bss_conf->cqm_rssi_hyst);
3591 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
3594 if (changed & BSS_CHANGED_BSSID &&
3595 (is_ibss || bss_conf->assoc))
3596 if (!is_zero_ether_addr(bss_conf->bssid)) {
3597 ret = wl12xx_cmd_build_null_data(wl, wlvif);
3601 ret = wl1271_build_qos_null_data(wl, vif);
3605 /* Need to update the BSSID (for filtering etc) */
3609 if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_HT)) {
3611 sta = ieee80211_find_sta(vif, bss_conf->bssid);
3615 /* save the supp_rates of the ap */
3616 sta_rate_set = sta->supp_rates[wl->hw->conf.channel->band];
3617 if (sta->ht_cap.ht_supported)
3619 (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET);
3620 sta_ht_cap = sta->ht_cap;
3627 if ((changed & BSS_CHANGED_ASSOC)) {
3628 if (bss_conf->assoc) {
3631 wlvif->aid = bss_conf->aid;
3635 * use basic rates from AP, and determine lowest rate
3636 * to use with control frames.
3638 rates = bss_conf->basic_rates;
3639 wlvif->basic_rate_set =
3640 wl1271_tx_enabled_rates_get(wl, rates,
3643 wl1271_tx_min_rate_get(wl,
3644 wlvif->basic_rate_set);
3647 wl1271_tx_enabled_rates_get(wl,
3650 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3655 * with wl1271, we don't need to update the
3656 * beacon_int and dtim_period, because the firmware
3657 * updates it by itself when the first beacon is
3658 * received after a join.
3660 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
3665 * Get a template for hardware connection maintenance
3667 dev_kfree_skb(wlvif->probereq);
3668 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
3671 ieoffset = offsetof(struct ieee80211_mgmt,
3672 u.probe_req.variable);
3673 wl1271_ssid_set(vif, wlvif->probereq, ieoffset);
3675 /* enable the connection monitoring feature */
3676 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
3680 /* use defaults when not associated */
3682 !!test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED,
3685 !!test_and_clear_bit(WLVIF_FLAG_STA_STATE_SENT,
3689 /* free probe-request template */
3690 dev_kfree_skb(wlvif->probereq);
3691 wlvif->probereq = NULL;
3693 /* revert back to minimum rates for the current band */
3694 wl1271_set_band_rate(wl, wlvif);
3696 wl1271_tx_min_rate_get(wl,
3697 wlvif->basic_rate_set);
3698 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3702 /* disable connection monitor features */
3703 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
3705 /* Disable the keep-alive feature */
3706 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
3710 /* restore the bssid filter and go to dummy bssid */
3712 u32 conf_flags = wl->hw->conf.flags;
3714 * we might have to disable roc, if there was
3715 * no IF_OPER_UP notification.
3718 ret = wl12xx_croc(wl, wlvif->role_id);
3723 * (we also need to disable roc in case of
3724 * roaming on the same channel. until we will
3725 * have a better flow...)
3727 if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
3728 ret = wl12xx_croc(wl,
3729 wlvif->dev_role_id);
3734 wl1271_unjoin(wl, wlvif);
3735 if (!(conf_flags & IEEE80211_CONF_IDLE))
3736 wl12xx_start_dev(wl, wlvif);
3741 if (changed & BSS_CHANGED_IBSS) {
3742 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
3743 bss_conf->ibss_joined);
3745 if (bss_conf->ibss_joined) {
3746 u32 rates = bss_conf->basic_rates;
3747 wlvif->basic_rate_set =
3748 wl1271_tx_enabled_rates_get(wl, rates,
3751 wl1271_tx_min_rate_get(wl,
3752 wlvif->basic_rate_set);
3754 /* by default, use 11b + OFDM rates */
3755 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
3756 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3762 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3766 if (changed & BSS_CHANGED_ARP_FILTER) {
3767 __be32 addr = bss_conf->arp_addr_list[0];
3768 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
3770 if (bss_conf->arp_addr_cnt == 1 &&
3771 bss_conf->arp_filter_enabled) {
3773 * The template should have been configured only upon
3774 * association. however, it seems that the correct ip
3775 * isn't being set (when sending), so we have to
3776 * reconfigure the template upon every ip change.
3778 ret = wl1271_cmd_build_arp_rsp(wl, wlvif, addr);
3780 wl1271_warning("build arp rsp failed: %d", ret);
3784 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
3785 ACX_ARP_FILTER_ARP_FILTERING,
3788 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
3795 ret = wl1271_join(wl, wlvif, set_assoc);
3797 wl1271_warning("cmd join failed %d", ret);
3801 /* ROC until connected (after EAPOL exchange) */
3803 ret = wl12xx_roc(wl, wlvif, wlvif->role_id);
3807 wl1271_check_operstate(wl, wlvif,
3808 ieee80211_get_operstate(vif));
3811 * stop device role if started (we might already be in
3814 if (wl12xx_dev_role_started(wlvif)) {
3815 ret = wl12xx_stop_dev(wl, wlvif);
3820 /* If we want to go in PSM but we're not there yet */
3821 if (test_bit(WLVIF_FLAG_PSM_REQUESTED, &wlvif->flags) &&
3822 !test_bit(WLVIF_FLAG_PSM, &wlvif->flags)) {
3824 ret = wl1271_ps_set_mode(wl, wlvif,
3825 STATION_AUTO_PS_MODE);
3831 /* Handle new association with HT. Do this after join. */
3833 if ((changed & BSS_CHANGED_HT) &&
3834 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3835 ret = wl1271_acx_set_ht_capabilities(wl,
3840 wl1271_warning("Set ht cap true failed %d",
3845 /* handle new association without HT and disassociation */
3846 else if (changed & BSS_CHANGED_ASSOC) {
3847 ret = wl1271_acx_set_ht_capabilities(wl,
3852 wl1271_warning("Set ht cap false failed %d",
3859 /* Handle HT information change. Done after join. */
3860 if ((changed & BSS_CHANGED_HT) &&
3861 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3862 ret = wl1271_acx_set_ht_information(wl, wlvif,
3863 bss_conf->ht_operation_mode);
3865 wl1271_warning("Set ht information failed %d", ret);
3874 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
3875 struct ieee80211_vif *vif,
3876 struct ieee80211_bss_conf *bss_conf,
3879 struct wl1271 *wl = hw->priv;
3880 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3881 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3884 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed 0x%x",
3887 mutex_lock(&wl->mutex);
3889 if (unlikely(wl->state == WL1271_STATE_OFF))
3892 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
3895 ret = wl1271_ps_elp_wakeup(wl);
3900 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
3902 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
3904 wl1271_ps_elp_sleep(wl);
3907 mutex_unlock(&wl->mutex);
3910 static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
3911 struct ieee80211_vif *vif, u16 queue,
3912 const struct ieee80211_tx_queue_params *params)
3914 struct wl1271 *wl = hw->priv;
3915 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3919 mutex_lock(&wl->mutex);
3921 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
3924 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
3926 ps_scheme = CONF_PS_SCHEME_LEGACY;
3928 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
3931 ret = wl1271_ps_elp_wakeup(wl);
3936 * the txop is confed in units of 32us by the mac80211,
3939 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
3940 params->cw_min, params->cw_max,
3941 params->aifs, params->txop << 5);
3945 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
3946 CONF_CHANNEL_TYPE_EDCF,
3947 wl1271_tx_get_queue(queue),
3948 ps_scheme, CONF_ACK_POLICY_LEGACY,
3952 wl1271_ps_elp_sleep(wl);
3955 mutex_unlock(&wl->mutex);
3960 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
3961 struct ieee80211_vif *vif)
3964 struct wl1271 *wl = hw->priv;
3965 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3966 u64 mactime = ULLONG_MAX;
3969 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
3971 mutex_lock(&wl->mutex);
3973 if (unlikely(wl->state == WL1271_STATE_OFF))
3976 ret = wl1271_ps_elp_wakeup(wl);
3980 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
3985 wl1271_ps_elp_sleep(wl);
3988 mutex_unlock(&wl->mutex);
3992 static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
3993 struct survey_info *survey)
3995 struct wl1271 *wl = hw->priv;
3996 struct ieee80211_conf *conf = &hw->conf;
4001 survey->channel = conf->channel;
4002 survey->filled = SURVEY_INFO_NOISE_DBM;
4003 survey->noise = wl->noise;
4008 static int wl1271_allocate_sta(struct wl1271 *wl,
4009 struct wl12xx_vif *wlvif,
4010 struct ieee80211_sta *sta)
4012 struct wl1271_station *wl_sta;
4016 if (wl->active_sta_count >= AP_MAX_STATIONS) {
4017 wl1271_warning("could not allocate HLID - too much stations");
4021 wl_sta = (struct wl1271_station *)sta->drv_priv;
4022 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4024 wl1271_warning("could not allocate HLID - too many links");
4028 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
4029 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
4030 wl->active_sta_count++;
4034 void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
4036 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
4039 clear_bit(hlid, wlvif->ap.sta_hlid_map);
4040 memset(wl->links[hlid].addr, 0, ETH_ALEN);
4041 wl->links[hlid].ba_bitmap = 0;
4042 wl1271_tx_reset_link_queues(wl, hlid);
4043 __clear_bit(hlid, &wl->ap_ps_map);
4044 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
4045 wl12xx_free_link(wl, wlvif, &hlid);
4046 wl->active_sta_count--;
4049 static int wl1271_op_sta_add(struct ieee80211_hw *hw,
4050 struct ieee80211_vif *vif,
4051 struct ieee80211_sta *sta)
4053 struct wl1271 *wl = hw->priv;
4054 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4055 struct wl1271_station *wl_sta;
4059 mutex_lock(&wl->mutex);
4061 if (unlikely(wl->state == WL1271_STATE_OFF))
4064 if (wlvif->bss_type != BSS_TYPE_AP_BSS)
4067 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4069 ret = wl1271_allocate_sta(wl, wlvif, sta);
4073 wl_sta = (struct wl1271_station *)sta->drv_priv;
4074 hlid = wl_sta->hlid;
4076 ret = wl1271_ps_elp_wakeup(wl);
4080 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
4084 ret = wl12xx_cmd_set_peer_state(wl, hlid);
4088 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true, hlid);
4093 wl1271_ps_elp_sleep(wl);
4097 wl1271_free_sta(wl, wlvif, hlid);
4100 mutex_unlock(&wl->mutex);
4104 static int wl1271_op_sta_remove(struct ieee80211_hw *hw,
4105 struct ieee80211_vif *vif,
4106 struct ieee80211_sta *sta)
4108 struct wl1271 *wl = hw->priv;
4109 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4110 struct wl1271_station *wl_sta;
4113 mutex_lock(&wl->mutex);
4115 if (unlikely(wl->state == WL1271_STATE_OFF))
4118 if (wlvif->bss_type != BSS_TYPE_AP_BSS)
4121 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4123 wl_sta = (struct wl1271_station *)sta->drv_priv;
4125 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4128 ret = wl1271_ps_elp_wakeup(wl);
4132 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
4136 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
4139 wl1271_ps_elp_sleep(wl);
4142 mutex_unlock(&wl->mutex);
4146 static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4147 struct ieee80211_vif *vif,
4148 enum ieee80211_ampdu_mlme_action action,
4149 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4152 struct wl1271 *wl = hw->priv;
4153 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4155 u8 hlid, *ba_bitmap;
4157 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4160 /* sanity check - the fields in FW are only 8bits wide */
4161 if (WARN_ON(tid > 0xFF))
4164 mutex_lock(&wl->mutex);
4166 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4171 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
4172 hlid = wlvif->sta.hlid;
4173 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
4174 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
4175 struct wl1271_station *wl_sta;
4177 wl_sta = (struct wl1271_station *)sta->drv_priv;
4178 hlid = wl_sta->hlid;
4179 ba_bitmap = &wl->links[hlid].ba_bitmap;
4185 ret = wl1271_ps_elp_wakeup(wl);
4189 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4193 case IEEE80211_AMPDU_RX_START:
4194 if (!wlvif->ba_support || !wlvif->ba_allowed) {
4199 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4201 wl1271_error("exceeded max RX BA sessions");
4205 if (*ba_bitmap & BIT(tid)) {
4207 wl1271_error("cannot enable RX BA session on active "
4212 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4215 *ba_bitmap |= BIT(tid);
4216 wl->ba_rx_session_count++;
4220 case IEEE80211_AMPDU_RX_STOP:
4221 if (!(*ba_bitmap & BIT(tid))) {
4223 wl1271_error("no active RX BA session on tid: %d",
4228 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4231 *ba_bitmap &= ~BIT(tid);
4232 wl->ba_rx_session_count--;
4237 * The BA initiator session management in FW independently.
4238 * Falling break here on purpose for all TX APDU commands.
4240 case IEEE80211_AMPDU_TX_START:
4241 case IEEE80211_AMPDU_TX_STOP:
4242 case IEEE80211_AMPDU_TX_OPERATIONAL:
4247 wl1271_error("Incorrect ampdu action id=%x\n", action);
4251 wl1271_ps_elp_sleep(wl);
4254 mutex_unlock(&wl->mutex);
4259 static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4260 struct ieee80211_vif *vif,
4261 const struct cfg80211_bitrate_mask *mask)
4263 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4264 struct wl1271 *wl = hw->priv;
4267 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4268 mask->control[NL80211_BAND_2GHZ].legacy,
4269 mask->control[NL80211_BAND_5GHZ].legacy);
4271 mutex_lock(&wl->mutex);
4273 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
4274 wlvif->bitrate_masks[i] =
4275 wl1271_tx_enabled_rates_get(wl,
4276 mask->control[i].legacy,
4279 if (unlikely(wl->state == WL1271_STATE_OFF))
4282 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4283 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4285 ret = wl1271_ps_elp_wakeup(wl);
4289 wl1271_set_band_rate(wl, wlvif);
4291 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4292 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4294 wl1271_ps_elp_sleep(wl);
4297 mutex_unlock(&wl->mutex);
4302 static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4303 struct ieee80211_channel_switch *ch_switch)
4305 struct wl1271 *wl = hw->priv;
4306 struct wl12xx_vif *wlvif;
4309 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4311 mutex_lock(&wl->mutex);
4313 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4314 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4315 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4316 ieee80211_chswitch_done(vif, false);
4321 ret = wl1271_ps_elp_wakeup(wl);
4325 /* TODO: change mac80211 to pass vif as param */
4326 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4327 ret = wl12xx_cmd_channel_switch(wl, wlvif, ch_switch);
4330 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4333 wl1271_ps_elp_sleep(wl);
4336 mutex_unlock(&wl->mutex);
4339 static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
4341 struct wl1271 *wl = hw->priv;
4344 mutex_lock(&wl->mutex);
4346 if (unlikely(wl->state == WL1271_STATE_OFF))
4349 /* packets are considered pending if in the TX queue or the FW */
4350 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
4352 mutex_unlock(&wl->mutex);
4357 /* can't be const, mac80211 writes to this */
4358 static struct ieee80211_rate wl1271_rates[] = {
4360 .hw_value = CONF_HW_BIT_RATE_1MBPS,
4361 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
4363 .hw_value = CONF_HW_BIT_RATE_2MBPS,
4364 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
4365 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4367 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
4368 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
4369 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4371 .hw_value = CONF_HW_BIT_RATE_11MBPS,
4372 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
4373 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4375 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4376 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4378 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4379 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4381 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4382 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4384 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4385 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4387 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4388 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4390 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4391 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4393 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4394 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4396 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4397 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4400 /* can't be const, mac80211 writes to this */
4401 static struct ieee80211_channel wl1271_channels[] = {
4402 { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
4403 { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
4404 { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
4405 { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
4406 { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
4407 { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
4408 { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
4409 { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
4410 { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
4411 { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
4412 { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
4413 { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
4414 { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
4415 { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
4418 /* mapping to indexes for wl1271_rates */
4419 static const u8 wl1271_rate_to_idx_2ghz[] = {
4420 /* MCS rates are used only with 11n */
4421 7, /* CONF_HW_RXTX_RATE_MCS7 */
4422 6, /* CONF_HW_RXTX_RATE_MCS6 */
4423 5, /* CONF_HW_RXTX_RATE_MCS5 */
4424 4, /* CONF_HW_RXTX_RATE_MCS4 */
4425 3, /* CONF_HW_RXTX_RATE_MCS3 */
4426 2, /* CONF_HW_RXTX_RATE_MCS2 */
4427 1, /* CONF_HW_RXTX_RATE_MCS1 */
4428 0, /* CONF_HW_RXTX_RATE_MCS0 */
4430 11, /* CONF_HW_RXTX_RATE_54 */
4431 10, /* CONF_HW_RXTX_RATE_48 */
4432 9, /* CONF_HW_RXTX_RATE_36 */
4433 8, /* CONF_HW_RXTX_RATE_24 */
4435 /* TI-specific rate */
4436 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22 */
4438 7, /* CONF_HW_RXTX_RATE_18 */
4439 6, /* CONF_HW_RXTX_RATE_12 */
4440 3, /* CONF_HW_RXTX_RATE_11 */
4441 5, /* CONF_HW_RXTX_RATE_9 */
4442 4, /* CONF_HW_RXTX_RATE_6 */
4443 2, /* CONF_HW_RXTX_RATE_5_5 */
4444 1, /* CONF_HW_RXTX_RATE_2 */
4445 0 /* CONF_HW_RXTX_RATE_1 */
4448 /* 11n STA capabilities */
4449 #define HW_RX_HIGHEST_RATE 72
4451 #define WL12XX_HT_CAP { \
4452 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 | \
4453 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT), \
4454 .ht_supported = true, \
4455 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K, \
4456 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
4458 .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, \
4459 .rx_highest = cpu_to_le16(HW_RX_HIGHEST_RATE), \
4460 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
4464 /* can't be const, mac80211 writes to this */
4465 static struct ieee80211_supported_band wl1271_band_2ghz = {
4466 .channels = wl1271_channels,
4467 .n_channels = ARRAY_SIZE(wl1271_channels),
4468 .bitrates = wl1271_rates,
4469 .n_bitrates = ARRAY_SIZE(wl1271_rates),
4470 .ht_cap = WL12XX_HT_CAP,
4473 /* 5 GHz data rates for WL1273 */
4474 static struct ieee80211_rate wl1271_rates_5ghz[] = {
4476 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4477 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4479 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4480 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4482 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4483 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4485 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4486 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4488 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4489 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4491 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4492 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4494 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4495 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4497 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4498 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4501 /* 5 GHz band channels for WL1273 */
4502 static struct ieee80211_channel wl1271_channels_5ghz[] = {
4503 { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
4504 { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
4505 { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
4506 { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
4507 { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
4508 { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
4509 { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
4510 { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
4511 { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
4512 { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
4513 { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
4514 { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
4515 { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
4516 { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
4517 { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
4518 { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
4519 { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
4520 { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
4521 { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
4522 { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
4523 { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
4524 { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
4525 { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
4526 { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
4527 { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
4528 { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
4529 { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
4530 { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
4531 { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
4532 { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
4533 { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
4534 { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
4535 { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
4536 { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
4539 /* mapping to indexes for wl1271_rates_5ghz */
4540 static const u8 wl1271_rate_to_idx_5ghz[] = {
4541 /* MCS rates are used only with 11n */
4542 7, /* CONF_HW_RXTX_RATE_MCS7 */
4543 6, /* CONF_HW_RXTX_RATE_MCS6 */
4544 5, /* CONF_HW_RXTX_RATE_MCS5 */
4545 4, /* CONF_HW_RXTX_RATE_MCS4 */
4546 3, /* CONF_HW_RXTX_RATE_MCS3 */
4547 2, /* CONF_HW_RXTX_RATE_MCS2 */
4548 1, /* CONF_HW_RXTX_RATE_MCS1 */
4549 0, /* CONF_HW_RXTX_RATE_MCS0 */
4551 7, /* CONF_HW_RXTX_RATE_54 */
4552 6, /* CONF_HW_RXTX_RATE_48 */
4553 5, /* CONF_HW_RXTX_RATE_36 */
4554 4, /* CONF_HW_RXTX_RATE_24 */
4556 /* TI-specific rate */
4557 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22 */
4559 3, /* CONF_HW_RXTX_RATE_18 */
4560 2, /* CONF_HW_RXTX_RATE_12 */
4561 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_11 */
4562 1, /* CONF_HW_RXTX_RATE_9 */
4563 0, /* CONF_HW_RXTX_RATE_6 */
4564 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_5_5 */
4565 CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_2 */
4566 CONF_HW_RXTX_RATE_UNSUPPORTED /* CONF_HW_RXTX_RATE_1 */
4569 static struct ieee80211_supported_band wl1271_band_5ghz = {
4570 .channels = wl1271_channels_5ghz,
4571 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
4572 .bitrates = wl1271_rates_5ghz,
4573 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
4574 .ht_cap = WL12XX_HT_CAP,
4577 static const u8 *wl1271_band_rate_to_idx[] = {
4578 [IEEE80211_BAND_2GHZ] = wl1271_rate_to_idx_2ghz,
4579 [IEEE80211_BAND_5GHZ] = wl1271_rate_to_idx_5ghz
4582 static const struct ieee80211_ops wl1271_ops = {
4583 .start = wl1271_op_start,
4584 .stop = wl1271_op_stop,
4585 .add_interface = wl1271_op_add_interface,
4586 .remove_interface = wl1271_op_remove_interface,
4587 .change_interface = wl12xx_op_change_interface,
4589 .suspend = wl1271_op_suspend,
4590 .resume = wl1271_op_resume,
4592 .config = wl1271_op_config,
4593 .prepare_multicast = wl1271_op_prepare_multicast,
4594 .configure_filter = wl1271_op_configure_filter,
4596 .set_key = wl1271_op_set_key,
4597 .hw_scan = wl1271_op_hw_scan,
4598 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
4599 .sched_scan_start = wl1271_op_sched_scan_start,
4600 .sched_scan_stop = wl1271_op_sched_scan_stop,
4601 .bss_info_changed = wl1271_op_bss_info_changed,
4602 .set_frag_threshold = wl1271_op_set_frag_threshold,
4603 .set_rts_threshold = wl1271_op_set_rts_threshold,
4604 .conf_tx = wl1271_op_conf_tx,
4605 .get_tsf = wl1271_op_get_tsf,
4606 .get_survey = wl1271_op_get_survey,
4607 .sta_add = wl1271_op_sta_add,
4608 .sta_remove = wl1271_op_sta_remove,
4609 .ampdu_action = wl1271_op_ampdu_action,
4610 .tx_frames_pending = wl1271_tx_frames_pending,
4611 .set_bitrate_mask = wl12xx_set_bitrate_mask,
4612 .channel_switch = wl12xx_op_channel_switch,
4613 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
4617 u8 wl1271_rate_to_idx(int rate, enum ieee80211_band band)
4621 BUG_ON(band >= sizeof(wl1271_band_rate_to_idx)/sizeof(u8 *));
4623 if (unlikely(rate >= CONF_HW_RXTX_RATE_MAX)) {
4624 wl1271_error("Illegal RX rate from HW: %d", rate);
4628 idx = wl1271_band_rate_to_idx[band][rate];
4629 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
4630 wl1271_error("Unsupported RX rate from HW: %d", rate);
4637 static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
4638 struct device_attribute *attr,
4641 struct wl1271 *wl = dev_get_drvdata(dev);
4646 mutex_lock(&wl->mutex);
4647 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
4649 mutex_unlock(&wl->mutex);
4655 static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
4656 struct device_attribute *attr,
4657 const char *buf, size_t count)
4659 struct wl1271 *wl = dev_get_drvdata(dev);
4663 ret = kstrtoul(buf, 10, &res);
4665 wl1271_warning("incorrect value written to bt_coex_mode");
4669 mutex_lock(&wl->mutex);
4673 if (res == wl->sg_enabled)
4676 wl->sg_enabled = res;
4678 if (wl->state == WL1271_STATE_OFF)
4681 ret = wl1271_ps_elp_wakeup(wl);
4685 wl1271_acx_sg_enable(wl, wl->sg_enabled);
4686 wl1271_ps_elp_sleep(wl);
4689 mutex_unlock(&wl->mutex);
4693 static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
4694 wl1271_sysfs_show_bt_coex_state,
4695 wl1271_sysfs_store_bt_coex_state);
4697 static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
4698 struct device_attribute *attr,
4701 struct wl1271 *wl = dev_get_drvdata(dev);
4706 mutex_lock(&wl->mutex);
4707 if (wl->hw_pg_ver >= 0)
4708 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
4710 len = snprintf(buf, len, "n/a\n");
4711 mutex_unlock(&wl->mutex);
4716 static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
4717 wl1271_sysfs_show_hw_pg_ver, NULL);
4719 static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
4720 struct bin_attribute *bin_attr,
4721 char *buffer, loff_t pos, size_t count)
4723 struct device *dev = container_of(kobj, struct device, kobj);
4724 struct wl1271 *wl = dev_get_drvdata(dev);
4728 ret = mutex_lock_interruptible(&wl->mutex);
4730 return -ERESTARTSYS;
4732 /* Let only one thread read the log at a time, blocking others */
4733 while (wl->fwlog_size == 0) {
4736 prepare_to_wait_exclusive(&wl->fwlog_waitq,
4738 TASK_INTERRUPTIBLE);
4740 if (wl->fwlog_size != 0) {
4741 finish_wait(&wl->fwlog_waitq, &wait);
4745 mutex_unlock(&wl->mutex);
4748 finish_wait(&wl->fwlog_waitq, &wait);
4750 if (signal_pending(current))
4751 return -ERESTARTSYS;
4753 ret = mutex_lock_interruptible(&wl->mutex);
4755 return -ERESTARTSYS;
4758 /* Check if the fwlog is still valid */
4759 if (wl->fwlog_size < 0) {
4760 mutex_unlock(&wl->mutex);
4764 /* Seeking is not supported - old logs are not kept. Disregard pos. */
4765 len = min(count, (size_t)wl->fwlog_size);
4766 wl->fwlog_size -= len;
4767 memcpy(buffer, wl->fwlog, len);
4769 /* Make room for new messages */
4770 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
4772 mutex_unlock(&wl->mutex);
4777 static struct bin_attribute fwlog_attr = {
4778 .attr = {.name = "fwlog", .mode = S_IRUSR},
4779 .read = wl1271_sysfs_read_fwlog,
4782 static bool wl12xx_mac_in_fuse(struct wl1271 *wl)
4784 bool supported = false;
4787 if (wl->chip.id == CHIP_ID_1283_PG20) {
4788 major = WL128X_PG_GET_MAJOR(wl->hw_pg_ver);
4789 minor = WL128X_PG_GET_MINOR(wl->hw_pg_ver);
4791 /* in wl128x we have the MAC address if the PG is >= (2, 1) */
4792 if (major > 2 || (major == 2 && minor >= 1))
4795 major = WL127X_PG_GET_MAJOR(wl->hw_pg_ver);
4796 minor = WL127X_PG_GET_MINOR(wl->hw_pg_ver);
4798 /* in wl127x we have the MAC address if the PG is >= (3, 1) */
4799 if (major == 3 && minor >= 1)
4803 wl1271_debug(DEBUG_PROBE,
4804 "PG Ver major = %d minor = %d, MAC %s present",
4805 major, minor, supported ? "is" : "is not");
4810 static void wl12xx_derive_mac_addresses(struct wl1271 *wl,
4811 u32 oui, u32 nic, int n)
4815 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x, n %d",
4818 if (nic + n - 1 > 0xffffff)
4819 wl1271_warning("NIC part of the MAC address wraps around!");
4821 for (i = 0; i < n; i++) {
4822 wl->addresses[i].addr[0] = (u8)(oui >> 16);
4823 wl->addresses[i].addr[1] = (u8)(oui >> 8);
4824 wl->addresses[i].addr[2] = (u8) oui;
4825 wl->addresses[i].addr[3] = (u8)(nic >> 16);
4826 wl->addresses[i].addr[4] = (u8)(nic >> 8);
4827 wl->addresses[i].addr[5] = (u8) nic;
4831 wl->hw->wiphy->n_addresses = n;
4832 wl->hw->wiphy->addresses = wl->addresses;
4835 static void wl12xx_get_fuse_mac(struct wl1271 *wl)
4839 wl1271_set_partition(wl, &wl12xx_part_table[PART_DRPW]);
4841 mac1 = wl1271_read32(wl, WL12XX_REG_FUSE_BD_ADDR_1);
4842 mac2 = wl1271_read32(wl, WL12XX_REG_FUSE_BD_ADDR_2);
4844 /* these are the two parts of the BD_ADDR */
4845 wl->fuse_oui_addr = ((mac2 & 0xffff) << 8) +
4846 ((mac1 & 0xff000000) >> 24);
4847 wl->fuse_nic_addr = mac1 & 0xffffff;
4849 wl1271_set_partition(wl, &wl12xx_part_table[PART_DOWN]);
4852 static int wl12xx_get_hw_info(struct wl1271 *wl)
4857 ret = wl12xx_set_power_on(wl);
4861 wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
4863 if (wl->chip.id == CHIP_ID_1283_PG20)
4864 die_info = wl1271_top_reg_read(wl, WL128X_REG_FUSE_DATA_2_1);
4866 die_info = wl1271_top_reg_read(wl, WL127X_REG_FUSE_DATA_2_1);
4868 wl->hw_pg_ver = (s8) (die_info & PG_VER_MASK) >> PG_VER_OFFSET;
4870 if (!wl12xx_mac_in_fuse(wl)) {
4871 wl->fuse_oui_addr = 0;
4872 wl->fuse_nic_addr = 0;
4874 wl12xx_get_fuse_mac(wl);
4877 wl1271_power_off(wl);
4882 static int wl1271_register_hw(struct wl1271 *wl)
4885 u32 oui_addr = 0, nic_addr = 0;
4887 if (wl->mac80211_registered)
4890 ret = wl12xx_get_hw_info(wl);
4892 wl1271_error("couldn't get hw info");
4896 ret = wl1271_fetch_nvs(wl);
4898 /* NOTE: The wl->nvs->nvs element must be first, in
4899 * order to simplify the casting, we assume it is at
4900 * the beginning of the wl->nvs structure.
4902 u8 *nvs_ptr = (u8 *)wl->nvs;
4905 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
4907 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
4910 /* if the MAC address is zeroed in the NVS derive from fuse */
4911 if (oui_addr == 0 && nic_addr == 0) {
4912 oui_addr = wl->fuse_oui_addr;
4913 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
4914 nic_addr = wl->fuse_nic_addr + 1;
4917 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr, 2);
4919 ret = ieee80211_register_hw(wl->hw);
4921 wl1271_error("unable to register mac80211 hw: %d", ret);
4925 wl->mac80211_registered = true;
4927 wl1271_debugfs_init(wl);
4929 register_netdevice_notifier(&wl1271_dev_notifier);
4931 wl1271_notice("loaded");
4937 static void wl1271_unregister_hw(struct wl1271 *wl)
4939 if (wl->state == WL1271_STATE_PLT)
4940 wl1271_plt_stop(wl);
4942 unregister_netdevice_notifier(&wl1271_dev_notifier);
4943 ieee80211_unregister_hw(wl->hw);
4944 wl->mac80211_registered = false;
4948 static int wl1271_init_ieee80211(struct wl1271 *wl)
4950 static const u32 cipher_suites[] = {
4951 WLAN_CIPHER_SUITE_WEP40,
4952 WLAN_CIPHER_SUITE_WEP104,
4953 WLAN_CIPHER_SUITE_TKIP,
4954 WLAN_CIPHER_SUITE_CCMP,
4955 WL1271_CIPHER_SUITE_GEM,
4958 /* The tx descriptor buffer and the TKIP space. */
4959 wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
4960 sizeof(struct wl1271_tx_hw_descr);
4963 /* FIXME: find a proper value */
4964 wl->hw->channel_change_time = 10000;
4965 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
4967 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
4968 IEEE80211_HW_SUPPORTS_PS |
4969 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4970 IEEE80211_HW_SUPPORTS_UAPSD |
4971 IEEE80211_HW_HAS_RATE_CONTROL |
4972 IEEE80211_HW_CONNECTION_MONITOR |
4973 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
4974 IEEE80211_HW_SPECTRUM_MGMT |
4975 IEEE80211_HW_AP_LINK_PS |
4976 IEEE80211_HW_AMPDU_AGGREGATION |
4977 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
4979 wl->hw->wiphy->cipher_suites = cipher_suites;
4980 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
4982 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4983 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
4984 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
4985 wl->hw->wiphy->max_scan_ssids = 1;
4986 wl->hw->wiphy->max_sched_scan_ssids = 16;
4987 wl->hw->wiphy->max_match_sets = 16;
4989 * Maximum length of elements in scanning probe request templates
4990 * should be the maximum length possible for a template, without
4991 * the IEEE80211 header of the template
4993 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_DFLT_SIZE -
4994 sizeof(struct ieee80211_header);
4996 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_DFLT_SIZE -
4997 sizeof(struct ieee80211_header);
4999 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5001 /* make sure all our channels fit in the scanned_ch bitmask */
5002 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5003 ARRAY_SIZE(wl1271_channels_5ghz) >
5004 WL1271_MAX_CHANNELS);
5006 * We keep local copies of the band structs because we need to
5007 * modify them on a per-device basis.
5009 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5010 sizeof(wl1271_band_2ghz));
5011 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5012 sizeof(wl1271_band_5ghz));
5014 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5015 &wl->bands[IEEE80211_BAND_2GHZ];
5016 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5017 &wl->bands[IEEE80211_BAND_5GHZ];
5020 wl->hw->max_rates = 1;
5022 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5024 /* the FW answers probe-requests in AP-mode */
5025 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5026 wl->hw->wiphy->probe_resp_offload =
5027 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5028 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5029 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5031 SET_IEEE80211_DEV(wl->hw, wl->dev);
5033 wl->hw->sta_data_size = sizeof(struct wl1271_station);
5034 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
5036 wl->hw->max_rx_aggregation_subframes = 8;
5041 #define WL1271_DEFAULT_CHANNEL 0
5043 static struct ieee80211_hw *wl1271_alloc_hw(void)
5045 struct ieee80211_hw *hw;
5050 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
5052 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5054 wl1271_error("could not alloc ieee80211_hw");
5060 memset(wl, 0, sizeof(*wl));
5062 INIT_LIST_HEAD(&wl->list);
5063 INIT_LIST_HEAD(&wl->wlvif_list);
5067 for (i = 0; i < NUM_TX_QUEUES; i++)
5068 for (j = 0; j < WL12XX_MAX_LINKS; j++)
5069 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5071 skb_queue_head_init(&wl->deferred_rx_queue);
5072 skb_queue_head_init(&wl->deferred_tx_queue);
5074 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
5075 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
5076 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5077 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5078 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
5080 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5081 if (!wl->freezable_wq) {
5086 wl->channel = WL1271_DEFAULT_CHANNEL;
5088 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
5089 wl->band = IEEE80211_BAND_2GHZ;
5092 wl->sg_enabled = true;
5095 wl->ap_fw_ps_map = 0;
5097 wl->platform_quirks = 0;
5098 wl->sched_scanning = false;
5099 wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
5100 wl->system_hlid = WL12XX_SYSTEM_HLID;
5101 wl->active_sta_count = 0;
5103 init_waitqueue_head(&wl->fwlog_waitq);
5105 /* The system link is always allocated */
5106 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5108 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
5109 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
5110 wl->tx_frames[i] = NULL;
5112 spin_lock_init(&wl->wl_lock);
5114 wl->state = WL1271_STATE_OFF;
5115 mutex_init(&wl->mutex);
5117 /* Apply default driver configuration. */
5118 wl1271_conf_init(wl);
5120 order = get_order(WL1271_AGGR_BUFFER_SIZE);
5121 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5122 if (!wl->aggr_buf) {
5127 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5128 if (!wl->dummy_packet) {
5133 /* Allocate one page for the FW log */
5134 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5137 goto err_dummy_packet;
5143 dev_kfree_skb(wl->dummy_packet);
5146 free_pages((unsigned long)wl->aggr_buf, order);
5149 destroy_workqueue(wl->freezable_wq);
5152 wl1271_debugfs_exit(wl);
5153 ieee80211_free_hw(hw);
5157 return ERR_PTR(ret);
5160 static int wl1271_free_hw(struct wl1271 *wl)
5162 /* Unblock any fwlog readers */
5163 mutex_lock(&wl->mutex);
5164 wl->fwlog_size = -1;
5165 wake_up_interruptible_all(&wl->fwlog_waitq);
5166 mutex_unlock(&wl->mutex);
5168 device_remove_bin_file(wl->dev, &fwlog_attr);
5170 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5172 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5173 free_page((unsigned long)wl->fwlog);
5174 dev_kfree_skb(wl->dummy_packet);
5175 free_pages((unsigned long)wl->aggr_buf,
5176 get_order(WL1271_AGGR_BUFFER_SIZE));
5178 wl1271_debugfs_exit(wl);
5185 kfree(wl->fw_status);
5186 kfree(wl->tx_res_if);
5187 destroy_workqueue(wl->freezable_wq);
5189 ieee80211_free_hw(wl->hw);
5194 static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5196 struct wl1271 *wl = cookie;
5197 unsigned long flags;
5199 wl1271_debug(DEBUG_IRQ, "IRQ");
5201 /* complete the ELP completion */
5202 spin_lock_irqsave(&wl->wl_lock, flags);
5203 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5204 if (wl->elp_compl) {
5205 complete(wl->elp_compl);
5206 wl->elp_compl = NULL;
5209 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5210 /* don't enqueue a work right now. mark it as pending */
5211 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5212 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5213 disable_irq_nosync(wl->irq);
5214 pm_wakeup_event(wl->dev, 0);
5215 spin_unlock_irqrestore(&wl->wl_lock, flags);
5218 spin_unlock_irqrestore(&wl->wl_lock, flags);
5220 return IRQ_WAKE_THREAD;
5223 static int __devinit wl12xx_probe(struct platform_device *pdev)
5225 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
5226 struct ieee80211_hw *hw;
5228 unsigned long irqflags;
5231 hw = wl1271_alloc_hw();
5233 wl1271_error("can't allocate hw");
5239 wl->irq = platform_get_irq(pdev, 0);
5240 wl->ref_clock = pdata->board_ref_clock;
5241 wl->tcxo_clock = pdata->board_tcxo_clock;
5242 wl->platform_quirks = pdata->platform_quirks;
5243 wl->set_power = pdata->set_power;
5244 wl->dev = &pdev->dev;
5245 wl->if_ops = pdata->ops;
5247 platform_set_drvdata(pdev, wl);
5249 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5250 irqflags = IRQF_TRIGGER_RISING;
5252 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5254 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wl1271_irq,
5258 wl1271_error("request_irq() failed: %d", ret);
5262 ret = enable_irq_wake(wl->irq);
5264 wl->irq_wake_enabled = true;
5265 device_init_wakeup(wl->dev, 1);
5266 if (pdata->pwr_in_suspend)
5267 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
5270 disable_irq(wl->irq);
5272 ret = wl1271_init_ieee80211(wl);
5276 ret = wl1271_register_hw(wl);
5280 /* Create sysfs file to control bt coex state */
5281 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5283 wl1271_error("failed to create sysfs file bt_coex_state");
5287 /* Create sysfs file to get HW PG version */
5288 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5290 wl1271_error("failed to create sysfs file hw_pg_ver");
5291 goto out_bt_coex_state;
5294 /* Create sysfs file for the FW log */
5295 ret = device_create_bin_file(wl->dev, &fwlog_attr);
5297 wl1271_error("failed to create sysfs file fwlog");
5304 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5307 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5310 free_irq(wl->irq, wl);
5319 static int __devexit wl12xx_remove(struct platform_device *pdev)
5321 struct wl1271 *wl = platform_get_drvdata(pdev);
5323 if (wl->irq_wake_enabled) {
5324 device_init_wakeup(wl->dev, 0);
5325 disable_irq_wake(wl->irq);
5327 wl1271_unregister_hw(wl);
5328 free_irq(wl->irq, wl);
5334 static const struct platform_device_id wl12xx_id_table[] __devinitconst = {
5336 { } /* Terminating Entry */
5338 MODULE_DEVICE_TABLE(platform, wl12xx_id_table);
5340 static struct platform_driver wl12xx_driver = {
5341 .probe = wl12xx_probe,
5342 .remove = __devexit_p(wl12xx_remove),
5343 .id_table = wl12xx_id_table,
5345 .name = "wl12xx_driver",
5346 .owner = THIS_MODULE,
5350 static int __init wl12xx_init(void)
5352 return platform_driver_register(&wl12xx_driver);
5354 module_init(wl12xx_init);
5356 static void __exit wl12xx_exit(void)
5358 platform_driver_unregister(&wl12xx_driver);
5360 module_exit(wl12xx_exit);
5362 u32 wl12xx_debug_level = DEBUG_NONE;
5363 EXPORT_SYMBOL_GPL(wl12xx_debug_level);
5364 module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
5365 MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
5367 module_param_named(fwlog, fwlog_param, charp, 0);
5368 MODULE_PARM_DESC(fwlog,
5369 "FW logger options: continuous, ondemand, dbgpins or disable");
5371 module_param(bug_on_recovery, bool, S_IRUSR | S_IWUSR);
5372 MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
5374 MODULE_LICENSE("GPL");
5375 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
5376 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");