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