mt76x2_main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  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 "mt76x2.h"
  17. static int
  18. mt76x2_start(struct ieee80211_hw *hw)
  19. {
  20. struct mt76x2_dev *dev = hw->priv;
  21. int ret;
  22. mutex_lock(&dev->mutex);
  23. ret = mt76x2_mac_start(dev);
  24. if (ret)
  25. goto out;
  26. ret = mt76x2_phy_start(dev);
  27. if (ret)
  28. goto out;
  29. ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mac_work,
  30. MT_CALIBRATE_INTERVAL);
  31. set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
  32. out:
  33. mutex_unlock(&dev->mutex);
  34. return ret;
  35. }
  36. static void
  37. mt76x2_stop(struct ieee80211_hw *hw)
  38. {
  39. struct mt76x2_dev *dev = hw->priv;
  40. mutex_lock(&dev->mutex);
  41. clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
  42. mt76x2_stop_hardware(dev);
  43. mutex_unlock(&dev->mutex);
  44. }
  45. static int
  46. mt76x2_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  47. {
  48. struct mt76x2_dev *dev = hw->priv;
  49. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  50. unsigned int idx = 0;
  51. if (vif->addr[0] & BIT(1))
  52. idx = 1 + (((dev->mt76.macaddr[0] ^ vif->addr[0]) >> 2) & 7);
  53. /*
  54. * Client mode typically only has one configurable BSSID register,
  55. * which is used for bssidx=0. This is linked to the MAC address.
  56. * Since mac80211 allows changing interface types, and we cannot
  57. * force the use of the primary MAC address for a station mode
  58. * interface, we need some other way of configuring a per-interface
  59. * remote BSSID.
  60. * The hardware provides an AP-Client feature, where bssidx 0-7 are
  61. * used for AP mode and bssidx 8-15 for client mode.
  62. * We shift the station interface bss index by 8 to force the
  63. * hardware to recognize the BSSID.
  64. * The resulting bssidx mismatch for unicast frames is ignored by hw.
  65. */
  66. if (vif->type == NL80211_IFTYPE_STATION)
  67. idx += 8;
  68. mvif->idx = idx;
  69. mvif->group_wcid.idx = MT_VIF_WCID(idx);
  70. mvif->group_wcid.hw_key_idx = -1;
  71. mt76x2_txq_init(dev, vif->txq);
  72. return 0;
  73. }
  74. static int
  75. mt76x2_set_channel(struct mt76x2_dev *dev, struct cfg80211_chan_def *chandef)
  76. {
  77. int ret;
  78. cancel_delayed_work_sync(&dev->cal_work);
  79. set_bit(MT76_RESET, &dev->mt76.state);
  80. mt76_set_channel(&dev->mt76);
  81. tasklet_disable(&dev->pre_tbtt_tasklet);
  82. tasklet_disable(&dev->dfs_pd.dfs_tasklet);
  83. mt76x2_mac_stop(dev, true);
  84. ret = mt76x2_phy_set_channel(dev, chandef);
  85. /* channel cycle counters read-and-clear */
  86. mt76_rr(dev, MT_CH_IDLE);
  87. mt76_rr(dev, MT_CH_BUSY);
  88. mt76x2_dfs_init_params(dev);
  89. mt76x2_mac_resume(dev);
  90. tasklet_enable(&dev->dfs_pd.dfs_tasklet);
  91. tasklet_enable(&dev->pre_tbtt_tasklet);
  92. clear_bit(MT76_RESET, &dev->mt76.state);
  93. mt76_txq_schedule_all(&dev->mt76);
  94. return ret;
  95. }
  96. static int
  97. mt76x2_config(struct ieee80211_hw *hw, u32 changed)
  98. {
  99. struct mt76x2_dev *dev = hw->priv;
  100. int ret = 0;
  101. mutex_lock(&dev->mutex);
  102. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  103. if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
  104. dev->rxfilter |= MT_RX_FILTR_CFG_PROMISC;
  105. else
  106. dev->rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
  107. mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
  108. }
  109. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  110. dev->txpower_conf = hw->conf.power_level * 2;
  111. /* convert to per-chain power for 2x2 devices */
  112. dev->txpower_conf -= 6;
  113. if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state)) {
  114. mt76x2_phy_set_txpower(dev);
  115. mt76x2_tx_set_txpwr_auto(dev, dev->txpower_conf);
  116. }
  117. }
  118. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  119. ieee80211_stop_queues(hw);
  120. ret = mt76x2_set_channel(dev, &hw->conf.chandef);
  121. ieee80211_wake_queues(hw);
  122. }
  123. mutex_unlock(&dev->mutex);
  124. return ret;
  125. }
  126. static void
  127. mt76x2_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  128. struct ieee80211_bss_conf *info, u32 changed)
  129. {
  130. struct mt76x2_dev *dev = hw->priv;
  131. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  132. mutex_lock(&dev->mutex);
  133. if (changed & BSS_CHANGED_BSSID)
  134. mt76x2_mac_set_bssid(dev, mvif->idx, info->bssid);
  135. if (changed & BSS_CHANGED_BEACON_INT) {
  136. mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
  137. MT_BEACON_TIME_CFG_INTVAL,
  138. info->beacon_int << 4);
  139. dev->beacon_int = info->beacon_int;
  140. dev->tbtt_count = 0;
  141. }
  142. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  143. tasklet_disable(&dev->pre_tbtt_tasklet);
  144. mt76x2_mac_set_beacon_enable(dev, mvif->idx,
  145. info->enable_beacon);
  146. tasklet_enable(&dev->pre_tbtt_tasklet);
  147. }
  148. if (changed & BSS_CHANGED_ERP_SLOT) {
  149. int slottime = info->use_short_slot ? 9 : 20;
  150. dev->slottime = slottime;
  151. mt76x2_set_tx_ackto(dev);
  152. }
  153. mutex_unlock(&dev->mutex);
  154. }
  155. void
  156. mt76x2_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps)
  157. {
  158. struct mt76x2_sta *msta = (struct mt76x2_sta *) sta->drv_priv;
  159. struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_dev, mt76);
  160. int idx = msta->wcid.idx;
  161. mt76_stop_tx_queues(&dev->mt76, sta, true);
  162. mt76x2_mac_wcid_set_drop(dev, idx, ps);
  163. }
  164. static void
  165. mt76x2_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  166. const u8 *mac)
  167. {
  168. struct mt76x2_dev *dev = hw->priv;
  169. tasklet_disable(&dev->pre_tbtt_tasklet);
  170. set_bit(MT76_SCANNING, &dev->mt76.state);
  171. }
  172. static void
  173. mt76x2_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  174. {
  175. struct mt76x2_dev *dev = hw->priv;
  176. clear_bit(MT76_SCANNING, &dev->mt76.state);
  177. tasklet_enable(&dev->pre_tbtt_tasklet);
  178. }
  179. static void
  180. mt76x2_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  181. u32 queues, bool drop)
  182. {
  183. }
  184. static int
  185. mt76x2_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif, int *dbm)
  186. {
  187. struct mt76x2_dev *dev = hw->priv;
  188. *dbm = dev->txpower_cur / 2;
  189. /* convert from per-chain power to combined output on 2x2 devices */
  190. *dbm += 3;
  191. return 0;
  192. }
  193. static void mt76x2_set_coverage_class(struct ieee80211_hw *hw,
  194. s16 coverage_class)
  195. {
  196. struct mt76x2_dev *dev = hw->priv;
  197. mutex_lock(&dev->mutex);
  198. dev->coverage_class = coverage_class;
  199. mt76x2_set_tx_ackto(dev);
  200. mutex_unlock(&dev->mutex);
  201. }
  202. static int
  203. mt76x2_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
  204. {
  205. return 0;
  206. }
  207. static int mt76x2_set_antenna(struct ieee80211_hw *hw, u32 tx_ant,
  208. u32 rx_ant)
  209. {
  210. struct mt76x2_dev *dev = hw->priv;
  211. if (!tx_ant || tx_ant > 3 || tx_ant != rx_ant)
  212. return -EINVAL;
  213. mutex_lock(&dev->mutex);
  214. dev->chainmask = (tx_ant == 3) ? 0x202 : 0x101;
  215. dev->mt76.antenna_mask = tx_ant;
  216. mt76_set_stream_caps(&dev->mt76, true);
  217. mt76x2_phy_set_antenna(dev);
  218. mutex_unlock(&dev->mutex);
  219. return 0;
  220. }
  221. static int mt76x2_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
  222. u32 *rx_ant)
  223. {
  224. struct mt76x2_dev *dev = hw->priv;
  225. mutex_lock(&dev->mutex);
  226. *tx_ant = dev->mt76.antenna_mask;
  227. *rx_ant = dev->mt76.antenna_mask;
  228. mutex_unlock(&dev->mutex);
  229. return 0;
  230. }
  231. static int
  232. mt76x2_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
  233. {
  234. struct mt76x2_dev *dev = hw->priv;
  235. if (val != ~0 && val > 0xffff)
  236. return -EINVAL;
  237. mutex_lock(&dev->mutex);
  238. mt76x2_mac_set_tx_protection(dev, val);
  239. mutex_unlock(&dev->mutex);
  240. return 0;
  241. }
  242. const struct ieee80211_ops mt76x2_ops = {
  243. .tx = mt76x2_tx,
  244. .start = mt76x2_start,
  245. .stop = mt76x2_stop,
  246. .add_interface = mt76x2_add_interface,
  247. .remove_interface = mt76x2_remove_interface,
  248. .config = mt76x2_config,
  249. .configure_filter = mt76x2_configure_filter,
  250. .bss_info_changed = mt76x2_bss_info_changed,
  251. .sta_add = mt76x2_sta_add,
  252. .sta_remove = mt76x2_sta_remove,
  253. .set_key = mt76x2_set_key,
  254. .conf_tx = mt76x2_conf_tx,
  255. .sw_scan_start = mt76x2_sw_scan,
  256. .sw_scan_complete = mt76x2_sw_scan_complete,
  257. .flush = mt76x2_flush,
  258. .ampdu_action = mt76x2_ampdu_action,
  259. .get_txpower = mt76x2_get_txpower,
  260. .wake_tx_queue = mt76_wake_tx_queue,
  261. .sta_rate_tbl_update = mt76x2_sta_rate_tbl_update,
  262. .release_buffered_frames = mt76_release_buffered_frames,
  263. .set_coverage_class = mt76x2_set_coverage_class,
  264. .get_survey = mt76_get_survey,
  265. .set_tim = mt76x2_set_tim,
  266. .set_antenna = mt76x2_set_antenna,
  267. .get_antenna = mt76x2_get_antenna,
  268. .set_rts_threshold = mt76x2_set_rts_threshold,
  269. };