Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 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> |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 12 | #include <linux/rtnetlink.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 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 | int ieee80211_rate_control_register(struct rate_control_ops *ops) |
| 25 | { |
| 26 | struct rate_control_alg *alg; |
| 27 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 28 | if (!ops->name) |
| 29 | return -EINVAL; |
| 30 | |
Johannes Berg | 999acd9 | 2007-10-28 14:49:33 +0100 | [diff] [blame] | 31 | 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 Gorcunov | b808ab1 | 2007-12-13 15:52:11 -0800 | [diff] [blame^] | 36 | mutex_unlock(&rate_ctrl_mutex); |
Johannes Berg | 999acd9 | 2007-10-28 14:49:33 +0100 | [diff] [blame] | 37 | return -EALREADY; |
| 38 | } |
| 39 | } |
| 40 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 41 | alg = kzalloc(sizeof(*alg), GFP_KERNEL); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 42 | if (alg == NULL) { |
Johannes Berg | 999acd9 | 2007-10-28 14:49:33 +0100 | [diff] [blame] | 43 | mutex_unlock(&rate_ctrl_mutex); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 44 | return -ENOMEM; |
| 45 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 46 | alg->ops = ops; |
| 47 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 48 | list_add_tail(&alg->list, &rate_ctrl_algs); |
| 49 | mutex_unlock(&rate_ctrl_mutex); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | EXPORT_SYMBOL(ieee80211_rate_control_register); |
| 54 | |
| 55 | void 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); |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | mutex_unlock(&rate_ctrl_mutex); |
| 67 | kfree(alg); |
| 68 | } |
| 69 | EXPORT_SYMBOL(ieee80211_rate_control_unregister); |
| 70 | |
| 71 | static struct rate_control_ops * |
| 72 | ieee80211_try_rate_control_ops_get(const char *name) |
| 73 | { |
| 74 | struct rate_control_alg *alg; |
| 75 | struct rate_control_ops *ops = NULL; |
| 76 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 77 | if (!name) |
| 78 | return NULL; |
| 79 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 80 | mutex_lock(&rate_ctrl_mutex); |
| 81 | list_for_each_entry(alg, &rate_ctrl_algs, list) { |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 82 | if (!strcmp(alg->ops->name, name)) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 83 | 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. */ |
| 94 | static struct rate_control_ops * |
| 95 | ieee80211_rate_control_ops_get(const char *name) |
| 96 | { |
| 97 | struct rate_control_ops *ops; |
| 98 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 99 | if (!name) |
| 100 | name = "simple"; |
| 101 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 102 | ops = ieee80211_try_rate_control_ops_get(name); |
| 103 | if (!ops) { |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 104 | request_module("rc80211_%s", name); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 105 | ops = ieee80211_try_rate_control_ops_get(name); |
| 106 | } |
| 107 | return ops; |
| 108 | } |
| 109 | |
| 110 | static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops) |
| 111 | { |
| 112 | module_put(ops->module); |
| 113 | } |
| 114 | |
| 115 | struct 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 | |
| 132 | fail_priv: |
| 133 | ieee80211_rate_control_ops_put(ref->ops); |
| 134 | fail_ops: |
| 135 | kfree(ref); |
| 136 | fail_ref: |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | static 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 | |
| 150 | struct rate_control_ref *rate_control_get(struct rate_control_ref *ref) |
| 151 | { |
| 152 | kref_get(&ref->kref); |
| 153 | return ref; |
| 154 | } |
| 155 | |
| 156 | void rate_control_put(struct rate_control_ref *ref) |
| 157 | { |
| 158 | kref_put(&ref->kref, rate_control_release); |
| 159 | } |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 160 | |
| 161 | int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local, |
| 162 | const char *name) |
| 163 | { |
| 164 | struct rate_control_ref *ref, *old; |
| 165 | |
| 166 | ASSERT_RTNL(); |
Johannes Berg | f9d540e | 2007-09-28 14:02:09 +0200 | [diff] [blame] | 167 | if (local->open_count || netif_running(local->mdev)) |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 168 | return -EBUSY; |
| 169 | |
| 170 | ref = rate_control_alloc(name, local); |
| 171 | if (!ref) { |
| 172 | printk(KERN_WARNING "%s: Failed to select rate control " |
Johannes Berg | dd1cd4c | 2007-09-18 17:29:20 -0400 | [diff] [blame] | 173 | "algorithm\n", wiphy_name(local->hw.wiphy)); |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 174 | return -ENOENT; |
| 175 | } |
| 176 | |
| 177 | old = local->rate_ctrl; |
| 178 | local->rate_ctrl = ref; |
| 179 | if (old) { |
| 180 | rate_control_put(old); |
| 181 | sta_info_flush(local, NULL); |
| 182 | } |
| 183 | |
| 184 | printk(KERN_DEBUG "%s: Selected rate control " |
Johannes Berg | dd1cd4c | 2007-09-18 17:29:20 -0400 | [diff] [blame] | 185 | "algorithm '%s'\n", wiphy_name(local->hw.wiphy), |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 186 | ref->ops->name); |
| 187 | |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | void rate_control_deinitialize(struct ieee80211_local *local) |
| 193 | { |
| 194 | struct rate_control_ref *ref; |
| 195 | |
| 196 | ref = local->rate_ctrl; |
| 197 | local->rate_ctrl = NULL; |
| 198 | rate_control_put(ref); |
| 199 | } |