rt2x00config.c 8.1 KB

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