rt2x00config.c 8.3 KB

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