2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
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.
11 #include <linux/kernel.h>
12 #include <linux/rtnetlink.h>
13 #include "ieee80211_rate.h"
14 #include "ieee80211_i.h"
16 struct rate_control_alg {
17 struct list_head list;
18 struct rate_control_ops *ops;
21 static LIST_HEAD(rate_ctrl_algs);
22 static DEFINE_MUTEX(rate_ctrl_mutex);
24 int ieee80211_rate_control_register(struct rate_control_ops *ops)
26 struct rate_control_alg *alg;
31 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
37 mutex_lock(&rate_ctrl_mutex);
38 list_add_tail(&alg->list, &rate_ctrl_algs);
39 mutex_unlock(&rate_ctrl_mutex);
43 EXPORT_SYMBOL(ieee80211_rate_control_register);
45 void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
47 struct rate_control_alg *alg;
49 mutex_lock(&rate_ctrl_mutex);
50 list_for_each_entry(alg, &rate_ctrl_algs, list) {
51 if (alg->ops == ops) {
56 mutex_unlock(&rate_ctrl_mutex);
59 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
61 static struct rate_control_ops *
62 ieee80211_try_rate_control_ops_get(const char *name)
64 struct rate_control_alg *alg;
65 struct rate_control_ops *ops = 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)) {
78 mutex_unlock(&rate_ctrl_mutex);
82 /* Get the rate control algorithm. If `name' is NULL, get the first
83 * available algorithm. */
84 static struct rate_control_ops *
85 ieee80211_rate_control_ops_get(const char *name)
87 struct rate_control_ops *ops;
92 ops = ieee80211_try_rate_control_ops_get(name);
94 request_module("rc80211_%s", name);
95 ops = ieee80211_try_rate_control_ops_get(name);
100 static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
102 module_put(ops->module);
105 struct rate_control_ref *rate_control_alloc(const char *name,
106 struct ieee80211_local *local)
108 struct rate_control_ref *ref;
110 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
113 kref_init(&ref->kref);
114 ref->ops = ieee80211_rate_control_ops_get(name);
117 ref->priv = ref->ops->alloc(local);
123 ieee80211_rate_control_ops_put(ref->ops);
130 static void rate_control_release(struct kref *kref)
132 struct rate_control_ref *ctrl_ref;
134 ctrl_ref = container_of(kref, struct rate_control_ref, kref);
135 ctrl_ref->ops->free(ctrl_ref->priv);
136 ieee80211_rate_control_ops_put(ctrl_ref->ops);
140 struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
142 kref_get(&ref->kref);
146 void rate_control_put(struct rate_control_ref *ref)
148 kref_put(&ref->kref, rate_control_release);
151 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
154 struct rate_control_ref *ref, *old;
157 if (local->open_count || netif_running(local->mdev))
160 ref = rate_control_alloc(name, local);
162 printk(KERN_WARNING "%s: Failed to select rate control "
163 "algorithm\n", wiphy_name(local->hw.wiphy));
167 old = local->rate_ctrl;
168 local->rate_ctrl = ref;
170 rate_control_put(old);
171 sta_info_flush(local, NULL);
174 printk(KERN_DEBUG "%s: Selected rate control "
175 "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
182 void rate_control_deinitialize(struct ieee80211_local *local)
184 struct rate_control_ref *ref;
186 ref = local->rate_ctrl;
187 local->rate_ctrl = NULL;
188 rate_control_put(ref);