1*2c8dccc7SJohannes Berg /* 2*2c8dccc7SJohannes Berg * Copyright 2002-2005, Instant802 Networks, Inc. 3*2c8dccc7SJohannes Berg * Copyright 2005, Devicescape Software, Inc. 4*2c8dccc7SJohannes Berg * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz> 5*2c8dccc7SJohannes Berg * 6*2c8dccc7SJohannes Berg * This program is free software; you can redistribute it and/or modify 7*2c8dccc7SJohannes Berg * it under the terms of the GNU General Public License version 2 as 8*2c8dccc7SJohannes Berg * published by the Free Software Foundation. 9*2c8dccc7SJohannes Berg */ 10*2c8dccc7SJohannes Berg 11*2c8dccc7SJohannes Berg #ifndef IEEE80211_RATE_H 12*2c8dccc7SJohannes Berg #define IEEE80211_RATE_H 13*2c8dccc7SJohannes Berg 14*2c8dccc7SJohannes Berg #include <linux/netdevice.h> 15*2c8dccc7SJohannes Berg #include <linux/skbuff.h> 16*2c8dccc7SJohannes Berg #include <linux/types.h> 17*2c8dccc7SJohannes Berg #include <linux/kref.h> 18*2c8dccc7SJohannes Berg #include <net/mac80211.h> 19*2c8dccc7SJohannes Berg #include "ieee80211_i.h" 20*2c8dccc7SJohannes Berg #include "sta_info.h" 21*2c8dccc7SJohannes Berg 22*2c8dccc7SJohannes Berg /* TODO: kdoc */ 23*2c8dccc7SJohannes Berg struct rate_selection { 24*2c8dccc7SJohannes Berg /* Selected transmission rate */ 25*2c8dccc7SJohannes Berg struct ieee80211_rate *rate; 26*2c8dccc7SJohannes Berg /* Non-ERP rate to use if mac80211 decides it cannot use an ERP rate */ 27*2c8dccc7SJohannes Berg struct ieee80211_rate *nonerp; 28*2c8dccc7SJohannes Berg /* probe with this rate, or NULL for no probing */ 29*2c8dccc7SJohannes Berg struct ieee80211_rate *probe; 30*2c8dccc7SJohannes Berg }; 31*2c8dccc7SJohannes Berg 32*2c8dccc7SJohannes Berg struct rate_control_ops { 33*2c8dccc7SJohannes Berg struct module *module; 34*2c8dccc7SJohannes Berg const char *name; 35*2c8dccc7SJohannes Berg void (*tx_status)(void *priv, struct net_device *dev, 36*2c8dccc7SJohannes Berg struct sk_buff *skb, 37*2c8dccc7SJohannes Berg struct ieee80211_tx_status *status); 38*2c8dccc7SJohannes Berg void (*get_rate)(void *priv, struct net_device *dev, 39*2c8dccc7SJohannes Berg struct ieee80211_supported_band *band, 40*2c8dccc7SJohannes Berg struct sk_buff *skb, 41*2c8dccc7SJohannes Berg struct rate_selection *sel); 42*2c8dccc7SJohannes Berg void (*rate_init)(void *priv, void *priv_sta, 43*2c8dccc7SJohannes Berg struct ieee80211_local *local, struct sta_info *sta); 44*2c8dccc7SJohannes Berg void (*clear)(void *priv); 45*2c8dccc7SJohannes Berg 46*2c8dccc7SJohannes Berg void *(*alloc)(struct ieee80211_local *local); 47*2c8dccc7SJohannes Berg void (*free)(void *priv); 48*2c8dccc7SJohannes Berg void *(*alloc_sta)(void *priv, gfp_t gfp); 49*2c8dccc7SJohannes Berg void (*free_sta)(void *priv, void *priv_sta); 50*2c8dccc7SJohannes Berg 51*2c8dccc7SJohannes Berg int (*add_attrs)(void *priv, struct kobject *kobj); 52*2c8dccc7SJohannes Berg void (*remove_attrs)(void *priv, struct kobject *kobj); 53*2c8dccc7SJohannes Berg void (*add_sta_debugfs)(void *priv, void *priv_sta, 54*2c8dccc7SJohannes Berg struct dentry *dir); 55*2c8dccc7SJohannes Berg void (*remove_sta_debugfs)(void *priv, void *priv_sta); 56*2c8dccc7SJohannes Berg }; 57*2c8dccc7SJohannes Berg 58*2c8dccc7SJohannes Berg struct rate_control_ref { 59*2c8dccc7SJohannes Berg struct rate_control_ops *ops; 60*2c8dccc7SJohannes Berg void *priv; 61*2c8dccc7SJohannes Berg struct kref kref; 62*2c8dccc7SJohannes Berg }; 63*2c8dccc7SJohannes Berg 64*2c8dccc7SJohannes Berg int ieee80211_rate_control_register(struct rate_control_ops *ops); 65*2c8dccc7SJohannes Berg void ieee80211_rate_control_unregister(struct rate_control_ops *ops); 66*2c8dccc7SJohannes Berg 67*2c8dccc7SJohannes Berg /* Get a reference to the rate control algorithm. If `name' is NULL, get the 68*2c8dccc7SJohannes Berg * first available algorithm. */ 69*2c8dccc7SJohannes Berg struct rate_control_ref *rate_control_alloc(const char *name, 70*2c8dccc7SJohannes Berg struct ieee80211_local *local); 71*2c8dccc7SJohannes Berg void rate_control_get_rate(struct net_device *dev, 72*2c8dccc7SJohannes Berg struct ieee80211_supported_band *sband, 73*2c8dccc7SJohannes Berg struct sk_buff *skb, 74*2c8dccc7SJohannes Berg struct rate_selection *sel); 75*2c8dccc7SJohannes Berg struct rate_control_ref *rate_control_get(struct rate_control_ref *ref); 76*2c8dccc7SJohannes Berg void rate_control_put(struct rate_control_ref *ref); 77*2c8dccc7SJohannes Berg 78*2c8dccc7SJohannes Berg static inline void rate_control_tx_status(struct net_device *dev, 79*2c8dccc7SJohannes Berg struct sk_buff *skb, 80*2c8dccc7SJohannes Berg struct ieee80211_tx_status *status) 81*2c8dccc7SJohannes Berg { 82*2c8dccc7SJohannes Berg struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 83*2c8dccc7SJohannes Berg struct rate_control_ref *ref = local->rate_ctrl; 84*2c8dccc7SJohannes Berg 85*2c8dccc7SJohannes Berg ref->ops->tx_status(ref->priv, dev, skb, status); 86*2c8dccc7SJohannes Berg } 87*2c8dccc7SJohannes Berg 88*2c8dccc7SJohannes Berg 89*2c8dccc7SJohannes Berg static inline void rate_control_rate_init(struct sta_info *sta, 90*2c8dccc7SJohannes Berg struct ieee80211_local *local) 91*2c8dccc7SJohannes Berg { 92*2c8dccc7SJohannes Berg struct rate_control_ref *ref = sta->rate_ctrl; 93*2c8dccc7SJohannes Berg ref->ops->rate_init(ref->priv, sta->rate_ctrl_priv, local, sta); 94*2c8dccc7SJohannes Berg } 95*2c8dccc7SJohannes Berg 96*2c8dccc7SJohannes Berg 97*2c8dccc7SJohannes Berg static inline void rate_control_clear(struct ieee80211_local *local) 98*2c8dccc7SJohannes Berg { 99*2c8dccc7SJohannes Berg struct rate_control_ref *ref = local->rate_ctrl; 100*2c8dccc7SJohannes Berg ref->ops->clear(ref->priv); 101*2c8dccc7SJohannes Berg } 102*2c8dccc7SJohannes Berg 103*2c8dccc7SJohannes Berg static inline void *rate_control_alloc_sta(struct rate_control_ref *ref, 104*2c8dccc7SJohannes Berg gfp_t gfp) 105*2c8dccc7SJohannes Berg { 106*2c8dccc7SJohannes Berg return ref->ops->alloc_sta(ref->priv, gfp); 107*2c8dccc7SJohannes Berg } 108*2c8dccc7SJohannes Berg 109*2c8dccc7SJohannes Berg static inline void rate_control_free_sta(struct rate_control_ref *ref, 110*2c8dccc7SJohannes Berg void *priv) 111*2c8dccc7SJohannes Berg { 112*2c8dccc7SJohannes Berg ref->ops->free_sta(ref->priv, priv); 113*2c8dccc7SJohannes Berg } 114*2c8dccc7SJohannes Berg 115*2c8dccc7SJohannes Berg static inline void rate_control_add_sta_debugfs(struct sta_info *sta) 116*2c8dccc7SJohannes Berg { 117*2c8dccc7SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS 118*2c8dccc7SJohannes Berg struct rate_control_ref *ref = sta->rate_ctrl; 119*2c8dccc7SJohannes Berg if (sta->debugfs.dir && ref->ops->add_sta_debugfs) 120*2c8dccc7SJohannes Berg ref->ops->add_sta_debugfs(ref->priv, sta->rate_ctrl_priv, 121*2c8dccc7SJohannes Berg sta->debugfs.dir); 122*2c8dccc7SJohannes Berg #endif 123*2c8dccc7SJohannes Berg } 124*2c8dccc7SJohannes Berg 125*2c8dccc7SJohannes Berg static inline void rate_control_remove_sta_debugfs(struct sta_info *sta) 126*2c8dccc7SJohannes Berg { 127*2c8dccc7SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS 128*2c8dccc7SJohannes Berg struct rate_control_ref *ref = sta->rate_ctrl; 129*2c8dccc7SJohannes Berg if (ref->ops->remove_sta_debugfs) 130*2c8dccc7SJohannes Berg ref->ops->remove_sta_debugfs(ref->priv, sta->rate_ctrl_priv); 131*2c8dccc7SJohannes Berg #endif 132*2c8dccc7SJohannes Berg } 133*2c8dccc7SJohannes Berg 134*2c8dccc7SJohannes Berg static inline int rate_supported(struct sta_info *sta, 135*2c8dccc7SJohannes Berg enum ieee80211_band band, 136*2c8dccc7SJohannes Berg int index) 137*2c8dccc7SJohannes Berg { 138*2c8dccc7SJohannes Berg return (sta == NULL || sta->supp_rates[band] & BIT(index)); 139*2c8dccc7SJohannes Berg } 140*2c8dccc7SJohannes Berg 141*2c8dccc7SJohannes Berg static inline int 142*2c8dccc7SJohannes Berg rate_lowest_index(struct ieee80211_local *local, 143*2c8dccc7SJohannes Berg struct ieee80211_supported_band *sband, 144*2c8dccc7SJohannes Berg struct sta_info *sta) 145*2c8dccc7SJohannes Berg { 146*2c8dccc7SJohannes Berg int i; 147*2c8dccc7SJohannes Berg 148*2c8dccc7SJohannes Berg for (i = 0; i < sband->n_bitrates; i++) 149*2c8dccc7SJohannes Berg if (rate_supported(sta, sband->band, i)) 150*2c8dccc7SJohannes Berg return i; 151*2c8dccc7SJohannes Berg 152*2c8dccc7SJohannes Berg /* warn when we cannot find a rate. */ 153*2c8dccc7SJohannes Berg WARN_ON(1); 154*2c8dccc7SJohannes Berg 155*2c8dccc7SJohannes Berg return 0; 156*2c8dccc7SJohannes Berg } 157*2c8dccc7SJohannes Berg 158*2c8dccc7SJohannes Berg static inline struct ieee80211_rate * 159*2c8dccc7SJohannes Berg rate_lowest(struct ieee80211_local *local, 160*2c8dccc7SJohannes Berg struct ieee80211_supported_band *sband, 161*2c8dccc7SJohannes Berg struct sta_info *sta) 162*2c8dccc7SJohannes Berg { 163*2c8dccc7SJohannes Berg return &sband->bitrates[rate_lowest_index(local, sband, sta)]; 164*2c8dccc7SJohannes Berg } 165*2c8dccc7SJohannes Berg 166*2c8dccc7SJohannes Berg 167*2c8dccc7SJohannes Berg /* functions for rate control related to a device */ 168*2c8dccc7SJohannes Berg int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local, 169*2c8dccc7SJohannes Berg const char *name); 170*2c8dccc7SJohannes Berg void rate_control_deinitialize(struct ieee80211_local *local); 171*2c8dccc7SJohannes Berg 172*2c8dccc7SJohannes Berg 173*2c8dccc7SJohannes Berg /* Rate control algorithms */ 174*2c8dccc7SJohannes Berg #if defined(RC80211_PID_COMPILE) || \ 175*2c8dccc7SJohannes Berg (defined(CONFIG_MAC80211_RC_PID) && \ 176*2c8dccc7SJohannes Berg !defined(CONFIG_MAC80211_RC_PID_MODULE)) 177*2c8dccc7SJohannes Berg extern int rc80211_pid_init(void); 178*2c8dccc7SJohannes Berg extern void rc80211_pid_exit(void); 179*2c8dccc7SJohannes Berg #else 180*2c8dccc7SJohannes Berg static inline int rc80211_pid_init(void) 181*2c8dccc7SJohannes Berg { 182*2c8dccc7SJohannes Berg return 0; 183*2c8dccc7SJohannes Berg } 184*2c8dccc7SJohannes Berg static inline void rc80211_pid_exit(void) 185*2c8dccc7SJohannes Berg { 186*2c8dccc7SJohannes Berg } 187*2c8dccc7SJohannes Berg #endif 188*2c8dccc7SJohannes Berg 189*2c8dccc7SJohannes Berg #endif /* IEEE80211_RATE_H */ 190