rt2x00config.c 8.0 KB

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