summaryrefslogtreecommitdiff
path: root/debugfs.c
blob: 3cf4dc46a618235b714e4297b15ee338148a18b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Driver for WiFi Performance Tracker
 *
 * Copyright 2022 Google LLC.
 *
 * Author: Star Chang <starchang@google.com>
 */
#include <linux/kernel.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/timekeeping.h>
#include <linux/rtc.h>
#include "core.h"
#include "debugfs.h"

static const char *const state2str[WLAN_SCENE_MAX] = {
	"Idle", "Web", "Youtube", "Low latency", "Throughput"
};

#define READ_BUF_SIZE 1024
static ssize_t action_read(struct file *file, char __user *userbuf, size_t count, loff_t *ppos)
{
	struct wlan_ptracker_core *core = file->private_data;
	char *buf;
	int len = 0;
	int i;
	ssize_t ret;

	buf = vmalloc(READ_BUF_SIZE);

	if (!buf)
		return 0;

	len += scnprintf(buf + len, READ_BUF_SIZE - len,
		"==== DSCP to AC mapping table ===\n");
	for (i = 0 ; i < DSCP_MAX; i++) {
		if (!core->dscp_to_ac[i])
			continue;
		len += scnprintf(buf + len, READ_BUF_SIZE - len,
			"dscp[%d]  : %u\n", i, core->dscp_to_ac[i]);
	}
	ret = simple_read_from_buffer(userbuf, count, ppos, buf, len);
	vfree(buf);
	return ret;
}

static void update_dscp(struct wlan_ptracker_core *core, u32 dscp, u32 ac)
{
	ptracker_info(core, "dscp %d, ac: %d\n", dscp, ac);
	if (dscp > DSCP_MASK)
		return;
	if (ac > WMM_AC_VO)
		return;

	core->dscp_to_ac[dscp] = ac;
}

static ssize_t action_write(struct file *file,
		const char __user *buf, size_t len, loff_t *ppos)
{
	struct wlan_ptracker_core *core = file->private_data;
	struct wlan_ptracker_debugfs *debugfs = &core->debugfs;
	u32 action;

	if (kstrtouint_from_user(buf, len, 10, &action))
		return -EFAULT;

	/* active action */
	switch (action) {
	case ACTION_DSCP_UPDATE:
		update_dscp(core, debugfs->dscp, debugfs->ac);
		break;
	default:
		ptracker_err(core, "action %d is not supported!\n", action);
		return -ENOTSUPP;
	}
	return 0;
}

static const struct file_operations dscp_ops = {
	.open = simple_open,
	.read = action_read,
	.write = action_write,
	.llseek = generic_file_llseek,
};

int wlan_ptracker_debugfs_init(struct wlan_ptracker_debugfs *debugfs)
{
	struct wlan_ptracker_core *core = container_of(
		debugfs, struct wlan_ptracker_core, debugfs);

	debugfs->root = debugfs_create_dir("wlan_ptracker", NULL);
	if (!debugfs->root)
		return -ENODEV;
	debugfs_create_file("action", 0600, debugfs->root, core, &dscp_ops);
	debugfs_create_u32("dscp", 0600, debugfs->root, &debugfs->dscp);
	debugfs_create_u32("ac", 0600, debugfs->root, &debugfs->ac);
	return 0;
}

void wlan_ptracker_debugfs_exit(struct wlan_ptracker_debugfs *debugfs)
{
	debugfs_remove_recursive(debugfs->root);
	debugfs->root = NULL;
}

struct history_manager *wlan_ptracker_history_create(int entry_count, int entry_size)
{
	struct history_manager *hm;

	if (entry_count < 0 || entry_size < sizeof(struct history_entry))
		return NULL;

	hm = kzalloc(sizeof(struct history_manager) + entry_size * entry_count, GFP_KERNEL);
	if (!hm)
		return NULL;

	/* initial manager */
	hm->entry_count = entry_count;
	hm->entry_size = entry_size;
	hm->cur = 0;
	hm->round = 0;
	mutex_init(&hm->mutex);
	return hm;
}

void wlan_ptracker_history_destroy(struct history_manager *hm)
{
	if (hm)
		kfree(hm);
}

void * wlan_ptracker_history_store(struct history_manager *hm, u32 state)
{
	struct history_entry *entry;

	if (!hm->entry_count)
		return NULL;

	entry = (struct history_entry *)(hm->entries + (hm->cur * hm->entry_size));
	entry->state = state;
	entry->valid = true;
	ktime_get_real_ts64(&entry->ts);

	/* update dytwt history */
	mutex_lock(&hm->mutex);
	hm->cur++;
	if (hm->cur / hm->entry_count)
		hm->round++;
	hm->cur %= hm->entry_count;
	mutex_unlock(&hm->mutex);
	return entry;
}

static int history_get_tm(struct history_entry *entry, char *time, size_t len)
{
	struct rtc_time tm;

	rtc_time64_to_tm(entry->ts.tv_sec - (sys_tz.tz_minuteswest * 60), &tm);
	return scnprintf(time, len, "%ptRs", &tm);
}

size_t wlan_ptracker_history_read(struct history_manager *hm, char *buf, int buf_len)
{
	u8 *ptr;
	struct history_entry *cur, *next;
	int len = 0;
	int i, j;

	len += scnprintf(buf + len, buf_len - len,
		"==== %s History ===\n", hm->name);
	len += scnprintf(buf + len, buf_len - len,
		"round: %d, cur: %d, entry len: %d,  size: %d\n",
		hm->round, hm->cur, hm->entry_count, hm->entry_size);

	ptr = hm->entries;
	for (i = 0 ; i < hm->entry_count; i++) {
		cur = (struct history_entry *) ptr;
		if (!cur->valid)
			break;
		j = (i + hm->entry_count + 1) % hm->entry_count;
		next = (struct history_entry *)(hm->entries + (j * hm->entry_size));
		len += scnprintf(buf + len, buf_len - len, "%02d: ", i);
		len += history_get_tm(cur, buf + len, buf_len - len);
		len += scnprintf(buf + len, buf_len - len, "%12s =>", state2str[cur->state]);
		if (hm->priv_read)
			len += hm->priv_read(cur, next->valid ? next : NULL, buf + len,
				buf_len - len);
		len += scnprintf(buf + len, buf_len - len, "\n");
		ptr += hm->entry_size;
	}
	return len;
}