blob: 3eb0696f375768095f1fa1f8d46c1dfd68957e49 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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 Nissler1abbe492007-12-20 13:50:07 +010021struct 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 Bencf0706e82007-05-05 11:45:53 -070025 struct ieee80211_rate *nonerp;
Mattias Nissler1abbe492007-12-20 13:50:07 +010026 /* probe with this rate, or NULL for no probing */
27 struct ieee80211_rate *probe;
Jiri Bencf0706e82007-05-05 11:45:53 -070028};
29
Jiri Bencf0706e82007-05-05 11:45:53 -070030struct 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 Nissler1abbe492007-12-20 13:50:07 +010036 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 Bencf0706e82007-05-05 11:45:53 -070039 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 Bence9f207f2007-05-05 11:46:38 -070050 void (*add_sta_debugfs)(void *priv, void *priv_sta,
51 struct dentry *dir);
52 void (*remove_sta_debugfs)(void *priv, void *priv_sta);
Jiri Bencf0706e82007-05-05 11:45:53 -070053};
54
55struct rate_control_ref {
56 struct rate_control_ops *ops;
57 void *priv;
58 struct kref kref;
59};
60
Johannes Bergac71c692007-10-28 14:17:44 +010061/* default 'simple' algorithm */
62extern struct rate_control_ops mac80211_rcsimple;
63
Mattias Nisslerad018372007-12-19 01:25:57 +010064/* 'PID' algorithm */
65extern struct rate_control_ops mac80211_rcpid;
66
Jiri Bencf0706e82007-05-05 11:45:53 -070067int ieee80211_rate_control_register(struct rate_control_ops *ops);
68void ieee80211_rate_control_unregister(struct rate_control_ops *ops);
69
70/* Get a reference to the rate control algorithm. If `name' is NULL, get the
71 * first available algorithm. */
72struct rate_control_ref *rate_control_alloc(const char *name,
73 struct ieee80211_local *local);
Mattias Nissler1abbe492007-12-20 13:50:07 +010074void rate_control_get_rate(struct net_device *dev,
75 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
76 struct rate_selection *sel);
Jiri Bencf0706e82007-05-05 11:45:53 -070077struct rate_control_ref *rate_control_get(struct rate_control_ref *ref);
78void rate_control_put(struct rate_control_ref *ref);
79
Mattias Nissler1abbe492007-12-20 13:50:07 +010080static inline void rate_control_tx_status(struct net_device *dev,
Jiri Bencf0706e82007-05-05 11:45:53 -070081 struct sk_buff *skb,
82 struct ieee80211_tx_status *status)
83{
Mattias Nissler1abbe492007-12-20 13:50:07 +010084 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -070085 struct rate_control_ref *ref = local->rate_ctrl;
Mattias Nissler1abbe492007-12-20 13:50:07 +010086
Jiri Bencf0706e82007-05-05 11:45:53 -070087 ref->ops->tx_status(ref->priv, dev, skb, status);
88}
89
90
Jiri Bencf0706e82007-05-05 11:45:53 -070091static inline void rate_control_rate_init(struct sta_info *sta,
92 struct ieee80211_local *local)
93{
94 struct rate_control_ref *ref = sta->rate_ctrl;
95 ref->ops->rate_init(ref->priv, sta->rate_ctrl_priv, local, sta);
96}
97
98
99static inline void rate_control_clear(struct ieee80211_local *local)
100{
101 struct rate_control_ref *ref = local->rate_ctrl;
102 ref->ops->clear(ref->priv);
103}
104
105static inline void *rate_control_alloc_sta(struct rate_control_ref *ref,
106 gfp_t gfp)
107{
108 return ref->ops->alloc_sta(ref->priv, gfp);
109}
110
111static inline void rate_control_free_sta(struct rate_control_ref *ref,
112 void *priv)
113{
114 ref->ops->free_sta(ref->priv, priv);
115}
116
Jiri Bence9f207f2007-05-05 11:46:38 -0700117static inline void rate_control_add_sta_debugfs(struct sta_info *sta)
118{
119#ifdef CONFIG_MAC80211_DEBUGFS
120 struct rate_control_ref *ref = sta->rate_ctrl;
121 if (sta->debugfs.dir && ref->ops->add_sta_debugfs)
122 ref->ops->add_sta_debugfs(ref->priv, sta->rate_ctrl_priv,
123 sta->debugfs.dir);
124#endif
125}
126
127static inline void rate_control_remove_sta_debugfs(struct sta_info *sta)
128{
129#ifdef CONFIG_MAC80211_DEBUGFS
130 struct rate_control_ref *ref = sta->rate_ctrl;
131 if (ref->ops->remove_sta_debugfs)
132 ref->ops->remove_sta_debugfs(ref->priv, sta->rate_ctrl_priv);
133#endif
134}
135
Mattias Nissler1abbe492007-12-20 13:50:07 +0100136static inline int
137rate_supported(struct sta_info *sta, struct ieee80211_hw_mode *mode, int index)
138{
139 return (sta == NULL || sta->supp_rates & BIT(index)) &&
140 (mode->rates[index].flags & IEEE80211_RATE_SUPPORTED);
141}
142
143static inline int
144rate_lowest_index(struct ieee80211_local *local, struct ieee80211_hw_mode *mode,
145 struct sta_info *sta)
146{
147 int i;
148
149 for (i = 0; i < mode->num_rates; i++) {
150 if (rate_supported(sta, mode, i))
151 return i;
152 }
153
154 /* warn when we cannot find a rate. */
155 WARN_ON(1);
156
157 return 0;
158}
159
160static inline struct ieee80211_rate *
161rate_lowest(struct ieee80211_local *local, struct ieee80211_hw_mode *mode,
162 struct sta_info *sta)
163{
164 return &mode->rates[rate_lowest_index(local, mode, sta)];
165}
166
Johannes Bergff688082007-07-27 15:43:23 +0200167
168/* functions for rate control related to a device */
169int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
170 const char *name);
171void rate_control_deinitialize(struct ieee80211_local *local);
172
Jiri Bencf0706e82007-05-05 11:45:53 -0700173#endif /* IEEE80211_RATE_H */