rt2x00config.c 8.6 KB

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