rt2x00config.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Copyright (C) 2004 - 2009 Ivo van Doorn <[email protected]>
  4. <http://rt2x00.serialmonkey.com>
  5. */
  6. /*
  7. Module: rt2x00lib
  8. Abstract: rt2x00 generic configuration routines.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include "rt2x00.h"
  13. #include "rt2x00lib.h"
  14. void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
  15. struct rt2x00_intf *intf,
  16. enum nl80211_iftype type,
  17. const u8 *mac, const u8 *bssid)
  18. {
  19. struct rt2x00intf_conf conf;
  20. unsigned int flags = 0;
  21. conf.type = type;
  22. switch (type) {
  23. case NL80211_IFTYPE_ADHOC:
  24. conf.sync = TSF_SYNC_ADHOC;
  25. break;
  26. case NL80211_IFTYPE_AP:
  27. case NL80211_IFTYPE_MESH_POINT:
  28. conf.sync = TSF_SYNC_AP_NONE;
  29. break;
  30. case NL80211_IFTYPE_STATION:
  31. conf.sync = TSF_SYNC_INFRA;
  32. break;
  33. default:
  34. conf.sync = TSF_SYNC_NONE;
  35. break;
  36. }
  37. /*
  38. * Note that when NULL is passed as address we will send
  39. * 00:00:00:00:00 to the device to clear the address.
  40. * This will prevent the device being confused when it wants
  41. * to ACK frames or considers itself associated.
  42. */
  43. memset(conf.mac, 0, sizeof(conf.mac));
  44. if (mac)
  45. memcpy(conf.mac, mac, ETH_ALEN);
  46. memset(conf.bssid, 0, sizeof(conf.bssid));
  47. if (bssid)
  48. memcpy(conf.bssid, bssid, ETH_ALEN);
  49. flags |= CONFIG_UPDATE_TYPE;
  50. if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
  51. flags |= CONFIG_UPDATE_MAC;
  52. if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
  53. flags |= CONFIG_UPDATE_BSSID;
  54. rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
  55. }
  56. void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
  57. struct rt2x00_intf *intf,
  58. struct ieee80211_bss_conf *bss_conf,
  59. u32 changed)
  60. {
  61. struct ieee80211_vif *vif = container_of(bss_conf, struct ieee80211_vif,
  62. bss_conf);
  63. struct rt2x00lib_erp erp;
  64. memset(&erp, 0, sizeof(erp));
  65. erp.short_preamble = bss_conf->use_short_preamble;
  66. erp.cts_protection = bss_conf->use_cts_prot;
  67. erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
  68. erp.sifs = SIFS;
  69. erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
  70. erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
  71. erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
  72. erp.basic_rates = bss_conf->basic_rates;
  73. erp.beacon_int = bss_conf->beacon_int;
  74. /* Update the AID, this is needed for dynamic PS support */
  75. rt2x00dev->aid = vif->cfg.assoc ? vif->cfg.aid : 0;
  76. rt2x00dev->last_beacon = bss_conf->sync_tsf;
  77. /* Update global beacon interval time, this is needed for PS support */
  78. rt2x00dev->beacon_int = bss_conf->beacon_int;
  79. if (changed & BSS_CHANGED_HT)
  80. erp.ht_opmode = bss_conf->ht_operation_mode;
  81. rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp, changed);
  82. }
  83. void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
  84. struct antenna_setup config)
  85. {
  86. struct link_ant *ant = &rt2x00dev->link.ant;
  87. struct antenna_setup *def = &rt2x00dev->default_ant;
  88. struct antenna_setup *active = &rt2x00dev->link.ant.active;
  89. /*
  90. * When the caller tries to send the SW diversity,
  91. * we must update the ANTENNA_RX_DIVERSITY flag to
  92. * enable the antenna diversity in the link tuner.
  93. *
  94. * Secondly, we must guarentee we never send the
  95. * software antenna diversity command to the driver.
  96. */
  97. if (!(ant->flags & ANTENNA_RX_DIVERSITY)) {
  98. if (config.rx == ANTENNA_SW_DIVERSITY) {
  99. ant->flags |= ANTENNA_RX_DIVERSITY;
  100. if (def->rx == ANTENNA_SW_DIVERSITY)
  101. config.rx = ANTENNA_B;
  102. else
  103. config.rx = def->rx;
  104. }
  105. } else if (config.rx == ANTENNA_SW_DIVERSITY)
  106. config.rx = active->rx;
  107. if (!(ant->flags & ANTENNA_TX_DIVERSITY)) {
  108. if (config.tx == ANTENNA_SW_DIVERSITY) {
  109. ant->flags |= ANTENNA_TX_DIVERSITY;
  110. if (def->tx == ANTENNA_SW_DIVERSITY)
  111. config.tx = ANTENNA_B;
  112. else
  113. config.tx = def->tx;
  114. }
  115. } else if (config.tx == ANTENNA_SW_DIVERSITY)
  116. config.tx = active->tx;
  117. /*
  118. * Antenna setup changes require the RX to be disabled,
  119. * else the changes will be ignored by the device.
  120. */
  121. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  122. rt2x00queue_stop_queue(rt2x00dev->rx);
  123. /*
  124. * Write new antenna setup to device and reset the link tuner.
  125. * The latter is required since we need to recalibrate the
  126. * noise-sensitivity ratio for the new setup.
  127. */
  128. rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
  129. rt2x00link_reset_tuner(rt2x00dev, true);
  130. memcpy(active, &config, sizeof(config));
  131. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  132. rt2x00queue_start_queue(rt2x00dev->rx);
  133. }
  134. static u16 rt2x00ht_center_channel(struct rt2x00_dev *rt2x00dev,
  135. struct ieee80211_conf *conf)
  136. {
  137. struct hw_mode_spec *spec = &rt2x00dev->spec;
  138. int center_channel;
  139. u16 i;
  140. /*
  141. * Initialize center channel to current channel.
  142. */
  143. center_channel = spec->channels[conf->chandef.chan->hw_value].channel;
  144. /*
  145. * Adjust center channel to HT40+ and HT40- operation.
  146. */
  147. if (conf_is_ht40_plus(conf))
  148. center_channel += 2;
  149. else if (conf_is_ht40_minus(conf))
  150. center_channel -= (center_channel == 14) ? 1 : 2;
  151. for (i = 0; i < spec->num_channels; i++)
  152. if (spec->channels[i].channel == center_channel)
  153. return i;
  154. WARN_ON(1);
  155. return conf->chandef.chan->hw_value;
  156. }
  157. void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
  158. struct ieee80211_conf *conf,
  159. unsigned int ieee80211_flags)
  160. {
  161. struct rt2x00lib_conf libconf;
  162. u16 hw_value;
  163. u16 autowake_timeout;
  164. u16 beacon_int;
  165. u16 beacon_diff;
  166. memset(&libconf, 0, sizeof(libconf));
  167. libconf.conf = conf;
  168. if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
  169. if (!conf_is_ht(conf))
  170. set_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
  171. else
  172. clear_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
  173. if (conf_is_ht40(conf)) {
  174. set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
  175. hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
  176. } else {
  177. clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
  178. hw_value = conf->chandef.chan->hw_value;
  179. }
  180. memcpy(&libconf.rf,
  181. &rt2x00dev->spec.channels[hw_value],
  182. sizeof(libconf.rf));
  183. memcpy(&libconf.channel,
  184. &rt2x00dev->spec.channels_info[hw_value],
  185. sizeof(libconf.channel));
  186. /* Used for VCO periodic calibration */
  187. rt2x00dev->rf_channel = libconf.rf.channel;
  188. }
  189. if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
  190. (ieee80211_flags & IEEE80211_CONF_CHANGE_PS))
  191. cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
  192. /*
  193. * Start configuration.
  194. */
  195. rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
  196. if (conf->flags & IEEE80211_CONF_PS)
  197. set_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
  198. else
  199. clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
  200. if (conf->flags & IEEE80211_CONF_MONITOR)
  201. set_bit(CONFIG_MONITORING, &rt2x00dev->flags);
  202. else
  203. clear_bit(CONFIG_MONITORING, &rt2x00dev->flags);
  204. rt2x00dev->curr_band = conf->chandef.chan->band;
  205. rt2x00dev->curr_freq = conf->chandef.chan->center_freq;
  206. rt2x00dev->tx_power = conf->power_level;
  207. rt2x00dev->short_retry = conf->short_frame_max_tx_count;
  208. rt2x00dev->long_retry = conf->long_frame_max_tx_count;
  209. /*
  210. * Some configuration changes affect the link quality
  211. * which means we need to reset the link tuner.
  212. */
  213. if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
  214. rt2x00link_reset_tuner(rt2x00dev, false);
  215. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
  216. rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
  217. (ieee80211_flags & IEEE80211_CONF_CHANGE_PS) &&
  218. (conf->flags & IEEE80211_CONF_PS)) {
  219. beacon_diff = (long)jiffies - (long)rt2x00dev->last_beacon;
  220. beacon_int = msecs_to_jiffies(rt2x00dev->beacon_int);
  221. if (beacon_diff > beacon_int)
  222. beacon_diff = 0;
  223. autowake_timeout = (conf->ps_dtim_period * beacon_int) - beacon_diff;
  224. queue_delayed_work(rt2x00dev->workqueue,
  225. &rt2x00dev->autowakeup_work,
  226. autowake_timeout - 15);
  227. }
  228. }