rt2x00config.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright (C) 2004 - 2009 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. const u8 *mac, const 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. case NL80211_IFTYPE_MESH_POINT:
  37. case NL80211_IFTYPE_WDS:
  38. conf.sync = TSF_SYNC_BEACON;
  39. break;
  40. case NL80211_IFTYPE_STATION:
  41. conf.sync = TSF_SYNC_INFRA;
  42. break;
  43. default:
  44. conf.sync = TSF_SYNC_NONE;
  45. break;
  46. }
  47. /*
  48. * Note that when NULL is passed as address we will send
  49. * 00:00:00:00:00 to the device to clear the address.
  50. * This will prevent the device being confused when it wants
  51. * to ACK frames or consideres itself associated.
  52. */
  53. memset(&conf.mac, 0, sizeof(conf.mac));
  54. if (mac)
  55. memcpy(&conf.mac, mac, ETH_ALEN);
  56. memset(&conf.bssid, 0, sizeof(conf.bssid));
  57. if (bssid)
  58. memcpy(&conf.bssid, bssid, ETH_ALEN);
  59. flags |= CONFIG_UPDATE_TYPE;
  60. if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
  61. flags |= CONFIG_UPDATE_MAC;
  62. if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
  63. flags |= CONFIG_UPDATE_BSSID;
  64. rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
  65. }
  66. void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
  67. struct rt2x00_intf *intf,
  68. struct ieee80211_bss_conf *bss_conf)
  69. {
  70. struct rt2x00lib_erp erp;
  71. memset(&erp, 0, sizeof(erp));
  72. erp.short_preamble = bss_conf->use_short_preamble;
  73. erp.cts_protection = bss_conf->use_cts_prot;
  74. erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
  75. erp.sifs = SIFS;
  76. erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
  77. erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
  78. erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
  79. erp.basic_rates = bss_conf->basic_rates;
  80. erp.beacon_int = bss_conf->beacon_int;
  81. /* Update global beacon interval time, this is needed for PS support */
  82. rt2x00dev->beacon_int = bss_conf->beacon_int;
  83. rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
  84. }
  85. static inline
  86. enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant,
  87. enum antenna default_ant)
  88. {
  89. if (current_ant != ANTENNA_SW_DIVERSITY)
  90. return current_ant;
  91. return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B;
  92. }
  93. void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
  94. struct antenna_setup config)
  95. {
  96. struct link_ant *ant = &rt2x00dev->link.ant;
  97. struct antenna_setup *def = &rt2x00dev->default_ant;
  98. struct antenna_setup *active = &rt2x00dev->link.ant.active;
  99. /*
  100. * Failsafe: Make sure we are not sending the
  101. * ANTENNA_SW_DIVERSITY state to the driver.
  102. * If that happens, fallback to hardware defaults,
  103. * or our own default.
  104. * If diversity handling is active for a particular antenna,
  105. * we shouldn't overwrite that antenna.
  106. * The calls to rt2x00lib_config_antenna_check()
  107. * might have caused that we restore back to the already
  108. * active setting. If that has happened we can quit.
  109. */
  110. if (!(ant->flags & ANTENNA_RX_DIVERSITY))
  111. config.rx = rt2x00lib_config_antenna_check(config.rx, def->rx);
  112. else
  113. config.rx = active->rx;
  114. if (!(ant->flags & ANTENNA_TX_DIVERSITY))
  115. config.tx = rt2x00lib_config_antenna_check(config.tx, def->tx);
  116. else
  117. config.tx = active->tx;
  118. if (config.rx == active->rx && config.tx == active->tx)
  119. return;
  120. /*
  121. * Antenna setup changes require the RX to be disabled,
  122. * else the changes will be ignored by the device.
  123. */
  124. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  125. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
  126. /*
  127. * Write new antenna setup to device and reset the link tuner.
  128. * The latter is required since we need to recalibrate the
  129. * noise-sensitivity ratio for the new setup.
  130. */
  131. rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
  132. rt2x00link_reset_tuner(rt2x00dev, true);
  133. memcpy(active, &config, sizeof(config));
  134. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  135. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
  136. }
  137. void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
  138. struct ieee80211_conf *conf,
  139. unsigned int ieee80211_flags)
  140. {
  141. struct rt2x00lib_conf libconf;
  142. memset(&libconf, 0, sizeof(libconf));
  143. libconf.conf = conf;
  144. if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
  145. if (conf_is_ht40(conf))
  146. __set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
  147. else
  148. __clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
  149. memcpy(&libconf.rf,
  150. &rt2x00dev->spec.channels[conf->channel->hw_value],
  151. sizeof(libconf.rf));
  152. memcpy(&libconf.channel,
  153. &rt2x00dev->spec.channels_info[conf->channel->hw_value],
  154. sizeof(libconf.channel));
  155. }
  156. /*
  157. * Start configuration.
  158. */
  159. rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
  160. /*
  161. * Some configuration changes affect the link quality
  162. * which means we need to reset the link tuner.
  163. */
  164. if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
  165. rt2x00link_reset_tuner(rt2x00dev, false);
  166. rt2x00dev->curr_band = conf->channel->band;
  167. rt2x00dev->tx_power = conf->power_level;
  168. rt2x00dev->short_retry = conf->short_frame_max_tx_count;
  169. rt2x00dev->long_retry = conf->long_frame_max_tx_count;
  170. rt2x00dev->rx_status.band = conf->channel->band;
  171. rt2x00dev->rx_status.freq = conf->channel->center_freq;
  172. }