Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002-2005, Instant802 Networks, Inc. |
| 3 | * Copyright 2005, 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 | #ifndef IEEE80211_RATE_H |
| 12 | #define IEEE80211_RATE_H |
| 13 | |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <net/mac80211.h> |
| 18 | #include "ieee80211_i.h" |
| 19 | #include "sta_info.h" |
| 20 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 21 | struct rate_selection { |
| 22 | /* Selected transmission rate */ |
| 23 | struct ieee80211_rate *rate; |
| 24 | /* Non-ERP rate to use if mac80211 decides it cannot use an ERP rate */ |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 25 | struct ieee80211_rate *nonerp; |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 26 | /* probe with this rate, or NULL for no probing */ |
| 27 | struct ieee80211_rate *probe; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 30 | struct rate_control_ops { |
| 31 | struct module *module; |
| 32 | const char *name; |
| 33 | void (*tx_status)(void *priv, struct net_device *dev, |
| 34 | struct sk_buff *skb, |
| 35 | struct ieee80211_tx_status *status); |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 36 | void (*get_rate)(void *priv, struct net_device *dev, |
| 37 | struct ieee80211_hw_mode *mode, struct sk_buff *skb, |
| 38 | struct rate_selection *sel); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 39 | void (*rate_init)(void *priv, void *priv_sta, |
| 40 | struct ieee80211_local *local, struct sta_info *sta); |
| 41 | void (*clear)(void *priv); |
| 42 | |
| 43 | void *(*alloc)(struct ieee80211_local *local); |
| 44 | void (*free)(void *priv); |
| 45 | void *(*alloc_sta)(void *priv, gfp_t gfp); |
| 46 | void (*free_sta)(void *priv, void *priv_sta); |
| 47 | |
| 48 | int (*add_attrs)(void *priv, struct kobject *kobj); |
| 49 | void (*remove_attrs)(void *priv, struct kobject *kobj); |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 50 | void (*add_sta_debugfs)(void *priv, void *priv_sta, |
| 51 | struct dentry *dir); |
| 52 | void (*remove_sta_debugfs)(void *priv, void *priv_sta); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct rate_control_ref { |
| 56 | struct rate_control_ops *ops; |
| 57 | void *priv; |
| 58 | struct kref kref; |
| 59 | }; |
| 60 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 61 | /* default 'simple' algorithm */ |
| 62 | extern struct rate_control_ops mac80211_rcsimple; |
| 63 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 64 | int ieee80211_rate_control_register(struct rate_control_ops *ops); |
| 65 | void ieee80211_rate_control_unregister(struct rate_control_ops *ops); |
| 66 | |
| 67 | /* Get a reference to the rate control algorithm. If `name' is NULL, get the |
| 68 | * first available algorithm. */ |
| 69 | struct rate_control_ref *rate_control_alloc(const char *name, |
| 70 | struct ieee80211_local *local); |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 71 | void rate_control_get_rate(struct net_device *dev, |
| 72 | struct ieee80211_hw_mode *mode, struct sk_buff *skb, |
| 73 | struct rate_selection *sel); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 74 | struct rate_control_ref *rate_control_get(struct rate_control_ref *ref); |
| 75 | void rate_control_put(struct rate_control_ref *ref); |
| 76 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 77 | static inline void rate_control_tx_status(struct net_device *dev, |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 78 | struct sk_buff *skb, |
| 79 | struct ieee80211_tx_status *status) |
| 80 | { |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 81 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 82 | struct rate_control_ref *ref = local->rate_ctrl; |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 83 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 84 | ref->ops->tx_status(ref->priv, dev, skb, status); |
| 85 | } |
| 86 | |
| 87 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 88 | static inline void rate_control_rate_init(struct sta_info *sta, |
| 89 | struct ieee80211_local *local) |
| 90 | { |
| 91 | struct rate_control_ref *ref = sta->rate_ctrl; |
| 92 | ref->ops->rate_init(ref->priv, sta->rate_ctrl_priv, local, sta); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static inline void rate_control_clear(struct ieee80211_local *local) |
| 97 | { |
| 98 | struct rate_control_ref *ref = local->rate_ctrl; |
| 99 | ref->ops->clear(ref->priv); |
| 100 | } |
| 101 | |
| 102 | static inline void *rate_control_alloc_sta(struct rate_control_ref *ref, |
| 103 | gfp_t gfp) |
| 104 | { |
| 105 | return ref->ops->alloc_sta(ref->priv, gfp); |
| 106 | } |
| 107 | |
| 108 | static inline void rate_control_free_sta(struct rate_control_ref *ref, |
| 109 | void *priv) |
| 110 | { |
| 111 | ref->ops->free_sta(ref->priv, priv); |
| 112 | } |
| 113 | |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 114 | static inline void rate_control_add_sta_debugfs(struct sta_info *sta) |
| 115 | { |
| 116 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 117 | struct rate_control_ref *ref = sta->rate_ctrl; |
| 118 | if (sta->debugfs.dir && ref->ops->add_sta_debugfs) |
| 119 | ref->ops->add_sta_debugfs(ref->priv, sta->rate_ctrl_priv, |
| 120 | sta->debugfs.dir); |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | static inline void rate_control_remove_sta_debugfs(struct sta_info *sta) |
| 125 | { |
| 126 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 127 | struct rate_control_ref *ref = sta->rate_ctrl; |
| 128 | if (ref->ops->remove_sta_debugfs) |
| 129 | ref->ops->remove_sta_debugfs(ref->priv, sta->rate_ctrl_priv); |
| 130 | #endif |
| 131 | } |
| 132 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 133 | static inline int |
| 134 | rate_supported(struct sta_info *sta, struct ieee80211_hw_mode *mode, int index) |
| 135 | { |
| 136 | return (sta == NULL || sta->supp_rates & BIT(index)) && |
| 137 | (mode->rates[index].flags & IEEE80211_RATE_SUPPORTED); |
| 138 | } |
| 139 | |
| 140 | static inline int |
| 141 | rate_lowest_index(struct ieee80211_local *local, struct ieee80211_hw_mode *mode, |
| 142 | struct sta_info *sta) |
| 143 | { |
| 144 | int i; |
| 145 | |
| 146 | for (i = 0; i < mode->num_rates; i++) { |
| 147 | if (rate_supported(sta, mode, i)) |
| 148 | return i; |
| 149 | } |
| 150 | |
| 151 | /* warn when we cannot find a rate. */ |
| 152 | WARN_ON(1); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static inline struct ieee80211_rate * |
| 158 | rate_lowest(struct ieee80211_local *local, struct ieee80211_hw_mode *mode, |
| 159 | struct sta_info *sta) |
| 160 | { |
| 161 | return &mode->rates[rate_lowest_index(local, mode, sta)]; |
| 162 | } |
| 163 | |
Johannes Berg | ff68808 | 2007-07-27 15:43:23 +0200 | [diff] [blame] | 164 | |
| 165 | /* functions for rate control related to a device */ |
| 166 | int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local, |
| 167 | const char *name); |
| 168 | void rate_control_deinitialize(struct ieee80211_local *local); |
| 169 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 170 | #endif /* IEEE80211_RATE_H */ |