blob: ac7ff6d0c6e5c34460cfd1fcf4d76678b3e0533f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sys.c - pseudo-bus for system 'devices' (cpus, PICs, timers, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * 2002-3 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
8 *
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.
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Pavel Machek438510f2005-04-16 15:25:24 -070022#include <linux/pm.h>
Adrian Bunkf67d1152006-01-19 17:30:17 +010023#include <linux/device.h>
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -070024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Adrian Bunkf67d1152006-01-19 17:30:17 +010026#include "base.h"
27
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070028extern struct kset devices_subsys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
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)
32
33
34static ssize_t
35sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
36{
37 struct sys_device * sysdev = to_sysdev(kobj);
38 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
39
40 if (sysdev_attr->show)
41 return sysdev_attr->show(sysdev, buffer);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050042 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45
46static ssize_t
47sysdev_store(struct kobject * kobj, struct attribute * attr,
48 const char * buffer, size_t count)
49{
50 struct sys_device * sysdev = to_sysdev(kobj);
51 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
52
53 if (sysdev_attr->store)
54 return sysdev_attr->store(sysdev, buffer, count);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050055 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58static struct sysfs_ops sysfs_ops = {
59 .show = sysdev_show,
60 .store = sysdev_store,
61};
62
63static struct kobj_type ktype_sysdev = {
64 .sysfs_ops = &sysfs_ops,
65};
66
67
68int sysdev_create_file(struct sys_device * s, struct sysdev_attribute * a)
69{
70 return sysfs_create_file(&s->kobj, &a->attr);
71}
72
73
74void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
75{
76 sysfs_remove_file(&s->kobj, &a->attr);
77}
78
79EXPORT_SYMBOL_GPL(sysdev_create_file);
80EXPORT_SYMBOL_GPL(sysdev_remove_file);
81
Shaohua Li670dd902006-05-08 13:45:57 +080082#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)
85
86static ssize_t sysdev_class_show(struct kobject *kobj, struct attribute *attr,
87 char *buffer)
88{
89 struct sysdev_class * class = to_sysdev_class(kobj);
90 struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);
91
92 if (class_attr->show)
93 return class_attr->show(class, buffer);
94 return -EIO;
95}
96
97static ssize_t sysdev_class_store(struct kobject *kobj, struct attribute *attr,
98 const char *buffer, size_t count)
99{
100 struct sysdev_class * class = to_sysdev_class(kobj);
101 struct sysdev_class_attribute * class_attr = to_sysdev_class_attr(attr);
102
103 if (class_attr->store)
104 return class_attr->store(class, buffer, count);
105 return -EIO;
106}
107
108static struct sysfs_ops sysfs_class_ops = {
109 .show = sysdev_class_show,
110 .store = sysdev_class_store,
111};
112
113static struct kobj_type ktype_sysdev_class = {
114 .sysfs_ops = &sysfs_class_ops,
115};
116
117int sysdev_class_create_file(struct sysdev_class *c,
118 struct sysdev_class_attribute *a)
119{
120 return sysfs_create_file(&c->kset.kobj, &a->attr);
121}
122EXPORT_SYMBOL_GPL(sysdev_class_create_file);
123
124void sysdev_class_remove_file(struct sysdev_class *c,
125 struct sysdev_class_attribute *a)
126{
127 sysfs_remove_file(&c->kset.kobj, &a->attr);
128}
129EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
132 * declare system_subsys
133 */
Shaohua Li670dd902006-05-08 13:45:57 +0800134static decl_subsys(system, &ktype_sysdev_class, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136int sysdev_class_register(struct sysdev_class * cls)
137{
138 pr_debug("Registering sysdev class '%s'\n",
139 kobject_name(&cls->kset.kobj));
140 INIT_LIST_HEAD(&cls->drivers);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700141 cls->kset.kobj.parent = &system_subsys.kobj;
Greg Kroah-Hartman27f20e52007-09-12 15:06:57 -0700142 cls->kset.kobj.kset = &system_subsys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return kset_register(&cls->kset);
144}
145
146void sysdev_class_unregister(struct sysdev_class * cls)
147{
148 pr_debug("Unregistering sysdev class '%s'\n",
149 kobject_name(&cls->kset.kobj));
150 kset_unregister(&cls->kset);
151}
152
153EXPORT_SYMBOL_GPL(sysdev_class_register);
154EXPORT_SYMBOL_GPL(sysdev_class_unregister);
155
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700156static DEFINE_MUTEX(sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/**
159 * sysdev_driver_register - Register auxillary driver
Akinobu Mita44b760a2007-08-19 16:51:14 +0900160 * @cls: Device class driver belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * @drv: Driver.
162 *
Akinobu Mita44b760a2007-08-19 16:51:14 +0900163 * @drv is inserted into @cls->drivers to be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * called on each operation on devices of that class. The refcount
165 * of @cls is incremented.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167
Akinobu Mita44b760a2007-08-19 16:51:14 +0900168int sysdev_driver_register(struct sysdev_class *cls, struct sysdev_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Akinobu Mita44b760a2007-08-19 16:51:14 +0900170 int err = 0;
171
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700172 mutex_lock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (cls && kset_get(&cls->kset)) {
174 list_add_tail(&drv->entry, &cls->drivers);
175
176 /* If devices of this class already exist, tell the driver */
177 if (drv->add) {
178 struct sys_device *dev;
179 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
180 drv->add(dev);
181 }
Akinobu Mita44b760a2007-08-19 16:51:14 +0900182 } else {
183 err = -EINVAL;
184 printk(KERN_ERR "%s: invalid device class\n", __FUNCTION__);
185 WARN_ON(1);
186 }
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700187 mutex_unlock(&sysdev_drivers_lock);
Akinobu Mita44b760a2007-08-19 16:51:14 +0900188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191
192/**
193 * sysdev_driver_unregister - Remove an auxillary driver.
194 * @cls: Class driver belongs to.
195 * @drv: Driver.
196 */
197void sysdev_driver_unregister(struct sysdev_class * cls,
198 struct sysdev_driver * drv)
199{
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700200 mutex_lock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 list_del_init(&drv->entry);
202 if (cls) {
203 if (drv->remove) {
204 struct sys_device *dev;
205 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
206 drv->remove(dev);
207 }
208 kset_put(&cls->kset);
209 }
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700210 mutex_unlock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213EXPORT_SYMBOL_GPL(sysdev_driver_register);
214EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
215
216
217
218/**
219 * sysdev_register - add a system device to the tree
220 * @sysdev: device in question
221 *
222 */
223int sysdev_register(struct sys_device * sysdev)
224{
225 int error;
226 struct sysdev_class * cls = sysdev->cls;
227
228 if (!cls)
229 return -EINVAL;
230
231 /* Make sure the kset is set */
232 sysdev->kobj.kset = &cls->kset;
233
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);
238 if (error)
239 return error;
240
241 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
242
243 /* Register the object */
244 error = kobject_register(&sysdev->kobj);
245
246 if (!error) {
247 struct sysdev_driver * drv;
248
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700249 mutex_lock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Generic notification is implicit, because it's that
251 * code that should have called us.
252 */
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Notify class auxillary drivers */
255 list_for_each_entry(drv, &cls->drivers, entry) {
256 if (drv->add)
257 drv->add(sysdev);
258 }
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700259 mutex_unlock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 return error;
262}
263
264void sysdev_unregister(struct sys_device * sysdev)
265{
266 struct sysdev_driver * drv;
267
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700268 mutex_lock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
270 if (drv->remove)
271 drv->remove(sysdev);
272 }
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700273 mutex_unlock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 kobject_unregister(&sysdev->kobj);
276}
277
278
279
280/**
281 * sysdev_shutdown - Shut down all system devices.
282 *
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
Akinobu Mita44b760a2007-08-19 16:51:14 +0900285 * each driver registered for the device - the auxillaries,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * and the class driver.
287 *
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.
292 */
293
294void sysdev_shutdown(void)
295{
296 struct sysdev_class * cls;
297
298 pr_debug("Shutting Down System Devices\n");
299
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700300 mutex_lock(&sysdev_drivers_lock);
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700301 list_for_each_entry_reverse(cls, &system_subsys.list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 kset.kobj.entry) {
303 struct sys_device * sysdev;
304
305 pr_debug("Shutting down type '%s':\n",
306 kobject_name(&cls->kset.kobj));
307
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));
311
Akinobu Mita44b760a2007-08-19 16:51:14 +0900312 /* Call auxillary drivers first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 list_for_each_entry(drv, &cls->drivers, entry) {
314 if (drv->shutdown)
315 drv->shutdown(sysdev);
316 }
317
318 /* Now call the generic one */
319 if (cls->shutdown)
320 cls->shutdown(sysdev);
321 }
322 }
Matthias Kaehlcke9f3f7762007-05-23 14:19:42 -0700323 mutex_unlock(&sysdev_drivers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Shaohua Liceaeade2005-08-11 10:37:39 +0800326static void __sysdev_resume(struct sys_device *dev)
327{
328 struct sysdev_class *cls = dev->cls;
329 struct sysdev_driver *drv;
330
331 /* First, call the class-specific one */
332 if (cls->resume)
333 cls->resume(dev);
334
335 /* Call auxillary drivers next. */
336 list_for_each_entry(drv, &cls->drivers, entry) {
337 if (drv->resume)
338 drv->resume(dev);
339 }
Shaohua Liceaeade2005-08-11 10:37:39 +0800340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342/**
343 * sysdev_suspend - Suspend all system devices.
344 * @state: Power state to enter.
345 *
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.
350 *
351 * This is only called by the device PM core, so we let them handle
352 * all synchronization.
353 */
354
Pavel Machek438510f2005-04-16 15:25:24 -0700355int sysdev_suspend(pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct sysdev_class * cls;
Shaohua Liceaeade2005-08-11 10:37:39 +0800358 struct sys_device *sysdev, *err_dev;
359 struct sysdev_driver *drv, *err_drv;
360 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 pr_debug("Suspending System Devices\n");
363
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700364 list_for_each_entry_reverse(cls, &system_subsys.list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 kset.kobj.entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 pr_debug("Suspending type '%s':\n",
368 kobject_name(&cls->kset.kobj));
369
370 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
372
Akinobu Mita44b760a2007-08-19 16:51:14 +0900373 /* Call auxillary drivers first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 list_for_each_entry(drv, &cls->drivers, entry) {
Shaohua Liceaeade2005-08-11 10:37:39 +0800375 if (drv->suspend) {
376 ret = drv->suspend(sysdev, state);
377 if (ret)
378 goto aux_driver;
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 /* Now call the generic one */
Shaohua Liceaeade2005-08-11 10:37:39 +0800383 if (cls->suspend) {
384 ret = cls->suspend(sysdev, state);
385 if (ret)
386 goto cls_driver;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 }
390 return 0;
Shaohua Liceaeade2005-08-11 10:37:39 +0800391 /* resume current sysdev */
392cls_driver:
393 drv = NULL;
394 printk(KERN_ERR "Class suspend failed for %s\n",
395 kobject_name(&sysdev->kobj));
396
397aux_driver:
398 if (drv)
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) {
402 if (err_drv == drv)
403 break;
404 if (err_drv->resume)
405 err_drv->resume(sysdev);
406 }
Shaohua Liceaeade2005-08-11 10:37:39 +0800407
Shaohua Liceaeade2005-08-11 10:37:39 +0800408 /* resume other sysdevs in current class */
409 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
410 if (err_dev == sysdev)
411 break;
412 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
413 __sysdev_resume(err_dev);
414 }
415
416 /* resume other classes */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700417 list_for_each_entry_continue(cls, &system_subsys.list,
Shaohua Liceaeade2005-08-11 10:37:39 +0800418 kset.kobj.entry) {
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);
422 }
423 }
424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427
428/**
429 * sysdev_resume - Bring system devices back to life.
430 *
431 * Similar to sys_device_suspend(), but we iterate the list forwards
432 * to guarantee that parent devices are resumed before their children.
433 *
434 * Note: Interrupts are disabled when called.
435 */
436
437int sysdev_resume(void)
438{
439 struct sysdev_class * cls;
440
441 pr_debug("Resuming System Devices\n");
442
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700443 list_for_each_entry(cls, &system_subsys.list, kset.kobj.entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct sys_device * sysdev;
445
446 pr_debug("Resuming type '%s':\n",
447 kobject_name(&cls->kset.kobj));
448
449 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
451
Shaohua Liceaeade2005-08-11 10:37:39 +0800452 __sysdev_resume(sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
455 return 0;
456}
457
458
459int __init system_bus_init(void)
460{
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700461 system_subsys.kobj.parent = &devices_subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return subsystem_register(&system_subsys);
463}
464
465EXPORT_SYMBOL_GPL(sysdev_register);
466EXPORT_SYMBOL_GPL(sysdev_unregister);