ieee80211_rate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/rtnetlink.h>
  12. #include "ieee80211_rate.h"
  13. #include "ieee80211_i.h"
  14. struct rate_control_alg {
  15. struct list_head list;
  16. struct rate_control_ops *ops;
  17. };
  18. static LIST_HEAD(rate_ctrl_algs);
  19. static DEFINE_MUTEX(rate_ctrl_mutex);
  20. static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
  21. module_param(ieee80211_default_rc_algo, charp, 0644);
  22. MODULE_PARM_DESC(ieee80211_default_rc_algo,
  23. "Default rate control algorithm for mac80211 to use");
  24. int ieee80211_rate_control_register(struct rate_control_ops *ops)
  25. {
  26. struct rate_control_alg *alg;
  27. if (!ops->name)
  28. return -EINVAL;
  29. mutex_lock(&rate_ctrl_mutex);
  30. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  31. if (!strcmp(alg->ops->name, ops->name)) {
  32. /* don't register an algorithm twice */
  33. WARN_ON(1);
  34. mutex_unlock(&rate_ctrl_mutex);
  35. return -EALREADY;
  36. }
  37. }
  38. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  39. if (alg == NULL) {
  40. mutex_unlock(&rate_ctrl_mutex);
  41. return -ENOMEM;
  42. }
  43. alg->ops = ops;
  44. list_add_tail(&alg->list, &rate_ctrl_algs);
  45. mutex_unlock(&rate_ctrl_mutex);
  46. return 0;
  47. }
  48. EXPORT_SYMBOL(ieee80211_rate_control_register);
  49. void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
  50. {
  51. struct rate_control_alg *alg;
  52. mutex_lock(&rate_ctrl_mutex);
  53. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  54. if (alg->ops == ops) {
  55. list_del(&alg->list);
  56. kfree(alg);
  57. break;
  58. }
  59. }
  60. mutex_unlock(&rate_ctrl_mutex);
  61. }
  62. EXPORT_SYMBOL(ieee80211_rate_control_unregister);
  63. static struct rate_control_ops *
  64. ieee80211_try_rate_control_ops_get(const char *name)
  65. {
  66. struct rate_control_alg *alg;
  67. struct rate_control_ops *ops = NULL;
  68. if (!name)
  69. return NULL;
  70. mutex_lock(&rate_ctrl_mutex);
  71. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  72. if (!strcmp(alg->ops->name, name))
  73. if (try_module_get(alg->ops->module)) {
  74. ops = alg->ops;
  75. break;
  76. }
  77. }
  78. mutex_unlock(&rate_ctrl_mutex);
  79. return ops;
  80. }
  81. /* Get the rate control algorithm. */
  82. static struct rate_control_ops *
  83. ieee80211_rate_control_ops_get(const char *name)
  84. {
  85. struct rate_control_ops *ops;
  86. const char *alg_name;
  87. if (!name)
  88. alg_name = ieee80211_default_rc_algo;
  89. else
  90. alg_name = name;
  91. ops = ieee80211_try_rate_control_ops_get(alg_name);
  92. if (!ops) {
  93. request_module("rc80211_%s", alg_name);
  94. ops = ieee80211_try_rate_control_ops_get(alg_name);
  95. }
  96. if (!ops && name)
  97. /* try default if specific alg requested but not found */
  98. ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
  99. /* try built-in one if specific alg requested but not found */
  100. if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
  101. ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
  102. return ops;
  103. }
  104. static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
  105. {
  106. module_put(ops->module);
  107. }
  108. struct rate_control_ref *rate_control_alloc(const char *name,
  109. struct ieee80211_local *local)
  110. {
  111. struct rate_control_ref *ref;
  112. ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
  113. if (!ref)
  114. goto fail_ref;
  115. kref_init(&ref->kref);
  116. ref->ops = ieee80211_rate_control_ops_get(name);
  117. if (!ref->ops)
  118. goto fail_ops;
  119. ref->priv = ref->ops->alloc(local);
  120. if (!ref->priv)
  121. goto fail_priv;
  122. return ref;
  123. fail_priv:
  124. ieee80211_rate_control_ops_put(ref->ops);
  125. fail_ops:
  126. kfree(ref);
  127. fail_ref:
  128. return NULL;
  129. }
  130. static void rate_control_release(struct kref *kref)
  131. {
  132. struct rate_control_ref *ctrl_ref;
  133. ctrl_ref = container_of(kref, struct rate_control_ref, kref);
  134. ctrl_ref->ops->free(ctrl_ref->priv);
  135. ieee80211_rate_control_ops_put(ctrl_ref->ops);
  136. kfree(ctrl_ref);
  137. }
  138. void rate_control_get_rate(struct net_device *dev,
  139. struct ieee80211_hw_mode *mode, struct sk_buff *skb,
  140. struct rate_selection *sel)
  141. {
  142. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  143. struct rate_control_ref *ref = local->rate_ctrl;
  144. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  145. struct sta_info *sta = sta_info_get(local, hdr->addr1);
  146. int i;
  147. memset(sel, 0, sizeof(struct rate_selection));
  148. ref->ops->get_rate(ref->priv, dev, mode, skb, sel);
  149. /* Select a non-ERP backup rate. */
  150. if (!sel->nonerp) {
  151. for (i = 0; i < mode->num_rates - 1; i++) {
  152. struct ieee80211_rate *rate = &mode->rates[i];
  153. if (sel->rate->rate < rate->rate)
  154. break;
  155. if (rate_supported(sta, mode, i) &&
  156. !(rate->flags & IEEE80211_RATE_ERP))
  157. sel->nonerp = rate;
  158. }
  159. }
  160. if (sta)
  161. sta_info_put(sta);
  162. }
  163. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  164. {
  165. kref_get(&ref->kref);
  166. return ref;
  167. }
  168. void rate_control_put(struct rate_control_ref *ref)
  169. {
  170. kref_put(&ref->kref, rate_control_release);
  171. }
  172. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  173. const char *name)
  174. {
  175. struct rate_control_ref *ref, *old;
  176. ASSERT_RTNL();
  177. if (local->open_count || netif_running(local->mdev))
  178. return -EBUSY;
  179. ref = rate_control_alloc(name, local);
  180. if (!ref) {
  181. printk(KERN_WARNING "%s: Failed to select rate control "
  182. "algorithm\n", wiphy_name(local->hw.wiphy));
  183. return -ENOENT;
  184. }
  185. old = local->rate_ctrl;
  186. local->rate_ctrl = ref;
  187. if (old) {
  188. rate_control_put(old);
  189. sta_info_flush(local, NULL);
  190. }
  191. printk(KERN_DEBUG "%s: Selected rate control "
  192. "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
  193. ref->ops->name);
  194. return 0;
  195. }
  196. void rate_control_deinitialize(struct ieee80211_local *local)
  197. {
  198. struct rate_control_ref *ref;
  199. ref = local->rate_ctrl;
  200. local->rate_ctrl = NULL;
  201. rate_control_put(ref);
  202. }