blob: 7d4448a9fbbd37b80dfc8dfeed496b84b40c6c56 [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
2 * sysfs.c - sysfs support
3 *
4 * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
5 *
6 * This code is licenced under the GPL.
7 */
8
9#include <linux/kernel.h>
10#include <linux/cpuidle.h>
11#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040013#include <linux/cpu.h>
ShuoX Liu3a533962012-03-28 15:19:11 -070014#include <linux/capability.h>
Daniel Lezcano8f3e9952012-10-31 01:09:02 +010015#include <linux/device.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040016
17#include "cpuidle.h"
18
19static unsigned int sysfs_switch;
20static int __init cpuidle_sysfs_setup(char *unused)
21{
22 sysfs_switch = 1;
23 return 1;
24}
25__setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
26
Kay Sievers8a25a2f2011-12-21 14:29:42 -080027static ssize_t show_available_governors(struct device *dev,
28 struct device_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070029 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040030{
31 ssize_t i = 0;
32 struct cpuidle_governor *tmp;
33
34 mutex_lock(&cpuidle_lock);
35 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
Daniel Lezcanof89ae892013-06-12 15:08:50 +020036 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) -
37 CPUIDLE_NAME_LEN - 2))
Len Brown4f86d3a2007-10-03 18:58:00 -040038 goto out;
39 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
40 }
41
42out:
43 i+= sprintf(&buf[i], "\n");
44 mutex_unlock(&cpuidle_lock);
45 return i;
46}
47
Kay Sievers8a25a2f2011-12-21 14:29:42 -080048static ssize_t show_current_driver(struct device *dev,
49 struct device_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070050 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040051{
52 ssize_t ret;
Len Brown752138d2010-05-22 16:57:26 -040053 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -040054
55 spin_lock(&cpuidle_driver_lock);
Len Brown752138d2010-05-22 16:57:26 -040056 if (cpuidle_driver)
57 ret = sprintf(buf, "%s\n", cpuidle_driver->name);
Len Brown4f86d3a2007-10-03 18:58:00 -040058 else
59 ret = sprintf(buf, "none\n");
60 spin_unlock(&cpuidle_driver_lock);
61
62 return ret;
63}
64
Kay Sievers8a25a2f2011-12-21 14:29:42 -080065static ssize_t show_current_governor(struct device *dev,
66 struct device_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070067 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040068{
69 ssize_t ret;
70
71 mutex_lock(&cpuidle_lock);
72 if (cpuidle_curr_governor)
73 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
74 else
75 ret = sprintf(buf, "none\n");
76 mutex_unlock(&cpuidle_lock);
77
78 return ret;
79}
80
Kay Sievers8a25a2f2011-12-21 14:29:42 -080081static ssize_t store_current_governor(struct device *dev,
82 struct device_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070083 const char *buf, size_t count)
Len Brown4f86d3a2007-10-03 18:58:00 -040084{
85 char gov_name[CPUIDLE_NAME_LEN];
86 int ret = -EINVAL;
87 size_t len = count;
88 struct cpuidle_governor *gov;
89
90 if (!len || len >= sizeof(gov_name))
91 return -EINVAL;
92
93 memcpy(gov_name, buf, len);
94 gov_name[len] = '\0';
95 if (gov_name[len - 1] == '\n')
96 gov_name[--len] = '\0';
97
98 mutex_lock(&cpuidle_lock);
99
100 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
101 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
102 ret = cpuidle_switch_governor(gov);
103 break;
104 }
105 }
106
107 mutex_unlock(&cpuidle_lock);
108
109 if (ret)
110 return ret;
111 else
112 return count;
113}
114
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800115static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
116static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
Len Brown4f86d3a2007-10-03 18:58:00 -0400117
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800118static struct attribute *cpuidle_default_attrs[] = {
119 &dev_attr_current_driver.attr,
120 &dev_attr_current_governor_ro.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400121 NULL
122};
123
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800124static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
125static DEVICE_ATTR(current_governor, 0644, show_current_governor,
126 store_current_governor);
Len Brown4f86d3a2007-10-03 18:58:00 -0400127
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800128static struct attribute *cpuidle_switch_attrs[] = {
129 &dev_attr_available_governors.attr,
130 &dev_attr_current_driver.attr,
131 &dev_attr_current_governor.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400132 NULL
133};
134
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800135static struct attribute_group cpuidle_attr_group = {
136 .attrs = cpuidle_default_attrs,
Len Brown4f86d3a2007-10-03 18:58:00 -0400137 .name = "cpuidle",
138};
139
140/**
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800141 * cpuidle_add_interface - add CPU global sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400142 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800143int cpuidle_add_interface(struct device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400144{
145 if (sysfs_switch)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800146 cpuidle_attr_group.attrs = cpuidle_switch_attrs;
Len Brown4f86d3a2007-10-03 18:58:00 -0400147
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800148 return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
Len Brown4f86d3a2007-10-03 18:58:00 -0400149}
150
151/**
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800152 * cpuidle_remove_interface - remove CPU global sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400153 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800154void cpuidle_remove_interface(struct device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400155{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800156 sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
Len Brown4f86d3a2007-10-03 18:58:00 -0400157}
158
159struct cpuidle_attr {
160 struct attribute attr;
161 ssize_t (*show)(struct cpuidle_device *, char *);
162 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
163};
164
165#define define_one_ro(_name, show) \
166 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
167#define define_one_rw(_name, show, store) \
168 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
169
170#define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
171#define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200172
173static ssize_t cpuidle_show(struct kobject *kobj, struct attribute *attr,
174 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -0400175{
176 int ret = -EIO;
177 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200178 struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
Len Brown4f86d3a2007-10-03 18:58:00 -0400179
180 if (cattr->show) {
181 mutex_lock(&cpuidle_lock);
182 ret = cattr->show(dev, buf);
183 mutex_unlock(&cpuidle_lock);
184 }
185 return ret;
186}
187
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200188static ssize_t cpuidle_store(struct kobject *kobj, struct attribute *attr,
189 const char *buf, size_t count)
Len Brown4f86d3a2007-10-03 18:58:00 -0400190{
191 int ret = -EIO;
192 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200193 struct cpuidle_attr *cattr = attr_to_cpuidleattr(attr);
Len Brown4f86d3a2007-10-03 18:58:00 -0400194
195 if (cattr->store) {
196 mutex_lock(&cpuidle_lock);
197 ret = cattr->store(dev, buf, count);
198 mutex_unlock(&cpuidle_lock);
199 }
200 return ret;
201}
202
Emese Revfy52cf25d2010-01-19 02:58:23 +0100203static const struct sysfs_ops cpuidle_sysfs_ops = {
Len Brown4f86d3a2007-10-03 18:58:00 -0400204 .show = cpuidle_show,
205 .store = cpuidle_store,
206};
207
208static void cpuidle_sysfs_release(struct kobject *kobj)
209{
210 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
211
212 complete(&dev->kobj_unregister);
213}
214
215static struct kobj_type ktype_cpuidle = {
216 .sysfs_ops = &cpuidle_sysfs_ops,
217 .release = cpuidle_sysfs_release,
218};
219
220struct cpuidle_state_attr {
221 struct attribute attr;
Deepthi Dharwar42027352011-10-28 16:20:33 +0530222 ssize_t (*show)(struct cpuidle_state *, \
223 struct cpuidle_state_usage *, char *);
ShuoX Liudc7fd272012-07-03 19:05:31 +0200224 ssize_t (*store)(struct cpuidle_state *, \
225 struct cpuidle_state_usage *, const char *, size_t);
Len Brown4f86d3a2007-10-03 18:58:00 -0400226};
227
228#define define_one_state_ro(_name, show) \
229static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
230
ShuoX Liu3a533962012-03-28 15:19:11 -0700231#define define_one_state_rw(_name, show, store) \
232static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
233
Len Brown4f86d3a2007-10-03 18:58:00 -0400234#define define_show_state_function(_name) \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530235static ssize_t show_state_##_name(struct cpuidle_state *state, \
236 struct cpuidle_state_usage *state_usage, char *buf) \
Len Brown4f86d3a2007-10-03 18:58:00 -0400237{ \
238 return sprintf(buf, "%u\n", state->_name);\
239}
240
ShuoX Liudc7fd272012-07-03 19:05:31 +0200241#define define_store_state_ull_function(_name) \
ShuoX Liu3a533962012-03-28 15:19:11 -0700242static ssize_t store_state_##_name(struct cpuidle_state *state, \
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200243 struct cpuidle_state_usage *state_usage, \
244 const char *buf, size_t size) \
ShuoX Liu3a533962012-03-28 15:19:11 -0700245{ \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200246 unsigned long long value; \
ShuoX Liu3a533962012-03-28 15:19:11 -0700247 int err; \
248 if (!capable(CAP_SYS_ADMIN)) \
249 return -EPERM; \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200250 err = kstrtoull(buf, 0, &value); \
ShuoX Liu3a533962012-03-28 15:19:11 -0700251 if (err) \
252 return err; \
253 if (value) \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200254 state_usage->_name = 1; \
ShuoX Liu3a533962012-03-28 15:19:11 -0700255 else \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200256 state_usage->_name = 0; \
ShuoX Liu3a533962012-03-28 15:19:11 -0700257 return size; \
258}
259
Yi Yang8b78cf62008-02-25 08:46:12 +0800260#define define_show_state_ull_function(_name) \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530261static ssize_t show_state_##_name(struct cpuidle_state *state, \
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200262 struct cpuidle_state_usage *state_usage, \
263 char *buf) \
Yi Yang8b78cf62008-02-25 08:46:12 +0800264{ \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530265 return sprintf(buf, "%llu\n", state_usage->_name);\
Yi Yang8b78cf62008-02-25 08:46:12 +0800266}
267
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800268#define define_show_state_str_function(_name) \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530269static ssize_t show_state_##_name(struct cpuidle_state *state, \
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200270 struct cpuidle_state_usage *state_usage, \
271 char *buf) \
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800272{ \
273 if (state->_name[0] == '\0')\
274 return sprintf(buf, "<null>\n");\
275 return sprintf(buf, "%s\n", state->_name);\
Len Brown4f86d3a2007-10-03 18:58:00 -0400276}
277
278define_show_state_function(exit_latency)
279define_show_state_function(power_usage)
Yi Yang8b78cf62008-02-25 08:46:12 +0800280define_show_state_ull_function(usage)
281define_show_state_ull_function(time)
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800282define_show_state_str_function(name)
283define_show_state_str_function(desc)
ShuoX Liudc7fd272012-07-03 19:05:31 +0200284define_show_state_ull_function(disable)
285define_store_state_ull_function(disable)
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800286
Len Brown4f86d3a2007-10-03 18:58:00 -0400287define_one_state_ro(name, show_state_name);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800288define_one_state_ro(desc, show_state_desc);
Len Brown4f86d3a2007-10-03 18:58:00 -0400289define_one_state_ro(latency, show_state_exit_latency);
290define_one_state_ro(power, show_state_power_usage);
291define_one_state_ro(usage, show_state_usage);
292define_one_state_ro(time, show_state_time);
ShuoX Liu3a533962012-03-28 15:19:11 -0700293define_one_state_rw(disable, show_state_disable, store_state_disable);
Len Brown4f86d3a2007-10-03 18:58:00 -0400294
295static struct attribute *cpuidle_state_default_attrs[] = {
296 &attr_name.attr,
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800297 &attr_desc.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400298 &attr_latency.attr,
299 &attr_power.attr,
300 &attr_usage.attr,
301 &attr_time.attr,
ShuoX Liu3a533962012-03-28 15:19:11 -0700302 &attr_disable.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400303 NULL
304};
305
Daniel Lezcano349631e2012-10-31 01:05:16 +0100306struct cpuidle_state_kobj {
307 struct cpuidle_state *state;
308 struct cpuidle_state_usage *state_usage;
309 struct completion kobj_unregister;
310 struct kobject kobj;
311};
312
Len Brown4f86d3a2007-10-03 18:58:00 -0400313#define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
314#define kobj_to_state(k) (kobj_to_state_obj(k)->state)
Deepthi Dharwar42027352011-10-28 16:20:33 +0530315#define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
Len Brown4f86d3a2007-10-03 18:58:00 -0400316#define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200317
318static ssize_t cpuidle_state_show(struct kobject *kobj, struct attribute *attr,
319 char * buf)
Len Brown4f86d3a2007-10-03 18:58:00 -0400320{
321 int ret = -EIO;
322 struct cpuidle_state *state = kobj_to_state(kobj);
Deepthi Dharwar42027352011-10-28 16:20:33 +0530323 struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400324 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
325
326 if (cattr->show)
Deepthi Dharwar42027352011-10-28 16:20:33 +0530327 ret = cattr->show(state, state_usage, buf);
Len Brown4f86d3a2007-10-03 18:58:00 -0400328
329 return ret;
330}
331
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200332static ssize_t cpuidle_state_store(struct kobject *kobj, struct attribute *attr,
333 const char *buf, size_t size)
ShuoX Liu3a533962012-03-28 15:19:11 -0700334{
335 int ret = -EIO;
336 struct cpuidle_state *state = kobj_to_state(kobj);
ShuoX Liudc7fd272012-07-03 19:05:31 +0200337 struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
ShuoX Liu3a533962012-03-28 15:19:11 -0700338 struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
339
340 if (cattr->store)
ShuoX Liudc7fd272012-07-03 19:05:31 +0200341 ret = cattr->store(state, state_usage, buf, size);
ShuoX Liu3a533962012-03-28 15:19:11 -0700342
343 return ret;
344}
345
Emese Revfy52cf25d2010-01-19 02:58:23 +0100346static const struct sysfs_ops cpuidle_state_sysfs_ops = {
Len Brown4f86d3a2007-10-03 18:58:00 -0400347 .show = cpuidle_state_show,
ShuoX Liu3a533962012-03-28 15:19:11 -0700348 .store = cpuidle_state_store,
Len Brown4f86d3a2007-10-03 18:58:00 -0400349};
350
351static void cpuidle_state_sysfs_release(struct kobject *kobj)
352{
353 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
354
355 complete(&state_obj->kobj_unregister);
356}
357
358static struct kobj_type ktype_state_cpuidle = {
359 .sysfs_ops = &cpuidle_state_sysfs_ops,
360 .default_attrs = cpuidle_state_default_attrs,
361 .release = cpuidle_state_sysfs_release,
362};
363
Jesper Juhl42b16b32011-01-17 00:09:38 +0100364static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
Len Brown4f86d3a2007-10-03 18:58:00 -0400365{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800366 kobject_put(&device->kobjs[i]->kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400367 wait_for_completion(&device->kobjs[i]->kobj_unregister);
368 kfree(device->kobjs[i]);
369 device->kobjs[i] = NULL;
370}
371
372/**
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000373 * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400374 * @device: the target device
375 */
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000376static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
Len Brown4f86d3a2007-10-03 18:58:00 -0400377{
378 int i, ret = -ENOMEM;
379 struct cpuidle_state_kobj *kobj;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000380 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
Len Brown4f86d3a2007-10-03 18:58:00 -0400381
382 /* state statistics */
Krzysztof Mazur392370e2013-01-11 23:20:09 +0100383 for (i = 0; i < device->state_count; i++) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400384 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
385 if (!kobj)
386 goto error_state;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530387 kobj->state = &drv->states[i];
Deepthi Dharwar42027352011-10-28 16:20:33 +0530388 kobj->state_usage = &device->states_usage[i];
Len Brown4f86d3a2007-10-03 18:58:00 -0400389 init_completion(&kobj->kobj_unregister);
390
Daniel Lezcanoe45a00d2012-10-26 12:26:32 +0200391 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
392 &device->kobj, "state%d", i);
Len Brown4f86d3a2007-10-03 18:58:00 -0400393 if (ret) {
394 kfree(kobj);
395 goto error_state;
396 }
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400397 kobject_uevent(&kobj->kobj, KOBJ_ADD);
Len Brown4f86d3a2007-10-03 18:58:00 -0400398 device->kobjs[i] = kobj;
399 }
400
401 return 0;
402
403error_state:
404 for (i = i - 1; i >= 0; i--)
405 cpuidle_free_state_kobj(device, i);
406 return ret;
407}
408
409/**
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000410 * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400411 * @device: the target device
412 */
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000413static void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
Len Brown4f86d3a2007-10-03 18:58:00 -0400414{
415 int i;
416
417 for (i = 0; i < device->state_count; i++)
418 cpuidle_free_state_kobj(device, i);
419}
420
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000421#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
422#define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
423#define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)
424
425#define define_one_driver_ro(_name, show) \
426 static struct cpuidle_driver_attr attr_driver_##_name = \
427 __ATTR(_name, 0644, show, NULL)
428
429struct cpuidle_driver_kobj {
430 struct cpuidle_driver *drv;
431 struct completion kobj_unregister;
432 struct kobject kobj;
433};
434
435struct cpuidle_driver_attr {
436 struct attribute attr;
437 ssize_t (*show)(struct cpuidle_driver *, char *);
438 ssize_t (*store)(struct cpuidle_driver *, const char *, size_t);
439};
440
441static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
442{
443 ssize_t ret;
444
445 spin_lock(&cpuidle_driver_lock);
446 ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
447 spin_unlock(&cpuidle_driver_lock);
448
449 return ret;
450}
451
452static void cpuidle_driver_sysfs_release(struct kobject *kobj)
453{
454 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
455 complete(&driver_kobj->kobj_unregister);
456}
457
Daniel Lezcanof89ae892013-06-12 15:08:50 +0200458static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute *attr,
459 char *buf)
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000460{
461 int ret = -EIO;
462 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
463 struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
464
465 if (dattr->show)
466 ret = dattr->show(driver_kobj->drv, buf);
467
468 return ret;
469}
470
471static ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr,
472 const char *buf, size_t size)
473{
474 int ret = -EIO;
475 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
476 struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
477
478 if (dattr->store)
479 ret = dattr->store(driver_kobj->drv, buf, size);
480
481 return ret;
482}
483
484define_one_driver_ro(name, show_driver_name);
485
486static const struct sysfs_ops cpuidle_driver_sysfs_ops = {
487 .show = cpuidle_driver_show,
488 .store = cpuidle_driver_store,
489};
490
491static struct attribute *cpuidle_driver_default_attrs[] = {
492 &attr_driver_name.attr,
493 NULL
494};
495
496static struct kobj_type ktype_driver_cpuidle = {
497 .sysfs_ops = &cpuidle_driver_sysfs_ops,
498 .default_attrs = cpuidle_driver_default_attrs,
499 .release = cpuidle_driver_sysfs_release,
500};
501
502/**
503 * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
504 * @device: the target device
505 */
506static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
507{
508 struct cpuidle_driver_kobj *kdrv;
509 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
510 int ret;
511
512 kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL);
513 if (!kdrv)
514 return -ENOMEM;
515
516 kdrv->drv = drv;
517 init_completion(&kdrv->kobj_unregister);
518
519 ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle,
520 &dev->kobj, "driver");
521 if (ret) {
522 kfree(kdrv);
523 return ret;
524 }
525
526 kobject_uevent(&kdrv->kobj, KOBJ_ADD);
527 dev->kobj_driver = kdrv;
528
529 return ret;
530}
531
532/**
533 * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
534 * @device: the target device
535 */
536static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
537{
538 struct cpuidle_driver_kobj *kdrv = dev->kobj_driver;
539 kobject_put(&kdrv->kobj);
540 wait_for_completion(&kdrv->kobj_unregister);
541 kfree(kdrv);
542}
543#else
544static inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
545{
546 return 0;
547}
548
549static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
550{
551 ;
552}
553#endif
554
555/**
556 * cpuidle_add_device_sysfs - adds device specific sysfs attributes
557 * @device: the target device
558 */
559int cpuidle_add_device_sysfs(struct cpuidle_device *device)
560{
561 int ret;
562
563 ret = cpuidle_add_state_sysfs(device);
564 if (ret)
565 return ret;
566
567 ret = cpuidle_add_driver_sysfs(device);
568 if (ret)
569 cpuidle_remove_state_sysfs(device);
570 return ret;
571}
572
573/**
574 * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
575 * @device : the target device
576 */
577void cpuidle_remove_device_sysfs(struct cpuidle_device *device)
578{
579 cpuidle_remove_driver_sysfs(device);
580 cpuidle_remove_state_sysfs(device);
581}
582
Len Brown4f86d3a2007-10-03 18:58:00 -0400583/**
584 * cpuidle_add_sysfs - creates a sysfs instance for the target device
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800585 * @dev: the target device
Len Brown4f86d3a2007-10-03 18:58:00 -0400586 */
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200587int cpuidle_add_sysfs(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400588{
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200589 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400590 int error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400591
Daniel Lezcanoe45a00d2012-10-26 12:26:32 +0200592 init_completion(&dev->kobj_unregister);
593
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800594 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400595 "cpuidle");
596 if (!error)
597 kobject_uevent(&dev->kobj, KOBJ_ADD);
598 return error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400599}
600
601/**
602 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800603 * @dev: the target device
Len Brown4f86d3a2007-10-03 18:58:00 -0400604 */
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200605void cpuidle_remove_sysfs(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400606{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800607 kobject_put(&dev->kobj);
Daniel Lezcanoe45a00d2012-10-26 12:26:32 +0200608 wait_for_completion(&dev->kobj_unregister);
Len Brown4f86d3a2007-10-03 18:58:00 -0400609}