summaryrefstree
path: root/kernel/techpack/display/msm/dp/secdp_logger.c
blob: b146d2e84a07b3fcedd93491d855cbd951c2b208 (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
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * DP logger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/time.h>
#include <linux/uaccess.h>
#include <linux/sched/clock.h>
#include <linux/secdp_logger.h>

#include "secdp_unit_test.h"

#define BUF_SIZE	SZ_64K
#define MAX_STR_LEN	160
#define PROC_FILE_NAME	"dplog"
#define LOG_PREFIX		"secdp"

static char log_buf[BUF_SIZE];
static unsigned int g_curpos;
static int is_secdp_logger_init;
static int is_buf_full;
static int log_max_count = -1;

void dp_logger_print_date_time(void)
{
	char tmp[64] = {0x0, };
	struct tm tm;
	u64 time;
	unsigned long nsec;
	unsigned long sec;

	time = local_clock();
	nsec = do_div(time, 1000000000);
	sec = get_seconds() - (sys_tz.tz_minuteswest * 60);
	time64_to_tm(sec, 0, &tm);
	snprintf(tmp, sizeof(tmp), "!@[%02d-%02d %02d:%02d:%02d.%03lu]",
		tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
		nsec / 1000000);

	secdp_logger_print("%s\n", tmp);
}

/* set max log count, if count is -1, no limit */
void secdp_logger_set_max_count(int count)
{
	log_max_count = count;

	dp_logger_print_date_time();
}

void secdp_logger_print(const char *fmt, ...)
{
	int len;
	va_list args;
	char buf[MAX_STR_LEN] = {0,};
	u64 time;
	unsigned long nsec;

	volatile unsigned int curpos;

	if (!is_secdp_logger_init)
		return;

	if (log_max_count == 0)
		return;
	else if (log_max_count > 0)
		log_max_count--;

	time = local_clock();
	nsec = do_div(time, 1000000000);
	len = snprintf(buf, sizeof(buf), "[%5lu.%06ld] ",
			(unsigned long)time, nsec / 1000);

	va_start(args, fmt);
	len += vsnprintf(buf + len, MAX_STR_LEN - len, fmt, args);
	va_end(args);

	if (len > MAX_STR_LEN)
		len = MAX_STR_LEN;

	curpos = g_curpos;
	if (curpos + len >= BUF_SIZE) {
		g_curpos = curpos = 0;
		is_buf_full = 1;
	}
	memcpy(log_buf + curpos, buf, len);
	g_curpos += len;
}

void secdp_logger_hex_dump(void *buf, void *pref, size_t size)
{
	uint8_t *ptr = buf;
	size_t i;
	char tmp[128] = {0x0, };
	char *ptmp = tmp;
	int len;

	if (!is_secdp_logger_init)
		return;

	if (log_max_count == 0)
		return;
	else if (log_max_count > 0)
		log_max_count--;

	for (i = 0; i < size; i++) {
		len = snprintf(ptmp, 4, "%02x ", *ptr++);
		ptmp = ptmp + len;
		if (((i+1)%16) == 0) {
			secdp_logger_print("%s%s\n", pref, tmp);
			ptmp = tmp;
		}
	}

	len = i % 16;
	if (len != 0) {
		tmp[len] = 0x0;
		secdp_logger_print("%s\n", tmp);
	}
}

static ssize_t secdp_logger_read(struct file *file, char __user *buf,
		size_t len, loff_t *offset)
{
	loff_t pos = *offset;
	ssize_t count;
	size_t size;
	volatile unsigned int curpos = g_curpos;

	if (is_buf_full || BUF_SIZE <= curpos)
		size = BUF_SIZE;
	else
		size = (size_t)curpos;

	if (pos >= size)
		return 0;

	count = min(len, size);

	if ((pos + count) > size)
		count = size - pos;

	if (copy_to_user(buf, log_buf + pos, count))
		return -EFAULT;

	*offset += count;

	return count;
}

static const struct file_operations secdp_logger_ops = {
	.owner = THIS_MODULE,
	.read = secdp_logger_read,
};

int secdp_logger_init(void)
{
	struct proc_dir_entry *entry;

	if (is_secdp_logger_init)
		return 0;

	entry = proc_create(PROC_FILE_NAME, 0444, NULL, &secdp_logger_ops);
	if (!entry) {
		pr_err("%s: failed to create proc entry\n", __func__);
		return 0;
	}

	proc_set_size(entry, BUF_SIZE);
	is_secdp_logger_init = 1;
	secdp_logger_print("dp logger init ok\n");

	return 0;
}