[SPARC64]: Set %gl to 1 in kvmap_itlb_longpath on SUN4V.
[firewire-audio.git] / drivers / base / sys.c
blob6fc23ab127bd52abd01f10eb55136a7c3057a12b
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);
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.
115 * @drv: Driver.
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
121 * each device.
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 */
132 if (drv->add) {
133 struct sys_device *dev;
134 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
135 drv->add(dev);
137 } else
138 list_add_tail(&drv->entry, &sysdev_drivers);
139 up(&sysdev_drivers_lock);
140 return 0;
145 * sysdev_driver_unregister - Remove an auxillary driver.
146 * @cls: Class driver belongs to.
147 * @drv: Driver.
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);
154 if (cls) {
155 if (drv->remove) {
156 struct sys_device *dev;
157 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
158 drv->remove(dev);
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)
177 int error;
178 struct sysdev_class * cls = sysdev->cls;
180 if (!cls)
181 return -EINVAL;
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);
190 if (error)
191 return error;
193 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
195 /* Register the object */
196 error = kobject_register(&sysdev->kobj);
198 if (!error) {
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) {
208 if (drv->add)
209 drv->add(sysdev);
212 /* Notify class auxillary drivers */
213 list_for_each_entry(drv, &cls->drivers, entry) {
214 if (drv->add)
215 drv->add(sysdev);
217 up(&sysdev_drivers_lock);
219 return error;
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) {
228 if (drv->remove)
229 drv->remove(sysdev);
232 list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
233 if (drv->remove)
234 drv->remove(sysdev);
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,
265 kset.kobj.entry) {
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) {
277 if (drv->shutdown)
278 drv->shutdown(sysdev);
281 /* Call auxillary drivers next. */
282 list_for_each_entry(drv, &cls->drivers, entry) {
283 if (drv->shutdown)
284 drv->shutdown(sysdev);
287 /* Now call the generic one */
288 if (cls->shutdown)
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 */
301 if (cls->resume)
302 cls->resume(dev);
304 /* Call auxillary drivers next. */
305 list_for_each_entry(drv, &cls->drivers, entry) {
306 if (drv->resume)
307 drv->resume(dev);
310 /* Call global drivers. */
311 list_for_each_entry(drv, &sysdev_drivers, entry) {
312 if (drv->resume)
313 drv->resume(dev);
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;
335 int ret;
337 pr_debug("Suspending System Devices\n");
339 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
340 kset.kobj.entry) {
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) {
350 if (drv->suspend) {
351 ret = drv->suspend(sysdev, state);
352 if (ret)
353 goto gbl_driver;
357 /* Call auxillary drivers next. */
358 list_for_each_entry(drv, &cls->drivers, entry) {
359 if (drv->suspend) {
360 ret = drv->suspend(sysdev, state);
361 if (ret)
362 goto aux_driver;
366 /* Now call the generic one */
367 if (cls->suspend) {
368 ret = cls->suspend(sysdev, state);
369 if (ret)
370 goto cls_driver;
374 return 0;
375 /* resume current sysdev */
376 cls_driver:
377 drv = NULL;
378 printk(KERN_ERR "Class suspend failed for %s\n",
379 kobject_name(&sysdev->kobj));
381 aux_driver:
382 if (drv)
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) {
386 if (err_drv == drv)
387 break;
388 if (err_drv->resume)
389 err_drv->resume(sysdev);
391 drv = NULL;
393 gbl_driver:
394 if (drv)
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) {
398 if (err_drv == drv)
399 break;
400 if (err_drv->resume)
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)
406 break;
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,
413 kset.kobj.entry) {
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);
419 return ret;
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);
450 return 0;
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);