[PATCH] w83627ehf: Add alarms support
[linux-2.6/linux-loongson.git] / drivers / base / sys.c
blob6858178b3aff8d28e727e2d487adf289817324bd
1 /*
2 * sys.c - pseudo-bus for system 'devices' (cpus, PICs, timers, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * 2002-3 Open Source Development Lab
7 * This file is released under the GPLv2
9 * This exports a 'system' bus type.
10 * By default, a 'sys' bus gets added to the root of the system. There will
11 * always be core system devices. Devices can use sysdev_register() to
12 * add themselves as children of the system bus.
15 #include <linux/config.h>
16 #include <linux/sysdev.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/pm.h>
24 #include <linux/device.h>
25 #include <asm/semaphore.h>
27 #include "base.h"
29 extern struct subsystem devices_subsys;
31 #define to_sysdev(k) container_of(k, struct sys_device, kobj)
32 #define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)
35 static ssize_t
36 sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
38 struct sys_device * sysdev = to_sysdev(kobj);
39 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
41 if (sysdev_attr->show)
42 return sysdev_attr->show(sysdev, buffer);
43 return -EIO;
47 static ssize_t
48 sysdev_store(struct kobject * kobj, struct attribute * attr,
49 const char * buffer, size_t count)
51 struct sys_device * sysdev = to_sysdev(kobj);
52 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
54 if (sysdev_attr->store)
55 return sysdev_attr->store(sysdev, buffer, count);
56 return -EIO;
59 static struct sysfs_ops sysfs_ops = {
60 .show = sysdev_show,
61 .store = sysdev_store,
64 static struct kobj_type ktype_sysdev = {
65 .sysfs_ops = &sysfs_ops,
69 int sysdev_create_file(struct sys_device * s, struct sysdev_attribute * a)
71 return sysfs_create_file(&s->kobj, &a->attr);
75 void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
77 sysfs_remove_file(&s->kobj, &a->attr);
80 EXPORT_SYMBOL_GPL(sysdev_create_file);
81 EXPORT_SYMBOL_GPL(sysdev_remove_file);
83 #define to_sysdev_class(k) container_of(k, struct sysdev_class, kset.kobj)
84 #define to_sysdev_class_attr(a) container_of(a, \
85 struct sysdev_class_attribute, attr)
87 static ssize_t sysdev_class_show(struct kobject *kobj, struct attribute *attr,
88 char *buffer)
90 struct sysdev_class * class = to_sysdev_class(kobj);
91 struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);
93 if (class_attr->show)
94 return class_attr->show(class, buffer);
95 return -EIO;
98 static ssize_t sysdev_class_store(struct kobject *kobj, struct attribute *attr,
99 const char *buffer, size_t count)
101 struct sysdev_class * class = to_sysdev_class(kobj);
102 struct sysdev_class_attribute * class_attr = to_sysdev_class_attr(attr);
104 if (class_attr->store)
105 return class_attr->store(class, buffer, count);
106 return -EIO;
109 static struct sysfs_ops sysfs_class_ops = {
110 .show = sysdev_class_show,
111 .store = sysdev_class_store,
114 static struct kobj_type ktype_sysdev_class = {
115 .sysfs_ops = &sysfs_class_ops,
118 int sysdev_class_create_file(struct sysdev_class *c,
119 struct sysdev_class_attribute *a)
121 return sysfs_create_file(&c->kset.kobj, &a->attr);
123 EXPORT_SYMBOL_GPL(sysdev_class_create_file);
125 void sysdev_class_remove_file(struct sysdev_class *c,
126 struct sysdev_class_attribute *a)
128 sysfs_remove_file(&c->kset.kobj, &a->attr);
130 EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
133 * declare system_subsys
135 static decl_subsys(system, &ktype_sysdev_class, NULL);
137 int sysdev_class_register(struct sysdev_class * cls)
139 pr_debug("Registering sysdev class '%s'\n",
140 kobject_name(&cls->kset.kobj));
141 INIT_LIST_HEAD(&cls->drivers);
142 cls->kset.subsys = &system_subsys;
143 kset_set_kset_s(cls, system_subsys);
144 return kset_register(&cls->kset);
147 void sysdev_class_unregister(struct sysdev_class * cls)
149 pr_debug("Unregistering sysdev class '%s'\n",
150 kobject_name(&cls->kset.kobj));
151 kset_unregister(&cls->kset);
154 EXPORT_SYMBOL_GPL(sysdev_class_register);
155 EXPORT_SYMBOL_GPL(sysdev_class_unregister);
158 static LIST_HEAD(sysdev_drivers);
159 static DECLARE_MUTEX(sysdev_drivers_lock);
162 * sysdev_driver_register - Register auxillary driver
163 * @cls: Device class driver belongs to.
164 * @drv: Driver.
166 * If @cls is valid, then @drv is inserted into @cls->drivers to be
167 * called on each operation on devices of that class. The refcount
168 * of @cls is incremented.
169 * Otherwise, @drv is inserted into sysdev_drivers, and called for
170 * each device.
173 int sysdev_driver_register(struct sysdev_class * cls,
174 struct sysdev_driver * drv)
176 down(&sysdev_drivers_lock);
177 if (cls && kset_get(&cls->kset)) {
178 list_add_tail(&drv->entry, &cls->drivers);
180 /* If devices of this class already exist, tell the driver */
181 if (drv->add) {
182 struct sys_device *dev;
183 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
184 drv->add(dev);
186 } else
187 list_add_tail(&drv->entry, &sysdev_drivers);
188 up(&sysdev_drivers_lock);
189 return 0;
194 * sysdev_driver_unregister - Remove an auxillary driver.
195 * @cls: Class driver belongs to.
196 * @drv: Driver.
198 void sysdev_driver_unregister(struct sysdev_class * cls,
199 struct sysdev_driver * drv)
201 down(&sysdev_drivers_lock);
202 list_del_init(&drv->entry);
203 if (cls) {
204 if (drv->remove) {
205 struct sys_device *dev;
206 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
207 drv->remove(dev);
209 kset_put(&cls->kset);
211 up(&sysdev_drivers_lock);
214 EXPORT_SYMBOL_GPL(sysdev_driver_register);
215 EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
220 * sysdev_register - add a system device to the tree
221 * @sysdev: device in question
224 int sysdev_register(struct sys_device * sysdev)
226 int error;
227 struct sysdev_class * cls = sysdev->cls;
229 if (!cls)
230 return -EINVAL;
232 /* Make sure the kset is set */
233 sysdev->kobj.kset = &cls->kset;
235 /* But make sure we point to the right type for sysfs translation */
236 sysdev->kobj.ktype = &ktype_sysdev;
237 error = kobject_set_name(&sysdev->kobj, "%s%d",
238 kobject_name(&cls->kset.kobj), sysdev->id);
239 if (error)
240 return error;
242 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
244 /* Register the object */
245 error = kobject_register(&sysdev->kobj);
247 if (!error) {
248 struct sysdev_driver * drv;
250 down(&sysdev_drivers_lock);
251 /* Generic notification is implicit, because it's that
252 * code that should have called us.
255 /* Notify global drivers */
256 list_for_each_entry(drv, &sysdev_drivers, entry) {
257 if (drv->add)
258 drv->add(sysdev);
261 /* Notify class auxillary drivers */
262 list_for_each_entry(drv, &cls->drivers, entry) {
263 if (drv->add)
264 drv->add(sysdev);
266 up(&sysdev_drivers_lock);
268 return error;
271 void sysdev_unregister(struct sys_device * sysdev)
273 struct sysdev_driver * drv;
275 down(&sysdev_drivers_lock);
276 list_for_each_entry(drv, &sysdev_drivers, entry) {
277 if (drv->remove)
278 drv->remove(sysdev);
281 list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
282 if (drv->remove)
283 drv->remove(sysdev);
285 up(&sysdev_drivers_lock);
287 kobject_unregister(&sysdev->kobj);
293 * sysdev_shutdown - Shut down all system devices.
295 * Loop over each class of system devices, and the devices in each
296 * of those classes. For each device, we call the shutdown method for
297 * each driver registered for the device - the globals, the auxillaries,
298 * and the class driver.
300 * Note: The list is iterated in reverse order, so that we shut down
301 * child devices before we shut down thier parents. The list ordering
302 * is guaranteed by virtue of the fact that child devices are registered
303 * after their parents.
306 void sysdev_shutdown(void)
308 struct sysdev_class * cls;
310 pr_debug("Shutting Down System Devices\n");
312 down(&sysdev_drivers_lock);
313 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
314 kset.kobj.entry) {
315 struct sys_device * sysdev;
317 pr_debug("Shutting down type '%s':\n",
318 kobject_name(&cls->kset.kobj));
320 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
321 struct sysdev_driver * drv;
322 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
324 /* Call global drivers first. */
325 list_for_each_entry(drv, &sysdev_drivers, entry) {
326 if (drv->shutdown)
327 drv->shutdown(sysdev);
330 /* Call auxillary drivers next. */
331 list_for_each_entry(drv, &cls->drivers, entry) {
332 if (drv->shutdown)
333 drv->shutdown(sysdev);
336 /* Now call the generic one */
337 if (cls->shutdown)
338 cls->shutdown(sysdev);
341 up(&sysdev_drivers_lock);
344 static void __sysdev_resume(struct sys_device *dev)
346 struct sysdev_class *cls = dev->cls;
347 struct sysdev_driver *drv;
349 /* First, call the class-specific one */
350 if (cls->resume)
351 cls->resume(dev);
353 /* Call auxillary drivers next. */
354 list_for_each_entry(drv, &cls->drivers, entry) {
355 if (drv->resume)
356 drv->resume(dev);
359 /* Call global drivers. */
360 list_for_each_entry(drv, &sysdev_drivers, entry) {
361 if (drv->resume)
362 drv->resume(dev);
367 * sysdev_suspend - Suspend all system devices.
368 * @state: Power state to enter.
370 * We perform an almost identical operation as sys_device_shutdown()
371 * above, though calling ->suspend() instead. Interrupts are disabled
372 * when this called. Devices are responsible for both saving state and
373 * quiescing or powering down the device.
375 * This is only called by the device PM core, so we let them handle
376 * all synchronization.
379 int sysdev_suspend(pm_message_t state)
381 struct sysdev_class * cls;
382 struct sys_device *sysdev, *err_dev;
383 struct sysdev_driver *drv, *err_drv;
384 int ret;
386 pr_debug("Suspending System Devices\n");
388 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
389 kset.kobj.entry) {
391 pr_debug("Suspending type '%s':\n",
392 kobject_name(&cls->kset.kobj));
394 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
395 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
397 /* Call global drivers first. */
398 list_for_each_entry(drv, &sysdev_drivers, entry) {
399 if (drv->suspend) {
400 ret = drv->suspend(sysdev, state);
401 if (ret)
402 goto gbl_driver;
406 /* Call auxillary drivers next. */
407 list_for_each_entry(drv, &cls->drivers, entry) {
408 if (drv->suspend) {
409 ret = drv->suspend(sysdev, state);
410 if (ret)
411 goto aux_driver;
415 /* Now call the generic one */
416 if (cls->suspend) {
417 ret = cls->suspend(sysdev, state);
418 if (ret)
419 goto cls_driver;
423 return 0;
424 /* resume current sysdev */
425 cls_driver:
426 drv = NULL;
427 printk(KERN_ERR "Class suspend failed for %s\n",
428 kobject_name(&sysdev->kobj));
430 aux_driver:
431 if (drv)
432 printk(KERN_ERR "Class driver suspend failed for %s\n",
433 kobject_name(&sysdev->kobj));
434 list_for_each_entry(err_drv, &cls->drivers, entry) {
435 if (err_drv == drv)
436 break;
437 if (err_drv->resume)
438 err_drv->resume(sysdev);
440 drv = NULL;
442 gbl_driver:
443 if (drv)
444 printk(KERN_ERR "sysdev driver suspend failed for %s\n",
445 kobject_name(&sysdev->kobj));
446 list_for_each_entry(err_drv, &sysdev_drivers, entry) {
447 if (err_drv == drv)
448 break;
449 if (err_drv->resume)
450 err_drv->resume(sysdev);
452 /* resume other sysdevs in current class */
453 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
454 if (err_dev == sysdev)
455 break;
456 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
457 __sysdev_resume(err_dev);
460 /* resume other classes */
461 list_for_each_entry_continue(cls, &system_subsys.kset.list,
462 kset.kobj.entry) {
463 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
464 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
465 __sysdev_resume(err_dev);
468 return ret;
473 * sysdev_resume - Bring system devices back to life.
475 * Similar to sys_device_suspend(), but we iterate the list forwards
476 * to guarantee that parent devices are resumed before their children.
478 * Note: Interrupts are disabled when called.
481 int sysdev_resume(void)
483 struct sysdev_class * cls;
485 pr_debug("Resuming System Devices\n");
487 list_for_each_entry(cls, &system_subsys.kset.list, kset.kobj.entry) {
488 struct sys_device * sysdev;
490 pr_debug("Resuming type '%s':\n",
491 kobject_name(&cls->kset.kobj));
493 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
494 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
496 __sysdev_resume(sysdev);
499 return 0;
503 int __init system_bus_init(void)
505 system_subsys.kset.kobj.parent = &devices_subsys.kset.kobj;
506 return subsystem_register(&system_subsys);
509 EXPORT_SYMBOL_GPL(sysdev_register);
510 EXPORT_SYMBOL_GPL(sysdev_unregister);