sysdev: Pass attribute in sysdev_class attributes show/store
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / cpuidle / sysfs.c
blobc9cefacabf37fe2126bbe0de12fe76c2bd80f7d7
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 struct sysdev_class_attribute *attr,
26 char *buf)
28 ssize_t i = 0;
29 struct cpuidle_governor *tmp;
31 mutex_lock(&cpuidle_lock);
32 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
33 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
34 goto out;
35 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
38 out:
39 i+= sprintf(&buf[i], "\n");
40 mutex_unlock(&cpuidle_lock);
41 return i;
44 static ssize_t show_current_driver(struct sysdev_class *class,
45 struct sysdev_class_attribute *attr,
46 char *buf)
48 ssize_t ret;
50 spin_lock(&cpuidle_driver_lock);
51 if (cpuidle_curr_driver)
52 ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
53 else
54 ret = sprintf(buf, "none\n");
55 spin_unlock(&cpuidle_driver_lock);
57 return ret;
60 static ssize_t show_current_governor(struct sysdev_class *class,
61 struct sysdev_class_attribute *attr,
62 char *buf)
64 ssize_t ret;
66 mutex_lock(&cpuidle_lock);
67 if (cpuidle_curr_governor)
68 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
69 else
70 ret = sprintf(buf, "none\n");
71 mutex_unlock(&cpuidle_lock);
73 return ret;
76 static ssize_t store_current_governor(struct sysdev_class *class,
77 struct sysdev_class_attribute *attr,
78 const char *buf, size_t count)
80 char gov_name[CPUIDLE_NAME_LEN];
81 int ret = -EINVAL;
82 size_t len = count;
83 struct cpuidle_governor *gov;
85 if (!len || len >= sizeof(gov_name))
86 return -EINVAL;
88 memcpy(gov_name, buf, len);
89 gov_name[len] = '\0';
90 if (gov_name[len - 1] == '\n')
91 gov_name[--len] = '\0';
93 mutex_lock(&cpuidle_lock);
95 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
96 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
97 ret = cpuidle_switch_governor(gov);
98 break;
102 mutex_unlock(&cpuidle_lock);
104 if (ret)
105 return ret;
106 else
107 return count;
110 static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL);
111 static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor,
112 NULL);
114 static struct attribute *cpuclass_default_attrs[] = {
115 &attr_current_driver.attr,
116 &attr_current_governor_ro.attr,
117 NULL
120 static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors,
121 NULL);
122 static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor,
123 store_current_governor);
125 static struct attribute *cpuclass_switch_attrs[] = {
126 &attr_available_governors.attr,
127 &attr_current_driver.attr,
128 &attr_current_governor.attr,
129 NULL
132 static struct attribute_group cpuclass_attr_group = {
133 .attrs = cpuclass_default_attrs,
134 .name = "cpuidle",
138 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
140 int cpuidle_add_class_sysfs(struct sysdev_class *cls)
142 if (sysfs_switch)
143 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
145 return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
149 * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
151 void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
153 sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
156 struct cpuidle_attr {
157 struct attribute attr;
158 ssize_t (*show)(struct cpuidle_device *, char *);
159 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
162 #define define_one_ro(_name, show) \
163 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
164 #define define_one_rw(_name, show, store) \
165 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
167 #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
168 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
169 static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
171 int ret = -EIO;
172 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
173 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
175 if (cattr->show) {
176 mutex_lock(&cpuidle_lock);
177 ret = cattr->show(dev, buf);
178 mutex_unlock(&cpuidle_lock);
180 return ret;
183 static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
184 const char * buf, size_t count)
186 int ret = -EIO;
187 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
188 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
190 if (cattr->store) {
191 mutex_lock(&cpuidle_lock);
192 ret = cattr->store(dev, buf, count);
193 mutex_unlock(&cpuidle_lock);
195 return ret;
198 static struct sysfs_ops cpuidle_sysfs_ops = {
199 .show = cpuidle_show,
200 .store = cpuidle_store,
203 static void cpuidle_sysfs_release(struct kobject *kobj)
205 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
207 complete(&dev->kobj_unregister);
210 static struct kobj_type ktype_cpuidle = {
211 .sysfs_ops = &cpuidle_sysfs_ops,
212 .release = cpuidle_sysfs_release,
215 struct cpuidle_state_attr {
216 struct attribute attr;
217 ssize_t (*show)(struct cpuidle_state *, char *);
218 ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
221 #define define_one_state_ro(_name, show) \
222 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
224 #define define_show_state_function(_name) \
225 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
227 return sprintf(buf, "%u\n", state->_name);\
230 #define define_show_state_ull_function(_name) \
231 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
233 return sprintf(buf, "%llu\n", state->_name);\
236 #define define_show_state_str_function(_name) \
237 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
239 if (state->_name[0] == '\0')\
240 return sprintf(buf, "<null>\n");\
241 return sprintf(buf, "%s\n", state->_name);\
244 define_show_state_function(exit_latency)
245 define_show_state_function(power_usage)
246 define_show_state_ull_function(usage)
247 define_show_state_ull_function(time)
248 define_show_state_str_function(name)
249 define_show_state_str_function(desc)
251 define_one_state_ro(name, show_state_name);
252 define_one_state_ro(desc, show_state_desc);
253 define_one_state_ro(latency, show_state_exit_latency);
254 define_one_state_ro(power, show_state_power_usage);
255 define_one_state_ro(usage, show_state_usage);
256 define_one_state_ro(time, show_state_time);
258 static struct attribute *cpuidle_state_default_attrs[] = {
259 &attr_name.attr,
260 &attr_desc.attr,
261 &attr_latency.attr,
262 &attr_power.attr,
263 &attr_usage.attr,
264 &attr_time.attr,
265 NULL
268 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
269 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
270 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
271 static ssize_t cpuidle_state_show(struct kobject * kobj,
272 struct attribute * attr ,char * buf)
274 int ret = -EIO;
275 struct cpuidle_state *state = kobj_to_state(kobj);
276 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
278 if (cattr->show)
279 ret = cattr->show(state, buf);
281 return ret;
284 static struct sysfs_ops cpuidle_state_sysfs_ops = {
285 .show = cpuidle_state_show,
288 static void cpuidle_state_sysfs_release(struct kobject *kobj)
290 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
292 complete(&state_obj->kobj_unregister);
295 static struct kobj_type ktype_state_cpuidle = {
296 .sysfs_ops = &cpuidle_state_sysfs_ops,
297 .default_attrs = cpuidle_state_default_attrs,
298 .release = cpuidle_state_sysfs_release,
301 static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
303 kobject_put(&device->kobjs[i]->kobj);
304 wait_for_completion(&device->kobjs[i]->kobj_unregister);
305 kfree(device->kobjs[i]);
306 device->kobjs[i] = NULL;
310 * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
311 * @device: the target device
313 int cpuidle_add_state_sysfs(struct cpuidle_device *device)
315 int i, ret = -ENOMEM;
316 struct cpuidle_state_kobj *kobj;
318 /* state statistics */
319 for (i = 0; i < device->state_count; i++) {
320 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
321 if (!kobj)
322 goto error_state;
323 kobj->state = &device->states[i];
324 init_completion(&kobj->kobj_unregister);
326 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
327 "state%d", i);
328 if (ret) {
329 kfree(kobj);
330 goto error_state;
332 kobject_uevent(&kobj->kobj, KOBJ_ADD);
333 device->kobjs[i] = kobj;
336 return 0;
338 error_state:
339 for (i = i - 1; i >= 0; i--)
340 cpuidle_free_state_kobj(device, i);
341 return ret;
345 * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
346 * @device: the target device
348 void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
350 int i;
352 for (i = 0; i < device->state_count; i++)
353 cpuidle_free_state_kobj(device, i);
357 * cpuidle_add_sysfs - creates a sysfs instance for the target device
358 * @sysdev: the target device
360 int cpuidle_add_sysfs(struct sys_device *sysdev)
362 int cpu = sysdev->id;
363 struct cpuidle_device *dev;
364 int error;
366 dev = per_cpu(cpuidle_devices, cpu);
367 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
368 "cpuidle");
369 if (!error)
370 kobject_uevent(&dev->kobj, KOBJ_ADD);
371 return error;
375 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
376 * @sysdev: the target device
378 void cpuidle_remove_sysfs(struct sys_device *sysdev)
380 int cpu = sysdev->id;
381 struct cpuidle_device *dev;
383 dev = per_cpu(cpuidle_devices, cpu);
384 kobject_put(&dev->kobj);