2 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/module.h>
21 #define CCK_RATE(_idx, _rate) { \
23 .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
24 .hw_value = (MT_PHY_TYPE_CCK << 8) | _idx, \
25 .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + _idx), \
28 #define OFDM_RATE(_idx, _rate) { \
30 .hw_value = (MT_PHY_TYPE_OFDM << 8) | _idx, \
31 .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | _idx, \
34 struct ieee80211_rate mt76x02_rates[] = {
48 EXPORT_SYMBOL_GPL(mt76x02_rates);
50 static const struct ieee80211_iface_limit mt76x02_if_limits[] = {
53 .types = BIT(NL80211_IFTYPE_ADHOC)
56 .types = BIT(NL80211_IFTYPE_STATION) |
57 #ifdef CONFIG_MAC80211_MESH
58 BIT(NL80211_IFTYPE_MESH_POINT) |
60 BIT(NL80211_IFTYPE_AP)
64 static const struct ieee80211_iface_combination mt76x02_if_comb[] = {
66 .limits = mt76x02_if_limits,
67 .n_limits = ARRAY_SIZE(mt76x02_if_limits),
69 .num_different_channels = 1,
70 .beacon_int_infra_match = true,
71 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
72 BIT(NL80211_CHAN_WIDTH_20) |
73 BIT(NL80211_CHAN_WIDTH_40) |
74 BIT(NL80211_CHAN_WIDTH_80),
79 mt76x02_led_set_config(struct mt76_dev *mdev, u8 delay_on,
82 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev,
86 val = MT_LED_STATUS_DURATION(0xff) |
87 MT_LED_STATUS_OFF(delay_off) |
88 MT_LED_STATUS_ON(delay_on);
90 mt76_wr(dev, MT_LED_S0(mdev->led_pin), val);
91 mt76_wr(dev, MT_LED_S1(mdev->led_pin), val);
93 val = MT_LED_CTRL_REPLAY(mdev->led_pin) |
94 MT_LED_CTRL_KICK(mdev->led_pin);
96 val |= MT_LED_CTRL_POLARITY(mdev->led_pin);
97 mt76_wr(dev, MT_LED_CTRL, val);
101 mt76x02_led_set_blink(struct led_classdev *led_cdev,
102 unsigned long *delay_on,
103 unsigned long *delay_off)
105 struct mt76_dev *mdev = container_of(led_cdev, struct mt76_dev,
107 u8 delta_on, delta_off;
109 delta_off = max_t(u8, *delay_off / 10, 1);
110 delta_on = max_t(u8, *delay_on / 10, 1);
112 mt76x02_led_set_config(mdev, delta_on, delta_off);
118 mt76x02_led_set_brightness(struct led_classdev *led_cdev,
119 enum led_brightness brightness)
121 struct mt76_dev *mdev = container_of(led_cdev, struct mt76_dev,
125 mt76x02_led_set_config(mdev, 0, 0xff);
127 mt76x02_led_set_config(mdev, 0xff, 0);
130 void mt76x02_init_device(struct mt76x02_dev *dev)
132 struct ieee80211_hw *hw = mt76_hw(dev);
133 struct wiphy *wiphy = hw->wiphy;
135 INIT_DELAYED_WORK(&dev->mac_work, mt76x02_mac_work);
139 hw->max_report_rates = 7;
140 hw->max_rate_tries = 1;
141 hw->extra_tx_headroom = 2;
143 wiphy->interface_modes =
144 BIT(NL80211_IFTYPE_STATION) |
145 #ifdef CONFIG_MAC80211_MESH
146 BIT(NL80211_IFTYPE_MESH_POINT) |
148 BIT(NL80211_IFTYPE_ADHOC);
150 if (mt76_is_usb(dev)) {
151 hw->extra_tx_headroom += sizeof(struct mt76x02_txwi) +
154 INIT_DELAYED_WORK(&dev->wdt_work, mt76x02_wdt_work);
156 mt76x02_dfs_init_detector(dev);
158 wiphy->reg_notifier = mt76x02_regd_notifier;
159 wiphy->iface_combinations = mt76x02_if_comb;
160 wiphy->n_iface_combinations = ARRAY_SIZE(mt76x02_if_comb);
161 wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
162 wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
164 /* init led callbacks */
165 if (IS_ENABLED(CONFIG_MT76_LEDS)) {
166 dev->mt76.led_cdev.brightness_set =
167 mt76x02_led_set_brightness;
168 dev->mt76.led_cdev.blink_set = mt76x02_led_set_blink;
172 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
174 hw->sta_data_size = sizeof(struct mt76x02_sta);
175 hw->vif_data_size = sizeof(struct mt76x02_vif);
177 ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
178 ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
180 dev->mt76.global_wcid.idx = 255;
181 dev->mt76.global_wcid.hw_key_idx = -1;
184 if (is_mt76x2(dev)) {
185 dev->mt76.sband_2g.sband.ht_cap.cap |=
186 IEEE80211_HT_CAP_LDPC_CODING;
187 dev->mt76.sband_5g.sband.ht_cap.cap |=
188 IEEE80211_HT_CAP_LDPC_CODING;
189 dev->mt76.chainmask = 0x202;
190 dev->mt76.antenna_mask = 3;
192 dev->mt76.chainmask = 0x101;
193 dev->mt76.antenna_mask = 1;
196 EXPORT_SYMBOL_GPL(mt76x02_init_device);
198 void mt76x02_configure_filter(struct ieee80211_hw *hw,
199 unsigned int changed_flags,
200 unsigned int *total_flags, u64 multicast)
202 struct mt76x02_dev *dev = hw->priv;
205 #define MT76_FILTER(_flag, _hw) do { \
206 flags |= *total_flags & FIF_##_flag; \
207 dev->mt76.rxfilter &= ~(_hw); \
208 dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw); \
211 mutex_lock(&dev->mt76.mutex);
213 dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_OTHER_BSS;
215 MT76_FILTER(FCSFAIL, MT_RX_FILTR_CFG_CRC_ERR);
216 MT76_FILTER(PLCPFAIL, MT_RX_FILTR_CFG_PHY_ERR);
217 MT76_FILTER(CONTROL, MT_RX_FILTR_CFG_ACK |
218 MT_RX_FILTR_CFG_CTS |
219 MT_RX_FILTR_CFG_CFEND |
220 MT_RX_FILTR_CFG_CFACK |
222 MT_RX_FILTR_CFG_CTRL_RSV);
223 MT76_FILTER(PSPOLL, MT_RX_FILTR_CFG_PSPOLL);
225 *total_flags = flags;
226 mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
228 mutex_unlock(&dev->mt76.mutex);
230 EXPORT_SYMBOL_GPL(mt76x02_configure_filter);
232 int mt76x02_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
233 struct ieee80211_sta *sta)
235 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
236 struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
237 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
240 memset(msta, 0, sizeof(*msta));
242 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, ARRAY_SIZE(dev->mt76.wcid));
248 msta->wcid.idx = idx;
249 msta->wcid.hw_key_idx = -1;
250 mt76x02_mac_wcid_setup(dev, idx, mvif->idx, sta->addr);
251 mt76x02_mac_wcid_set_drop(dev, idx, false);
253 if (vif->type == NL80211_IFTYPE_AP)
254 set_bit(MT_WCID_FLAG_CHECK_PS, &msta->wcid.flags);
258 EXPORT_SYMBOL_GPL(mt76x02_sta_add);
260 void mt76x02_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
261 struct ieee80211_sta *sta)
263 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
264 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
267 mt76x02_mac_wcid_set_drop(dev, idx, true);
268 mt76x02_mac_wcid_setup(dev, idx, 0, NULL);
270 EXPORT_SYMBOL_GPL(mt76x02_sta_remove);
273 mt76x02_vif_init(struct mt76x02_dev *dev, struct ieee80211_vif *vif,
276 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
277 struct mt76_txq *mtxq;
279 memset(mvif, 0, sizeof(*mvif));
282 mvif->group_wcid.idx = MT_VIF_WCID(idx);
283 mvif->group_wcid.hw_key_idx = -1;
284 mtxq = (struct mt76_txq *) vif->txq->drv_priv;
285 mtxq->wcid = &mvif->group_wcid;
287 mt76_txq_init(&dev->mt76, vif->txq);
291 mt76x02_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
293 struct mt76x02_dev *dev = hw->priv;
294 unsigned int idx = 0;
296 /* Allow to change address in HW if we create first interface. */
297 if (!dev->vif_mask &&
298 (((vif->addr[0] ^ dev->mt76.macaddr[0]) & ~GENMASK(4, 1)) ||
299 memcmp(vif->addr + 1, dev->mt76.macaddr + 1, ETH_ALEN - 1)))
300 mt76x02_mac_setaddr(dev, vif->addr);
302 if (vif->addr[0] & BIT(1))
303 idx = 1 + (((dev->mt76.macaddr[0] ^ vif->addr[0]) >> 2) & 7);
306 * Client mode typically only has one configurable BSSID register,
307 * which is used for bssidx=0. This is linked to the MAC address.
308 * Since mac80211 allows changing interface types, and we cannot
309 * force the use of the primary MAC address for a station mode
310 * interface, we need some other way of configuring a per-interface
312 * The hardware provides an AP-Client feature, where bssidx 0-7 are
313 * used for AP mode and bssidx 8-15 for client mode.
314 * We shift the station interface bss index by 8 to force the
315 * hardware to recognize the BSSID.
316 * The resulting bssidx mismatch for unicast frames is ignored by hw.
318 if (vif->type == NL80211_IFTYPE_STATION)
321 if (dev->vif_mask & BIT(idx))
324 dev->vif_mask |= BIT(idx);
326 mt76x02_vif_init(dev, vif, idx);
329 EXPORT_SYMBOL_GPL(mt76x02_add_interface);
331 void mt76x02_remove_interface(struct ieee80211_hw *hw,
332 struct ieee80211_vif *vif)
334 struct mt76x02_dev *dev = hw->priv;
335 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
337 mt76_txq_remove(&dev->mt76, vif->txq);
338 dev->vif_mask &= ~BIT(mvif->idx);
340 EXPORT_SYMBOL_GPL(mt76x02_remove_interface);
342 int mt76x02_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
343 struct ieee80211_ampdu_params *params)
345 enum ieee80211_ampdu_mlme_action action = params->action;
346 struct ieee80211_sta *sta = params->sta;
347 struct mt76x02_dev *dev = hw->priv;
348 struct mt76x02_sta *msta = (struct mt76x02_sta *) sta->drv_priv;
349 struct ieee80211_txq *txq = sta->txq[params->tid];
350 u16 tid = params->tid;
351 u16 *ssn = ¶ms->ssn;
352 struct mt76_txq *mtxq;
357 mtxq = (struct mt76_txq *)txq->drv_priv;
360 case IEEE80211_AMPDU_RX_START:
361 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid,
362 *ssn, params->buf_size);
363 mt76_set(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
365 case IEEE80211_AMPDU_RX_STOP:
366 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
367 mt76_clear(dev, MT_WCID_ADDR(msta->wcid.idx) + 4,
370 case IEEE80211_AMPDU_TX_OPERATIONAL:
372 mtxq->send_bar = false;
373 ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
375 case IEEE80211_AMPDU_TX_STOP_FLUSH:
376 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
378 ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
380 case IEEE80211_AMPDU_TX_START:
381 mtxq->agg_ssn = *ssn << 4;
382 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
384 case IEEE80211_AMPDU_TX_STOP_CONT:
386 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
392 EXPORT_SYMBOL_GPL(mt76x02_ampdu_action);
394 int mt76x02_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
395 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
396 struct ieee80211_key_conf *key)
398 struct mt76x02_dev *dev = hw->priv;
399 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
400 struct mt76x02_sta *msta;
401 struct mt76_wcid *wcid;
402 int idx = key->keyidx;
405 /* fall back to sw encryption for unsupported ciphers */
406 switch (key->cipher) {
407 case WLAN_CIPHER_SUITE_WEP40:
408 case WLAN_CIPHER_SUITE_WEP104:
409 case WLAN_CIPHER_SUITE_TKIP:
410 case WLAN_CIPHER_SUITE_CCMP:
417 * The hardware does not support per-STA RX GTK, fall back
418 * to software mode for these.
420 if ((vif->type == NL80211_IFTYPE_ADHOC ||
421 vif->type == NL80211_IFTYPE_MESH_POINT) &&
422 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
423 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
424 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
427 msta = sta ? (struct mt76x02_sta *) sta->drv_priv : NULL;
428 wcid = msta ? &msta->wcid : &mvif->group_wcid;
430 if (cmd == SET_KEY) {
431 key->hw_key_idx = wcid->idx;
432 wcid->hw_key_idx = idx;
433 if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
434 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
438 if (idx == wcid->hw_key_idx) {
439 wcid->hw_key_idx = -1;
445 mt76_wcid_key_setup(&dev->mt76, wcid, key);
448 if (key || wcid->hw_key_idx == idx) {
449 ret = mt76x02_mac_wcid_set_key(dev, wcid->idx, key);
454 return mt76x02_mac_shared_key_setup(dev, mvif->idx, idx, key);
457 return mt76x02_mac_wcid_set_key(dev, msta->wcid.idx, key);
459 EXPORT_SYMBOL_GPL(mt76x02_set_key);
461 int mt76x02_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
462 u16 queue, const struct ieee80211_tx_queue_params *params)
464 struct mt76x02_dev *dev = hw->priv;
465 u8 cw_min = 5, cw_max = 10, qid;
468 qid = dev->mt76.q_tx[queue].hw_idx;
471 cw_min = fls(params->cw_min);
473 cw_max = fls(params->cw_max);
475 val = FIELD_PREP(MT_EDCA_CFG_TXOP, params->txop) |
476 FIELD_PREP(MT_EDCA_CFG_AIFSN, params->aifs) |
477 FIELD_PREP(MT_EDCA_CFG_CWMIN, cw_min) |
478 FIELD_PREP(MT_EDCA_CFG_CWMAX, cw_max);
479 mt76_wr(dev, MT_EDCA_CFG_AC(qid), val);
481 val = mt76_rr(dev, MT_WMM_TXOP(qid));
482 val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(qid));
483 val |= params->txop << MT_WMM_TXOP_SHIFT(qid);
484 mt76_wr(dev, MT_WMM_TXOP(qid), val);
486 val = mt76_rr(dev, MT_WMM_AIFSN);
487 val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(qid));
488 val |= params->aifs << MT_WMM_AIFSN_SHIFT(qid);
489 mt76_wr(dev, MT_WMM_AIFSN, val);
491 val = mt76_rr(dev, MT_WMM_CWMIN);
492 val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(qid));
493 val |= cw_min << MT_WMM_CWMIN_SHIFT(qid);
494 mt76_wr(dev, MT_WMM_CWMIN, val);
496 val = mt76_rr(dev, MT_WMM_CWMAX);
497 val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(qid));
498 val |= cw_max << MT_WMM_CWMAX_SHIFT(qid);
499 mt76_wr(dev, MT_WMM_CWMAX, val);
503 EXPORT_SYMBOL_GPL(mt76x02_conf_tx);
505 void mt76x02_set_tx_ackto(struct mt76x02_dev *dev)
507 u8 ackto, sifs, slottime = dev->slottime;
509 /* As defined by IEEE 802.11-2007 17.3.8.6 */
510 slottime += 3 * dev->coverage_class;
511 mt76_rmw_field(dev, MT_BKOFF_SLOT_CFG,
512 MT_BKOFF_SLOT_CFG_SLOTTIME, slottime);
514 sifs = mt76_get_field(dev, MT_XIFS_TIME_CFG,
515 MT_XIFS_TIME_CFG_OFDM_SIFS);
517 ackto = slottime + sifs;
518 mt76_rmw_field(dev, MT_TX_TIMEOUT_CFG,
519 MT_TX_TIMEOUT_CFG_ACKTO, ackto);
521 EXPORT_SYMBOL_GPL(mt76x02_set_tx_ackto);
523 void mt76x02_set_coverage_class(struct ieee80211_hw *hw,
526 struct mt76x02_dev *dev = hw->priv;
528 mutex_lock(&dev->mt76.mutex);
529 dev->coverage_class = coverage_class;
530 mt76x02_set_tx_ackto(dev);
531 mutex_unlock(&dev->mt76.mutex);
533 EXPORT_SYMBOL_GPL(mt76x02_set_coverage_class);
535 int mt76x02_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
537 struct mt76x02_dev *dev = hw->priv;
539 if (val != ~0 && val > 0xffff)
542 mutex_lock(&dev->mt76.mutex);
543 mt76x02_mac_set_rts_thresh(dev, val);
544 mutex_unlock(&dev->mt76.mutex);
548 EXPORT_SYMBOL_GPL(mt76x02_set_rts_threshold);
550 void mt76x02_sta_rate_tbl_update(struct ieee80211_hw *hw,
551 struct ieee80211_vif *vif,
552 struct ieee80211_sta *sta)
554 struct mt76x02_dev *dev = hw->priv;
555 struct mt76x02_sta *msta = (struct mt76x02_sta *) sta->drv_priv;
556 struct ieee80211_sta_rates *rates = rcu_dereference(sta->rates);
557 struct ieee80211_tx_rate rate = {};
562 rate.idx = rates->rate[0].idx;
563 rate.flags = rates->rate[0].flags;
564 mt76x02_mac_wcid_set_rate(dev, &msta->wcid, &rate);
565 msta->wcid.max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, &rate);
567 EXPORT_SYMBOL_GPL(mt76x02_sta_rate_tbl_update);
569 int mt76x02_insert_hdr_pad(struct sk_buff *skb)
571 int len = ieee80211_get_hdrlen_from_skb(skb);
577 memmove(skb->data, skb->data + 2, len);
580 skb->data[len + 1] = 0;
583 EXPORT_SYMBOL_GPL(mt76x02_insert_hdr_pad);
585 void mt76x02_remove_hdr_pad(struct sk_buff *skb, int len)
592 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
593 memmove(skb->data + len, skb->data, hdrlen);
596 EXPORT_SYMBOL_GPL(mt76x02_remove_hdr_pad);
598 void mt76x02_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
601 struct mt76x02_dev *dev = hw->priv;
603 if (mt76_is_mmio(dev))
604 tasklet_disable(&dev->pre_tbtt_tasklet);
605 set_bit(MT76_SCANNING, &dev->mt76.state);
607 EXPORT_SYMBOL_GPL(mt76x02_sw_scan);
609 void mt76x02_sw_scan_complete(struct ieee80211_hw *hw,
610 struct ieee80211_vif *vif)
612 struct mt76x02_dev *dev = hw->priv;
614 clear_bit(MT76_SCANNING, &dev->mt76.state);
615 if (mt76_is_mmio(dev))
616 tasklet_enable(&dev->pre_tbtt_tasklet);
618 if (dev->cal.gain_init_done) {
619 /* Restore AGC gain and resume calibration after scanning. */
620 dev->cal.low_gain = -1;
621 ieee80211_queue_delayed_work(hw, &dev->cal_work, 0);
624 EXPORT_SYMBOL_GPL(mt76x02_sw_scan_complete);
626 void mt76x02_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta,
629 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
630 struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
631 int idx = msta->wcid.idx;
633 mt76_stop_tx_queues(&dev->mt76, sta, true);
634 mt76x02_mac_wcid_set_drop(dev, idx, ps);
636 EXPORT_SYMBOL_GPL(mt76x02_sta_ps);
638 const u16 mt76x02_beacon_offsets[16] = {
639 /* 1024 byte per beacon */
648 /* BSS idx 8-15 not used for beacons */
659 static void mt76x02_set_beacon_offsets(struct mt76x02_dev *dev)
661 u16 val, base = MT_BEACON_BASE;
665 for (i = 0; i < 16; i++) {
666 val = mt76x02_beacon_offsets[i] - base;
667 regs[i / 4] |= (val / 64) << (8 * (i % 4));
670 for (i = 0; i < 4; i++)
671 mt76_wr(dev, MT_BCN_OFFSET(i), regs[i]);
674 void mt76x02_init_beacon_config(struct mt76x02_dev *dev)
678 if (mt76_is_mmio(dev)) {
679 /* Fire a pre-TBTT interrupt 8 ms before TBTT */
680 mt76_rmw_field(dev, MT_INT_TIMER_CFG, MT_INT_TIMER_CFG_PRE_TBTT,
682 mt76_rmw_field(dev, MT_INT_TIMER_CFG, MT_INT_TIMER_CFG_GP_TIMER,
684 mt76_wr(dev, MT_INT_TIMER_EN, 0);
687 mt76_clear(dev, MT_BEACON_TIME_CFG, (MT_BEACON_TIME_CFG_TIMER_EN |
688 MT_BEACON_TIME_CFG_TBTT_EN |
689 MT_BEACON_TIME_CFG_BEACON_TX));
690 mt76_set(dev, MT_BEACON_TIME_CFG, MT_BEACON_TIME_CFG_SYNC_MODE);
691 mt76_wr(dev, MT_BCN_BYPASS_MASK, 0xffff);
693 for (i = 0; i < 8; i++)
694 mt76x02_mac_set_beacon(dev, i, NULL);
696 mt76x02_set_beacon_offsets(dev);
698 EXPORT_SYMBOL_GPL(mt76x02_init_beacon_config);
700 void mt76x02_bss_info_changed(struct ieee80211_hw *hw,
701 struct ieee80211_vif *vif,
702 struct ieee80211_bss_conf *info,
705 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
706 struct mt76x02_dev *dev = hw->priv;
708 mutex_lock(&dev->mt76.mutex);
710 if (changed & BSS_CHANGED_BSSID)
711 mt76x02_mac_set_bssid(dev, mvif->idx, info->bssid);
713 if (changed & BSS_CHANGED_HT || changed & BSS_CHANGED_ERP_CTS_PROT)
714 mt76x02_mac_set_tx_protection(dev, info->use_cts_prot,
715 info->ht_operation_mode);
717 if (changed & BSS_CHANGED_BEACON_INT) {
718 mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
719 MT_BEACON_TIME_CFG_INTVAL,
720 info->beacon_int << 4);
721 dev->beacon_int = info->beacon_int;
724 if (changed & BSS_CHANGED_BEACON_ENABLED)
725 mt76x02_mac_set_beacon_enable(dev, vif, info->enable_beacon);
727 if (changed & BSS_CHANGED_ERP_PREAMBLE)
728 mt76x02_mac_set_short_preamble(dev, info->use_short_preamble);
730 if (changed & BSS_CHANGED_ERP_SLOT) {
731 int slottime = info->use_short_slot ? 9 : 20;
733 dev->slottime = slottime;
734 mt76x02_set_tx_ackto(dev);
737 mutex_unlock(&dev->mt76.mutex);
739 EXPORT_SYMBOL_GPL(mt76x02_bss_info_changed);
741 void mt76x02_config_mac_addr_list(struct mt76x02_dev *dev)
743 struct ieee80211_hw *hw = mt76_hw(dev);
744 struct wiphy *wiphy = hw->wiphy;
747 for (i = 0; i < ARRAY_SIZE(dev->macaddr_list); i++) {
748 u8 *addr = dev->macaddr_list[i].addr;
750 memcpy(addr, dev->mt76.macaddr, ETH_ALEN);
756 addr[0] ^= ((i - 1) << 2);
758 wiphy->addresses = dev->macaddr_list;
759 wiphy->n_addresses = ARRAY_SIZE(dev->macaddr_list);
761 EXPORT_SYMBOL_GPL(mt76x02_config_mac_addr_list);
763 MODULE_LICENSE("Dual BSD/GPL");