Merge branch 'linux-next' of git://git.infradead.org/~dedekind/ubi-2.6
[linux-2.6] / net / mac80211 / debugfs_sta.c
1 /*
2  * Copyright 2003-2005  Devicescape Software, Inc.
3  * Copyright (c) 2006   Jiri Benc <jbenc@suse.cz>
4  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
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/debugfs.h>
12 #include <linux/ieee80211.h>
13 #include "ieee80211_i.h"
14 #include "debugfs.h"
15 #include "debugfs_sta.h"
16 #include "sta_info.h"
17
18 /* sta attributtes */
19
20 #define STA_READ(name, buflen, field, format_string)                    \
21 static ssize_t sta_ ##name## _read(struct file *file,                   \
22                                    char __user *userbuf,                \
23                                    size_t count, loff_t *ppos)          \
24 {                                                                       \
25         int res;                                                        \
26         struct sta_info *sta = file->private_data;                      \
27         char buf[buflen];                                               \
28         res = scnprintf(buf, buflen, format_string, sta->field);        \
29         return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
30 }
31 #define STA_READ_D(name, field) STA_READ(name, 20, field, "%d\n")
32 #define STA_READ_U(name, field) STA_READ(name, 20, field, "%u\n")
33 #define STA_READ_LU(name, field) STA_READ(name, 20, field, "%lu\n")
34 #define STA_READ_S(name, field) STA_READ(name, 20, field, "%s\n")
35
36 #define STA_OPS(name)                                                   \
37 static const struct file_operations sta_ ##name## _ops = {              \
38         .read = sta_##name##_read,                                      \
39         .open = mac80211_open_file_generic,                             \
40 }
41
42 #define STA_OPS_WR(name)                                                \
43 static const struct file_operations sta_ ##name## _ops = {              \
44         .read = sta_##name##_read,                                      \
45         .write = sta_##name##_write,                                    \
46         .open = mac80211_open_file_generic,                             \
47 }
48
49 #define STA_FILE(name, field, format)                                   \
50                 STA_READ_##format(name, field)                          \
51                 STA_OPS(name)
52
53 STA_FILE(aid, sta.aid, D);
54 STA_FILE(dev, sdata->dev->name, S);
55 STA_FILE(rx_packets, rx_packets, LU);
56 STA_FILE(tx_packets, tx_packets, LU);
57 STA_FILE(rx_bytes, rx_bytes, LU);
58 STA_FILE(tx_bytes, tx_bytes, LU);
59 STA_FILE(rx_duplicates, num_duplicates, LU);
60 STA_FILE(rx_fragments, rx_fragments, LU);
61 STA_FILE(rx_dropped, rx_dropped, LU);
62 STA_FILE(tx_fragments, tx_fragments, LU);
63 STA_FILE(tx_filtered, tx_filtered_count, LU);
64 STA_FILE(tx_retry_failed, tx_retry_failed, LU);
65 STA_FILE(tx_retry_count, tx_retry_count, LU);
66 STA_FILE(last_signal, last_signal, D);
67 STA_FILE(last_qual, last_qual, D);
68 STA_FILE(last_noise, last_noise, D);
69 STA_FILE(wep_weak_iv_count, wep_weak_iv_count, LU);
70
71 static ssize_t sta_flags_read(struct file *file, char __user *userbuf,
72                               size_t count, loff_t *ppos)
73 {
74         char buf[100];
75         struct sta_info *sta = file->private_data;
76         u32 staflags = get_sta_flags(sta);
77         int res = scnprintf(buf, sizeof(buf), "%s%s%s%s%s%s%s",
78                 staflags & WLAN_STA_AUTH ? "AUTH\n" : "",
79                 staflags & WLAN_STA_ASSOC ? "ASSOC\n" : "",
80                 staflags & WLAN_STA_PS ? "PS\n" : "",
81                 staflags & WLAN_STA_AUTHORIZED ? "AUTHORIZED\n" : "",
82                 staflags & WLAN_STA_SHORT_PREAMBLE ? "SHORT PREAMBLE\n" : "",
83                 staflags & WLAN_STA_WME ? "WME\n" : "",
84                 staflags & WLAN_STA_WDS ? "WDS\n" : "");
85         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
86 }
87 STA_OPS(flags);
88
89 static ssize_t sta_num_ps_buf_frames_read(struct file *file,
90                                           char __user *userbuf,
91                                           size_t count, loff_t *ppos)
92 {
93         char buf[20];
94         struct sta_info *sta = file->private_data;
95         int res = scnprintf(buf, sizeof(buf), "%u\n",
96                             skb_queue_len(&sta->ps_tx_buf));
97         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
98 }
99 STA_OPS(num_ps_buf_frames);
100
101 static ssize_t sta_inactive_ms_read(struct file *file, char __user *userbuf,
102                                     size_t count, loff_t *ppos)
103 {
104         char buf[20];
105         struct sta_info *sta = file->private_data;
106         int res = scnprintf(buf, sizeof(buf), "%d\n",
107                             jiffies_to_msecs(jiffies - sta->last_rx));
108         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
109 }
110 STA_OPS(inactive_ms);
111
112 static ssize_t sta_last_seq_ctrl_read(struct file *file, char __user *userbuf,
113                                       size_t count, loff_t *ppos)
114 {
115         char buf[15*NUM_RX_DATA_QUEUES], *p = buf;
116         int i;
117         struct sta_info *sta = file->private_data;
118         for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
119                 p += scnprintf(p, sizeof(buf)+buf-p, "%x ",
120                                le16_to_cpu(sta->last_seq_ctrl[i]));
121         p += scnprintf(p, sizeof(buf)+buf-p, "\n");
122         return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
123 }
124 STA_OPS(last_seq_ctrl);
125
126 static ssize_t sta_agg_status_read(struct file *file, char __user *userbuf,
127                                         size_t count, loff_t *ppos)
128 {
129         char buf[768], *p = buf;
130         int i;
131         struct sta_info *sta = file->private_data;
132         p += scnprintf(p, sizeof(buf)+buf-p, "Agg state for STA is:\n");
133         p += scnprintf(p, sizeof(buf)+buf-p, " STA next dialog_token is %d \n "
134                         "TIDs info is: \n TID :",
135                         (sta->ampdu_mlme.dialog_token_allocator + 1));
136         for (i = 0; i < STA_TID_NUM; i++)
137                 p += scnprintf(p, sizeof(buf)+buf-p, "%5d", i);
138
139         p += scnprintf(p, sizeof(buf)+buf-p, "\n RX  :");
140         for (i = 0; i < STA_TID_NUM; i++)
141                 p += scnprintf(p, sizeof(buf)+buf-p, "%5d",
142                         sta->ampdu_mlme.tid_state_rx[i]);
143
144         p += scnprintf(p, sizeof(buf)+buf-p, "\n DTKN:");
145         for (i = 0; i < STA_TID_NUM; i++)
146                 p += scnprintf(p, sizeof(buf)+buf-p, "%5d",
147                         sta->ampdu_mlme.tid_state_rx[i]?
148                         sta->ampdu_mlme.tid_rx[i]->dialog_token : 0);
149
150         p += scnprintf(p, sizeof(buf)+buf-p, "\n TX  :");
151         for (i = 0; i < STA_TID_NUM; i++)
152                 p += scnprintf(p, sizeof(buf)+buf-p, "%5d",
153                         sta->ampdu_mlme.tid_state_tx[i]);
154
155         p += scnprintf(p, sizeof(buf)+buf-p, "\n DTKN:");
156         for (i = 0; i < STA_TID_NUM; i++)
157                 p += scnprintf(p, sizeof(buf)+buf-p, "%5d",
158                         sta->ampdu_mlme.tid_state_tx[i]?
159                         sta->ampdu_mlme.tid_tx[i]->dialog_token : 0);
160
161         p += scnprintf(p, sizeof(buf)+buf-p, "\n SSN :");
162         for (i = 0; i < STA_TID_NUM; i++)
163                 p += scnprintf(p, sizeof(buf)+buf-p, "%5d",
164                         sta->ampdu_mlme.tid_state_tx[i]?
165                         sta->ampdu_mlme.tid_tx[i]->ssn : 0);
166
167         p += scnprintf(p, sizeof(buf)+buf-p, "\n");
168
169         return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
170 }
171
172 static ssize_t sta_agg_status_write(struct file *file,
173                 const char __user *user_buf, size_t count, loff_t *ppos)
174 {
175         struct sta_info *sta = file->private_data;
176         struct ieee80211_local *local = sta->sdata->local;
177         struct ieee80211_hw *hw = &local->hw;
178         u8 *da = sta->sta.addr;
179         static int tid_static_tx[16] = {0, 0, 0, 0, 0, 0, 0, 0,
180                                         0, 0, 0, 0, 0, 0, 0, 0};
181         static int tid_static_rx[16] = {1, 1, 1, 1, 1, 1, 1, 1,
182                                         1, 1, 1, 1, 1, 1, 1, 1};
183         char *endp;
184         char buf[32];
185         int buf_size, rs;
186         unsigned int tid_num;
187         char state[4];
188
189         memset(buf, 0x00, sizeof(buf));
190         buf_size = min(count, (sizeof(buf)-1));
191         if (copy_from_user(buf, user_buf, buf_size))
192                 return -EFAULT;
193
194         tid_num = simple_strtoul(buf, &endp, 0);
195         if (endp == buf)
196                 return -EINVAL;
197
198         if ((tid_num >= 100) && (tid_num <= 115)) {
199                 /* toggle Rx aggregation command */
200                 tid_num = tid_num - 100;
201                 if (tid_static_rx[tid_num] == 1) {
202                         strcpy(state, "off");
203                         ieee80211_sta_stop_rx_ba_session(sta->sdata, da, tid_num, 0,
204                                         WLAN_REASON_QSTA_REQUIRE_SETUP);
205                         sta->ampdu_mlme.tid_state_rx[tid_num] |=
206                                         HT_AGG_STATE_DEBUGFS_CTL;
207                         tid_static_rx[tid_num] = 0;
208                 } else {
209                         strcpy(state, "on ");
210                         sta->ampdu_mlme.tid_state_rx[tid_num] &=
211                                         ~HT_AGG_STATE_DEBUGFS_CTL;
212                         tid_static_rx[tid_num] = 1;
213                 }
214                 printk(KERN_DEBUG "debugfs - try switching tid %u %s\n",
215                                 tid_num, state);
216         } else if ((tid_num >= 0) && (tid_num <= 15)) {
217                 /* toggle Tx aggregation command */
218                 if (tid_static_tx[tid_num] == 0) {
219                         strcpy(state, "on ");
220                         rs =  ieee80211_start_tx_ba_session(hw, da, tid_num);
221                         if (rs == 0)
222                                 tid_static_tx[tid_num] = 1;
223                 } else {
224                         strcpy(state, "off");
225                         rs =  ieee80211_stop_tx_ba_session(hw, da, tid_num, 1);
226                         if (rs == 0)
227                                 tid_static_tx[tid_num] = 0;
228                 }
229                 printk(KERN_DEBUG "debugfs - switching tid %u %s, return=%d\n",
230                                 tid_num, state, rs);
231         }
232
233         return count;
234 }
235 STA_OPS_WR(agg_status);
236
237 #define DEBUGFS_ADD(name) \
238         sta->debugfs.name = debugfs_create_file(#name, 0400, \
239                 sta->debugfs.dir, sta, &sta_ ##name## _ops);
240
241 #define DEBUGFS_DEL(name) \
242         debugfs_remove(sta->debugfs.name);\
243         sta->debugfs.name = NULL;
244
245
246 void ieee80211_sta_debugfs_add(struct sta_info *sta)
247 {
248         struct dentry *stations_dir = sta->local->debugfs.stations;
249         DECLARE_MAC_BUF(mbuf);
250         u8 *mac;
251
252         sta->debugfs.add_has_run = true;
253
254         if (!stations_dir)
255                 return;
256
257         mac = print_mac(mbuf, sta->sta.addr);
258
259         /*
260          * This might fail due to a race condition:
261          * When mac80211 unlinks a station, the debugfs entries
262          * remain, but it is already possible to link a new
263          * station with the same address which triggers adding
264          * it to debugfs; therefore, if the old station isn't
265          * destroyed quickly enough the old station's debugfs
266          * dir might still be around.
267          */
268         sta->debugfs.dir = debugfs_create_dir(mac, stations_dir);
269         if (!sta->debugfs.dir)
270                 return;
271
272         DEBUGFS_ADD(flags);
273         DEBUGFS_ADD(num_ps_buf_frames);
274         DEBUGFS_ADD(inactive_ms);
275         DEBUGFS_ADD(last_seq_ctrl);
276         DEBUGFS_ADD(agg_status);
277 }
278
279 void ieee80211_sta_debugfs_remove(struct sta_info *sta)
280 {
281         DEBUGFS_DEL(flags);
282         DEBUGFS_DEL(num_ps_buf_frames);
283         DEBUGFS_DEL(inactive_ms);
284         DEBUGFS_DEL(last_seq_ctrl);
285         DEBUGFS_DEL(agg_status);
286
287         debugfs_remove(sta->debugfs.dir);
288         sta->debugfs.dir = NULL;
289 }