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 #include "mt76.h"
17 
18 static int
19 mt76_reg_set(void *data, u64 val)
20 {
21 	struct mt76_dev *dev = data;
22 
23 	dev->bus->wr(dev, dev->debugfs_reg, val);
24 	return 0;
25 }
26 
27 static int
28 mt76_reg_get(void *data, u64 *val)
29 {
30 	struct mt76_dev *dev = data;
31 
32 	*val = dev->bus->rr(dev, dev->debugfs_reg);
33 	return 0;
34 }
35 
36 DEFINE_DEBUGFS_ATTRIBUTE(fops_regval, mt76_reg_get, mt76_reg_set,
37 			 "0x%08llx\n");
38 
39 static int
40 mt76_queues_read(struct seq_file *s, void *data)
41 {
42 	struct mt76_dev *dev = dev_get_drvdata(s->private);
43 	int i;
44 
45 	for (i = 0; i < ARRAY_SIZE(dev->q_tx); i++) {
46 		struct mt76_queue *q = &dev->q_tx[i];
47 
48 		if (!q->ndesc)
49 			continue;
50 
51 		seq_printf(s,
52 			   "%d:	queued=%d head=%d tail=%d swq_queued=%d\n",
53 			   i, q->queued, q->head, q->tail, q->swq_queued);
54 	}
55 
56 	return 0;
57 }
58 
59 void mt76_seq_puts_array(struct seq_file *file, const char *str,
60 			 s8 *val, int len)
61 {
62 	int i;
63 
64 	seq_printf(file, "%10s:", str);
65 	for (i = 0; i < len; i++)
66 		seq_printf(file, " %2d", val[i]);
67 	seq_puts(file, "\n");
68 }
69 EXPORT_SYMBOL_GPL(mt76_seq_puts_array);
70 
71 static int mt76_read_rate_txpower(struct seq_file *s, void *data)
72 {
73 	struct mt76_dev *dev = dev_get_drvdata(s->private);
74 
75 	mt76_seq_puts_array(s, "CCK", dev->rate_power.cck,
76 			    ARRAY_SIZE(dev->rate_power.cck));
77 	mt76_seq_puts_array(s, "OFDM", dev->rate_power.ofdm,
78 			    ARRAY_SIZE(dev->rate_power.ofdm));
79 	mt76_seq_puts_array(s, "STBC", dev->rate_power.stbc,
80 			    ARRAY_SIZE(dev->rate_power.stbc));
81 	mt76_seq_puts_array(s, "HT", dev->rate_power.ht,
82 			    ARRAY_SIZE(dev->rate_power.ht));
83 	mt76_seq_puts_array(s, "VHT", dev->rate_power.vht,
84 			    ARRAY_SIZE(dev->rate_power.vht));
85 	return 0;
86 }
87 
88 struct dentry *mt76_register_debugfs(struct mt76_dev *dev)
89 {
90 	struct dentry *dir;
91 
92 	dir = debugfs_create_dir("mt76", dev->hw->wiphy->debugfsdir);
93 	if (!dir)
94 		return NULL;
95 
96 	debugfs_create_u8("led_pin", 0600, dir, &dev->led_pin);
97 	debugfs_create_u32("regidx", 0600, dir, &dev->debugfs_reg);
98 	debugfs_create_file_unsafe("regval", 0600, dir, dev,
99 				   &fops_regval);
100 	debugfs_create_blob("eeprom", 0400, dir, &dev->eeprom);
101 	if (dev->otp.data)
102 		debugfs_create_blob("otp", 0400, dir, &dev->otp);
103 	debugfs_create_devm_seqfile(dev->dev, "queues", dir, mt76_queues_read);
104 	debugfs_create_devm_seqfile(dev->dev, "rate_txpower", dir,
105 				    mt76_read_rate_txpower);
106 
107 	return dir;
108 }
109 EXPORT_SYMBOL_GPL(mt76_register_debugfs);
110