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>
24 #include <linux/device.h>
25 #include <asm/semaphore.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)
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);
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);
59 static struct sysfs_ops sysfs_ops = {
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);
84 * declare system_subsys
86 static decl_subsys(system, &ktype_sysdev, NULL);
88 int sysdev_class_register(struct sysdev_class * cls)
90 pr_debug("Registering sysdev class '%s'\n",
91 kobject_name(&cls->kset.kobj));
92 INIT_LIST_HEAD(&cls->drivers);
93 cls->kset.subsys = &system_subsys;
94 kset_set_kset_s(cls, system_subsys);
95 return kset_register(&cls->kset);
98 void sysdev_class_unregister(struct sysdev_class * cls)
100 pr_debug("Unregistering sysdev class '%s'\n",
101 kobject_name(&cls->kset.kobj));
102 kset_unregister(&cls->kset);
105 EXPORT_SYMBOL_GPL(sysdev_class_register);
106 EXPORT_SYMBOL_GPL(sysdev_class_unregister);
109 static LIST_HEAD(sysdev_drivers);
110 static DECLARE_MUTEX(sysdev_drivers_lock);
113 * sysdev_driver_register - Register auxillary driver
114 * @cls: Device class driver belongs to.
117 * If @cls is valid, then @drv is inserted into @cls->drivers to be
118 * called on each operation on devices of that class. The refcount
119 * of @cls is incremented.
120 * Otherwise, @drv is inserted into sysdev_drivers, and called for
124 int sysdev_driver_register(struct sysdev_class * cls,
125 struct sysdev_driver * drv)
127 down(&sysdev_drivers_lock);
128 if (cls && kset_get(&cls->kset)) {
129 list_add_tail(&drv->entry, &cls->drivers);
131 /* If devices of this class already exist, tell the driver */
133 struct sys_device *dev;
134 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
138 list_add_tail(&drv->entry, &sysdev_drivers);
139 up(&sysdev_drivers_lock);
145 * sysdev_driver_unregister - Remove an auxillary driver.
146 * @cls: Class driver belongs to.
149 void sysdev_driver_unregister(struct sysdev_class * cls,
150 struct sysdev_driver * drv)
152 down(&sysdev_drivers_lock);
153 list_del_init(&drv->entry);
156 struct sys_device *dev;
157 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
160 kset_put(&cls->kset);
162 up(&sysdev_drivers_lock);
165 EXPORT_SYMBOL_GPL(sysdev_driver_register);
166 EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
171 * sysdev_register - add a system device to the tree
172 * @sysdev: device in question
175 int sysdev_register(struct sys_device * sysdev)
178 struct sysdev_class * cls = sysdev->cls;
183 /* Make sure the kset is set */
184 sysdev->kobj.kset = &cls->kset;
186 /* But make sure we point to the right type for sysfs translation */
187 sysdev->kobj.ktype = &ktype_sysdev;
188 error = kobject_set_name(&sysdev->kobj, "%s%d",
189 kobject_name(&cls->kset.kobj), sysdev->id);
193 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
195 /* Register the object */
196 error = kobject_register(&sysdev->kobj);
199 struct sysdev_driver * drv;
201 down(&sysdev_drivers_lock);
202 /* Generic notification is implicit, because it's that
203 * code that should have called us.
206 /* Notify global drivers */
207 list_for_each_entry(drv, &sysdev_drivers, entry) {
212 /* Notify class auxillary drivers */
213 list_for_each_entry(drv, &cls->drivers, entry) {
217 up(&sysdev_drivers_lock);
222 void sysdev_unregister(struct sys_device * sysdev)
224 struct sysdev_driver * drv;
226 down(&sysdev_drivers_lock);
227 list_for_each_entry(drv, &sysdev_drivers, entry) {
232 list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
236 up(&sysdev_drivers_lock);
238 kobject_unregister(&sysdev->kobj);
244 * sysdev_shutdown - Shut down all system devices.
246 * Loop over each class of system devices, and the devices in each
247 * of those classes. For each device, we call the shutdown method for
248 * each driver registered for the device - the globals, the auxillaries,
249 * and the class driver.
251 * Note: The list is iterated in reverse order, so that we shut down
252 * child devices before we shut down thier parents. The list ordering
253 * is guaranteed by virtue of the fact that child devices are registered
254 * after their parents.
257 void sysdev_shutdown(void)
259 struct sysdev_class * cls;
261 pr_debug("Shutting Down System Devices\n");
263 down(&sysdev_drivers_lock);
264 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
266 struct sys_device * sysdev;
268 pr_debug("Shutting down type '%s':\n",
269 kobject_name(&cls->kset.kobj));
271 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
272 struct sysdev_driver * drv;
273 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
275 /* Call global drivers first. */
276 list_for_each_entry(drv, &sysdev_drivers, entry) {
278 drv->shutdown(sysdev);
281 /* Call auxillary drivers next. */
282 list_for_each_entry(drv, &cls->drivers, entry) {
284 drv->shutdown(sysdev);
287 /* Now call the generic one */
289 cls->shutdown(sysdev);
292 up(&sysdev_drivers_lock);
295 static void __sysdev_resume(struct sys_device *dev)
297 struct sysdev_class *cls = dev->cls;
298 struct sysdev_driver *drv;
300 /* First, call the class-specific one */
304 /* Call auxillary drivers next. */
305 list_for_each_entry(drv, &cls->drivers, entry) {
310 /* Call global drivers. */
311 list_for_each_entry(drv, &sysdev_drivers, entry) {
318 * sysdev_suspend - Suspend all system devices.
319 * @state: Power state to enter.
321 * We perform an almost identical operation as sys_device_shutdown()
322 * above, though calling ->suspend() instead. Interrupts are disabled
323 * when this called. Devices are responsible for both saving state and
324 * quiescing or powering down the device.
326 * This is only called by the device PM core, so we let them handle
327 * all synchronization.
330 int sysdev_suspend(pm_message_t state)
332 struct sysdev_class * cls;
333 struct sys_device *sysdev, *err_dev;
334 struct sysdev_driver *drv, *err_drv;
337 pr_debug("Suspending System Devices\n");
339 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
342 pr_debug("Suspending type '%s':\n",
343 kobject_name(&cls->kset.kobj));
345 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
346 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
348 /* Call global drivers first. */
349 list_for_each_entry(drv, &sysdev_drivers, entry) {
351 ret = drv->suspend(sysdev, state);
357 /* Call auxillary drivers next. */
358 list_for_each_entry(drv, &cls->drivers, entry) {
360 ret = drv->suspend(sysdev, state);
366 /* Now call the generic one */
368 ret = cls->suspend(sysdev, state);
375 /* resume current sysdev */
378 printk(KERN_ERR "Class suspend failed for %s\n",
379 kobject_name(&sysdev->kobj));
383 printk(KERN_ERR "Class driver suspend failed for %s\n",
384 kobject_name(&sysdev->kobj));
385 list_for_each_entry(err_drv, &cls->drivers, entry) {
389 err_drv->resume(sysdev);
395 printk(KERN_ERR "sysdev driver suspend failed for %s\n",
396 kobject_name(&sysdev->kobj));
397 list_for_each_entry(err_drv, &sysdev_drivers, entry) {
401 err_drv->resume(sysdev);
403 /* resume other sysdevs in current class */
404 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
405 if (err_dev == sysdev)
407 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
408 __sysdev_resume(err_dev);
411 /* resume other classes */
412 list_for_each_entry_continue(cls, &system_subsys.kset.list,
414 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
415 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
416 __sysdev_resume(err_dev);
424 * sysdev_resume - Bring system devices back to life.
426 * Similar to sys_device_suspend(), but we iterate the list forwards
427 * to guarantee that parent devices are resumed before their children.
429 * Note: Interrupts are disabled when called.
432 int sysdev_resume(void)
434 struct sysdev_class * cls;
436 pr_debug("Resuming System Devices\n");
438 list_for_each_entry(cls, &system_subsys.kset.list, kset.kobj.entry) {
439 struct sys_device * sysdev;
441 pr_debug("Resuming type '%s':\n",
442 kobject_name(&cls->kset.kobj));
444 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
445 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
447 __sysdev_resume(sysdev);
454 int __init system_bus_init(void)
456 system_subsys.kset.kobj.parent = &devices_subsys.kset.kobj;
457 return subsystem_register(&system_subsys);
460 EXPORT_SYMBOL_GPL(sysdev_register);
461 EXPORT_SYMBOL_GPL(sysdev_unregister);