rt2x00config.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  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 ieee80211_if_types type,
  28. u8 *mac, u8 *bssid)
  29. {
  30. struct rt2x00intf_conf conf;
  31. unsigned int flags = 0;
  32. conf.type = type;
  33. switch (type) {
  34. case IEEE80211_IF_TYPE_IBSS:
  35. case IEEE80211_IF_TYPE_AP:
  36. conf.sync = TSF_SYNC_BEACON;
  37. break;
  38. case IEEE80211_IF_TYPE_STA:
  39. conf.sync = TSF_SYNC_INFRA;
  40. break;
  41. default:
  42. conf.sync = TSF_SYNC_NONE;
  43. break;
  44. }
  45. /*
  46. * Note that when NULL is passed as address we will send
  47. * 00:00:00:00:00 to the device to clear the address.
  48. * This will prevent the device being confused when it wants
  49. * to ACK frames or consideres itself associated.
  50. */
  51. memset(&conf.mac, 0, sizeof(conf.mac));
  52. if (mac)
  53. memcpy(&conf.mac, mac, ETH_ALEN);
  54. memset(&conf.bssid, 0, sizeof(conf.bssid));
  55. if (bssid)
  56. memcpy(&conf.bssid, bssid, ETH_ALEN);
  57. flags |= CONFIG_UPDATE_TYPE;
  58. if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
  59. flags |= CONFIG_UPDATE_MAC;
  60. if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
  61. flags |= CONFIG_UPDATE_BSSID;
  62. rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
  63. }
  64. void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
  65. struct rt2x00_intf *intf,
  66. struct ieee80211_bss_conf *bss_conf)
  67. {
  68. struct rt2x00lib_erp erp;
  69. int retval;
  70. memset(&erp, 0, sizeof(erp));
  71. erp.short_preamble = bss_conf->use_short_preamble;
  72. erp.ack_timeout = PLCP + get_duration(ACK_SIZE, 10);
  73. erp.ack_consume_time = SIFS + PLCP + get_duration(ACK_SIZE, 10);
  74. if (rt2x00dev->hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME)
  75. erp.ack_timeout += SHORT_DIFS;
  76. else
  77. erp.ack_timeout += DIFS;
  78. if (bss_conf->use_short_preamble) {
  79. erp.ack_timeout += SHORT_PREAMBLE;
  80. erp.ack_consume_time += SHORT_PREAMBLE;
  81. } else {
  82. erp.ack_timeout += PREAMBLE;
  83. erp.ack_consume_time += PREAMBLE;
  84. }
  85. retval = rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
  86. if (retval) {
  87. spin_lock(&intf->lock);
  88. intf->delayed_flags |= DELAYED_CONFIG_ERP;
  89. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  90. spin_unlock(&intf->lock);
  91. }
  92. }
  93. void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
  94. enum antenna rx, enum antenna tx)
  95. {
  96. struct rt2x00lib_conf libconf;
  97. libconf.ant.rx = rx;
  98. libconf.ant.tx = tx;
  99. if (rx == rt2x00dev->link.ant.active.rx &&
  100. tx == rt2x00dev->link.ant.active.tx)
  101. return;
  102. /*
  103. * Antenna setup changes require the RX to be disabled,
  104. * else the changes will be ignored by the device.
  105. */
  106. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  107. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
  108. /*
  109. * Write new antenna setup to device and reset the link tuner.
  110. * The latter is required since we need to recalibrate the
  111. * noise-sensitivity ratio for the new setup.
  112. */
  113. rt2x00dev->ops->lib->config(rt2x00dev, &libconf, CONFIG_UPDATE_ANTENNA);
  114. rt2x00lib_reset_link_tuner(rt2x00dev);
  115. rt2x00dev->link.ant.active.rx = libconf.ant.rx;
  116. rt2x00dev->link.ant.active.tx = libconf.ant.tx;
  117. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  118. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
  119. }
  120. void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
  121. struct ieee80211_conf *conf, const int force_config)
  122. {
  123. struct rt2x00lib_conf libconf;
  124. struct ieee80211_supported_band *band;
  125. struct ieee80211_rate *rate;
  126. struct antenna_setup *default_ant = &rt2x00dev->default_ant;
  127. struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
  128. int flags = 0;
  129. int short_slot_time;
  130. /*
  131. * In some situations we want to force all configurations
  132. * to be reloaded (When resuming for instance).
  133. */
  134. if (force_config) {
  135. flags = CONFIG_UPDATE_ALL;
  136. goto config;
  137. }
  138. /*
  139. * Check which configuration options have been
  140. * updated and should be send to the device.
  141. */
  142. if (rt2x00dev->rx_status.band != conf->channel->band)
  143. flags |= CONFIG_UPDATE_PHYMODE;
  144. if (rt2x00dev->rx_status.freq != conf->channel->center_freq)
  145. flags |= CONFIG_UPDATE_CHANNEL;
  146. if (rt2x00dev->tx_power != conf->power_level)
  147. flags |= CONFIG_UPDATE_TXPOWER;
  148. /*
  149. * Determining changes in the antenna setups request several checks:
  150. * antenna_sel_{r,t}x = 0
  151. * -> Does active_{r,t}x match default_{r,t}x
  152. * -> Is default_{r,t}x SW_DIVERSITY
  153. * antenna_sel_{r,t}x = 1/2
  154. * -> Does active_{r,t}x match antenna_sel_{r,t}x
  155. * The reason for not updating the antenna while SW diversity
  156. * should be used is simple: Software diversity means that
  157. * we should switch between the antenna's based on the
  158. * quality. This means that the current antenna is good enough
  159. * to work with untill the link tuner decides that an antenna
  160. * switch should be performed.
  161. */
  162. if (!conf->antenna_sel_rx &&
  163. default_ant->rx != ANTENNA_SW_DIVERSITY &&
  164. default_ant->rx != active_ant->rx)
  165. flags |= CONFIG_UPDATE_ANTENNA;
  166. else if (conf->antenna_sel_rx &&
  167. conf->antenna_sel_rx != active_ant->rx)
  168. flags |= CONFIG_UPDATE_ANTENNA;
  169. else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
  170. flags |= CONFIG_UPDATE_ANTENNA;
  171. if (!conf->antenna_sel_tx &&
  172. default_ant->tx != ANTENNA_SW_DIVERSITY &&
  173. default_ant->tx != active_ant->tx)
  174. flags |= CONFIG_UPDATE_ANTENNA;
  175. else if (conf->antenna_sel_tx &&
  176. conf->antenna_sel_tx != active_ant->tx)
  177. flags |= CONFIG_UPDATE_ANTENNA;
  178. else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
  179. flags |= CONFIG_UPDATE_ANTENNA;
  180. /*
  181. * The following configuration options are never
  182. * stored anywhere and will always be updated.
  183. */
  184. flags |= CONFIG_UPDATE_SLOT_TIME;
  185. flags |= CONFIG_UPDATE_BEACON_INT;
  186. /*
  187. * We have determined what options should be updated,
  188. * now precalculate device configuration values depending
  189. * on what configuration options need to be updated.
  190. */
  191. config:
  192. memset(&libconf, 0, sizeof(libconf));
  193. if (flags & CONFIG_UPDATE_PHYMODE) {
  194. band = &rt2x00dev->bands[conf->channel->band];
  195. rate = &band->bitrates[band->n_bitrates - 1];
  196. libconf.band = conf->channel->band;
  197. libconf.basic_rates = rt2x00_get_rate(rate->hw_value)->ratemask;
  198. }
  199. if (flags & CONFIG_UPDATE_CHANNEL) {
  200. memcpy(&libconf.rf,
  201. &rt2x00dev->spec.channels[conf->channel->hw_value],
  202. sizeof(libconf.rf));
  203. }
  204. if (flags & CONFIG_UPDATE_ANTENNA) {
  205. if (conf->antenna_sel_rx)
  206. libconf.ant.rx = conf->antenna_sel_rx;
  207. else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
  208. libconf.ant.rx = default_ant->rx;
  209. else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
  210. libconf.ant.rx = ANTENNA_B;
  211. if (conf->antenna_sel_tx)
  212. libconf.ant.tx = conf->antenna_sel_tx;
  213. else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
  214. libconf.ant.tx = default_ant->tx;
  215. else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
  216. libconf.ant.tx = ANTENNA_B;
  217. }
  218. if (flags & CONFIG_UPDATE_SLOT_TIME) {
  219. short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
  220. libconf.slot_time =
  221. short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
  222. libconf.sifs = SIFS;
  223. libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
  224. libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
  225. libconf.eifs = EIFS;
  226. }
  227. libconf.conf = conf;
  228. /*
  229. * Start configuration.
  230. */
  231. rt2x00dev->ops->lib->config(rt2x00dev, &libconf, flags);
  232. /*
  233. * Some configuration changes affect the link quality
  234. * which means we need to reset the link tuner.
  235. */
  236. if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
  237. rt2x00lib_reset_link_tuner(rt2x00dev);
  238. if (flags & CONFIG_UPDATE_PHYMODE) {
  239. rt2x00dev->curr_band = conf->channel->band;
  240. rt2x00dev->rx_status.band = conf->channel->band;
  241. }
  242. rt2x00dev->rx_status.freq = conf->channel->center_freq;
  243. rt2x00dev->tx_power = conf->power_level;
  244. if (flags & CONFIG_UPDATE_ANTENNA) {
  245. rt2x00dev->link.ant.active.rx = libconf.ant.rx;
  246. rt2x00dev->link.ant.active.tx = libconf.ant.tx;
  247. }
  248. }