wlcore: add support macros to easily add conf debugfs entries
[cascardo/linux.git] / drivers / net / wireless / ti / wlcore / debugfs.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include "debugfs.h"
25
26 #include <linux/skbuff.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29
30 #include "wlcore.h"
31 #include "debug.h"
32 #include "acx.h"
33 #include "ps.h"
34 #include "io.h"
35 #include "tx.h"
36 #include "hw_ops.h"
37
38 /* ms */
39 #define WL1271_DEBUGFS_STATS_LIFETIME 1000
40
41 /* debugfs macros idea from mac80211 */
42 int wl1271_format_buffer(char __user *userbuf, size_t count,
43                          loff_t *ppos, char *fmt, ...)
44 {
45         va_list args;
46         char buf[DEBUGFS_FORMAT_BUFFER_SIZE];
47         int res;
48
49         va_start(args, fmt);
50         res = vscnprintf(buf, sizeof(buf), fmt, args);
51         va_end(args);
52
53         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
54 }
55 EXPORT_SYMBOL_GPL(wl1271_format_buffer);
56
57 void wl1271_debugfs_update_stats(struct wl1271 *wl)
58 {
59         int ret;
60
61         mutex_lock(&wl->mutex);
62
63         ret = wl1271_ps_elp_wakeup(wl);
64         if (ret < 0)
65                 goto out;
66
67         if (wl->state == WL1271_STATE_ON && !wl->plt &&
68             time_after(jiffies, wl->stats.fw_stats_update +
69                        msecs_to_jiffies(WL1271_DEBUGFS_STATS_LIFETIME))) {
70                 wl1271_acx_statistics(wl, wl->stats.fw_stats);
71                 wl->stats.fw_stats_update = jiffies;
72         }
73
74         wl1271_ps_elp_sleep(wl);
75
76 out:
77         mutex_unlock(&wl->mutex);
78 }
79 EXPORT_SYMBOL_GPL(wl1271_debugfs_update_stats);
80
81 DEBUGFS_READONLY_FILE(retry_count, "%u", wl->stats.retry_count);
82 DEBUGFS_READONLY_FILE(excessive_retries, "%u",
83                       wl->stats.excessive_retries);
84
85 static ssize_t tx_queue_len_read(struct file *file, char __user *userbuf,
86                                  size_t count, loff_t *ppos)
87 {
88         struct wl1271 *wl = file->private_data;
89         u32 queue_len;
90         char buf[20];
91         int res;
92
93         queue_len = wl1271_tx_total_queue_count(wl);
94
95         res = scnprintf(buf, sizeof(buf), "%u\n", queue_len);
96         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
97 }
98
99 static const struct file_operations tx_queue_len_ops = {
100         .read = tx_queue_len_read,
101         .open = simple_open,
102         .llseek = default_llseek,
103 };
104
105 static void chip_op_handler(struct wl1271 *wl, unsigned long value,
106                             void *arg)
107 {
108         int ret;
109         int (*chip_op) (struct wl1271 *wl);
110
111         if (!arg) {
112                 wl1271_warning("debugfs chip_op_handler with no callback");
113                 return;
114         }
115
116         ret = wl1271_ps_elp_wakeup(wl);
117         if (ret < 0)
118                 return;
119
120         chip_op = arg;
121         chip_op(wl);
122
123         wl1271_ps_elp_sleep(wl);
124 }
125
126
127 static inline void no_write_handler(struct wl1271 *wl,
128                                     unsigned long value,
129                                     unsigned long param)
130 {
131 }
132
133 #define WL12XX_CONF_DEBUGFS(param, conf_sub_struct,                     \
134                             min_val, max_val, write_handler_locked,     \
135                             write_handler_arg)                          \
136         static ssize_t param##_read(struct file *file,                  \
137                                       char __user *user_buf,            \
138                                       size_t count, loff_t *ppos)       \
139         {                                                               \
140         struct wl1271 *wl = file->private_data;                         \
141         return wl1271_format_buffer(user_buf, count,                    \
142                                     ppos, "%d\n",                       \
143                                     wl->conf.conf_sub_struct.param);    \
144         }                                                               \
145                                                                         \
146         static ssize_t param##_write(struct file *file,                 \
147                                      const char __user *user_buf,       \
148                                      size_t count, loff_t *ppos)        \
149         {                                                               \
150         struct wl1271 *wl = file->private_data;                         \
151         unsigned long value;                                            \
152         int ret;                                                        \
153                                                                         \
154         ret = kstrtoul_from_user(user_buf, count, 10, &value);          \
155         if (ret < 0) {                                                  \
156                 wl1271_warning("illegal value for " #param);            \
157                 return -EINVAL;                                         \
158         }                                                               \
159                                                                         \
160         if (value < min_val || value > max_val) {                       \
161                 wl1271_warning(#param " is not in valid range");        \
162                 return -ERANGE;                                         \
163         }                                                               \
164                                                                         \
165         mutex_lock(&wl->mutex);                                         \
166         wl->conf.conf_sub_struct.param = value;                         \
167                                                                         \
168         write_handler_locked(wl, value, write_handler_arg);             \
169                                                                         \
170         mutex_unlock(&wl->mutex);                                       \
171         return count;                                                   \
172         }                                                               \
173                                                                         \
174         static const struct file_operations param##_ops = {             \
175                 .read = param##_read,                                   \
176                 .write = param##_write,                                 \
177                 .open = simple_open,                                    \
178                 .llseek = default_llseek,                               \
179         };
180 static ssize_t gpio_power_read(struct file *file, char __user *user_buf,
181                           size_t count, loff_t *ppos)
182 {
183         struct wl1271 *wl = file->private_data;
184         bool state = test_bit(WL1271_FLAG_GPIO_POWER, &wl->flags);
185
186         int res;
187         char buf[10];
188
189         res = scnprintf(buf, sizeof(buf), "%d\n", state);
190
191         return simple_read_from_buffer(user_buf, count, ppos, buf, res);
192 }
193
194 static ssize_t gpio_power_write(struct file *file,
195                            const char __user *user_buf,
196                            size_t count, loff_t *ppos)
197 {
198         struct wl1271 *wl = file->private_data;
199         unsigned long value;
200         int ret;
201
202         ret = kstrtoul_from_user(user_buf, count, 10, &value);
203         if (ret < 0) {
204                 wl1271_warning("illegal value in gpio_power");
205                 return -EINVAL;
206         }
207
208         mutex_lock(&wl->mutex);
209
210         if (value)
211                 wl1271_power_on(wl);
212         else
213                 wl1271_power_off(wl);
214
215         mutex_unlock(&wl->mutex);
216         return count;
217 }
218
219 static const struct file_operations gpio_power_ops = {
220         .read = gpio_power_read,
221         .write = gpio_power_write,
222         .open = simple_open,
223         .llseek = default_llseek,
224 };
225
226 static ssize_t start_recovery_write(struct file *file,
227                                     const char __user *user_buf,
228                                     size_t count, loff_t *ppos)
229 {
230         struct wl1271 *wl = file->private_data;
231
232         mutex_lock(&wl->mutex);
233         wl12xx_queue_recovery_work(wl);
234         mutex_unlock(&wl->mutex);
235
236         return count;
237 }
238
239 static const struct file_operations start_recovery_ops = {
240         .write = start_recovery_write,
241         .open = simple_open,
242         .llseek = default_llseek,
243 };
244
245 static ssize_t dynamic_ps_timeout_read(struct file *file, char __user *user_buf,
246                           size_t count, loff_t *ppos)
247 {
248         struct wl1271 *wl = file->private_data;
249
250         return wl1271_format_buffer(user_buf, count,
251                                     ppos, "%d\n",
252                                     wl->conf.conn.dynamic_ps_timeout);
253 }
254
255 static ssize_t dynamic_ps_timeout_write(struct file *file,
256                                     const char __user *user_buf,
257                                     size_t count, loff_t *ppos)
258 {
259         struct wl1271 *wl = file->private_data;
260         struct wl12xx_vif *wlvif;
261         unsigned long value;
262         int ret;
263
264         ret = kstrtoul_from_user(user_buf, count, 10, &value);
265         if (ret < 0) {
266                 wl1271_warning("illegal value in dynamic_ps");
267                 return -EINVAL;
268         }
269
270         if (value < 1 || value > 65535) {
271                 wl1271_warning("dyanmic_ps_timeout is not in valid range");
272                 return -ERANGE;
273         }
274
275         mutex_lock(&wl->mutex);
276
277         wl->conf.conn.dynamic_ps_timeout = value;
278
279         if (wl->state == WL1271_STATE_OFF)
280                 goto out;
281
282         ret = wl1271_ps_elp_wakeup(wl);
283         if (ret < 0)
284                 goto out;
285
286         /* In case we're already in PSM, trigger it again to set new timeout
287          * immediately without waiting for re-association
288          */
289
290         wl12xx_for_each_wlvif_sta(wl, wlvif) {
291                 if (test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags))
292                         wl1271_ps_set_mode(wl, wlvif, STATION_AUTO_PS_MODE);
293         }
294
295         wl1271_ps_elp_sleep(wl);
296
297 out:
298         mutex_unlock(&wl->mutex);
299         return count;
300 }
301
302 static const struct file_operations dynamic_ps_timeout_ops = {
303         .read = dynamic_ps_timeout_read,
304         .write = dynamic_ps_timeout_write,
305         .open = simple_open,
306         .llseek = default_llseek,
307 };
308
309 static ssize_t forced_ps_read(struct file *file, char __user *user_buf,
310                           size_t count, loff_t *ppos)
311 {
312         struct wl1271 *wl = file->private_data;
313
314         return wl1271_format_buffer(user_buf, count,
315                                     ppos, "%d\n",
316                                     wl->conf.conn.forced_ps);
317 }
318
319 static ssize_t forced_ps_write(struct file *file,
320                                     const char __user *user_buf,
321                                     size_t count, loff_t *ppos)
322 {
323         struct wl1271 *wl = file->private_data;
324         struct wl12xx_vif *wlvif;
325         unsigned long value;
326         int ret, ps_mode;
327
328         ret = kstrtoul_from_user(user_buf, count, 10, &value);
329         if (ret < 0) {
330                 wl1271_warning("illegal value in forced_ps");
331                 return -EINVAL;
332         }
333
334         if (value != 1 && value != 0) {
335                 wl1271_warning("forced_ps should be either 0 or 1");
336                 return -ERANGE;
337         }
338
339         mutex_lock(&wl->mutex);
340
341         if (wl->conf.conn.forced_ps == value)
342                 goto out;
343
344         wl->conf.conn.forced_ps = value;
345
346         if (wl->state == WL1271_STATE_OFF)
347                 goto out;
348
349         ret = wl1271_ps_elp_wakeup(wl);
350         if (ret < 0)
351                 goto out;
352
353         /* In case we're already in PSM, trigger it again to switch mode
354          * immediately without waiting for re-association
355          */
356
357         ps_mode = value ? STATION_POWER_SAVE_MODE : STATION_AUTO_PS_MODE;
358
359         wl12xx_for_each_wlvif_sta(wl, wlvif) {
360                 if (test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags))
361                         wl1271_ps_set_mode(wl, wlvif, ps_mode);
362         }
363
364         wl1271_ps_elp_sleep(wl);
365
366 out:
367         mutex_unlock(&wl->mutex);
368         return count;
369 }
370
371 static const struct file_operations forced_ps_ops = {
372         .read = forced_ps_read,
373         .write = forced_ps_write,
374         .open = simple_open,
375         .llseek = default_llseek,
376 };
377
378 static ssize_t split_scan_timeout_read(struct file *file, char __user *user_buf,
379                           size_t count, loff_t *ppos)
380 {
381         struct wl1271 *wl = file->private_data;
382
383         return wl1271_format_buffer(user_buf, count,
384                                     ppos, "%d\n",
385                                     wl->conf.scan.split_scan_timeout / 1000);
386 }
387
388 static ssize_t split_scan_timeout_write(struct file *file,
389                                     const char __user *user_buf,
390                                     size_t count, loff_t *ppos)
391 {
392         struct wl1271 *wl = file->private_data;
393         unsigned long value;
394         int ret;
395
396         ret = kstrtoul_from_user(user_buf, count, 10, &value);
397         if (ret < 0) {
398                 wl1271_warning("illegal value in split_scan_timeout");
399                 return -EINVAL;
400         }
401
402         if (value == 0)
403                 wl1271_info("split scan will be disabled");
404
405         mutex_lock(&wl->mutex);
406
407         wl->conf.scan.split_scan_timeout = value * 1000;
408
409         mutex_unlock(&wl->mutex);
410         return count;
411 }
412
413 static const struct file_operations split_scan_timeout_ops = {
414         .read = split_scan_timeout_read,
415         .write = split_scan_timeout_write,
416         .open = simple_open,
417         .llseek = default_llseek,
418 };
419
420 static ssize_t driver_state_read(struct file *file, char __user *user_buf,
421                                  size_t count, loff_t *ppos)
422 {
423         struct wl1271 *wl = file->private_data;
424         int res = 0;
425         ssize_t ret;
426         char *buf;
427
428 #define DRIVER_STATE_BUF_LEN 1024
429
430         buf = kmalloc(DRIVER_STATE_BUF_LEN, GFP_KERNEL);
431         if (!buf)
432                 return -ENOMEM;
433
434         mutex_lock(&wl->mutex);
435
436 #define DRIVER_STATE_PRINT(x, fmt)   \
437         (res += scnprintf(buf + res, DRIVER_STATE_BUF_LEN - res,\
438                           #x " = " fmt "\n", wl->x))
439
440 #define DRIVER_STATE_PRINT_LONG(x) DRIVER_STATE_PRINT(x, "%ld")
441 #define DRIVER_STATE_PRINT_INT(x)  DRIVER_STATE_PRINT(x, "%d")
442 #define DRIVER_STATE_PRINT_STR(x)  DRIVER_STATE_PRINT(x, "%s")
443 #define DRIVER_STATE_PRINT_LHEX(x) DRIVER_STATE_PRINT(x, "0x%lx")
444 #define DRIVER_STATE_PRINT_HEX(x)  DRIVER_STATE_PRINT(x, "0x%x")
445
446         DRIVER_STATE_PRINT_INT(tx_blocks_available);
447         DRIVER_STATE_PRINT_INT(tx_allocated_blocks);
448         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[0]);
449         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[1]);
450         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[2]);
451         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[3]);
452         DRIVER_STATE_PRINT_INT(tx_frames_cnt);
453         DRIVER_STATE_PRINT_LHEX(tx_frames_map[0]);
454         DRIVER_STATE_PRINT_INT(tx_queue_count[0]);
455         DRIVER_STATE_PRINT_INT(tx_queue_count[1]);
456         DRIVER_STATE_PRINT_INT(tx_queue_count[2]);
457         DRIVER_STATE_PRINT_INT(tx_queue_count[3]);
458         DRIVER_STATE_PRINT_INT(tx_packets_count);
459         DRIVER_STATE_PRINT_INT(tx_results_count);
460         DRIVER_STATE_PRINT_LHEX(flags);
461         DRIVER_STATE_PRINT_INT(tx_blocks_freed);
462         DRIVER_STATE_PRINT_INT(rx_counter);
463         DRIVER_STATE_PRINT_INT(state);
464         DRIVER_STATE_PRINT_INT(channel);
465         DRIVER_STATE_PRINT_INT(band);
466         DRIVER_STATE_PRINT_INT(power_level);
467         DRIVER_STATE_PRINT_INT(sg_enabled);
468         DRIVER_STATE_PRINT_INT(enable_11a);
469         DRIVER_STATE_PRINT_INT(noise);
470         DRIVER_STATE_PRINT_HEX(ap_fw_ps_map);
471         DRIVER_STATE_PRINT_LHEX(ap_ps_map);
472         DRIVER_STATE_PRINT_HEX(quirks);
473         DRIVER_STATE_PRINT_HEX(irq);
474         /* TODO: ref_clock and tcxo_clock were moved to wl12xx priv */
475         DRIVER_STATE_PRINT_HEX(hw_pg_ver);
476         DRIVER_STATE_PRINT_HEX(platform_quirks);
477         DRIVER_STATE_PRINT_HEX(chip.id);
478         DRIVER_STATE_PRINT_STR(chip.fw_ver_str);
479         DRIVER_STATE_PRINT_INT(sched_scanning);
480
481 #undef DRIVER_STATE_PRINT_INT
482 #undef DRIVER_STATE_PRINT_LONG
483 #undef DRIVER_STATE_PRINT_HEX
484 #undef DRIVER_STATE_PRINT_LHEX
485 #undef DRIVER_STATE_PRINT_STR
486 #undef DRIVER_STATE_PRINT
487 #undef DRIVER_STATE_BUF_LEN
488
489         mutex_unlock(&wl->mutex);
490
491         ret = simple_read_from_buffer(user_buf, count, ppos, buf, res);
492         kfree(buf);
493         return ret;
494 }
495
496 static const struct file_operations driver_state_ops = {
497         .read = driver_state_read,
498         .open = simple_open,
499         .llseek = default_llseek,
500 };
501
502 static ssize_t vifs_state_read(struct file *file, char __user *user_buf,
503                                  size_t count, loff_t *ppos)
504 {
505         struct wl1271 *wl = file->private_data;
506         struct wl12xx_vif *wlvif;
507         int ret, res = 0;
508         const int buf_size = 4096;
509         char *buf;
510         char tmp_buf[64];
511
512         buf = kzalloc(buf_size, GFP_KERNEL);
513         if (!buf)
514                 return -ENOMEM;
515
516         mutex_lock(&wl->mutex);
517
518 #define VIF_STATE_PRINT(x, fmt)                         \
519         (res += scnprintf(buf + res, buf_size - res,    \
520                           #x " = " fmt "\n", wlvif->x))
521
522 #define VIF_STATE_PRINT_LONG(x)  VIF_STATE_PRINT(x, "%ld")
523 #define VIF_STATE_PRINT_INT(x)   VIF_STATE_PRINT(x, "%d")
524 #define VIF_STATE_PRINT_STR(x)   VIF_STATE_PRINT(x, "%s")
525 #define VIF_STATE_PRINT_LHEX(x)  VIF_STATE_PRINT(x, "0x%lx")
526 #define VIF_STATE_PRINT_LLHEX(x) VIF_STATE_PRINT(x, "0x%llx")
527 #define VIF_STATE_PRINT_HEX(x)   VIF_STATE_PRINT(x, "0x%x")
528
529 #define VIF_STATE_PRINT_NSTR(x, len)                            \
530         do {                                                    \
531                 memset(tmp_buf, 0, sizeof(tmp_buf));            \
532                 memcpy(tmp_buf, wlvif->x,                       \
533                        min_t(u8, len, sizeof(tmp_buf) - 1));    \
534                 res += scnprintf(buf + res, buf_size - res,     \
535                                  #x " = %s\n", tmp_buf);        \
536         } while (0)
537
538         wl12xx_for_each_wlvif(wl, wlvif) {
539                 VIF_STATE_PRINT_INT(role_id);
540                 VIF_STATE_PRINT_INT(bss_type);
541                 VIF_STATE_PRINT_LHEX(flags);
542                 VIF_STATE_PRINT_INT(p2p);
543                 VIF_STATE_PRINT_INT(dev_role_id);
544                 VIF_STATE_PRINT_INT(dev_hlid);
545
546                 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
547                     wlvif->bss_type == BSS_TYPE_IBSS) {
548                         VIF_STATE_PRINT_INT(sta.hlid);
549                         VIF_STATE_PRINT_INT(sta.ba_rx_bitmap);
550                         VIF_STATE_PRINT_INT(sta.basic_rate_idx);
551                         VIF_STATE_PRINT_INT(sta.ap_rate_idx);
552                         VIF_STATE_PRINT_INT(sta.p2p_rate_idx);
553                         VIF_STATE_PRINT_INT(sta.qos);
554                 } else {
555                         VIF_STATE_PRINT_INT(ap.global_hlid);
556                         VIF_STATE_PRINT_INT(ap.bcast_hlid);
557                         VIF_STATE_PRINT_LHEX(ap.sta_hlid_map[0]);
558                         VIF_STATE_PRINT_INT(ap.mgmt_rate_idx);
559                         VIF_STATE_PRINT_INT(ap.bcast_rate_idx);
560                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[0]);
561                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[1]);
562                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[2]);
563                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[3]);
564                 }
565                 VIF_STATE_PRINT_INT(last_tx_hlid);
566                 VIF_STATE_PRINT_LHEX(links_map[0]);
567                 VIF_STATE_PRINT_NSTR(ssid, wlvif->ssid_len);
568                 VIF_STATE_PRINT_INT(band);
569                 VIF_STATE_PRINT_INT(channel);
570                 VIF_STATE_PRINT_HEX(bitrate_masks[0]);
571                 VIF_STATE_PRINT_HEX(bitrate_masks[1]);
572                 VIF_STATE_PRINT_HEX(basic_rate_set);
573                 VIF_STATE_PRINT_HEX(basic_rate);
574                 VIF_STATE_PRINT_HEX(rate_set);
575                 VIF_STATE_PRINT_INT(beacon_int);
576                 VIF_STATE_PRINT_INT(default_key);
577                 VIF_STATE_PRINT_INT(aid);
578                 VIF_STATE_PRINT_INT(session_counter);
579                 VIF_STATE_PRINT_INT(psm_entry_retry);
580                 VIF_STATE_PRINT_INT(power_level);
581                 VIF_STATE_PRINT_INT(rssi_thold);
582                 VIF_STATE_PRINT_INT(last_rssi_event);
583                 VIF_STATE_PRINT_INT(ba_support);
584                 VIF_STATE_PRINT_INT(ba_allowed);
585                 VIF_STATE_PRINT_LLHEX(tx_security_seq);
586                 VIF_STATE_PRINT_INT(tx_security_last_seq_lsb);
587         }
588
589 #undef VIF_STATE_PRINT_INT
590 #undef VIF_STATE_PRINT_LONG
591 #undef VIF_STATE_PRINT_HEX
592 #undef VIF_STATE_PRINT_LHEX
593 #undef VIF_STATE_PRINT_LLHEX
594 #undef VIF_STATE_PRINT_STR
595 #undef VIF_STATE_PRINT_NSTR
596 #undef VIF_STATE_PRINT
597
598         mutex_unlock(&wl->mutex);
599
600         ret = simple_read_from_buffer(user_buf, count, ppos, buf, res);
601         kfree(buf);
602         return ret;
603 }
604
605 static const struct file_operations vifs_state_ops = {
606         .read = vifs_state_read,
607         .open = simple_open,
608         .llseek = default_llseek,
609 };
610
611 static ssize_t dtim_interval_read(struct file *file, char __user *user_buf,
612                                   size_t count, loff_t *ppos)
613 {
614         struct wl1271 *wl = file->private_data;
615         u8 value;
616
617         if (wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_DTIM ||
618             wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_N_DTIM)
619                 value = wl->conf.conn.listen_interval;
620         else
621                 value = 0;
622
623         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
624 }
625
626 static ssize_t dtim_interval_write(struct file *file,
627                                    const char __user *user_buf,
628                                    size_t count, loff_t *ppos)
629 {
630         struct wl1271 *wl = file->private_data;
631         unsigned long value;
632         int ret;
633
634         ret = kstrtoul_from_user(user_buf, count, 10, &value);
635         if (ret < 0) {
636                 wl1271_warning("illegal value for dtim_interval");
637                 return -EINVAL;
638         }
639
640         if (value < 1 || value > 10) {
641                 wl1271_warning("dtim value is not in valid range");
642                 return -ERANGE;
643         }
644
645         mutex_lock(&wl->mutex);
646
647         wl->conf.conn.listen_interval = value;
648         /* for some reason there are different event types for 1 and >1 */
649         if (value == 1)
650                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_DTIM;
651         else
652                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM;
653
654         /*
655          * we don't reconfigure ACX_WAKE_UP_CONDITIONS now, so it will only
656          * take effect on the next time we enter psm.
657          */
658         mutex_unlock(&wl->mutex);
659         return count;
660 }
661
662 static const struct file_operations dtim_interval_ops = {
663         .read = dtim_interval_read,
664         .write = dtim_interval_write,
665         .open = simple_open,
666         .llseek = default_llseek,
667 };
668
669
670
671 static ssize_t suspend_dtim_interval_read(struct file *file,
672                                           char __user *user_buf,
673                                           size_t count, loff_t *ppos)
674 {
675         struct wl1271 *wl = file->private_data;
676         u8 value;
677
678         if (wl->conf.conn.suspend_wake_up_event == CONF_WAKE_UP_EVENT_DTIM ||
679             wl->conf.conn.suspend_wake_up_event == CONF_WAKE_UP_EVENT_N_DTIM)
680                 value = wl->conf.conn.suspend_listen_interval;
681         else
682                 value = 0;
683
684         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
685 }
686
687 static ssize_t suspend_dtim_interval_write(struct file *file,
688                                            const char __user *user_buf,
689                                            size_t count, loff_t *ppos)
690 {
691         struct wl1271 *wl = file->private_data;
692         unsigned long value;
693         int ret;
694
695         ret = kstrtoul_from_user(user_buf, count, 10, &value);
696         if (ret < 0) {
697                 wl1271_warning("illegal value for suspend_dtim_interval");
698                 return -EINVAL;
699         }
700
701         if (value < 1 || value > 10) {
702                 wl1271_warning("suspend_dtim value is not in valid range");
703                 return -ERANGE;
704         }
705
706         mutex_lock(&wl->mutex);
707
708         wl->conf.conn.suspend_listen_interval = value;
709         /* for some reason there are different event types for 1 and >1 */
710         if (value == 1)
711                 wl->conf.conn.suspend_wake_up_event = CONF_WAKE_UP_EVENT_DTIM;
712         else
713                 wl->conf.conn.suspend_wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM;
714
715         mutex_unlock(&wl->mutex);
716         return count;
717 }
718
719
720 static const struct file_operations suspend_dtim_interval_ops = {
721         .read = suspend_dtim_interval_read,
722         .write = suspend_dtim_interval_write,
723         .open = simple_open,
724         .llseek = default_llseek,
725 };
726
727 static ssize_t beacon_interval_read(struct file *file, char __user *user_buf,
728                                     size_t count, loff_t *ppos)
729 {
730         struct wl1271 *wl = file->private_data;
731         u8 value;
732
733         if (wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_BEACON ||
734             wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_N_BEACONS)
735                 value = wl->conf.conn.listen_interval;
736         else
737                 value = 0;
738
739         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
740 }
741
742 static ssize_t beacon_interval_write(struct file *file,
743                                      const char __user *user_buf,
744                                      size_t count, loff_t *ppos)
745 {
746         struct wl1271 *wl = file->private_data;
747         unsigned long value;
748         int ret;
749
750         ret = kstrtoul_from_user(user_buf, count, 10, &value);
751         if (ret < 0) {
752                 wl1271_warning("illegal value for beacon_interval");
753                 return -EINVAL;
754         }
755
756         if (value < 1 || value > 255) {
757                 wl1271_warning("beacon interval value is not in valid range");
758                 return -ERANGE;
759         }
760
761         mutex_lock(&wl->mutex);
762
763         wl->conf.conn.listen_interval = value;
764         /* for some reason there are different event types for 1 and >1 */
765         if (value == 1)
766                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_BEACON;
767         else
768                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_N_BEACONS;
769
770         /*
771          * we don't reconfigure ACX_WAKE_UP_CONDITIONS now, so it will only
772          * take effect on the next time we enter psm.
773          */
774         mutex_unlock(&wl->mutex);
775         return count;
776 }
777
778 static const struct file_operations beacon_interval_ops = {
779         .read = beacon_interval_read,
780         .write = beacon_interval_write,
781         .open = simple_open,
782         .llseek = default_llseek,
783 };
784
785 static ssize_t rx_streaming_interval_write(struct file *file,
786                            const char __user *user_buf,
787                            size_t count, loff_t *ppos)
788 {
789         struct wl1271 *wl = file->private_data;
790         struct wl12xx_vif *wlvif;
791         unsigned long value;
792         int ret;
793
794         ret = kstrtoul_from_user(user_buf, count, 10, &value);
795         if (ret < 0) {
796                 wl1271_warning("illegal value in rx_streaming_interval!");
797                 return -EINVAL;
798         }
799
800         /* valid values: 0, 10-100 */
801         if (value && (value < 10 || value > 100)) {
802                 wl1271_warning("value is not in range!");
803                 return -ERANGE;
804         }
805
806         mutex_lock(&wl->mutex);
807
808         wl->conf.rx_streaming.interval = value;
809
810         ret = wl1271_ps_elp_wakeup(wl);
811         if (ret < 0)
812                 goto out;
813
814         wl12xx_for_each_wlvif_sta(wl, wlvif) {
815                 wl1271_recalc_rx_streaming(wl, wlvif);
816         }
817
818         wl1271_ps_elp_sleep(wl);
819 out:
820         mutex_unlock(&wl->mutex);
821         return count;
822 }
823
824 static ssize_t rx_streaming_interval_read(struct file *file,
825                             char __user *userbuf,
826                             size_t count, loff_t *ppos)
827 {
828         struct wl1271 *wl = file->private_data;
829         return wl1271_format_buffer(userbuf, count, ppos,
830                                     "%d\n", wl->conf.rx_streaming.interval);
831 }
832
833 static const struct file_operations rx_streaming_interval_ops = {
834         .read = rx_streaming_interval_read,
835         .write = rx_streaming_interval_write,
836         .open = simple_open,
837         .llseek = default_llseek,
838 };
839
840 static ssize_t rx_streaming_always_write(struct file *file,
841                            const char __user *user_buf,
842                            size_t count, loff_t *ppos)
843 {
844         struct wl1271 *wl = file->private_data;
845         struct wl12xx_vif *wlvif;
846         unsigned long value;
847         int ret;
848
849         ret = kstrtoul_from_user(user_buf, count, 10, &value);
850         if (ret < 0) {
851                 wl1271_warning("illegal value in rx_streaming_write!");
852                 return -EINVAL;
853         }
854
855         /* valid values: 0, 10-100 */
856         if (!(value == 0 || value == 1)) {
857                 wl1271_warning("value is not in valid!");
858                 return -EINVAL;
859         }
860
861         mutex_lock(&wl->mutex);
862
863         wl->conf.rx_streaming.always = value;
864
865         ret = wl1271_ps_elp_wakeup(wl);
866         if (ret < 0)
867                 goto out;
868
869         wl12xx_for_each_wlvif_sta(wl, wlvif) {
870                 wl1271_recalc_rx_streaming(wl, wlvif);
871         }
872
873         wl1271_ps_elp_sleep(wl);
874 out:
875         mutex_unlock(&wl->mutex);
876         return count;
877 }
878
879 static ssize_t rx_streaming_always_read(struct file *file,
880                             char __user *userbuf,
881                             size_t count, loff_t *ppos)
882 {
883         struct wl1271 *wl = file->private_data;
884         return wl1271_format_buffer(userbuf, count, ppos,
885                                     "%d\n", wl->conf.rx_streaming.always);
886 }
887
888 static const struct file_operations rx_streaming_always_ops = {
889         .read = rx_streaming_always_read,
890         .write = rx_streaming_always_write,
891         .open = simple_open,
892         .llseek = default_llseek,
893 };
894
895 static ssize_t beacon_filtering_write(struct file *file,
896                                       const char __user *user_buf,
897                                       size_t count, loff_t *ppos)
898 {
899         struct wl1271 *wl = file->private_data;
900         struct wl12xx_vif *wlvif;
901         char buf[10];
902         size_t len;
903         unsigned long value;
904         int ret;
905
906         len = min(count, sizeof(buf) - 1);
907         if (copy_from_user(buf, user_buf, len))
908                 return -EFAULT;
909         buf[len] = '\0';
910
911         ret = kstrtoul(buf, 0, &value);
912         if (ret < 0) {
913                 wl1271_warning("illegal value for beacon_filtering!");
914                 return -EINVAL;
915         }
916
917         mutex_lock(&wl->mutex);
918
919         ret = wl1271_ps_elp_wakeup(wl);
920         if (ret < 0)
921                 goto out;
922
923         wl12xx_for_each_wlvif(wl, wlvif) {
924                 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, !!value);
925         }
926
927         wl1271_ps_elp_sleep(wl);
928 out:
929         mutex_unlock(&wl->mutex);
930         return count;
931 }
932
933 static const struct file_operations beacon_filtering_ops = {
934         .write = beacon_filtering_write,
935         .open = simple_open,
936         .llseek = default_llseek,
937 };
938
939 static int wl1271_debugfs_add_files(struct wl1271 *wl,
940                                     struct dentry *rootdir)
941 {
942         int ret = 0;
943         struct dentry *entry, *streaming;
944
945         DEBUGFS_ADD(tx_queue_len, rootdir);
946         DEBUGFS_ADD(retry_count, rootdir);
947         DEBUGFS_ADD(excessive_retries, rootdir);
948
949         DEBUGFS_ADD(gpio_power, rootdir);
950         DEBUGFS_ADD(start_recovery, rootdir);
951         DEBUGFS_ADD(driver_state, rootdir);
952         DEBUGFS_ADD(vifs_state, rootdir);
953         DEBUGFS_ADD(dtim_interval, rootdir);
954         DEBUGFS_ADD(suspend_dtim_interval, rootdir);
955         DEBUGFS_ADD(beacon_interval, rootdir);
956         DEBUGFS_ADD(beacon_filtering, rootdir);
957         DEBUGFS_ADD(dynamic_ps_timeout, rootdir);
958         DEBUGFS_ADD(forced_ps, rootdir);
959         DEBUGFS_ADD(split_scan_timeout, rootdir);
960
961         streaming = debugfs_create_dir("rx_streaming", rootdir);
962         if (!streaming || IS_ERR(streaming))
963                 goto err;
964
965         DEBUGFS_ADD_PREFIX(rx_streaming, interval, streaming);
966         DEBUGFS_ADD_PREFIX(rx_streaming, always, streaming);
967
968
969         return 0;
970
971 err:
972         if (IS_ERR(entry))
973                 ret = PTR_ERR(entry);
974         else
975                 ret = -ENOMEM;
976
977         return ret;
978 }
979
980 void wl1271_debugfs_reset(struct wl1271 *wl)
981 {
982         if (!wl->stats.fw_stats)
983                 return;
984
985         memset(wl->stats.fw_stats, 0, wl->stats.fw_stats_len);
986         wl->stats.retry_count = 0;
987         wl->stats.excessive_retries = 0;
988 }
989
990 int wl1271_debugfs_init(struct wl1271 *wl)
991 {
992         int ret;
993         struct dentry *rootdir;
994
995         rootdir = debugfs_create_dir(KBUILD_MODNAME,
996                                      wl->hw->wiphy->debugfsdir);
997
998         if (IS_ERR(rootdir)) {
999                 ret = PTR_ERR(rootdir);
1000                 goto out;
1001         }
1002
1003         wl->stats.fw_stats = kzalloc(wl->stats.fw_stats_len, GFP_KERNEL);
1004         if (!wl->stats.fw_stats) {
1005                 ret = -ENOMEM;
1006                 goto out_remove;
1007         }
1008
1009         wl->stats.fw_stats_update = jiffies;
1010
1011         ret = wl1271_debugfs_add_files(wl, rootdir);
1012         if (ret < 0)
1013                 goto out_exit;
1014
1015         ret = wlcore_debugfs_init(wl, rootdir);
1016         if (ret < 0)
1017                 goto out_exit;
1018
1019         goto out;
1020
1021 out_exit:
1022         wl1271_debugfs_exit(wl);
1023
1024 out_remove:
1025         debugfs_remove_recursive(rootdir);
1026
1027 out:
1028         return ret;
1029 }
1030
1031 void wl1271_debugfs_exit(struct wl1271 *wl)
1032 {
1033         kfree(wl->stats.fw_stats);
1034         wl->stats.fw_stats = NULL;
1035 }