fat: small optimization to __fat_readdir()
[linux-2.6/mini2440.git] / drivers / cpuidle / sysfs.c
blob31a0e0b455b6e3c61ffef1ef004351fac0085dcd
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 sys_device *dev,
25 struct sysdev_attribute *attr, 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 sys_device *dev,
44 struct sysdev_attribute *attr, 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 sys_device *dev,
59 struct sysdev_attribute *attr, 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 sys_device *dev,
74 struct sysdev_attribute *attr,
75 const char *buf, size_t count)
77 char gov_name[CPUIDLE_NAME_LEN];
78 int ret = -EINVAL;
79 size_t len = count;
80 struct cpuidle_governor *gov;
82 if (!len || len >= sizeof(gov_name))
83 return -EINVAL;
85 memcpy(gov_name, buf, len);
86 gov_name[len] = '\0';
87 if (gov_name[len - 1] == '\n')
88 gov_name[--len] = '\0';
90 mutex_lock(&cpuidle_lock);
92 list_for_each_entry(gov, &cpuidle_governors, governor_list) {
93 if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
94 ret = cpuidle_switch_governor(gov);
95 break;
99 mutex_unlock(&cpuidle_lock);
101 if (ret)
102 return ret;
103 else
104 return count;
107 static SYSDEV_ATTR(current_driver, 0444, show_current_driver, NULL);
108 static SYSDEV_ATTR(current_governor_ro, 0444, show_current_governor, NULL);
110 static struct attribute *cpuclass_default_attrs[] = {
111 &attr_current_driver.attr,
112 &attr_current_governor_ro.attr,
113 NULL
116 static SYSDEV_ATTR(available_governors, 0444, show_available_governors, NULL);
117 static SYSDEV_ATTR(current_governor, 0644, show_current_governor,
118 store_current_governor);
120 static struct attribute *cpuclass_switch_attrs[] = {
121 &attr_available_governors.attr,
122 &attr_current_driver.attr,
123 &attr_current_governor.attr,
124 NULL
127 static struct attribute_group cpuclass_attr_group = {
128 .attrs = cpuclass_default_attrs,
129 .name = "cpuidle",
133 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
135 int cpuidle_add_class_sysfs(struct sysdev_class *cls)
137 if (sysfs_switch)
138 cpuclass_attr_group.attrs = cpuclass_switch_attrs;
140 return sysfs_create_group(&cls->kset.kobj, &cpuclass_attr_group);
144 * cpuidle_remove_class_sysfs - remove CPU global sysfs attributes
146 void cpuidle_remove_class_sysfs(struct sysdev_class *cls)
148 sysfs_remove_group(&cls->kset.kobj, &cpuclass_attr_group);
151 struct cpuidle_attr {
152 struct attribute attr;
153 ssize_t (*show)(struct cpuidle_device *, char *);
154 ssize_t (*store)(struct cpuidle_device *, const char *, size_t count);
157 #define define_one_ro(_name, show) \
158 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
159 #define define_one_rw(_name, show, store) \
160 static struct cpuidle_attr attr_##_name = __ATTR(_name, 0644, show, store)
162 #define kobj_to_cpuidledev(k) container_of(k, struct cpuidle_device, kobj)
163 #define attr_to_cpuidleattr(a) container_of(a, struct cpuidle_attr, attr)
164 static ssize_t cpuidle_show(struct kobject * kobj, struct attribute * attr ,char * buf)
166 int ret = -EIO;
167 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
168 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
170 if (cattr->show) {
171 mutex_lock(&cpuidle_lock);
172 ret = cattr->show(dev, buf);
173 mutex_unlock(&cpuidle_lock);
175 return ret;
178 static ssize_t cpuidle_store(struct kobject * kobj, struct attribute * attr,
179 const char * buf, size_t count)
181 int ret = -EIO;
182 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
183 struct cpuidle_attr * cattr = attr_to_cpuidleattr(attr);
185 if (cattr->store) {
186 mutex_lock(&cpuidle_lock);
187 ret = cattr->store(dev, buf, count);
188 mutex_unlock(&cpuidle_lock);
190 return ret;
193 static struct sysfs_ops cpuidle_sysfs_ops = {
194 .show = cpuidle_show,
195 .store = cpuidle_store,
198 static void cpuidle_sysfs_release(struct kobject *kobj)
200 struct cpuidle_device *dev = kobj_to_cpuidledev(kobj);
202 complete(&dev->kobj_unregister);
205 static struct kobj_type ktype_cpuidle = {
206 .sysfs_ops = &cpuidle_sysfs_ops,
207 .release = cpuidle_sysfs_release,
210 struct cpuidle_state_attr {
211 struct attribute attr;
212 ssize_t (*show)(struct cpuidle_state *, char *);
213 ssize_t (*store)(struct cpuidle_state *, const char *, size_t);
216 #define define_one_state_ro(_name, show) \
217 static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0444, show, NULL)
219 #define define_show_state_function(_name) \
220 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
222 return sprintf(buf, "%u\n", state->_name);\
225 #define define_show_state_ull_function(_name) \
226 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
228 return sprintf(buf, "%llu\n", state->_name);\
231 #define define_show_state_str_function(_name) \
232 static ssize_t show_state_##_name(struct cpuidle_state *state, char *buf) \
234 if (state->_name[0] == '\0')\
235 return sprintf(buf, "<null>\n");\
236 return sprintf(buf, "%s\n", state->_name);\
239 define_show_state_function(exit_latency)
240 define_show_state_function(power_usage)
241 define_show_state_ull_function(usage)
242 define_show_state_ull_function(time)
243 define_show_state_str_function(name)
244 define_show_state_str_function(desc)
246 define_one_state_ro(name, show_state_name);
247 define_one_state_ro(desc, show_state_desc);
248 define_one_state_ro(latency, show_state_exit_latency);
249 define_one_state_ro(power, show_state_power_usage);
250 define_one_state_ro(usage, show_state_usage);
251 define_one_state_ro(time, show_state_time);
253 static struct attribute *cpuidle_state_default_attrs[] = {
254 &attr_name.attr,
255 &attr_desc.attr,
256 &attr_latency.attr,
257 &attr_power.attr,
258 &attr_usage.attr,
259 &attr_time.attr,
260 NULL
263 #define kobj_to_state_obj(k) container_of(k, struct cpuidle_state_kobj, kobj)
264 #define kobj_to_state(k) (kobj_to_state_obj(k)->state)
265 #define attr_to_stateattr(a) container_of(a, struct cpuidle_state_attr, attr)
266 static ssize_t cpuidle_state_show(struct kobject * kobj,
267 struct attribute * attr ,char * buf)
269 int ret = -EIO;
270 struct cpuidle_state *state = kobj_to_state(kobj);
271 struct cpuidle_state_attr * cattr = attr_to_stateattr(attr);
273 if (cattr->show)
274 ret = cattr->show(state, buf);
276 return ret;
279 static struct sysfs_ops cpuidle_state_sysfs_ops = {
280 .show = cpuidle_state_show,
283 static void cpuidle_state_sysfs_release(struct kobject *kobj)
285 struct cpuidle_state_kobj *state_obj = kobj_to_state_obj(kobj);
287 complete(&state_obj->kobj_unregister);
290 static struct kobj_type ktype_state_cpuidle = {
291 .sysfs_ops = &cpuidle_state_sysfs_ops,
292 .default_attrs = cpuidle_state_default_attrs,
293 .release = cpuidle_state_sysfs_release,
296 static void inline cpuidle_free_state_kobj(struct cpuidle_device *device, int i)
298 kobject_put(&device->kobjs[i]->kobj);
299 wait_for_completion(&device->kobjs[i]->kobj_unregister);
300 kfree(device->kobjs[i]);
301 device->kobjs[i] = NULL;
305 * cpuidle_add_driver_sysfs - adds driver-specific sysfs attributes
306 * @device: the target device
308 int cpuidle_add_state_sysfs(struct cpuidle_device *device)
310 int i, ret = -ENOMEM;
311 struct cpuidle_state_kobj *kobj;
313 /* state statistics */
314 for (i = 0; i < device->state_count; i++) {
315 kobj = kzalloc(sizeof(struct cpuidle_state_kobj), GFP_KERNEL);
316 if (!kobj)
317 goto error_state;
318 kobj->state = &device->states[i];
319 init_completion(&kobj->kobj_unregister);
321 ret = kobject_init_and_add(&kobj->kobj, &ktype_state_cpuidle, &device->kobj,
322 "state%d", i);
323 if (ret) {
324 kfree(kobj);
325 goto error_state;
327 kobject_uevent(&kobj->kobj, KOBJ_ADD);
328 device->kobjs[i] = kobj;
331 return 0;
333 error_state:
334 for (i = i - 1; i >= 0; i--)
335 cpuidle_free_state_kobj(device, i);
336 return ret;
340 * cpuidle_remove_driver_sysfs - removes driver-specific sysfs attributes
341 * @device: the target device
343 void cpuidle_remove_state_sysfs(struct cpuidle_device *device)
345 int i;
347 for (i = 0; i < device->state_count; i++)
348 cpuidle_free_state_kobj(device, i);
352 * cpuidle_add_sysfs - creates a sysfs instance for the target device
353 * @sysdev: the target device
355 int cpuidle_add_sysfs(struct sys_device *sysdev)
357 int cpu = sysdev->id;
358 struct cpuidle_device *dev;
359 int error;
361 dev = per_cpu(cpuidle_devices, cpu);
362 error = kobject_init_and_add(&dev->kobj, &ktype_cpuidle, &sysdev->kobj,
363 "cpuidle");
364 if (!error)
365 kobject_uevent(&dev->kobj, KOBJ_ADD);
366 return error;
370 * cpuidle_remove_sysfs - deletes a sysfs instance on the target device
371 * @sysdev: the target device
373 void cpuidle_remove_sysfs(struct sys_device *sysdev)
375 int cpu = sysdev->id;
376 struct cpuidle_device *dev;
378 dev = per_cpu(cpuidle_devices, cpu);
379 kobject_put(&dev->kobj);