827a728fd6636eedb94202e4a12d7fe5af706da4
[firefly-linux-kernel-4.4.55.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
181 WL12XX_CONF_DEBUGFS(irq_pkt_threshold, rx, 0, 65535,
182                     chip_op_handler, wl1271_acx_init_rx_interrupt)
183 WL12XX_CONF_DEBUGFS(irq_blk_threshold, rx, 0, 65535,
184                     chip_op_handler, wl1271_acx_init_rx_interrupt)
185 WL12XX_CONF_DEBUGFS(irq_timeout, rx, 0, 100,
186                     chip_op_handler, wl1271_acx_init_rx_interrupt)
187
188 static ssize_t gpio_power_read(struct file *file, char __user *user_buf,
189                           size_t count, loff_t *ppos)
190 {
191         struct wl1271 *wl = file->private_data;
192         bool state = test_bit(WL1271_FLAG_GPIO_POWER, &wl->flags);
193
194         int res;
195         char buf[10];
196
197         res = scnprintf(buf, sizeof(buf), "%d\n", state);
198
199         return simple_read_from_buffer(user_buf, count, ppos, buf, res);
200 }
201
202 static ssize_t gpio_power_write(struct file *file,
203                            const char __user *user_buf,
204                            size_t count, loff_t *ppos)
205 {
206         struct wl1271 *wl = file->private_data;
207         unsigned long value;
208         int ret;
209
210         ret = kstrtoul_from_user(user_buf, count, 10, &value);
211         if (ret < 0) {
212                 wl1271_warning("illegal value in gpio_power");
213                 return -EINVAL;
214         }
215
216         mutex_lock(&wl->mutex);
217
218         if (value)
219                 wl1271_power_on(wl);
220         else
221                 wl1271_power_off(wl);
222
223         mutex_unlock(&wl->mutex);
224         return count;
225 }
226
227 static const struct file_operations gpio_power_ops = {
228         .read = gpio_power_read,
229         .write = gpio_power_write,
230         .open = simple_open,
231         .llseek = default_llseek,
232 };
233
234 static ssize_t start_recovery_write(struct file *file,
235                                     const char __user *user_buf,
236                                     size_t count, loff_t *ppos)
237 {
238         struct wl1271 *wl = file->private_data;
239
240         mutex_lock(&wl->mutex);
241         wl12xx_queue_recovery_work(wl);
242         mutex_unlock(&wl->mutex);
243
244         return count;
245 }
246
247 static const struct file_operations start_recovery_ops = {
248         .write = start_recovery_write,
249         .open = simple_open,
250         .llseek = default_llseek,
251 };
252
253 static ssize_t dynamic_ps_timeout_read(struct file *file, char __user *user_buf,
254                           size_t count, loff_t *ppos)
255 {
256         struct wl1271 *wl = file->private_data;
257
258         return wl1271_format_buffer(user_buf, count,
259                                     ppos, "%d\n",
260                                     wl->conf.conn.dynamic_ps_timeout);
261 }
262
263 static ssize_t dynamic_ps_timeout_write(struct file *file,
264                                     const char __user *user_buf,
265                                     size_t count, loff_t *ppos)
266 {
267         struct wl1271 *wl = file->private_data;
268         struct wl12xx_vif *wlvif;
269         unsigned long value;
270         int ret;
271
272         ret = kstrtoul_from_user(user_buf, count, 10, &value);
273         if (ret < 0) {
274                 wl1271_warning("illegal value in dynamic_ps");
275                 return -EINVAL;
276         }
277
278         if (value < 1 || value > 65535) {
279                 wl1271_warning("dyanmic_ps_timeout is not in valid range");
280                 return -ERANGE;
281         }
282
283         mutex_lock(&wl->mutex);
284
285         wl->conf.conn.dynamic_ps_timeout = value;
286
287         if (wl->state == WL1271_STATE_OFF)
288                 goto out;
289
290         ret = wl1271_ps_elp_wakeup(wl);
291         if (ret < 0)
292                 goto out;
293
294         /* In case we're already in PSM, trigger it again to set new timeout
295          * immediately without waiting for re-association
296          */
297
298         wl12xx_for_each_wlvif_sta(wl, wlvif) {
299                 if (test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags))
300                         wl1271_ps_set_mode(wl, wlvif, STATION_AUTO_PS_MODE);
301         }
302
303         wl1271_ps_elp_sleep(wl);
304
305 out:
306         mutex_unlock(&wl->mutex);
307         return count;
308 }
309
310 static const struct file_operations dynamic_ps_timeout_ops = {
311         .read = dynamic_ps_timeout_read,
312         .write = dynamic_ps_timeout_write,
313         .open = simple_open,
314         .llseek = default_llseek,
315 };
316
317 static ssize_t forced_ps_read(struct file *file, char __user *user_buf,
318                           size_t count, loff_t *ppos)
319 {
320         struct wl1271 *wl = file->private_data;
321
322         return wl1271_format_buffer(user_buf, count,
323                                     ppos, "%d\n",
324                                     wl->conf.conn.forced_ps);
325 }
326
327 static ssize_t forced_ps_write(struct file *file,
328                                     const char __user *user_buf,
329                                     size_t count, loff_t *ppos)
330 {
331         struct wl1271 *wl = file->private_data;
332         struct wl12xx_vif *wlvif;
333         unsigned long value;
334         int ret, ps_mode;
335
336         ret = kstrtoul_from_user(user_buf, count, 10, &value);
337         if (ret < 0) {
338                 wl1271_warning("illegal value in forced_ps");
339                 return -EINVAL;
340         }
341
342         if (value != 1 && value != 0) {
343                 wl1271_warning("forced_ps should be either 0 or 1");
344                 return -ERANGE;
345         }
346
347         mutex_lock(&wl->mutex);
348
349         if (wl->conf.conn.forced_ps == value)
350                 goto out;
351
352         wl->conf.conn.forced_ps = value;
353
354         if (wl->state == WL1271_STATE_OFF)
355                 goto out;
356
357         ret = wl1271_ps_elp_wakeup(wl);
358         if (ret < 0)
359                 goto out;
360
361         /* In case we're already in PSM, trigger it again to switch mode
362          * immediately without waiting for re-association
363          */
364
365         ps_mode = value ? STATION_POWER_SAVE_MODE : STATION_AUTO_PS_MODE;
366
367         wl12xx_for_each_wlvif_sta(wl, wlvif) {
368                 if (test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags))
369                         wl1271_ps_set_mode(wl, wlvif, ps_mode);
370         }
371
372         wl1271_ps_elp_sleep(wl);
373
374 out:
375         mutex_unlock(&wl->mutex);
376         return count;
377 }
378
379 static const struct file_operations forced_ps_ops = {
380         .read = forced_ps_read,
381         .write = forced_ps_write,
382         .open = simple_open,
383         .llseek = default_llseek,
384 };
385
386 static ssize_t split_scan_timeout_read(struct file *file, char __user *user_buf,
387                           size_t count, loff_t *ppos)
388 {
389         struct wl1271 *wl = file->private_data;
390
391         return wl1271_format_buffer(user_buf, count,
392                                     ppos, "%d\n",
393                                     wl->conf.scan.split_scan_timeout / 1000);
394 }
395
396 static ssize_t split_scan_timeout_write(struct file *file,
397                                     const char __user *user_buf,
398                                     size_t count, loff_t *ppos)
399 {
400         struct wl1271 *wl = file->private_data;
401         unsigned long value;
402         int ret;
403
404         ret = kstrtoul_from_user(user_buf, count, 10, &value);
405         if (ret < 0) {
406                 wl1271_warning("illegal value in split_scan_timeout");
407                 return -EINVAL;
408         }
409
410         if (value == 0)
411                 wl1271_info("split scan will be disabled");
412
413         mutex_lock(&wl->mutex);
414
415         wl->conf.scan.split_scan_timeout = value * 1000;
416
417         mutex_unlock(&wl->mutex);
418         return count;
419 }
420
421 static const struct file_operations split_scan_timeout_ops = {
422         .read = split_scan_timeout_read,
423         .write = split_scan_timeout_write,
424         .open = simple_open,
425         .llseek = default_llseek,
426 };
427
428 static ssize_t driver_state_read(struct file *file, char __user *user_buf,
429                                  size_t count, loff_t *ppos)
430 {
431         struct wl1271 *wl = file->private_data;
432         int res = 0;
433         ssize_t ret;
434         char *buf;
435
436 #define DRIVER_STATE_BUF_LEN 1024
437
438         buf = kmalloc(DRIVER_STATE_BUF_LEN, GFP_KERNEL);
439         if (!buf)
440                 return -ENOMEM;
441
442         mutex_lock(&wl->mutex);
443
444 #define DRIVER_STATE_PRINT(x, fmt)   \
445         (res += scnprintf(buf + res, DRIVER_STATE_BUF_LEN - res,\
446                           #x " = " fmt "\n", wl->x))
447
448 #define DRIVER_STATE_PRINT_LONG(x) DRIVER_STATE_PRINT(x, "%ld")
449 #define DRIVER_STATE_PRINT_INT(x)  DRIVER_STATE_PRINT(x, "%d")
450 #define DRIVER_STATE_PRINT_STR(x)  DRIVER_STATE_PRINT(x, "%s")
451 #define DRIVER_STATE_PRINT_LHEX(x) DRIVER_STATE_PRINT(x, "0x%lx")
452 #define DRIVER_STATE_PRINT_HEX(x)  DRIVER_STATE_PRINT(x, "0x%x")
453
454         DRIVER_STATE_PRINT_INT(tx_blocks_available);
455         DRIVER_STATE_PRINT_INT(tx_allocated_blocks);
456         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[0]);
457         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[1]);
458         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[2]);
459         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[3]);
460         DRIVER_STATE_PRINT_INT(tx_frames_cnt);
461         DRIVER_STATE_PRINT_LHEX(tx_frames_map[0]);
462         DRIVER_STATE_PRINT_INT(tx_queue_count[0]);
463         DRIVER_STATE_PRINT_INT(tx_queue_count[1]);
464         DRIVER_STATE_PRINT_INT(tx_queue_count[2]);
465         DRIVER_STATE_PRINT_INT(tx_queue_count[3]);
466         DRIVER_STATE_PRINT_INT(tx_packets_count);
467         DRIVER_STATE_PRINT_INT(tx_results_count);
468         DRIVER_STATE_PRINT_LHEX(flags);
469         DRIVER_STATE_PRINT_INT(tx_blocks_freed);
470         DRIVER_STATE_PRINT_INT(rx_counter);
471         DRIVER_STATE_PRINT_INT(state);
472         DRIVER_STATE_PRINT_INT(channel);
473         DRIVER_STATE_PRINT_INT(band);
474         DRIVER_STATE_PRINT_INT(power_level);
475         DRIVER_STATE_PRINT_INT(sg_enabled);
476         DRIVER_STATE_PRINT_INT(enable_11a);
477         DRIVER_STATE_PRINT_INT(noise);
478         DRIVER_STATE_PRINT_HEX(ap_fw_ps_map);
479         DRIVER_STATE_PRINT_LHEX(ap_ps_map);
480         DRIVER_STATE_PRINT_HEX(quirks);
481         DRIVER_STATE_PRINT_HEX(irq);
482         /* TODO: ref_clock and tcxo_clock were moved to wl12xx priv */
483         DRIVER_STATE_PRINT_HEX(hw_pg_ver);
484         DRIVER_STATE_PRINT_HEX(platform_quirks);
485         DRIVER_STATE_PRINT_HEX(chip.id);
486         DRIVER_STATE_PRINT_STR(chip.fw_ver_str);
487         DRIVER_STATE_PRINT_INT(sched_scanning);
488
489 #undef DRIVER_STATE_PRINT_INT
490 #undef DRIVER_STATE_PRINT_LONG
491 #undef DRIVER_STATE_PRINT_HEX
492 #undef DRIVER_STATE_PRINT_LHEX
493 #undef DRIVER_STATE_PRINT_STR
494 #undef DRIVER_STATE_PRINT
495 #undef DRIVER_STATE_BUF_LEN
496
497         mutex_unlock(&wl->mutex);
498
499         ret = simple_read_from_buffer(user_buf, count, ppos, buf, res);
500         kfree(buf);
501         return ret;
502 }
503
504 static const struct file_operations driver_state_ops = {
505         .read = driver_state_read,
506         .open = simple_open,
507         .llseek = default_llseek,
508 };
509
510 static ssize_t vifs_state_read(struct file *file, char __user *user_buf,
511                                  size_t count, loff_t *ppos)
512 {
513         struct wl1271 *wl = file->private_data;
514         struct wl12xx_vif *wlvif;
515         int ret, res = 0;
516         const int buf_size = 4096;
517         char *buf;
518         char tmp_buf[64];
519
520         buf = kzalloc(buf_size, GFP_KERNEL);
521         if (!buf)
522                 return -ENOMEM;
523
524         mutex_lock(&wl->mutex);
525
526 #define VIF_STATE_PRINT(x, fmt)                         \
527         (res += scnprintf(buf + res, buf_size - res,    \
528                           #x " = " fmt "\n", wlvif->x))
529
530 #define VIF_STATE_PRINT_LONG(x)  VIF_STATE_PRINT(x, "%ld")
531 #define VIF_STATE_PRINT_INT(x)   VIF_STATE_PRINT(x, "%d")
532 #define VIF_STATE_PRINT_STR(x)   VIF_STATE_PRINT(x, "%s")
533 #define VIF_STATE_PRINT_LHEX(x)  VIF_STATE_PRINT(x, "0x%lx")
534 #define VIF_STATE_PRINT_LLHEX(x) VIF_STATE_PRINT(x, "0x%llx")
535 #define VIF_STATE_PRINT_HEX(x)   VIF_STATE_PRINT(x, "0x%x")
536
537 #define VIF_STATE_PRINT_NSTR(x, len)                            \
538         do {                                                    \
539                 memset(tmp_buf, 0, sizeof(tmp_buf));            \
540                 memcpy(tmp_buf, wlvif->x,                       \
541                        min_t(u8, len, sizeof(tmp_buf) - 1));    \
542                 res += scnprintf(buf + res, buf_size - res,     \
543                                  #x " = %s\n", tmp_buf);        \
544         } while (0)
545
546         wl12xx_for_each_wlvif(wl, wlvif) {
547                 VIF_STATE_PRINT_INT(role_id);
548                 VIF_STATE_PRINT_INT(bss_type);
549                 VIF_STATE_PRINT_LHEX(flags);
550                 VIF_STATE_PRINT_INT(p2p);
551                 VIF_STATE_PRINT_INT(dev_role_id);
552                 VIF_STATE_PRINT_INT(dev_hlid);
553
554                 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
555                     wlvif->bss_type == BSS_TYPE_IBSS) {
556                         VIF_STATE_PRINT_INT(sta.hlid);
557                         VIF_STATE_PRINT_INT(sta.ba_rx_bitmap);
558                         VIF_STATE_PRINT_INT(sta.basic_rate_idx);
559                         VIF_STATE_PRINT_INT(sta.ap_rate_idx);
560                         VIF_STATE_PRINT_INT(sta.p2p_rate_idx);
561                         VIF_STATE_PRINT_INT(sta.qos);
562                 } else {
563                         VIF_STATE_PRINT_INT(ap.global_hlid);
564                         VIF_STATE_PRINT_INT(ap.bcast_hlid);
565                         VIF_STATE_PRINT_LHEX(ap.sta_hlid_map[0]);
566                         VIF_STATE_PRINT_INT(ap.mgmt_rate_idx);
567                         VIF_STATE_PRINT_INT(ap.bcast_rate_idx);
568                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[0]);
569                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[1]);
570                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[2]);
571                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[3]);
572                 }
573                 VIF_STATE_PRINT_INT(last_tx_hlid);
574                 VIF_STATE_PRINT_LHEX(links_map[0]);
575                 VIF_STATE_PRINT_NSTR(ssid, wlvif->ssid_len);
576                 VIF_STATE_PRINT_INT(band);
577                 VIF_STATE_PRINT_INT(channel);
578                 VIF_STATE_PRINT_HEX(bitrate_masks[0]);
579                 VIF_STATE_PRINT_HEX(bitrate_masks[1]);
580                 VIF_STATE_PRINT_HEX(basic_rate_set);
581                 VIF_STATE_PRINT_HEX(basic_rate);
582                 VIF_STATE_PRINT_HEX(rate_set);
583                 VIF_STATE_PRINT_INT(beacon_int);
584                 VIF_STATE_PRINT_INT(default_key);
585                 VIF_STATE_PRINT_INT(aid);
586                 VIF_STATE_PRINT_INT(session_counter);
587                 VIF_STATE_PRINT_INT(psm_entry_retry);
588                 VIF_STATE_PRINT_INT(power_level);
589                 VIF_STATE_PRINT_INT(rssi_thold);
590                 VIF_STATE_PRINT_INT(last_rssi_event);
591                 VIF_STATE_PRINT_INT(ba_support);
592                 VIF_STATE_PRINT_INT(ba_allowed);
593                 VIF_STATE_PRINT_LLHEX(tx_security_seq);
594                 VIF_STATE_PRINT_INT(tx_security_last_seq_lsb);
595         }
596
597 #undef VIF_STATE_PRINT_INT
598 #undef VIF_STATE_PRINT_LONG
599 #undef VIF_STATE_PRINT_HEX
600 #undef VIF_STATE_PRINT_LHEX
601 #undef VIF_STATE_PRINT_LLHEX
602 #undef VIF_STATE_PRINT_STR
603 #undef VIF_STATE_PRINT_NSTR
604 #undef VIF_STATE_PRINT
605
606         mutex_unlock(&wl->mutex);
607
608         ret = simple_read_from_buffer(user_buf, count, ppos, buf, res);
609         kfree(buf);
610         return ret;
611 }
612
613 static const struct file_operations vifs_state_ops = {
614         .read = vifs_state_read,
615         .open = simple_open,
616         .llseek = default_llseek,
617 };
618
619 static ssize_t dtim_interval_read(struct file *file, char __user *user_buf,
620                                   size_t count, loff_t *ppos)
621 {
622         struct wl1271 *wl = file->private_data;
623         u8 value;
624
625         if (wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_DTIM ||
626             wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_N_DTIM)
627                 value = wl->conf.conn.listen_interval;
628         else
629                 value = 0;
630
631         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
632 }
633
634 static ssize_t dtim_interval_write(struct file *file,
635                                    const char __user *user_buf,
636                                    size_t count, loff_t *ppos)
637 {
638         struct wl1271 *wl = file->private_data;
639         unsigned long value;
640         int ret;
641
642         ret = kstrtoul_from_user(user_buf, count, 10, &value);
643         if (ret < 0) {
644                 wl1271_warning("illegal value for dtim_interval");
645                 return -EINVAL;
646         }
647
648         if (value < 1 || value > 10) {
649                 wl1271_warning("dtim value is not in valid range");
650                 return -ERANGE;
651         }
652
653         mutex_lock(&wl->mutex);
654
655         wl->conf.conn.listen_interval = value;
656         /* for some reason there are different event types for 1 and >1 */
657         if (value == 1)
658                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_DTIM;
659         else
660                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM;
661
662         /*
663          * we don't reconfigure ACX_WAKE_UP_CONDITIONS now, so it will only
664          * take effect on the next time we enter psm.
665          */
666         mutex_unlock(&wl->mutex);
667         return count;
668 }
669
670 static const struct file_operations dtim_interval_ops = {
671         .read = dtim_interval_read,
672         .write = dtim_interval_write,
673         .open = simple_open,
674         .llseek = default_llseek,
675 };
676
677
678
679 static ssize_t suspend_dtim_interval_read(struct file *file,
680                                           char __user *user_buf,
681                                           size_t count, loff_t *ppos)
682 {
683         struct wl1271 *wl = file->private_data;
684         u8 value;
685
686         if (wl->conf.conn.suspend_wake_up_event == CONF_WAKE_UP_EVENT_DTIM ||
687             wl->conf.conn.suspend_wake_up_event == CONF_WAKE_UP_EVENT_N_DTIM)
688                 value = wl->conf.conn.suspend_listen_interval;
689         else
690                 value = 0;
691
692         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
693 }
694
695 static ssize_t suspend_dtim_interval_write(struct file *file,
696                                            const char __user *user_buf,
697                                            size_t count, loff_t *ppos)
698 {
699         struct wl1271 *wl = file->private_data;
700         unsigned long value;
701         int ret;
702
703         ret = kstrtoul_from_user(user_buf, count, 10, &value);
704         if (ret < 0) {
705                 wl1271_warning("illegal value for suspend_dtim_interval");
706                 return -EINVAL;
707         }
708
709         if (value < 1 || value > 10) {
710                 wl1271_warning("suspend_dtim value is not in valid range");
711                 return -ERANGE;
712         }
713
714         mutex_lock(&wl->mutex);
715
716         wl->conf.conn.suspend_listen_interval = value;
717         /* for some reason there are different event types for 1 and >1 */
718         if (value == 1)
719                 wl->conf.conn.suspend_wake_up_event = CONF_WAKE_UP_EVENT_DTIM;
720         else
721                 wl->conf.conn.suspend_wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM;
722
723         mutex_unlock(&wl->mutex);
724         return count;
725 }
726
727
728 static const struct file_operations suspend_dtim_interval_ops = {
729         .read = suspend_dtim_interval_read,
730         .write = suspend_dtim_interval_write,
731         .open = simple_open,
732         .llseek = default_llseek,
733 };
734
735 static ssize_t beacon_interval_read(struct file *file, char __user *user_buf,
736                                     size_t count, loff_t *ppos)
737 {
738         struct wl1271 *wl = file->private_data;
739         u8 value;
740
741         if (wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_BEACON ||
742             wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_N_BEACONS)
743                 value = wl->conf.conn.listen_interval;
744         else
745                 value = 0;
746
747         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
748 }
749
750 static ssize_t beacon_interval_write(struct file *file,
751                                      const char __user *user_buf,
752                                      size_t count, loff_t *ppos)
753 {
754         struct wl1271 *wl = file->private_data;
755         unsigned long value;
756         int ret;
757
758         ret = kstrtoul_from_user(user_buf, count, 10, &value);
759         if (ret < 0) {
760                 wl1271_warning("illegal value for beacon_interval");
761                 return -EINVAL;
762         }
763
764         if (value < 1 || value > 255) {
765                 wl1271_warning("beacon interval value is not in valid range");
766                 return -ERANGE;
767         }
768
769         mutex_lock(&wl->mutex);
770
771         wl->conf.conn.listen_interval = value;
772         /* for some reason there are different event types for 1 and >1 */
773         if (value == 1)
774                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_BEACON;
775         else
776                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_N_BEACONS;
777
778         /*
779          * we don't reconfigure ACX_WAKE_UP_CONDITIONS now, so it will only
780          * take effect on the next time we enter psm.
781          */
782         mutex_unlock(&wl->mutex);
783         return count;
784 }
785
786 static const struct file_operations beacon_interval_ops = {
787         .read = beacon_interval_read,
788         .write = beacon_interval_write,
789         .open = simple_open,
790         .llseek = default_llseek,
791 };
792
793 static ssize_t rx_streaming_interval_write(struct file *file,
794                            const char __user *user_buf,
795                            size_t count, loff_t *ppos)
796 {
797         struct wl1271 *wl = file->private_data;
798         struct wl12xx_vif *wlvif;
799         unsigned long value;
800         int ret;
801
802         ret = kstrtoul_from_user(user_buf, count, 10, &value);
803         if (ret < 0) {
804                 wl1271_warning("illegal value in rx_streaming_interval!");
805                 return -EINVAL;
806         }
807
808         /* valid values: 0, 10-100 */
809         if (value && (value < 10 || value > 100)) {
810                 wl1271_warning("value is not in range!");
811                 return -ERANGE;
812         }
813
814         mutex_lock(&wl->mutex);
815
816         wl->conf.rx_streaming.interval = value;
817
818         ret = wl1271_ps_elp_wakeup(wl);
819         if (ret < 0)
820                 goto out;
821
822         wl12xx_for_each_wlvif_sta(wl, wlvif) {
823                 wl1271_recalc_rx_streaming(wl, wlvif);
824         }
825
826         wl1271_ps_elp_sleep(wl);
827 out:
828         mutex_unlock(&wl->mutex);
829         return count;
830 }
831
832 static ssize_t rx_streaming_interval_read(struct file *file,
833                             char __user *userbuf,
834                             size_t count, loff_t *ppos)
835 {
836         struct wl1271 *wl = file->private_data;
837         return wl1271_format_buffer(userbuf, count, ppos,
838                                     "%d\n", wl->conf.rx_streaming.interval);
839 }
840
841 static const struct file_operations rx_streaming_interval_ops = {
842         .read = rx_streaming_interval_read,
843         .write = rx_streaming_interval_write,
844         .open = simple_open,
845         .llseek = default_llseek,
846 };
847
848 static ssize_t rx_streaming_always_write(struct file *file,
849                            const char __user *user_buf,
850                            size_t count, loff_t *ppos)
851 {
852         struct wl1271 *wl = file->private_data;
853         struct wl12xx_vif *wlvif;
854         unsigned long value;
855         int ret;
856
857         ret = kstrtoul_from_user(user_buf, count, 10, &value);
858         if (ret < 0) {
859                 wl1271_warning("illegal value in rx_streaming_write!");
860                 return -EINVAL;
861         }
862
863         /* valid values: 0, 10-100 */
864         if (!(value == 0 || value == 1)) {
865                 wl1271_warning("value is not in valid!");
866                 return -EINVAL;
867         }
868
869         mutex_lock(&wl->mutex);
870
871         wl->conf.rx_streaming.always = value;
872
873         ret = wl1271_ps_elp_wakeup(wl);
874         if (ret < 0)
875                 goto out;
876
877         wl12xx_for_each_wlvif_sta(wl, wlvif) {
878                 wl1271_recalc_rx_streaming(wl, wlvif);
879         }
880
881         wl1271_ps_elp_sleep(wl);
882 out:
883         mutex_unlock(&wl->mutex);
884         return count;
885 }
886
887 static ssize_t rx_streaming_always_read(struct file *file,
888                             char __user *userbuf,
889                             size_t count, loff_t *ppos)
890 {
891         struct wl1271 *wl = file->private_data;
892         return wl1271_format_buffer(userbuf, count, ppos,
893                                     "%d\n", wl->conf.rx_streaming.always);
894 }
895
896 static const struct file_operations rx_streaming_always_ops = {
897         .read = rx_streaming_always_read,
898         .write = rx_streaming_always_write,
899         .open = simple_open,
900         .llseek = default_llseek,
901 };
902
903 static ssize_t beacon_filtering_write(struct file *file,
904                                       const char __user *user_buf,
905                                       size_t count, loff_t *ppos)
906 {
907         struct wl1271 *wl = file->private_data;
908         struct wl12xx_vif *wlvif;
909         char buf[10];
910         size_t len;
911         unsigned long value;
912         int ret;
913
914         len = min(count, sizeof(buf) - 1);
915         if (copy_from_user(buf, user_buf, len))
916                 return -EFAULT;
917         buf[len] = '\0';
918
919         ret = kstrtoul(buf, 0, &value);
920         if (ret < 0) {
921                 wl1271_warning("illegal value for beacon_filtering!");
922                 return -EINVAL;
923         }
924
925         mutex_lock(&wl->mutex);
926
927         ret = wl1271_ps_elp_wakeup(wl);
928         if (ret < 0)
929                 goto out;
930
931         wl12xx_for_each_wlvif(wl, wlvif) {
932                 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, !!value);
933         }
934
935         wl1271_ps_elp_sleep(wl);
936 out:
937         mutex_unlock(&wl->mutex);
938         return count;
939 }
940
941 static const struct file_operations beacon_filtering_ops = {
942         .write = beacon_filtering_write,
943         .open = simple_open,
944         .llseek = default_llseek,
945 };
946
947 static int wl1271_debugfs_add_files(struct wl1271 *wl,
948                                     struct dentry *rootdir)
949 {
950         int ret = 0;
951         struct dentry *entry, *streaming;
952
953         DEBUGFS_ADD(tx_queue_len, rootdir);
954         DEBUGFS_ADD(retry_count, rootdir);
955         DEBUGFS_ADD(excessive_retries, rootdir);
956
957         DEBUGFS_ADD(gpio_power, rootdir);
958         DEBUGFS_ADD(start_recovery, rootdir);
959         DEBUGFS_ADD(driver_state, rootdir);
960         DEBUGFS_ADD(vifs_state, rootdir);
961         DEBUGFS_ADD(dtim_interval, rootdir);
962         DEBUGFS_ADD(suspend_dtim_interval, rootdir);
963         DEBUGFS_ADD(beacon_interval, rootdir);
964         DEBUGFS_ADD(beacon_filtering, rootdir);
965         DEBUGFS_ADD(dynamic_ps_timeout, rootdir);
966         DEBUGFS_ADD(forced_ps, rootdir);
967         DEBUGFS_ADD(split_scan_timeout, rootdir);
968         DEBUGFS_ADD(irq_pkt_threshold, rootdir);
969         DEBUGFS_ADD(irq_blk_threshold, rootdir);
970         DEBUGFS_ADD(irq_timeout, rootdir);
971
972         streaming = debugfs_create_dir("rx_streaming", rootdir);
973         if (!streaming || IS_ERR(streaming))
974                 goto err;
975
976         DEBUGFS_ADD_PREFIX(rx_streaming, interval, streaming);
977         DEBUGFS_ADD_PREFIX(rx_streaming, always, streaming);
978
979
980         return 0;
981
982 err:
983         if (IS_ERR(entry))
984                 ret = PTR_ERR(entry);
985         else
986                 ret = -ENOMEM;
987
988         return ret;
989 }
990
991 void wl1271_debugfs_reset(struct wl1271 *wl)
992 {
993         if (!wl->stats.fw_stats)
994                 return;
995
996         memset(wl->stats.fw_stats, 0, wl->stats.fw_stats_len);
997         wl->stats.retry_count = 0;
998         wl->stats.excessive_retries = 0;
999 }
1000
1001 int wl1271_debugfs_init(struct wl1271 *wl)
1002 {
1003         int ret;
1004         struct dentry *rootdir;
1005
1006         rootdir = debugfs_create_dir(KBUILD_MODNAME,
1007                                      wl->hw->wiphy->debugfsdir);
1008
1009         if (IS_ERR(rootdir)) {
1010                 ret = PTR_ERR(rootdir);
1011                 goto out;
1012         }
1013
1014         wl->stats.fw_stats = kzalloc(wl->stats.fw_stats_len, GFP_KERNEL);
1015         if (!wl->stats.fw_stats) {
1016                 ret = -ENOMEM;
1017                 goto out_remove;
1018         }
1019
1020         wl->stats.fw_stats_update = jiffies;
1021
1022         ret = wl1271_debugfs_add_files(wl, rootdir);
1023         if (ret < 0)
1024                 goto out_exit;
1025
1026         ret = wlcore_debugfs_init(wl, rootdir);
1027         if (ret < 0)
1028                 goto out_exit;
1029
1030         goto out;
1031
1032 out_exit:
1033         wl1271_debugfs_exit(wl);
1034
1035 out_remove:
1036         debugfs_remove_recursive(rootdir);
1037
1038 out:
1039         return ret;
1040 }
1041
1042 void wl1271_debugfs_exit(struct wl1271 *wl)
1043 {
1044         kfree(wl->stats.fw_stats);
1045         wl->stats.fw_stats = NULL;
1046 }