summaryrefslogtreecommitdiff
path: root/drivers/cpuquiet/cpuquiet_attribute.c
blob: 9f1aa430149d06b8207f49081a0325e85b88957a (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
/*
 * Copyright (c) 2012 NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/cpuquiet.h>

ssize_t show_int_attribute(struct cpuquiet_attribute *cattr, char *buf)
{
	return sprintf(buf, "%d\n", *((int *)cattr->param));
}

ssize_t store_int_attribute(struct cpuquiet_attribute *cattr,
					const char *buf, size_t count)
{
	int err, val;

	err = kstrtoint(buf, 0, &val);
	if (err < 0)
		return err;

	*((int *)(cattr->param)) = val;

	if (cattr->store_callback)
		cattr->store_callback(cattr);

	return count;
}

ssize_t show_bool_attribute(struct cpuquiet_attribute *cattr, char *buf)
{
	return sprintf(buf, "%d\n", *((bool *)cattr->param));
}

ssize_t store_bool_attribute(struct cpuquiet_attribute *cattr,
					const char *buf, size_t count)
{
	int err, val;

	err = kstrtoint(buf, 0, &val);
	if (err < 0)
		return err;

	if (val < 0 || val > 1)
		return -EINVAL;

	*((bool *)(cattr->param)) = val;

	if (cattr->store_callback)
		cattr->store_callback(cattr);

	return count;
}

ssize_t show_uint_attribute(struct cpuquiet_attribute *cattr, char *buf)
{
	return sprintf(buf, "%u\n", *((unsigned int *)cattr->param));
}

ssize_t store_uint_attribute(struct cpuquiet_attribute *cattr,
					const char *buf, size_t count)
{
	int err;
	unsigned int val;

	err = kstrtouint(buf, 0, &val);
	if (err < 0)
		return err;

	*((unsigned int *)(cattr->param)) = val;

	if (cattr->store_callback)
		cattr->store_callback(cattr);

	return count;
}

ssize_t store_ulong_attribute(struct cpuquiet_attribute *cattr,
					const char *buf, size_t count)
{
	int err;
	unsigned long val;

	err = kstrtoul(buf, 0, &val);
	if (err < 0)
		return err;

	*((unsigned long *)(cattr->param)) = val;

	if (cattr->store_callback)
		cattr->store_callback(cattr);

	return count;
}

ssize_t show_ulong_attribute(struct cpuquiet_attribute *cattr,
					char *buf)
{
	return sprintf(buf, "%lu\n", *((unsigned long *)cattr->param));
}

ssize_t cpuquiet_auto_sysfs_store(struct kobject *kobj,
	struct attribute *attr, const char *buf, size_t count)
{
	struct cpuquiet_attribute *cattr =
		container_of(attr, struct cpuquiet_attribute, attr);

	if (cattr->store)
		return cattr->store(cattr, buf, count);

	return -EINVAL;
}

ssize_t cpuquiet_auto_sysfs_show(struct kobject *kobj,
		struct attribute *attr, char *buf)
{
	struct cpuquiet_attribute *cattr =
		container_of(attr, struct cpuquiet_attribute, attr);

	return cattr->show(cattr, buf);
}