Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / cpuidle / sysfs.c
blob0ba9c8b8ee743e960eed3db8e5d9108d7c5eeadc
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/slab.h>
13 #include <linux/cpu.h>
15 #include "cpuidle.h"
17 static unsigned int sysfs_switch;
18 static int __init cpuidle_sysfs_setup(char *unused)
20 sysfs_switch = 1;
21 return 1;
23 __setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);
25 static ssize_t show_available_governors(struct sysdev_class *class,
26 struct sysdev_class_attribute *attr,
27 char *buf)
29 ssize_t i = 0;
30 struct cpuidle_governor *tmp;
32 mutex_lock(&cpuidle_lock);
33 list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
34 if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
35 goto out;
36 i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
39 out:
40 i+= sprintf(&buf[i], "\n");
41 mutex_unlock(&cpuidle_lock);
42 return i;
45 static ssize_t show_current_driver(struct sysdev_class *class,
46 struct sysdev_class_attribute *attr,
47 char *buf)
49 ssize_t ret;
51 spin_lock(&cpuidle_driver_lock);
52 if (cpuidle_curr_driver)
53 ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
54 else
55 ret = sprintf(buf, "none\n");
56 spin_unlock(&cpuidle_driver_lock);
58 return ret;
61 static ssize_t show_current_governor(struct sysdev_class *class,
62 struct sysdev_class_attribute *attr,
63 char *buf)
65 ssize_t ret;
67 mutex_lock(&cpuidle_lock);
68 if (cpuidle_curr_governor)
69 ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
70 else
71 ret = sprintf(buf, "none\n");
72 mutex_unlock(&cpuidle_lock);
74 return ret;
77 static ssize_t store_current_governor(struct sysdev_class *class,
78 struct sysdev_class_attribute *attr,
79 const char *buf, size_t count)
81 char gov_name[CPUIDLE_NAME_LEN];
82 int ret = -EINVAL;
83 size_t len = count;
84 struct cpuidle_governor *gov;
86 if (!len || len >= sizeof(gov_name))
87 return -EINVAL;
89 memcpy(gov_name, buf, len);
90 gov_name[len] = '\0';
91 if (gov_name[len - 1] == '\n')
92 gov_name[--len] = '\0';
94 mutex_lock(&cpuidle_lock);
96 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
97 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
98 ret = cpuidle_switch_governor(gov);
99 break;
103 mutex_unlock(&cpuidle_lock);
105 if (ret)
106 return ret;
107 else
108 return count;
111 static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL);
112 static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor,
113 NULL);
115 static struct attribute *cpuclass_default_attrs[] = {
116 &attr_current_driver.attr,
117 &attr_current_governor_ro.attr,
118 NULL
121 static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors,
122 NULL);
123 static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor,
124 store_current_governor);
126 static struct attribute *cpuclass_switch_attrs[] = {
127 &attr_available_governors.attr,
128 &attr_current_driver.attr,
129 &attr_current_governor.attr,
130 NULL
133 static struct attribute_group cpuclass_attr_group = {
134 .attrs = cpuclass_default_attrs,
135 .name = "cpuidle",
139 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
141 int cpuidle_add_class_sysfs(struct sysdev_class *cls)
143 if (sysfs_switch)
144 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
146 return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
150 * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
152 void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
154 sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
157 struct cpuidle_attr {
158 struct attribute attr;
159 ssize_t (*show)(struct cpuidle_device *, char *);
160 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
163 #define define_one_ro(_name, show) \
164 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
165 #define define_one_rw(_name, show, store) \
166 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
168 #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
169 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
170 static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
172 int ret = -EIO;
173 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
174 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
176 if (cattr->show) {
177 mutex_lock(&cpuidle_lock);
178 ret = cattr->show(dev, buf);
179 mutex_unlock(&cpuidle_lock);
181 return ret;
184 static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
185 const char * buf, size_t count)
187 int ret = -EIO;
188 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
189 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
191 if (cattr->store) {
192 mutex_lock(&cpuidle_lock);
193 ret = cattr->store(dev, buf, count);
194 mutex_unlock(&cpuidle_lock);
196 return ret;
199 static const struct sysfs_ops cpuidle_sysfs_ops = {
200 .show = cpuidle_show,
201 .store = cpuidle_store,
204 static void cpuidle_sysfs_release(struct kobject *kobj)
206 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
208 complete(&dev->kobj_unregister);
211 static struct kobj_type ktype_cpuidle = {
212 .sysfs_ops = &cpuidle_sysfs_ops,
213 .release = cpuidle_sysfs_release,
216 struct cpuidle_state_attr {
217 struct attribute attr;
218 ssize_t (*show)(struct cpuidle_state *, char *);
219 ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
222 #define define_one_state_ro(_name, show) \
223 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
225 #define define_show_state_function(_name) \
226 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
228 return sprintf(buf, "%u\n", state->_name);\
231 #define define_show_state_ull_function(_name) \
232 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
234 return sprintf(buf, "%llu\n", state->_name);\
237 #define define_show_state_str_function(_name) \
238 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
240 if (state->_name[0] == '\0')\
241 return sprintf(buf, "<null>\n");\
242 return sprintf(buf, "%s\n", state->_name);\
245 define_show_state_function(exit_latency)
246 define_show_state_function(power_usage)
247 define_show_state_ull_function(usage)
248 define_show_state_ull_function(time)
249 define_show_state_str_function(name)
250 define_show_state_str_function(desc)
252 define_one_state_ro(name, show_state_name);
253 define_one_state_ro(desc, show_state_desc);
254 define_one_state_ro(latency, show_state_exit_latency);
255 define_one_state_ro(power, show_state_power_usage);
256 define_one_state_ro(usage, show_state_usage);
257 define_one_state_ro(time, show_state_time);
259 static struct attribute *cpuidle_state_default_attrs[] = {
260 &attr_name.attr,
261 &attr_desc.attr,
262 &attr_latency.attr,
263 &attr_power.attr,
264 &attr_usage.attr,
265 &attr_time.attr,
266 NULL
269 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
270 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
271 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
272 static ssize_t cpuidle_state_show(struct kobject * kobj,
273 struct attribute * attr ,char * buf)
275 int ret = -EIO;
276 struct cpuidle_state *state = kobj_to_state(kobj);
277 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
279 if (cattr->show)
280 ret = cattr->show(state, buf);
282 return ret;
285 static const struct sysfs_ops cpuidle_state_sysfs_ops = {
286 .show = cpuidle_state_show,
289 static void cpuidle_state_sysfs_release(struct kobject *kobj)
291 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
293 complete(&state_obj->kobj_unregister);
296 static struct kobj_type ktype_state_cpuidle = {
297 .sysfs_ops = &cpuidle_state_sysfs_ops,
298 .default_attrs = cpuidle_state_default_attrs,
299 .release = cpuidle_state_sysfs_release,
302 static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
304 kobject_put(&device->kobjs[i]->kobj);
305 wait_for_completion(&device->kobjs[i]->kobj_unregister);
306 kfree(device->kobjs[i]);
307 device->kobjs[i] = NULL;
311 * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
312 * @device: the target device
314 int cpuidle_add_state_sysfs(struct cpuidle_device *device)
316 int i, ret = -ENOMEM;
317 struct cpuidle_state_kobj *kobj;
319 /* state statistics */
320 for (i = 0; i < device->state_count; i++) {
321 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
322 if (!kobj)
323 goto error_state;
324 kobj->state = &device->states[i];
325 init_completion(&kobj->kobj_unregister);
327 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
328 "state%d", i);
329 if (ret) {
330 kfree(kobj);
331 goto error_state;
333 kobject_uevent(&kobj->kobj, KOBJ_ADD);
334 device->kobjs[i] = kobj;
337 return 0;
339 error_state:
340 for (i = i - 1; i >= 0; i--)
341 cpuidle_free_state_kobj(device, i);
342 return ret;
346 * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
347 * @device: the target device
349 void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
351 int i;
353 for (i = 0; i < device->state_count; i++)
354 cpuidle_free_state_kobj(device, i);
358 * cpuidle_add_sysfs - creates a sysfs instance for the target device
359 * @sysdev: the target device
361 int cpuidle_add_sysfs(struct sys_device *sysdev)
363 int cpu = sysdev->id;
364 struct cpuidle_device *dev;
365 int error;
367 dev = per_cpu(cpuidle_devices, cpu);
368 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
369 "cpuidle");
370 if (!error)
371 kobject_uevent(&dev->kobj, KOBJ_ADD);
372 return error;
376 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
377 * @sysdev: the target device
379 void cpuidle_remove_sysfs(struct sys_device *sysdev)
381 int cpu = sysdev->id;
382 struct cpuidle_device *dev;
384 dev = per_cpu(cpuidle_devices, cpu);
385 kobject_put(&dev->kobj);