iwlwifi: Add debugfs to iwl core
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-debugfs.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * Contact Information:
25  * Tomas Winkler <tomas.winkler@intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *****************************************************************************/
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/debugfs.h>
32
33 #include <linux/ieee80211.h>
34 #include <net/mac80211.h>
35
36
37 #include "iwl-4965.h"
38 #include "iwl-debug.h"
39 #include "iwl-4965-io.h"
40
41
42 /* create and remove of files */
43 #define DEBUGFS_ADD_DIR(name, parent) do {                              \
44         dbgfs->dir_##name = debugfs_create_dir(#name, parent);          \
45         if (!(dbgfs->dir_##name))                                       \
46                 goto err;                                               \
47 } while (0)
48
49 #define DEBUGFS_ADD_FILE(name, parent) do {                             \
50         dbgfs->dbgfs_##parent##_files.file_##name =                     \
51         debugfs_create_file(#name, 0644, dbgfs->dir_##parent, priv,     \
52                                 &iwl_dbgfs_##name##_ops);               \
53         if (!(dbgfs->dbgfs_##parent##_files.file_##name))               \
54                 goto err;                                               \
55 } while (0)
56
57 #define DEBUGFS_REMOVE(name)  do {              \
58         debugfs_remove(name);                   \
59         name = NULL;                            \
60 } while (0);
61
62 /* file operation */
63 #define DEBUGFS_READ_FUNC(name)                                         \
64 static ssize_t iwl_dbgfs_##name##_read(struct file *file,               \
65                                         char __user *user_buf,          \
66                                         size_t count, loff_t *ppos);
67
68 #define DEBUGFS_WRITE_FUNC(name)                                        \
69 static ssize_t iwl_dbgfs_##name##_write(struct file *file,              \
70                                         const char __user *user_buf,    \
71                                         size_t count, loff_t *ppos);
72
73
74 static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
75 {
76         file->private_data = inode->i_private;
77         return 0;
78 }
79
80 #define DEBUGFS_READ_FILE_OPS(name)                                     \
81         DEBUGFS_READ_FUNC(name);                                        \
82 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
83         .read = iwl_dbgfs_##name##_read,                                \
84         .open = iwl_dbgfs_open_file_generic,                            \
85 };
86
87 #define DEBUGFS_READ_WRITE_FILE_OPS(name)                               \
88         DEBUGFS_READ_FUNC(name);                                        \
89         DEBUGFS_WRITE_FUNC(name);                                       \
90 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
91         .write = iwl_dbgfs_##name##_write,                              \
92         .read = iwl_dbgfs_##name##_read,                                \
93         .open = iwl_dbgfs_open_file_generic,                            \
94 };
95
96
97 static ssize_t iwl_dbgfs_tx_statistics_read(struct file *file,
98                                                 char __user *user_buf,
99                                                 size_t count, loff_t *ppos) {
100
101         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
102         char buf[256];
103         int pos = 0;
104
105         pos += sprintf(buf+pos, "mgmt: %u\n", priv->tx_stats[0].cnt);
106         pos += sprintf(buf+pos, "ctrl: %u\n", priv->tx_stats[1].cnt);
107         pos += sprintf(buf+pos, "data: %u\n", priv->tx_stats[2].cnt);
108
109         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
110 }
111
112 static ssize_t iwl_dbgfs_rx_statistics_read(struct file *file,
113                                                 char __user *user_buf,
114                                                 size_t count, loff_t *ppos) {
115
116         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
117         char buf[256];
118         int pos = 0;
119
120         pos += sprintf(buf+pos, "mgmt: %u\n", priv->rx_stats[0].cnt);
121         pos += sprintf(buf+pos, "ctrl: %u\n", priv->rx_stats[1].cnt);
122         pos += sprintf(buf+pos, "data: %u\n", priv->rx_stats[2].cnt);
123
124         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
125 }
126
127 #define BYTE1_MASK 0x000000ff;
128 #define BYTE2_MASK 0x0000ffff;
129 #define BYTE3_MASK 0x00ffffff;
130 static ssize_t iwl_dbgfs_sram_read(struct file *file,
131                                         char __user *user_buf,
132                                         size_t count, loff_t *ppos)
133 {
134         u32 val;
135         char buf[1024];
136         ssize_t ret;
137         int i;
138         int pos = 0;
139         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
140
141         printk(KERN_DEBUG "offset is: 0x%x\tlen is: 0x%x\n",
142         priv->dbgfs->sram_offset, priv->dbgfs->sram_len);
143
144         iwl4965_grab_nic_access(priv);
145         for (i = priv->dbgfs->sram_len; i > 0; i -= 4) {
146                 val = iwl4965_read_targ_mem(priv, priv->dbgfs->sram_offset + \
147                                         priv->dbgfs->sram_len - i);
148                 if (i < 4) {
149                         switch (i) {
150                         case 1:
151                                 val &= BYTE1_MASK;
152                                 break;
153                         case 2:
154                                 val &= BYTE2_MASK;
155                                 break;
156                         case 3:
157                                 val &= BYTE3_MASK;
158                                 break;
159                         }
160                 }
161                 pos += sprintf(buf+pos, "0x%08x ", val);
162         }
163         pos += sprintf(buf+pos, "\n");
164         iwl4965_release_nic_access(priv);
165
166         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
167         return ret;
168 }
169
170 static ssize_t iwl_dbgfs_sram_write(struct file *file,
171                                         const char __user *user_buf,
172                                         size_t count, loff_t *ppos)
173 {
174         struct iwl_priv *priv = file->private_data;
175         char buf[64];
176         int buf_size;
177         u32 offset, len;
178
179         memset(buf, 0, sizeof(buf));
180         buf_size = min(count, sizeof(buf) -  1);
181         if (copy_from_user(buf, user_buf, buf_size))
182                 return -EFAULT;
183
184         if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
185                 priv->dbgfs->sram_offset = offset;
186                 priv->dbgfs->sram_len = len;
187         } else {
188                 priv->dbgfs->sram_offset = 0;
189                 priv->dbgfs->sram_len = 0;
190         }
191
192         return count;
193 }
194
195 static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
196                                         size_t count, loff_t *ppos)
197 {
198         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
199         struct iwl4965_station_entry *station;
200         int max_sta = priv->hw_setting.max_stations;
201         char *buf;
202         int i, j, pos = 0;
203         ssize_t ret;
204         /* Add 30 for initial string */
205         const size_t bufsz = 30 + sizeof(char) * 500 * (priv->num_stations);
206         DECLARE_MAC_BUF(mac);
207
208         buf = kmalloc(bufsz, GFP_KERNEL);
209         if(!buf)
210                 return -ENOMEM;
211
212         pos += sprintf(buf+pos, "num of stations: %d\n\n",
213                         priv->num_stations);
214
215         for (i = 0; i < max_sta; i++) {
216                 station = &priv->stations[i];
217                 if (station->used) {
218                         pos += sprintf(buf+pos, "station %d:\ngeneral data:\n",
219                                         i+1);
220                         print_mac(mac, station->sta.sta.addr);
221                         pos += sprintf(buf+pos, "id: %u\n",
222                                         station->sta.sta.sta_id);
223                         pos += sprintf(buf+pos, "mode: %u\n",
224                                         station->sta.mode);
225                         pos += sprintf(buf+pos, "flags: 0x%x\n",
226                                         station->sta.station_flags_msk);
227                         pos += sprintf(buf+pos, "ps_status: %u\n",
228                                         station->ps_status);
229
230                         pos += sprintf(buf+pos, "tid data:\n");
231
232                         pos += sprintf(buf+pos, "seq_num\t\ttxq_id\t");
233                         pos += sprintf(buf+pos, "frame_count\twait_for_ba\t");
234                         pos += sprintf(buf+pos, "start_idx\tbitmap0\t");
235                         pos += sprintf(buf+pos, "bitmap1\trate_n_flags\n");
236
237                         for (j = 0; j < MAX_TID_COUNT; j++) {
238                                 pos += sprintf(buf+pos, "[%d]:\t\t%u\t",
239                                                 j, station->tid[j].seq_number);
240                                 pos += sprintf(buf+pos, "%u\t\t%u\t\t%u\t\t",
241                                                 station->tid[j].agg.txq_id,
242                                                 station->tid[j].agg.frame_count,
243                                                 station->tid[j].agg.wait_for_ba);
244                                 pos += sprintf(buf+pos, "%u\t%llu\t%u\n",
245                                                 station->tid[j].agg.start_idx,
246                                                 station->tid[j].agg.bitmap,
247                                                 station->tid[j].agg.rate_n_flags);
248                         }
249                         pos += sprintf(buf+pos, "\n");
250                 }
251         }
252
253         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
254         kfree(buf);
255         return ret;
256 }
257
258
259 DEBUGFS_READ_WRITE_FILE_OPS(sram);
260 DEBUGFS_READ_FILE_OPS(stations);
261 DEBUGFS_READ_FILE_OPS(rx_statistics);
262 DEBUGFS_READ_FILE_OPS(tx_statistics);
263
264 /*
265  * Create the debugfs files and directories
266  *
267  */
268 int iwl_dbgfs_register(struct iwl_priv *priv, const char *name)
269 {
270         struct iwl_debugfs *dbgfs;
271
272         dbgfs = kzalloc(sizeof(struct iwl_debugfs), GFP_KERNEL);
273         if (!dbgfs) {
274                 goto err;
275         }
276
277         priv->dbgfs = dbgfs;
278         dbgfs->name = name;
279         dbgfs->dir_drv = debugfs_create_dir(name, NULL);
280         if (!dbgfs->dir_drv || IS_ERR(dbgfs->dir_drv)){
281                 goto err;
282         }
283
284         DEBUGFS_ADD_DIR(data, dbgfs->dir_drv);
285         DEBUGFS_ADD_FILE(sram, data);
286         DEBUGFS_ADD_FILE(stations, data);
287         DEBUGFS_ADD_FILE(rx_statistics, data);
288         DEBUGFS_ADD_FILE(tx_statistics, data);
289
290         return 0;
291
292 err:
293         IWL_ERROR("Can't open the debugfs directory\n");
294         iwl_dbgfs_unregister(priv);
295         return -ENOENT;
296 }
297 EXPORT_SYMBOL(iwl_dbgfs_register);
298
299 /**
300  * Remove the debugfs files and directories
301  *
302  */
303 void iwl_dbgfs_unregister(struct iwl_priv *priv)
304 {
305         if (!(priv->dbgfs))
306                 return;
307
308         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_rx_statistics);
309         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_tx_statistics);
310         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_sram);
311         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_stations);
312         DEBUGFS_REMOVE(priv->dbgfs->dir_data);
313         DEBUGFS_REMOVE(priv->dbgfs->dir_drv);
314         kfree(priv->dbgfs);
315         priv->dbgfs = NULL;
316 }
317 EXPORT_SYMBOL(iwl_dbgfs_unregister);
318
319