Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / cpuidle / sysfs.c
blob97b003839fb6ab0df4249fd29424f4ba05f0ec3b
1 /*
2 * sysfs.c - sysfs support
4 * (C) 2006-2007 Shaohua Li <shaohua.li@intel.com>
6 * This code is licenced under the GPL.
7 */
9 #include <linux/kernel.h>
10 #include <linux/cpuidle.h>
11 #include <linux/sysfs.h>
12 #include <linux/cpu.h>
14 #include "cpuidle.h"
16 static unsigned int sysfs_switch;
17 static int __init cpuidle_sysfs_setup(char *unused)
19 sysfs_switch = 1;
20 return 1;
22 __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
24 static ssize_t show_available_governors(struct sysdev_class *class,
25 char *buf)
27 ssize_t i = 0;
28 struct cpuidle_governor *tmp;
30 mutex_lock(&cpuidle_lock);
31 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
32 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
33 goto out;
34 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
37 out:
38 i+= sprintf(&buf[i], "\n");
39 mutex_unlock(&cpuidle_lock);
40 return i;
43 static ssize_t show_current_driver(struct sysdev_class *class,
44 char *buf)
46 ssize_t ret;
48 spin_lock(&cpuidle_driver_lock);
49 if (cpuidle_curr_driver)
50 ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
51 else
52 ret = sprintf(buf, "none\n");
53 spin_unlock(&cpuidle_driver_lock);
55 return ret;
58 static ssize_t show_current_governor(struct sysdev_class *class,
59 char *buf)
61 ssize_t ret;
63 mutex_lock(&cpuidle_lock);
64 if (cpuidle_curr_governor)
65 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
66 else
67 ret = sprintf(buf, "none\n");
68 mutex_unlock(&cpuidle_lock);
70 return ret;
73 static ssize_t store_current_governor(struct sysdev_class *class,
74 const char *buf, size_t count)
76 char gov_name[CPUIDLE_NAME_LEN];
77 int ret = -EINVAL;
78 size_t len = count;
79 struct cpuidle_governor *gov;
81 if (!len || len >= sizeof(gov_name))
82 return -EINVAL;
84 memcpy(gov_name, buf, len);
85 gov_name[len] = '\0';
86 if (gov_name[len - 1] == '\n')
87 gov_name[--len] = '\0';
89 mutex_lock(&cpuidle_lock);
91 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
92 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
93 ret = cpuidle_switch_governor(gov);
94 break;
98 mutex_unlock(&cpuidle_lock);
100 if (ret)
101 return ret;
102 else
103 return count;
106 static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL);
107 static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor,
108 NULL);
110 static struct attribute *cpuclass_default_attrs[] = {
111 &attr_current_driver.attr,
112 &attr_current_governor_ro.attr,
113 NULL
116 static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors,
117 NULL);
118 static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor,
119 store_current_governor);
121 static struct attribute *cpuclass_switch_attrs[] = {
122 &attr_available_governors.attr,
123 &attr_current_driver.attr,
124 &attr_current_governor.attr,
125 NULL
128 static struct attribute_group cpuclass_attr_group = {
129 .attrs = cpuclass_default_attrs,
130 .name = "cpuidle",
134 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
136 int cpuidle_add_class_sysfs(struct sysdev_class *cls)
138 if (sysfs_switch)
139 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
141 return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
145 * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
147 void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
149 sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
152 struct cpuidle_attr {
153 struct attribute attr;
154 ssize_t (*show)(struct cpuidle_device *, char *);
155 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
158 #define define_one_ro(_name, show) \
159 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
160 #define define_one_rw(_name, show, store) \
161 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
163 #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
164 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
165 static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
167 int ret = -EIO;
168 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
169 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
171 if (cattr->show) {
172 mutex_lock(&cpuidle_lock);
173 ret = cattr->show(dev, buf);
174 mutex_unlock(&cpuidle_lock);
176 return ret;
179 static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
180 const char * buf, size_t count)
182 int ret = -EIO;
183 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
184 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
186 if (cattr->store) {
187 mutex_lock(&cpuidle_lock);
188 ret = cattr->store(dev, buf, count);
189 mutex_unlock(&cpuidle_lock);
191 return ret;
194 static struct sysfs_ops cpuidle_sysfs_ops = {
195 .show = cpuidle_show,
196 .store = cpuidle_store,
199 static void cpuidle_sysfs_release(struct kobject *kobj)
201 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
203 complete(&dev->kobj_unregister);
206 static struct kobj_type ktype_cpuidle = {
207 .sysfs_ops = &cpuidle_sysfs_ops,
208 .release = cpuidle_sysfs_release,
211 struct cpuidle_state_attr {
212 struct attribute attr;
213 ssize_t (*show)(struct cpuidle_state *, char *);
214 ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
217 #define define_one_state_ro(_name, show) \
218 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
220 #define define_show_state_function(_name) \
221 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
223 return sprintf(buf, "%u\n", state->_name);\
226 #define define_show_state_ull_function(_name) \
227 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
229 return sprintf(buf, "%llu\n", state->_name);\
232 #define define_show_state_str_function(_name) \
233 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
235 if (state->_name[0] == '\0')\
236 return sprintf(buf, "<null>\n");\
237 return sprintf(buf, "%s\n", state->_name);\
240 define_show_state_function(exit_latency)
241 define_show_state_function(power_usage)
242 define_show_state_ull_function(usage)
243 define_show_state_ull_function(time)
244 define_show_state_str_function(name)
245 define_show_state_str_function(desc)
247 define_one_state_ro(name, show_state_name);
248 define_one_state_ro(desc, show_state_desc);
249 define_one_state_ro(latency, show_state_exit_latency);
250 define_one_state_ro(power, show_state_power_usage);
251 define_one_state_ro(usage, show_state_usage);
252 define_one_state_ro(time, show_state_time);
254 static struct attribute *cpuidle_state_default_attrs[] = {
255 &attr_name.attr,
256 &attr_desc.attr,
257 &attr_latency.attr,
258 &attr_power.attr,
259 &attr_usage.attr,
260 &attr_time.attr,
261 NULL
264 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
265 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
266 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
267 static ssize_t cpuidle_state_show(struct kobject * kobj,
268 struct attribute * attr ,char * buf)
270 int ret = -EIO;
271 struct cpuidle_state *state = kobj_to_state(kobj);
272 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
274 if (cattr->show)
275 ret = cattr->show(state, buf);
277 return ret;
280 static struct sysfs_ops cpuidle_state_sysfs_ops = {
281 .show = cpuidle_state_show,
284 static void cpuidle_state_sysfs_release(struct kobject *kobj)
286 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
288 complete(&state_obj->kobj_unregister);
291 static struct kobj_type ktype_state_cpuidle = {
292 .sysfs_ops = &cpuidle_state_sysfs_ops,
293 .default_attrs = cpuidle_state_default_attrs,
294 .release = cpuidle_state_sysfs_release,
297 static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
299 kobject_put(&device->kobjs[i]->kobj);
300 wait_for_completion(&device->kobjs[i]->kobj_unregister);
301 kfree(device->kobjs[i]);
302 device->kobjs[i] = NULL;
306 * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
307 * @device: the target device
309 int cpuidle_add_state_sysfs(struct cpuidle_device *device)
311 int i, ret = -ENOMEM;
312 struct cpuidle_state_kobj *kobj;
314 /* state statistics */
315 for (i = 0; i < device->state_count; i++) {
316 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
317 if (!kobj)
318 goto error_state;
319 kobj->state = &device->states[i];
320 init_completion(&kobj->kobj_unregister);
322 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
323 "state%d", i);
324 if (ret) {
325 kfree(kobj);
326 goto error_state;
328 kobject_uevent(&kobj->kobj, KOBJ_ADD);
329 device->kobjs[i] = kobj;
332 return 0;
334 error_state:
335 for (i = i - 1; i >= 0; i--)
336 cpuidle_free_state_kobj(device, i);
337 return ret;
341 * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
342 * @device: the target device
344 void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
346 int i;
348 for (i = 0; i < device->state_count; i++)
349 cpuidle_free_state_kobj(device, i);
353 * cpuidle_add_sysfs - creates a sysfs instance for the target device
354 * @sysdev: the target device
356 int cpuidle_add_sysfs(struct sys_device *sysdev)
358 int cpu = sysdev->id;
359 struct cpuidle_device *dev;
360 int error;
362 dev = per_cpu(cpuidle_devices, cpu);
363 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
364 "cpuidle");
365 if (!error)
366 kobject_uevent(&dev->kobj, KOBJ_ADD);
367 return error;
371 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
372 * @sysdev: the target device
374 void cpuidle_remove_sysfs(struct sys_device *sysdev)
376 int cpu = sysdev->id;
377 struct cpuidle_device *dev;
379 dev = per_cpu(cpuidle_devices, cpu);
380 kobject_put(&dev->kobj);