blob: 6858178b3aff8d28e727e2d487adf289817324bd [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
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>
Pavel Machek438510f2005-04-16 15:25:24 -070023#include <linux/pm.h>
Adrian Bunkf67d1152006-01-19 17:30:17 +010024#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080025#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Adrian Bunkf67d1152006-01-19 17:30:17 +010027#include "base.h"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029extern struct subsystem devices_subsys;
30
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)
33
34
35static ssize_t
36sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
37{
38 struct sys_device * sysdev = to_sysdev(kobj);
39 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
40
41 if (sysdev_attr->show)
42 return sysdev_attr->show(sysdev, buffer);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050043 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46
47static ssize_t
48sysdev_store(struct kobject * kobj, struct attribute * attr,
49 const char * buffer, size_t count)
50{
51 struct sys_device * sysdev = to_sysdev(kobj);
52 struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);
53
54 if (sysdev_attr->store)
55 return sysdev_attr->store(sysdev, buffer, count);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050056 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59static struct sysfs_ops sysfs_ops = {
60 .show = sysdev_show,
61 .store = sysdev_store,
62};
63
64static struct kobj_type ktype_sysdev = {
65 .sysfs_ops = &sysfs_ops,
66};
67
68
69int sysdev_create_file(struct sys_device * s, struct sysdev_attribute * a)
70{
71 return sysfs_create_file(&s->kobj, &a->attr);
72}
73
74
75void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
76{
77 sysfs_remove_file(&s->kobj, &a->attr);
78}
79
80EXPORT_SYMBOL_GPL(sysdev_create_file);
81EXPORT_SYMBOL_GPL(sysdev_remove_file);
82
Shaohua Li670dd902006-05-08 13:45:57 +080083#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)
86
87static ssize_t sysdev_class_show(struct kobject *kobj, struct attribute *attr,
88 char *buffer)
89{
90 struct sysdev_class * class = to_sysdev_class(kobj);
91 struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);
92
93 if (class_attr->show)
94 return class_attr->show(class, buffer);
95 return -EIO;
96}
97
98static ssize_t sysdev_class_store(struct kobject *kobj, struct attribute *attr,
99 const char *buffer, size_t count)
100{
101 struct sysdev_class * class = to_sysdev_class(kobj);
102 struct sysdev_class_attribute * class_attr = to_sysdev_class_attr(attr);
103
104 if (class_attr->store)
105 return class_attr->store(class, buffer, count);
106 return -EIO;
107}
108
109static struct sysfs_ops sysfs_class_ops = {
110 .show = sysdev_class_show,
111 .store = sysdev_class_store,
112};
113
114static struct kobj_type ktype_sysdev_class = {
115 .sysfs_ops = &sysfs_class_ops,
116};
117
118int sysdev_class_create_file(struct sysdev_class *c,
119 struct sysdev_class_attribute *a)
120{
121 return sysfs_create_file(&c->kset.kobj, &a->attr);
122}
123EXPORT_SYMBOL_GPL(sysdev_class_create_file);
124
125void sysdev_class_remove_file(struct sysdev_class *c,
126 struct sysdev_class_attribute *a)
127{
128 sysfs_remove_file(&c->kset.kobj, &a->attr);
129}
130EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * declare system_subsys
134 */
Shaohua Li670dd902006-05-08 13:45:57 +0800135static decl_subsys(system, &ktype_sysdev_class, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137int sysdev_class_register(struct sysdev_class * cls)
138{
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);
145}
146
147void sysdev_class_unregister(struct sysdev_class * cls)
148{
149 pr_debug("Unregistering sysdev class '%s'\n",
150 kobject_name(&cls->kset.kobj));
151 kset_unregister(&cls->kset);
152}
153
154EXPORT_SYMBOL_GPL(sysdev_class_register);
155EXPORT_SYMBOL_GPL(sysdev_class_unregister);
156
157
158static LIST_HEAD(sysdev_drivers);
159static DECLARE_MUTEX(sysdev_drivers_lock);
160
161/**
162 * sysdev_driver_register - Register auxillary driver
163 * @cls: Device class driver belongs to.
164 * @drv: Driver.
165 *
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
170 * each device.
171 */
172
173int sysdev_driver_register(struct sysdev_class * cls,
174 struct sysdev_driver * drv)
175{
176 down(&sysdev_drivers_lock);
177 if (cls && kset_get(&cls->kset)) {
178 list_add_tail(&drv->entry, &cls->drivers);
179
180 /* If devices of this class already exist, tell the driver */
181 if (drv->add) {
182 struct sys_device *dev;
183 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
184 drv->add(dev);
185 }
186 } else
187 list_add_tail(&drv->entry, &sysdev_drivers);
188 up(&sysdev_drivers_lock);
189 return 0;
190}
191
192
193/**
194 * sysdev_driver_unregister - Remove an auxillary driver.
195 * @cls: Class driver belongs to.
196 * @drv: Driver.
197 */
198void sysdev_driver_unregister(struct sysdev_class * cls,
199 struct sysdev_driver * drv)
200{
201 down(&sysdev_drivers_lock);
202 list_del_init(&drv->entry);
203 if (cls) {
204 if (drv->remove) {
205 struct sys_device *dev;
206 list_for_each_entry(dev, &cls->kset.list, kobj.entry)
207 drv->remove(dev);
208 }
209 kset_put(&cls->kset);
210 }
211 up(&sysdev_drivers_lock);
212}
213
214EXPORT_SYMBOL_GPL(sysdev_driver_register);
215EXPORT_SYMBOL_GPL(sysdev_driver_unregister);
216
217
218
219/**
220 * sysdev_register - add a system device to the tree
221 * @sysdev: device in question
222 *
223 */
224int sysdev_register(struct sys_device * sysdev)
225{
226 int error;
227 struct sysdev_class * cls = sysdev->cls;
228
229 if (!cls)
230 return -EINVAL;
231
232 /* Make sure the kset is set */
233 sysdev->kobj.kset = &cls->kset;
234
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);
239 if (error)
240 return error;
241
242 pr_debug("Registering sys device '%s'\n", kobject_name(&sysdev->kobj));
243
244 /* Register the object */
245 error = kobject_register(&sysdev->kobj);
246
247 if (!error) {
248 struct sysdev_driver * drv;
249
250 down(&sysdev_drivers_lock);
251 /* Generic notification is implicit, because it's that
252 * code that should have called us.
253 */
254
255 /* Notify global drivers */
256 list_for_each_entry(drv, &sysdev_drivers, entry) {
257 if (drv->add)
258 drv->add(sysdev);
259 }
260
261 /* Notify class auxillary drivers */
262 list_for_each_entry(drv, &cls->drivers, entry) {
263 if (drv->add)
264 drv->add(sysdev);
265 }
266 up(&sysdev_drivers_lock);
267 }
268 return error;
269}
270
271void sysdev_unregister(struct sys_device * sysdev)
272{
273 struct sysdev_driver * drv;
274
275 down(&sysdev_drivers_lock);
276 list_for_each_entry(drv, &sysdev_drivers, entry) {
277 if (drv->remove)
278 drv->remove(sysdev);
279 }
280
281 list_for_each_entry(drv, &sysdev->cls->drivers, entry) {
282 if (drv->remove)
283 drv->remove(sysdev);
284 }
285 up(&sysdev_drivers_lock);
286
287 kobject_unregister(&sysdev->kobj);
288}
289
290
291
292/**
293 * sysdev_shutdown - Shut down all system devices.
294 *
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.
299 *
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.
304 */
305
306void sysdev_shutdown(void)
307{
308 struct sysdev_class * cls;
309
310 pr_debug("Shutting Down System Devices\n");
311
312 down(&sysdev_drivers_lock);
313 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
314 kset.kobj.entry) {
315 struct sys_device * sysdev;
316
317 pr_debug("Shutting down type '%s':\n",
318 kobject_name(&cls->kset.kobj));
319
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));
323
324 /* Call global drivers first. */
325 list_for_each_entry(drv, &sysdev_drivers, entry) {
326 if (drv->shutdown)
327 drv->shutdown(sysdev);
328 }
329
330 /* Call auxillary drivers next. */
331 list_for_each_entry(drv, &cls->drivers, entry) {
332 if (drv->shutdown)
333 drv->shutdown(sysdev);
334 }
335
336 /* Now call the generic one */
337 if (cls->shutdown)
338 cls->shutdown(sysdev);
339 }
340 }
341 up(&sysdev_drivers_lock);
342}
343
Shaohua Liceaeade2005-08-11 10:37:39 +0800344static void __sysdev_resume(struct sys_device *dev)
345{
346 struct sysdev_class *cls = dev->cls;
347 struct sysdev_driver *drv;
348
349 /* First, call the class-specific one */
350 if (cls->resume)
351 cls->resume(dev);
352
353 /* Call auxillary drivers next. */
354 list_for_each_entry(drv, &cls->drivers, entry) {
355 if (drv->resume)
356 drv->resume(dev);
357 }
358
359 /* Call global drivers. */
360 list_for_each_entry(drv, &sysdev_drivers, entry) {
361 if (drv->resume)
362 drv->resume(dev);
363 }
364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366/**
367 * sysdev_suspend - Suspend all system devices.
368 * @state: Power state to enter.
369 *
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.
374 *
375 * This is only called by the device PM core, so we let them handle
376 * all synchronization.
377 */
378
Pavel Machek438510f2005-04-16 15:25:24 -0700379int sysdev_suspend(pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct sysdev_class * cls;
Shaohua Liceaeade2005-08-11 10:37:39 +0800382 struct sys_device *sysdev, *err_dev;
383 struct sysdev_driver *drv, *err_drv;
384 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 pr_debug("Suspending System Devices\n");
387
388 list_for_each_entry_reverse(cls, &system_subsys.kset.list,
389 kset.kobj.entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 pr_debug("Suspending type '%s':\n",
392 kobject_name(&cls->kset.kobj));
393
394 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
396
397 /* Call global drivers first. */
398 list_for_each_entry(drv, &sysdev_drivers, entry) {
Shaohua Liceaeade2005-08-11 10:37:39 +0800399 if (drv->suspend) {
400 ret = drv->suspend(sysdev, state);
401 if (ret)
402 goto gbl_driver;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
406 /* Call auxillary drivers next. */
407 list_for_each_entry(drv, &cls->drivers, entry) {
Shaohua Liceaeade2005-08-11 10:37:39 +0800408 if (drv->suspend) {
409 ret = drv->suspend(sysdev, state);
410 if (ret)
411 goto aux_driver;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 /* Now call the generic one */
Shaohua Liceaeade2005-08-11 10:37:39 +0800416 if (cls->suspend) {
417 ret = cls->suspend(sysdev, state);
418 if (ret)
419 goto cls_driver;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 }
423 return 0;
Shaohua Liceaeade2005-08-11 10:37:39 +0800424 /* resume current sysdev */
425cls_driver:
426 drv = NULL;
427 printk(KERN_ERR "Class suspend failed for %s\n",
428 kobject_name(&sysdev->kobj));
429
430aux_driver:
431 if (drv)
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) {
435 if (err_drv == drv)
436 break;
437 if (err_drv->resume)
438 err_drv->resume(sysdev);
439 }
440 drv = NULL;
441
442gbl_driver:
443 if (drv)
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) {
447 if (err_drv == drv)
448 break;
449 if (err_drv->resume)
450 err_drv->resume(sysdev);
451 }
452 /* resume other sysdevs in current class */
453 list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
454 if (err_dev == sysdev)
455 break;
456 pr_debug(" %s\n", kobject_name(&err_dev->kobj));
457 __sysdev_resume(err_dev);
458 }
459
460 /* resume other classes */
461 list_for_each_entry_continue(cls, &system_subsys.kset.list,
462 kset.kobj.entry) {
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);
466 }
467 }
468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471
472/**
473 * sysdev_resume - Bring system devices back to life.
474 *
475 * Similar to sys_device_suspend(), but we iterate the list forwards
476 * to guarantee that parent devices are resumed before their children.
477 *
478 * Note: Interrupts are disabled when called.
479 */
480
481int sysdev_resume(void)
482{
483 struct sysdev_class * cls;
484
485 pr_debug("Resuming System Devices\n");
486
487 list_for_each_entry(cls, &system_subsys.kset.list, kset.kobj.entry) {
488 struct sys_device * sysdev;
489
490 pr_debug("Resuming type '%s':\n",
491 kobject_name(&cls->kset.kobj));
492
493 list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 pr_debug(" %s\n", kobject_name(&sysdev->kobj));
495
Shaohua Liceaeade2005-08-11 10:37:39 +0800496 __sysdev_resume(sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 }
499 return 0;
500}
501
502
503int __init system_bus_init(void)
504{
505 system_subsys.kset.kobj.parent = &devices_subsys.kset.kobj;
506 return subsystem_register(&system_subsys);
507}
508
509EXPORT_SYMBOL_GPL(sysdev_register);
510EXPORT_SYMBOL_GPL(sysdev_unregister);