rt2x00config.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Copyright (C) 2004 - 2007 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. /*
  26. * The MAC and BSSID addressess are simple array of bytes,
  27. * these arrays are little endian, so when sending the addressess
  28. * to the drivers, copy the it into a endian-signed variable.
  29. *
  30. * Note that all devices (except rt2500usb) have 32 bits
  31. * register word sizes. This means that whatever variable we
  32. * pass _must_ be a multiple of 32 bits. Otherwise the device
  33. * might not accept what we are sending to it.
  34. * This will also make it easier for the driver to write
  35. * the data to the device.
  36. *
  37. * Also note that when NULL is passed as address the
  38. * we will send 00:00:00:00:00 to the device to clear the address.
  39. * This will prevent the device being confused when it wants
  40. * to ACK frames or consideres itself associated.
  41. */
  42. void rt2x00lib_config_mac_addr(struct rt2x00_dev *rt2x00dev, u8 *mac)
  43. {
  44. __le32 reg[2];
  45. memset(&reg, 0, sizeof(reg));
  46. if (mac)
  47. memcpy(&reg, mac, ETH_ALEN);
  48. rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, &reg[0]);
  49. }
  50. void rt2x00lib_config_bssid(struct rt2x00_dev *rt2x00dev, u8 *bssid)
  51. {
  52. __le32 reg[2];
  53. memset(&reg, 0, sizeof(reg));
  54. if (bssid)
  55. memcpy(&reg, bssid, ETH_ALEN);
  56. rt2x00dev->ops->lib->config_bssid(rt2x00dev, &reg[0]);
  57. }
  58. void rt2x00lib_config_type(struct rt2x00_dev *rt2x00dev, const int type)
  59. {
  60. int tsf_sync;
  61. switch (type) {
  62. case IEEE80211_IF_TYPE_IBSS:
  63. case IEEE80211_IF_TYPE_AP:
  64. tsf_sync = TSF_SYNC_BEACON;
  65. break;
  66. case IEEE80211_IF_TYPE_STA:
  67. tsf_sync = TSF_SYNC_INFRA;
  68. break;
  69. default:
  70. tsf_sync = TSF_SYNC_NONE;
  71. break;
  72. }
  73. rt2x00dev->ops->lib->config_type(rt2x00dev, type, tsf_sync);
  74. }
  75. void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
  76. enum antenna rx, enum antenna tx)
  77. {
  78. struct rt2x00lib_conf libconf;
  79. libconf.ant.rx = rx;
  80. libconf.ant.tx = tx;
  81. if (rx == rt2x00dev->link.ant.active.rx &&
  82. tx == rt2x00dev->link.ant.active.tx)
  83. return;
  84. /*
  85. * Antenna setup changes require the RX to be disabled,
  86. * else the changes will be ignored by the device.
  87. */
  88. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  89. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
  90. /*
  91. * Write new antenna setup to device and reset the link tuner.
  92. * The latter is required since we need to recalibrate the
  93. * noise-sensitivity ratio for the new setup.
  94. */
  95. rt2x00dev->ops->lib->config(rt2x00dev, CONFIG_UPDATE_ANTENNA, &libconf);
  96. rt2x00lib_reset_link_tuner(rt2x00dev);
  97. rt2x00dev->link.ant.active.rx = libconf.ant.rx;
  98. rt2x00dev->link.ant.active.tx = libconf.ant.tx;
  99. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  100. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
  101. }
  102. void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
  103. struct ieee80211_conf *conf, const int force_config)
  104. {
  105. struct rt2x00lib_conf libconf;
  106. struct ieee80211_hw_mode *mode;
  107. struct ieee80211_rate *rate;
  108. struct antenna_setup *default_ant = &rt2x00dev->default_ant;
  109. struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
  110. int flags = 0;
  111. int short_slot_time;
  112. /*
  113. * In some situations we want to force all configurations
  114. * to be reloaded (When resuming for instance).
  115. */
  116. if (force_config) {
  117. flags = CONFIG_UPDATE_ALL;
  118. goto config;
  119. }
  120. /*
  121. * Check which configuration options have been
  122. * updated and should be send to the device.
  123. */
  124. if (rt2x00dev->rx_status.phymode != conf->phymode)
  125. flags |= CONFIG_UPDATE_PHYMODE;
  126. if (rt2x00dev->rx_status.channel != conf->channel)
  127. flags |= CONFIG_UPDATE_CHANNEL;
  128. if (rt2x00dev->tx_power != conf->power_level)
  129. flags |= CONFIG_UPDATE_TXPOWER;
  130. /*
  131. * Determining changes in the antenna setups request several checks:
  132. * antenna_sel_{r,t}x = 0
  133. * -> Does active_{r,t}x match default_{r,t}x
  134. * -> Is default_{r,t}x SW_DIVERSITY
  135. * antenna_sel_{r,t}x = 1/2
  136. * -> Does active_{r,t}x match antenna_sel_{r,t}x
  137. * The reason for not updating the antenna while SW diversity
  138. * should be used is simple: Software diversity means that
  139. * we should switch between the antenna's based on the
  140. * quality. This means that the current antenna is good enough
  141. * to work with untill the link tuner decides that an antenna
  142. * switch should be performed.
  143. */
  144. if (!conf->antenna_sel_rx &&
  145. default_ant->rx != ANTENNA_SW_DIVERSITY &&
  146. default_ant->rx != active_ant->rx)
  147. flags |= CONFIG_UPDATE_ANTENNA;
  148. else if (conf->antenna_sel_rx &&
  149. conf->antenna_sel_rx != active_ant->rx)
  150. flags |= CONFIG_UPDATE_ANTENNA;
  151. else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
  152. flags |= CONFIG_UPDATE_ANTENNA;
  153. if (!conf->antenna_sel_tx &&
  154. default_ant->tx != ANTENNA_SW_DIVERSITY &&
  155. default_ant->tx != active_ant->tx)
  156. flags |= CONFIG_UPDATE_ANTENNA;
  157. else if (conf->antenna_sel_tx &&
  158. conf->antenna_sel_tx != active_ant->tx)
  159. flags |= CONFIG_UPDATE_ANTENNA;
  160. else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
  161. flags |= CONFIG_UPDATE_ANTENNA;
  162. /*
  163. * The following configuration options are never
  164. * stored anywhere and will always be updated.
  165. */
  166. flags |= CONFIG_UPDATE_SLOT_TIME;
  167. flags |= CONFIG_UPDATE_BEACON_INT;
  168. /*
  169. * We have determined what options should be updated,
  170. * now precalculate device configuration values depending
  171. * on what configuration options need to be updated.
  172. */
  173. config:
  174. memset(&libconf, 0, sizeof(libconf));
  175. if (flags & CONFIG_UPDATE_PHYMODE) {
  176. switch (conf->phymode) {
  177. case MODE_IEEE80211A:
  178. libconf.phymode = HWMODE_A;
  179. break;
  180. case MODE_IEEE80211B:
  181. libconf.phymode = HWMODE_B;
  182. break;
  183. case MODE_IEEE80211G:
  184. libconf.phymode = HWMODE_G;
  185. break;
  186. default:
  187. ERROR(rt2x00dev,
  188. "Attempt to configure unsupported mode (%d)"
  189. "Defaulting to 802.11b", conf->phymode);
  190. libconf.phymode = HWMODE_B;
  191. }
  192. mode = &rt2x00dev->hwmodes[libconf.phymode];
  193. rate = &mode->rates[mode->num_rates - 1];
  194. libconf.basic_rates =
  195. DEVICE_GET_RATE_FIELD(rate->val, RATEMASK) & DEV_BASIC_RATEMASK;
  196. }
  197. if (flags & CONFIG_UPDATE_CHANNEL) {
  198. memcpy(&libconf.rf,
  199. &rt2x00dev->spec.channels[conf->channel_val],
  200. sizeof(libconf.rf));
  201. }
  202. if (flags & CONFIG_UPDATE_ANTENNA) {
  203. if (conf->antenna_sel_rx)
  204. libconf.ant.rx = conf->antenna_sel_rx;
  205. else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
  206. libconf.ant.rx = default_ant->rx;
  207. else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
  208. libconf.ant.rx = ANTENNA_B;
  209. if (conf->antenna_sel_tx)
  210. libconf.ant.tx = conf->antenna_sel_tx;
  211. else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
  212. libconf.ant.tx = default_ant->tx;
  213. else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
  214. libconf.ant.tx = ANTENNA_B;
  215. }
  216. if (flags & CONFIG_UPDATE_SLOT_TIME) {
  217. short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
  218. libconf.slot_time =
  219. short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
  220. libconf.sifs = SIFS;
  221. libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
  222. libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
  223. libconf.eifs = EIFS;
  224. }
  225. libconf.conf = conf;
  226. /*
  227. * Start configuration.
  228. */
  229. rt2x00dev->ops->lib->config(rt2x00dev, flags, &libconf);
  230. /*
  231. * Some configuration changes affect the link quality
  232. * which means we need to reset the link tuner.
  233. */
  234. if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
  235. rt2x00lib_reset_link_tuner(rt2x00dev);
  236. if (flags & CONFIG_UPDATE_PHYMODE) {
  237. rt2x00dev->curr_hwmode = libconf.phymode;
  238. rt2x00dev->rx_status.phymode = conf->phymode;
  239. }
  240. rt2x00dev->rx_status.freq = conf->freq;
  241. rt2x00dev->rx_status.channel = conf->channel;
  242. rt2x00dev->tx_power = conf->power_level;
  243. if (flags & CONFIG_UPDATE_ANTENNA) {
  244. rt2x00dev->link.ant.active.rx = libconf.ant.rx;
  245. rt2x00dev->link.ant.active.tx = libconf.ant.tx;
  246. }
  247. }