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
);
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
,
90 struct sysdev_class
* class = to_sysdev_class(kobj
);
91 struct sysdev_class_attribute
*class_attr
= to_sysdev_class_attr(attr
);
94 return class_attr
->show(class, buffer
);
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
);
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.
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
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 */
182 struct sys_device
*dev
;
183 list_for_each_entry(dev
, &cls
->kset
.list
, kobj
.entry
)
187 list_add_tail(&drv
->entry
, &sysdev_drivers
);
188 up(&sysdev_drivers_lock
);
194 * sysdev_driver_unregister - Remove an auxillary driver.
195 * @cls: Class driver belongs to.
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
);
205 struct sys_device
*dev
;
206 list_for_each_entry(dev
, &cls
->kset
.list
, kobj
.entry
)
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
)
227 struct sysdev_class
* cls
= sysdev
->cls
;
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
);
242 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev
->kobj
));
244 /* Register the object */
245 error
= kobject_register(&sysdev
->kobj
);
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
) {
261 /* Notify class auxillary drivers */
262 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
266 up(&sysdev_drivers_lock
);
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
) {
281 list_for_each_entry(drv
, &sysdev
->cls
->drivers
, entry
) {
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
,
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
) {
327 drv
->shutdown(sysdev
);
330 /* Call auxillary drivers next. */
331 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
333 drv
->shutdown(sysdev
);
336 /* Now call the generic one */
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 */
353 /* Call auxillary drivers next. */
354 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
359 /* Call global drivers. */
360 list_for_each_entry(drv
, &sysdev_drivers
, entry
) {
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
;
386 pr_debug("Suspending System Devices\n");
388 list_for_each_entry_reverse(cls
, &system_subsys
.kset
.list
,
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
) {
400 ret
= drv
->suspend(sysdev
, state
);
406 /* Call auxillary drivers next. */
407 list_for_each_entry(drv
, &cls
->drivers
, entry
) {
409 ret
= drv
->suspend(sysdev
, state
);
415 /* Now call the generic one */
417 ret
= cls
->suspend(sysdev
, state
);
424 /* resume current sysdev */
427 printk(KERN_ERR
"Class suspend failed for %s\n",
428 kobject_name(&sysdev
->kobj
));
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
) {
438 err_drv
->resume(sysdev
);
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
) {
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
)
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
,
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
);
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
);
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
);