1/*
2 * NewportMedia WiFi chipset driver test tools - wilc-debug
3 * Copyright (c) 2012 NewportMedia Inc.
4 * Author: SSW <sswd@wilcsemic.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#if defined(WILC_DEBUGFS)
13#include <linux/module.h>
14#include <linux/debugfs.h>
15#include <linux/poll.h>
16#include <linux/sched.h>
17
18#include "wilc_wlan_if.h"
19
20
21static struct dentry *wilc_dir;
22
23/*
24 * --------------------------------------------------------------------------------
25 */
26
27#define DBG_REGION_ALL	(GENERIC_DBG | HOSTAPD_DBG | HOSTINF_DBG | CORECONFIG_DBG | CFG80211_DBG | INT_DBG | TX_DBG | RX_DBG | LOCK_DBG | INIT_DBG | BUS_DBG | MEM_DBG)
28#define DBG_LEVEL_ALL	(DEBUG | INFO | WRN | ERR)
29atomic_t REGION = ATOMIC_INIT(INIT_DBG | GENERIC_DBG | CFG80211_DBG | FIRM_DBG | HOSTAPD_DBG);
30atomic_t DEBUG_LEVEL = ATOMIC_INIT(ERR);
31
32/*
33 * --------------------------------------------------------------------------------
34 */
35
36
37static ssize_t wilc_debug_level_read(struct file *file, char __user *userbuf, size_t count, loff_t *ppos)
38{
39	char buf[128];
40	int res = 0;
41
42	/* only allow read from start */
43	if (*ppos > 0)
44		return 0;
45
46	res = scnprintf(buf, sizeof(buf), "Debug Level: %x\n", atomic_read(&DEBUG_LEVEL));
47
48	return simple_read_from_buffer(userbuf, count, ppos, buf, res);
49}
50
51static ssize_t wilc_debug_level_write(struct file *filp, const char __user *buf,
52					size_t count, loff_t *ppos)
53{
54	int flag = 0;
55	int ret;
56
57	ret = kstrtouint_from_user(buf, count, 16, &flag);
58	if (ret)
59		return ret;
60
61	if (flag > DBG_LEVEL_ALL) {
62		printk("%s, value (0x%08x) is out of range, stay previous flag (0x%08x)\n", __func__, flag, atomic_read(&DEBUG_LEVEL));
63		return -EINVAL;
64	}
65
66	atomic_set(&DEBUG_LEVEL, (int)flag);
67
68	if (flag == 0)
69		printk("Debug-level disabled\n");
70	else
71		printk("Debug-level enabled\n");
72
73	return count;
74}
75
76static ssize_t wilc_debug_region_read(struct file *file, char __user *userbuf, size_t count, loff_t *ppos)
77{
78	char buf[128];
79	int res = 0;
80
81	/* only allow read from start */
82	if (*ppos > 0)
83		return 0;
84
85	res = scnprintf(buf, sizeof(buf), "Debug region: %x\n", atomic_read(&REGION));
86
87	return simple_read_from_buffer(userbuf, count, ppos, buf, res);
88}
89
90static ssize_t wilc_debug_region_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
91{
92	char buffer[128] = {};
93	int flag;
94
95	if (count > sizeof(buffer))
96		return -EINVAL;
97
98	if (copy_from_user(buffer, buf, count)) {
99		return -EFAULT;
100	}
101
102	flag = buffer[0] - '0';
103
104	if (flag > DBG_REGION_ALL) {
105		printk("%s, value (0x%08x) is out of range, stay previous flag (0x%08x)\n", __func__, flag, atomic_read(&REGION));
106		return -EFAULT;
107	}
108
109	atomic_set(&REGION, (int)flag);
110	printk("new debug-region is %x\n", atomic_read(&REGION));
111
112	return count;
113}
114
115/*
116 * --------------------------------------------------------------------------------
117 */
118
119#define FOPS(_open, _read, _write, _poll) { \
120		.owner	= THIS_MODULE, \
121		.open	= (_open), \
122		.read	= (_read), \
123		.write	= (_write), \
124		.poll		= (_poll), \
125}
126
127struct wilc_debugfs_info_t {
128	const char *name;
129	int perm;
130	unsigned int data;
131	struct file_operations fops;
132};
133
134static struct wilc_debugfs_info_t debugfs_info[] = {
135	{ "wilc_debug_level",	0666,	(DEBUG | ERR), FOPS(NULL, wilc_debug_level_read, wilc_debug_level_write, NULL), },
136	{ "wilc_debug_region",	0666,	(INIT_DBG | GENERIC_DBG | CFG80211_DBG), FOPS(NULL, wilc_debug_region_read, wilc_debug_region_write, NULL), },
137};
138
139int wilc_debugfs_init(void)
140{
141	int i;
142
143	struct dentry *debugfs_files;
144	struct wilc_debugfs_info_t *info;
145
146	wilc_dir = debugfs_create_dir("wilc_wifi", NULL);
147	if (wilc_dir ==  ERR_PTR(-ENODEV)) {
148		/* it's not error. the debugfs is just not being enabled. */
149		printk("ERR, kernel has built without debugfs support\n");
150		return 0;
151	}
152
153	if (!wilc_dir) {
154		printk("ERR, debugfs create dir\n");
155		return -1;
156	}
157
158	for (i = 0; i < ARRAY_SIZE(debugfs_info); i++) {
159		info = &debugfs_info[i];
160		debugfs_files = debugfs_create_file(info->name,
161						    info->perm,
162						    wilc_dir,
163						    &info->data,
164						    &info->fops);
165
166		if (!debugfs_files) {
167			printk("ERR fail to create the debugfs file, %s\n", info->name);
168			debugfs_remove_recursive(wilc_dir);
169			return -1;
170		}
171	}
172	return 0;
173}
174
175void wilc_debugfs_remove(void)
176{
177	debugfs_remove_recursive(wilc_dir);
178}
179
180#endif
181
182