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/sysdev.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
23 #include <linux/device.h>
24 #include <linux/mutex.h>
28 extern struct kset devices_subsys
;
30 #define to_sysdev(k) container_of(k, struct sys_device, kobj)
31 #define to_sysdev_attr(a) container_of(a, struct sysdev_attribute, attr)
35 sysdev_show(struct kobject
* kobj
, struct attribute
* attr
, char * buffer
)
37 struct sys_device
* sysdev
= to_sysdev(kobj
);
38 struct sysdev_attribute
* sysdev_attr
= to_sysdev_attr(attr
);
40 if (sysdev_attr
->show
)
41 return sysdev_attr
->show(sysdev
, buffer
);
47 sysdev_store(struct kobject
* kobj
, struct attribute
* attr
,
48 const char * buffer
, size_t count
)
50 struct sys_device
* sysdev
= to_sysdev(kobj
);
51 struct sysdev_attribute
* sysdev_attr
= to_sysdev_attr(attr
);
53 if (sysdev_attr
->store
)
54 return sysdev_attr
->store(sysdev
, buffer
, count
);
58 static struct sysfs_ops sysfs_ops
= {
60 .store
= sysdev_store
,
63 static struct kobj_type ktype_sysdev
= {
64 .sysfs_ops
= &sysfs_ops
,
68 int sysdev_create_file(struct sys_device
* s
, struct sysdev_attribute
* a
)
70 return sysfs_create_file(&s
->kobj
, &a
->attr
);
74 void sysdev_remove_file(struct sys_device
* s
, struct sysdev_attribute
* a
)
76 sysfs_remove_file(&s
->kobj
, &a
->attr
);
79 EXPORT_SYMBOL_GPL(sysdev_create_file
);
80 EXPORT_SYMBOL_GPL(sysdev_remove_file
);
82 #define to_sysdev_class(k) container_of(k, struct sysdev_class, kset.kobj)
83 #define to_sysdev_class_attr(a) container_of(a, \
84 struct sysdev_class_attribute, attr)
86 static ssize_t
sysdev_class_show(struct kobject
*kobj
, struct attribute
*attr
,
89 struct sysdev_class
* class = to_sysdev_class(kobj
);
90 struct sysdev_class_attribute
*class_attr
= to_sysdev_class_attr(attr
);
93 return class_attr
->show(class, buffer
);
97 static ssize_t
sysdev_class_store(struct kobject
*kobj
, struct attribute
*attr
,
98 const char *buffer
, size_t count
)
100 struct sysdev_class
* class = to_sysdev_class(kobj
);
101 struct sysdev_class_attribute
* class_attr
= to_sysdev_class_attr(attr
);
103 if (class_attr
->store
)
104 return class_attr
->store(class, buffer
, count
);
108 static struct sysfs_ops sysfs_class_ops
= {
109 .show
= sysdev_class_show
,
110 .store
= sysdev_class_store
,
113 static struct kobj_type ktype_sysdev_class
= {
114 .sysfs_ops
= &sysfs_class_ops
,
117 int sysdev_class_create_file(struct sysdev_class
*c
,
118 struct sysdev_class_attribute
*a
)
120 return sysfs_create_file(&c
->kset
.kobj
, &a
->attr
);
122 EXPORT_SYMBOL_GPL(sysdev_class_create_file
);
124 void sysdev_class_remove_file(struct sysdev_class
*c
,
125 struct sysdev_class_attribute
*a
)
127 sysfs_remove_file(&c
->kset
.kobj
, &a
->attr
);
129 EXPORT_SYMBOL_GPL(sysdev_class_remove_file
);
132 * declare system_subsys
134 static decl_subsys(system
, &ktype_sysdev_class
, NULL
);
136 int sysdev_class_register(struct sysdev_class
* cls
)
138 pr_debug("Registering sysdev class '%s'\n",
139 kobject_name(&cls
->kset
.kobj
));
140 INIT_LIST_HEAD(&cls
->drivers
);
141 cls
->kset
.kobj
.parent
= &system_subsys
.kobj
;
142 cls
->kset
.kobj
.kset
= &system_subsys
;
143 return kset_register(&cls
->kset
);
146 void sysdev_class_unregister(struct sysdev_class
* cls
)
148 pr_debug("Unregistering sysdev class '%s'\n",
149 kobject_name(&cls
->kset
.kobj
));
150 kset_unregister(&cls
->kset
);
153 EXPORT_SYMBOL_GPL(sysdev_class_register
);
154 EXPORT_SYMBOL_GPL(sysdev_class_unregister
);
156 static DEFINE_MUTEX(sysdev_drivers_lock
);
159 * sysdev_driver_register - Register auxillary driver
160 * @cls: Device class driver belongs to.
163 * @drv is inserted into @cls->drivers to be
164 * called on each operation on devices of that class. The refcount
165 * of @cls is incremented.
168 int sysdev_driver_register(struct sysdev_class
*cls
, struct sysdev_driver
*drv
)
172 mutex_lock(&sysdev_drivers_lock
);
173 if (cls
&& kset_get(&cls
->kset
)) {
174 list_add_tail(&drv
->entry
, &cls
->drivers
);
176 /* If devices of this class already exist, tell the driver */
178 struct sys_device
*dev
;
179 list_for_each_entry(dev
, &cls
->kset
.list
, kobj
.entry
)
184 printk(KERN_ERR
"%s: invalid device class\n", __FUNCTION__
);
187 mutex_unlock(&sysdev_drivers_lock
);
193 * sysdev_driver_unregister - Remove an auxillary driver.
194 * @cls: Class driver belongs to.
197 void sysdev_driver_unregister(struct sysdev_class
* cls
,
198 struct sysdev_driver
* drv
)
200 mutex_lock(&sysdev_drivers_lock
);
201 list_del_init(&drv
->entry
);
204 struct sys_device
*dev
;
205 list_for_each_entry(dev
, &cls
->kset
.list
, kobj
.entry
)
208 kset_put(&cls
->kset
);
210 mutex_unlock(&sysdev_drivers_lock
);
213 EXPORT_SYMBOL_GPL(sysdev_driver_register
);
214 EXPORT_SYMBOL_GPL(sysdev_driver_unregister
);
219 * sysdev_register - add a system device to the tree
220 * @sysdev: device in question
223 int sysdev_register(struct sys_device
* sysdev
)
226 struct sysdev_class
* cls
= sysdev
->cls
;
231 /* Make sure the kset is set */
232 sysdev
->kobj
.kset
= &cls
->kset
;
234 /* But make sure we point to the right type for sysfs translation */
235 sysdev
->kobj
.ktype
= &ktype_sysdev
;
236 error
= kobject_set_name(&sysdev
->kobj
, "%s%d",
237 kobject_name(&cls
->kset
.kobj
), sysdev
->id
);
241 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev
->kobj
));
243 /* Register the object */
244 error
= kobject_register(&sysdev
->kobj
);
247 struct sysdev_driver
* drv
;
249 mutex_lock(&sysdev_drivers_lock
);
250 /* Generic notification is implicit, because it's that
251 * code that should have called us.
254 /* Notify class auxillary drivers */
255 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
259 mutex_unlock(&sysdev_drivers_lock
);
264 void sysdev_unregister(struct sys_device
* sysdev
)
266 struct sysdev_driver
* drv
;
268 mutex_lock(&sysdev_drivers_lock
);
269 list_for_each_entry(drv
, &sysdev
->cls
->drivers
, entry
) {
273 mutex_unlock(&sysdev_drivers_lock
);
275 kobject_unregister(&sysdev
->kobj
);
281 * sysdev_shutdown - Shut down all system devices.
283 * Loop over each class of system devices, and the devices in each
284 * of those classes. For each device, we call the shutdown method for
285 * each driver registered for the device - the auxillaries,
286 * and the class driver.
288 * Note: The list is iterated in reverse order, so that we shut down
289 * child devices before we shut down thier parents. The list ordering
290 * is guaranteed by virtue of the fact that child devices are registered
291 * after their parents.
294 void sysdev_shutdown(void)
296 struct sysdev_class
* cls
;
298 pr_debug("Shutting Down System Devices\n");
300 mutex_lock(&sysdev_drivers_lock
);
301 list_for_each_entry_reverse(cls
, &system_subsys
.list
,
303 struct sys_device
* sysdev
;
305 pr_debug("Shutting down type '%s':\n",
306 kobject_name(&cls
->kset
.kobj
));
308 list_for_each_entry(sysdev
, &cls
->kset
.list
, kobj
.entry
) {
309 struct sysdev_driver
* drv
;
310 pr_debug(" %s\n", kobject_name(&sysdev
->kobj
));
312 /* Call auxillary drivers first */
313 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
315 drv
->shutdown(sysdev
);
318 /* Now call the generic one */
320 cls
->shutdown(sysdev
);
323 mutex_unlock(&sysdev_drivers_lock
);
326 static void __sysdev_resume(struct sys_device
*dev
)
328 struct sysdev_class
*cls
= dev
->cls
;
329 struct sysdev_driver
*drv
;
331 /* First, call the class-specific one */
335 /* Call auxillary drivers next. */
336 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
343 * sysdev_suspend - Suspend all system devices.
344 * @state: Power state to enter.
346 * We perform an almost identical operation as sys_device_shutdown()
347 * above, though calling ->suspend() instead. Interrupts are disabled
348 * when this called. Devices are responsible for both saving state and
349 * quiescing or powering down the device.
351 * This is only called by the device PM core, so we let them handle
352 * all synchronization.
355 int sysdev_suspend(pm_message_t state
)
357 struct sysdev_class
* cls
;
358 struct sys_device
*sysdev
, *err_dev
;
359 struct sysdev_driver
*drv
, *err_drv
;
362 pr_debug("Suspending System Devices\n");
364 list_for_each_entry_reverse(cls
, &system_subsys
.list
,
367 pr_debug("Suspending type '%s':\n",
368 kobject_name(&cls
->kset
.kobj
));
370 list_for_each_entry(sysdev
, &cls
->kset
.list
, kobj
.entry
) {
371 pr_debug(" %s\n", kobject_name(&sysdev
->kobj
));
373 /* Call auxillary drivers first */
374 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
376 ret
= drv
->suspend(sysdev
, state
);
382 /* Now call the generic one */
384 ret
= cls
->suspend(sysdev
, state
);
391 /* resume current sysdev */
394 printk(KERN_ERR
"Class suspend failed for %s\n",
395 kobject_name(&sysdev
->kobj
));
399 printk(KERN_ERR
"Class driver suspend failed for %s\n",
400 kobject_name(&sysdev
->kobj
));
401 list_for_each_entry(err_drv
, &cls
->drivers
, entry
) {
405 err_drv
->resume(sysdev
);
408 /* resume other sysdevs in current class */
409 list_for_each_entry(err_dev
, &cls
->kset
.list
, kobj
.entry
) {
410 if (err_dev
== sysdev
)
412 pr_debug(" %s\n", kobject_name(&err_dev
->kobj
));
413 __sysdev_resume(err_dev
);
416 /* resume other classes */
417 list_for_each_entry_continue(cls
, &system_subsys
.list
,
419 list_for_each_entry(err_dev
, &cls
->kset
.list
, kobj
.entry
) {
420 pr_debug(" %s\n", kobject_name(&err_dev
->kobj
));
421 __sysdev_resume(err_dev
);
429 * sysdev_resume - Bring system devices back to life.
431 * Similar to sys_device_suspend(), but we iterate the list forwards
432 * to guarantee that parent devices are resumed before their children.
434 * Note: Interrupts are disabled when called.
437 int sysdev_resume(void)
439 struct sysdev_class
* cls
;
441 pr_debug("Resuming System Devices\n");
443 list_for_each_entry(cls
, &system_subsys
.list
, kset
.kobj
.entry
) {
444 struct sys_device
* sysdev
;
446 pr_debug("Resuming type '%s':\n",
447 kobject_name(&cls
->kset
.kobj
));
449 list_for_each_entry(sysdev
, &cls
->kset
.list
, kobj
.entry
) {
450 pr_debug(" %s\n", kobject_name(&sysdev
->kobj
));
452 __sysdev_resume(sysdev
);
459 int __init
system_bus_init(void)
461 system_subsys
.kobj
.parent
= &devices_subsys
.kobj
;
462 return subsystem_register(&system_subsys
);
465 EXPORT_SYMBOL_GPL(sysdev_register
);
466 EXPORT_SYMBOL_GPL(sysdev_unregister
);