ieee80211_rate.c 4.1 KB

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