blob: e495b0998b4dfc67bbc4801e883ef05d4ab96a76 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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
11#include <linux/kernel.h>
Johannes Bergff688082007-07-27 15:43:23 +020012#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070013#include "ieee80211_rate.h"
14#include "ieee80211_i.h"
15
16struct rate_control_alg {
17 struct list_head list;
18 struct rate_control_ops *ops;
19};
20
21static LIST_HEAD(rate_ctrl_algs);
22static DEFINE_MUTEX(rate_ctrl_mutex);
23
24int ieee80211_rate_control_register(struct rate_control_ops *ops)
25{
26 struct rate_control_alg *alg;
27
Johannes Bergac71c692007-10-28 14:17:44 +010028 if (!ops->name)
29 return -EINVAL;
30
Johannes Berg999acd92007-10-28 14:49:33 +010031 mutex_lock(&rate_ctrl_mutex);
32 list_for_each_entry(alg, &rate_ctrl_algs, list) {
33 if (!strcmp(alg->ops->name, ops->name)) {
34 /* don't register an algorithm twice */
35 WARN_ON(1);
Cyrill Gorcunovb808ab12007-12-13 15:52:11 -080036 mutex_unlock(&rate_ctrl_mutex);
Johannes Berg999acd92007-10-28 14:49:33 +010037 return -EALREADY;
38 }
39 }
40
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070041 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -070042 if (alg == NULL) {
Johannes Berg999acd92007-10-28 14:49:33 +010043 mutex_unlock(&rate_ctrl_mutex);
Jiri Bencf0706e82007-05-05 11:45:53 -070044 return -ENOMEM;
45 }
Jiri Bencf0706e82007-05-05 11:45:53 -070046 alg->ops = ops;
47
Jiri Bencf0706e82007-05-05 11:45:53 -070048 list_add_tail(&alg->list, &rate_ctrl_algs);
49 mutex_unlock(&rate_ctrl_mutex);
50
51 return 0;
52}
53EXPORT_SYMBOL(ieee80211_rate_control_register);
54
55void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
56{
57 struct rate_control_alg *alg;
58
59 mutex_lock(&rate_ctrl_mutex);
60 list_for_each_entry(alg, &rate_ctrl_algs, list) {
61 if (alg->ops == ops) {
62 list_del(&alg->list);
Cyrill Gorcunov20880e82007-12-13 16:17:03 -080063 kfree(alg);
Jiri Bencf0706e82007-05-05 11:45:53 -070064 break;
65 }
66 }
67 mutex_unlock(&rate_ctrl_mutex);
Jiri Bencf0706e82007-05-05 11:45:53 -070068}
69EXPORT_SYMBOL(ieee80211_rate_control_unregister);
70
71static struct rate_control_ops *
72ieee80211_try_rate_control_ops_get(const char *name)
73{
74 struct rate_control_alg *alg;
75 struct rate_control_ops *ops = NULL;
76
Johannes Bergac71c692007-10-28 14:17:44 +010077 if (!name)
78 return NULL;
79
Jiri Bencf0706e82007-05-05 11:45:53 -070080 mutex_lock(&rate_ctrl_mutex);
81 list_for_each_entry(alg, &rate_ctrl_algs, list) {
Johannes Bergac71c692007-10-28 14:17:44 +010082 if (!strcmp(alg->ops->name, name))
Jiri Bencf0706e82007-05-05 11:45:53 -070083 if (try_module_get(alg->ops->module)) {
84 ops = alg->ops;
85 break;
86 }
87 }
88 mutex_unlock(&rate_ctrl_mutex);
89 return ops;
90}
91
92/* Get the rate control algorithm. If `name' is NULL, get the first
93 * available algorithm. */
94static struct rate_control_ops *
95ieee80211_rate_control_ops_get(const char *name)
96{
97 struct rate_control_ops *ops;
98
Johannes Bergac71c692007-10-28 14:17:44 +010099 if (!name)
100 name = "simple";
101
Jiri Bencf0706e82007-05-05 11:45:53 -0700102 ops = ieee80211_try_rate_control_ops_get(name);
103 if (!ops) {
Johannes Bergac71c692007-10-28 14:17:44 +0100104 request_module("rc80211_%s", name);
Jiri Bencf0706e82007-05-05 11:45:53 -0700105 ops = ieee80211_try_rate_control_ops_get(name);
106 }
107 return ops;
108}
109
110static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
111{
112 module_put(ops->module);
113}
114
115struct rate_control_ref *rate_control_alloc(const char *name,
116 struct ieee80211_local *local)
117{
118 struct rate_control_ref *ref;
119
120 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
121 if (!ref)
122 goto fail_ref;
123 kref_init(&ref->kref);
124 ref->ops = ieee80211_rate_control_ops_get(name);
125 if (!ref->ops)
126 goto fail_ops;
127 ref->priv = ref->ops->alloc(local);
128 if (!ref->priv)
129 goto fail_priv;
130 return ref;
131
132fail_priv:
133 ieee80211_rate_control_ops_put(ref->ops);
134fail_ops:
135 kfree(ref);
136fail_ref:
137 return NULL;
138}
139
140static void rate_control_release(struct kref *kref)
141{
142 struct rate_control_ref *ctrl_ref;
143
144 ctrl_ref = container_of(kref, struct rate_control_ref, kref);
145 ctrl_ref->ops->free(ctrl_ref->priv);
146 ieee80211_rate_control_ops_put(ctrl_ref->ops);
147 kfree(ctrl_ref);
148}
149
Mattias Nissler1abbe492007-12-20 13:50:07 +0100150void rate_control_get_rate(struct net_device *dev,
151 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
152 struct rate_selection *sel)
153{
154 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
155 struct rate_control_ref *ref = local->rate_ctrl;
156 struct ieee80211_sub_if_data *sdata;
157 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
158 struct sta_info *sta = sta_info_get(local, hdr->addr1);
159 int i;
160 u16 fc;
161
162 memset(sel, 0, sizeof(struct rate_selection));
163
164 /* Send management frames and broadcast/multicast data using lowest
165 * rate. */
166 fc = le16_to_cpu(hdr->frame_control);
167 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
168 is_multicast_ether_addr(hdr->addr1))
169 sel->rate = rate_lowest(local, mode, sta);
170
171 /* If a forced rate is in effect, select it. */
172 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
173 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
174 sel->rate = &mode->rates[sdata->bss->force_unicast_rateidx];
175
176 /* If we haven't found the rate yet, ask the rate control algo. */
177 if (!sel->rate)
178 ref->ops->get_rate(ref->priv, dev, mode, skb, sel);
179
180 /* Select a non-ERP backup rate. */
181 if (!sel->nonerp) {
182 for (i = 0; i < mode->num_rates - 1; i++) {
183 struct ieee80211_rate *rate = &mode->rates[i];
184 if (sel->rate->rate < rate->rate)
185 break;
186
187 if (rate_supported(sta, mode, i) &&
188 !(rate->flags & IEEE80211_RATE_ERP))
189 sel->nonerp = rate;
190 }
191 }
192
193 if (sta)
194 sta_info_put(sta);
195}
196
Jiri Bencf0706e82007-05-05 11:45:53 -0700197struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
198{
199 kref_get(&ref->kref);
200 return ref;
201}
202
203void rate_control_put(struct rate_control_ref *ref)
204{
205 kref_put(&ref->kref, rate_control_release);
206}
Johannes Bergff688082007-07-27 15:43:23 +0200207
208int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
209 const char *name)
210{
211 struct rate_control_ref *ref, *old;
212
213 ASSERT_RTNL();
Johannes Bergf9d540e2007-09-28 14:02:09 +0200214 if (local->open_count || netif_running(local->mdev))
Johannes Bergff688082007-07-27 15:43:23 +0200215 return -EBUSY;
216
217 ref = rate_control_alloc(name, local);
218 if (!ref) {
219 printk(KERN_WARNING "%s: Failed to select rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400220 "algorithm\n", wiphy_name(local->hw.wiphy));
Johannes Bergff688082007-07-27 15:43:23 +0200221 return -ENOENT;
222 }
223
224 old = local->rate_ctrl;
225 local->rate_ctrl = ref;
226 if (old) {
227 rate_control_put(old);
228 sta_info_flush(local, NULL);
229 }
230
231 printk(KERN_DEBUG "%s: Selected rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400232 "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
Johannes Bergff688082007-07-27 15:43:23 +0200233 ref->ops->name);
234
235
236 return 0;
237}
238
239void rate_control_deinitialize(struct ieee80211_local *local)
240{
241 struct rate_control_ref *ref;
242
243 ref = local->rate_ctrl;
244 local->rate_ctrl = NULL;
245 rate_control_put(ref);
246}