ieee80211_rate.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "ieee80211_rate.h"
  12. #include "ieee80211_i.h"
  13. struct rate_control_alg {
  14. struct list_head list;
  15. struct rate_control_ops *ops;
  16. };
  17. static LIST_HEAD(rate_ctrl_algs);
  18. static DEFINE_MUTEX(rate_ctrl_mutex);
  19. int ieee80211_rate_control_register(struct rate_control_ops *ops)
  20. {
  21. struct rate_control_alg *alg;
  22. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  23. if (alg == NULL) {
  24. return -ENOMEM;
  25. }
  26. alg->ops = ops;
  27. mutex_lock(&rate_ctrl_mutex);
  28. list_add_tail(&alg->list, &rate_ctrl_algs);
  29. mutex_unlock(&rate_ctrl_mutex);
  30. return 0;
  31. }
  32. EXPORT_SYMBOL(ieee80211_rate_control_register);
  33. void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
  34. {
  35. struct rate_control_alg *alg;
  36. mutex_lock(&rate_ctrl_mutex);
  37. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  38. if (alg->ops == ops) {
  39. list_del(&alg->list);
  40. break;
  41. }
  42. }
  43. mutex_unlock(&rate_ctrl_mutex);
  44. kfree(alg);
  45. }
  46. EXPORT_SYMBOL(ieee80211_rate_control_unregister);
  47. static struct rate_control_ops *
  48. ieee80211_try_rate_control_ops_get(const char *name)
  49. {
  50. struct rate_control_alg *alg;
  51. struct rate_control_ops *ops = NULL;
  52. mutex_lock(&rate_ctrl_mutex);
  53. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  54. if (!name || !strcmp(alg->ops->name, name))
  55. if (try_module_get(alg->ops->module)) {
  56. ops = alg->ops;
  57. break;
  58. }
  59. }
  60. mutex_unlock(&rate_ctrl_mutex);
  61. return ops;
  62. }
  63. /* Get the rate control algorithm. If `name' is NULL, get the first
  64. * available algorithm. */
  65. static struct rate_control_ops *
  66. ieee80211_rate_control_ops_get(const char *name)
  67. {
  68. struct rate_control_ops *ops;
  69. ops = ieee80211_try_rate_control_ops_get(name);
  70. if (!ops) {
  71. request_module("rc80211_%s", name ? name : "default");
  72. ops = ieee80211_try_rate_control_ops_get(name);
  73. }
  74. return ops;
  75. }
  76. static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
  77. {
  78. module_put(ops->module);
  79. }
  80. struct rate_control_ref *rate_control_alloc(const char *name,
  81. struct ieee80211_local *local)
  82. {
  83. struct rate_control_ref *ref;
  84. ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
  85. if (!ref)
  86. goto fail_ref;
  87. kref_init(&ref->kref);
  88. ref->ops = ieee80211_rate_control_ops_get(name);
  89. if (!ref->ops)
  90. goto fail_ops;
  91. ref->priv = ref->ops->alloc(local);
  92. if (!ref->priv)
  93. goto fail_priv;
  94. return ref;
  95. fail_priv:
  96. ieee80211_rate_control_ops_put(ref->ops);
  97. fail_ops:
  98. kfree(ref);
  99. fail_ref:
  100. return NULL;
  101. }
  102. static void rate_control_release(struct kref *kref)
  103. {
  104. struct rate_control_ref *ctrl_ref;
  105. ctrl_ref = container_of(kref, struct rate_control_ref, kref);
  106. ctrl_ref->ops->free(ctrl_ref->priv);
  107. ieee80211_rate_control_ops_put(ctrl_ref->ops);
  108. kfree(ctrl_ref);
  109. }
  110. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  111. {
  112. kref_get(&ref->kref);
  113. return ref;
  114. }
  115. void rate_control_put(struct rate_control_ref *ref)
  116. {
  117. kref_put(&ref->kref, rate_control_release);
  118. }