rt2x00config.c 8.9 KB

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