mac80211: make PID rate control algorithm the default
[pandora-kernel.git] / net / mac80211 / ieee80211_rate.c
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
11 #include <linux/kernel.h>
12 #include <linux/rtnetlink.h>
13 #include "ieee80211_rate.h"
14 #include "ieee80211_i.h"
15
16 struct rate_control_alg {
17         struct list_head list;
18         struct rate_control_ops *ops;
19 };
20
21 static LIST_HEAD(rate_ctrl_algs);
22 static DEFINE_MUTEX(rate_ctrl_mutex);
23
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");
28
29 int ieee80211_rate_control_register(struct rate_control_ops *ops)
30 {
31         struct rate_control_alg *alg;
32
33         if (!ops->name)
34                 return -EINVAL;
35
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 */
40                         WARN_ON(1);
41                         mutex_unlock(&rate_ctrl_mutex);
42                         return -EALREADY;
43                 }
44         }
45
46         alg = kzalloc(sizeof(*alg), GFP_KERNEL);
47         if (alg == NULL) {
48                 mutex_unlock(&rate_ctrl_mutex);
49                 return -ENOMEM;
50         }
51         alg->ops = ops;
52
53         list_add_tail(&alg->list, &rate_ctrl_algs);
54         mutex_unlock(&rate_ctrl_mutex);
55
56         return 0;
57 }
58 EXPORT_SYMBOL(ieee80211_rate_control_register);
59
60 void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
61 {
62         struct rate_control_alg *alg;
63
64         mutex_lock(&rate_ctrl_mutex);
65         list_for_each_entry(alg, &rate_ctrl_algs, list) {
66                 if (alg->ops == ops) {
67                         list_del(&alg->list);
68                         kfree(alg);
69                         break;
70                 }
71         }
72         mutex_unlock(&rate_ctrl_mutex);
73 }
74 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
75
76 static struct rate_control_ops *
77 ieee80211_try_rate_control_ops_get(const char *name)
78 {
79         struct rate_control_alg *alg;
80         struct rate_control_ops *ops = NULL;
81
82         if (!name)
83                 return NULL;
84
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)) {
89                                 ops = alg->ops;
90                                 break;
91                         }
92         }
93         mutex_unlock(&rate_ctrl_mutex);
94         return ops;
95 }
96
97 /* Get the rate control algorithm. */
98 static struct rate_control_ops *
99 ieee80211_rate_control_ops_get(const char *name)
100 {
101         struct rate_control_ops *ops;
102         const char *alg_name;
103
104         if (!name)
105                 alg_name = ieee80211_default_rc_algo;
106         else
107                 alg_name = name;
108
109         ops = ieee80211_try_rate_control_ops_get(alg_name);
110         if (!ops) {
111                 request_module("rc80211_%s", alg_name);
112                 ops = ieee80211_try_rate_control_ops_get(alg_name);
113         }
114         if (!ops && name)
115                 /* try default if specific alg requested but not found */
116                 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
117
118         return ops;
119 }
120
121 static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
122 {
123         module_put(ops->module);
124 }
125
126 struct rate_control_ref *rate_control_alloc(const char *name,
127                                             struct ieee80211_local *local)
128 {
129         struct rate_control_ref *ref;
130
131         ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
132         if (!ref)
133                 goto fail_ref;
134         kref_init(&ref->kref);
135         ref->ops = ieee80211_rate_control_ops_get(name);
136         if (!ref->ops)
137                 goto fail_ops;
138         ref->priv = ref->ops->alloc(local);
139         if (!ref->priv)
140                 goto fail_priv;
141         return ref;
142
143 fail_priv:
144         ieee80211_rate_control_ops_put(ref->ops);
145 fail_ops:
146         kfree(ref);
147 fail_ref:
148         return NULL;
149 }
150
151 static void rate_control_release(struct kref *kref)
152 {
153         struct rate_control_ref *ctrl_ref;
154
155         ctrl_ref = container_of(kref, struct rate_control_ref, kref);
156         ctrl_ref->ops->free(ctrl_ref->priv);
157         ieee80211_rate_control_ops_put(ctrl_ref->ops);
158         kfree(ctrl_ref);
159 }
160
161 void rate_control_get_rate(struct net_device *dev,
162                            struct ieee80211_hw_mode *mode, struct sk_buff *skb,
163                            struct rate_selection *sel)
164 {
165         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
166         struct rate_control_ref *ref = local->rate_ctrl;
167         struct ieee80211_sub_if_data *sdata;
168         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
169         struct sta_info *sta = sta_info_get(local, hdr->addr1);
170         int i;
171         u16 fc;
172
173         memset(sel, 0, sizeof(struct rate_selection));
174
175         /* Send management frames and broadcast/multicast data using lowest
176          * rate. */
177         fc = le16_to_cpu(hdr->frame_control);
178         if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
179             is_multicast_ether_addr(hdr->addr1))
180                 sel->rate = rate_lowest(local, mode, sta);
181
182         /* If a forced rate is in effect, select it. */
183         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
184         if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
185                 sel->rate = &mode->rates[sdata->bss->force_unicast_rateidx];
186
187         /* If we haven't found the rate yet, ask the rate control algo. */
188         if (!sel->rate)
189                 ref->ops->get_rate(ref->priv, dev, mode, skb, sel);
190
191         /* Select a non-ERP backup rate. */
192         if (!sel->nonerp) {
193                 for (i = 0; i < mode->num_rates - 1; i++) {
194                         struct ieee80211_rate *rate = &mode->rates[i];
195                         if (sel->rate->rate < rate->rate)
196                                 break;
197
198                         if (rate_supported(sta, mode, i) &&
199                             !(rate->flags & IEEE80211_RATE_ERP))
200                                 sel->nonerp = rate;
201                 }
202         }
203
204         if (sta)
205                 sta_info_put(sta);
206 }
207
208 struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
209 {
210         kref_get(&ref->kref);
211         return ref;
212 }
213
214 void rate_control_put(struct rate_control_ref *ref)
215 {
216         kref_put(&ref->kref, rate_control_release);
217 }
218
219 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
220                                  const char *name)
221 {
222         struct rate_control_ref *ref, *old;
223
224         ASSERT_RTNL();
225         if (local->open_count || netif_running(local->mdev))
226                 return -EBUSY;
227
228         ref = rate_control_alloc(name, local);
229         if (!ref) {
230                 printk(KERN_WARNING "%s: Failed to select rate control "
231                        "algorithm\n", wiphy_name(local->hw.wiphy));
232                 return -ENOENT;
233         }
234
235         old = local->rate_ctrl;
236         local->rate_ctrl = ref;
237         if (old) {
238                 rate_control_put(old);
239                 sta_info_flush(local, NULL);
240         }
241
242         printk(KERN_DEBUG "%s: Selected rate control "
243                "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
244                ref->ops->name);
245
246
247         return 0;
248 }
249
250 void rate_control_deinitialize(struct ieee80211_local *local)
251 {
252         struct rate_control_ref *ref;
253
254         ref = local->rate_ctrl;
255         local->rate_ctrl = NULL;
256         rate_control_put(ref);
257 }
258