rt2x00config.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. memset(&erp, 0, sizeof(erp));
  70. erp.short_preamble = bss_conf->use_short_preamble;
  71. erp.ack_timeout = PLCP + get_duration(ACK_SIZE, 10);
  72. erp.ack_consume_time = SIFS + PLCP + get_duration(ACK_SIZE, 10);
  73. if (rt2x00dev->hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME)
  74. erp.ack_timeout += SHORT_DIFS;
  75. else
  76. erp.ack_timeout += DIFS;
  77. if (bss_conf->use_short_preamble) {
  78. erp.ack_timeout += SHORT_PREAMBLE;
  79. erp.ack_consume_time += SHORT_PREAMBLE;
  80. } else {
  81. erp.ack_timeout += PREAMBLE;
  82. erp.ack_consume_time += PREAMBLE;
  83. }
  84. rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
  85. }
  86. void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
  87. enum antenna rx, enum antenna tx)
  88. {
  89. struct rt2x00lib_conf libconf;
  90. libconf.ant.rx = rx;
  91. libconf.ant.tx = tx;
  92. if (rx == rt2x00dev->link.ant.active.rx &&
  93. tx == rt2x00dev->link.ant.active.tx)
  94. return;
  95. /*
  96. * Antenna setup changes require the RX to be disabled,
  97. * else the changes will be ignored by the device.
  98. */
  99. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  100. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
  101. /*
  102. * Write new antenna setup to device and reset the link tuner.
  103. * The latter is required since we need to recalibrate the
  104. * noise-sensitivity ratio for the new setup.
  105. */
  106. rt2x00dev->ops->lib->config(rt2x00dev, &libconf, CONFIG_UPDATE_ANTENNA);
  107. rt2x00lib_reset_link_tuner(rt2x00dev);
  108. rt2x00dev->link.ant.active.rx = libconf.ant.rx;
  109. rt2x00dev->link.ant.active.tx = libconf.ant.tx;
  110. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  111. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
  112. }
  113. static u32 rt2x00lib_get_basic_rates(struct ieee80211_supported_band *band)
  114. {
  115. const struct rt2x00_rate *rate;
  116. unsigned int i;
  117. u32 mask = 0;
  118. for (i = 0; i < band->n_bitrates; i++) {
  119. rate = rt2x00_get_rate(band->bitrates[i].hw_value);
  120. if (rate->flags & DEV_RATE_BASIC)
  121. mask |= rate->ratemask;
  122. }
  123. return mask;
  124. }
  125. void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
  126. struct ieee80211_conf *conf, const int force_config)
  127. {
  128. struct rt2x00lib_conf libconf;
  129. struct ieee80211_supported_band *band;
  130. struct antenna_setup *default_ant = &rt2x00dev->default_ant;
  131. struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
  132. int flags = 0;
  133. int short_slot_time;
  134. /*
  135. * In some situations we want to force all configurations
  136. * to be reloaded (When resuming for instance).
  137. */
  138. if (force_config) {
  139. flags = CONFIG_UPDATE_ALL;
  140. goto config;
  141. }
  142. /*
  143. * Check which configuration options have been
  144. * updated and should be send to the device.
  145. */
  146. if (rt2x00dev->rx_status.band != conf->channel->band)
  147. flags |= CONFIG_UPDATE_PHYMODE;
  148. if (rt2x00dev->rx_status.freq != conf->channel->center_freq)
  149. flags |= CONFIG_UPDATE_CHANNEL;
  150. if (rt2x00dev->tx_power != conf->power_level)
  151. flags |= CONFIG_UPDATE_TXPOWER;
  152. /*
  153. * Determining changes in the antenna setups request several checks:
  154. * antenna_sel_{r,t}x = 0
  155. * -> Does active_{r,t}x match default_{r,t}x
  156. * -> Is default_{r,t}x SW_DIVERSITY
  157. * antenna_sel_{r,t}x = 1/2
  158. * -> Does active_{r,t}x match antenna_sel_{r,t}x
  159. * The reason for not updating the antenna while SW diversity
  160. * should be used is simple: Software diversity means that
  161. * we should switch between the antenna's based on the
  162. * quality. This means that the current antenna is good enough
  163. * to work with untill the link tuner decides that an antenna
  164. * switch should be performed.
  165. */
  166. if (!conf->antenna_sel_rx &&
  167. default_ant->rx != ANTENNA_SW_DIVERSITY &&
  168. default_ant->rx != active_ant->rx)
  169. flags |= CONFIG_UPDATE_ANTENNA;
  170. else if (conf->antenna_sel_rx &&
  171. conf->antenna_sel_rx != active_ant->rx)
  172. flags |= CONFIG_UPDATE_ANTENNA;
  173. else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
  174. flags |= CONFIG_UPDATE_ANTENNA;
  175. if (!conf->antenna_sel_tx &&
  176. default_ant->tx != ANTENNA_SW_DIVERSITY &&
  177. default_ant->tx != active_ant->tx)
  178. flags |= CONFIG_UPDATE_ANTENNA;
  179. else if (conf->antenna_sel_tx &&
  180. conf->antenna_sel_tx != active_ant->tx)
  181. flags |= CONFIG_UPDATE_ANTENNA;
  182. else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
  183. flags |= CONFIG_UPDATE_ANTENNA;
  184. /*
  185. * The following configuration options are never
  186. * stored anywhere and will always be updated.
  187. */
  188. flags |= CONFIG_UPDATE_SLOT_TIME;
  189. flags |= CONFIG_UPDATE_BEACON_INT;
  190. /*
  191. * We have determined what options should be updated,
  192. * now precalculate device configuration values depending
  193. * on what configuration options need to be updated.
  194. */
  195. config:
  196. memset(&libconf, 0, sizeof(libconf));
  197. if (flags & CONFIG_UPDATE_PHYMODE) {
  198. band = &rt2x00dev->bands[conf->channel->band];
  199. libconf.band = conf->channel->band;
  200. libconf.basic_rates = rt2x00lib_get_basic_rates(band);
  201. }
  202. if (flags & CONFIG_UPDATE_CHANNEL) {
  203. memcpy(&libconf.rf,
  204. &rt2x00dev->spec.channels[conf->channel->hw_value],
  205. sizeof(libconf.rf));
  206. }
  207. if (flags & CONFIG_UPDATE_ANTENNA) {
  208. if (conf->antenna_sel_rx)
  209. libconf.ant.rx = conf->antenna_sel_rx;
  210. else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
  211. libconf.ant.rx = default_ant->rx;
  212. else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
  213. libconf.ant.rx = ANTENNA_B;
  214. if (conf->antenna_sel_tx)
  215. libconf.ant.tx = conf->antenna_sel_tx;
  216. else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
  217. libconf.ant.tx = default_ant->tx;
  218. else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
  219. libconf.ant.tx = ANTENNA_B;
  220. }
  221. if (flags & CONFIG_UPDATE_SLOT_TIME) {
  222. short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
  223. libconf.slot_time =
  224. short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
  225. libconf.sifs = SIFS;
  226. libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
  227. libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
  228. libconf.eifs = EIFS;
  229. }
  230. libconf.conf = conf;
  231. /*
  232. * Start configuration.
  233. */
  234. rt2x00dev->ops->lib->config(rt2x00dev, &libconf, flags);
  235. /*
  236. * Some configuration changes affect the link quality
  237. * which means we need to reset the link tuner.
  238. */
  239. if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
  240. rt2x00lib_reset_link_tuner(rt2x00dev);
  241. if (flags & CONFIG_UPDATE_PHYMODE) {
  242. rt2x00dev->curr_band = conf->channel->band;
  243. rt2x00dev->rx_status.band = conf->channel->band;
  244. }
  245. rt2x00dev->rx_status.freq = conf->channel->center_freq;
  246. rt2x00dev->tx_power = conf->power_level;
  247. if (flags & CONFIG_UPDATE_ANTENNA) {
  248. rt2x00dev->link.ant.active.rx = libconf.ant.rx;
  249. rt2x00dev->link.ant.active.tx = libconf.ant.tx;
  250. }
  251. }