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>
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 static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
25 module_param(ieee80211_default_rc_algo, charp, 0644);
26 MODULE_PARM_DESC(ieee80211_default_rc_algo,
27 "Default rate control algorithm for mac80211 to use");
29 int ieee80211_rate_control_register(struct rate_control_ops *ops)
31 struct rate_control_alg *alg;
36 mutex_lock(&rate_ctrl_mutex);
37 list_for_each_entry(alg, &rate_ctrl_algs, list) {
38 if (!strcmp(alg->ops->name, ops->name)) {
39 /* don't register an algorithm twice */
41 mutex_unlock(&rate_ctrl_mutex);
46 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
48 mutex_unlock(&rate_ctrl_mutex);
53 list_add_tail(&alg->list, &rate_ctrl_algs);
54 mutex_unlock(&rate_ctrl_mutex);
58 EXPORT_SYMBOL(ieee80211_rate_control_register);
60 void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
62 struct rate_control_alg *alg;
64 mutex_lock(&rate_ctrl_mutex);
65 list_for_each_entry(alg, &rate_ctrl_algs, list) {
66 if (alg->ops == ops) {
72 mutex_unlock(&rate_ctrl_mutex);
74 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
76 static struct rate_control_ops *
77 ieee80211_try_rate_control_ops_get(const char *name)
79 struct rate_control_alg *alg;
80 struct rate_control_ops *ops = NULL;
85 mutex_lock(&rate_ctrl_mutex);
86 list_for_each_entry(alg, &rate_ctrl_algs, list) {
87 if (!strcmp(alg->ops->name, name))
88 if (try_module_get(alg->ops->module)) {
93 mutex_unlock(&rate_ctrl_mutex);
97 /* Get the rate control algorithm. */
98 static struct rate_control_ops *
99 ieee80211_rate_control_ops_get(const char *name)
101 struct rate_control_ops *ops;
102 const char *alg_name;
105 alg_name = ieee80211_default_rc_algo;
109 ops = ieee80211_try_rate_control_ops_get(alg_name);
111 request_module("rc80211_%s", alg_name);
112 ops = ieee80211_try_rate_control_ops_get(alg_name);
115 /* try default if specific alg requested but not found */
116 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
118 /* try built-in one if specific alg requested but not found */
119 if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
120 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
125 static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
127 module_put(ops->module);
130 struct rate_control_ref *rate_control_alloc(const char *name,
131 struct ieee80211_local *local)
133 struct rate_control_ref *ref;
135 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
138 kref_init(&ref->kref);
139 ref->ops = ieee80211_rate_control_ops_get(name);
142 ref->priv = ref->ops->alloc(local);
148 ieee80211_rate_control_ops_put(ref->ops);
155 static void rate_control_release(struct kref *kref)
157 struct rate_control_ref *ctrl_ref;
159 ctrl_ref = container_of(kref, struct rate_control_ref, kref);
160 ctrl_ref->ops->free(ctrl_ref->priv);
161 ieee80211_rate_control_ops_put(ctrl_ref->ops);
165 void rate_control_get_rate(struct net_device *dev,
166 struct ieee80211_supported_band *sband,
168 struct rate_selection *sel)
170 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
171 struct rate_control_ref *ref = local->rate_ctrl;
172 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
173 struct sta_info *sta;
177 sta = sta_info_get(local, hdr->addr1);
180 sel->nonerp_idx = -1;
183 ref->ops->get_rate(ref->priv, dev, sband, skb, sel);
185 BUG_ON(sel->rate_idx < 0);
187 /* Select a non-ERP backup rate. */
188 if (sel->nonerp_idx < 0) {
189 for (i = 0; i < sband->n_bitrates; i++) {
190 struct ieee80211_rate *rate = &sband->bitrates[i];
191 if (sband->bitrates[sel->rate_idx].bitrate < rate->bitrate)
194 if (rate_supported(sta, sband->band, i) &&
195 !(rate->flags & IEEE80211_RATE_ERP_G))
203 struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
205 kref_get(&ref->kref);
209 void rate_control_put(struct rate_control_ref *ref)
211 kref_put(&ref->kref, rate_control_release);
214 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
217 struct rate_control_ref *ref, *old;
220 if (local->open_count || netif_running(local->mdev))
223 ref = rate_control_alloc(name, local);
225 printk(KERN_WARNING "%s: Failed to select rate control "
226 "algorithm\n", wiphy_name(local->hw.wiphy));
230 old = local->rate_ctrl;
231 local->rate_ctrl = ref;
233 rate_control_put(old);
234 sta_info_flush(local, NULL);
237 printk(KERN_DEBUG "%s: Selected rate control "
238 "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
245 void rate_control_deinitialize(struct ieee80211_local *local)
247 struct rate_control_ref *ref;
249 ref = local->rate_ctrl;
250 local->rate_ctrl = NULL;
251 rate_control_put(ref);