mt76x2u_main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "mt76x2u.h"
  17. static int mt76x2u_start(struct ieee80211_hw *hw)
  18. {
  19. struct mt76x2_dev *dev = hw->priv;
  20. int ret;
  21. mutex_lock(&dev->mutex);
  22. ret = mt76x2u_mac_start(dev);
  23. if (ret)
  24. goto out;
  25. set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
  26. out:
  27. mutex_unlock(&dev->mutex);
  28. return ret;
  29. }
  30. static void mt76x2u_stop(struct ieee80211_hw *hw)
  31. {
  32. struct mt76x2_dev *dev = hw->priv;
  33. mutex_lock(&dev->mutex);
  34. clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
  35. mt76x2u_stop_hw(dev);
  36. mutex_unlock(&dev->mutex);
  37. }
  38. static int mt76x2u_add_interface(struct ieee80211_hw *hw,
  39. struct ieee80211_vif *vif)
  40. {
  41. struct mt76x2_dev *dev = hw->priv;
  42. struct mt76x2_vif *mvif = (struct mt76x2_vif *)vif->drv_priv;
  43. unsigned int idx = 0;
  44. if (!ether_addr_equal(dev->mt76.macaddr, vif->addr))
  45. mt76x2u_mac_setaddr(dev, vif->addr);
  46. mvif->idx = idx;
  47. mvif->group_wcid.idx = MT_VIF_WCID(idx);
  48. mvif->group_wcid.hw_key_idx = -1;
  49. mt76x2_txq_init(dev, vif->txq);
  50. return 0;
  51. }
  52. static int
  53. mt76x2u_set_channel(struct mt76x2_dev *dev,
  54. struct cfg80211_chan_def *chandef)
  55. {
  56. int err;
  57. cancel_delayed_work_sync(&dev->cal_work);
  58. set_bit(MT76_RESET, &dev->mt76.state);
  59. mt76_set_channel(&dev->mt76);
  60. mt76_clear(dev, MT_TXOP_CTRL_CFG, BIT(20));
  61. mt76_clear(dev, MT_TXOP_HLDR_ET, BIT(1));
  62. mt76x2_mac_stop(dev, false);
  63. err = mt76x2u_phy_set_channel(dev, chandef);
  64. mt76x2u_mac_resume(dev);
  65. clear_bit(MT76_RESET, &dev->mt76.state);
  66. mt76_txq_schedule_all(&dev->mt76);
  67. return err;
  68. }
  69. static void
  70. mt76x2u_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  71. struct ieee80211_bss_conf *info, u32 changed)
  72. {
  73. struct mt76x2_dev *dev = hw->priv;
  74. mutex_lock(&dev->mutex);
  75. if (changed & BSS_CHANGED_ASSOC) {
  76. mt76x2u_phy_channel_calibrate(dev);
  77. mt76x2_apply_gain_adj(dev);
  78. }
  79. if (changed & BSS_CHANGED_BSSID) {
  80. mt76_wr(dev, MT_MAC_BSSID_DW0,
  81. get_unaligned_le32(info->bssid));
  82. mt76_wr(dev, MT_MAC_BSSID_DW1,
  83. get_unaligned_le16(info->bssid + 4));
  84. }
  85. mutex_unlock(&dev->mutex);
  86. }
  87. static int
  88. mt76x2u_config(struct ieee80211_hw *hw, u32 changed)
  89. {
  90. struct mt76x2_dev *dev = hw->priv;
  91. int err = 0;
  92. mutex_lock(&dev->mutex);
  93. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  94. if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
  95. dev->rxfilter |= MT_RX_FILTR_CFG_PROMISC;
  96. else
  97. dev->rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
  98. mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
  99. }
  100. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  101. ieee80211_stop_queues(hw);
  102. err = mt76x2u_set_channel(dev, &hw->conf.chandef);
  103. ieee80211_wake_queues(hw);
  104. }
  105. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  106. dev->txpower_conf = hw->conf.power_level * 2;
  107. /* convert to per-chain power for 2x2 devices */
  108. dev->txpower_conf -= 6;
  109. if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state))
  110. mt76x2_phy_set_txpower(dev);
  111. }
  112. mutex_unlock(&dev->mutex);
  113. return err;
  114. }
  115. static void
  116. mt76x2u_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  117. const u8 *mac)
  118. {
  119. struct mt76x2_dev *dev = hw->priv;
  120. set_bit(MT76_SCANNING, &dev->mt76.state);
  121. }
  122. static void
  123. mt76x2u_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  124. {
  125. struct mt76x2_dev *dev = hw->priv;
  126. clear_bit(MT76_SCANNING, &dev->mt76.state);
  127. }
  128. const struct ieee80211_ops mt76x2u_ops = {
  129. .tx = mt76x2_tx,
  130. .start = mt76x2u_start,
  131. .stop = mt76x2u_stop,
  132. .add_interface = mt76x2u_add_interface,
  133. .remove_interface = mt76x2_remove_interface,
  134. .sta_add = mt76x2_sta_add,
  135. .sta_remove = mt76x2_sta_remove,
  136. .set_key = mt76x2_set_key,
  137. .ampdu_action = mt76x2_ampdu_action,
  138. .config = mt76x2u_config,
  139. .wake_tx_queue = mt76_wake_tx_queue,
  140. .bss_info_changed = mt76x2u_bss_info_changed,
  141. .configure_filter = mt76x2_configure_filter,
  142. .conf_tx = mt76x2_conf_tx,
  143. .sw_scan_start = mt76x2u_sw_scan,
  144. .sw_scan_complete = mt76x2u_sw_scan_complete,
  145. .sta_rate_tbl_update = mt76x2_sta_rate_tbl_update,
  146. };