rt2x00config.c 8.9 KB

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