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>
25 extern struct subsystem devices_subsys;
27 #define to_sysdev(k) container_of(k, struct sys_device, kobj)
28 #define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)
32 sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
34 struct sys_device * sysdev = to_sysdev(kobj);
35 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
37 if (sysdev_attr->show)
38 return sysdev_attr->show(sysdev, buffer);
44 sysdev_store(struct kobject * kobj, struct attribute * attr,
45 const char * buffer, size_t count)
47 struct sys_device * sysdev = to_sysdev(kobj);
48 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
50 if (sysdev_attr->store)
51 return sysdev_attr->store(sysdev, buffer, count);
55 static struct sysfs_ops sysfs_ops = {
57 .store = sysdev_store,
60 static struct kobj_type ktype_sysdev = {
61 .sysfs_ops = &sysfs_ops,
65 int sysdev_create_file(struct sys_device * s, struct sysdev_attribute * a)
67 return sysfs_create_file(&s->kobj, &a->attr);
71 void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
73 sysfs_remove_file(&s->kobj, &a->attr);
76 EXPORT_SYMBOL_GPL(sysdev_create_file);
77 EXPORT_SYMBOL_GPL(sysdev_remove_file);
80 * declare system_subsys
82 static decl_subsys(system, &ktype_sysdev, NULL);
84 int sysdev_class_register(struct sysdev_class * cls)
86 pr_debug("Registering sysdev class '%s'\n",
87 kobject_name(&cls->kset.kobj));
88 INIT_LIST_HEAD(&cls->drivers);
89 cls->kset.subsys = &system_subsys;
90 kset_set_kset_s(cls, system_subsys);
91 return kset_register(&cls->kset);
94 void sysdev_class_unregister(struct sysdev_class * cls)
96 pr_debug("Unregistering sysdev class '%s'\n",
97 kobject_name(&cls->kset.kobj));
98 kset_unregister(&cls->kset);
101 EXPORT_SYMBOL_GPL(sysdev_class_register);
102 EXPORT_SYMBOL_GPL(sysdev_class_unregister);
105 static LIST_HEAD(sysdev_drivers);
106 static DECLARE_MUTEX(sysdev_drivers_lock);
109 * sysdev_driver_register - Register auxillary driver
110 * @cls: Device class driver belongs to.
113 * If @cls is valid, then @drv is inserted into @cls->drivers to be
114 * called on each operation on devices of that class. The refcount
115 * of @cls is incremented.
116 * Otherwise, @drv is inserted into sysdev_drivers, and called for
120 int sysdev_driver_register(struct sysdev_class * cls,
121 struct sysdev_driver * drv)
123 down(&sysdev_drivers_lock);
124 if (cls && kset_get(&cls->kset)) {
125 list_add_tail(&drv->entry, &cls->drivers);
127 /* If devices of this class already exist, tell the driver */
129 struct sys_device *dev;
130 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
134 list_add_tail(&drv->entry, &sysdev_drivers);
135 up(&sysdev_drivers_lock);
141 * sysdev_driver_unregister - Remove an auxillary driver.
142 * @cls: Class driver belongs to.
145 void sysdev_driver_unregister(struct sysdev_class * cls,
146 struct sysdev_driver * drv)
148 down(&sysdev_drivers_lock);
149 list_del_init(&drv->entry);
152 struct sys_device *dev;
153 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
156 kset_put(&cls->kset);
158 up(&sysdev_drivers_lock);
161 EXPORT_SYMBOL_GPL(sysdev_driver_register);
162 EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
167 * sysdev_register - add a system device to the tree
168 * @sysdev: device in question
171 int sysdev_register(struct sys_device * sysdev)
174 struct sysdev_class * cls = sysdev->cls;
179 /* Make sure the kset is set */
180 sysdev->kobj.kset = &cls->kset;
182 /* But make sure we point to the right type for sysfs translation */
183 sysdev->kobj.ktype = &ktype_sysdev;
184 error = kobject_set_name(&sysdev->kobj, "%s%d",
185 kobject_name(&cls->kset.kobj), sysdev->id);
189 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
191 /* Register the object */
192 error = kobject_register(&sysdev->kobj);
195 struct sysdev_driver * drv;
197 down(&sysdev_drivers_lock);
198 /* Generic notification is implicit, because it's that
199 * code that should have called us.
202 /* Notify global drivers */
203 list_for_each_entry(drv, &sysdev_drivers, entry) {
208 /* Notify class auxillary drivers */
209 list_for_each_entry(drv, &cls->drivers, entry) {
213 up(&sysdev_drivers_lock);
218 void sysdev_unregister(struct sys_device * sysdev)
220 struct sysdev_driver * drv;
222 down(&sysdev_drivers_lock);
223 list_for_each_entry(drv, &sysdev_drivers, entry) {
228 list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
232 up(&sysdev_drivers_lock);
234 kobject_unregister(&sysdev->kobj);
240 * sysdev_shutdown - Shut down all system devices.
242 * Loop over each class of system devices, and the devices in each
243 * of those classes. For each device, we call the shutdown method for
244 * each driver registered for the device - the globals, the auxillaries,
245 * and the class driver.
247 * Note: The list is iterated in reverse order, so that we shut down
248 * child devices before we shut down thier parents. The list ordering
249 * is guaranteed by virtue of the fact that child devices are registered
250 * after their parents.
253 void sysdev_shutdown(void)
255 struct sysdev_class * cls;
257 pr_debug("Shutting Down System Devices\n");
259 down(&sysdev_drivers_lock);
260 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
262 struct sys_device * sysdev;
264 pr_debug("Shutting down type '%s':\n",
265 kobject_name(&cls->kset.kobj));
267 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
268 struct sysdev_driver * drv;
269 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
271 /* Call global drivers first. */
272 list_for_each_entry(drv, &sysdev_drivers, entry) {
274 drv->shutdown(sysdev);
277 /* Call auxillary drivers next. */
278 list_for_each_entry(drv, &cls->drivers, entry) {
280 drv->shutdown(sysdev);
283 /* Now call the generic one */
285 cls->shutdown(sysdev);
288 up(&sysdev_drivers_lock);
291 static void __sysdev_resume(struct sys_device *dev)
293 struct sysdev_class *cls = dev->cls;
294 struct sysdev_driver *drv;
296 /* First, call the class-specific one */
300 /* Call auxillary drivers next. */
301 list_for_each_entry(drv, &cls->drivers, entry) {
306 /* Call global drivers. */
307 list_for_each_entry(drv, &sysdev_drivers, entry) {
314 * sysdev_suspend - Suspend all system devices.
315 * @state: Power state to enter.
317 * We perform an almost identical operation as sys_device_shutdown()
318 * above, though calling ->suspend() instead. Interrupts are disabled
319 * when this called. Devices are responsible for both saving state and
320 * quiescing or powering down the device.
322 * This is only called by the device PM core, so we let them handle
323 * all synchronization.
326 int sysdev_suspend(pm_message_t state)
328 struct sysdev_class * cls;
329 struct sys_device *sysdev, *err_dev;
330 struct sysdev_driver *drv, *err_drv;
333 pr_debug("Suspending System Devices\n");
335 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
338 pr_debug("Suspending type '%s':\n",
339 kobject_name(&cls->kset.kobj));
341 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
342 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
344 /* Call global drivers first. */
345 list_for_each_entry(drv, &sysdev_drivers, entry) {
347 ret = drv->suspend(sysdev, state);
353 /* Call auxillary drivers next. */
354 list_for_each_entry(drv, &cls->drivers, entry) {
356 ret = drv->suspend(sysdev, state);
362 /* Now call the generic one */
364 ret = cls->suspend(sysdev, state);
371 /* resume current sysdev */
374 printk(KERN_ERR "Class suspend failed for %s\n",
375 kobject_name(&sysdev->kobj));
379 printk(KERN_ERR "Class driver suspend failed for %s\n",
380 kobject_name(&sysdev->kobj));
381 list_for_each_entry(err_drv, &cls->drivers, entry) {
385 err_drv->resume(sysdev);
391 printk(KERN_ERR "sysdev driver suspend failed for %s\n",
392 kobject_name(&sysdev->kobj));
393 list_for_each_entry(err_drv, &sysdev_drivers, entry) {
397 err_drv->resume(sysdev);
399 /* resume other sysdevs in current class */
400 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
401 if (err_dev == sysdev)
403 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
404 __sysdev_resume(err_dev);
407 /* resume other classes */
408 list_for_each_entry_continue(cls, &system_subsys.kset.list,
410 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
411 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
412 __sysdev_resume(err_dev);
420 * sysdev_resume - Bring system devices back to life.
422 * Similar to sys_device_suspend(), but we iterate the list forwards
423 * to guarantee that parent devices are resumed before their children.
425 * Note: Interrupts are disabled when called.
428 int sysdev_resume(void)
430 struct sysdev_class * cls;
432 pr_debug("Resuming System Devices\n");
434 list_for_each_entry(cls, &system_subsys.kset.list, kset.kobj.entry) {
435 struct sys_device * sysdev;
437 pr_debug("Resuming type '%s':\n",
438 kobject_name(&cls->kset.kobj));
440 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
441 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
443 __sysdev_resume(sysdev);
450 int __init system_bus_init(void)
452 system_subsys.kset.kobj.parent = &devices_subsys.kset.kobj;
453 return subsystem_register(&system_subsys);
456 EXPORT_SYMBOL_GPL(sysdev_register);
457 EXPORT_SYMBOL_GPL(sysdev_unregister);