cpuquiet: Adding runnable thread knobs
[firefly-linux-kernel-4.4.55.git] / drivers / cpuquiet / cpuquiet_attribute.c
1 /*
2  * Copyright (c) 2012 NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  */
17
18 #include <linux/cpuquiet.h>
19
20 ssize_t show_int_attribute(struct cpuquiet_attribute *cattr, char *buf)
21 {
22         return sprintf(buf, "%d\n", *((int *)cattr->param));
23 }
24
25 ssize_t store_int_attribute(struct cpuquiet_attribute *cattr,
26                                         const char *buf, size_t count)
27 {
28         int err, val;
29
30         err = kstrtoint(buf, 0, &val);
31         if (err < 0)
32                 return err;
33
34         *((int *)(cattr->param)) = val;
35
36         if (cattr->store_callback)
37                 cattr->store_callback(cattr);
38
39         return count;
40 }
41
42 ssize_t show_bool_attribute(struct cpuquiet_attribute *cattr, char *buf)
43 {
44         return sprintf(buf, "%d\n", *((bool *)cattr->param));
45 }
46
47 ssize_t store_bool_attribute(struct cpuquiet_attribute *cattr,
48                                         const char *buf, size_t count)
49 {
50         int err, val;
51
52         err = kstrtoint(buf, 0, &val);
53         if (err < 0)
54                 return err;
55
56         if (val < 0 || val > 1)
57                 return -EINVAL;
58
59         *((bool *)(cattr->param)) = val;
60
61         if (cattr->store_callback)
62                 cattr->store_callback(cattr);
63
64         return count;
65 }
66
67 ssize_t show_uint_attribute(struct cpuquiet_attribute *cattr, char *buf)
68 {
69         return sprintf(buf, "%u\n", *((unsigned int *)cattr->param));
70 }
71
72 ssize_t store_uint_attribute(struct cpuquiet_attribute *cattr,
73                                         const char *buf, size_t count)
74 {
75         int err;
76         unsigned int val;
77
78         err = kstrtouint(buf, 0, &val);
79         if (err < 0)
80                 return err;
81
82         *((unsigned int *)(cattr->param)) = val;
83
84         if (cattr->store_callback)
85                 cattr->store_callback(cattr);
86
87         return count;
88 }
89
90 ssize_t store_ulong_attribute(struct cpuquiet_attribute *cattr,
91                                         const char *buf, size_t count)
92 {
93         int err;
94         unsigned long val;
95
96         err = kstrtoul(buf, 0, &val);
97         if (err < 0)
98                 return err;
99
100         *((unsigned long *)(cattr->param)) = val;
101
102         if (cattr->store_callback)
103                 cattr->store_callback(cattr);
104
105         return count;
106 }
107
108 ssize_t show_ulong_attribute(struct cpuquiet_attribute *cattr,
109                                         char *buf)
110 {
111         return sprintf(buf, "%lu\n", *((unsigned long *)cattr->param));
112 }
113
114 ssize_t cpuquiet_auto_sysfs_store(struct kobject *kobj,
115         struct attribute *attr, const char *buf, size_t count)
116 {
117         struct cpuquiet_attribute *cattr =
118                 container_of(attr, struct cpuquiet_attribute, attr);
119
120         if (cattr->store)
121                 return cattr->store(cattr, buf, count);
122
123         return -EINVAL;
124 }
125
126 ssize_t cpuquiet_auto_sysfs_show(struct kobject *kobj,
127                 struct attribute *attr, char *buf)
128 {
129         struct cpuquiet_attribute *cattr =
130                 container_of(attr, struct cpuquiet_attribute, attr);
131
132         return cattr->show(cattr, buf);
133 }