blob: 428754af62366cda8fe71dee47b128c5da602969 [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) {
36 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
37 goto out;
38 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
39 }
40
41out:
42 i+= sprintf(&buf[i], "\n");
43 mutex_unlock(&cpuidle_lock);
44 return i;
45}
46
Kay Sievers8a25a2f2011-12-21 14:29:42 -080047static ssize_t show_current_driver(struct device *dev,
48 struct device_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070049 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040050{
51 ssize_t ret;
Len Brown752138d2010-05-22 16:57:26 -040052 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
Len Brown4f86d3a2007-10-03 18:58:00 -040053
54 spin_lock(&cpuidle_driver_lock);
Len Brown752138d2010-05-22 16:57:26 -040055 if (cpuidle_driver)
56 ret = sprintf(buf, "%s\n", cpuidle_driver->name);
Len Brown4f86d3a2007-10-03 18:58:00 -040057 else
58 ret = sprintf(buf, "none\n");
59 spin_unlock(&cpuidle_driver_lock);
60
61 return ret;
62}
63
Kay Sievers8a25a2f2011-12-21 14:29:42 -080064static ssize_t show_current_governor(struct device *dev,
65 struct device_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070066 char *buf)
Len Brown4f86d3a2007-10-03 18:58:00 -040067{
68 ssize_t ret;
69
70 mutex_lock(&cpuidle_lock);
71 if (cpuidle_curr_governor)
72 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
73 else
74 ret = sprintf(buf, "none\n");
75 mutex_unlock(&cpuidle_lock);
76
77 return ret;
78}
79
Kay Sievers8a25a2f2011-12-21 14:29:42 -080080static ssize_t store_current_governor(struct device *dev,
81 struct device_attribute *attr,
Rabin Vincent66198f32008-08-12 15:08:45 -070082 const char *buf, size_t count)
Len Brown4f86d3a2007-10-03 18:58:00 -040083{
84 char gov_name[CPUIDLE_NAME_LEN];
85 int ret = -EINVAL;
86 size_t len = count;
87 struct cpuidle_governor *gov;
88
89 if (!len || len >= sizeof(gov_name))
90 return -EINVAL;
91
92 memcpy(gov_name, buf, len);
93 gov_name[len] = '\0';
94 if (gov_name[len - 1] == '\n')
95 gov_name[--len] = '\0';
96
97 mutex_lock(&cpuidle_lock);
98
99 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
100 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
101 ret = cpuidle_switch_governor(gov);
102 break;
103 }
104 }
105
106 mutex_unlock(&cpuidle_lock);
107
108 if (ret)
109 return ret;
110 else
111 return count;
112}
113
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800114static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);
115static DEVICE_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
Len Brown4f86d3a2007-10-03 18:58:00 -0400116
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800117static struct attribute *cpuidle_default_attrs[] = {
118 &dev_attr_current_driver.attr,
119 &dev_attr_current_governor_ro.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400120 NULL
121};
122
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800123static DEVICE_ATTR(available_governors, 0444, show_available_governors, NULL);
124static DEVICE_ATTR(current_governor, 0644, show_current_governor,
125 store_current_governor);
Len Brown4f86d3a2007-10-03 18:58:00 -0400126
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800127static struct attribute *cpuidle_switch_attrs[] = {
128 &dev_attr_available_governors.attr,
129 &dev_attr_current_driver.attr,
130 &dev_attr_current_governor.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400131 NULL
132};
133
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800134static struct attribute_group cpuidle_attr_group = {
135 .attrs = cpuidle_default_attrs,
Len Brown4f86d3a2007-10-03 18:58:00 -0400136 .name = "cpuidle",
137};
138
139/**
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800140 * cpuidle_add_interface - add CPU global sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400141 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800142int cpuidle_add_interface(struct device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400143{
144 if (sysfs_switch)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800145 cpuidle_attr_group.attrs = cpuidle_switch_attrs;
Len Brown4f86d3a2007-10-03 18:58:00 -0400146
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800147 return sysfs_create_group(&dev->kobj, &cpuidle_attr_group);
Len Brown4f86d3a2007-10-03 18:58:00 -0400148}
149
150/**
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800151 * cpuidle_remove_interface - remove CPU global sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400152 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800153void cpuidle_remove_interface(struct device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400154{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800155 sysfs_remove_group(&dev->kobj, &cpuidle_attr_group);
Len Brown4f86d3a2007-10-03 18:58:00 -0400156}
157
158struct cpuidle_attr {
159 struct attribute attr;
160 ssize_t (*show)(struct cpuidle_device *, char *);
161 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
162};
163
164#define define_one_ro(_name, show) \
165 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
166#define define_one_rw(_name, show, store) \
167 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
168
169#define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
170#define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
171static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
172{
173 int ret = -EIO;
174 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
175 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
176
177 if (cattr->show) {
178 mutex_lock(&cpuidle_lock);
179 ret = cattr->show(dev, buf);
180 mutex_unlock(&cpuidle_lock);
181 }
182 return ret;
183}
184
185static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
186 const char * buf, size_t count)
187{
188 int ret = -EIO;
189 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
190 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
191
192 if (cattr->store) {
193 mutex_lock(&cpuidle_lock);
194 ret = cattr->store(dev, buf, count);
195 mutex_unlock(&cpuidle_lock);
196 }
197 return ret;
198}
199
Emese Revfy52cf25d2010-01-19 02:58:23 +0100200static const struct sysfs_ops cpuidle_sysfs_ops = {
Len Brown4f86d3a2007-10-03 18:58:00 -0400201 .show = cpuidle_show,
202 .store = cpuidle_store,
203};
204
205static void cpuidle_sysfs_release(struct kobject *kobj)
206{
207 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
208
209 complete(&dev->kobj_unregister);
210}
211
212static struct kobj_type ktype_cpuidle = {
213 .sysfs_ops = &cpuidle_sysfs_ops,
214 .release = cpuidle_sysfs_release,
215};
216
217struct cpuidle_state_attr {
218 struct attribute attr;
Deepthi Dharwar42027352011-10-28 16:20:33 +0530219 ssize_t (*show)(struct cpuidle_state *, \
220 struct cpuidle_state_usage *, char *);
ShuoX Liudc7fd272012-07-03 19:05:31 +0200221 ssize_t (*store)(struct cpuidle_state *, \
222 struct cpuidle_state_usage *, const char *, size_t);
Len Brown4f86d3a2007-10-03 18:58:00 -0400223};
224
225#define define_one_state_ro(_name, show) \
226static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
227
ShuoX Liu3a533962012-03-28 15:19:11 -0700228#define define_one_state_rw(_name, show, store) \
229static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
230
Len Brown4f86d3a2007-10-03 18:58:00 -0400231#define define_show_state_function(_name) \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530232static ssize_t show_state_##_name(struct cpuidle_state *state, \
233 struct cpuidle_state_usage *state_usage, char *buf) \
Len Brown4f86d3a2007-10-03 18:58:00 -0400234{ \
235 return sprintf(buf, "%u\n", state->_name);\
236}
237
ShuoX Liudc7fd272012-07-03 19:05:31 +0200238#define define_store_state_ull_function(_name) \
ShuoX Liu3a533962012-03-28 15:19:11 -0700239static ssize_t store_state_##_name(struct cpuidle_state *state, \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200240 struct cpuidle_state_usage *state_usage, \
ShuoX Liu3a533962012-03-28 15:19:11 -0700241 const char *buf, size_t size) \
242{ \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200243 unsigned long long value; \
ShuoX Liu3a533962012-03-28 15:19:11 -0700244 int err; \
245 if (!capable(CAP_SYS_ADMIN)) \
246 return -EPERM; \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200247 err = kstrtoull(buf, 0, &value); \
ShuoX Liu3a533962012-03-28 15:19:11 -0700248 if (err) \
249 return err; \
250 if (value) \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200251 state_usage->_name = 1; \
ShuoX Liu3a533962012-03-28 15:19:11 -0700252 else \
ShuoX Liudc7fd272012-07-03 19:05:31 +0200253 state_usage->_name = 0; \
ShuoX Liu3a533962012-03-28 15:19:11 -0700254 return size; \
255}
256
Yi Yang8b78cf62008-02-25 08:46:12 +0800257#define define_show_state_ull_function(_name) \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530258static ssize_t show_state_##_name(struct cpuidle_state *state, \
259 struct cpuidle_state_usage *state_usage, char *buf) \
Yi Yang8b78cf62008-02-25 08:46:12 +0800260{ \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530261 return sprintf(buf, "%llu\n", state_usage->_name);\
Yi Yang8b78cf62008-02-25 08:46:12 +0800262}
263
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800264#define define_show_state_str_function(_name) \
Deepthi Dharwar42027352011-10-28 16:20:33 +0530265static ssize_t show_state_##_name(struct cpuidle_state *state, \
266 struct cpuidle_state_usage *state_usage, char *buf) \
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800267{ \
268 if (state->_name[0] == '\0')\
269 return sprintf(buf, "<null>\n");\
270 return sprintf(buf, "%s\n", state->_name);\
Len Brown4f86d3a2007-10-03 18:58:00 -0400271}
272
273define_show_state_function(exit_latency)
274define_show_state_function(power_usage)
Yi Yang8b78cf62008-02-25 08:46:12 +0800275define_show_state_ull_function(usage)
276define_show_state_ull_function(time)
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800277define_show_state_str_function(name)
278define_show_state_str_function(desc)
ShuoX Liudc7fd272012-07-03 19:05:31 +0200279define_show_state_ull_function(disable)
280define_store_state_ull_function(disable)
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800281
Len Brown4f86d3a2007-10-03 18:58:00 -0400282define_one_state_ro(name, show_state_name);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800283define_one_state_ro(desc, show_state_desc);
Len Brown4f86d3a2007-10-03 18:58:00 -0400284define_one_state_ro(latency, show_state_exit_latency);
285define_one_state_ro(power, show_state_power_usage);
286define_one_state_ro(usage, show_state_usage);
287define_one_state_ro(time, show_state_time);
ShuoX Liu3a533962012-03-28 15:19:11 -0700288define_one_state_rw(disable, show_state_disable, store_state_disable);
Len Brown4f86d3a2007-10-03 18:58:00 -0400289
290static struct attribute *cpuidle_state_default_attrs[] = {
291 &attr_name.attr,
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800292 &attr_desc.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400293 &attr_latency.attr,
294 &attr_power.attr,
295 &attr_usage.attr,
296 &attr_time.attr,
ShuoX Liu3a533962012-03-28 15:19:11 -0700297 &attr_disable.attr,
Len Brown4f86d3a2007-10-03 18:58:00 -0400298 NULL
299};
300
Daniel Lezcano349631e2012-10-31 01:05:16 +0100301struct cpuidle_state_kobj {
302 struct cpuidle_state *state;
303 struct cpuidle_state_usage *state_usage;
304 struct completion kobj_unregister;
305 struct kobject kobj;
306};
307
Len Brown4f86d3a2007-10-03 18:58:00 -0400308#define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
309#define kobj_to_state(k) (kobj_to_state_obj(k)->state)
Deepthi Dharwar42027352011-10-28 16:20:33 +0530310#define kobj_to_state_usage(k) (kobj_to_state_obj(k)->state_usage)
Len Brown4f86d3a2007-10-03 18:58:00 -0400311#define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
312static ssize_t cpuidle_state_show(struct kobject * kobj,
313 struct attribute * attr ,char * buf)
314{
315 int ret = -EIO;
316 struct cpuidle_state *state = kobj_to_state(kobj);
Deepthi Dharwar42027352011-10-28 16:20:33 +0530317 struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400318 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
319
320 if (cattr->show)
Deepthi Dharwar42027352011-10-28 16:20:33 +0530321 ret = cattr->show(state, state_usage, buf);
Len Brown4f86d3a2007-10-03 18:58:00 -0400322
323 return ret;
324}
325
ShuoX Liu3a533962012-03-28 15:19:11 -0700326static ssize_t cpuidle_state_store(struct kobject *kobj,
327 struct attribute *attr, const char *buf, size_t size)
328{
329 int ret = -EIO;
330 struct cpuidle_state *state = kobj_to_state(kobj);
ShuoX Liudc7fd272012-07-03 19:05:31 +0200331 struct cpuidle_state_usage *state_usage = kobj_to_state_usage(kobj);
ShuoX Liu3a533962012-03-28 15:19:11 -0700332 struct cpuidle_state_attr *cattr = attr_to_stateattr(attr);
333
334 if (cattr->store)
ShuoX Liudc7fd272012-07-03 19:05:31 +0200335 ret = cattr->store(state, state_usage, buf, size);
ShuoX Liu3a533962012-03-28 15:19:11 -0700336
337 return ret;
338}
339
Emese Revfy52cf25d2010-01-19 02:58:23 +0100340static const struct sysfs_ops cpuidle_state_sysfs_ops = {
Len Brown4f86d3a2007-10-03 18:58:00 -0400341 .show = cpuidle_state_show,
ShuoX Liu3a533962012-03-28 15:19:11 -0700342 .store = cpuidle_state_store,
Len Brown4f86d3a2007-10-03 18:58:00 -0400343};
344
345static void cpuidle_state_sysfs_release(struct kobject *kobj)
346{
347 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
348
349 complete(&state_obj->kobj_unregister);
350}
351
352static struct kobj_type ktype_state_cpuidle = {
353 .sysfs_ops = &cpuidle_state_sysfs_ops,
354 .default_attrs = cpuidle_state_default_attrs,
355 .release = cpuidle_state_sysfs_release,
356};
357
Jesper Juhl42b16b32011-01-17 00:09:38 +0100358static inline void cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
Len Brown4f86d3a2007-10-03 18:58:00 -0400359{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800360 kobject_put(&device->kobjs[i]->kobj);
Len Brown4f86d3a2007-10-03 18:58:00 -0400361 wait_for_completion(&device->kobjs[i]->kobj_unregister);
362 kfree(device->kobjs[i]);
363 device->kobjs[i] = NULL;
364}
365
366/**
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000367 * cpuidle_add_state_sysfs - adds cpuidle states sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400368 * @device: the target device
369 */
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000370static int cpuidle_add_state_sysfs(struct cpuidle_device *device)
Len Brown4f86d3a2007-10-03 18:58:00 -0400371{
372 int i, ret = -ENOMEM;
373 struct cpuidle_state_kobj *kobj;
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000374 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(device);
Len Brown4f86d3a2007-10-03 18:58:00 -0400375
376 /* state statistics */
Krzysztof Mazur392370e2013-01-11 23:20:09 +0100377 for (i = 0; i < device->state_count; i++) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400378 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
379 if (!kobj)
380 goto error_state;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530381 kobj->state = &drv->states[i];
Deepthi Dharwar42027352011-10-28 16:20:33 +0530382 kobj->state_usage = &device->states_usage[i];
Len Brown4f86d3a2007-10-03 18:58:00 -0400383 init_completion(&kobj->kobj_unregister);
384
Daniel Lezcanoe45a00d2012-10-26 12:26:32 +0200385 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle,
386 &device->kobj, "state%d", i);
Len Brown4f86d3a2007-10-03 18:58:00 -0400387 if (ret) {
388 kfree(kobj);
389 goto error_state;
390 }
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400391 kobject_uevent(&kobj->kobj, KOBJ_ADD);
Len Brown4f86d3a2007-10-03 18:58:00 -0400392 device->kobjs[i] = kobj;
393 }
394
395 return 0;
396
397error_state:
398 for (i = i - 1; i >= 0; i--)
399 cpuidle_free_state_kobj(device, i);
400 return ret;
401}
402
403/**
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000404 * cpuidle_remove_driver_sysfs - removes the cpuidle states sysfs attributes
Len Brown4f86d3a2007-10-03 18:58:00 -0400405 * @device: the target device
406 */
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000407static void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
Len Brown4f86d3a2007-10-03 18:58:00 -0400408{
409 int i;
410
411 for (i = 0; i < device->state_count; i++)
412 cpuidle_free_state_kobj(device, i);
413}
414
Daniel Lezcanobf4d1b52012-10-31 16:44:48 +0000415#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
416#define kobj_to_driver_kobj(k) container_of(k, struct cpuidle_driver_kobj, kobj)
417#define attr_to_driver_attr(a) container_of(a, struct cpuidle_driver_attr, attr)
418
419#define define_one_driver_ro(_name, show) \
420 static struct cpuidle_driver_attr attr_driver_##_name = \
421 __ATTR(_name, 0644, show, NULL)
422
423struct cpuidle_driver_kobj {
424 struct cpuidle_driver *drv;
425 struct completion kobj_unregister;
426 struct kobject kobj;
427};
428
429struct cpuidle_driver_attr {
430 struct attribute attr;
431 ssize_t (*show)(struct cpuidle_driver *, char *);
432 ssize_t (*store)(struct cpuidle_driver *, const char *, size_t);
433};
434
435static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
436{
437 ssize_t ret;
438
439 spin_lock(&cpuidle_driver_lock);
440 ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
441 spin_unlock(&cpuidle_driver_lock);
442
443 return ret;
444}
445
446static void cpuidle_driver_sysfs_release(struct kobject *kobj)
447{
448 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
449 complete(&driver_kobj->kobj_unregister);
450}
451
452static ssize_t cpuidle_driver_show(struct kobject *kobj, struct attribute * attr,
453 char * buf)
454{
455 int ret = -EIO;
456 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
457 struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
458
459 if (dattr->show)
460 ret = dattr->show(driver_kobj->drv, buf);
461
462 return ret;
463}
464
465static ssize_t cpuidle_driver_store(struct kobject *kobj, struct attribute *attr,
466 const char *buf, size_t size)
467{
468 int ret = -EIO;
469 struct cpuidle_driver_kobj *driver_kobj = kobj_to_driver_kobj(kobj);
470 struct cpuidle_driver_attr *dattr = attr_to_driver_attr(attr);
471
472 if (dattr->store)
473 ret = dattr->store(driver_kobj->drv, buf, size);
474
475 return ret;
476}
477
478define_one_driver_ro(name, show_driver_name);
479
480static const struct sysfs_ops cpuidle_driver_sysfs_ops = {
481 .show = cpuidle_driver_show,
482 .store = cpuidle_driver_store,
483};
484
485static struct attribute *cpuidle_driver_default_attrs[] = {
486 &attr_driver_name.attr,
487 NULL
488};
489
490static struct kobj_type ktype_driver_cpuidle = {
491 .sysfs_ops = &cpuidle_driver_sysfs_ops,
492 .default_attrs = cpuidle_driver_default_attrs,
493 .release = cpuidle_driver_sysfs_release,
494};
495
496/**
497 * cpuidle_add_driver_sysfs - adds the driver name sysfs attribute
498 * @device: the target device
499 */
500static int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
501{
502 struct cpuidle_driver_kobj *kdrv;
503 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
504 int ret;
505
506 kdrv = kzalloc(sizeof(*kdrv), GFP_KERNEL);
507 if (!kdrv)
508 return -ENOMEM;
509
510 kdrv->drv = drv;
511 init_completion(&kdrv->kobj_unregister);
512
513 ret = kobject_init_and_add(&kdrv->kobj, &ktype_driver_cpuidle,
514 &dev->kobj, "driver");
515 if (ret) {
516 kfree(kdrv);
517 return ret;
518 }
519
520 kobject_uevent(&kdrv->kobj, KOBJ_ADD);
521 dev->kobj_driver = kdrv;
522
523 return ret;
524}
525
526/**
527 * cpuidle_remove_driver_sysfs - removes the driver name sysfs attribute
528 * @device: the target device
529 */
530static void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
531{
532 struct cpuidle_driver_kobj *kdrv = dev->kobj_driver;
533 kobject_put(&kdrv->kobj);
534 wait_for_completion(&kdrv->kobj_unregister);
535 kfree(kdrv);
536}
537#else
538static inline int cpuidle_add_driver_sysfs(struct cpuidle_device *dev)
539{
540 return 0;
541}
542
543static inline void cpuidle_remove_driver_sysfs(struct cpuidle_device *dev)
544{
545 ;
546}
547#endif
548
549/**
550 * cpuidle_add_device_sysfs - adds device specific sysfs attributes
551 * @device: the target device
552 */
553int cpuidle_add_device_sysfs(struct cpuidle_device *device)
554{
555 int ret;
556
557 ret = cpuidle_add_state_sysfs(device);
558 if (ret)
559 return ret;
560
561 ret = cpuidle_add_driver_sysfs(device);
562 if (ret)
563 cpuidle_remove_state_sysfs(device);
564 return ret;
565}
566
567/**
568 * cpuidle_remove_device_sysfs : removes device specific sysfs attributes
569 * @device : the target device
570 */
571void cpuidle_remove_device_sysfs(struct cpuidle_device *device)
572{
573 cpuidle_remove_driver_sysfs(device);
574 cpuidle_remove_state_sysfs(device);
575}
576
Len Brown4f86d3a2007-10-03 18:58:00 -0400577/**
578 * cpuidle_add_sysfs - creates a sysfs instance for the target device
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800579 * @dev: the target device
Len Brown4f86d3a2007-10-03 18:58:00 -0400580 */
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200581int cpuidle_add_sysfs(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400582{
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200583 struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400584 int error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400585
Daniel Lezcanoe45a00d2012-10-26 12:26:32 +0200586 init_completion(&dev->kobj_unregister);
587
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800588 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &cpu_dev->kobj,
Greg Kroah-Hartman94f57f32007-12-17 15:54:39 -0400589 "cpuidle");
590 if (!error)
591 kobject_uevent(&dev->kobj, KOBJ_ADD);
592 return error;
Len Brown4f86d3a2007-10-03 18:58:00 -0400593}
594
595/**
596 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800597 * @dev: the target device
Len Brown4f86d3a2007-10-03 18:58:00 -0400598 */
Daniel Lezcano1aef40e2012-10-26 12:26:24 +0200599void cpuidle_remove_sysfs(struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400600{
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800601 kobject_put(&dev->kobj);
Daniel Lezcanoe45a00d2012-10-26 12:26:32 +0200602 wait_for_completion(&dev->kobj_unregister);
Len Brown4f86d3a2007-10-03 18:58:00 -0400603}