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 "ieee80211_rate.h"
13 #include "ieee80211_i.h"
15 struct rate_control_alg
{
16 struct list_head list
;
17 struct rate_control_ops
*ops
;
20 static LIST_HEAD(rate_ctrl_algs
);
21 static DEFINE_MUTEX(rate_ctrl_mutex
);
23 int ieee80211_rate_control_register(struct rate_control_ops
*ops
)
25 struct rate_control_alg
*alg
;
27 alg
= kmalloc(sizeof(*alg
), GFP_KERNEL
);
31 memset(alg
, 0, sizeof(*alg
));
34 mutex_lock(&rate_ctrl_mutex
);
35 list_add_tail(&alg
->list
, &rate_ctrl_algs
);
36 mutex_unlock(&rate_ctrl_mutex
);
40 EXPORT_SYMBOL(ieee80211_rate_control_register
);
42 void ieee80211_rate_control_unregister(struct rate_control_ops
*ops
)
44 struct rate_control_alg
*alg
;
46 mutex_lock(&rate_ctrl_mutex
);
47 list_for_each_entry(alg
, &rate_ctrl_algs
, list
) {
48 if (alg
->ops
== ops
) {
53 mutex_unlock(&rate_ctrl_mutex
);
56 EXPORT_SYMBOL(ieee80211_rate_control_unregister
);
58 static struct rate_control_ops
*
59 ieee80211_try_rate_control_ops_get(const char *name
)
61 struct rate_control_alg
*alg
;
62 struct rate_control_ops
*ops
= NULL
;
64 mutex_lock(&rate_ctrl_mutex
);
65 list_for_each_entry(alg
, &rate_ctrl_algs
, list
) {
66 if (!name
|| !strcmp(alg
->ops
->name
, name
))
67 if (try_module_get(alg
->ops
->module
)) {
72 mutex_unlock(&rate_ctrl_mutex
);
76 /* Get the rate control algorithm. If `name' is NULL, get the first
77 * available algorithm. */
78 static struct rate_control_ops
*
79 ieee80211_rate_control_ops_get(const char *name
)
81 struct rate_control_ops
*ops
;
83 ops
= ieee80211_try_rate_control_ops_get(name
);
85 request_module("rc80211_%s", name
? name
: "default");
86 ops
= ieee80211_try_rate_control_ops_get(name
);
91 static void ieee80211_rate_control_ops_put(struct rate_control_ops
*ops
)
93 module_put(ops
->module
);
96 struct rate_control_ref
*rate_control_alloc(const char *name
,
97 struct ieee80211_local
*local
)
99 struct rate_control_ref
*ref
;
101 ref
= kmalloc(sizeof(struct rate_control_ref
), GFP_KERNEL
);
104 kref_init(&ref
->kref
);
105 ref
->ops
= ieee80211_rate_control_ops_get(name
);
108 ref
->priv
= ref
->ops
->alloc(local
);
114 ieee80211_rate_control_ops_put(ref
->ops
);
121 static void rate_control_release(struct kref
*kref
)
123 struct rate_control_ref
*ctrl_ref
;
125 ctrl_ref
= container_of(kref
, struct rate_control_ref
, kref
);
126 ctrl_ref
->ops
->free(ctrl_ref
->priv
);
127 ieee80211_rate_control_ops_put(ctrl_ref
->ops
);
131 struct rate_control_ref
*rate_control_get(struct rate_control_ref
*ref
)
133 kref_get(&ref
->kref
);
137 void rate_control_put(struct rate_control_ref
*ref
)
139 kref_put(&ref
->kref
, rate_control_release
);