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); |
| 36 | return -EALREADY; |
| 37 | } |
| 38 | } |
| 39 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 40 | alg = kzalloc(sizeof(*alg), GFP_KERNEL); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 41 | if (alg == NULL) { |
Johannes Berg | 999acd9 | 2007-10-28 14:49:33 +0100 | [diff] [blame^] | 42 | mutex_unlock(&rate_ctrl_mutex); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 43 | return -ENOMEM; |
| 44 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 45 | alg->ops = ops; |
| 46 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 47 | list_add_tail(&alg->list, &rate_ctrl_algs); |
| 48 | mutex_unlock(&rate_ctrl_mutex); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | EXPORT_SYMBOL(ieee80211_rate_control_register); |
| 53 | |
| 54 | void ieee80211_rate_control_unregister(struct rate_control_ops *ops) |
| 55 | { |
| 56 | struct rate_control_alg *alg; |
| 57 | |
| 58 | mutex_lock(&rate_ctrl_mutex); |
| 59 | list_for_each_entry(alg, &rate_ctrl_algs, list) { |
| 60 | if (alg->ops == ops) { |
| 61 | list_del(&alg->list); |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | mutex_unlock(&rate_ctrl_mutex); |
| 66 | kfree(alg); |
| 67 | } |
| 68 | EXPORT_SYMBOL(ieee80211_rate_control_unregister); |
| 69 | |
| 70 | static struct rate_control_ops * |
| 71 | ieee80211_try_rate_control_ops_get(const char *name) |
| 72 | { |
| 73 | struct rate_control_alg *alg; |
| 74 | struct rate_control_ops *ops = NULL; |
| 75 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 76 | if (!name) |
| 77 | return NULL; |
| 78 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 79 | mutex_lock(&rate_ctrl_mutex); |
| 80 | list_for_each_entry(alg, &rate_ctrl_algs, list) { |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 81 | if (!strcmp(alg->ops->name, name)) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 82 | if (try_module_get(alg->ops->module)) { |
| 83 | ops = alg->ops; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | mutex_unlock(&rate_ctrl_mutex); |
| 88 | return ops; |
| 89 | } |
| 90 | |
| 91 | /* Get the rate control algorithm. If `name' is NULL, get the first |
| 92 | * available algorithm. */ |
| 93 | static struct rate_control_ops * |
| 94 | ieee80211_rate_control_ops_get(const char *name) |
| 95 | { |
| 96 | struct rate_control_ops *ops; |
| 97 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 98 | if (!name) |
| 99 | name = "simple"; |
| 100 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 101 | ops = ieee80211_try_rate_control_ops_get(name); |
| 102 | if (!ops) { |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 103 | request_module("rc80211_%s", name); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 104 | ops = ieee80211_try_rate_control_ops_get(name); |
| 105 | } |
| 106 | return ops; |
| 107 | } |
| 108 | |
| 109 | static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops) |
| 110 | { |
| 111 | module_put(ops->module); |
| 112 | } |
| 113 | |
| 114 | struct rate_control_ref *rate_control_alloc(const char *name, |
| 115 | struct ieee80211_local *local) |
| 116 | { |
| 117 | struct rate_control_ref *ref; |
| 118 | |
| 119 | ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL); |
| 120 | if (!ref) |
| 121 | goto fail_ref; |
| 122 | kref_init(&ref->kref); |
| 123 | ref->ops = ieee80211_rate_control_ops_get(name); |
| 124 | if (!ref->ops) |
| 125 | goto fail_ops; |
| 126 | ref->priv = ref->ops->alloc(local); |
| 127 | if (!ref->priv) |
| 128 | goto fail_priv; |
| 129 | return ref; |
| 130 | |
| 131 | fail_priv: |
| 132 | ieee80211_rate_control_ops_put(ref->ops); |
| 133 | fail_ops: |
| 134 | kfree(ref); |
| 135 | fail_ref: |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | static void rate_control_release(struct kref *kref) |
| 140 | { |
| 141 | struct rate_control_ref *ctrl_ref; |
| 142 | |
| 143 | ctrl_ref = container_of(kref, struct rate_control_ref, kref); |
| 144 | ctrl_ref->ops->free(ctrl_ref->priv); |
| 145 | ieee80211_rate_control_ops_put(ctrl_ref->ops); |
| 146 | kfree(ctrl_ref); |
| 147 | } |
| 148 | |
| 149 | struct rate_control_ref *rate_control_get(struct rate_control_ref *ref) |
| 150 | { |
| 151 | kref_get(&ref->kref); |
| 152 | return ref; |
| 153 | } |
| 154 | |
| 155 | void rate_control_put(struct rate_control_ref *ref) |
| 156 | { |
| 157 | kref_put(&ref->kref, rate_control_release); |
| 158 | } |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 159 | |
| 160 | int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local, |
| 161 | const char *name) |
| 162 | { |
| 163 | struct rate_control_ref *ref, *old; |
| 164 | |
| 165 | ASSERT_RTNL(); |
Johannes Berg | f9d540e | 2007-09-28 14:02:09 +0200 | [diff] [blame] | 166 | if (local->open_count || netif_running(local->mdev)) |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 167 | return -EBUSY; |
| 168 | |
| 169 | ref = rate_control_alloc(name, local); |
| 170 | if (!ref) { |
| 171 | printk(KERN_WARNING "%s: Failed to select rate control " |
Johannes Berg | dd1cd4c | 2007-09-18 17:29:20 -0400 | [diff] [blame] | 172 | "algorithm\n", wiphy_name(local->hw.wiphy)); |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 173 | return -ENOENT; |
| 174 | } |
| 175 | |
| 176 | old = local->rate_ctrl; |
| 177 | local->rate_ctrl = ref; |
| 178 | if (old) { |
| 179 | rate_control_put(old); |
| 180 | sta_info_flush(local, NULL); |
| 181 | } |
| 182 | |
| 183 | printk(KERN_DEBUG "%s: Selected rate control " |
Johannes Berg | dd1cd4c | 2007-09-18 17:29:20 -0400 | [diff] [blame] | 184 | "algorithm '%s'\n", wiphy_name(local->hw.wiphy), |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 185 | ref->ops->name); |
| 186 | |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | void rate_control_deinitialize(struct ieee80211_local *local) |
| 192 | { |
| 193 | struct rate_control_ref *ref; |
| 194 | |
| 195 | ref = local->rate_ctrl; |
| 196 | local->rate_ctrl = NULL; |
| 197 | rate_control_put(ref); |
| 198 | } |