ieee80211_rate.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. int ieee80211_rate_control_register(struct rate_control_ops *ops)
  21. {
  22. struct rate_control_alg *alg;
  23. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  24. if (alg == NULL) {
  25. return -ENOMEM;
  26. }
  27. alg->ops = ops;
  28. mutex_lock(&rate_ctrl_mutex);
  29. list_add_tail(&alg->list, &rate_ctrl_algs);
  30. mutex_unlock(&rate_ctrl_mutex);
  31. return 0;
  32. }
  33. EXPORT_SYMBOL(ieee80211_rate_control_register);
  34. void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
  35. {
  36. struct rate_control_alg *alg;
  37. mutex_lock(&rate_ctrl_mutex);
  38. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  39. if (alg->ops == ops) {
  40. list_del(&alg->list);
  41. break;
  42. }
  43. }
  44. mutex_unlock(&rate_ctrl_mutex);
  45. kfree(alg);
  46. }
  47. EXPORT_SYMBOL(ieee80211_rate_control_unregister);
  48. static struct rate_control_ops *
  49. ieee80211_try_rate_control_ops_get(const char *name)
  50. {
  51. struct rate_control_alg *alg;
  52. struct rate_control_ops *ops = NULL;
  53. mutex_lock(&rate_ctrl_mutex);
  54. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  55. if (!name || !strcmp(alg->ops->name, name))
  56. if (try_module_get(alg->ops->module)) {
  57. ops = alg->ops;
  58. break;
  59. }
  60. }
  61. mutex_unlock(&rate_ctrl_mutex);
  62. return ops;
  63. }
  64. /* Get the rate control algorithm. If `name' is NULL, get the first
  65. * available algorithm. */
  66. static struct rate_control_ops *
  67. ieee80211_rate_control_ops_get(const char *name)
  68. {
  69. struct rate_control_ops *ops;
  70. ops = ieee80211_try_rate_control_ops_get(name);
  71. if (!ops) {
  72. request_module("rc80211_%s", name ? name : "default");
  73. ops = ieee80211_try_rate_control_ops_get(name);
  74. }
  75. return ops;
  76. }
  77. static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
  78. {
  79. module_put(ops->module);
  80. }
  81. struct rate_control_ref *rate_control_alloc(const char *name,
  82. struct ieee80211_local *local)
  83. {
  84. struct rate_control_ref *ref;
  85. ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
  86. if (!ref)
  87. goto fail_ref;
  88. kref_init(&ref->kref);
  89. ref->ops = ieee80211_rate_control_ops_get(name);
  90. if (!ref->ops)
  91. goto fail_ops;
  92. ref->priv = ref->ops->alloc(local);
  93. if (!ref->priv)
  94. goto fail_priv;
  95. return ref;
  96. fail_priv:
  97. ieee80211_rate_control_ops_put(ref->ops);
  98. fail_ops:
  99. kfree(ref);
  100. fail_ref:
  101. return NULL;
  102. }
  103. static void rate_control_release(struct kref *kref)
  104. {
  105. struct rate_control_ref *ctrl_ref;
  106. ctrl_ref = container_of(kref, struct rate_control_ref, kref);
  107. ctrl_ref->ops->free(ctrl_ref->priv);
  108. ieee80211_rate_control_ops_put(ctrl_ref->ops);
  109. kfree(ctrl_ref);
  110. }
  111. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  112. {
  113. kref_get(&ref->kref);
  114. return ref;
  115. }
  116. void rate_control_put(struct rate_control_ref *ref)
  117. {
  118. kref_put(&ref->kref, rate_control_release);
  119. }
  120. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  121. const char *name)
  122. {
  123. struct rate_control_ref *ref, *old;
  124. ASSERT_RTNL();
  125. if (local->open_count || netif_running(local->mdev))
  126. return -EBUSY;
  127. ref = rate_control_alloc(name, local);
  128. if (!ref) {
  129. printk(KERN_WARNING "%s: Failed to select rate control "
  130. "algorithm\n", wiphy_name(local->hw.wiphy));
  131. return -ENOENT;
  132. }
  133. old = local->rate_ctrl;
  134. local->rate_ctrl = ref;
  135. if (old) {
  136. rate_control_put(old);
  137. sta_info_flush(local, NULL);
  138. }
  139. printk(KERN_DEBUG "%s: Selected rate control "
  140. "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
  141. ref->ops->name);
  142. return 0;
  143. }
  144. void rate_control_deinitialize(struct ieee80211_local *local)
  145. {
  146. struct rate_control_ref *ref;
  147. ref = local->rate_ctrl;
  148. local->rate_ctrl = NULL;
  149. rate_control_put(ref);
  150. }