Merge tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck...
[pandora-kernel.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 #define WLCORE_MAX_BLOCK_SIZE ((size_t)(4*PAGE_SIZE))
42
43 /* debugfs macros idea from mac80211 */
44 int wl1271_format_buffer(char __user *userbuf, size_t count,
45                          loff_t *ppos, char *fmt, ...)
46 {
47         va_list args;
48         char buf[DEBUGFS_FORMAT_BUFFER_SIZE];
49         int res;
50
51         va_start(args, fmt);
52         res = vscnprintf(buf, sizeof(buf), fmt, args);
53         va_end(args);
54
55         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
56 }
57 EXPORT_SYMBOL_GPL(wl1271_format_buffer);
58
59 void wl1271_debugfs_update_stats(struct wl1271 *wl)
60 {
61         int ret;
62
63         mutex_lock(&wl->mutex);
64
65         ret = wl1271_ps_elp_wakeup(wl);
66         if (ret < 0)
67                 goto out;
68
69         if (wl->state == WL1271_STATE_ON && !wl->plt &&
70             time_after(jiffies, wl->stats.fw_stats_update +
71                        msecs_to_jiffies(WL1271_DEBUGFS_STATS_LIFETIME))) {
72                 wl1271_acx_statistics(wl, wl->stats.fw_stats);
73                 wl->stats.fw_stats_update = jiffies;
74         }
75
76         wl1271_ps_elp_sleep(wl);
77
78 out:
79         mutex_unlock(&wl->mutex);
80 }
81 EXPORT_SYMBOL_GPL(wl1271_debugfs_update_stats);
82
83 DEBUGFS_READONLY_FILE(retry_count, "%u", wl->stats.retry_count);
84 DEBUGFS_READONLY_FILE(excessive_retries, "%u",
85                       wl->stats.excessive_retries);
86
87 static ssize_t tx_queue_len_read(struct file *file, char __user *userbuf,
88                                  size_t count, loff_t *ppos)
89 {
90         struct wl1271 *wl = file->private_data;
91         u32 queue_len;
92         char buf[20];
93         int res;
94
95         queue_len = wl1271_tx_total_queue_count(wl);
96
97         res = scnprintf(buf, sizeof(buf), "%u\n", queue_len);
98         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
99 }
100
101 static const struct file_operations tx_queue_len_ops = {
102         .read = tx_queue_len_read,
103         .open = simple_open,
104         .llseek = default_llseek,
105 };
106
107 static void chip_op_handler(struct wl1271 *wl, unsigned long value,
108                             void *arg)
109 {
110         int ret;
111         int (*chip_op) (struct wl1271 *wl);
112
113         if (!arg) {
114                 wl1271_warning("debugfs chip_op_handler with no callback");
115                 return;
116         }
117
118         ret = wl1271_ps_elp_wakeup(wl);
119         if (ret < 0)
120                 return;
121
122         chip_op = arg;
123         chip_op(wl);
124
125         wl1271_ps_elp_sleep(wl);
126 }
127
128
129 static inline void no_write_handler(struct wl1271 *wl,
130                                     unsigned long value,
131                                     unsigned long param)
132 {
133 }
134
135 #define WL12XX_CONF_DEBUGFS(param, conf_sub_struct,                     \
136                             min_val, max_val, write_handler_locked,     \
137                             write_handler_arg)                          \
138         static ssize_t param##_read(struct file *file,                  \
139                                       char __user *user_buf,            \
140                                       size_t count, loff_t *ppos)       \
141         {                                                               \
142         struct wl1271 *wl = file->private_data;                         \
143         return wl1271_format_buffer(user_buf, count,                    \
144                                     ppos, "%d\n",                       \
145                                     wl->conf.conf_sub_struct.param);    \
146         }                                                               \
147                                                                         \
148         static ssize_t param##_write(struct file *file,                 \
149                                      const char __user *user_buf,       \
150                                      size_t count, loff_t *ppos)        \
151         {                                                               \
152         struct wl1271 *wl = file->private_data;                         \
153         unsigned long value;                                            \
154         int ret;                                                        \
155                                                                         \
156         ret = kstrtoul_from_user(user_buf, count, 10, &value);          \
157         if (ret < 0) {                                                  \
158                 wl1271_warning("illegal value for " #param);            \
159                 return -EINVAL;                                         \
160         }                                                               \
161                                                                         \
162         if (value < min_val || value > max_val) {                       \
163                 wl1271_warning(#param " is not in valid range");        \
164                 return -ERANGE;                                         \
165         }                                                               \
166                                                                         \
167         mutex_lock(&wl->mutex);                                         \
168         wl->conf.conf_sub_struct.param = value;                         \
169                                                                         \
170         write_handler_locked(wl, value, write_handler_arg);             \
171                                                                         \
172         mutex_unlock(&wl->mutex);                                       \
173         return count;                                                   \
174         }                                                               \
175                                                                         \
176         static const struct file_operations param##_ops = {             \
177                 .read = param##_read,                                   \
178                 .write = param##_write,                                 \
179                 .open = simple_open,                                    \
180                 .llseek = default_llseek,                               \
181         };
182
183 WL12XX_CONF_DEBUGFS(irq_pkt_threshold, rx, 0, 65535,
184                     chip_op_handler, wl1271_acx_init_rx_interrupt)
185 WL12XX_CONF_DEBUGFS(irq_blk_threshold, rx, 0, 65535,
186                     chip_op_handler, wl1271_acx_init_rx_interrupt)
187 WL12XX_CONF_DEBUGFS(irq_timeout, rx, 0, 100,
188                     chip_op_handler, wl1271_acx_init_rx_interrupt)
189
190 static ssize_t gpio_power_read(struct file *file, char __user *user_buf,
191                           size_t count, loff_t *ppos)
192 {
193         struct wl1271 *wl = file->private_data;
194         bool state = test_bit(WL1271_FLAG_GPIO_POWER, &wl->flags);
195
196         int res;
197         char buf[10];
198
199         res = scnprintf(buf, sizeof(buf), "%d\n", state);
200
201         return simple_read_from_buffer(user_buf, count, ppos, buf, res);
202 }
203
204 static ssize_t gpio_power_write(struct file *file,
205                            const char __user *user_buf,
206                            size_t count, loff_t *ppos)
207 {
208         struct wl1271 *wl = file->private_data;
209         unsigned long value;
210         int ret;
211
212         ret = kstrtoul_from_user(user_buf, count, 10, &value);
213         if (ret < 0) {
214                 wl1271_warning("illegal value in gpio_power");
215                 return -EINVAL;
216         }
217
218         mutex_lock(&wl->mutex);
219
220         if (value)
221                 wl1271_power_on(wl);
222         else
223                 wl1271_power_off(wl);
224
225         mutex_unlock(&wl->mutex);
226         return count;
227 }
228
229 static const struct file_operations gpio_power_ops = {
230         .read = gpio_power_read,
231         .write = gpio_power_write,
232         .open = simple_open,
233         .llseek = default_llseek,
234 };
235
236 static ssize_t start_recovery_write(struct file *file,
237                                     const char __user *user_buf,
238                                     size_t count, loff_t *ppos)
239 {
240         struct wl1271 *wl = file->private_data;
241
242         mutex_lock(&wl->mutex);
243         wl12xx_queue_recovery_work(wl);
244         mutex_unlock(&wl->mutex);
245
246         return count;
247 }
248
249 static const struct file_operations start_recovery_ops = {
250         .write = start_recovery_write,
251         .open = simple_open,
252         .llseek = default_llseek,
253 };
254
255 static ssize_t dynamic_ps_timeout_read(struct file *file, char __user *user_buf,
256                           size_t count, loff_t *ppos)
257 {
258         struct wl1271 *wl = file->private_data;
259
260         return wl1271_format_buffer(user_buf, count,
261                                     ppos, "%d\n",
262                                     wl->conf.conn.dynamic_ps_timeout);
263 }
264
265 static ssize_t dynamic_ps_timeout_write(struct file *file,
266                                     const char __user *user_buf,
267                                     size_t count, loff_t *ppos)
268 {
269         struct wl1271 *wl = file->private_data;
270         struct wl12xx_vif *wlvif;
271         unsigned long value;
272         int ret;
273
274         ret = kstrtoul_from_user(user_buf, count, 10, &value);
275         if (ret < 0) {
276                 wl1271_warning("illegal value in dynamic_ps");
277                 return -EINVAL;
278         }
279
280         if (value < 1 || value > 65535) {
281                 wl1271_warning("dyanmic_ps_timeout is not in valid range");
282                 return -ERANGE;
283         }
284
285         mutex_lock(&wl->mutex);
286
287         wl->conf.conn.dynamic_ps_timeout = value;
288
289         if (wl->state == WL1271_STATE_OFF)
290                 goto out;
291
292         ret = wl1271_ps_elp_wakeup(wl);
293         if (ret < 0)
294                 goto out;
295
296         /* In case we're already in PSM, trigger it again to set new timeout
297          * immediately without waiting for re-association
298          */
299
300         wl12xx_for_each_wlvif_sta(wl, wlvif) {
301                 if (test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags))
302                         wl1271_ps_set_mode(wl, wlvif, STATION_AUTO_PS_MODE);
303         }
304
305         wl1271_ps_elp_sleep(wl);
306
307 out:
308         mutex_unlock(&wl->mutex);
309         return count;
310 }
311
312 static const struct file_operations dynamic_ps_timeout_ops = {
313         .read = dynamic_ps_timeout_read,
314         .write = dynamic_ps_timeout_write,
315         .open = simple_open,
316         .llseek = default_llseek,
317 };
318
319 static ssize_t forced_ps_read(struct file *file, char __user *user_buf,
320                           size_t count, loff_t *ppos)
321 {
322         struct wl1271 *wl = file->private_data;
323
324         return wl1271_format_buffer(user_buf, count,
325                                     ppos, "%d\n",
326                                     wl->conf.conn.forced_ps);
327 }
328
329 static ssize_t forced_ps_write(struct file *file,
330                                     const char __user *user_buf,
331                                     size_t count, loff_t *ppos)
332 {
333         struct wl1271 *wl = file->private_data;
334         struct wl12xx_vif *wlvif;
335         unsigned long value;
336         int ret, ps_mode;
337
338         ret = kstrtoul_from_user(user_buf, count, 10, &value);
339         if (ret < 0) {
340                 wl1271_warning("illegal value in forced_ps");
341                 return -EINVAL;
342         }
343
344         if (value != 1 && value != 0) {
345                 wl1271_warning("forced_ps should be either 0 or 1");
346                 return -ERANGE;
347         }
348
349         mutex_lock(&wl->mutex);
350
351         if (wl->conf.conn.forced_ps == value)
352                 goto out;
353
354         wl->conf.conn.forced_ps = value;
355
356         if (wl->state == WL1271_STATE_OFF)
357                 goto out;
358
359         ret = wl1271_ps_elp_wakeup(wl);
360         if (ret < 0)
361                 goto out;
362
363         /* In case we're already in PSM, trigger it again to switch mode
364          * immediately without waiting for re-association
365          */
366
367         ps_mode = value ? STATION_POWER_SAVE_MODE : STATION_AUTO_PS_MODE;
368
369         wl12xx_for_each_wlvif_sta(wl, wlvif) {
370                 if (test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags))
371                         wl1271_ps_set_mode(wl, wlvif, ps_mode);
372         }
373
374         wl1271_ps_elp_sleep(wl);
375
376 out:
377         mutex_unlock(&wl->mutex);
378         return count;
379 }
380
381 static const struct file_operations forced_ps_ops = {
382         .read = forced_ps_read,
383         .write = forced_ps_write,
384         .open = simple_open,
385         .llseek = default_llseek,
386 };
387
388 static ssize_t split_scan_timeout_read(struct file *file, char __user *user_buf,
389                           size_t count, loff_t *ppos)
390 {
391         struct wl1271 *wl = file->private_data;
392
393         return wl1271_format_buffer(user_buf, count,
394                                     ppos, "%d\n",
395                                     wl->conf.scan.split_scan_timeout / 1000);
396 }
397
398 static ssize_t split_scan_timeout_write(struct file *file,
399                                     const char __user *user_buf,
400                                     size_t count, loff_t *ppos)
401 {
402         struct wl1271 *wl = file->private_data;
403         unsigned long value;
404         int ret;
405
406         ret = kstrtoul_from_user(user_buf, count, 10, &value);
407         if (ret < 0) {
408                 wl1271_warning("illegal value in split_scan_timeout");
409                 return -EINVAL;
410         }
411
412         if (value == 0)
413                 wl1271_info("split scan will be disabled");
414
415         mutex_lock(&wl->mutex);
416
417         wl->conf.scan.split_scan_timeout = value * 1000;
418
419         mutex_unlock(&wl->mutex);
420         return count;
421 }
422
423 static const struct file_operations split_scan_timeout_ops = {
424         .read = split_scan_timeout_read,
425         .write = split_scan_timeout_write,
426         .open = simple_open,
427         .llseek = default_llseek,
428 };
429
430 static ssize_t driver_state_read(struct file *file, char __user *user_buf,
431                                  size_t count, loff_t *ppos)
432 {
433         struct wl1271 *wl = file->private_data;
434         int res = 0;
435         ssize_t ret;
436         char *buf;
437
438 #define DRIVER_STATE_BUF_LEN 1024
439
440         buf = kmalloc(DRIVER_STATE_BUF_LEN, GFP_KERNEL);
441         if (!buf)
442                 return -ENOMEM;
443
444         mutex_lock(&wl->mutex);
445
446 #define DRIVER_STATE_PRINT(x, fmt)   \
447         (res += scnprintf(buf + res, DRIVER_STATE_BUF_LEN - res,\
448                           #x " = " fmt "\n", wl->x))
449
450 #define DRIVER_STATE_PRINT_LONG(x) DRIVER_STATE_PRINT(x, "%ld")
451 #define DRIVER_STATE_PRINT_INT(x)  DRIVER_STATE_PRINT(x, "%d")
452 #define DRIVER_STATE_PRINT_STR(x)  DRIVER_STATE_PRINT(x, "%s")
453 #define DRIVER_STATE_PRINT_LHEX(x) DRIVER_STATE_PRINT(x, "0x%lx")
454 #define DRIVER_STATE_PRINT_HEX(x)  DRIVER_STATE_PRINT(x, "0x%x")
455
456         DRIVER_STATE_PRINT_INT(tx_blocks_available);
457         DRIVER_STATE_PRINT_INT(tx_allocated_blocks);
458         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[0]);
459         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[1]);
460         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[2]);
461         DRIVER_STATE_PRINT_INT(tx_allocated_pkts[3]);
462         DRIVER_STATE_PRINT_INT(tx_frames_cnt);
463         DRIVER_STATE_PRINT_LHEX(tx_frames_map[0]);
464         DRIVER_STATE_PRINT_INT(tx_queue_count[0]);
465         DRIVER_STATE_PRINT_INT(tx_queue_count[1]);
466         DRIVER_STATE_PRINT_INT(tx_queue_count[2]);
467         DRIVER_STATE_PRINT_INT(tx_queue_count[3]);
468         DRIVER_STATE_PRINT_INT(tx_packets_count);
469         DRIVER_STATE_PRINT_INT(tx_results_count);
470         DRIVER_STATE_PRINT_LHEX(flags);
471         DRIVER_STATE_PRINT_INT(tx_blocks_freed);
472         DRIVER_STATE_PRINT_INT(rx_counter);
473         DRIVER_STATE_PRINT_INT(state);
474         DRIVER_STATE_PRINT_INT(channel);
475         DRIVER_STATE_PRINT_INT(band);
476         DRIVER_STATE_PRINT_INT(power_level);
477         DRIVER_STATE_PRINT_INT(sg_enabled);
478         DRIVER_STATE_PRINT_INT(enable_11a);
479         DRIVER_STATE_PRINT_INT(noise);
480         DRIVER_STATE_PRINT_HEX(ap_fw_ps_map);
481         DRIVER_STATE_PRINT_LHEX(ap_ps_map);
482         DRIVER_STATE_PRINT_HEX(quirks);
483         DRIVER_STATE_PRINT_HEX(irq);
484         /* TODO: ref_clock and tcxo_clock were moved to wl12xx priv */
485         DRIVER_STATE_PRINT_HEX(hw_pg_ver);
486         DRIVER_STATE_PRINT_HEX(platform_quirks);
487         DRIVER_STATE_PRINT_HEX(chip.id);
488         DRIVER_STATE_PRINT_STR(chip.fw_ver_str);
489         DRIVER_STATE_PRINT_INT(sched_scanning);
490
491 #undef DRIVER_STATE_PRINT_INT
492 #undef DRIVER_STATE_PRINT_LONG
493 #undef DRIVER_STATE_PRINT_HEX
494 #undef DRIVER_STATE_PRINT_LHEX
495 #undef DRIVER_STATE_PRINT_STR
496 #undef DRIVER_STATE_PRINT
497 #undef DRIVER_STATE_BUF_LEN
498
499         mutex_unlock(&wl->mutex);
500
501         ret = simple_read_from_buffer(user_buf, count, ppos, buf, res);
502         kfree(buf);
503         return ret;
504 }
505
506 static const struct file_operations driver_state_ops = {
507         .read = driver_state_read,
508         .open = simple_open,
509         .llseek = default_llseek,
510 };
511
512 static ssize_t vifs_state_read(struct file *file, char __user *user_buf,
513                                  size_t count, loff_t *ppos)
514 {
515         struct wl1271 *wl = file->private_data;
516         struct wl12xx_vif *wlvif;
517         int ret, res = 0;
518         const int buf_size = 4096;
519         char *buf;
520         char tmp_buf[64];
521
522         buf = kzalloc(buf_size, GFP_KERNEL);
523         if (!buf)
524                 return -ENOMEM;
525
526         mutex_lock(&wl->mutex);
527
528 #define VIF_STATE_PRINT(x, fmt)                         \
529         (res += scnprintf(buf + res, buf_size - res,    \
530                           #x " = " fmt "\n", wlvif->x))
531
532 #define VIF_STATE_PRINT_LONG(x)  VIF_STATE_PRINT(x, "%ld")
533 #define VIF_STATE_PRINT_INT(x)   VIF_STATE_PRINT(x, "%d")
534 #define VIF_STATE_PRINT_STR(x)   VIF_STATE_PRINT(x, "%s")
535 #define VIF_STATE_PRINT_LHEX(x)  VIF_STATE_PRINT(x, "0x%lx")
536 #define VIF_STATE_PRINT_LLHEX(x) VIF_STATE_PRINT(x, "0x%llx")
537 #define VIF_STATE_PRINT_HEX(x)   VIF_STATE_PRINT(x, "0x%x")
538
539 #define VIF_STATE_PRINT_NSTR(x, len)                            \
540         do {                                                    \
541                 memset(tmp_buf, 0, sizeof(tmp_buf));            \
542                 memcpy(tmp_buf, wlvif->x,                       \
543                        min_t(u8, len, sizeof(tmp_buf) - 1));    \
544                 res += scnprintf(buf + res, buf_size - res,     \
545                                  #x " = %s\n", tmp_buf);        \
546         } while (0)
547
548         wl12xx_for_each_wlvif(wl, wlvif) {
549                 VIF_STATE_PRINT_INT(role_id);
550                 VIF_STATE_PRINT_INT(bss_type);
551                 VIF_STATE_PRINT_LHEX(flags);
552                 VIF_STATE_PRINT_INT(p2p);
553                 VIF_STATE_PRINT_INT(dev_role_id);
554                 VIF_STATE_PRINT_INT(dev_hlid);
555
556                 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
557                     wlvif->bss_type == BSS_TYPE_IBSS) {
558                         VIF_STATE_PRINT_INT(sta.hlid);
559                         VIF_STATE_PRINT_INT(sta.ba_rx_bitmap);
560                         VIF_STATE_PRINT_INT(sta.basic_rate_idx);
561                         VIF_STATE_PRINT_INT(sta.ap_rate_idx);
562                         VIF_STATE_PRINT_INT(sta.p2p_rate_idx);
563                         VIF_STATE_PRINT_INT(sta.qos);
564                 } else {
565                         VIF_STATE_PRINT_INT(ap.global_hlid);
566                         VIF_STATE_PRINT_INT(ap.bcast_hlid);
567                         VIF_STATE_PRINT_LHEX(ap.sta_hlid_map[0]);
568                         VIF_STATE_PRINT_INT(ap.mgmt_rate_idx);
569                         VIF_STATE_PRINT_INT(ap.bcast_rate_idx);
570                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[0]);
571                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[1]);
572                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[2]);
573                         VIF_STATE_PRINT_INT(ap.ucast_rate_idx[3]);
574                 }
575                 VIF_STATE_PRINT_INT(last_tx_hlid);
576                 VIF_STATE_PRINT_LHEX(links_map[0]);
577                 VIF_STATE_PRINT_NSTR(ssid, wlvif->ssid_len);
578                 VIF_STATE_PRINT_INT(band);
579                 VIF_STATE_PRINT_INT(channel);
580                 VIF_STATE_PRINT_HEX(bitrate_masks[0]);
581                 VIF_STATE_PRINT_HEX(bitrate_masks[1]);
582                 VIF_STATE_PRINT_HEX(basic_rate_set);
583                 VIF_STATE_PRINT_HEX(basic_rate);
584                 VIF_STATE_PRINT_HEX(rate_set);
585                 VIF_STATE_PRINT_INT(beacon_int);
586                 VIF_STATE_PRINT_INT(default_key);
587                 VIF_STATE_PRINT_INT(aid);
588                 VIF_STATE_PRINT_INT(session_counter);
589                 VIF_STATE_PRINT_INT(psm_entry_retry);
590                 VIF_STATE_PRINT_INT(power_level);
591                 VIF_STATE_PRINT_INT(rssi_thold);
592                 VIF_STATE_PRINT_INT(last_rssi_event);
593                 VIF_STATE_PRINT_INT(ba_support);
594                 VIF_STATE_PRINT_INT(ba_allowed);
595                 VIF_STATE_PRINT_LLHEX(tx_security_seq);
596                 VIF_STATE_PRINT_INT(tx_security_last_seq_lsb);
597         }
598
599 #undef VIF_STATE_PRINT_INT
600 #undef VIF_STATE_PRINT_LONG
601 #undef VIF_STATE_PRINT_HEX
602 #undef VIF_STATE_PRINT_LHEX
603 #undef VIF_STATE_PRINT_LLHEX
604 #undef VIF_STATE_PRINT_STR
605 #undef VIF_STATE_PRINT_NSTR
606 #undef VIF_STATE_PRINT
607
608         mutex_unlock(&wl->mutex);
609
610         ret = simple_read_from_buffer(user_buf, count, ppos, buf, res);
611         kfree(buf);
612         return ret;
613 }
614
615 static const struct file_operations vifs_state_ops = {
616         .read = vifs_state_read,
617         .open = simple_open,
618         .llseek = default_llseek,
619 };
620
621 static ssize_t dtim_interval_read(struct file *file, char __user *user_buf,
622                                   size_t count, loff_t *ppos)
623 {
624         struct wl1271 *wl = file->private_data;
625         u8 value;
626
627         if (wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_DTIM ||
628             wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_N_DTIM)
629                 value = wl->conf.conn.listen_interval;
630         else
631                 value = 0;
632
633         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
634 }
635
636 static ssize_t dtim_interval_write(struct file *file,
637                                    const char __user *user_buf,
638                                    size_t count, loff_t *ppos)
639 {
640         struct wl1271 *wl = file->private_data;
641         unsigned long value;
642         int ret;
643
644         ret = kstrtoul_from_user(user_buf, count, 10, &value);
645         if (ret < 0) {
646                 wl1271_warning("illegal value for dtim_interval");
647                 return -EINVAL;
648         }
649
650         if (value < 1 || value > 10) {
651                 wl1271_warning("dtim value is not in valid range");
652                 return -ERANGE;
653         }
654
655         mutex_lock(&wl->mutex);
656
657         wl->conf.conn.listen_interval = value;
658         /* for some reason there are different event types for 1 and >1 */
659         if (value == 1)
660                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_DTIM;
661         else
662                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM;
663
664         /*
665          * we don't reconfigure ACX_WAKE_UP_CONDITIONS now, so it will only
666          * take effect on the next time we enter psm.
667          */
668         mutex_unlock(&wl->mutex);
669         return count;
670 }
671
672 static const struct file_operations dtim_interval_ops = {
673         .read = dtim_interval_read,
674         .write = dtim_interval_write,
675         .open = simple_open,
676         .llseek = default_llseek,
677 };
678
679
680
681 static ssize_t suspend_dtim_interval_read(struct file *file,
682                                           char __user *user_buf,
683                                           size_t count, loff_t *ppos)
684 {
685         struct wl1271 *wl = file->private_data;
686         u8 value;
687
688         if (wl->conf.conn.suspend_wake_up_event == CONF_WAKE_UP_EVENT_DTIM ||
689             wl->conf.conn.suspend_wake_up_event == CONF_WAKE_UP_EVENT_N_DTIM)
690                 value = wl->conf.conn.suspend_listen_interval;
691         else
692                 value = 0;
693
694         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
695 }
696
697 static ssize_t suspend_dtim_interval_write(struct file *file,
698                                            const char __user *user_buf,
699                                            size_t count, loff_t *ppos)
700 {
701         struct wl1271 *wl = file->private_data;
702         unsigned long value;
703         int ret;
704
705         ret = kstrtoul_from_user(user_buf, count, 10, &value);
706         if (ret < 0) {
707                 wl1271_warning("illegal value for suspend_dtim_interval");
708                 return -EINVAL;
709         }
710
711         if (value < 1 || value > 10) {
712                 wl1271_warning("suspend_dtim value is not in valid range");
713                 return -ERANGE;
714         }
715
716         mutex_lock(&wl->mutex);
717
718         wl->conf.conn.suspend_listen_interval = value;
719         /* for some reason there are different event types for 1 and >1 */
720         if (value == 1)
721                 wl->conf.conn.suspend_wake_up_event = CONF_WAKE_UP_EVENT_DTIM;
722         else
723                 wl->conf.conn.suspend_wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM;
724
725         mutex_unlock(&wl->mutex);
726         return count;
727 }
728
729
730 static const struct file_operations suspend_dtim_interval_ops = {
731         .read = suspend_dtim_interval_read,
732         .write = suspend_dtim_interval_write,
733         .open = simple_open,
734         .llseek = default_llseek,
735 };
736
737 static ssize_t beacon_interval_read(struct file *file, char __user *user_buf,
738                                     size_t count, loff_t *ppos)
739 {
740         struct wl1271 *wl = file->private_data;
741         u8 value;
742
743         if (wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_BEACON ||
744             wl->conf.conn.wake_up_event == CONF_WAKE_UP_EVENT_N_BEACONS)
745                 value = wl->conf.conn.listen_interval;
746         else
747                 value = 0;
748
749         return wl1271_format_buffer(user_buf, count, ppos, "%d\n", value);
750 }
751
752 static ssize_t beacon_interval_write(struct file *file,
753                                      const char __user *user_buf,
754                                      size_t count, loff_t *ppos)
755 {
756         struct wl1271 *wl = file->private_data;
757         unsigned long value;
758         int ret;
759
760         ret = kstrtoul_from_user(user_buf, count, 10, &value);
761         if (ret < 0) {
762                 wl1271_warning("illegal value for beacon_interval");
763                 return -EINVAL;
764         }
765
766         if (value < 1 || value > 255) {
767                 wl1271_warning("beacon interval value is not in valid range");
768                 return -ERANGE;
769         }
770
771         mutex_lock(&wl->mutex);
772
773         wl->conf.conn.listen_interval = value;
774         /* for some reason there are different event types for 1 and >1 */
775         if (value == 1)
776                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_BEACON;
777         else
778                 wl->conf.conn.wake_up_event = CONF_WAKE_UP_EVENT_N_BEACONS;
779
780         /*
781          * we don't reconfigure ACX_WAKE_UP_CONDITIONS now, so it will only
782          * take effect on the next time we enter psm.
783          */
784         mutex_unlock(&wl->mutex);
785         return count;
786 }
787
788 static const struct file_operations beacon_interval_ops = {
789         .read = beacon_interval_read,
790         .write = beacon_interval_write,
791         .open = simple_open,
792         .llseek = default_llseek,
793 };
794
795 static ssize_t rx_streaming_interval_write(struct file *file,
796                            const char __user *user_buf,
797                            size_t count, loff_t *ppos)
798 {
799         struct wl1271 *wl = file->private_data;
800         struct wl12xx_vif *wlvif;
801         unsigned long value;
802         int ret;
803
804         ret = kstrtoul_from_user(user_buf, count, 10, &value);
805         if (ret < 0) {
806                 wl1271_warning("illegal value in rx_streaming_interval!");
807                 return -EINVAL;
808         }
809
810         /* valid values: 0, 10-100 */
811         if (value && (value < 10 || value > 100)) {
812                 wl1271_warning("value is not in range!");
813                 return -ERANGE;
814         }
815
816         mutex_lock(&wl->mutex);
817
818         wl->conf.rx_streaming.interval = value;
819
820         ret = wl1271_ps_elp_wakeup(wl);
821         if (ret < 0)
822                 goto out;
823
824         wl12xx_for_each_wlvif_sta(wl, wlvif) {
825                 wl1271_recalc_rx_streaming(wl, wlvif);
826         }
827
828         wl1271_ps_elp_sleep(wl);
829 out:
830         mutex_unlock(&wl->mutex);
831         return count;
832 }
833
834 static ssize_t rx_streaming_interval_read(struct file *file,
835                             char __user *userbuf,
836                             size_t count, loff_t *ppos)
837 {
838         struct wl1271 *wl = file->private_data;
839         return wl1271_format_buffer(userbuf, count, ppos,
840                                     "%d\n", wl->conf.rx_streaming.interval);
841 }
842
843 static const struct file_operations rx_streaming_interval_ops = {
844         .read = rx_streaming_interval_read,
845         .write = rx_streaming_interval_write,
846         .open = simple_open,
847         .llseek = default_llseek,
848 };
849
850 static ssize_t rx_streaming_always_write(struct file *file,
851                            const char __user *user_buf,
852                            size_t count, loff_t *ppos)
853 {
854         struct wl1271 *wl = file->private_data;
855         struct wl12xx_vif *wlvif;
856         unsigned long value;
857         int ret;
858
859         ret = kstrtoul_from_user(user_buf, count, 10, &value);
860         if (ret < 0) {
861                 wl1271_warning("illegal value in rx_streaming_write!");
862                 return -EINVAL;
863         }
864
865         /* valid values: 0, 10-100 */
866         if (!(value == 0 || value == 1)) {
867                 wl1271_warning("value is not in valid!");
868                 return -EINVAL;
869         }
870
871         mutex_lock(&wl->mutex);
872
873         wl->conf.rx_streaming.always = value;
874
875         ret = wl1271_ps_elp_wakeup(wl);
876         if (ret < 0)
877                 goto out;
878
879         wl12xx_for_each_wlvif_sta(wl, wlvif) {
880                 wl1271_recalc_rx_streaming(wl, wlvif);
881         }
882
883         wl1271_ps_elp_sleep(wl);
884 out:
885         mutex_unlock(&wl->mutex);
886         return count;
887 }
888
889 static ssize_t rx_streaming_always_read(struct file *file,
890                             char __user *userbuf,
891                             size_t count, loff_t *ppos)
892 {
893         struct wl1271 *wl = file->private_data;
894         return wl1271_format_buffer(userbuf, count, ppos,
895                                     "%d\n", wl->conf.rx_streaming.always);
896 }
897
898 static const struct file_operations rx_streaming_always_ops = {
899         .read = rx_streaming_always_read,
900         .write = rx_streaming_always_write,
901         .open = simple_open,
902         .llseek = default_llseek,
903 };
904
905 static ssize_t beacon_filtering_write(struct file *file,
906                                       const char __user *user_buf,
907                                       size_t count, loff_t *ppos)
908 {
909         struct wl1271 *wl = file->private_data;
910         struct wl12xx_vif *wlvif;
911         char buf[10];
912         size_t len;
913         unsigned long value;
914         int ret;
915
916         len = min(count, sizeof(buf) - 1);
917         if (copy_from_user(buf, user_buf, len))
918                 return -EFAULT;
919         buf[len] = '\0';
920
921         ret = kstrtoul(buf, 0, &value);
922         if (ret < 0) {
923                 wl1271_warning("illegal value for beacon_filtering!");
924                 return -EINVAL;
925         }
926
927         mutex_lock(&wl->mutex);
928
929         ret = wl1271_ps_elp_wakeup(wl);
930         if (ret < 0)
931                 goto out;
932
933         wl12xx_for_each_wlvif(wl, wlvif) {
934                 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, !!value);
935         }
936
937         wl1271_ps_elp_sleep(wl);
938 out:
939         mutex_unlock(&wl->mutex);
940         return count;
941 }
942
943 static const struct file_operations beacon_filtering_ops = {
944         .write = beacon_filtering_write,
945         .open = simple_open,
946         .llseek = default_llseek,
947 };
948
949 static ssize_t fw_stats_raw_read(struct file *file,
950                                  char __user *userbuf,
951                                  size_t count, loff_t *ppos)
952 {
953         struct wl1271 *wl = file->private_data;
954
955         wl1271_debugfs_update_stats(wl);
956
957         return simple_read_from_buffer(userbuf, count, ppos,
958                                        wl->stats.fw_stats,
959                                        wl->stats.fw_stats_len);
960 }
961
962 static const struct file_operations fw_stats_raw_ops = {
963         .read = fw_stats_raw_read,
964         .open = simple_open,
965         .llseek = default_llseek,
966 };
967
968 static ssize_t sleep_auth_read(struct file *file, char __user *user_buf,
969                                size_t count, loff_t *ppos)
970 {
971         struct wl1271 *wl = file->private_data;
972
973         return wl1271_format_buffer(user_buf, count,
974                                     ppos, "%d\n",
975                                     wl->sleep_auth);
976 }
977
978 static ssize_t sleep_auth_write(struct file *file,
979                                 const char __user *user_buf,
980                                 size_t count, loff_t *ppos)
981 {
982         struct wl1271 *wl = file->private_data;
983         unsigned long value;
984         int ret;
985
986         ret = kstrtoul_from_user(user_buf, count, 0, &value);
987         if (ret < 0) {
988                 wl1271_warning("illegal value in sleep_auth");
989                 return -EINVAL;
990         }
991
992         if (value < 0 || value > WL1271_PSM_MAX) {
993                 wl1271_warning("sleep_auth must be between 0 and %d",
994                                WL1271_PSM_MAX);
995                 return -ERANGE;
996         }
997
998         mutex_lock(&wl->mutex);
999
1000         wl->conf.conn.sta_sleep_auth = value;
1001
1002         if (wl->state == WL1271_STATE_OFF) {
1003                 /* this will show up on "read" in case we are off */
1004                 wl->sleep_auth = value;
1005                 goto out;
1006         }
1007
1008         ret = wl1271_ps_elp_wakeup(wl);
1009         if (ret < 0)
1010                 goto out;
1011
1012         ret = wl1271_acx_sleep_auth(wl, value);
1013         if (ret < 0)
1014                 goto out_sleep;
1015
1016 out_sleep:
1017         wl1271_ps_elp_sleep(wl);
1018 out:
1019         mutex_unlock(&wl->mutex);
1020         return count;
1021 }
1022
1023 static const struct file_operations sleep_auth_ops = {
1024         .read = sleep_auth_read,
1025         .write = sleep_auth_write,
1026         .open = simple_open,
1027         .llseek = default_llseek,
1028 };
1029
1030 static ssize_t dev_mem_read(struct file *file,
1031              char __user *user_buf, size_t count,
1032              loff_t *ppos)
1033 {
1034         struct wl1271 *wl = file->private_data;
1035         struct wlcore_partition_set part, old_part;
1036         size_t bytes = count;
1037         int ret;
1038         char *buf;
1039
1040         /* only requests of dword-aligned size and offset are supported */
1041         if (bytes % 4)
1042                 return -EINVAL;
1043
1044         if (*ppos % 4)
1045                 return -EINVAL;
1046
1047         /* function should return in reasonable time */
1048         bytes = min(bytes, WLCORE_MAX_BLOCK_SIZE);
1049
1050         if (bytes == 0)
1051                 return -EINVAL;
1052
1053         memset(&part, 0, sizeof(part));
1054         part.mem.start = file->f_pos;
1055         part.mem.size = bytes;
1056
1057         buf = kmalloc(bytes, GFP_KERNEL);
1058         if (!buf)
1059                 return -ENOMEM;
1060
1061         mutex_lock(&wl->mutex);
1062
1063         if (wl->state == WL1271_STATE_OFF) {
1064                 ret = -EFAULT;
1065                 goto skip_read;
1066         }
1067
1068         ret = wl1271_ps_elp_wakeup(wl);
1069         if (ret < 0)
1070                 goto skip_read;
1071
1072         /* store current partition and switch partition */
1073         memcpy(&old_part, &wl->curr_part, sizeof(old_part));
1074         ret = wlcore_set_partition(wl, &part);
1075         if (ret < 0)
1076                 goto part_err;
1077
1078         ret = wlcore_raw_read(wl, 0, buf, bytes, false);
1079         if (ret < 0)
1080                 goto read_err;
1081
1082 read_err:
1083         /* recover partition */
1084         ret = wlcore_set_partition(wl, &old_part);
1085         if (ret < 0)
1086                 goto part_err;
1087
1088 part_err:
1089         wl1271_ps_elp_sleep(wl);
1090
1091 skip_read:
1092         mutex_unlock(&wl->mutex);
1093
1094         if (ret == 0) {
1095                 ret = copy_to_user(user_buf, buf, bytes);
1096                 if (ret < bytes) {
1097                         bytes -= ret;
1098                         *ppos += bytes;
1099                         ret = 0;
1100                 } else {
1101                         ret = -EFAULT;
1102                 }
1103         }
1104
1105         kfree(buf);
1106
1107         return ((ret == 0) ? bytes : ret);
1108 }
1109
1110 static ssize_t dev_mem_write(struct file *file, const char __user *user_buf,
1111                 size_t count, loff_t *ppos)
1112 {
1113         struct wl1271 *wl = file->private_data;
1114         struct wlcore_partition_set part, old_part;
1115         size_t bytes = count;
1116         int ret;
1117         char *buf;
1118
1119         /* only requests of dword-aligned size and offset are supported */
1120         if (bytes % 4)
1121                 return -EINVAL;
1122
1123         if (*ppos % 4)
1124                 return -EINVAL;
1125
1126         /* function should return in reasonable time */
1127         bytes = min(bytes, WLCORE_MAX_BLOCK_SIZE);
1128
1129         if (bytes == 0)
1130                 return -EINVAL;
1131
1132         memset(&part, 0, sizeof(part));
1133         part.mem.start = file->f_pos;
1134         part.mem.size = bytes;
1135
1136         buf = kmalloc(bytes, GFP_KERNEL);
1137         if (!buf)
1138                 return -ENOMEM;
1139
1140         ret = copy_from_user(buf, user_buf, bytes);
1141         if (ret) {
1142                 ret = -EFAULT;
1143                 goto err_out;
1144         }
1145
1146         mutex_lock(&wl->mutex);
1147
1148         if (wl->state == WL1271_STATE_OFF) {
1149                 ret = -EFAULT;
1150                 goto skip_write;
1151         }
1152
1153         ret = wl1271_ps_elp_wakeup(wl);
1154         if (ret < 0)
1155                 goto skip_write;
1156
1157         /* store current partition and switch partition */
1158         memcpy(&old_part, &wl->curr_part, sizeof(old_part));
1159         ret = wlcore_set_partition(wl, &part);
1160         if (ret < 0)
1161                 goto part_err;
1162
1163         ret = wlcore_raw_write(wl, 0, buf, bytes, false);
1164         if (ret < 0)
1165                 goto write_err;
1166
1167 write_err:
1168         /* recover partition */
1169         ret = wlcore_set_partition(wl, &old_part);
1170         if (ret < 0)
1171                 goto part_err;
1172
1173 part_err:
1174         wl1271_ps_elp_sleep(wl);
1175
1176 skip_write:
1177         mutex_unlock(&wl->mutex);
1178
1179         if (ret == 0)
1180                 *ppos += bytes;
1181
1182 err_out:
1183         kfree(buf);
1184
1185         return ((ret == 0) ? bytes : ret);
1186 }
1187
1188 static loff_t dev_mem_seek(struct file *file, loff_t offset, int orig)
1189 {
1190         loff_t ret;
1191
1192         /* only requests of dword-aligned size and offset are supported */
1193         if (offset % 4)
1194                 return -EINVAL;
1195
1196         switch (orig) {
1197         case SEEK_SET:
1198                 file->f_pos = offset;
1199                 ret = file->f_pos;
1200                 break;
1201         case SEEK_CUR:
1202                 file->f_pos += offset;
1203                 ret = file->f_pos;
1204                 break;
1205         default:
1206                 ret = -EINVAL;
1207         }
1208
1209         return ret;
1210 }
1211
1212 static const struct file_operations dev_mem_ops = {
1213         .open = simple_open,
1214         .read = dev_mem_read,
1215         .write = dev_mem_write,
1216         .llseek = dev_mem_seek,
1217 };
1218
1219 static int wl1271_debugfs_add_files(struct wl1271 *wl,
1220                                     struct dentry *rootdir)
1221 {
1222         int ret = 0;
1223         struct dentry *entry, *streaming;
1224
1225         DEBUGFS_ADD(tx_queue_len, rootdir);
1226         DEBUGFS_ADD(retry_count, rootdir);
1227         DEBUGFS_ADD(excessive_retries, rootdir);
1228
1229         DEBUGFS_ADD(gpio_power, rootdir);
1230         DEBUGFS_ADD(start_recovery, rootdir);
1231         DEBUGFS_ADD(driver_state, rootdir);
1232         DEBUGFS_ADD(vifs_state, rootdir);
1233         DEBUGFS_ADD(dtim_interval, rootdir);
1234         DEBUGFS_ADD(suspend_dtim_interval, rootdir);
1235         DEBUGFS_ADD(beacon_interval, rootdir);
1236         DEBUGFS_ADD(beacon_filtering, rootdir);
1237         DEBUGFS_ADD(dynamic_ps_timeout, rootdir);
1238         DEBUGFS_ADD(forced_ps, rootdir);
1239         DEBUGFS_ADD(split_scan_timeout, rootdir);
1240         DEBUGFS_ADD(irq_pkt_threshold, rootdir);
1241         DEBUGFS_ADD(irq_blk_threshold, rootdir);
1242         DEBUGFS_ADD(irq_timeout, rootdir);
1243         DEBUGFS_ADD(fw_stats_raw, rootdir);
1244         DEBUGFS_ADD(sleep_auth, rootdir);
1245
1246         streaming = debugfs_create_dir("rx_streaming", rootdir);
1247         if (!streaming || IS_ERR(streaming))
1248                 goto err;
1249
1250         DEBUGFS_ADD_PREFIX(rx_streaming, interval, streaming);
1251         DEBUGFS_ADD_PREFIX(rx_streaming, always, streaming);
1252
1253         DEBUGFS_ADD_PREFIX(dev, mem, rootdir);
1254
1255         return 0;
1256
1257 err:
1258         if (IS_ERR(entry))
1259                 ret = PTR_ERR(entry);
1260         else
1261                 ret = -ENOMEM;
1262
1263         return ret;
1264 }
1265
1266 void wl1271_debugfs_reset(struct wl1271 *wl)
1267 {
1268         if (!wl->stats.fw_stats)
1269                 return;
1270
1271         memset(wl->stats.fw_stats, 0, wl->stats.fw_stats_len);
1272         wl->stats.retry_count = 0;
1273         wl->stats.excessive_retries = 0;
1274 }
1275
1276 int wl1271_debugfs_init(struct wl1271 *wl)
1277 {
1278         int ret;
1279         struct dentry *rootdir;
1280
1281         rootdir = debugfs_create_dir(KBUILD_MODNAME,
1282                                      wl->hw->wiphy->debugfsdir);
1283
1284         if (IS_ERR(rootdir)) {
1285                 ret = PTR_ERR(rootdir);
1286                 goto out;
1287         }
1288
1289         wl->stats.fw_stats = kzalloc(wl->stats.fw_stats_len, GFP_KERNEL);
1290         if (!wl->stats.fw_stats) {
1291                 ret = -ENOMEM;
1292                 goto out_remove;
1293         }
1294
1295         wl->stats.fw_stats_update = jiffies;
1296
1297         ret = wl1271_debugfs_add_files(wl, rootdir);
1298         if (ret < 0)
1299                 goto out_exit;
1300
1301         ret = wlcore_debugfs_init(wl, rootdir);
1302         if (ret < 0)
1303                 goto out_exit;
1304
1305         goto out;
1306
1307 out_exit:
1308         wl1271_debugfs_exit(wl);
1309
1310 out_remove:
1311         debugfs_remove_recursive(rootdir);
1312
1313 out:
1314         return ret;
1315 }
1316
1317 void wl1271_debugfs_exit(struct wl1271 *wl)
1318 {
1319         kfree(wl->stats.fw_stats);
1320         wl->stats.fw_stats = NULL;
1321 }