1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/debugfs.h>
18 #include "mt76x02.h"
19 
20 static int
21 mt76x02_ampdu_stat_read(struct seq_file *file, void *data)
22 {
23 	struct mt76x02_dev *dev = file->private;
24 	int i, j;
25 
26 	for (i = 0; i < 4; i++) {
27 		seq_puts(file, "Length: ");
28 		for (j = 0; j < 8; j++)
29 			seq_printf(file, "%8d | ", i * 8 + j + 1);
30 		seq_puts(file, "\n");
31 		seq_puts(file, "Count:  ");
32 		for (j = 0; j < 8; j++)
33 			seq_printf(file, "%8d | ", dev->aggr_stats[i * 8 + j]);
34 		seq_puts(file, "\n");
35 		seq_puts(file, "--------");
36 		for (j = 0; j < 8; j++)
37 			seq_puts(file, "-----------");
38 		seq_puts(file, "\n");
39 	}
40 
41 	return 0;
42 }
43 
44 static int
45 mt76x02_ampdu_stat_open(struct inode *inode, struct file *f)
46 {
47 	return single_open(f, mt76x02_ampdu_stat_read, inode->i_private);
48 }
49 
50 static int read_txpower(struct seq_file *file, void *data)
51 {
52 	struct mt76x02_dev *dev = dev_get_drvdata(file->private);
53 
54 	seq_printf(file, "Target power: %d\n", dev->target_power);
55 
56 	mt76_seq_puts_array(file, "Delta", dev->target_power_delta,
57 			    ARRAY_SIZE(dev->target_power_delta));
58 	return 0;
59 }
60 
61 static const struct file_operations fops_ampdu_stat = {
62 	.open = mt76x02_ampdu_stat_open,
63 	.read = seq_read,
64 	.llseek = seq_lseek,
65 	.release = single_release,
66 };
67 
68 static int
69 mt76x02_dfs_stat_read(struct seq_file *file, void *data)
70 {
71 	struct mt76x02_dev *dev = file->private;
72 	struct mt76x02_dfs_pattern_detector *dfs_pd = &dev->dfs_pd;
73 	int i;
74 
75 	seq_printf(file, "allocated sequences:\t%d\n",
76 		   dfs_pd->seq_stats.seq_pool_len);
77 	seq_printf(file, "used sequences:\t\t%d\n",
78 		   dfs_pd->seq_stats.seq_len);
79 	seq_puts(file, "\n");
80 
81 	for (i = 0; i < MT_DFS_NUM_ENGINES; i++) {
82 		seq_printf(file, "engine: %d\n", i);
83 		seq_printf(file, "  hw pattern detected:\t%d\n",
84 			   dfs_pd->stats[i].hw_pattern);
85 		seq_printf(file, "  hw pulse discarded:\t%d\n",
86 			   dfs_pd->stats[i].hw_pulse_discarded);
87 		seq_printf(file, "  sw pattern detected:\t%d\n",
88 			   dfs_pd->stats[i].sw_pattern);
89 	}
90 
91 	return 0;
92 }
93 
94 static int
95 mt76x02_dfs_stat_open(struct inode *inode, struct file *f)
96 {
97 	return single_open(f, mt76x02_dfs_stat_read, inode->i_private);
98 }
99 
100 static const struct file_operations fops_dfs_stat = {
101 	.open = mt76x02_dfs_stat_open,
102 	.read = seq_read,
103 	.llseek = seq_lseek,
104 	.release = single_release,
105 };
106 
107 static int read_agc(struct seq_file *file, void *data)
108 {
109 	struct mt76x02_dev *dev = dev_get_drvdata(file->private);
110 
111 	seq_printf(file, "avg_rssi: %d\n", dev->cal.avg_rssi_all);
112 	seq_printf(file, "low_gain: %d\n", dev->cal.low_gain);
113 	seq_printf(file, "false_cca: %d\n", dev->cal.false_cca);
114 	seq_printf(file, "agc_gain_adjust: %d\n", dev->cal.agc_gain_adjust);
115 
116 	return 0;
117 }
118 
119 static int
120 mt76_edcca_set(void *data, u64 val)
121 {
122 	struct mt76x02_dev *dev = data;
123 	enum nl80211_dfs_regions region = dev->mt76.region;
124 
125 	mutex_lock(&dev->mt76.mutex);
126 
127 	dev->ed_monitor_enabled = !!val;
128 	dev->ed_monitor = dev->ed_monitor_enabled &&
129 			  region == NL80211_DFS_ETSI;
130 	mt76x02_edcca_init(dev);
131 
132 	mutex_unlock(&dev->mt76.mutex);
133 
134 	return 0;
135 }
136 
137 static int
138 mt76_edcca_get(void *data, u64 *val)
139 {
140 	struct mt76x02_dev *dev = data;
141 
142 	*val = dev->ed_monitor_enabled;
143 	return 0;
144 }
145 
146 DEFINE_DEBUGFS_ATTRIBUTE(fops_edcca, mt76_edcca_get, mt76_edcca_set,
147 			 "%lld\n");
148 
149 void mt76x02_init_debugfs(struct mt76x02_dev *dev)
150 {
151 	struct dentry *dir;
152 
153 	dir = mt76_register_debugfs(&dev->mt76);
154 	if (!dir)
155 		return;
156 
157 	debugfs_create_u8("temperature", 0400, dir, &dev->cal.temp);
158 	debugfs_create_bool("tpc", 0600, dir, &dev->enable_tpc);
159 
160 	debugfs_create_file("edcca", 0600, dir, dev, &fops_edcca);
161 	debugfs_create_file("ampdu_stat", 0400, dir, dev, &fops_ampdu_stat);
162 	debugfs_create_file("dfs_stats", 0400, dir, dev, &fops_dfs_stat);
163 	debugfs_create_devm_seqfile(dev->mt76.dev, "txpower", dir,
164 				    read_txpower);
165 
166 	debugfs_create_devm_seqfile(dev->mt76.dev, "agc", dir, read_agc);
167 
168 	debugfs_create_u32("tx_hang_reset", 0400, dir, &dev->tx_hang_reset);
169 }
170 EXPORT_SYMBOL_GPL(mt76x02_init_debugfs);
171